一、目的
1、使用GLSL实现图形的缩放。
二、程序运行结果
三、缩放变换
缩放变换非常简单,它的目的是增大或者缩小物体的尺寸。
二维空间坐标(x,y)以(0, 0)为中心在水平方向上缩放a倍,在垂直方向上缩放b倍,指的是变换后坐标位置距离(0,0)的水平距离变为原坐标离位置中心点的水平距离的a倍,垂直距离变为原坐标离位置中心点的垂直距离的b倍。
根据以上定义,(x,y)以(0,0)为中心缩放后变换后的坐标为(m,n),即(m,n) = (a·x, b·y)。显然变换后的坐标离中心点的水平距离由x|缩放为|ax|,垂直距离由|y|缩放为|by|。若a>1,则表示在水平方向放大,就是离中心点的水平距离增大了。反之,在水平方向上缩小。同样,若b>1,则表示在垂直方向放大,就是离中心点的垂直距离增大了。反之,在垂直方向上缩小。若a = b,即常说的等比例缩放。例如,(-100,100)以(0,0)为中心放大两倍,则坐标变换为(-200,200)。
对于三维空间上的一点(x,y,z),我们使用4*4齐次矩阵的形式来表示缩放变换:
四、代码解析
1、在GLSL程序中,建立一个4*4齐次矩阵dot,将该矩阵与每个点相乘可得到缩放scare倍的新点坐标。
2、使用glUniform1f(glGetUniformLocation(self.program, “scale”), scale)将变化的值赋给GLSL程序中的scale变量。
五、源代码
"""
glfw_square01.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(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));
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 scale", 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(1,10):
firstSquare0.render()
# Swap front and back buffers
glfw.swap_buffers(window)
# Poll for and process events
glfw.poll_events()
glfw.terminate()
六、参考文献:
1、Sun‘刺眼的博客 https://www.cnblogs.com/android-blogs/p/5454692.html
2、大龙10简书 https://www.jianshu.com/p/4382b25ad797