1. Put mouse interactivity into the file (note: must use glfw mouse function, no need to import other module).• Click Left Mouse button - Rotate about the x-axis• Click Middle Mouse button - Rotate about the y-axis• Click Right Mouse button - Rotate about the z-axis
import glfw
import pyrr
from OpenGL.GL import *
from OpenGL.GL.shaders import *
import numpy
def main():
# initialize glfw
if not glfw.init():
return
window = glfw.create_window(800, 600, "My OpenGL window", None, None)
if not window:
glfw.terminate()
return
glfw.make_context_current(window)
glClearColor(0.2,0.3,0.2,1.0)
glEnable(GL_DEPTH_TEST)
cube = [-0.5, -0.5, 0.5, 1.0, 0.0, 0.0,
0.5, -0.5, 0.5, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0,
-0.5, 0.5, 0.5, 1.0, 1.0, 1.0,
-0.5, -0.5, -0.5, 1.0, 0.0, 0.0,
0.5, -0.5, -0.5, 0.0, 1.0, 0.0,
0.5, 0.5, -0.5, 0.0, 0.0, 1.0,
-0.5, 0.5, -0.5, 1.0, 1.0, 1.0]
cube = numpy.array(cube, dtype = numpy.float32)
indices = [0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4,
4, 5, 1, 1, 0, 4,
6, 7, 3, 3, 2, 6,
5, 6, 2, 2, 1, 5,
7, 4, 0, 0, 3, 7]
indices = numpy.array(indices, dtype = numpy.uint32)
vertex_shader = """
#version 330
in vec3 position;
in vec3 colour;
uniform mat4 transform;
out vec3 newColour;
void main()
{
gl_Position = transform * vec4(position,1.0f);
newColour = colour;
}
"""
fragment_shader = """
#version 330
in vec3 newColour;
out vec4 outColour;
void main()
{
outColour = vec4(newColour,1.0f);
}
"""
shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.
compileShader(vertex_shader, GL_VERTEX_SHADER),OpenGL.GL.shaders.
compileShader(fragment_shader, GL_FRAGMENT_SHADER))
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, 192, cube, GL_STATIC_DRAW)
EBO = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 144, indices, GL_STATIC_DRAW)
position = glGetAttribLocation(shader, "position")
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
glEnableVertexAttribArray(position)
colour = glGetAttribLocation(shader, "colour")
glVertexAttribPointer(colour, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
glEnableVertexAttribArray(colour)
glUseProgram(shader)
while not glfw.window_should_close(window):
glfw.poll_events()
#must set numpy.pi
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS)
{
}
elif if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS){
}
elif (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS){
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.get_time())
rot_y = pyrr.Matrix44.from_y_rotation(0.5 * glfw.get_time())
rot_z = pyrr.Matrix44.from_z_rotation(0.5 * glfw.get_time())
transformLoc = glGetUniformLocation(shader, "transform")
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, rot_x * rot_y * rot_z)
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, None)
glfw.swap_buffers(window)
glfw.terminate()
if __name__ == "__main__":
main()
glfwMouseFunc(mouse)
glfwSetMouseButtonCallback(window, mouse_button_callback);