gpt4 book ai didi

c++ - 顶点着色器中属性的位置不正确

转载 作者:行者123 更新时间:2023-11-30 04:46:50 24 4
gpt4 key购买 nike

我试图在我的顶点着色器中找到我的属性的位置。当我打印位置时,我得到 0、-1 和 -1。 “0”对应于 aPos 变量。即使我将 aPos 变量向下移动,它仍然打印 0。我读到 -1 意味着找不到该变量。

这是我的代码:

 // build and compile shaders
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
GLint status = GL_TRUE;
glGetShaderiv( vs, GL_COMPILE_STATUS, &status );

GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment_shader, NULL);
glCompileShader(fs);


GLuint shader_program = glCreateProgram(); //creating joint shader program
glAttachShader(shader_program, fs);
glAttachShader(shader_program, vs);
glLinkProgram(shader_program);

GLint loc_pos = glGetAttribLocation( shader_program, "aPos" );
std::cout << "apos position: " << loc_pos << " ";

loc_pos = glGetAttribLocation( shader_program, "aNormal" );
std::cout << "normal position: " << loc_pos << " ";

loc_pos = glGetAttribLocation( shader_program, "TexCoords" );
std::cout << "texCoords position: " << loc_pos << " ";

这是我的顶点着色器:

#version 150 core

in vec3 aPos;
in vec3 aNormal;
in vec2 aTexCoords;


out vec2 TexCoords;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
TexCoords = aTexCoords;
gl_Position = projection * view * model * vec4(aPos, 1.0);
}

片段着色器:

#version 150 core
out vec4 FragColor;

in vec2 TexCoords;

uniform sampler2D texture_diffuse1;

void main()
{
FragColor = texture(texture_diffuse1, TexCoords);
//FragColor = vec4(1.0);
}

任何帮助将不胜感激!!

最佳答案

您不会获得 aNormal 的位置,因为 aNormal 不是事件的程序资源,因为它没有在顶点着色器中“使用”。因此驱动程序优化了属性 aNormal 并且它没有属性索引。
如果在片段着色器中没有使用TexCoords,那么aTexCoords也是一样的,如果TexCoords没有被“使用”,那么 aTexCoords 也没有被“使用”。

参见 OpenGL 4.6 Core Profile Specification - 7.3.1 Program Interfaces , 第 102 页:

7.3.1 Program Interfaces

When a program object is made part of the current rendering state, its executable code may communicate with other GL pipeline stages or application code through a variety of interfaces. When a program is linked, the GL builds a list of active resources for each interface. Examples of active resources include variables, interface blocks, and subroutines used by shader code. Resources referenced in shader code are considered active unless the compiler and linker can conclusively determine that they have no observable effect on the results produced by the executable code of the program.


代码

loc_pos = glGetAttribLocation( shader_program, "TexCoords" );

返回 -1,因为 "TexCoords" 不是属性的名称。属性的名称是 "aTexCoords" 而不是 "TexCoords"

关于c++ - 顶点着色器中属性的位置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56523718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com