如何分别旋转多个四边形?(Pygame,PyOpengl) [英] How can I rotate multiple quads separately? (Pygame, PyOpengl)

查看:70
本文介绍了如何分别旋转多个四边形?(Pygame,PyOpengl)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想大致围绕它们自己的轴旋转我的四边形.我的第一个想法是使用 glPushMatrix() 和 glPophMatrix(),但不起作用.无论如何,我相信这是个好方法.

代码:

导入时间,pygame从 pygame.locals 导入 *从 OpenGL.GL 导入 *从 OpenGL.GLU 导入 *def makeQuad():glBegin(GL_QUADS)glVertex2f(0, 0)glVertex2f(0, 50)glVertex2f(50, 50)glVertex2f(50, 0)glEnd()定义主():pygame.init()显示 = (800,800)pygame.display.set_mode(显示,DOUBLEBUF|OPENGL)gluOrtho2D(0, 800, 0, 800)为真:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:pygame.quit()放弃()glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)glPushMatrix()glTranslated(150, 0, 0)makeQuad()glTranslated(100, 250, 0)makeQuad()glPopMatrix()pygame.display.flip()主要的()

解决方案

注意,不推荐使用 glBegin/glEnd 序列和固定函数管道矩阵堆栈绘制几十年来.阅读

I'd like to rotate my quads roughly around they own axis. My first thought was to use glPushMatrix() and glPophMatrix(), but doesn't work. Regardless of that I believe this is the good way.

Code:

import time, pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

def makeQuad():
    glBegin(GL_QUADS)
    glVertex2f(0, 0)
    glVertex2f(0, 50)
    glVertex2f(50, 50)
    glVertex2f(50, 0)
    glEnd()

def main():
    pygame.init()
    display = (800,800)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    gluOrtho2D(0, 800, 0, 800)

    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)

        glPushMatrix()
        glTranslated(150, 0, 0)

        makeQuad()

        glTranslated(100, 250, 0)

        makeQuad()

        glPopMatrix()

        pygame.display.flip()

main()

解决方案

Note, that drawing by glBegin/glEnd sequences and the fixed function pipeline matrix stack, 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, OpenGL has different matrix modes, which are GL_MODELVIEW, GL_PROJECTION, and GL_TEXTURE - see glMatrixMode.
Ech matrix mode provides a current matrix and a matrix stack. glPushMatrix / glPopMatrix push the current matrix to the stack and pop the top mast matrix form the stack and store it to the current matrix

If you want to do an individual matrix transformation to an object, then you have to:

  • Push the current matrix to the stack by glPushMatrix
  • Do the transformations like glTranslate, glRotate, glScale or even glLoadMatrix which loads a completely new matrix. For the sak of completeness, there als exists glLoadIdentity and glMultMatrix.
  • Draw the object.
  • Pop the top most matrix glPopMatrix, to restore the current matrix and the matrix stack. Now the current matrix and the matrix stack seem to be so, as the above transformations never would have happened.

e.g.

glPushMatrix()
glTranslated(250, 100, 0)
glRotated(5, 0, 0, 1)
makeQuad()
glPopMatrix()

If you want to animate the quads separately, then i recommend to use the 2 control variables a1 and a2, which control the angle of rotation and are incremented ongoing in the main loop:

a1, a2 = 0, 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)

    glPushMatrix()
    glTranslated(250, 100, 0)
    glRotated(a1, 0, 0, 1)
    makeQuad()
    glPopMatrix()

    glPushMatrix()
    glTranslated(100, 250, 0)
    glRotated(a2, 0, 0, 1)
    makeQuad()
    glPopMatrix()

    a1 += 1
    a2 += 2

    pygame.display.flip()

Preview:

这篇关于如何分别旋转多个四边形?(Pygame,PyOpengl)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆