简体   繁体   English

如何使用 Modern OpenGL 和 Python 旋转三角形

[英]How to rotate the triangle with Modern OpenGL and Python

I have written a code to render a triangle using a shader program.我编写了一个代码来使用着色器程序渲染三角形。 I want to rotate the triangle.我想旋转三角形。 I'm using PyGLM to set a transformation matrix.我正在使用 PyGLM 设置转换矩阵。 Here I'm presenting the whole code.在这里,我将展示整个代码。 If I run this code a triangle is appearing in the window as expected, but there is no rotation.如果我运行此代码,则会按预期在 window 中出现一个三角形,但没有旋转。 I think I've failed to pass the transformation matrix to the buffer.我想我未能将转换矩阵传递给缓冲区。

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from OpenGL.GL import shaders

import numpy as np
import glm


VERTEX_SHADER = """

#version 330

    in vec4 position;
    in vec3 color;
    out vec3 newColor;
    
    void main()
    {
    gl_Position = position;
    newColor = color;
    }

"""

FRAGMENT_SHADER = """
#version 330
    in vec3 newColor;
    out vec4 outColor;
    
    void main()
    {

        outColor = vec4(newColor,1.0f);

    }

"""

shaderProgram = None

def initliaze():
    global VERTEXT_SHADER
    global FRAGMEN_SHADER
    global shaderProgram

    vertexshader = shaders.compileShader(VERTEX_SHADER, GL_VERTEX_SHADER)
    fragmentshader = shaders.compileShader(FRAGMENT_SHADER, GL_FRAGMENT_SHADER)

    shaderProgram = shaders.compileProgram(vertexshader, fragmentshader)


    triangles = [-0.5, -0.5, 0.0, 1.0,0.0,0.0,
                 0.5, -0.5, 0.0,  0.0,1.0,0.0,
                 0.0, 0.5, 0.0,    0,0,0.0,1.0]

    triangles = np.array(triangles, dtype=np.float32)


    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, triangles.nbytes, triangles, GL_DYNAMIC_DRAW)


    position = glGetAttribLocation(shaderProgram, 'position')
    glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
    glEnableVertexAttribArray(position)
   
    color = glGetAttribLocation(shaderProgram, 'color')
    glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
    glEnableVertexAttribArray(color)


def render():
    global shaderProgram
    global angle
    
   
    #shader
    glUseProgram(shaderProgram)


    
    glClearColor(0, 0, 0, 1)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    #transform matrix
    transform = glm.mat4(1)
    transform = glm.translate(transform, glm.vec3(0.5,-0.5,0.0))
    transform = glm.rotate(transform, glutGet(GLUT_ELAPSED_TIME),glm.vec3(0,0,1))
    
    transformLoc = glGetUniformLocation(shaderProgram,"transform")
    glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm.value_ptr(transform))
    
    
    
  
    #render program
    glDrawArrays(GL_TRIANGLES, 0, 3)
    


    
    glUseProgram(0)
    glutSwapBuffers()

    
def main():
   
    glutInit([])
    glutInitWindowSize(640, 480)
    glutCreateWindow("pyopengl with glut 2")
    initliaze()
    glutDisplayFunc(render)
    glutMainLoop()


if __name__ == '__main__':
    main()

In VERTEX_SHADER you didn't mentioned transform variable.在 VERTEX_SHADER 你没有提到变换变量。 So your triangle position remain fixed after you run the program.所以你的三角形 position 在你运行程序后保持不变。 Change your VERTEX_SHADER as following.如下更改您的 VERTEX_SHADER。

VERTEX_SHADER = """

#version 330

    in vec4 position;
    in vec3 color;
    out vec3 newColor;
    uniform mat4 transform;
    
    void main()
    {
    gl_Position = transform*position;
    newColor = color;
    }
"""

In your code you are accessing the location of location of a uniform variable transform by following line.在您的代码中,您正在通过以下行访问统一变量变换的位置。

transformLoc = glGetUniformLocation(shaderProgram,"transform")

You should add glutPostRedisplay() function after the glutSwapBuffers() function to visualize the continuous change.您应该在glutSwapBuffers( ) function 之后添加glutPostRedisplay() function 以可视化连续变化。

Looks like you will want to create your own library from GLM.看起来你会想从 GLM 创建你自己的库。 What you're doing in the code above no longer works.您在上面的代码中所做的不再有效。 As another user stated, this is a good template to build functionality from.正如另一位用户所说,是一个构建功能的好模板。 I'd suggest downloading GLM, taking it apart, and reverse engineering what you need into Python.我建议下载 GLM,将其拆开,然后将您需要的内容逆向工程到 Python 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM