Sign in to follow this  

Trouble with OpenGL

This topic is 4595 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

Im trying to get these two rectangles to rotate next to eachother - instead they both orbit the middle point - I can't figure out how to make them NOT orbit! HELP!
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <SDL/SDL.h>

GLfloat ratio;


void draw_square()
{

        glBegin(GL_POLYGON);
            glVertex3f(-1.0f, 1.0f, 0.0f); 
            glVertex3f(-1.0f, 0.5f, 0.0f);
            glVertex3f(-0.5f, 0.5f, 0.0f);
            glVertex3f(-0.5f, 1.0f, 0.0f);
        glEnd();
 
}

int main(int Arg_N, char ** Arg_V)
{

    ratio = 640/480;
    SDL_Event event;
    
    float theta = 0.0f;


    SDL_Init(SDL_INIT_VIDEO);
    SDL_SetVideoMode(640, 480, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME);

    glViewport(0, 0, 640, 480);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0f);

    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);



    //glMatrixMode(GL_TEXTURE);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective( 45.0f, ratio, 0.1f, 100.0f );

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    for(int done = 0; !done;)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();

        glTranslatef(-0.25f, 0.0f, -6.0f);
        glRotatef(theta, 0.0f, 0.0f, 1.0f);
        draw_square();
       
        glLoadIdentity();

        glTranslatef(0.25f, 0.0f, -6.0f);
        glRotatef(theta, 0.0f, 0.0f, 1.0f);
        draw_square();

        glLoadIdentity();
        SDL_GL_SwapBuffers();
        theta += 1.0f;
        

SDL_PollEvent(&event);
        
        if(event.key.keysym.sym == SDLK_ESCAPE)done = 1; 
    }
            SDL_Quit();
return(0);

}


}



Share this post


Link to post
Share on other sites
I think you need to perform the rotation and translations in the opposite order.

glRotatef(theta, 0.0f, 0.0f, 1.0f);
glTranslatef(-0.25f, 0.0f, -6.0f);

You want to rotate the square and then move it, rather than move the square then rotate, as the rotation occurs around the origin.

Share this post


Link to post
Share on other sites
I've actually tried that - and it doesn't do anything.

Thank you for replying though - but this is the most confusing thing I have ever... I went and looked at the NeHe tutorials too, they are practically identical.

Share this post


Link to post
Share on other sites
OpenGL is a real confusing realm at first, I can agree to that. :)

Your error is in your coordinates specified for the polygon you drew. If you want something to rotate around its centre, be sure to give coordinates around the centre of the object. Changing the polygon's coordinates to these (which is still a 0.5x0.5 rectangle, but around the origin instead) gives the intended result:


glBegin(GL_POLYGON);
glVertex3f(-0.5f, 0.5f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, 0.5f, 0.0f);
glEnd();


And good luck on your OpenGL adventures! :)

Share this post


Link to post
Share on other sites
Oh man thank you so much - gave ya a rating.


I reviewed the difference between these two, and I see exactly the problem now - thank you so much, again.


*bows deeply*

*continues to confuse self*

Share this post


Link to post
Share on other sites

This topic is 4595 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this