Local origin rotation

Started by
4 comments, last by SilentSounD 16 years, 6 months ago
Hey guys, I'm new here. I'm trying to rotate a cube that is selected around it's local origin, 180 degrees so that the bottom face is on top. i want the cube's position to remain the same (rotation in place) so to speak. I've tried several different things, but i can't seem to figure this out. any help would be greatly appreciated! below is my draw scene code:

glRotatef(rotateX, 1, 0, 0); //Mouse rotation around X-Axis
glRotatef(rotateY, 0, 1, 0); //Mouse rotation around Y-Axis
int numDraw = 0;
if (iGame->started) {
	for ( vector<Match *>::iterator it = Match::matches.begin(); it != Match::matches.end(); it++) {
		if (numDraw < iGame->iLevel)
		{
			(*it)->drawMatch();
			numDraw++;
		}
	}
}










this is my draw cube code:

void Match::drawMatch(void)
{
	if (!visible) return;

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	if (!texture)
		glDisable(GL_TEXTURE_2D);

	if (!selected) 
		glColor3f(1.0f, 1.0f, 1.0f);
	else
		glColor3f(0.2f, 0.1f, 1.0f);

	glBindTexture(GL_TEXTURE_2D, texture->texID);

	if (selected)
	{
		glPushMatrix();
		glTranslatef( 0.0, h * 3, 0); //not sure why but h * 3 seems to put the square at the right height
		glRotatef(180, 0, 0, 1);
	}

	glPushName( (int)this);
	glNormal3f(0, -1, 0);
	glBegin(GL_QUADS);
		//top face
		glTexCoord2f(1,					1);
		glVertex3d( x + w,				y + h, z );

		glTexCoord2f(1,					0);
		glVertex3d( x + w,				y + h, z + w);

		glTexCoord2f(0,					0);
		glVertex3d( x,					y + h, z  + w);

		glTexCoord2f(0,					1);
		glVertex3d( x,					y + h, z);
	glEnd();
	
	glBindTexture(GL_TEXTURE_2D, baseText->texID);
	glBegin(GL_QUADS);
		//front face
		glTexCoord2f(1,					1);
		glVertex3d( x + w,				y, z + w);

		glTexCoord2f(1,					0);
		glVertex3d( x,					y, z + w);

		glTexCoord2f(0,					0);
		glVertex3d( x,				y + h, z + w);

		glTexCoord2f(0,					1);
		glVertex3d( x + w,			y + h, z + w);

		//botttom face
		glTexCoord2f(1,					1);
		glVertex3d( x + w,				y, z );

		glTexCoord2f(1,					0);
		glVertex3d( x + w,				y, z + w);

		glTexCoord2f(0,					0);
		glVertex3d( x,					y, z  + w);

		glTexCoord2f(0,					1);
		glVertex3d( x,					y, z);

		//right face
		glTexCoord2f(0,					1);
		glVertex3d( x + w,				y + h, z );

		glTexCoord2f(1,					1);
		glVertex3d( x + w,				y, z);

		glTexCoord2f(1,					0);
		glVertex3d( x + w,				y, z  + w);

		glTexCoord2f(0,					0);
		glVertex3d( x + w,				y + h, z + w);

		//left face
		glTexCoord2f(0,					1);
		glVertex3d( x,				y + h, z );

		glTexCoord2f(1,					1);
		glVertex3d( x,				y, z);

		glTexCoord2f(1,					0);
		glVertex3d( x,				y, z  + w);

		glTexCoord2f(0,					0);
		glVertex3d( x,				y + h, z + w);
	glEnd();

	glPopName();
	if (selected) {
		glPopMatrix();
	}
}









this is how the cube's x,y,z positions are set

void Match::updatePositions(void) {
	int level = iGame->iLevel; //iGame->iLevel can be 16, 36 or 64 (16 = 8x8  squares, etc.)
	int sqrtLvl = sqrt((double)level);
	int xoffset = -((sqrtLvl * w + offsetX) / 2);
	int zoffset = -((sqrtLvl * w + offsetZ) / 2);
	int x = 0;
	
	for (int i = 0; i < level; i++) {
		Match::matches->setPos(xoffset, h, zoffset);
		Match::matches->setVisible(true);
		Match::matches->setSelected(false);

		xoffset += w + offsetX;
		x++;

		if (x == sqrtLvl) {
			xoffset = -((sqrtLvl * w + offsetX / 2) / 2);
			zoffset += h + offsetZ;
			x = 0;
		}
	}
}


notice how the blue cube is on the opposite side of where the "Hole" is plus an offset. [Edited by - SilentSounD on September 25, 2007 3:24:47 AM]
Advertisement
The problem I think is that you are drawing the cubes in global coordinates instead of local. Your cube drawing code should be generic and look like:
drawCube(int n)
{
glPushMatrix();
glTranslatef(cube[n].x, cube[n].y, cube[n].z)
<- put local rotation here
glBegin();
// draw front face
glVertex3F(-cubew, -cubeh, -cubed);
glVertex3F(cubew, -cubeh, -cubed);
glVertex3F(cubew, cubeh, -cubed);
glVertex3F(-cubew, cubeh, -cubed);
// draw other faces etc...
glEnd();
glPopMatrix();
}
i've tried both.

They both have the same result.
Quote:Original post by SilentSounD
i've tried both.

They both have the same result.


And you are absolutely sure that you FIRST translated and then rotated AND constructed the cube around the origin instead of god-knows-where? Because your code and his code would seem to be doing two pretty different things. Something that might help to understand the issue might be to use one single display list for a cube and then use this and only this one display list to draw all cubes in their positions (ignoring different textures for now).
f@dzhttp://festini.device-zero.de
yeah, i realize i should be drawing the cubes at translate to 0,0,0, rotate and then translate them to where they should be.

thanks for the help so-far, i'll try this again later.
Just wanted you all to know that i got it working:

here is my draw code now
	int numDraw = 0;	int row = 0;	int sqrtLvl = sqrt((double)iGame->iLevel);	glTranslatef(-8 * (sqrtLvl/2) + 1, 0, -8 * (sqrtLvl/2) + 1);	if (iGame->started) {		for ( vector<Match *>::iterator it = Match::matches.begin(); it != Match::matches.end(); it++) {			if (numDraw < iGame->iLevel)			{							//(*it)->drawMatch();				if ((*it)->getSelected()) {					glColor3f(1,0,0);					glPushMatrix();					//glTranslatef(0,0,0);					glRotatef(180, 0, 0, 1);					glTranslatef(0,0,0);				} else {					glColor3f(1,1,1);				}				if ((*it)->visible) {					glBindTexture(GL_TEXTURE_2D, (*it)->texture->texID);					glPushName((int)(*it));					glBegin(GL_QUADS);					//top face					glTexCoord2f(1,				1);					glVertex3d( -4,		4,		-4 );					glTexCoord2f(1,				0);					glVertex3d( 4,		4,		-4);					glTexCoord2f(0,				0);					glVertex3d( 4,		4,		4);					glTexCoord2f(0,				1);					glVertex3d( -4,		4,		4);					glEnd();					glBindTexture(GL_TEXTURE_2D, (*it)->baseText->texID);					glBegin(GL_QUADS);					//top face					glTexCoord2f(1,				1);					glVertex3d( -4,		-4,		-4 );					glTexCoord2f(1,				0);					glVertex3d( 4,		-4,		-4);					glTexCoord2f(0,				0);					glVertex3d( 4,		-4,		4);					glTexCoord2f(0,				1);					glVertex3d( -4,		-4,		4);					//front face					glTexCoord2f(1,				1);					glVertex3d( -4,		4,		4 );					glTexCoord2f(1,				0);					glVertex3d( -4,		-4,		4);					glTexCoord2f(0,				0);					glVertex3d( 4,		-4,		4);					glTexCoord2f(0,				1);					glVertex3d( 4,		4,		4);					glEnd();					glPopName();				}				numDraw++;				row++;				if ((*it)->getSelected()) glPopMatrix();				if (row == sqrtLvl) {					glTranslatef(-10 * sqrtLvl, 0, 10);					row = 0;				}				glTranslatef(10, 0, 0);			}		}

This topic is closed to new replies.

Advertisement