3
votes

I'm quite a rookie in GLSL and I've been struggling to try to find a way to color all the vertices of a cube in different color. Each face has 4 vertices and a cube has 6 faces, so 6 * 4 = 24. But I can only draw 1 color per corner.

Vertex Shader:

#version 330

uniform mat4 u_m_matrix;
uniform mat4 u_vp_matrix;

layout (location=0) in vec3 a_position;
layout (location=1) in vec3 a_normal;

out vec3 normal;

void main()
{
    normal = a_position;
    gl_Position = u_vp_matrix * u_m_matrix * vec4(a_position, 1.0);
}

Fragment Shader:

#version 330

in vec3 normal;

out vec4 fragColor;

void main() {

    fragColor = vec4(normal, 1.0);
 }

Result:

1
You have to be more specific. What do you want to achieve? "How draw all vertices of a cube in a different color?" - The vertices are at the corners of the cube. All the corners have different colors, haven't they? - Rabbid76
I need to paint all verctices in different color. Each face have 4 vertives so 4 * 6 = 24 colors. - luckmen
each face have 4 vertices (Correct!), and a cube have 6 faces (Correct again!), so 6 * 4 = 24. (Now let me stop you, a cube has 8 vertices, each face shares 2 vertices with another face) - jalsh
@jalsh each face shares every vertex with another face in any closed model. - 3Dave
Not to be blunt, but where are you getting this 24 number from? Cubes have eight distinct vertices. Your sample image already draws each one in distinct color. - 3Dave

1 Answers

3
votes

If you want to color each face in a different color and you want to find a in-shader solution, the a possibility would be to color the fragments of the cube, dependent on the component of the vertex coordinate with the maximum amount.

Pass the vertex coordinate to the fragment shader:

#version 330

uniform mat4 u_m_matrix;
uniform mat4 u_vp_matrix;

layout (location=0) in vec3 a_position;
//layout (location=1) in vec3 a_normal;

out vec3 vertPos;

void main()
{
    vertPos     = a_position;
    gl_Position = u_vp_matrix * u_m_matrix * vec4(a_position, 1.0);
}

Find the component of the vertex coordinate withe maximum absolut value and chose a color:

#version 330

in vec3 vertPos;

out vec4 fragColor;

void main() {

    vec3 posAbs  = abs(vertPos);
    vec3 color   = step(posAbs.yzx, posAbs) * step(posAbs.zxy, posAbs); 
    color       += (1.0 - step(color.zxy * vertPos.zxy, vec3(0.0)));

    fragColor = vec4(color, 1.0);
}


If the normal vectors are face normals, then there is even a simpler solution using the normal vectors:

Pass the normal vector to the fragment shader:

#version 330

uniform mat4 u_m_matrix;
uniform mat4 u_vp_matrix;

layout (location=0) in vec3 a_position;
layout (location=1) in vec3 a_normal;

out vec3 normal;

void main()
{
    normal      = a_normal;
    gl_Position = u_vp_matrix * u_m_matrix * vec4(a_position, 1.0);
}

Compute the color dependent on the normal vector:

#version 330

in vec3 normal;

out vec4 fragColor;

void main() {

    vec3 color = abs(normal.xyz) + max(normal.zxy, 0.0);
    fragColor  = vec4(color, 1.0);
}

[...] so I need 24 colors. [...]

In that case I suggest the following solution.

#version 330

in vec3 vertPos;

out vec4 fragColor;

void main() {

    vec3 posAbs = abs(vertPos);
    vec3 color  = (step(posAbs.yzx, posAbs) * step(posAbs.zxy, posAbs) +
                   step(0.0, vertPos.yzx)) * 0.5;
    fragColor   = vec4(color, 1.0);
}