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.


int main( int argc, char* argv[] ){

	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;

	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);



	return 0;

}//End main


void EventLoop(void)
    SDL_Event event;
    bool done = false;

    while((!done) /*&& (SDL_WaitEvent(&event))*/) {


                switch(event.type) {
			case SDL_KEYDOWN:
                          if(event.key.keysym.sym == SDLK_ESCAPE)
					done = true;
        }   // End switch
    }   // End while
}//End EventLoop


void GLRendering(){


        //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


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

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 :slight_smile:

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…

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.


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:

	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

	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

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.

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