Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to start learning OpenGL. So I got myself the Superbible and followed all instructions. Here's the first code sample:

// 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 somehow I alway run into a "Unhandled exception at 0x00000000 in Triangle.exe: 0x00000005: Access violation reading location 0x00000000".

Then it alway stops at glutMainLoop();

What is this? I use VS2008 Professional Edition.

share|improve this question
1  
Use your debugger. – Jonathan Hobbs Oct 29 '11 at 12:29
I would try to compile a version without using shaders, and see what happens. I am not very familiar with this GLShaderManager.hh, I have never used such thing. If you are new to OpenGL, I would initially try to understand a simple hello world without using shaders, you can get it from here davidwparker.com/2011/07/22/opengl-screencast-hello-world-glut – Dan Oct 29 '11 at 13:26
2  
Stating the obvious here... you are trying to read a NULL pointer. Use your debugger with stack trace and see where it is reading the NULL pointer and why is it NULL. – Samaursa Oct 31 '11 at 2:40
Looked into it. It seems the problem lies in SetupRC(). The error occurs when the triangleBatch code is executed. If I comment out trianglebatch.Begin(); + ....CopyVertexData3f(vVerts); + triangleBatch.end(); It works and shows the scene. – bodycountPP Nov 2 '11 at 9:12

closed as too localized by Tetrad Mar 12 at 4:55

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Browse other questions tagged or ask your own question.