gpt4 book ai didi

c++ - 0xC0000005 : Access violation executing location 0x00000000. (OpenGL)

转载 作者:搜寻专家 更新时间:2023-10-31 00:14:32 47 4
gpt4 key购买 nike

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

当尝试生成 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() { }

最佳答案

尝试 this .

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

关于c++ - 0xC0000005 : Access violation executing location 0x00000000. (OpenGL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22813625/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com