3

I'm building a game using OpenGL and C++. I'm using GLFW and GLAD. I'm currently in the process of setting up simple shaders, but I'm completely roadblocked by a compilation problem. In a nutshell, shader compilation fails with no error message.

Here's my vertex shader (it's meant to draw 2D images and text):

#version 330 core

layout (location = 0) in vec2 vPosition;
layout (location = 1) in vec2 vTexCoords;
layout (location = 2) in vec4 vColor;

out vec4 fColor;
out vec2 fTexCoords;

uniform mat4 mvp;

void main()
{
    vec4 position = mvp * vec4(vPosition, 0, 1);
    position.y *= -1;

    gl_Position = position;
    fColor = vColor;
    fTexCoords = vTexCoords;
}

And here's the relevant code to create the shader, load the shader source, compile the shader, and check for errors.

GLuint shaderId = glCreateShader(GL_VERTEX_SHADER);

std::string source = FileUtilities::ReadAllText(Paths::Shaders + filename);

GLchar const* file = source.c_str();
GLint length = static_cast<GLint>(source.size());

glShaderSource(shaderId, 0, &file, &length);
glCompileShader(shaderId);

GLint status;

glGetShaderiv(shaderId, GL_COMPILE_STATUS, &status);

if (status == GL_FALSE)
{
    GLint logSize = 0;

    glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &logSize);

    std::vector<GLchar> message = std::vector<GLchar>(logSize);

    glGetShaderInfoLog(shaderId, logSize, nullptr, &message[0]);
    glDeleteShader(shaderId);

    std::cout << std::string(message.begin(), message.end());
}

Using that code, logSize is returned as 1, meaning that I'm unable to access the error message provided by GL. From what I can tell, the message doesn't exist at all. I've already seen the question posted here, in which the issue was a missing call to glCompileShader. As you can see, my code does call that function.

In attempting to solve this problem, I've already confirmed a few things.

  • My shader source (a string) is being read correctly. The source is a single string that, as far as I can tell, exactly matches the actual shader file.
  • There are no casting issues from the string source to GLchar const* or GLint (the variables file and length). Both look correct.
  • If I artificially inflate the value of logSize (to, say, 1000), the resulting message is nothing but zeroes. No error message exists.
  • I am calling glfwInit() and related functions before reaching this point in the code. Querying glGetString(GL_VERSION) does correctly return the target version (3.3.0).

Does anyone know how to fix this? As I said, my progress is completely blocked since I can't render anything (2D or 3D) without working shaders.

Thank you!

Grimelios
  • 351
  • 2
  • 11

1 Answers1

7

The problem is that you never upload any shader source to the shader.

The second parameter in this line:

glShaderSource(shaderId, 0, &file, &length);

tells OpenGL to load 0 code strings to the shader (nothing). Change this to 1, and it should work.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
BDL
  • 21,052
  • 22
  • 49
  • 55
  • Wow. Nice catch. Still my fault, but it's pretty frustrating that GL doesn't generate an error for passing zero. It's also strange to me that the shader isn't compiled anyway, given that the source code string is valid. – Grimelios Mar 30 '18 at 17:54
  • Well that wouldn't really make sense either. The third param is a list of shader sources. I believe this is so that you can create a template (such as utility functions), that can be included in any shader you want, increasing code re-usability. – Luple May 09 '18 at 22:33