Reputation: 124
So in my OpenGL code, whenever I try using a shader program in my project, the screen just stays black.
I've purposely made an error in the shader code to make sure the error checking worked, and it did, so when I went to fix it and run the program again it just gave a black screen.
I've went into other projects to make sure shader programs worked, and it did, but for this specific project, they just don't work at all.
This code is just part of a simple triangle program.
But as you can see, the UseProgram
call is commented out.
When I uncomment it, I just get a black screen.
// I'm only using this for testing purposes
static constexpr const char* vertexShaderCode = R"(
#version 460 core
layout (location = 0) in vec2 vertexPosition;
void main() {
gl_Position = vec4(vertexPosition, 0.0, 0.0);
}
)";
static constexpr const char* fragmentShaderCode = R"(
#version 460 core
out vec4 fragmentColor;
void main() {
fragmentColor = vec4(0.1, 0.1, 1.0, 1.0);
}
)";
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderCode, nullptr);
glShaderSource(fragmentShader, 1, &fragmentShaderCode, nullptr);
glCompileShader(vertexShader);
glCompileShader(fragmentShader);
// error checking
// I've made sure it logs errors to the console, and it does.
{
int compileSuccess;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &compileSuccess);
if (!compileSuccess) {
char infoLog[512];
glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
cerr << "Failed to compile vertex shader!" << '\n';
cerr << "Shader Info log: " << infoLog << '\n';
}
}
{
int compileSuccess;
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &compileSuccess);
if (!compileSuccess) {
char infoLog[512];
glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
cerr << "Failed to compile fragment shader!" << '\n';
cerr << "Shader Info log: " << infoLog << '\n';
}
}
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// This logs errors and it works as well.
{
int linkSuccess;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkSuccess);
if (!linkSuccess) {
char infoLog[512];
glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
cerr << "Failed to link Shader Program!" << '\n';
cerr << "Shader Program Info log: " << infoLog << '\n';
}
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// when this is un commented the screen stays black
//glUseProgram(shaderProgram);
// rest of code to create triangle ...
Upvotes: 1
Views: 435
Reputation: 210877
The 4th component of the clip space coordinate should be to be 1.0:
gl_Position = vec4(vertexPosition, 0.0, 0.0);
gl_Position = vec4(vertexPosition, 0.0, 1.0);
The clip space coordinate is a Homogeneous coordinates. The normalized device coordinate is a Cartesian coordinate it is computed by dividing the x, y and z component of the clip space coordinate by the w component of the clip space coordinate (Perspective divide).
Upvotes: 1