计算法线几何着色器 [英] Calculate Normals Geometry Shader

查看:153
本文介绍了计算法线几何着色器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写自己的GLSL着色器,并希望具有平滑的着色.当我计算法线(将它们发送到VBO之前)时,阴影起作用了,但是这里的问题是当我用骨骼矩阵实现动画时,法线不是核心.

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.

这是我的几何着色器:

#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.

推荐答案

OpenGL中的几何着色器只能访问单个三角形,而不能访问整个网格,因此法线必须从单个三角形计算.

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.

这篇关于计算法线几何着色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆