gpt4 book ai didi

c++ - 使用 OpenGL 4.5 更改所选顶点的颜色

转载 作者:行者123 更新时间:2023-11-28 04:31:28 25 4
gpt4 key购买 nike

我正在用 C++ 编写一个程序,使用 OpenGL 根据用户输入插入和编辑三角形。在屏幕上显示一个三角形后,用户可以单击任何顶点并通过按数字键来决定为其分配什么颜色。每个带编号的键代表一种颜色,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 的新手,所以任何事情都有帮助。

最佳答案

Do I need to create a second VBO for just colors? If so, how do I set up the code to use both of them? I am very new to OpenGL so anything helps.

是的,你可以。但也可以在一个 VBO 中对顶点坐标和颜色进行编码。

无论如何,您必须创建一个顶点着色器,它在顶点坐标之外还有一个第二个属性。该属性是与顶点坐标关联的颜色。并且必须传递给片段着色器(在下面使用Raw string literal):

顶点着色器

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 Vertex Buffer Objects其中第一个包含带有 x 和 y 坐标的顶点位置,第二个包含具有红色、绿色和蓝色 channel 的颜色,那么通用顶点属性数组的定义可能如下所示:

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 坐标,后跟红色、绿色和蓝色 channel ,那么通用顶点属性数组的定义可能如下所示:

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++ - 使用 OpenGL 4.5 更改所选顶点的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52784603/

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