showing a simple cube!!!

Started by
2 comments, last by Brother Bob 16 years, 8 months ago
why doesn't this code show anything on screen? what's wrong??????? it should show a cube.

int main(int argc, char *argv[])
{
	unsigned char *k;
	bool keepdoing = true;
	
	SDL_Init(SDL_INIT_VIDEO);
	SDL_WM_SetCaption("My first OpenGL programm !",NULL);
	SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
	while (keepdoing)
	{
		SDL_PumpEvents();
		k=SDL_GetKeyState(NULL);
		
		if(k[SDLK_ESCAPE])
			keepdoing=false;
		if(k[SDLK_LEFT])
			glRotatef(0.1f, 0.0f, 1.0f, 0.0f);
		if(k[SDLK_RIGHT])
			glRotatef(0.1f, 0.0f,-1.0f, 0.0f);
		if(k[SDLK_UP])
			glRotatef(0.1f, 1.0f, 0.0f, 0.0f);
		if(k[SDLK_DOWN])
			glRotatef(0.1f, -1.0f, 0.0f, 0.0f);
		
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		glTranslatef(0.0f, 0.0f, -7.0f);
		
		glBegin(GL_QUADS);			
			
		glColor3f(0.0f,1.0f,0.0f);			// Set The Color To Green
		glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Top)
		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Top)
		glVertex3f(-1.0f, 1.0f, 1.0f);			// Bottom Left Of The Quad (Top)
		glVertex3f( 1.0f, 1.0f, 1.0f);	
			
		glColor3f(1.0f,0.5f,0.0f);			// Set The Color To Orange
		glVertex3f( 1.0f,-1.0f, 1.0f);			// Top Right Of The Quad (Bottom)
		glVertex3f(-1.0f,-1.0f, 1.0f);			// Top Left Of The Quad (Bottom)
		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Bottom)
		glVertex3f( 1.0f,-1.0f,-1.0f);				
				
		glColor3f(1.0f,0.0f,0.0f);			// Set The Color To Red
		glVertex3f( 1.0f, 1.0f, 1.0f);			// Top Right Of The Quad (Front)
		glVertex3f(-1.0f, 1.0f, 1.0f);			// Top Left Of The Quad (Front)
		glVertex3f(-1.0f,-1.0f, 1.0f);			// Bottom Left Of The Quad (Front)
		glVertex3f( 1.0f,-1.0f, 1.0f);	
				
		glColor3f(1.0f,1.0f,0.0f);			// Set The Color To Yellow
		glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Back)
		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Back)
		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Back)
		glVertex3f( 1.0f, 1.0f,-1.0f);	
		
		glColor3f(0.0f,0.0f,1.0f);			// Set The Color To Blue
		glVertex3f(-1.0f, 1.0f, 1.0f);			// Top Right Of The Quad (Left)
		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Left)
		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Left)
		glVertex3f(-1.0f,-1.0f, 1.0f);
		
		glColor3f(1.0f,0.0f,1.0f);			// Set The Color To Violet
		glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Right)
		glVertex3f( 1.0f, 1.0f, 1.0f);			// Top Left Of The Quad (Right)
		glVertex3f( 1.0f,-1.0f, 1.0f);			// Bottom Left Of The Quad (Right)
		glVertex3f( 1.0f,-1.0f,-1.0f);	
		
		glEnd();
		glFlush();
		SDL_GL_SwapBuffers();
    }
    SDL_Quit();
    return 0;
}

Advertisement
You have to set up the projection matrix for 3D and viewport. After that, set it to modelview to then draw the scene.

int main(int argc, char *argv[]){	unsigned char *k;	bool keepdoing = true;		SDL_Init(SDL_INIT_VIDEO);	SDL_WM_SetCaption("My first OpenGL programm !",NULL);	SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);	while (keepdoing)	{		SDL_PumpEvents();		k=SDL_GetKeyState(NULL);				if(k[SDLK_ESCAPE])			keepdoing=false;		if(k[SDLK_LEFT])			glRotatef(0.1f, 0.0f, 1.0f, 0.0f);		if(k[SDLK_RIGHT])			glRotatef(0.1f, 0.0f,-1.0f, 0.0f);		if(k[SDLK_UP])			glRotatef(0.1f, 1.0f, 0.0f, 0.0f);		if(k[SDLK_DOWN])			glRotatef(0.1f, -1.0f, 0.0f, 0.0f);				glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);                //NEW CODE                glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height );                glMatrixMode( GL_PROJECTION );                glLoadIdentity();                gluPerspective( 45.0f, 640.0f / 480.0f, 0.1f, 100.0f);                glMatrixMode(GL_MODELVIEW);                glLoadIdentity();		glTranslatef(0.0f, 0.0f, -7.0f);				glBegin(GL_QUADS);								glColor3f(0.0f,1.0f,0.0f);			// Set The Color To Green		glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Top)		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Top)		glVertex3f(-1.0f, 1.0f, 1.0f);			// Bottom Left Of The Quad (Top)		glVertex3f( 1.0f, 1.0f, 1.0f);						glColor3f(1.0f,0.5f,0.0f);			// Set The Color To Orange		glVertex3f( 1.0f,-1.0f, 1.0f);			// Top Right Of The Quad (Bottom)		glVertex3f(-1.0f,-1.0f, 1.0f);			// Top Left Of The Quad (Bottom)		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Bottom)		glVertex3f( 1.0f,-1.0f,-1.0f);										glColor3f(1.0f,0.0f,0.0f);			// Set The Color To Red		glVertex3f( 1.0f, 1.0f, 1.0f);			// Top Right Of The Quad (Front)		glVertex3f(-1.0f, 1.0f, 1.0f);			// Top Left Of The Quad (Front)		glVertex3f(-1.0f,-1.0f, 1.0f);			// Bottom Left Of The Quad (Front)		glVertex3f( 1.0f,-1.0f, 1.0f);							glColor3f(1.0f,1.0f,0.0f);			// Set The Color To Yellow		glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Back)		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Back)		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Back)		glVertex3f( 1.0f, 1.0f,-1.0f);					glColor3f(0.0f,0.0f,1.0f);			// Set The Color To Blue		glVertex3f(-1.0f, 1.0f, 1.0f);			// Top Right Of The Quad (Left)		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Left)		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Left)		glVertex3f(-1.0f,-1.0f, 1.0f);				glColor3f(1.0f,0.0f,1.0f);			// Set The Color To Violet		glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Right)		glVertex3f( 1.0f, 1.0f, 1.0f);			// Top Left Of The Quad (Right)		glVertex3f( 1.0f,-1.0f, 1.0f);			// Bottom Left Of The Quad (Right)		glVertex3f( 1.0f,-1.0f,-1.0f);					glEnd();		glFlush();		SDL_GL_SwapBuffers();    }    SDL_Quit();    return 0;}
Mmmm. The problem is that i am following the nehe tutorials and there it doesn't say anything about that. Are those tutorials wrong?
It does mention it in the very first tutorial.

This topic is closed to new replies.

Advertisement