I am currently trying to learn OpenGL, and I have the following issue: when I use glGetUniformLocation to locate my shader uniform, it simply cannot find this uniform. Do you have any idea why?
My code:
const char* vertexShaderSource = R"(
#version 460 core
uniform vec3 transformation;
in vec3 position;
out vec4 color;
void main()
{
gl_Position = vec4(position, 1.0);
color = vec4(1.0, 1.0, 0.0, 1.0);
}
)";
const char* fragmentShaderSource = R"(
#version 460 core
in vec4 color;
out vec4 outColor;
void main()
{
outColor = color;
}
)";
int main()
{
SDL_Init(SDL_INIT_VIDEO);
Window2D window2D("Debug", 800, 600);
gladLoadGL();
glViewport(0, 0, 800, 600);
//Dreieck vertices
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
//Shader
//Vertex Shader
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
//Fragment Shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
//Programm
GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
//Uniform
GLint uniform = glGetUniformLocation(program, "transformation");
if (uniform == -1) {
std::cerr << "Error: Not Found" << std::endl;
}
//Shader
//VBO and VAO
GLuint VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*) 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
while(window2D.windowRun() == false)
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
window2D.closeWindow();
return 0;
}
Otherwise, everything is actually working; the triangle is displayed in yellow. It's just the uniform that isn't functioning
"Since I am still a beginner, unfortunately, I don't know where to start. I only know that the shader compiles correctly because I can see the yellow triangle. Also, I know that glGetUniformLocation always returns -1, as I observe it through my check every time."
Aucun commentaire:
Enregistrer un commentaire