【发布时间】:2020-05-10 22:09:44
【问题描述】:
我正在尝试使用单个 VBO 和着色器渲染 3 个不同颜色的点。我将位置和颜色值传递给单个 VBO:
vertices = np.array([[0, 0.1, 0.6, 1, 1, 0], [0.3, 0.1, 0, 0, 1, 1], [0.8, 0.5, 0, 0, 1, 0]], dtype='f')
然后我试图读入我的着色器。我在片段着色器中添加了 alpha。我正在使用glVertexAttribPointer 来区分我的 x、y、z 和我的 r、g、b、a。我可以渲染我的 x,y,z:
glEnableVertexAttribArray(glGetAttribLocation(shader_program, "position"))
glVertexAttribPointer(glGetAttribLocation(shader_program, "position"), 3, GL_FLOAT, False, 6*4, None)
我无法使用 glVertexAttribPointer 加载我的 r,g,b,a,因为我从文档中了解到:
glEnableVertexAttribArray(glGetAttribLocation(shader_program, "color"))
glVertexAttribPointer(glGetAttribLocation(shader_program, "color"), 3, GL_FLOAT, False, 6*4, 3*4)
这不会渲染任何可见点。我的着色器肯定在工作,就像我将指针的字节偏移更改为无时一样:
glEnableVertexAttribArray(glGetAttribLocation(shader_program, "color")) glVertexAttribPointer(glGetAttribLocation(shader_program, "color"), 3, GL_FLOAT, False, 6*4, None)
我的点根据我的标准化 x,y,z 坐标着色(即glVertexAttribPointer 将我的 x 坐标解释为 r,y 坐标解释为 g,z 坐标解释为 b),这是有道理的,因为字节偏移量为 0。当然字节偏移量应该是3 * 4?我的两个想法是我要么
- 误解了
glVertexAttribPointer的最后一个参数或 - 不知何故使顶点透明
但我尝试更改颜色的顺序、将 alpha 添加到原始数据集等,但均未成功。任何帮助表示赞赏。
完整代码:
from OpenGL.arrays import vbo
import pygame
import numpy as np
from OpenGL.GL import *
from OpenGL.GL.shaders import *
def getFileContent(file):
content = open(file, 'r').read()
return content
def run():
pygame.init()
screen = pygame.display.set_mode((800,600), pygame.OPENGL | pygame.DOUBLEBUF)
vertices = np.array([[0, 0.1, 0.6, 1, 1, 0], [0.3, 0.1, 0, 0, 1, 1], [0.8, 0.5, 0, 0, 1, 0]], dtype='f')
vertexPositions = vbo.VBO(vertices)
indices = np.array([[0,1,2]], dtype=np.int32)
indexPositions = vbo.VBO(indices, target=GL_ELEMENT_ARRAY_BUFFER)
shader_program = glCreateProgram()
vertex_source = compileShader(getFileContent("testing_vert_shader.vert"), GL_VERTEX_SHADER)
fragment_source = compileShader(getFileContent("testing_frag_shader.frag"), GL_FRAGMENT_SHADER)
glAttachShader(shader_program, vertex_source)
glAttachShader(shader_program, fragment_source)
glBindAttribLocation(shader_program, 0, "position")
glBindAttribLocation(shader_program, 1, "color")
glLinkProgram(shader_program)
print(glGetProgramiv(shader_program, GL_LINK_STATUS)) # shader linked == 1
while True:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glUseProgram(shader_program)
indexPositions.bind()
vertexPositions.bind()
glEnableVertexAttribArray(glGetAttribLocation(shader_program, "position"))
glVertexAttribPointer(glGetAttribLocation(shader_program, "position"), 3, GL_FLOAT, False, 6*4, None)
# red vertices rendered if these two lines are commented out
glEnableVertexAttribArray(glGetAttribLocation(shader_program, "color"))
glVertexAttribPointer(glGetAttribLocation(shader_program, "color"), 3, GL_FLOAT, False, 6*4, None) # changing last argument of glVertexAttribPointer to None results in x,y,z being read as colours
glDrawElements(GL_POINTS, 3, GL_UNSIGNED_INT, None)
# Show the screen
pygame.display.flip()
run()
顶点着色器:
#version 330 core
in vec3 position;
in vec3 color;
out vec3 vColor;
void main()
{
gl_Position = vec4(position, 1.0);
vColor = vec3(color);
}
片段着色器:
#version 330 core
in vec3 vColor;
out vec4 outcolor;
void main()
{
outcolor = vec4(vColor, 1.0f);
}
【问题讨论】:
标签: python opengl glsl shader pyopengl