1

I'm having some issues getting debug info from openGL. I don't know if it makes any difference but I'm using glfw3 and glew (dynamically linked)

I can't seem to compile shaders and error messages I get is just gibberish.

Here's a sample code:

shader.ID = glCreateShader(shader.type);
const GLchar *source = (const GLchar *)shader.source.c_str();
glShaderSource(shader.ID, 1, &source, 0);
glCompileShader(shader.ID);
GLint compiled = 0;
glGetShaderiv(shader.ID, GL_COMPILE_STATUS, &compiled);
if (compiled == GL_FALSE) {
    GLint logLength = 0;
    glGetShaderiv(shader.ID, GL_INFO_LOG_LENGTH, &logLength); //won't update logLength
    //std::cout << logLength << std::endl;
    GLchar* infoLog = new char[logLength + 1];
    glGetShaderInfoLog(shader.ID, logLength, NULL, infoLog);
    std::cerr << infoLog << std::endl;
    delete[] infoLog;
    glDeleteShader(shader.ID);
}

glGetShaderiv won't update logLength, and the output I get will be something like this:

═²²²²½½½½½½½½■ε■

This is the first time I'm trying to compile shaders, am I missing something?

EDIT: Context Creation

#include <GL\glew\glew.h>
#include <GL\GLFW\glfw3.h>

void setupGLFW() {
    //glfwSetErrorCallback(glfwErrorCallback);
    if (GL_FALSE == glfwInit()) {
        std::cerr << "Error Initializing GLFW" << std::endl;
        exit(-1);
    }
    GLFWwindow* window = glfwCreateWindow(640, 480, "TEST", NULL, NULL);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1); 
}

void setupGLEW() {
    glewExperimental = GL_TRUE;
    const GLenum glewErr = glewInit();
    if (glewErr != GLEW_OK) {
        cerr << "GLEW failed to initialize." << endl;
        exit(-1);
    }
}
void testshaderLoader()
{
    setupGLFW();
    setupGLEW();
    glEnable(GL_DEBUG_OUTPUT);
    ShaderLoader sloader;
    string pathToShader = "absolute/path/to/shader"
    GLenum shaderType = GL_FRAGMENT_SHADER;
    sloader.loadShader(pathToShader, shaderType);
}
int main()
{
    testshaderLoader();
    return 0;
}
duoren
  • 43
  • 5
  • 1
    Do you have a current OpenGL context when making these calls? Are other calls working? – Reto Koradi Mar 05 '15 at 04:45
  • @AndonM.Coleman Hi, I tried casting to char but it didn't seem to make any difference. I can't help but wonder if the problem has something to do with glGetShaderiv. According to [this](https://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetShaderiv.xml) GL_INFO_LOG_LENGTH should return 0 if there are no logs, but it doesn't even bother updating the value I pass. – duoren Mar 05 '15 at 05:05
  • @RetoKoradi Hi, Yes I'm making the calls after I create the context. I updated my code to show how I'm creating the context. Other calls seem to work fine. – duoren Mar 05 '15 at 05:06
  • Your assumption that `compiled == GL_FALSE` indicats, that there is a shader, is wrong. You would need to check for an error using `glGetError ` after calling `glGetShaderiv( ... GL_COMPILE_STATUS ... )` too. You also never ensured that `glCreateShader` successfully created a shader (no `shader.ID != 0` and `glGetError` test after `glCreateShader`) . – t.niese Mar 05 '15 at 05:20
  • Can you show how you use `#include` for glfw3 and glew? And any `GLFW_INCLUDE_` macros? – Brett Hale Mar 05 '15 at 05:37
  • @BrettHale Hi, linked libs: glew32.lib glfw3dll.lib #include #include I'm not using any GLFW macros. – duoren Mar 05 '15 at 06:00
  • @t.niese Hi, I didn't know I needed glGetError(). I thought glGetShaderInfoLog would be able to generate a log without. In any case, I added glGetError() after glGetShaderiv and didn't seem to make a difference. – duoren Mar 05 '15 at 06:02
  • What is the value of `shader.ID`? What is the return value of `glGetError()` right after `glGetShaderiv( ... GL_COMPILE_STATUS ... )`? It is not about that you need `glGetError()` to get `glGetShaderInfoLog` working, but if there is an error reported by `glGetError()` then using `glGetShaderInfoLog` is useless in your particular case. – t.niese Mar 05 '15 at 06:16
  • 1
    Post a [MCVE](http://stackoverflow.com/help/mcve). Feel free to use [the code in my answer here](http://stackoverflow.com/a/28437536/44729) as a base. – genpfault Mar 05 '15 at 06:18
  • @t.niese glGetError() returns 1280 (GL_INVALID_ENUM) glCreateShader works fine, ID is 0 (initially ID set to -1) – duoren Mar 05 '15 at 06:40
  • 1
    You really should read the docs: [opengl.org: glCreateShader](https://www.opengl.org/sdk/docs/man/html/glCreateShader.xhtml#errors) : `[...]This function returns 0 if an error occurs creating the shader object.[...]`. And `glGetError` shows you that already `glGetShaderiv( ... GL_COMPILE_STATUS ... )` failed, so you wont be able to get any log either. – t.niese Mar 05 '15 at 06:45
  • @genpfault Thanks for the link! I didn't wanna post all my code since my shader loader is not minimal by any means (trying to emulate #include inside glsl, gets shader type by looking at its extension) I'll simplify my code as much as I can and will update it soon. – duoren Mar 05 '15 at 06:48
  • @t.niese I see, the shader I'm testing is not even my code, it's a very minimal vertex and fragment shaders I found online and I'm passing GL_VERTEX_SHADER for vertex shader, GL_FRAGMENT_SHADER for fragment. Thanks for pointing that out, I'll read the docs thoroughly. – duoren Mar 05 '15 at 06:52

0 Answers0