Reputation: 333
I am trying to find the locations of my attributes in my vertex shader. When I print the locations I get 0, -1, and -1. The "0" corresponds to the aPos variable. Even if I move the aPos variable down it still prints 0. I read that -1 means that the variable can't be found.
Here is my code:
// 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 << " ";
Here is my vertex shader:
#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);
}
Fragment shader:
#version 150 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D texture_diffuse1;
void main()
{
FragColor = texture(texture_diffuse1, TexCoords);
//FragColor = vec4(1.0);
}
Any help would be greatly appreciated!!
Upvotes: 1
Views: 245
Reputation: 210878
You won't get a location for aNormal
, because aNormal
is not an active program resource, because it is not "used" in the vertex shader. So the driver optimize away the attribute aNormal
and it gets no attribute index.
If you don't use TexCoords
in the fragment shader, then the same is the case for aTexCoords
, if TexCoords
is not "used", then aTexCoords
is not "used", too.
See OpenGL 4.6 Core Profile Specification - 7.3.1 Program Interfaces, page 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.
The code
loc_pos = glGetAttribLocation( shader_program, "TexCoords" );
returns -1, because "TexCoords"
is not th name of an attribute. The name of the attribute is "aTexCoords"
rather than "TexCoords"
.
Upvotes: 1