点没有颜色,即使有着色器
Points have no color, even with a shader
我正在尝试为 OpenGL 应用程序中的每个点设置不同的颜色,为此我构建了 2 个 float
std::vectors
调用,vertex_position_data
和 vertex_color_data
,分别代表x,y,z和r,g,b.
坐标:0.9996775489540122 0.8108964808511072 0.9943463478468979
RGB:0.317364987841259 0.620614750460882 0.54
有了它,我做到了:
unsigned int vertexArrayID;
unsigned int vertex_bufferID;
unsigned int color_bufferID;
glGenVertexArrays(1, &vertexArrayID);
glBindVertexArray(vertexArrayID);
glCreateBuffers(1, &vertex_bufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertex_bufferID);
glBufferData(GL_ARRAY_BUFFER, vertex_position_data.size() * sizeof(vertex_position_data[0]), &vertex_position_data[0], GL_STATIC_DRAW);
glCreateBuffers(1, &color_bufferID);
glBindBuffer(GL_ARRAY_BUFFER, color_bufferID);
glBufferData(GL_ARRAY_BUFFER, vertex_color_data.size() * sizeof(vertex_color_data[0]), &vertex_color_data[0], GL_STATIC_DRAW);
glEnableVertexArrayAttrib(vertex_bufferID, 0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexArrayAttrib(color_bufferID, 1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glBindVertexArray(vertexArrayID);
并在循环内调用:
while (!glfwWindowShouldClose(viewPortWindow))
{
/* Rendering Area*/
glClearColor(0.690f, 0.862f, 0.890f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPointSize(10);
glDrawArrays(GL_POINTS, 0, 27);
glfwSwapBuffers(viewPortWindow);
glfwPollEvents();
}
观察:出于测试目的,我设置了一个具有 10 个点坐标(实际上是 9)的输入,9 * 3 = 27。
顶点着色器
#version 330 core
layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 vertex_color;
out vec3 vs_color;
void main()
{
gl_Position = vec4(vertex_position, 1.0f);
vs_color = vertex_color;
}
片段着色器
#version 330 core
in vec3 vs_color;
out vec4 fs_color;
void main()
{
fs_color = vec4(vs_color, 1.0f);
}
通过运行它,我得到:
为什么我得到那些黑点?
在调用 glVertexAttribPointer
之前,您必须绑定 ARRAY_BUFFER。此外,glEnableVertexArrayAttrib
的第一个参数是顶点数组对象而不是顶点缓冲区对象:
glEnableVertexArrayAttrib(vertexArrayID, 0);
glBindBuffer(GL_ARRAY_BUFFER, vertex_bufferID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexArrayAttrib(vertexArrayID, 1);
glBindBuffer(GL_ARRAY_BUFFER, color_bufferID);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glVertexAttribPointer
associated the buffer the buffer currently bound to the ARRAY_BUFFER target to the specified vertex attribute. The reference to the buffer is stored in the state vector of the currently bound Vertex Array Object. See also Vertex Buffer Object.
我正在尝试为 OpenGL 应用程序中的每个点设置不同的颜色,为此我构建了 2 个 float
std::vectors
调用,vertex_position_data
和 vertex_color_data
,分别代表x,y,z和r,g,b.
坐标:0.9996775489540122 0.8108964808511072 0.9943463478468979
RGB:0.317364987841259 0.620614750460882 0.54
有了它,我做到了:
unsigned int vertexArrayID;
unsigned int vertex_bufferID;
unsigned int color_bufferID;
glGenVertexArrays(1, &vertexArrayID);
glBindVertexArray(vertexArrayID);
glCreateBuffers(1, &vertex_bufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertex_bufferID);
glBufferData(GL_ARRAY_BUFFER, vertex_position_data.size() * sizeof(vertex_position_data[0]), &vertex_position_data[0], GL_STATIC_DRAW);
glCreateBuffers(1, &color_bufferID);
glBindBuffer(GL_ARRAY_BUFFER, color_bufferID);
glBufferData(GL_ARRAY_BUFFER, vertex_color_data.size() * sizeof(vertex_color_data[0]), &vertex_color_data[0], GL_STATIC_DRAW);
glEnableVertexArrayAttrib(vertex_bufferID, 0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexArrayAttrib(color_bufferID, 1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glBindVertexArray(vertexArrayID);
并在循环内调用:
while (!glfwWindowShouldClose(viewPortWindow))
{
/* Rendering Area*/
glClearColor(0.690f, 0.862f, 0.890f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPointSize(10);
glDrawArrays(GL_POINTS, 0, 27);
glfwSwapBuffers(viewPortWindow);
glfwPollEvents();
}
观察:出于测试目的,我设置了一个具有 10 个点坐标(实际上是 9)的输入,9 * 3 = 27。
顶点着色器
#version 330 core
layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 vertex_color;
out vec3 vs_color;
void main()
{
gl_Position = vec4(vertex_position, 1.0f);
vs_color = vertex_color;
}
片段着色器
#version 330 core
in vec3 vs_color;
out vec4 fs_color;
void main()
{
fs_color = vec4(vs_color, 1.0f);
}
通过运行它,我得到:
为什么我得到那些黑点?
在调用 glVertexAttribPointer
之前,您必须绑定 ARRAY_BUFFER。此外,glEnableVertexArrayAttrib
的第一个参数是顶点数组对象而不是顶点缓冲区对象:
glEnableVertexArrayAttrib(vertexArrayID, 0);
glBindBuffer(GL_ARRAY_BUFFER, vertex_bufferID);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glEnableVertexArrayAttrib(vertexArrayID, 1);
glBindBuffer(GL_ARRAY_BUFFER, color_bufferID);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glVertexAttribPointer
associated the buffer the buffer currently bound to the ARRAY_BUFFER target to the specified vertex attribute. The reference to the buffer is stored in the state vector of the currently bound Vertex Array Object. See also Vertex Buffer Object.