Frage

I wrote a code that draws the following cube: cube with culled faces

As seen, every side has the wrong face culled, namely the outer faces.

What do I have to change so that the faces point in the other direction?

The whole cube is generated by the following (geometry) shader:

#version 330 core

layout(points) in;
layout(triangle_strip, max_vertices=24) out;

out vec2 tex_coord;

uniform mat4x4 model;
uniform mat4x4 view;
uniform mat4x4 projection;

uniform float size = 1.0;

const vec2 texc[4] = vec2[](vec2(0, 0),
                            vec2(1, 0),
                            vec2(0, 1),
                            vec2(1, 1));

void main() {
    float asize = size / 2;
    vec4 offset[24] = vec4[] (
                            vec4(-asize,  asize, -asize, 0.0), //t
                            vec4( asize,  asize, -asize, 0.0),
                            vec4(-asize,  asize,  asize, 0.0),
                            vec4( asize,  asize,  asize, 0.0),

                            vec4(-asize, -asize,  asize, 0.0), //d
                            vec4( asize, -asize,  asize, 0.0),
                            vec4(-asize, -asize, -asize, 0.0),
                            vec4( asize, -asize, -asize, 0.0),

                            vec4(-asize,  asize, -asize, 0.0), //l
                            vec4(-asize,  asize,  asize, 0.0),
                            vec4(-asize, -asize, -asize, 0.0),
                            vec4(-asize, -asize,  asize, 0.0),

                            vec4( asize,  asize,  asize, 0.0), //r
                            vec4( asize,  asize, -asize, 0.0),
                            vec4( asize, -asize,  asize, 0.0),
                            vec4( asize, -asize, -asize, 0.0),

                            vec4(-asize,  asize,  asize, 0.0), //f
                            vec4( asize,  asize,  asize, 0.0),
                            vec4(-asize, -asize,  asize, 0.0),
                            vec4( asize, -asize,  asize, 0.0),

                            vec4( asize,  asize, -asize, 0.0), //b
                            vec4(-asize,  asize, -asize, 0.0),
                            vec4( asize, -asize, -asize, 0.0),
                            vec4(-asize, -asize, -asize, 0.0));

    int i, j, k;
    for(i = 0; i < gl_in.length(); ++i) {
        for(k = 0; k < offset.length() / 4; ++k) {
            gl_PrimitiveID = k;
            for(j = 0; j < 4; ++j) {
                mat4x4 base;
                gl_Position = projection * view * model * (gl_in[i].gl_Position + offset[j + k*4]);
                tex_coord = texc[j];
                EmitVertex();
            }
            EndPrimitive();
        }
    }
}

This are the uniform matrices:

glm::mat4x4 model;
glm::mat4x4 view = glm::lookAt(glm::vec3(1.1f, 1.3f, 1.2f),
                            glm::vec3(0.0f, 0.0f, 0.0f),
                            glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4x4 projection = glm::perspective(80.0f, (float)dw/(float)dh, 0.01f, 100.0f);
War es hilfreich?

Lösung

You're just outputting triangles with the wrong winding order, i.e. clockwise instead of counterclockwise. By default a face is considered "front facing" if its vertices have counterclockwise winding; glFrontFace can be used to control this (passing GL_CCW for counterclockwise winding, GL_CW for clockwise winding).

Do yourself a favour and fix the cube's data. :)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top