3

I compiled the following code:

// Triangle.cpp
// Our first OpenGL program that will just draw a triangle on the screen.

#include <GLTools.h>            // OpenGL toolkit
#include <GLShaderManager.h>    // Shader Manager Class

#ifdef __APPLE__
#include <glut/glut.h>          // OS X version of GLUT
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>            // Windows FreeGlut equivalent
#endif

GLBatch triangleBatch;
GLShaderManager shaderManager;

///////////////////////////////////////////////////////////////////////////////
// Window has changed size, or has just been created. In either case, we need
// to use the window dimensions to set the viewport and the projection matrix.
void ChangeSize(int w, int h)
    {
    glViewport(0, 0, w, h);
    }


///////////////////////////////////////////////////////////////////////////////
// This function does any needed initialization on the rendering context. 
// This is the first opportunity to do any OpenGL related tasks.
void SetupRC()
    {
    // Blue background
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f );

    shaderManager.InitializeStockShaders();

    // Load up a triangle
    GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f, 
                          0.5f, 0.0f, 0.0f,
                          0.0f, 0.5f, 0.0f };

    triangleBatch.Begin(GL_TRIANGLES, 3);
    triangleBatch.CopyVertexData3f(vVerts);
    triangleBatch.End();
    }



///////////////////////////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
    {
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
    triangleBatch.Draw();

    // Perform the buffer swap to display back buffer
    glutSwapBuffers();
    }


///////////////////////////////////////////////////////////////////////////////
// Main entry point for GLUT based programs
int main(int argc, char* argv[])
    {
    gltSetWorkingDirectory(argv[0]);

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Triangle");
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);

    GLenum err = glewInit();
    if (GLEW_OK != err) {
        fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
        return 1;
        }

    SetupRC();

    glutMainLoop();
    return 0;
    }

But when i try to execute it the program crash, then the debugger gives me the following error:

Excepción no controlada en 0x00000000 en Triangle.exe: 0xC0000005: Infracción de acceso al leer la ubicación 0x00000000.
fpointbin
  • 1,633
  • 3
  • 16
  • 20
  • The error's translation would be very nice of you – Armen Tsirunyan Jul 07 '11 at 16:02
  • On what line does the access violation occur? – Steve Jul 07 '11 at 16:04
  • Dereferencing a null pointer... – Bo Persson Jul 07 '11 at 16:06
  • A debugger would help to identify the actual line where the null pointer is dereferenced. It's hard to guess what's going wrong here, superficially it looks ok. And frankly, since the debugger will point to the offending line in 2 seconds whereas digging through the code will take hours, there's little reason not to use a debugger. – Damon Jul 07 '11 at 16:24
  • @Armen Access Violation, easy enough to figure out – MGZero Jul 07 '11 at 16:24
  • Program crashes at:shaderManager.InitializeStockShaders(); – fpointbin Jul 07 '11 at 16:49
  • It seems unlikely that this function would be faulty, seeing how thousands of people use it... but anyway. Try setting a breakpoint on the function call and see if the program reaches the breakpoint or crashes before (it should). If it reaches the breakpoint, "step into" and step line by line until you have the exact line of the crash. – Damon Jul 07 '11 at 17:09
  • I setted a breakpoint in that line, but when i press F11 program throw the exeption message... and break the function and go to the main program at line "glutMainLoop" – fpointbin Jul 07 '11 at 17:15
  • @fpointbin: did you build in debug? Both your program and this "GLTools" library? – Nicol Bolas Jul 07 '11 at 17:25
  • So it isn't that line, obviously :) You can always set a breakpoint in the first line of main (your program luckily isn't so terribly long) and step line by line until you get a crash. Once it's clear where the crash occurs, it is hopefully easier to tell what's wrong. – Damon Jul 07 '11 at 17:25
  • @Nicol Bolas you've got the reason x'D i forgot it sorry... So the line in GLTools.cpp that crashes is: hVertexShader = glCreateShader(GL_VERTEX_SHADER); and the program throw the error – fpointbin Jul 07 '11 at 17:30
  • What do `glGetString(GL_VERSION)`, `glGetString(GL_VENDOR)`, and `glGetString(GL_RENDERER)` return after the `glutCreateWindow()` call? – genpfault Jul 07 '11 at 17:35
  • How can i see the return values of a function in vs 2008? sorry im a newbie... i just started to read the sb opengl book :S... Im thinking that the problem is the version of my opengl (?) – fpointbin Jul 07 '11 at 17:55
  • Ok... i tried it: const GLubyte *str; and str = glGetString(GL_VERSION); but 0x00000000 error pointer at... then cant evaluate the expression – fpointbin Jul 07 '11 at 18:02
  • Can you get [this program](http://stackoverflow.com/questions/4338729/preserve-aspect-ratio-of-2d-object-on-window-resize/4352701#4352701) to build and run in your environment? – genpfault Jul 07 '11 at 18:12
  • Compiler gives me the following : LINK : fatal error LNK1104: no se puede abrir el archivo 'freeglut.lib' – fpointbin Jul 07 '11 at 18:21

2 Answers2

4

To use glCreateShader(GL_VERTEX_SHADER) you must be running OpenGL 2.0 or higher. One way to tell is to check the value of GLEW_VERSION_2_0 (after your call to glewInit()). If the value is true then OpenGL 2.0 is supported. Otherwise, you may need to update your graphics driver or use a newer graphics card.

fintelia
  • 1,201
  • 6
  • 17
1

The only way that this:

hVertexShader = glCreateShader(GL_VERTEX_SHADER)

Can get a NULL pointer exception (are you sure it's this line and not the one before it?) is if glCreateShader is NULL. GLTools is part of the OpenGL Superbible volume 5's distribution; it's not a "standard" OpenGL tool, so I can't say much about it.

But you seem to be initializing GLEW. And since you don't directly include the GLEW header, I can only guess that GLTools is including it for you. So GLEW's initialization ought to be carrying over to GLTools.

Check the value of "glCreateShader". Follow GLEW's #define for this function all the way back to the actual variable that GLEW defines, and then check this variable's value. If it is NULL, then you've got problems. Perhaps GLEW's initialization failed.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982