2
votes

I have a windows build environment using cygwin and GCC, and am linking to the libraries for GLEE, GLUT, and opengl32. This is a Win32 build.

All calls to glCreateShader are returning 0, yet I'm not picking up any errors. The following is based on the Lighthouse tutorials for GLUT and GLSL, so the sequence of GL operations should be correct.

Here's the relevant code..

#define WIN32

#include <stdio.h>

#include <GL/GLee.h>
#include <GL/glut.h>

#include "SampleUtils.h"
#include "LineShaders.h"

GLint lineVertexHandle      = 0;
unsigned int lineShaderProgramID;

...

int main(int argc, char **argv) {

    // init GLUT and create window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("Lighthouse3D Tutorials");

    // register callbacks
    glutDisplayFunc(renderScene);
    glutReshapeFunc(changeSize);
    glutIdleFunc(renderScene);
        
    // initialize the shaders
    init();
    // enter GLUT event processing cycle
    glutMainLoop();
    return 0;
}

void init() {
    glClearColor( 0.0, 0.0, 0.0, 1.0 ); /* Set the clear color */

    lineShaderProgramID = SampleUtils::createProgramFromBuffer(lineMeshVertexShader,lineFragmentShader);
    
    lineVertexHandle = glGetAttribLocation(lineShaderProgramID,"vertexPosition");

}

SampleUtils is a utility class w/ the following methods for shader handling. The shaders lineMeshVertexShader and lineFragmentShader are defined in LineShaders.h.

unsigned int SampleUtils::createProgramFromBuffer(const char* vertexShaderBuffer, const char* fragmentShaderBuffer) {
    checkGlError("cPFB");

    // scroll down for initShader() - we never get past this point.
    GLuint vertexShader = initShader(GL_VERTEX_SHADER, vertexShaderBuffer);

    if (!vertexShader)
        return 0;    

    GLuint fragmentShader = initShader(GL_FRAGMENT_SHADER,
                                        fragmentShaderBuffer);
    if (!fragmentShader)
        return 0;

    GLuint program = glCreateProgram();
    if (program)
    {
        glAttachShader(program, vertexShader);
        checkGlError("glAttachShader");
        
        glAttachShader(program, fragmentShader);
        checkGlError("glAttachShader");
        
        glLinkProgram(program);
        GLint linkStatus = GL_FALSE;
        glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
        
        if (linkStatus != GL_TRUE)
        {
            GLint bufLength = 0;
            glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
            if (bufLength)
            {
                char* buf = (char*) malloc(bufLength);
                if (buf)
                {
                    glGetProgramInfoLog(program, bufLength, NULL, buf);
                    LOG("Could not link program: %s", buf);
                    free(buf);
                }
            }
            glDeleteProgram(program);
            program = 0;
        }
    }
    return program;

    }

    unsigned int
    SampleUtils::initShader(unsigned int shaderType, const char* source)
    {
    checkGlError("initShader");
    //GLuint shader = glCreateShader((GLenum)shaderType);

    /* trying explicit enum, just in case - shader is still always 0 */
    GLuint shader = glCreateShader(GL_VERTEX_SHADER);

    LOG("SHADER %i", shader);
    
    if (shader)
    {
        glShaderSource(shader, 1, &source, NULL);
        glCompileShader(shader);
        GLint compiled = 0;
        glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
    
        if (!compiled)
        {
            GLint infoLen = 0;
            glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
            if (infoLen)
            {
                char* buf = (char*) malloc(infoLen);
                if (buf)
                {
                    glGetShaderInfoLog(shader, infoLen, NULL, buf);
                    LOG("Could not compile shader %d: %s", 
                        shaderType, buf);
                    free(buf);
                }
                glDeleteShader(shader);
                shader = 0;
            }
        }
    }
    return shader;

}

void SampleUtils::checkGlError(const char* operation) { 
    for (GLint error = glGetError(); error; error = glGetError())
        LOG("after %s() glError (0x%x)", operation, error);
}

I'm wondering if the context isn't fully initialized when glCreateShader is called. But I've tried calling init() within the callbacks as well, with no effect. My searches on this issue have turned up the advice to build a known-good example, to confirm the availability of glCreateShader - if anyone has one for C++, pls advise.


UPDATE:

Based on the feedback here I'd checked my OpenGL support using the glewinfo utility and it's reporting that this system is limited to 1.1. - https://docs.google.com/document/d/1LauILzvvxgsT3G2KdRXDTOG7163jpEuwtyno_Y2Ck78/edit?hl=en_US

e.g.

---------------------------
    GLEW Extension Info
---------------------------

GLEW version 1.6.0
Reporting capabilities of pixelformat 2
Running on a GDI Generic from Microsoft Corporation
OpenGL version 1.1.0 is supported

GL_VERSION_1_1:                                                OK
---------------

GL_VERSION_1_2:                                                MISSING
---------------

etc.

What's strange is that with GLEE I was able to compile these extensions, though they apparently don't work. I've checked my gl.h and glext.h header files and they are current - the extensions are there. So how is this dealt with on Windows? How do you set up and link your environment so that you can develop w/ more than 1.1 using cygwin and Eclipse?

2
I've never used GLee, but I know that because of its unique loading methadology, it may simply return 0 if your OpenGL version doesn't support the function. So do you get an OpenGL context version 2.0 or greater? - Nicol Bolas
No glGetString(GL_VERSION) is giving me 1.1.0. But I've read that this is to be expected on Windows. I'll try GLEW and see if that helps. - olo
@olo I don't think GLEW will change that. This is only to be expected if your hardware only supports GL 1.1 or if you have a very old graphics driver (maybe the Windows default one?). Or maybe you are linking to the wrong "opengl32.dll" and not the one in your system directory?. - Christian Rau
My NVidia Driver is version 8.15.11.8593, dated 2009. This in on a newer Win 7 64bit system. I'm linking to opengl32, which should be the dll in my system32 directory. - olo
If you are getting version 1.1.0, then you are initializing OpenGL incorrectly. - Nicol Bolas

2 Answers

4
votes

The solution to this question was provided in the comments, and I'm highlighting it here in order to close this question.

All that was required was a driver upgrade to a version that supports the extensions that I'm using. So I installed NVidia's OpenGL driver, which can be obtained here - http://developer.nvidia.com/opengl-driver

It appears that my system's original NVidia driver was subverted so that the native windows OpenGL driver was being used. This only supports OpenGL 1.1. But I'd mistakenly thought that a GL_VERSION of 1.1.0 was normal on Windows - based on some bad advice I'd gotten. And the fact that I was able to compile and execute this code without errors led me to assume that the extensions were present. They were not.

3
votes

I have had the same problem, but it was a silly C++ language trick : my shader was compiled in a global / static variable (which was a wrapper class to use program shader), which was so initialized before having a GL context. Hope it can help...