PDA

View Full Version : SDL/OpenGL shape rotation problem!



OhSnapEpicPwnage
04-18-2011, 03:14 PM
Why does this fail?
Whenever angle is set to 0 shape is visible, but when its increased to 0.1 or higher then the shape dissappears :S.

#include "SDL/SDL.h"
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl\glaux.h>
void LoopFunc();

SDL_Surface *screen = NULL;
SDL_Event event;

GLfloat angle= 0.0f;

int main(int argc,char *args[])
{
int start;

SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE | SDL_OPENGL);
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
glViewport(0, 0, 800, 600);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 800, 600, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D | GL_DEPTH_TEST);
glLoadIdentity();


while(true)
{
start = SDL_GetTicks();

if(SDL_PollEvent(&amp;event))
{
if(event.type == SDL_QUIT)
{
return false;
}
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE)
{
return false;
}
}
}

LoopFunc();


SDL_Flip(screen);
}

SDL_Quit();

return 0;
}

void LoopFunc()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glRotatef(angle,1.0f,0.0f,0.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(350.0f,100.0f,-1.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(100.0f,400.0f,-1.0f);
glColor3f(0.0f,0.0f,3.0f);
glVertex3f(600.0f,400.0f,-1.0f);
glEnd();

angle+= 0.2f;

SDL_GL_SwapBuffers();
}

V-man
04-18-2011, 03:54 PM
glEnable(GL_TEXTURE_2D | GL_DEPTH_TEST);
is not valid code

See
http://www.opengl.org/wiki/Common_Mistakes#Bitfield_enumerators

Convert
glOrtho(0, 800, 600, 0, 1, -1);
to
glOrtho(0.0, 800.0, 600.0, 0.0, 1000.0, 1000.0);

OhSnapEpicPwnage
04-18-2011, 04:31 PM
Thanks man!
Only that

glOrtho(0.0, 800.0, 600.0, 0.0, 1000.0, 1000.0);
fails,but works if i do

glOrtho(0.0, 800.0, 600.0, 0.0, 1000.0, -1000.0);
then it works just fine.