Skip to main content
Bounty Awarded with 50 reputation awarded by CommunityBot
fixed vec4+vec3 error, flipped the face vertex order of -Z face for proper culling.
Source Link
Stephane Hockenhull
  • 12.1k
  • 1
  • 27
  • 44

Your geometry shader should take a single point (center of the cube) with a front vector and an up vector.

You can then output 12 triangles (6 faces x 2 tri for a quad) using the single point as the center of the cube and the cross-product of front & up for the right vector.

// table of all triangle vertices to make the cube
const vec3 face_table[12*3] = {
  vec3(1, 1, 1), vec3(-1, 1, 1), vec3(-1, -1, 1),  // first face Z=1
  vec3(1, 1, 1), vec3(-1, -1, 1), vec3(1, -1, 1), 
  vec3(-1, 1, -1), vec3(-1, 1, -1), vec3(-1, -1, -1),  // second face Z=-1 (note the inverted X for culling)
  vec3(-1, 1, -1), vec3(-1, -1, -1), vec3(-1, -1, -1), 
  ... // and so on
};

Then generate the faces in a 12 triangle loop:

vec3 right_vector = cross(front_vector, up_vector); // or you can preset it as uniform

for(int i=0; i < 12*3; ++i){
  gl_Position = MVP*MVP*vec4((gl_in[i].gl_Position.xyz + face_table[i].x * right_vector + face_table[i].y * up_vector + face_table[i].z * front_vector), 1);
  EmitVertex();
}

Note that you can also use your ROT matrix uniform in the geometry shader instead: right_vector = ROT[0], up_vector = ROT[1], front_vector = ROT[2].

And that MVP is now in the geometry shader. The vertex shader should be a simple pass-through gl_Position=vec4(vertPos, 1).

EDIT: fixed vec4+vec3 error, flipped the face vertex order of -Z face for proper culling.

Your geometry shader should take a single point (center of the cube) with a front vector and an up vector.

You can then output 12 triangles (6 faces x 2 tri for a quad) using the single point as the center of the cube and the cross-product of front & up for the right vector.

// table of all triangle vertices to make the cube
const vec3 face_table[12*3] = {
  vec3(1, 1, 1), vec3(-1, 1, 1), vec3(-1, -1, 1),  // first face Z=1
  vec3(1, 1, 1), vec3(-1, -1, 1), vec3(1, -1, 1), 
  vec3(1, 1, -1), vec3(-1, 1, -1), vec3(-1, -1, -1),  // second face Z=-1
  vec3(1, 1, -1), vec3(-1, -1, -1), vec3(1, -1, -1), 
  ... // and so on
};

Then generate the faces in a 12 triangle loop:

vec3 right_vector = cross(front_vector, up_vector); // or you can preset it as uniform

for(int i=0; i < 12*3; ++i){
  gl_Position = MVP*(gl_in[i].gl_Position + face_table[i].x * right_vector + face_table[i].y * up_vector + face_table[i].z * front_vector);
  EmitVertex();
}

Note that you can also use your ROT matrix uniform in the geometry shader instead: right_vector = ROT[0], up_vector = ROT[1], front_vector = ROT[2].

And that MVP is now in the geometry shader. The vertex shader should be a simple pass-through gl_Position=vec4(vertPos, 1).

Your geometry shader should take a single point (center of the cube) with a front vector and an up vector.

You can then output 12 triangles (6 faces x 2 tri for a quad) using the single point as the center of the cube and the cross-product of front & up for the right vector.

// table of all triangle vertices to make the cube
const vec3 face_table[12*3] = {
  vec3(1, 1, 1), vec3(-1, 1, 1), vec3(-1, -1, 1),  // first face Z=1
  vec3(1, 1, 1), vec3(-1, -1, 1), vec3(1, -1, 1), 
  vec3(-1, 1, -1), vec3(1, 1, -1), vec3(1, -1, -1),  // second face Z=-1 (note the inverted X for culling)
  vec3(-1, 1, -1), vec3(1, -1, -1), vec3(-1, -1, -1), 
  ... // and so on
};

Then generate the faces in a 12 triangle loop:

vec3 right_vector = cross(front_vector, up_vector); // or you can preset it as uniform

for(int i=0; i < 12*3; ++i){
  gl_Position = MVP*vec4((gl_in[i].gl_Position.xyz + face_table[i].x * right_vector + face_table[i].y * up_vector + face_table[i].z * front_vector), 1);
  EmitVertex();
}

Note that you can also use your ROT matrix uniform in the geometry shader instead: right_vector = ROT[0], up_vector = ROT[1], front_vector = ROT[2].

And that MVP is now in the geometry shader. The vertex shader should be a simple pass-through gl_Position=vec4(vertPos, 1).

EDIT: fixed vec4+vec3 error, flipped the face vertex order of -Z face for proper culling.

Source Link
Stephane Hockenhull
  • 12.1k
  • 1
  • 27
  • 44

Your geometry shader should take a single point (center of the cube) with a front vector and an up vector.

You can then output 12 triangles (6 faces x 2 tri for a quad) using the single point as the center of the cube and the cross-product of front & up for the right vector.

// table of all triangle vertices to make the cube
const vec3 face_table[12*3] = {
  vec3(1, 1, 1), vec3(-1, 1, 1), vec3(-1, -1, 1),  // first face Z=1
  vec3(1, 1, 1), vec3(-1, -1, 1), vec3(1, -1, 1), 
  vec3(1, 1, -1), vec3(-1, 1, -1), vec3(-1, -1, -1),  // second face Z=-1
  vec3(1, 1, -1), vec3(-1, -1, -1), vec3(1, -1, -1), 
  ... // and so on
};

Then generate the faces in a 12 triangle loop:

vec3 right_vector = cross(front_vector, up_vector); // or you can preset it as uniform

for(int i=0; i < 12*3; ++i){
  gl_Position = MVP*(gl_in[i].gl_Position + face_table[i].x * right_vector + face_table[i].y * up_vector + face_table[i].z * front_vector);
  EmitVertex();
}

Note that you can also use your ROT matrix uniform in the geometry shader instead: right_vector = ROT[0], up_vector = ROT[1], front_vector = ROT[2].

And that MVP is now in the geometry shader. The vertex shader should be a simple pass-through gl_Position=vec4(vertPos, 1).