一、目的
1、综合使用GLSL实现图形的平移、缩放、旋转。
二、程序运行结果
三、变换综合应用
对点进行先平移,在缩放,最后旋转操作,即将点坐标齐次矩阵与平移矩阵、缩放矩阵、旋转矩阵相乘得到的变换后的坐标。
四、代码解析
程序中render(self,a,b,scale,r): 参数a,b表示x轴平移a,y轴平移b;缩放scale倍,然后绕Z轴旋转r度。
五、源代码
"""
glfw_square04.py
Author: dalong10
Description: Draw 4 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 a;
uniform float b;
uniform float scale;
uniform float theta;
void main(){
mat4 rot1=mat4(vec4(1.0, 0.0,0.0,0),
vec4(0.0, 1.0,0.0,0),
vec4(0.0,0.0,1.0,0.0),
vec4(a,b,0.0,1.0));
mat4 rot2=mat4(vec4(scale, 0.0,0.0,0.0),
vec4(0.0, scale,0.0,0.0),
vec4(0.0,0.0,1.0,0.0),
vec4(0.0,0.0,0.0,1.0));
mat4 rot3=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=rot3 *rot2 *rot1 * 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,a,b,scale,r):
# use shader
glUseProgram(self.program)
glUniform1f(glGetUniformLocation(self.program, "a"), a)
glUniform1f(glGetUniformLocation(self.program, "b"), b)
glUniform1f(glGetUniformLocation(self.program, "scale"), scale)
theta = r*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, "4 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)
firstSquare0.render(0.0,1,0.3,0)
firstSquare0.render(1,0.0,0.4,15)
firstSquare0.render(0.0,-1,0.5,30)
firstSquare0.render(-0.5,0.0,0.6,45)
# Swap front and back buffers
glfw.swap_buffers(window)
# Poll for and process events
glfw.poll_events()
glfw.terminate()