Calculate Normals Geometry Shader

我的未来我决定 提交于 2020-01-03 03:35:10

问题


Im currently writing my own GLSL shaders, and wanted to have smooth shading. The shading worked when i calculated the normals bevore sending them to a VBO, but the problem here is when I implement animations with bone matricies, the normals are not corect.

I am using a geometry shader to calculate the normals, but i cant find out how to smooth them.

Here is my geometry shader:

#version 150

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

in vec2 texCoord0[3];
in vec3 worldPos0[3];

out vec2 texCoord1;
out vec3 normal1;
out vec3 worldPos1;

 void main()
 {

        vec3 n = cross(worldPos0[1].xyz-worldPos0[0].xyz, worldPos0[2].xyz-worldPos0[0].xyz);
        for(int i = 0; i < gl_in.length(); i++)
        {
             gl_Position = gl_in[i].gl_Position;

             texCoord1 = texCoord0;
             normal1 = n;
             worldPos1 = worldPos0;

             EmitVertex();
        }
}

I need the faces next to the face that I calculate the normals for, but i dont know how to get them.


回答1:


The geometry shader in OpenGL only has access to single triangles and not the whole mesh, so the normal must be calculated from a single triangle.

The usual solution to this problem is to calculate the normals once for each vertex and store them in vertex arrays for easy access. This turns out to be faster and simpler, as you don't need to recalculate anything in shaders.



来源:https://stackoverflow.com/questions/19346019/calculate-normals-geometry-shader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!