Issue
I'm trying to rotate 2 objects independently but when I run the code both objects rotate in the same direction.
Here I save the matrix and rotate a square under the cube:
def rotate_square():
glColor3f(1,1,1)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glRotatef(45,0,1,0)
glBegin(GL_LINES)
glVertex3fv(v1);
glVertex3fv(v2);
glVertex3fv(v1);
glVertex3fv(v3);
glVertex3fv(v3);
glVertex3fv(v4);
glVertex3fv(v2);
glVertex3fv(v4);
glEnd()
glPopMatrix()
Main function:
def main():
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE, DOUBLEBUF|OPENGL)
resize(*SCREEN_SIZE)
print(glGetString(GL_VERSION))
gluLookAt(0, 0, -6, 0, 0, 0, 0, 1, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Rotating the cube:
glRotatef(1,0,1,0)
glutWireCube(2)
rotate_square()
pygame.display.flip()
pygame.time.wait(10)
Solution
Note, that drawing by glBegin
/glEnd
sequences, the fixed function pipeline matrix stack and fixed function pipeline per vertex light model, is deprecated since decades.
Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.
Anyway, in common an object is transformed by the model matrix, then the scene is transformed by the view matrix and the projection matrix.
Sadly at the deprecated OpenGL fixed function pipeline, the model matrix and the view matrix are concatenated and can't be handled separately that easy.
You have to transform each object by its own model matrix to rotate them independently. Create the 4*4 matrices and initialize them by the identity matrix:
model1 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
model2 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
To manipulate the model matrices, load them to the matrix stack by glLoadMatrix
. Then manipulate them and finally read the result back by glGetFloatv(GL_MODELVIEW_MATRIX)
:
glPushMatrix()
glLoadMatrixf(model1)
glRotatef(1,0,1,0)
model1 = glGetFloatv(GL_MODELVIEW_MATRIX)
glPopMatrix()
If the model matrix has to be applied, it can be multiplied to the matrix stack by glMultMatrix
:
glPushMatrix()
glMultMatrixf(model1)
glutWireCube(2)
glPopMatrix()
The final code may look like this:
model1 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
model2 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
def rotate_square():
global model2
glPushMatrix()
glLoadMatrixf(model2)
glRotatef(5,0,1,0)
model2 = glGetFloatv(GL_MODELVIEW_MATRIX)
glPopMatrix()
glColor3f(1,1,1)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glMultMatrixf(model2)
glBegin(GL_LINES)
glVertex3fv(v1)
glVertex3fv(v2)
glVertex3fv(v1)
glVertex3fv(v3)
glVertex3fv(v3)
glVertex3fv(v4)
glVertex3fv(v2)
glVertex3fv(v4)
glEnd()
glPopMatrix()
def main():
global model1
# [...]
while True:
# [...]
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadMatrixf(model1)
glRotatef(1,0,1,0)
model1 = glGetFloatv(GL_MODELVIEW_MATRIX)
glPopMatrix()
glPushMatrix()
glMultMatrixf(model1)
glutWireCube(2)
glPopMatrix()
rotate_square()
pygame.display.flip()
pygame.time.wait(10)
Answered By - Rabbid76
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.