OpenGL is giving me trouble

Started by
7 comments, last by Drakkcon 19 years, 4 months ago

#include <SDL/SDL.h>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

#define KeyPressed event.key.keysym.sym

SDL_Event event;
float xtheta = 0.0f, ytheta = 0.0f, ztheta = 0.0f;

void SetupGlAndSDL();

int main(int Arg_N, char ** Arg_V)
{
  SetupGlAndSDL();
   
  for(int done = 0; !done;)
  {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0f,0.0f,0.0f);
    glRotatef(xtheta, ytheta, ztheta, 1.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);			// Bottom Right Of The Quad (Top)
		
		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);			// Bottom Right Of The Quad (Bottom)
		
		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);			// Bottom Right Of The Quad (Front)
		
	  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);			// Top Left Of The Quad (Back)
		
		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);			// Bottom Right Of The Quad (Left)
		
		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);			// Bottom Right Of The Quad (Right)	
	  ////////////////////////////
    glEnd();						// Done Drawing The Quad
    
    SDL_GL_SwapBuffers();
    SDL_PollEvent(&event);
    
    if(KeyPressed == SDLK_ESCAPE)
      done = 1;
    if(KeyPressed == SDLK_UP)
      xtheta += 0.5f;
    if(KeyPressed == SDLK_DOWN)
    {  
      xtheta -= 0.5f;
      
      if(xtheta < 0)
        xtheta = 0;
       
    }
    
    if(KeyPressed == SDLK_RIGHT)
      ytheta += 0.5f;
    if(KeyPressed == SDLK_LEFT)
    {  
      ytheta -= 0.5f;
      
      if(ytheta < 0)
        ytheta = 0;
       
    }
    if(KeyPressed == SDLK_x)
      ztheta += 0.5f;
    if(KeyPressed == SDLK_z)
    {  
      ztheta -= 0.5f;
      
      if(ztheta < 0)
        ztheta = 0;
    }   
        
  }
  

  SDL_Quit();
  
  return(0);
}

void SetupGlAndSDL()
{
    
  SDL_Init(SDL_INIT_VIDEO);
  SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME | SDL_FULLSCREEN);
  
  glViewport(0, 0, 1024, 768);
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glClearDepth(1.0);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glShadeModel(GL_SMOOTH);
  glMatrixMode(GL_PROJECTION);
  glMatrixMode(GL_MODELVIEW);

}  

This worked perfectly with my triangle, but it doesn't work with the set of quads I copied from NeHe designed to make a cube. What did I do wrong?
Advertisement
What happens? Blank Screen? Crashing?

EDIT: Does it no draw properly? Try moving your camera (glTranslatef) back a bit.
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
If I change glTranslate's Z value (the last value) I can see nothing. I can't really explain the noise on the screen. It's solid yellow until I touch an arrow key. Try running it yourself maybe, I can't describe it.
I changed the z value on the translate function to -10 and it worked fine...

EDIT: Though I only used the draw code...not the whole thing.
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
I don't see where you setup the projection matrix. In your setupgl function you switch to projection mode but don't do anything other than instantly switch back to modelview. I don't know if this is exactly your problem but it doesn't help not having a valid projection matrix setup.


-SirKnight
Well, it doesn't for me....
Any idea why?

All I see is a black screen.
Quote:Original post by SirKnight
I don't see where you setup the projection matrix. In your setupgl function you switch to projection mode but don't do anything other than instantly switch back to modelview. I don't know if this is exactly your problem but it doesn't help not having a valid projection matrix setup.


-SirKnight


Perfect! That solved the problem! Thanks alot
Ah so it did turn out to be that. Good. Glad to hear it's working now!


-SirKnight
Yes, thanks alot sir knight. If anyone cares, here's the working source:
#include <SDL/SDL.h>#include <windows.h>#include <gl/gl.h>#include <gl/glu.h>#define KeyPressed event.key.keysym.symSDL_Event event;float xtheta = 0.0f, ytheta = 0.0f, ztheta = 0.0f;void SetupGlAndSDL();int main(int Arg_N, char ** Arg_V){  SetupGlAndSDL();     for(int done = 0; !done;)  {    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glLoadIdentity();    glTranslatef(0.0f,0.0f,-10.0f);    glRotatef(xtheta, ytheta, ztheta, 1.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);			// Bottom Right Of The Quad (Top)				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);			// Bottom Right Of The Quad (Bottom)				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);			// Bottom Right Of The Quad (Front)			  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);			// Top Left Of The Quad (Back)				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);			// Bottom Right Of The Quad (Left)				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);			// Bottom Right Of The Quad (Right)		  ////////////////////////////    glEnd();						// Done Drawing The Quad        SDL_GL_SwapBuffers();    SDL_PollEvent(&event);        if(KeyPressed == SDLK_ESCAPE)      done = 1;    if(KeyPressed == SDLK_UP)      xtheta += 0.5f;    if(KeyPressed == SDLK_DOWN)    {        xtheta -= 0.5f;            if(xtheta < 0)        xtheta = 0;           }        if(KeyPressed == SDLK_RIGHT)      ytheta += 0.5f;    if(KeyPressed == SDLK_LEFT)    {        ytheta -= 0.5f;            if(ytheta < 0)        ytheta = 0;           }    if(KeyPressed == SDLK_x)      ztheta += 0.5f;    if(KeyPressed == SDLK_z)    {        ztheta -= 0.5f;            if(ztheta < 0)        ztheta = 0;    }             }    SDL_Quit();    return(0);}void SetupGlAndSDL(){      SDL_Init(SDL_INIT_VIDEO);  SDL_SetVideoMode(1024, 768, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME | SDL_FULLSCREEN);    glViewport(0, 0, 1024, 768);  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  glClearDepth(1.0);    glDepthFunc(GL_LESS);  glEnable(GL_DEPTH_TEST);  glShadeModel(GL_SMOOTH);    glMatrixMode(GL_PROJECTION);  glLoadIdentity();	gluPerspective(45.0f,1024.0f/768.0f,0.1f,100.0f);	  glMatrixMode(GL_MODELVIEW);}  

This topic is closed to new replies.

Advertisement