Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: Very beginnner questions with OpenGL in SDL

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2008
    Posts
    9

    Very beginnner questions with OpenGL in SDL

    I'm using code from Nehe's tutorial, lesson 2, and a little from another SDL/OpenGL tutorial to learn how to use OpenGL, but I'd prefer to use the SDL library in replacement of having to use Win32 code and such.

    The problem that I'm having here is that I should be rendering this:


    But instead, I'm rendering nothing... until I comment out a glTranslatef line, then I'm rendering this:




    It's really weird, like it's drawing to the whole screen (I have a widescreen, thus it's stretched and proportions are wrong).

    Anyway, here's my code. I've tried toying with it, and comparing it to NeHe's and other SDL/OpenGL tutorials, but it just doesn't seem to work correctly. There's nothing here very complex, so I'm hoping it'll be very easy for you guys to spot.


    Main:
    Code :
    int main( int argc, char* argv[] ){
     
            SDL_Init(SDL_INIT_EVERYTHING);
     
    	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
     
    	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
    	SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
    	SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE,8 );
    	SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32 );
     
     
    	SDL_Surface* drawContext;
    	Uint32 flags;
     
    	flags = SDL_OPENGL | SDL_FULLSCREEN | SDL_HWSURFACE;
    	drawContext = SDL_SetVideoMode(1024, 768, 0, flags);
     
     
    	glShadeModel(GL_SMOOTH);						// Enables Smooth Shading
    	glDepthFunc(GL_LEQUAL);							// The Type Of Depth Test To Do
    	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    	glClearDepth(1.0);
    	glEnable(GL_DEPTH_TEST);
    	glMatrixMode(GL_PROJECTION);
    	glMatrixMode(GL_MODELVIEW);
     
     
    	EventLoop();
     
    	SDL_Quit();
     
    	return 0;
     
    }//End main

    EventLoop():
    Code :
    void EventLoop(void)
    {
        SDL_Event event;
        bool done = false;
     
     
        while((!done) /*&& (SDL_WaitEvent(&event))*/) {
     
    		GLRendering();
     
     
    		SDL_PollEvent(&event);
                    switch(event.type) {
    ////////////////////////////////////                
    			case SDL_KEYDOWN:
                              if(event.key.keysym.sym == SDLK_ESCAPE)
    					done = true;
     
    				break;
    //////////////////////////////////////                
                default:
                    break;
            }   // End switch
     
        }   // End while
     
    }//End EventLoop

    GLRendering():
    Code :
    void GLRendering(){
     
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();
     
            //The Line I had to comment out.
    //	glTranslatef(-1.5f,0.0f,-6.0f);					// Move Left 1.5 Units And Into The Screen 6.0
     
     
    	glBegin(GL_TRIANGLES);						// Drawing Using Triangles
    		glVertex3f( 0.0f, 1.0f, 0.0f);				// Top
    		glVertex3f(-1.0f,-1.0f, 0.0f);				// Bottom Left
    		glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right
    	glEnd();							// Finished Drawing The Triangle
     
    	glTranslatef(3.0f,0.0f,0.0f);					// Move Right 3 Units
     
    	glBegin(GL_QUADS);						// Draw A Quad
    		glVertex3f(-1.0f, 1.0f, 0.0f);				// Top Left
    		glVertex3f( 1.0f, 1.0f, 0.0f);				// Top Right
    		glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right
    		glVertex3f(-1.0f,-1.0f, 0.0f);				// Bottom Left
    	glEnd();							// Done Drawing The Quad
     
     
    	SDL_GL_SwapBuffers();
    }

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Dec 2000
    Location
    Reutlingen, Germany
    Posts
    2,052

    Re: Very beginnner problem with OpenGL in SDL

    Seems you have moved the camera forwards, so that the polygons are actually behind you, thus you can't see them.

    Try
    glTranslatef (-1.5f,0.0f,6.0f);

    (switched sign in the z-coordinate)

    It's often a bit confusing, which calls you need to do in which order (glTranslate and glRotate) and which signs to use. Often it is not intuitive and you just need to play around with the signs to find out, which combination yields the desired results.

    Hope that helps, welcome to OpenGL :-)
    Jan.
    GLIM - Immediate Mode Emulation for GL3

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2008
    Posts
    9

    Re: Very beginnner problem with OpenGL in SDL

    Nah, that didn't work. It still shows a blank screen. It seems as though the depth can only be a value between -1.0 and 1.0, otherwise the screen is blank.

    Also weird, this just started happening with the line...

    glTranslatef (-1.5f,0.0f,-1.0f);

    (Can't see it with the white background on these forums, but it's partially drawing the box on the right of the screen).


    Any number above 1.0 or below -1.0 shows nothing still. So yeah, it probably is zoomed in, but I don't know how to make it zoom out...

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Dec 2000
    Location
    Reutlingen, Germany
    Posts
    2,052

    Re: Very beginnner problem with OpenGL in SDL

    Do you set up your camera somewhere in your code, or is the code you posted all?

    You should have a gluPerspective-call somewhere, where you set up your near and far clipping plane. Otherwise it is indeed possible, that only objects with a distance <= 1 to you, will be drawn, since every thing else is clipped.

    Jan.
    GLIM - Immediate Mode Emulation for GL3

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2008
    Posts
    9

    Re: Very beginnner problem with OpenGL in SDL

    Okay, so I added the instruction, but that didn't solve it, for some reason...

    However, what's really weird is I copy/pasted the instructions of example code of Lesson 2 straight into the program, making it look as such:

    Code :
    	int width = 1024, height = 768;
     
    	glViewport(0,0,width,height);						// Reset The Current Viewport
     
    	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
    	glLoadIdentity();									// Reset The Projection Matrix
     
    	// Calculate The Aspect Ratio Of The Window
    	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
     
    	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
    	glLoadIdentity();	// Reset The Modelview Matrix
     
     
     
    	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
    	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
    	glClearDepth(1.0f);									// Depth Buffer Setup
    	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
    	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations

    And now, it's working for some reason wheras it didn't before... but they're the same instructions done in a different order.

    So I guess you have to do them in a particular order then? Must've been my problem all along... whoops... x_x

  6. #6
    Junior Member Regular Contributor
    Join Date
    Feb 2006
    Location
    greece
    Posts
    183

    Re: Very beginnner problem with OpenGL in SDL

    It's only natural. Without glFrustum your viewing volume is the unit cube(you can only view objects with -1 <= x <= 1, -1 <= y <= 1, -1 <= z <= 1).
    Thus any translation with absolute z > 1 makes objects disappear(actually, it clips any vertices with z = 0 in the objects). The glFrustum call redifines your viewing volume to start at z = -0.1 and end at z = -100. So now when you translate your objects at -6.0 it is within the viewing volume and thus can be seen. I hope it helped.
    while(1){keyboardsolo(FORTE, BPM_190);}

  7. #7
    Junior Member Newbie
    Join Date
    Aug 2007
    Location
    USA
    Posts
    9

    Re: Very beginnner problem with OpenGL in SDL

    Remember that OpenGL is (essentially) a state machine, so yes, the order of function calls is important.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •