There seems to be an error in how you have ordered the vertices for rendering the cube. You also only emit vertices for one single strip when you need to actually create multiple strips or a list for all the sides of the cube. A cube consists of 6 faces, and each face can be created with a triangle strip, but you'll typically need to end one strip before you start another to draw a closed 3D shape like a cube.
Let me provide you a corrected version of the geometry shader to make the cube visible from all sides, assuming that center is the center point of the cube:
#version 450 core
layout (points) in;
layout (triangle_strip, max_vertices = 24) out;
layout(location = 20) uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix; // model-view matrix should also be passed to transform vertices correctly
out vec4 gColor;
in vec4 vertexColor[];
void emitVertex(vec4 position, vec4 color) {
gColor = color;
gl_Position = projectionMatrix * modelViewMatrix * position;
EmitVertex();
}
void main() {
vec4 center = gl_in[0].gl_Position;
float halfSize = 0.45f;
vec4 offsets[8] = vec4[8](
vec4(-halfSize, -halfSize, halfSize, 0.0),
vec4( halfSize, -halfSize, halfSize, 0.0),
vec4( halfSize, halfSize, halfSize, 0.0),
vec4(-halfSize, halfSize, halfSize, 0.0),
vec4(-halfSize, -halfSize, -halfSize, 0.0),
vec4( halfSize, -halfSize, -halfSize, 0.0),
vec4( halfSize, halfSize, -halfSize, 0.0),
vec4(-halfSize, halfSize, -halfSize, 0.0)
);
vec4 color = vertexColor[0];
int indices[6][4] = int[6][4](
{0, 1, 3, 2}, // front face
{4, 5, 0, 1}, // bottom face
{7, 6, 4, 5}, // back face
{3, 2, 7, 6}, // top face
{1, 5, 2, 6}, // right face
{4, 0, 7, 3} // left face
);
for (int face = 0; face < 6; ++face) {
for (int j = 0; j < 4; ++j) {
emitVertex(center + offsets[indices[face][j]], color);
}
EndPrimitive();
}
}
The provided shader above defines a function emitVertex to avoid redundancy and handle applying transformations and emitting vertices to streamline the code. We have also included an array offsets to define the relative positions of the vertices of a cube centered at the origin, and an array indices to index the offsets of the cube's vertices in a consistent order to form each of the faces correctly. Lastly, we transformed the position of vertices using both the model-view and the projection matrices, which is a standard practice when rendering 3D objects.
Note: You must include the model-view matrix uniform in the shader and pass an appropriate value from your rendering engine, which concatenates model, view (and possibly normal) transformations. It's necessary for the correct placement of the object in the world and the camera's view. The code snippet assumes the declaration and calculation of the model-view matrix is handled elsewhere in your code.
Also, the max_vertices has been changed to 24 because the cube has 6 faces and each face will be a quad composed of 4 vertices when you end each primitive. This change will ensure that you can emit all required vertices for a complete cube.