#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);
}
OpenGL is giving me trouble
What happens? Blank Screen? Crashing?
EDIT: Does it no draw properly? Try moving your camera (glTranslatef) back a bit.
EDIT: Does it no draw properly? Try moving your camera (glTranslatef) back a bit.
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.
EDIT: Though I only used the draw code...not the whole thing.
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
-SirKnight
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
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
Popular Topics
Advertisement