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:
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():
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():
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();
}