【opengl 与 python】ogldev教程python实现 lesson 8.0 放缩

这次效果与tutorial5一样,你们随意感受下吧

 

#翻译第八课 Scale transformation
from OpenGL.GL import * 
from OpenGL.GL.shaders import compileShader,compileProgram
import numpy as np
import pyglet
import ctypes

window = pyglet.window.Window(width = 800, height = 600, caption = "tutorial 02")

VERT = """
    #version 330 /* opengl的版本 */
    uniform mat4 pvm;
    in vec3 position;

    void main(){
        gl_Position = pvm * vec4(position,1.0) ;
    } 
"""
FRAG = """
    #version 330 

    out vec4 outputF; 
    void main()
    {
        outputF = vec4(1.0, 0.0, 0.0, 1.0);
    } 

"""
shader = compileProgram(compileShader(VERT, GL_VERTEX_SHADER),
                      compileShader(FRAG,GL_FRAGMENT_SHADER))
glUseProgram(shader)
_position = glGetAttribLocation(shader, b"position")
_pvm = glGetUniformLocation(shader, b"pvm")

VERTEX = np.array([0.0, 1.0, 0.0,
                   -1.0, -1.0, 0.0,
                   1.0, -1.0, 0.0], np.float32)
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, VERTEX, GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0)

glClearColor(0.0, 0.0, 0.0, 1.0)

Scale = 0
@window.event
def on_draw():
    global Scale
    glClear(GL_COLOR_BUFFER_BIT)

    Scale += 0.01
    #放缩变换,与之前5.0翻译所实现的效果一样(都是做同样处理),只不过这次我们用矩阵
    pvm = np.matrix([[np.sin(Scale), 0.0, 0.0, 0.0],
                     [0.0, np.sin(Scale), 0.0, 0.0],
                     [0.0, 0.0, np.sin(Scale), 0.0],
                     [0.0, 0.0, 0.0, 1.0]], np.float32)
    glUniformMatrix4fv(_pvm, 1, GL_TRUE, pvm)
    glEnableVertexAttribArray(_position)

    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glVertexAttribPointer(_position, 3, GL_FLOAT, False, 0, ctypes.c_void_p(0))
    
    glDrawArrays(GL_TRIANGLES, 0, 3)

    glDisableVertexAttribArray(_position)

@window.event
def update(dt):
    pass
pyglet.clock.schedule_interval(update, 1/60)

pyglet.app.run()

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。