PDA

View Full Version : Problem with basic triangle rotation



ukdeveloper
08-14-2005, 10:36 AM
Hello, I'm a first time poster, and new to OpenGL.

I'm using OpenGL on top of SDL, and I've hit a problem when it comes to getting a triangle to rotate. This code is based on lesson 4 as found at NeHe.GameDev.Net (http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=04) .

My main problem is that I can't make my triangle rotate in the same manner that the one in the tutorial does. Here's the code I'm using:


#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <GL/GL.h>
#include <GL/glu.h>

enum RETURNTYPE {success,failure};

void Draw();
void HandleInput();

bool Running;

GLfloat r_angle=0.0f;


int main(int argc, char *argv[])
{

if(SDL_Init(SDL_INIT_VIDEO)<0)
{

return failure;

}

Running=true;

SDL_GL_SetAttribute(SDL_GL_RED_SIZE,5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);

SDL_Surface *screen=SDL_SetVideoMode(1024,768,32,SDL_OPENGL|SD L_FULLSCREEN);

if(!screen)
{

return failure;

}

glViewport(0,0,1024,768);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(0.0f,1024,768,0.0f,-1.0f,1.0f);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glClearColor(0.0f,0.0f,0.0f,0.0f);

glClearDepth(1.0f);

glDepthFunc(GL_LEQUAL);

glEnable(GL_DEPTH_TEST);

glShadeModel(GL_SMOOTH);

glDisable(GL_CULL_FACE);

glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);


while(Running)
{

Draw();

HandleInput();

}


}

void Draw()
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glTranslatef(335.0f,250.0f,0.0f);

glRotatef(r_angle,0.0f,1.0f,0.0f);

// Draws a basic triangle centred at (250,250)
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f(-100.0f,100.0f,0.0f); // Bottom left point of triangle

glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(100.0f,100.0f,0.0f); // Bottom right

glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(0.0f,-100.0f,0.0f); // Top point
glEnd();

SDL_GL_SwapBuffers();

r_angle+=0.1f;

}

void HandleInput()
{

SDL_Event keypress;

while(SDL_PollEvent(&amp;keypress))
{

if(keypress.type==SDL_KEYDOWN)

switch(keypress.key.keysym.sym)
{

case SDLK_ESCAPE:

Running=false;

break;

}

}

} Most of that is for keyboard input etc., not really important.

The thing is, the behaviour is weird. Run the code and you'll see what I mean. The triangle isn't rotating properly, I suspect the problem lies in how my viewport is set up. I want the triangle to rotate in the same way as the link I gave, how do I go about that?

Thanks for your help,

ukdeveloper.

Ther
08-15-2005, 11:49 AM
I don't know actually what you want to do, but maybe it works better if you rotate before you translate, because it is matter what you doing first.

memfr0b
08-15-2005, 11:03 PM
Originally posted by ukdeveloper:

glRotatef(r_angle,0.0f,1.0f,0.0f);

// Draws a basic triangle centred at (250,250)
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f(-100.0f,100.0f,0.0f); // Bottom left point of triangle

glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(100.0f,100.0f,0.0f); // Bottom right

glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(0.0f,-100.0f,0.0f); // Top point
glEnd();Your triangle is located in the x/y-plane, yet you are rotating around the y-axis. What you probably want is a rotation around the z-axis.


glRotate(r_angle,0.0f,0.0f,1.0f);