Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 0xC0000005:访问冲突执行位置0x00000000。(OpenGL)_C++_Opengl_Access Violation_Glew - Fatal编程技术网

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

C++ 0xC0000005:访问冲突执行位置0x00000000。(OpenGL),c++,opengl,access-violation,glew,C++,Opengl,Access Violation,Glew,我已经研究了堆栈上关于这一点的其他几个问题,其中提到了去引用空指针,但我不明白这是否适用于我的代码 试图生成VAO时,代码在World.cpp的第33行崩溃: glGenVertexArrays(1, &vao); 给我标题中显示的错误。如果我把那行注释掉,程序运行得很好 PhaseEngineMain.cpp PhaseEngineController.h #pragma一次 #包括“SDL.h” #包括“glew.h” #包括“World.h” #包括 类相位控制器 { 公众: 相

我已经研究了堆栈上关于这一点的其他几个问题,其中提到了去引用空指针,但我不明白这是否适用于我的代码

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

glGenVertexArrays(1, &vao);
给我标题中显示的错误。如果我把那行注释掉,程序运行得很好

PhaseEngineMain.cpp

PhaseEngineController.h

#pragma一次
#包括“SDL.h”
#包括“glew.h”
#包括“World.h”
#包括
类相位控制器
{
公众:
相位控制器();
~PhaseEngineController();
void InitialiseEngine();
void initialseopengl();
无效初始值SDL(Uint32 x、Uint32 y、Uint32宽度、Uint32高度、Uint32标志);
void InitialiseGLEW();
void setClearColor(浮子r、浮子g、浮子b、浮子a);
作废PrintInitializationInfo();
void start();
无效停止();
无效运行();
void UpdateLoop();
void RenderLoop();
void SwapBackBuffer();
私人:
SDL_窗口*窗口;
SDL_GLContext opengl_context;
世界;
bool running=false;
};
PhaseEngineController.cpp

#包括“PhaseEngineController.h”
PhaseEngineController::PhaseEngineController()
{
初始化引擎();
初始化SDL(500900600,显示SDL_窗口| SDL_窗口| OPENGL);
初始化OpenGL();
InitialiseGLEW();
PrintInitializationInfo();
addMesh();
}
PhaseEngineController::~PhaseEngineController()
{
SDL_GL_DeleteContext(opengl_context);
SDL_窗口(窗口);
SDL_退出();
}
void PhaseEngineController::InitialiseEngine()
{
std::cout试试看


简短回答:默认情况下,GLEW无法访问OpenGL核心配置文件的某些部分,因此在调用
glewInit()之前会说“
glewExperimental=GL\u TRUE;
可能会有帮助。

您能具体指出第33行指的是哪一行吗?很抱歉,我没有意识到它没有行号,请检查问题以获得重新编辑的GlgenVertexarray(1,&vao);在哪里创建/初始化GL上下文?
window
是否非空?
opengl\u context
?在创建GL上下文之前,为什么要调用
initialseopengl()
?为什么不检查
glewInit()的返回值
?看起来可能就是这种情况。OP正在使用GLEW,包括它,并调用
glewInit
#include "PhaseEngineMain.h"
#include "PhaseEngineController.h"

int main(int argc, char *argv[])
{
    PhaseEngineController engine;
    engine.start();
    return 0;
}
#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;
};
#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;
}
#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() { }