Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ OpenGL
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
#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);
	glBegin(GL_QUADS);
		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:
 
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 23-Jan-12 11:07am
Edited 23-Jan-12 20:24pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

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.
 
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)
{
	glBegin(GL_QUADS);
 
	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();
}
  Permalink  
v4
Comments
Member 7991625 at 24-Jan-12 1:22am
   
When i do that, the cubes run around in spiral. I'l update the post with src
Rate this: bad
good
Please Sign up or sign in to vote.

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/
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use matrixes. That made it work
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 200
1 George Jonsson 175
2 Kornfeld Eliyahu Peter 169
3 PIEBALDconsult 110
4 Richard MacCutchan 85
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 25 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100