Rotaciones Python OpenGL
Rotaciones Python OpenGL
ShapeID = 0
jump = 0.1
rotation_jump = 45.0
rotations = {
1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0,
5: 0.0, 6: 0.0, 7: 0.0
}
rotation_direction = 1
positions = {
1: [0.0, 0.0], 2: [0.0, 0.0], 3: [0.25, 0.25],
4: [0.0, 0.0], 5: [0.0, 0.0], 6: [-0.25, -0.25], 7: [0.5, 0.0]
}
def BlueTriangle():
inc = 0.5
glColor3f(0.0, 1.0, 1.0)
glBegin(GL_TRIANGLES)
glVertex2f(0.0, 0.0)
glVertex2f(-inc, inc)
glVertex2f(inc, inc)
glEnd()
return [0.0, 1/3]
def RedTriangle():
inc = 0.5
glColor3f(1.0, 0.0, 0.0)
glBegin(GL_TRIANGLES)
glVertex2f(0.0, 0.0)
glVertex2f(-inc, -inc)
glVertex2f(-inc, inc)
glEnd()
return [-1/3, 0.0]
def GreenTriangle():
inc = 0.25
glColor3f(0.0, 1.0, 0.0)
glBegin(GL_TRIANGLES)
glVertex2f(0.0, 0.0)
glVertex2f(inc, inc)
glVertex2f(inc, -inc)
glEnd()
return [0.5/3, 0.0]
def YellowSquare():
inc = 0.25
glColor3f(1.0, 1.0, 0.0)
glBegin(GL_QUADS)
glVertex2f(0.0, 0.0)
glVertex2f(inc, inc)
glVertex2f(2.0 * inc, 0.0)
glVertex2f(inc, -inc)
glEnd()
return
def PinkTriangle():
inc = 0.25
glColor3f(1.0, 0.0, 0.5)
glBegin(GL_TRIANGLES)
glVertex2f(0.0, 0.0)
glVertex2f(inc, -inc)
glVertex2f(-inc, -inc)
glEnd()
return [0.0, -0.5/3]
def WhiteRhomboid():
inc = 0.25
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_QUADS)
glVertex2f(0.0, 0.0)
glVertex2f(2.0 * inc, 0.0)
glVertex2f(inc, -inc)
glVertex2f(-inc, -inc)
glEnd()
return [0.125, -0.125]
def NavyTriangle():
inc = 0.5
glColor3f(0.3, 0.6, 1.0)
glBegin(GL_TRIANGLES)
glVertex2f(0.0, 0.0)
glVertex2f(0.0, -inc)
glVertex2f(-inc, -inc)
glEnd()
return [-0.5/3, -1/3]
def GetCentroid(shape_id):
if shape_id == 1:
return BlueTriangle.centroid
elif shape_id == 2:
return RedTriangle.centroid
elif shape_id == 3:
return GreenTriangle.centroid
elif shape_id == 4:
return YellowSquare.centroid
elif shape_id == 5:
return PinkTriangle.centroid
elif shape_id == 6:
return WhiteRhomboid.centroid
elif shape_id == 7:
return NavyTriangle.centroid
return
def DrawShape(shape_id):
if shape_id == 1:
BlueTriangle()
elif shape_id == 2:
RedTriangle()
elif shape_id == 3:
GreenTriangle()
elif shape_id == 4:
YellowSquare()
elif shape_id == 5:
PinkTriangle()
elif shape_id == 6:
WhiteRhomboid()
elif shape_id == 7:
NavyTriangle()
def DrawScene():
glClear(GL_COLOR_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
for shape_id, (x, y) in positions.items():
centroid_x, centroid_y = GetCentroid(shape_id)
glPushMatrix()
glTranslatef(x, y, 0.0)
glTranslatef(centroid_x, centroid_y, 0.0)
glRotatef(rotations[shape_id], 0.0, 0.0, 1.0)
glTranslatef(-centroid_x, -centroid_y, 0.0)
DrawShape(shape_id)
glPopMatrix()
glutSwapBuffers()
if isinstance(key, bytes):
key = key.decode("utf-8")
if key.isdigit():
ShapeID = int(key)
if ShapeID in positions:
if key == 'r':
rotations[ShapeID] += rotation_direction * rotation_jump
elif key == 'd':
rotation_direction *= -1
glutPostRedisplay()
def init():
glClearColor(0.0, 0.0, 0.0, 1.0)
gluOrtho2D(-2, 2, -1.5, 1.5)
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
glutInitWindowSize(800, 600)
glutCreateWindow(b"Tangram 2D")
init()
glutDisplayFunc(DrawScene)
glutKeyboardFunc(handle_key)
glutSpecialFunc(handle_special_key)
glutMainLoop()
if __name__ == "__main__":
main()