Search code examples
pythonopenglpyopenglcoordinate-transformation

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. 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. 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()


Solution

  • In VERTEX_SHADER you didn't mentioned transform variable. So your triangle position remain fixed after you run the program. Change your VERTEX_SHADER as following.

    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.