Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make single color face for a cube in GLSL opegl

I have a rotating cube which continuously rotates

enter image description here

But it always rendered in such a way with interpolated gradient color

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to color single face with only one color exactly one triangle piece as below

enter image description here

My GLSL

#version 410 core
attribute vec3 position;
attribute vec3 color;
varying vec3 outColor;
uniform float time;
uniform mat4 matrix;
void main()
{
    float theta = time;
    
    float co = cos(theta);
    float si = sin(theta);

    mat4 rotationY = mat4(co, 0, si, 0,
                          0, 1, 0, 0,
                         -si, 0, co, 0,
                         0, 0, 0, 1);

    mat4 rotationX = mat4(1, 0, 0, 0,
                          0, co, -si, 0,
                          0, si, co, 0,
                          0, 0, 0, 1);

    outColor = color;
    gl_Position = matrix * rotationY * rotationX * vec4(position,1.f);
}
#version 410 core
varying vec3 outColor;
uniform float time;
void main()
{
    gl_FragColor = vec4(outColor,1);
}

My GLFW code

        glClearColor(1f, 1f, 1f, 1f)
        glClear(GL_COLOR_BUFFER_BIT.toUInt() or GL_DEPTH_BUFFER_BIT.toUInt())
        
        var time = glfwGetTime()
        glUniform1f!!(uniformTime, time.toFloat())
        
        glDrawElements(GL_TRIANGLES, verticesIndex.size, GL_UNSIGNED_SHORT, NULL)
        
        glfwSwapBuffers(window)
        glfwPollEvents()

My color array

[
0.8, 0.007315, 0.026764, 
0.01696, 0.8, 0.005661, 
0.007843, 0.001524, 0.8, 
0.01696, 0.8, 0.005661, 
0.007843, 0.001524, 0.8, 
0.01696, 0.8, 0.005661, 
0.007843, 0.001524, 0.8, 
0.01696, 0.8, 0.005661, 
0.007843, 0.001524, 0.8, 
0.01696, 0.8, 0.005661, 
0.007843, 0.001524, 0.8, 
0.01696, 0.8, 0.005661
]

>Solution :

The usual way is to create a mesh with color attributes where each face has its own vertices with a color attribute of the same color.


Alternatively, you can try using the flat Interpolation qualifier:

The value will not be interpolated. The value given to the fragment shader is the value from the Provoking Vertex for that primitive.

Vertex shader

#version 410 core
in vec3 position;
in vec3 color;

flat out vec3 outColor;

void main()
{
    outColor = color

    // [...]
}

Fragment shader:

#version 410 core

flat in vec3 outColor;
out vec4 fragColor;

void main()
{
    fragColor = vec4(outColor, 1.0);
}

Another option would be to create an array of colors for each triangle primitive and store this color array in a Shader Storage Buffer Object. Use gl_VertexID to address the color in the vertex shader.

vec4 outColor;

layout(std430, binding = 0) buffer primitiveColors
{
    vec4 colors[];
};

void main()
{
    outColor = colors[gl_VertexID / 3];

    // [...]
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading