Вопрос

I'm working with a VERY simple program that is passing an array of points into the programable pipline to draw a cube. I'm trying to set it up so I can change the geometry every frame (based on some external input) and as such, for lighting, I need to recalculate the face normals after they have been changed. I'm doing it in the geometry shader as an exercise.

My shaders are as follows:

Vertex:

#version 330 core

layout(location = 0) in vec3 vertexPosition_modelspace;

uniform mat4 MVP;

void main(){    
    gl_Position =  MVP * vec4(vertexPosition_modelspace,1);
}

Geometry:

#version 330

precision highp float;

layout (triangles) in;
layout (triangle_strip) out;
layout (max_vertices = 3) out;

out vec3 normal;

void main(void)
{
    for (int i = 0; i < gl_in.length(); i++) {
        gl_Position = gl_in[i].gl_Position;
        EmitVertex();
    }

    EndPrimitive();

    normal = vec3(0,0,1);
}

Fragment:

#version 330

in vec3 normal;
out vec4 color;

void main()
{
    color = vec4(normal,1);
}

I was trying to calculate the normals (but I have pulled out that code for debugging reasons) and the normal value was freaking out when I ran the program. So I set the fragment color to be whatever the normal value is. (To see if it was indeed the normal.) So for whatever reason, the value of 'normal' is flipping between red,green, white, and black for no reason that I can tell. If I simply specify a color vector in the fragment shader then the cube renders fine with that color everywhere.

Why is the value of normal changing at all?

Thoughts?

Это было полезно?

Решение

The value of normal is undefined.

You have to write to all of the geometry shader outputs before each call to EmitVertex. GS's don't work like immediate mode rendering.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top