c++ - 使用 OpenGL 4.5 更改所选顶点的颜色
问题描述
我正在使用 OpenGL 在 C++ 中编写一个程序,以根据用户输入插入和编辑三角形。在屏幕上显示一个三角形后,用户可以单击任何顶点并通过按数字键来决定为其分配什么颜色。每个编号的键代表一种颜色,1 可以是红色,2 可以是蓝色,以此类推到 9。
现在我有代码来识别他们点击了 VBO 中的哪个顶点,但我不明白一旦我识别了顶点该怎么做。我知道我可以使用我现在正在使用的 glUniform3f,但这会使场景中的每个三角形都具有相同的颜色。
顶点着色器和片段着色器:
Program program;
const GLchar* vertex_shader =
"#version 150 core\n"
"in vec2 position;"
"void main()"
"{"
" gl_Position = vec4(position, 0.0, 1.0);"
"}";
const GLchar* fragment_shader =
"#version 150 core\n"
"out vec4 outColor;"
"uniform vec3 triangleColor;"
"void main()"
"{"
" outColor = vec4(triangleColor, 1.0);"
"}";
我需要为颜色创建第二个 VBO 吗?如果是这样,我如何设置代码以使用它们?我对 OpenGL 很陌生,所以任何事情都有帮助。
解决方案
我是否需要为颜色创建第二个 VBO?如果是这样,我如何设置代码以同时使用它们?我对 OpenGL 很陌生,所以任何事情都有帮助。
是的你可以。但也可以在一个 VBO 中编码顶点坐标和颜色。
无论如何,您必须创建一个顶点着色器,它除了顶点坐标之外还有第二个属性。该属性是与顶点坐标关联的颜色。并且必须传递给片段着色器(在以下原始字符串文字中使用):
顶点着色器
const GLchar* vertex_shader = R"(
#version 150 core
in vec2 position;
in vec3 color;
out vec3 vColor;
void main()
{
vColor = color;
gl_Position = vec4(position, 0.0, 1.0);
}
)";
片段着色器
const GLchar* fragment_shader = R"(
#version 150 core
in vec3 vColor;
out vec4 outColor;
void main()
{
outColor = vec4(vColor.rgb, 1.0);
}
)";
C++
GLuint program = ...;
glLinkProgram( program );
GLint position_inx = glGetAttribLocation(program, "position");
GLint color_inx = glGetAttribLocation(program, "color");
如果要使用 2 个顶点缓冲区对象,其中第一个包含 x 和 y 坐标的顶点位置,第二个包含具有红色、绿色和蓝色通道的颜色,那么通用顶点属性数组的定义可能看起来像这样:
GLuint VBOpos = ....; // x0, y0, x1, y1, ...
GLuint VBOcol = ....; // r0, g0, b0, r1, g1, b1, ...
glBindBuffer( GL_ARRAY_BUFFER, VBOpos );
glEnableVertexAttribArray( position_inx );
glVertexAttribPointer(
position_inx,
2, GL_FLOAT, GL_FALSE, // 2 floats per coordinate
0, nullptr ); // tightly packed
glBindBuffer( GL_ARRAY_BUFFER, VBOcol );
glEnableVertexAttribArray( color_inx );
glVertexAttribPointer(
color_inx,
3, GL_FLOAT, GL_FALSE, // 3 floats per color
0, nullptr ); // tightly packed
如果要使用 1 Vertex Buffer Objects,其中包含 x 和 y 坐标,后跟红色、绿色和蓝色通道,那么通用顶点属性数组的定义可能如下所示:
GLuint VBO = ....; // x0, y0, r0, g0, b0, x1, y1, r1, g1, b1, ...
glBindBuffer( GL_ARRAY_BUFFER, VBO );
glEnableVertexAttribArray( position_inx );
glVertexAttribPointer(
position_inx,
2, GL_FLOAT, GL_FALSE, // 2 floats per coordinate
5 * sizeof(float), // stride of 5 floats from one tuple to the next one
(void*)0 ); // offset of the vertex coordinates is 0
glEnableVertexAttribArray( color_inx );
glVertexAttribPointer(
color_inx,
3, GL_FLOAT, GL_FALSE, // 3 floats per color
5 * sizeof(float), // stride of 5 floats from one tuple to the next one
(void*)(2 * sizeof(float)); // offset of the color attribute is 2 * sizeof(float)
推荐阅读
- c# - “'Gdip' 的类型初始化程序引发了异常。” C#
- python-3.x - 谁能帮我弄清楚为什么我会收到这个错误?
- python - 如何基于 csv 文件中的另一列向现有 csv 文件添加新列
- android - Android:如何仅在 Kotlin 中首次显示屏幕?
- opendaylight - 如何在 OpenDaylight Sodium 控制器中启动 dlux
- regex - 正则表达式:获取两个字符之间的文本
- java - 尝试检查括号是平衡的还是不平衡的
- vue.js - 在 vuejs 中处理动态输入
- centos - 找不到匹配项:centos-release-openstack-stein
- javascript - 使用连接函数向 Hoc 提供状态时反应给出错误