gpt4 book ai didi

python - PyOpenGL 将变量传递给顶点着色器

转载 作者:行者123 更新时间:2023-12-01 01:24:30 31 4
gpt4 key购买 nike

经过一些建议,我决定用困难的方式学习一点 OpenGL。我尝试将一个 (float - 名为 myAttrib) 变量传递给顶点着色器,它似乎有效(第 33 行),但显然不起作用't。稍后我想将代码分成更多的部分。

代码:

from OpenGL.GL import *
from OpenGL.GL.shaders import *

import pygame
from pygame.locals import *
import numpy, time

def getFileContent(file):
content = open(file, 'r').read()
return content

def initDraw():
pygame.init()
pygame.display.set_mode((640, 480), HWSURFACE | OPENGL | DOUBLEBUF)

vertices = [0.0, 0.5,
0.5, -0.5,
-0.5, -0.5,]

vertices = numpy.array(vertices, dtype = numpy.float32)

myAttrib = 0.3

vertexShader = compileShader(getFileContent("helloTriangle.vert"), GL_VERTEX_SHADER)
fragmentShader = compileShader(getFileContent("helloTriangle.frag"), GL_FRAGMENT_SHADER)

shaderProgram = glCreateProgram()
glAttachShader(shaderProgram, vertexShader)
glAttachShader(shaderProgram, fragmentShader)
glBindAttribLocation(shaderProgram, 1, "myAttrib")
glLinkProgram(shaderProgram)

print(glGetAttribLocation(shaderProgram, "myAttrib"))

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices)
glEnableVertexAttribArray(0)

glClearColor(0, 0, 0, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

glUseProgram(shaderProgram)
glDrawArrays(GL_TRIANGLES, 0, 3)

pygame.display.flip()
time.sleep(2)

initDraw()

顶点着色器:

#version 130

attribute vec2 vPosition;
attribute float myAttrib;

void main()
{
gl_Position = vec4(vPosition.x, vPosition.y + myAttrib, 0.0, 1.0);
}

片段着色器:

#version 130

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

最佳答案

如果您使用属性,则必须定义一组通用顶点属性,每个顶点坐标有一个属性值(在您的情况下为 3)。请参阅Vertex Specification .
如果您要使用uniform,那么您可以为存储在默认uniform block 中的uniform变量中的程序设置一个值(您可以将其想象为一个全局变量)。请参阅Uniform (GLSL) :

情况 1 - 属性 myAttrib:

顶点着色器

#version 130

attribute vec2 vPosition;
attribute float myAttrib;

void main()
{
gl_Position = vec4(vPosition.x, vPosition.y + myAttrib, 0.0, 1.0);
}

您必须像定义顶点坐标一样定义一组通用顶点属性:

attrib = [-0.2, 0.2, 0.0]
myAttrib = numpy.array(attrib, dtype = numpy.float32)

# .....

glLinkProgram(shaderProgram)
myAttrib_location = glGetAttribLocation(shaderProgram, "myAttrib")

# .....

glVertexAttribPointer(myAttrib_location, 1, GL_FLOAT, GL_FALSE, 0, myAttrib)
glEnableVertexAttribArray(myAttrib_location)

glBindAttribLocation(shaderProgram, 1, "myAttrib")
glLinkProgram(shaderProgram)

# .....

glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, myAttrib)
glEnableVertexAttribArray(1)

案例 2 - 制服 myUniform

顶点着色器

#version 130

attribute vec2 vPosition;
uniform float myUniform;

void main()
{
gl_Position = vec4(vPosition.x, vPosition.y + myUniform, 0.0, 1.0);
}

通过glGetUniformLocation获取制服(“myUniform”)的位置并将值设置为统一的 glUniform :

myUniform = 0.3

# .....

glLinkProgram(shaderProgram)
myUniform_location = glGetUniformLocation(shaderProgram, "myUniform")

# .....

glUseProgram(shaderProgram)
glUniform1f(myUniform_location, myUniform)

glDrawArrays(GL_TRIANGLES, 0, 3)

关于python - PyOpenGL 将变量传递给顶点着色器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53487526/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com