Jump to content
  • Advertisement
Sign in to follow this  
Drakkcon

OpenGL is giving me trouble

This topic is 5048 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.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,-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);

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!