0xC0000005:访问冲突执行位置0x00000000。 (OpenGL的)

时间:2014-04-02 13:51:09

标签: c++ opengl access-violation glew

我已经在堆栈上看了几个关于此问题的其他问题,它提到了取消引用空指针,但我不明白这是否适用于我的代码。

在尝试生成VAO时,代码在World.cpp的第33行崩溃:

glGenVertexArrays(1, &vao);

给我标题中显示的错误。如果我注释掉那行,程序运行正常。

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() { }

1 个答案:

答案 0 :(得分:6)

尝试this

简短回答:默认情况下,GLEW无法访问OpenGL核心配置文件的某些部分,因此在致电glewExperimental = GL_TRUE;之前说“glewInit()”可能有所帮助。