c++ - 如何根据时间或某些功能动态更改每个顶点的颜色?

标签 c++ opengl

我想绘制高斯函数 (Z=f(x,y,sigma)) 的二维轮廓。 x,y 是顶点的 x,y;z=0。我使用 Z 得到每个顶点的颜色。当 sigma 在 0.01-1.0 之间改变时,Z 和颜色值会改变。如何更新颜色?

  glBufferData(GL_ARRAY_BUFFER, sizeof(GaussianVertexices), GaussianVertexices,    GL_STATIC_DRAW);//GaussianVertexices contains Pos and Color;
//...
while (!glfwWindowShouldClose(window))
{
    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    GaussShader.use();
    sigma += 0.01;
    if (sigma>1.0)
    {
        sigma = 0.1;
    }
    // how can i update color value with sigma changed
    glBindVertexArray(GaussVAO);
    glPointSize(2.0f);
    glDrawArrays(GL_POINTS, 0, POINTS_NUMS);
    glfwSwapBuffers(window);
    glfwPollEvents();
}

顶点着色器:

#version 330 core
layout(location=0) in vec2 apos;
layout(location=1) in vec3 acolor;

out vec3 color;


void main()
{
    gl_Position=vec4(apos.x,apos.y,0.0,1.0f);
    color=acolor;
}

片段着色器

#version 330 core
in vec3 color;
out vec4 fragColor;

void main()
{
    fragColor=vec4(color,0.5);
}

更新: 我试过这个。

 while (!glfwWindowShouldClose(window))
    {//....
        GaussShader.use();
        sigma += 0.01;
        CreateGuassColor(GaussVertexices, GaussColor);
        if (sigma>1.0)
        {
            sigma = 0.01;
        }

        glBindVertexArray(GaussVAO);
        glBindBuffer(GL_ARRAY_BUFFER, ColorVBO);
        void *ptr = nullptr;
          ptr  = glMapBufferRange(GL_ARRAY_BUFFER, 0, sizeof(GaussColor), GL_MAP_WRITE_BIT);

        memcpy(ptr, GaussColor, sizeof(GaussColor));
        glUnmapBuffer(GL_ARRAY_BUFFER);
        glBindVertexArray(0);
        glBindVertexArray(GaussVAO);
        glPointSize(2.0f);
        glDrawArrays(GL_POINTS, 0, POINTS_NUMS);
    }

最佳答案

添加:

uniform vec4 vertexColor;

到你的片段着色器。

确保在第一次创建着色器程序时获得它的 ID。这是在您甚至编译和链接您的顶点和片段着色器之前。你这样做:

GLuint gaussShaderProg = glCreateProgram(); 

然后做:

GLint uniformLocation = glGetUniformLocation(gaussShaderProg, "vertexColor");

然后在你的循环中:

while (!glfwWindowShouldClose(window))
{
    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    sigma += 0.01;
    if (sigma>1.0)
    {
        sigma = 0.1;
    }

    GaussShader.use(); // Make sure the shader program is 
                       // bound before trying to     update the uniform

    float vertColor[] = { 1, 1, 1, 1 }; // Whatever colour you want
    glUniform4fv(uniformLocation​, 1, (GLfloat*)vertColor​); // Now your fragment shader 
                                         // can access the updated colour


    glBindVertexArray(GaussVAO);
    glPointSize(2.0f);
    glDrawArrays(GL_POINTS, 0, POINTS_NUMS);
    glfwSwapBuffers(window);
    glfwPollEvents();
}

然后在你的片段着色器中输出统一的颜色(vec4)。确保颜色的第四个分量为 1,以防你打开了 alpha 混合。

关于c++ - 如何根据时间或某些功能动态更改每个顶点的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48002003/

相关文章:

c++ - 应用程序未能正确初始化 (0xc0150002)

c# - 将代码添加到 SWIG 中自动生成的类

c++ - orthoProject freetype opengl

c - OpenGL矩阵问题

c++ - 将glm四元数转换为旋转矩阵并将其与opengl一起使用

c++ - 使用 SetThreadAffinityMask() 动态选择要运行的线程

c++ - 从 Windows 模块中的资源中提取文件

cocoa - 使用 OpenGL 和 Cocoa 绘制文本的最佳方法?

c++ - 产生错误 "expression must be a modifiable lvalue"

linux - 具有 GPU 支持但不支持窗口的离屏渲染