Question

In OpenGL, I can easily rotate a triangle by something like this:

glRotate(90, 1, 0, 0);
    glBegin(GL_TRIANGLES);
    glVertex3f(-1,0,0);
    glVertex3f(1,0,0);
    glVertex3f(0,1,0);
    glEnd();
glRotate(-90, 1, 0, 0);

However, I would like to rotate only ONE of the vertices, not all three that make up the triangle, but I would still like it to draw the triangle in the end.

I've tried things like this, but with no success:

glBegin(GL_TRIANGLES);

    // Rotate only this vertex.
    glRotate(90, 1, 0, 0);
    glVertex3f(-1,0,0);
    glRotate(-90, 1, 0, 0);

    glVertex3f(1,0,0);
    glVertex3f(0,1,0);
glEnd();

Any ideas?

Was it helpful?

Solution

You can't put glRotate calls between glBegin and glEnd.

Only a subset of GL commands can be used between glBegin and glEnd. The commands are glVertex, glColor, glIndex, glNormal, glTexCoord, glEvalCoord, glEvalPoint, glMaterial, and glEdgeFlag.

See http://www.cs.uccs.edu/~ssemwal/glman.html for the full description.

To answer your question, if you only want to rotate a single vertex you will need to do that manually before calling glVertex3f.

In your specific case (rotation about the x axis) the code would look like this:

glBegin(GL_TRIANGLES);

// Rotate only the vertex <x,y,z> about the x axis an angle of theta.
glVertex3f(x, y*cos(theta) - z*sin(theta), y*sin(theta) + z*cos(theta));

glVertex3f(1,0,0);
glVertex3f(0,1,0);
glEnd();

If you need to rotate about a different axis or a generic axis then see the wikipedia page about 3D rotation matrices: http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top