0

I'm writing a simple demo with the fixed pipeline mode in OpenGL

First I write some codes that will draw a rectangle, with top-left point (x1,y1), and buttom-right point (x2,y2)

        // Drawing here
    glPushMatrix();
    // Perform rotation first
    glTranslatef(x_cent, y_cent, 0.0f);
    glRotatef(angle, 0.0f, 0.0f, 1.0f);
    glScalef(scale, scale, 1.0f);
    // Set origin back to the screen center with the rotation set above
    glTranslatef(-x_cent, -y_cent, 0.0f);

    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glBegin(GL_QUADS);
    {
        glVertex2f(x1, y2);
        glVertex2f(x2, y2);
        glVertex2f(x2, y1);
        glVertex2f(x1, y1);
    }
    glEnd();

    // End of drawing
    glPopMatrix();

After I tweak the value of the angle with arrow keys, say 45 degrees counter-clockwise, and now the rectangle will rotate 45 degrees counter-clockwise. Then I translate it with arrow keys, it moves in the direction of up, down, left, and right. Everything works fine.

So I write anoter part of code that will draw a general polygon. It's similar to the code above. I simply draw lines between each pair of points

for (int i = 0; i < p_size; i++)
{
    float x1 = point[i].x, x2 = point[(i + 1) % p_size].x;
    float y1 = point[i].y, y2 = point[(i + 1) % p_size].y;

    x1 = ((x1 + 0.5f) / winWidth) * 2.0f - 1.0f;
    x2 = ((x2 + 0.5f) / winWidth) * 2.0f - 1.0f;
    y1 = ((-y1 + 0.5f) / winHeight) * 2.0f + 1.0f;
    y2 = ((-y2 + 0.5f) / winHeight) * 2.0f + 1.0f;

    // Set the curent coord origin to this poly's center
    float x_cent = (left + right) / 2.0, y_cent = (top + down) / 2.0;
    float ratio = winWidth / winHeight;

    if (ratio > 1)
    {
        x1 *= ratio;
        x2 *= ratio;
    }

    else //if (ratio < 1)
    {
        y1 /= ratio;
        y2 /= ratio;
    }
    // Drawing here
    glPushMatrix();
    // Perform rotation first
    glTranslatef(x_cent, y_cent, 0.0f);
    glRotatef(angle, 0.0f, 0.0f, 1.0f);
    glScalef(scale, scale, 1.0f);
    // Set origin back to the screen center with the rotation set above
    glTranslatef(-x_cent, -y_cent, 0.0f);

    glBegin(GL_LINES);
    {
        glVertex2f(x1, y1);
        glVertex2f(x2, y2);
    }
    glEnd();

    // End of drawing
    glPopMatrix();
}

I rotate the polygon with arrow keys, that works fine, say also 45 degrees counter-clockwise. But after the rotation, when I translate it with up key, it moves not up but in the direction of 45 degrees.

Bbut why can the rectangle translate normally in the 4 directions after rotation? These two part of codes are almost the same, why is this happening...

RushSykes
  • 41
  • 1
  • 9
  • By pressing arrow keys I add a uint of translation to each of the point's coordinates – RushSykes Oct 27 '16 at 04:34
  • 1
    Oh I got it solved, it's beacuse after the translation I forgot to update the x_cent and y_cent – RushSykes Oct 27 '16 at 05:39
  • 1
    see my OpenGL/C++ source and demos in: [swift sphere combine star data](http://stackoverflow.com/a/40171880/2521214) and [OpenGL - Rotation not working](http://stackoverflow.com/a/40104549/2521214) to get the feeling of the difference between local and global rotations. Also take a look at the: [Understanding 4x4 homogenous transform matrices](http://stackoverflow.com/a/28084380/2521214) if you need more detailed info ... – Spektre Oct 27 '16 at 11:43

0 Answers0