# Need a help with triangles positions

## Recommended Posts

Hi for all I'm putting some triangles(9 triangles) in my program but the results I didn't understand. What I'm doing for each triangles is: - I make a call for glTranslateMatrix(); - I make a call for glRotateMatrix() in the z-axis; - Draw the triangle; - Add 45 in the iAngle variable; I'm doing this to position and rotate the triangles one after another in the x-axis. The first 3 triangles works fine but the 4 to the 8 down a little bit and in the 9th triangle returns to the normal. What this thing occurs? This is the img: And this is the code that I've made:
while(!bQuit)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
bQuit =!bQuit;
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
GetClientRect(hwnd, &tempRect);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
SetFieldOfView(tempRect.right,tempRect.bottom);
glMatrixMode(GL_MODELVIEW);
for(int i = 0, x = 8, y = 0.1, iAngle = 0 ; i < 9 ; i++, x-=2)
{
glPushMatrix();
glTranslatef(x, y, -20);
glRotatef(iAngle, 0, 0, 1);
glColor3f(1,1,1);
glBegin(GL_TRIANGLES);
glVertex2f(0,0);
glVertex2f(1,0);
glVertex2f(1/2,1);
glEnd();
glPopMatrix();
iAngle+=45;
}
SwapBuffers(hdc);
}



##### Share on other sites
The image looks correct for the code you posted at a quick glance. I don't understand; do you not want the triangle to rotate around one of its vertices and rotate around the center instead? If so you'll either need to define the triangle around its model-space origin or translate the triangle a little. Rotation is about the origin.

##### Share on other sites
jpetrie, I want to rotate around the center but I don't want to do other translation. How can I do rotate around the center of each triangle?

##### Share on other sites
The rotation matrices built by glRotate() are axis-angle rotations about the origin. This is a fact of life (and mathematics).

Consequently, your options are:
1) Redefine your triangle. Currently, your triangle is defined in model space such that one of its vertices is at the origin. Change the vertices so that the triangle is defined in model space such that its center is at the origin. This might look (approximately) like (0,1) (-1,-1) (1,-1) or something of that general nature.

2) Apply a translation to center the triangle at the origin. Apply the rotation. Apply the opposite translate to place the triangle back where you want it (if you actually want it back there; usually you do).

These are really the same thing, and there is really no other option.

## 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

• ### Forum Statistics

• Total Topics
628350
• Total Posts
2982211

• 10
• 9
• 24
• 11
• 9