LWJGL 着色器法线的别名

LWJGL shader normal's alias

我正在使用 LWJGL 和一些显卡(AMD Radeon 系列)编写我的应用程序我无法在顶点着色器中更改法线的名称,因为出现问题并且屏幕上只出现一个大三角形。这是我的代码:

#version 150

//our attributes
in vec3 a_position;
in vec2 a_textureCoords;
in vec3 a_normal; //Here is an error

//send the color out to the fragment shader
out vec2 vTextureCoords;

void main(void)
{
    a_normal;
    gl_Position = vec4(a_position, 1.0);
    vTextureCoords = a_textureCoords;
}

如果我将着色器更改为:

#version 150

//our attributes
in vec3 a_position;
in vec2 a_textureCoords;
in vec3 normal;

//send the color out to the fragment shader
out vec2 vTextureCoords;

void main(void)
{
    normal;
    gl_Position = vec4(a_position, 1.0);
    vTextureCoords = a_textureCoords;
}

一切正常,网格显示应有的样子。正常吗? 还有我的绑定属性函数:

//Before
GL20.glBindAttribLocation(s_programID, 2, "a_normal");

//After
GL20.glBindAttribLocation(s_programID, 2, "normal");

编辑

着色器中的属性位置:

//Before
a_normal 0
a_position 1
a_textureCoords 2

//After
a_position 0
a_textureCoords 1
normal 2

我建议你这样写:

#version 150

#define POSITION  0
#define TEX_COORD 1 
#define NORMAL    2 

//our attributes
layout (location = POSITION) in vec3 a_position;
layout (location = TEX_COORD) in vec2 a_textureCoords;
layout (location = NORMAL) in vec3 a_normal;

//send the color out to the fragment shader
out vec2 vTextureCoords;

void main(void)
{
    a_normal;
    gl_Position = vec4(a_position, 1.0);
    vTextureCoords = a_textureCoords;
}

然后在 java 你有类似的东西:

interface Semantic {

   interface Attr {

      int POSITION = 0;
      int TEX_COORD = 1;
      int NORMAL = 2;
   }
}

您将在 glVertexAttribPointerglEnableVertexAttribute 函数中使用

否则,记得glBindAttribLocation before or glGetAttribLocation after linking the program

Ps: 记住做一些有用的事情 a_normal 否则 glsl 编译器会通过完全删除它来优化它