15,746,248 members
See more:
Hi I am writing a demo in OpenGL. In one of the scenes, i am having a few cubes that rotate around the center of themselves(x, y, z). To draw the cube, i get the coordinates for the center(x, y, z), and draw a cube from -1, -1, -1 to 1, 1, 1(you get the idea)

Anyway, when rotating, all the cubes rotate around 0, 0, 0. How can i rotate around x, y, z instead?

Source:

PartCubes.cpp
C++
```#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "GLWrapper.h"
#include "Nevermind/GLNMD.h"
#include "PartCubes.h"
#include "Box.h"

using namespace GLWrapper;

PartCubes::PartCubes(void)
{
xpos = 0;
ypos = 0;
zpos = 0;
}

PartCubes::~PartCubes(void)
{
}
void PartCubes::draw(float time)
{
srand(666);
for(int i = 0; i < 400; i++)
{
float x = (float)rand()/(float)RAND_MAX;
float y = (float)rand()/(float)RAND_MAX;
float z = (float)rand()/(float)RAND_MAX;
x = x * 20;
y = y * 20;
z = z * 20;
float xrot = (float)rand()/(float)RAND_MAX;
xrot = (xrot * 2) * time;
//float x = 0;
//float y = 0;
//float z = 0;
drawCube(x + xpos, y + ypos, z + zpos, xrot);
}

}
void PartCubes::drawCube(float x, float y, float z, float xrot)
{
glTranslatef(-x,-y,-z);
glRotatef(xrot, 1, 0, 0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(1.0 + x, -1.0 + y, -1.0 + z);
glVertex3f(1.0 + x, -1.0 + y, 1.0 + z);
glVertex3f(-1.0 + x, -1.0 + y, 1.0 + z);
glVertex3f(-1.0 + x, -1.0 + y, -1.0 + z);
glVertex3f(1.0 + x, 1.0 + y, -0.999999 + z);
glVertex3f(-1.0 + x, 1.0 + y, -1.0 + z);
glVertex3f(-1.0 + x, 1.0 + y, 1.0 + z);
glVertex3f(0.999999 + x, 1.0 + y, 1.000001 + z);
glVertex3f(1.0 + x, -1.0 + y, -1.0 + z);
glVertex3f(1.0 + x, 1.0 + y, -0.999999 + z);
glVertex3f(0.999999 + x, 1.0 + y, 1.000001 + z);
glVertex3f(1.0 + x, -1.0 + y, 1.0 + z);
glVertex3f(1.0 + x, -1.0 + y, 1.0 + z);
glVertex3f(0.999999 + x, 1.0 + y, 1.000001 + z);
glVertex3f(-1.0 + x, 1.0 + y, 1.0 + z);
glVertex3f(-1.0 + x, -1.0 + y, 1.0 + z);
glVertex3f(-1.0 + x, -1.0 + y, 1.0 + z);
glVertex3f(-1.0 + x, 1.0 + y, 1.0 + z);
glVertex3f(-1.0 + x, 1.0 + y, -1.0 + z);
glVertex3f(-1.0 + x, -1.0 + y, -1.0 + z);
glVertex3f(1.0 + x, 1.0 + y, -0.999999 + z);
glVertex3f(1.0 + x, -1.0 + y, -1.0 + z);
glVertex3f(-1.0 + x, -1.0 + y, -1.0 + z);
glVertex3f(-1.0 + x, 1.0 + y, -1.0 + z);
glEnd();
glTranslatef(x,y,z);
}```

PartCubes.h:

C++
```class PartCubes
{
public:
PartCubes(void);
~PartCubes(void);
void draw(float time);
float xpos;
float ypos;
float zpos;
void drawCube(float x, float y, float z, float xrot);
};```
Posted
Updated 23-Jan-12 19:24pm
v2

## Solution 1

1. Use the glTranslate(-x, -y, -z) function to move the object relative to (0,0,0)
2. Call glRotate() to rotate the objects, relative to (0,0,0)
3. Call glTranslate(x, y, z) to move the objects back to a position relative to (x,y,z).

Be sure that you draw your model relative to (0,0,0), and let OpenGL do the work of moving it around.

C++
```void PartCubes::drawCube(float x, float y, float z, float xrot)
{
glPushMatrix();

static float xoff = 1.0f;
static float yoff = 0.5f;
static float zoff = 0.0f;

glTranslatef(x,y,z);
glRotatef(xrot, 0, 1, 0);
glTranslatef(xoff,yoff,zoff);

drawCube();

glTranslatef(x,y,z);

glPopMatrix();
}

void PartCubes::drawCube(void)
{

glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -0.0f);
glVertex3f(0.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
}```

v4
Liam S. Crouch 24-Jan-12 1:22am
When i do that, the cubes run around in spiral. I'l update the post with src

## Solution 2

Rotations are done around Origin(0,0,0).

So if you need to rotate around a point other than origin, you need to back translate by that amount, and carry out the rotation then again translate to that point. It is pretty straight forward.

If your object is a convex shape, you can find the center easily right ? Then back translate using glTranslate(-object.x,-object.y-object.z) then do rotation using glRotate(...) then again translate to the original intended location using same glTranslate(object.x,object.y,object.z).

Hopes this helps
http://cgmath.blogspot.com/

## Solution 3

Use matrixes. That made it work