I have looked at several other questions on stack about this and it mentions de-referencing null pointers but I don't understand if that applies to my code.
The code crashes at line 33 in the World.cpp when trying to generate a VAO:
glGenVertexArrays(1, &vao);
giving me the error shown in the title. If I comment out that line the program runs fine.
PhaseEngineMain.cpp
#include "PhaseEngineMain.h"
#include "PhaseEngineController.h"
int main(int argc, char *argv[])
{
PhaseEngineController engine;
engine.start();
return 0;
}
PhaseEngineController.h
#pragma once
#include "SDL.h"
#include "glew.h"
#include "World.h"
#include <iostream>
class PhaseEngineController
{
public:
PhaseEngineController();
~PhaseEngineController();
void InitialiseEngine();
void IntitialseOpenGL();
void InitialiseSDL(Uint32 x, Uint32 y, Uint32 width, Uint32 height, Uint32 flags);
void InitialiseGLEW();
void SetClearColour(float r, float g, float b, float a);
void PrintIntialisationInfo();
void start();
void stop();
void run();
void UpdateLoop();
void RenderLoop();
void SwapBackBuffer();
private:
SDL_Window* window;
SDL_GLContext opengl_context;
World world;
bool running = false;
};
PhaseEngineController.cpp
#include "PhaseEngineController.h"
PhaseEngineController::PhaseEngineController()
{
InitialiseEngine();
InitialiseSDL(500, 500, 900, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
IntitialseOpenGL();
InitialiseGLEW();
PrintIntialisationInfo();
world.addMesh();
}
PhaseEngineController::~PhaseEngineController()
{
SDL_GL_DeleteContext(opengl_context);
SDL_DestroyWindow(window);
SDL_Quit();
}
void PhaseEngineController::InitialiseEngine()
{
std::cout << "Intialising...\n" << std::endl;
}
void PhaseEngineController::IntitialseOpenGL()
{
opengl_context = SDL_GL_CreateContext(window);
glClearColor(0, 0,0, 1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
std::cout << "gl initialised - rendering ready" << std::endl;
}
void PhaseEngineController::InitialiseSDL(Uint32 x, Uint32 y, Uint32 w, Uint32 h, Uint32 f)
{
int error = SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
window = SDL_CreateWindow("Phaze Engine", x, y, w, h, f);
std::cout << "SDL initialised - window created" << std::endl;
}
void PhaseEngineController::InitialiseGLEW()
{
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
else
{
std::cout << "GlEW Intialised - library ready" << std::endl;
}
}
void PhaseEngineController::SetClearColour(float r, float g, float b, float a)
{
glClearColor(r, g, b, a);
}
void PhaseEngineController::start()
{
if (running) return;
running = true;
run();
}
void PhaseEngineController::stop()
{
if (!running) return;
running = false;
}
void PhaseEngineController::run()
{
while (running)
{
//std::cout << "working" << std::endl;
RenderLoop();
//UpdateLoop();
}
}
void PhaseEngineController::UpdateLoop()
{
SDL_Event event;
while (running)
{
/* Check for new events */
while (SDL_PollEvent(&event))
{
/* If a quit event has been sent */
if (event.type == SDL_QUIT)
{
/* Quit the application */
running = false;
}
}
}
world.update();
}
void PhaseEngineController::RenderLoop()
{
world.render();
SwapBackBuffer();
}
void PhaseEngineController::SwapBackBuffer()
{
SDL_GL_SwapWindow(window);
}
void PhaseEngineController::PrintIntialisationInfo()
{
std::cout << "\nEngine Initialized...\n" << std::endl;
}
World.cpp
#include <iostream>
unsigned int vbo = 0;
unsigned int vao = 0;
float points[] = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
World::World()
{
std::cout << "world created" << std::endl;
}
World::~World()
{
std::cout << "world destroyed" << std::endl;
}
void World::addMesh()
{
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao); //// <-- crashes here, this is line 33
std::cout << "mesh added " << std::endl;
}
void World::update() { }
void World::render() { }
Try this.
Short answer: GLEW has trouble accessing some parts of the OpenGL core profile by default, so saying "glewExperimental = GL_TRUE;
" before your call to glewInit()
might help.