My program compiles fine but when I run it, it crashes at glDrawArrays(GL_TRIANGLE_STRIP, 0, 4) saying:
Unhandled exception at 0x50D6C94E (nvoglv32.dll) in Chapter 2.exe: 0xC0000005: Access violation reading location 0x00000000.
A page also appears that says nvoglv32.pdb is not loaded. I checked to see if the pointers and values I passed were correct, and assert did not pick up anything.
Here's the code
Example.h
#include <windows.h>
#include "Example.h"
#include <iostream>
#include <gl\GLU.h>
#include <gl\glext.h>
#include "BufferUtilities.h"
bool Example::init()
{
if (!start())
{
return false;
}
glEnable(GL_DEPTH_TEST);
glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
GLfloat verticies[] = {
-2.0f, -2.0f, -2.0f,
2.0f, -2.0f, -2.0f,
-2.0f, -2.0f, 2.0f,
2.0f, -2.0f, 2.0f,
};
GLfloat colors[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f,
};
createArrayBuffer(verticies, &vertexBuffer, GL_STATIC_DRAW);
createArrayBuffer(colors, &colorBuffer, GL_STATIC_DRAW);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
return true;
}
void Example::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
bindAndPoint(&colorBuffer, 3);
bindAndPoint(&vertexBuffer, 3);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
BufferUtilities.h
#pragma once
#include <Windows.h>
#include <gl\GL.h>
#include <gl\GLU.h>
#include <gl\GLEXT.h>
#include <gl\wglext.h>
#include <iostream>
#include <assert.h>
#define BUFFER_OFFSET(i) ((char *)NULL+(i))
static PFNGLGENBUFFERSARBPROC glGenBuffers = NULL;
static PFNGLBINDBUFFERPROC glBindBuffer = NULL;
static PFNGLBUFFERDATAPROC glBufferData = NULL;
inline bool start()
{
glGenBuffers = (PFNGLGENBUFFERSARBPROC)wglGetProcAddress("glGenBuffers");
glBindBuffer = (PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer");
glBufferData = (PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData");
if (!glGenBuffers|| !glBindBuffer || !glBufferData)
{
std::cerr << "Vertex buffer objects are not supported by your graphics card." << std::endl;
return false;
}
return true;
}
inline void createArrayBuffer(GLfloat *array, GLuint *buffer, GLenum usage)
{
assert(array != NULL);
assert(buffer != NULL);
assert(usage != NULL);
glGenBuffers(1, buffer);
glBindBuffer(GL_ARRAY_BUFFER, *buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * (sizeof(array) / sizeof(array[0])), &array[0], usage);
}
inline void bindAndPoint(GLuint *buffer, int size)
{
assert(buffer != NULL);
assert(size != NULL);
glBindBuffer(GL_ARRAY_BUFFER, *buffer);
glColorPointer(size, GL_FLOAT, 0, BUFFER_OFFSET(0));
}