Results 1 to 5 of 5

Thread: rotate 2D object

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2009
    Posts
    22

    rotate 2D object

    hi,
    I'm trying to draw a 2D object (triangle or polygon) when i move the mouse the object that has been drawn must be rotate on x, y axis.
    the following code is my implementation:

    void draw_triangle(void){
    glTranslatef(translate_x, translate_y, 0);
    glRotatef(beta, 1, 0, 0);
    glRotatef(alpha, 0, 1, 0);
    glColor3f(1, 1, 1);

    if(strcmp(_triangle->texture_path, "" ) != 0){
    glBegin(GL_TRIANGLES);
    glTexCoord2f( (_triangle->v[0]).x, (_triangle->v[0]).y);
    glVertex2f((_triangle->v[0]).x, (_triangle->v[0]).y);
    glTexCoord2f( (_triangle->v[1]).x, (_triangle->v[1]).y);
    glVertex2f((_triangle->v[1]).x, (_triangle->v[1]).y);
    glTexCoord2f ((_triangle->v[2]).x, (_triangle->v[2]).y);
    glVertex2f((_triangle->v[2]).x, (_triangle->v[2]).y);
    glEnd();
    }else{
    glBegin(GL_TRIANGLES);
    glVertex2f((_triangle->v[0]).x, (_triangle->v[0]).y);
    glVertex2f((_triangle->v[1]).x, (_triangle->v[1]).y);
    glVertex2f((_triangle->v[2]).x, (_triangle->v[2]).y);
    glEnd();
    }

    glScalef(xscale, yscale, 1.0f);
    }

    void DisplayFunc_triangle(void){

    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    glViewport(0, 0, width / 2, height / 2);
    glPushMatrix();
    glTranslatef(0, 0, -10);
    glRotatef(0, 1, 0, 0);
    glRotatef(0, 0, 1, 0);

    GLfloat light_position[] = { 0, 0, 1, 0};
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    draw_axis();
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    draw_triangle();
    glDisable(GL_LIGHTING);
    glDisable(GL_LIGHT0);
    glPopMatrix();

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();

    glLoadIdentity();
    glOrtho(-1.2, 1.2, -1.2, 1.2, -1.2, 1.2);
    glMatrixMode(GL_MODELVIEW);

    glViewport(0, height / 2 + 1, width / 2 + 1, height / 2);
    glPushMatrix();
    glRotatef(90, 0, 1, 0);
    draw_axis();

    enable_texture2D();
    draw_triangle();
    glPopMatrix();

    disable_texture2D();
    glViewport(width / 2 + 1, height / 2 + 1, width / 2, height / 2);
    glPushMatrix();
    draw_axis();
    enable_texture2D();
    draw_triangle();
    glPopMatrix();
    disable_texture2D();

    glViewport(width / 2 + 1, 0, width / 2, height / 2);
    glPushMatrix();
    glRotatef(90, 1, 0, 0);
    draw_axis();
    enable_texture2D();
    draw_triangle();
    glPopMatrix();
    disable_texture2D();

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);

    glFlush();
    glutSwapBuffers();
    }

    void MotionFunc_triangle(int x, int y){
    if (GLUT_DOWN == left_click){
    beta = beta + (y - yold) / 2.f;
    alpha = alpha + (x - xold) / 2.f;
    glutPostRedisplay();
    }else if(GLUT_DOWN == right_click){
    translate_x = translate_x + (x - xold )/300.0;
    translate_y = translate_y + (y - yold )/300.0;
    glutPostRedisplay();
    }
    xold = x;
    yold = y;
    }

    when i click left button i get strange rotation.. because I specify the rotation only on the x,y axis but the object move on x, y and z axis... why? can you help me please?

  2. #2
    Member Contributor
    Join Date
    Jan 2001
    Posts
    82

    Re: rotate 2D object

    I may be misunderstanding you, but I believe that you want the object to always face the viewer, correct? If so the problem is that you're rotating around the x and y axis instead of the z:

    Around the x axis:



    Around the y axis:



    Around the z axis:


  3. #3
    Junior Member Newbie
    Join Date
    Jul 2009
    Posts
    22

    Re: rotate 2D object

    thanks mark for reply,
    I want rotate the object only on the x or y or x,y axis (depend on mouse position).
    I think there is an error(in my code) because the object rotate around x, y, z axis.

  4. #4
    Member Contributor
    Join Date
    Jan 2001
    Posts
    82

    Re: rotate 2D object

    So you are trying to do 3D rotations? It isn't going to look correct since you're using glOrtho.

    It you rotate, say, 45 on the x axis and then 45 on the y axis, the object will appear to rotate along the z axis since the vertices are being transformed along z. See the above pics.

    I'm really not understanding what you're trying to accomplish. It sounds like you are trying to implement a 3D trackball, however, without perspective correction, the object will just shrink on the axis of rotation. If you are trying to accomplish something like the last pic I posted, rotating around the x and y axis will not accomplish it.

  5. #5
    Junior Member Newbie
    Join Date
    Jul 2009
    Posts
    22

    Re: rotate 2D object

    I'm using 4 viewport, in first viewport you can see a perspective view in the others projected object. when I use glutSolid* the rotation work well (the object rotate only on the x, y axis). I get bad rotation of the object only when I use
    glBegin (GL_TRIANGLES) .... or glBegin(GL_QUADS), moreover, in the last case the object is also translated on the x,y, z axis.
    how can i solve this strange rotation !! the viewport is necessary and is necessary display the projected object in the
    others viewport.

    I don't understand why: in first case (glutSolid*) rotation works well and second case (GL_TRIANGLES) no.
    I'm trying to do a 2D rotation of the object.

Similar Threads

  1. select object from multiple object and rotate
    By somnath in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-05-2011, 04:57 PM
  2. Rotate an object
    By Inquirer in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 08-03-2010, 07:28 PM
  3. How to Rotate 3D Object
    By Balaji CS in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-04-2010, 02:25 AM
  4. rotate an object
    By Ziggurat in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 03-24-2002, 11:57 AM
  5. rotate object?
    By phenius in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-27-2001, 07:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean