Advertisement

can i get some help?

Started by April 26, 2002 06:04 PM
10 comments, last by _Titan_ 22 years, 10 months ago
I sent a post a few days ago about not beign able to rotate my object, because it rotates the whole screen. Can someone take a look at my code and see if they can help me? I''ll zip it up and email it to you. I tried translating to the object 1st then drawing it but that didnt work. I tried a whole bunch of stuff. Now i am just out of ideas. Thanks in advance.
not exactly clear on what you''re doing, but have you tried using glPushMatrix() and glPopMatrix() ?
www.FlegDev.tkJust code it!
Advertisement
i have 5 dice, if i want to rotate them, i can not reotate any one individual die. It rotates the whole scene. See?
You need to push the matrix, translate to center of the cube, rotate on whatever axis, draw cube, then pop the matrix.

ok sweet
will try that
No it didnt work
Now they look like syncronized swimmers in dice suites! LOL! They rotate then they all come in and meet then they go back out again! HAHA very funny. I will post the function below

int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer

glLoadIdentity(); // Reset The Current Modelview Matrix
// Move Left 1.5 Units And Into The Screen 6.0
glTranslatef(0.0,-3.0,-6.0);

Dice[0].DrawBox();
Dice[1].DrawBox();
Dice[2].DrawBox();
Dice[3].DrawBox();
Dice[4].DrawBox();

glPrint(5,5,TEXT);
rot+=1.55;

return TRUE; // Keep Going
}

int Box::DrawBox() {

glBindTexture(GL_TEXTURE_2D, textures[Box::Textures[0]].texID);
glPushMatrix();
glTranslatef(Box::x,Box::y,Box::z);

glRotatef(rot,0.0,1.0,0.0);

glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glTexCoord2f(0.0f,0.0f); glVertex3f(Box::x + Box::width/2,Box::y + Box::height/2,Box::z + Box::depth/2);
glTexCoord2f(1.0f,0.0f); glVertex3f(Box::x + Box::width/2,Box::y - Box::height/2,Box::z + Box::depth/2);
glTexCoord2f(1.0f,1.0f); glVertex3f(Box::x - Box::width/2,Box::y - Box::height/2,Box::z + Box::depth/2);
glTexCoord2f(0.0f,1.0f); glVertex3f(Box::x - Box::width/2,Box::y + Box::height/2,Box::z + Box::depth/2);
glEnd();

glBindTexture(GL_TEXTURE_2D, textures[Box::Textures[1]].texID);
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glTexCoord2f(0.0f,0.0f); glVertex3f(Box::x - Box::width/2,Box::y + Box::height/2,Box::z + Box::depth/2);
glTexCoord2f(1.0f,0.0f); glVertex3f(Box::x - Box::width/2,Box::y - Box::height/2,Box::z + Box::depth/2);
glTexCoord2f(1.0f,1.0f); glVertex3f(Box::x - Box::width/2,Box::y - Box::height/2,Box::z - Box::depth/2);
glTexCoord2f(0.0f,1.0f); glVertex3f(Box::x - Box::width/2,Box::y + Box::height/2,Box::z - Box::depth/2);
glEnd();

glBindTexture(GL_TEXTURE_2D, textures[Box::Textures[2]].texID);
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glTexCoord2f(0.0f,0.0f); glVertex3f(Box::x - Box::width/2,Box::y + Box::height/2,Box::z - Box::depth/2);
glTexCoord2f(1.0f,0.0f); glVertex3f(Box::x - Box::width/2,Box::y - Box::height/2,Box::z - Box::depth/2);
glTexCoord2f(1.0f,1.0f); glVertex3f(Box::x + Box::width/2,Box::y - Box::height/2,Box::z - Box::depth/2);
glTexCoord2f(0.0f,1.0f); glVertex3f(Box::x + Box::width/2,Box::y + Box::height/2,Box::z - Box::depth/2);
glEnd();

glBindTexture(GL_TEXTURE_2D, textures[Box::Textures[3]].texID);
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glTexCoord2f(0.0f,0.0f); glVertex3f(Box::x + Box::width/2,Box::y + Box::height/2,Box::z - Box::depth/2);
glTexCoord2f(1.0f,0.0f); glVertex3f(Box::x + Box::width/2,Box::y - Box::height/2,Box::z - Box::depth/2);
glTexCoord2f(1.0f,1.0f); glVertex3f(Box::x + Box::width/2,Box::y - Box::height/2,Box::z + Box::depth/2);
glTexCoord2f(0.0f,1.0f); glVertex3f(Box::x + Box::width/2,Box::y + Box::height/2,Box::z + Box::depth/2);
glEnd();

glBindTexture(GL_TEXTURE_2D, textures[Box::Textures[4]].texID);
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glTexCoord2f(0.0f,0.0f); glVertex3f(Box::x + Box::width/2,Box::y + Box::height/2,Box::z - Box::depth/2);
glTexCoord2f(1.0f,0.0f); glVertex3f(Box::x + Box::width/2,Box::y + Box::height/2,Box::z + Box::depth/2);
glTexCoord2f(1.0f,1.0f); glVertex3f(Box::x - Box::width/2,Box::y + Box::height/2,Box::z + Box::depth/2);
glTexCoord2f(0.0f,1.0f); glVertex3f(Box::x - Box::width/2,Box::y + Box::height/2,Box::z - Box::depth/2);
glEnd();

glBindTexture(GL_TEXTURE_2D, textures[Box::Textures[5]].texID);
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glTexCoord2f(0.0f,0.0f); glVertex3f(Box::x + Box::width/2,Box::y - Box::height/2,Box::z - Box::depth/2);
glTexCoord2f(1.0f,0.0f); glVertex3f(Box::x + Box::width/2,Box::y - Box::height/2,Box::z + Box::depth/2);
glTexCoord2f(1.0f,1.0f); glVertex3f(Box::x - Box::width/2,Box::y - Box::height/2,Box::z + Box::depth/2);
glTexCoord2f(0.0f,1.0f); glVertex3f(Box::x - Box::width/2,Box::y - Box::height/2,Box::z - Box::depth/2);
glEnd();
glPopMatrix();
return true;
}
Advertisement
I don''t know what values you set to Box::x,Box::y,Box::z so...First, I wouldnt worry about translating about them (*yet) Just call glLoadIdentity() before each quad, translate the zaxis by say 20, and before you do all of this push the matrix stack, and then "pull" it after you''re done. This should work and cause every entity to act by itself. I know this seems to be very similar to what you''re doing, but do what I said and see what happens.
masterghttp:/masterg.andyc.org
Ok, i tried that but no luck, the boxes just sit there in space and do nothing, not even move. Any other suggestions?

BTW: Much sppriciated for the help
Do this for evey cube:

glPushMatrix();
glTranslatef(whatever);
glRotate(whatever);
DrawCube();
glPopMatrix();

now change the whatever values for each cube and be sure theyre not the same and you can control them all this way, now the move independant from each other

btw.. keep track of every rotation by creating a rotating value for each cube

-xill
Hi there, sorry about this post, but I can't remember how to display code in those nice little white boxes, so here goes....

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
Dice[0].DrawBox();
Dice[1].DrawBox();
Dice[2].DrawBox();
Dice[3].DrawBox();
Dice[4].DrawBox();

glPrint(5,5,TEXT);
rot+=1.55;

return TRUE; // Keep Going
}

int Box::DrawBox() {
glPushMatrix();
glTranslatef(0.0,-3.0,-6.0); // Move Left 1.5 Units And Into The Screen 6.0
glTranslatef(Box::x,Box::y,Box::z); // ** U already had this line in, but
// were also 'giving' the position in the
// glVertex3f lines below where you used box::x+Box::Width/2, box::y... etc
// These parts have been killed!
glPushMatrix();
glRotatef(rot, 0.0, 1.0, 0.0);
glBindTexture(GL_TEXTURE_2D, textures[Box::Textures[0]].texID);

glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glTexCoord2f(0.0f,0.0f); glVertex3f(+Box::width/2, +Box::height/2, +Box::depth/2);
glTexCoord2f(1.0f,0.0f); glVertex3f(+Box::width/2, -Box::height/2, +Box::depth/2);
glTexCoord2f(1.0f,1.0f); glVertex3f(-Box::width/2, -Box::height/2, +Box::depth/2);
glTexCoord2f(0.0f,1.0f); glVertex3f(-Box::width/2, +Box::height/2, +Box::depth/2);
glEnd();

glBindTexture(GL_TEXTURE_2D, textures[Box::Textures[1]].texID);
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glTexCoord2f(0.0f,0.0f); glVertex3f(-Box::width/2, +Box::height/2, +Box::depth/2);
glTexCoord2f(1.0f,0.0f); glVertex3f(-Box::width/2, -Box::height/2, +Box::depth/2);
glTexCoord2f(1.0f,1.0f); glVertex3f(-Box::width/2, -Box::height/2, -Box::depth/2);
glTexCoord2f(0.0f,1.0f); glVertex3f(-Box::width/2, +Box::height/2, -Box::depth/2);
glEnd();

glBindTexture(GL_TEXTURE_2D, textures[Box::Textures[2]].texID);
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glTexCoord2f(0.0f,0.0f); glVertex3f(-Box::width/2, +Box::height/2, -Box::depth/2);
glTexCoord2f(1.0f,0.0f); glVertex3f(-Box::width/2, -Box::height/2, -Box::depth/2);
glTexCoord2f(1.0f,1.0f); glVertex3f(+Box::width/2, -Box::height/2, -Box::depth/2);
glTexCoord2f(0.0f,1.0f); glVertex3f(+Box::width/2, +Box::height/2, -Box::depth/2);
glEnd();

glBindTexture(GL_TEXTURE_2D, textures[Box::Textures[3]].texID);
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glTexCoord2f(0.0f,0.0f); glVertex3f(+Box::width/2, +Box::height/2, -Box::depth/2);
glTexCoord2f(1.0f,0.0f); glVertex3f(+Box::width/2, -Box::height/2, -Box::depth/2);
glTexCoord2f(1.0f,1.0f); glVertex3f(+Box::width/2, -Box::height/2, +Box::depth/2);
glTexCoord2f(0.0f,1.0f); glVertex3f(+Box::width/2, +Box::height/2, +Box::depth/2);
glEnd();

glBindTexture(GL_TEXTURE_2D, textures[Box::Textures[4]].texID);
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glTexCoord2f(0.0f,0.0f); glVertex3f(+Box::width/2, +Box::height/2,-Box::depth/2);
glTexCoord2f(1.0f,0.0f); glVertex3f(+Box::width/2, +Box::height/2,+Box::depth/2);
glTexCoord2f(1.0f,1.0f); glVertex3f(-Box::width/2, +Box::height/2,+Box::depth/2);
glTexCoord2f(0.0f,1.0f); glVertex3f(-Box::width/2, +Box::height/2,-Box::depth/2);
glEnd();

glBindTexture(GL_TEXTURE_2D, textures[Box::Textures[5]].texID);
glBegin(GL_QUADS);
glColor3f(1.0,1.0,1.0);
glTexCoord2f(0.0f,0.0f); glVertex3f(+Box::width/2, -Box::height/2, -Box::depth/2);
glTexCoord2f(1.0f,0.0f); glVertex3f(+Box::width/2, -Box::height/2, +Box::depth/2);
glTexCoord2f(1.0f,1.0f); glVertex3f(-Box::width/2, -Box::height/2, +Box::depth/2);
glTexCoord2f(0.0f,1.0f); glVertex3f(-Box::width/2, -Box::height/2, -Box::depth/2);
glEnd();

glPopMatrix();
return true;
}

My (dodgy) Explanation

Example1.
glLoadIdentity(); // Reset matrix
glTranslatef(IntoScreenabit); // Move into screen
glTranslatef(dicePosition); // Move to dice position
glRotatef(); // Rotate (dice)
DrawDice();

This will draw the dice at it's correct position and rotated about it's own axis, this is what you were doing, except that in the actual drawing of the die (In glVertex3f(+Box::width/2, -Box::height/2, -Box::depth/2) The +Box::width/2 etc was err 'giving' another position, making the pseudo code look like this...

glLoadIdentity();
glTranslatef(IntoScreenAbit);
glTranslatef(dicePosition);
glRotatef(diceAngle);
glTranslatef(ExtraDicePosition); // New (And screws stuff up)

Screws stuff up because...

Once you have called glRotate, the translate err, translates from the angle given from glRotate rather than from the initial matrix just after glLoadIdentity();

Example, if the diceAngle in the above code is 90degrees along the Y axis and then you translated 5 units along the X axis and drew the dice again, it would not be 5 units to the right of the last dice, but instead, be drawn five units closer to the screen.

Lastly, the glPushMatrix() and glPopMatrix() commands are used to 'store' and 'retrieve' the matrix so that you can effectivly undo any translations/rotations since glPushMatrix(), usefull if drawing multiple dice

If anything is unclear, I appologise as I'm rubbish at explaining things

[edited by - TerraX on May 1, 2002 3:15:56 AM]

This topic is closed to new replies.

Advertisement