一、目的
1、使用GLSL实现图形的旋转。
二、程序运行结果
三、旋转变换
旋转变换,就是说给定一个角度和点,我们将点绕着一个坐标轴旋转。在旋转过程中发生变化的总是x,y,z三个坐标里面的其中两个,而不让第三个坐标值变化。这意味着,旋转路径总在三个坐标轴平面中的一个之中:绕 Z 轴的是 xy 面、绕 X 轴的是 yz 面、绕 Y 轴的是 xy 面。当然还有许多复杂的旋转变换可以让你绕任意一个向量旋转。
对于三维空间上的一点(x,y,z),绕Z 轴旋转a弧度,我们使用4*4齐次矩阵的形式来表示:
四、代码解析
1、theta = i*PI/180.0 角度转换为弧度;程序中旋转了45度。
2、向量vec4(cos(theta),sin(theta),0.0,0)表示的是第1列,矩阵运算后,点(x,y)绕Z轴旋转theta弧度到(xcos(theta)-ysin(theta),xsin(theta)+ycos(theta))处。
五、源代码
"""
glfw_square03.py
Author: dalong10
Description: Draw a Square, learning OPENGL
"""
import glutils #Common OpenGL utilities,see glutils.py
import sys, random, math
import OpenGL
from OpenGL.GL import *
from OpenGL.GL.shaders import *
import numpy
import numpy as np
import glfw
strVS = """
#version 330 core
layout(location = 0) in vec3 position;
uniform float theta;
void main(){
mat4 rot=mat4(vec4(cos(theta),sin(theta),0.0,0),
vec4(-sin(theta), cos(theta),0.0,0),
vec4(0.0, 0.0,1.0, 0.0),
vec4(0.0, 0.0,0.0, 1.0));
gl_Position=rot * vec4(position.x, position.y, position.z, 1.0);
}
"""
strFS = """
#version 330 core
out vec3 color;
void main(){
color = vec3(1,1,0);
}
"""
class FirstSquare:
def __init__(self, side):
self.side = side
# load shaders
self.program = glutils.loadShaders(strVS, strFS)
glUseProgram(self.program)
s = side/2.0
vertices = [
-s, s, 0,
s, s, 0,
s, -s, 0 ,
-s, -s, 0
]
# set up vertex array object (VAO)
self.vao = glGenVertexArrays(1)
glBindVertexArray(self.vao)
# set up VBOs
vertexData = numpy.array(vertices, numpy.float32)
self.vertexBuffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)
glBufferData(GL_ARRAY_BUFFER, 4*len(vertexData), vertexData,
GL_STATIC_DRAW)
#enable arrays
self.vertIndex = 0
glEnableVertexAttribArray(self.vertIndex)
# set buffers
glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
# unbind VAO
glBindVertexArray(0)
def render(self):
# use shader
glUseProgram(self.program)
theta = i*PI/180.0
glUniform1f(glGetUniformLocation(self.program, "theta"), theta)
# bind VAO
glBindVertexArray(self.vao)
# draw
glDrawArrays(GL_LINE_LOOP, 0, 4)
# unbind VAO
glBindVertexArray(0)
if __name__ == '__main__':
import sys
import glfw
import OpenGL.GL as gl
def on_key(window, key, scancode, action, mods):
if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
glfw.set_window_should_close(window,1)
# Initialize the library
if not glfw.init():
sys.exit()
# Create a windowed mode window and its OpenGL context
window = glfw.create_window(300, 300, "Rotation Square ", None, None)
if not window:
glfw.terminate()
sys.exit()
# Make the window's context current
glfw.make_context_current(window)
# Install a key handler
glfw.set_key_callback(window, on_key)
PI = 3.14159265358979323846264
# Loop until the user closes the window
while not glfw.window_should_close(window):
# Render here
width, height = glfw.get_framebuffer_size(window)
ratio = width / float(height)
gl.glViewport(0, 0, width, height)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
gl.glClearColor(0.0,0.0,4.0,0.0)
firstSquare0 = FirstSquare(1.0)
i=45.0
firstSquare0.render()
# Swap front and back buffers
glfw.swap_buffers(window)
# Poll for and process events
glfw.poll_events()
glfw.terminate()