一、目的
1、使用GLSL实现图形的平移。
二、程序运行结果
三、平移变换
平移变换的任务是将一个对象沿着一个任意长度和方向的向量移动。
对于三维空间上的一点(x,y,z),我们使用4*4齐次矩阵的形式来表示平移变换:
使用四维向量来表示三维向量的做法称作齐次坐标,这对3D图形学来说普遍而又实用。向量的第四个分量称之为 “w” 。事实上,我们在以前教程中见到的着色器中的内置变量 gl_Position 就是一个四维向量,w 分量会在将3D场景投影到 2D 平面时起重要作用!通用的做法是: w = 1 时表示点,w = 0 时表示向量。原因是点可以被平移但是向量不行,你可以改变向量的长度和方向,但是无论向量的起点在哪里,所有具有相同长度和方向的向量是被视为相同的!所以你可以简单地用原点作为所有向量的起点!当我们将 w 设置为0时,乘以变换矩阵后的向量还会是相同的向量。
四、代码解析
1、在GLSL程序中,建立一个4*4齐次矩阵dot,将该矩阵与每个点相乘可得到x,y坐标都平移scare的新点坐标。
2、向量vec4(scale,scale,0.0,1.0)表示的是第4列,矩阵运算后,点(x,y)移到(x+scale,y+scale)处
五、源代码
"""
glfw_square02.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 scale;
void main(){
mat4 rot=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(scale,scale,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)
scale = i*0.1
glUniform1f(glGetUniformLocation(self.program, "scale"), scale)
# 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(640, 480, "Square translation transformation", 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)
# 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)
for i in range(-4,4):
firstSquare0.render()
# Swap front and back buffers
glfw.swap_buffers(window)
# Poll for and process events
glfw.poll_events()
glfw.terminate()