Hi, how to fix this problem, the out_color
works when I set it to vec4(1, 0, 0, 1)
but the triangle becomes black when I assing out_color
with in_uv
or in_color
. How to fix that problem?
Version: GL3W
When a shader is failed to compile then there is an error message and the triangle becomes white, but there is no error message and the triangle is not white but black which means that I successfully compiled the shaders.
SimpleVertexShader.vert
#version 330 core
// in - Vertex
layout (location = 0) in vec3 in_pos;
layout (location = 1) in vec2 in_uv;
layout (location = 2) in vec4 in_color;
// out
layout (location = 0) out vec2 out_uv;
layout (location = 1) out vec4 out_color;
void main() {
gl_Position = vec4(in_pos, 1);
out_uv = in_uv;
out_color = in_color;
}
SimpleFragmentShader.frag
#version 330 core
// in
layout (location = 0) in vec2 in_uv;
layout (location = 1) in vec4 in_color;
// out
layout (location = 0) out vec4 out_color;
void main() {
// Red triangle
//out_color = vec4(1, 0, 0, 1);
// Test UV
//out_color = vec4(in_uv.x, in_uv.y, 0, 1);
// Test color
out_color = vec4(in_color.rgb, 1);
}
Initialization in the .cpp file
programID = LoadShaders(
"SimpleVertexShader.vert",
"SimpleFragmentShader.frag");
static const GLfloat vertex_buffer_data[] = {
// Positions UVs Colors
-0.7f, -0.7f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.7f, -0.7f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.7f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f,
};
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data), vertex_buffer_data, GL_STATIC_DRAW);
In the loop in the .cpp file
glUseProgram(programID);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // index
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
(3 + 2 + 3) * sizeof(GLfloat), // stride
(void*)0 // array buffer offset
);
glVertexAttribPointer(
1, // index
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
(3 + 2 + 3) * sizeof(GLfloat), // stride
(void*)(3 * sizeof(GLfloat)) // array buffer offset
);
glVertexAttribPointer(
2, // index
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
(3 + 2 + 3) * sizeof(GLfloat), // stride
(void*)((3 + 2) * sizeof(GLfloat)) // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle
glDisableVertexAttribArray(0);