Advertisement

rotation: weird results

Started by March 01, 2005 09:48 AM
6 comments, last by adam17 19 years, 11 months ago
Screenshot: Free Image Hosting at www.ImageShack.us And here is the code :

#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>

#define TRUE 1
#define FALSE 0

int DrawGLScene(GLfloat rquad);
int InitGL(GLvoid);
void Reshape(int xres, int yres);

int main(void)
{
    SDL_Surface * screen;
    SDL_Event event;
    int STATE = 0;
    GLfloat rotquad = 0.0f;
    
    if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0)
    {
        printf("%s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    
    atexit(SDL_Quit);
    
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE,   8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,  8);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    
    screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
    
    if (screen == NULL)
    {
        printf("%s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    
    Reshape(screen->w, screen->h);
    rotquad = 0.2f;
    
    if (!InitGL())
    {
        printf("Error when initializing OpenGL.\n");
        exit(EXIT_FAILURE);
    }
     
    while (!STATE)
    {
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
                STATE = 1;
                
            if (event.type == SDL_KEYDOWN)
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    STATE = 1;
        }
        
        if (!DrawGLScene(rotquad))
            STATE = 1;
            
        rotquad += 0.2;
    }
   
    exit(EXIT_SUCCESS);
}

int InitGL(GLvoid)
{
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -6.0);
    
    return TRUE;
}

int DrawGLScene(GLfloat rquad)
{ 
    glRotatef(rquad, 1.0f, 0.0f, 0.0f);
   
    glBegin(GL_QUADS);
        glColor3f(0.0f, 0.0f, 0.0f);
        glVertex3f(-0.5, -0.5, 0.0);
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(-0.5, 0.5, 0.0);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(0.5, 0.5, 0.0);
        glColor3f(0.5f, 0.5f, 0.5f);
        glVertex3f(0.5, -0.5, 0.0);
    glEnd();
    
    SDL_GL_SwapBuffers();
    
    return TRUE;
}

void Reshape(int xres, int yres)
{
    if (yres == 0)
        yres = 1;
        
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, xres / yres, 1.5f, 20.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
} 




I am betting it is rotating so fast that is what is causing the artifacting.
Advertisement
Why? I'm only updating it by 0.2.
try adding the lines

glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslatef(0.0, 0.0, -6.0);

in the begining of DrawGLScene.
yep that worked thanks man. Can you please explain why?
It looks like you werent your image and depth buffers which would mean the previous frames would still be displayed for a start. Not clearing your z buffers would cause z-fighting.
Advertisement
Ok, but why do you have to reset the view each time with glTranslate? Sorry, I'm new to 3d graphics and openGL altogether.
Quote:
Original post by subflood
Ok, but why do you have to reset the view each time with glTranslate? Sorry, I'm new to 3d graphics and openGL altogether.

each time you do a new frame, all of the buffers are cleared out and the matrices are reset. thats why you need to reset it every frame or everytime you call drawscene

This topic is closed to new replies.

Advertisement