简体   繁体   English

OpenGL:如何改变每个顶点的颜色?

[英]OpenGL: How to change the color of each vertex?

I am trying to change the color of the vertices of each triangle that I have created using the following code:我正在尝试更改使用以下代码创建的每个三角形的顶点颜色:

This is the Point structure;这是点结构; this structure stores the position data and color data of each vertex:这个结构存储position数据和每个顶点的颜色数据:

struct Point{
    float x;
    float y;
    float z;
    float r;
    float g;
    float b;
};

Here is created the main triangle of the Sierpinski's triangle;这里创建了谢尔宾斯基三角形的主三角形; I created a vector that contains all the vertex data, and then the data is loaded into the buffer:我创建了一个包含所有顶点数据的向量,然后将数据加载到缓冲区中:

Point A, B, C;
A = {0.0f, 0.9f, 0.0f, 1.0f, 0.0f, 0.0f};
B = {0.9f, -0.9f, 0.0f, 0.0f, 1.0f, 0.0f};
C = {-0.9f, -0.9f, 0.0f, 0.0f, 0.0f, 1.0f};
std::vector<Point> vertices;   
int depth = 6;    
draw_triangles(A, B, C, depth, vertices);
      
Point vertices2[vertices.size()];
for(int i = 0;i<vertices.size();i++){
    vertices2[i] = vertices[i];
}
    
unsigned int VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);

glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices2), vertices2, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3* sizeof(float)));
glEnableVertexAttribArray(1);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

This function generates all the vertex data, and stores the position data of each vertex in the vertices vector:这个 function 生成所有顶点数据,并将每个顶点的 position 数据存储在vertices向量中:

void draw_triangles(Point A, Point B, Point C, int depth, std::vector<Point>& vertices){
    if(depth == 0) return;
    
    Point X = {(((B.x + C.x)/2) + B.x)/2, (B.y + A.y)/2, 0.0f, 1.0f, 0.0f, 0.0f};
    Point Y = {(B.x + C.x) / 2, B.y, 0.0f, 0.0f, 1.0f, 0.0f};
    Point Z = Z = {(((B.x + C.x)/2) + C.x)/2, (B.y + A.y)/2, 0.0f, 0.0f, 0.0f, 1.0f};
   
    vertices.push_back(X);
    vertices.push_back(Y);
    vertices.push_back(Z);
    
    depth--; 
    draw_triangles(A, X, Z, depth, vertices);
    draw_triangles(A, X, Z, depth, vertices);
    draw_triangles(X, B, Y, depth, vertices);
    draw_triangles(Z, Y, C, depth, vertices);
}

when the code draws, I get the following output:当代码绘制时,我得到以下 output:

获得的输出

My question is: How can I change the color of each vertex?我的问题是:如何更改每个顶点的颜色?

For example, the red vertex, change it to blue, the green one to red, the blue one to green and so on, generating a kind of animation.例如,红色顶点,将其变为蓝色,将绿色变为红色,将蓝色变为绿色等等,生成一种 animation。

Edit编辑

In the code, the color is set, but what I really want is that the color of each vertex change over the time.在代码中,设置了颜色,但我真正想要的是每个顶点的颜色随时间而变化。

When you want to change the color of your vertices over time, you probably want to modify it frame by frame as in the example at the following link:当您想随着时间的推移更改顶点的颜色时,您可能希望逐帧修改它,如以下链接中的示例所示:

https://godbolt.org/z/Y1brGx https://godbolt.org/z/Y1brGx

I have set up a simple way of making those structures change their values during the rendering loop;我已经建立了一种简单的方法来让这些结构在渲染循环期间改变它们的值; you can get [more] creative with that once you got the idea.一旦你有了这个想法,你就可以获得[更多]创意。 What is important is that you update the data in your structures and you load the new data in the vertex buffer object at each frame, so that the vertex shader and the fragment shader can read the new values.重要的是更新结构中的数据在每一帧将新数据加载到顶点缓冲区 object 中,以便顶点着色器和片段着色器可以读取新值。

Note that you can make the colors change during each frame in different ways;请注意,您可以使 colors 在每一帧期间以不同的方式发生变化; another idea could be using random numbers to set the colors' components.另一个想法可能是使用随机数来设置颜色的组件。

As a side note, you should be able to pass vertices.data() to glBufferData as a pointer to your vertex data, and avoid declaring an array of Point s for it.附带说明一下,您应该能够将vertices.data()作为指向顶点数据的指针传递给glBufferData ,并避免为其声明Point数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM