Studio社区中获取访问冲突异常

Studio社区中获取访问冲突异常

本文介绍了使用glfw3 glew和opengl在Visual Studio社区中获取访问冲突异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题我已经有一段时间了,我不能为我的爱找到解决方法.

I have been having this problem for a while and I cant for the love of me find a solution.

我想绘制一个简单的三角形.但是在编译程序时,我一直在Visual Studio中获得此输出.

I want to render a simple triangle. But i keep getting this output in visual studio when compiling the program.

注意>我不相信这不是链接问题,而是其他问题.我已经检查了我的链接器无数次了,一切都在那里!

NOTE> I do not belive it is not a linking problem but something else. I have checked my linker countless times and everything is there!

LINK: https://pastebin.com/xeTDd0Qu

主要

static const GLfloat g_vertex_buffer_data[] = {
    100.0f, 100.0f, 0.0f,
    150.0f, 100.0f, 0.0f,
    100.0f, 150.0f, 0.0f,
};



GLFWwindow* window;
window = initWindow(640, 480, "Title");


GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);


GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

while (!glfwWindowShouldClose(window)) {

    glViewport(0, 0, 640, 480);
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(0);

    glFlush();

    glfwSwapBuffers(window);
    glfwPollEvents();
}

glfwTerminate();

return 0;

initWindow()

initWindow()

GLFWwindow* initWindow(int a_width, int a_height, const char* title) {
glewExperimental = GL_TRUE;
int err = glewInit();
if (!err) {
    exit(-1);
}

if (!glfwInit()) {
    printf("glfwInit() failed!");
    return nullptr;
}

GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);

if (!window) {
    glfwTerminate();
    return nullptr;
}

return window;

}

谢谢!

我收到异常消息:ConvexHullVisualiser.exe中的0x00000000引发异常:0xC0000005:执行位置0x00000000的访问冲突.

Exception message i get:Exception thrown at 0x00000000 in ConvexHullVisualiser.exe: 0xC0000005: Access violation executing location 0x00000000.

推荐答案

所收到的错误告诉您您正在尝试执行指向NULL的函数指针.大多数OpenGL函数是(在Windows上)函数指针,并在运行时加载.总体而言,这意味着您正在尝试执行尚未加载的OpenGL函数.

The error you get tells you that you are trying to execute a function pointer that points to NULL. Most OpenGL functions are (on windows) function pointers and are loaded at runtime. In total, this means you are trying to execute an OpenGL function that has not been loaded.

最可能发生这种情况的原因是,只有在存在有效的OpenGL上下文的情况下,才能成功初始化GLEW.由于上下文是由glfwCreateWindow创建的,因此必须在此行之后调用glewInit.

Most probably, this happens because GLEW can only be initialized successfully if there is a valid OpenGL context present. Since the context is created by glfwCreateWindow, glewInit has to be called after this line.

您还缺少对glfwMakeContextCurrent的绑定OpenGL上下文的调用到活动线程.

You are also missing a call to glfwMakeContextCurrent to bind the OpenGL contextto the active thread.

if (!glfwInit()) {
    printf("glfwInit() failed!");
    return nullptr;
}

GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);

if (!window) {
    glfwTerminate();
    return nullptr;
}

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
int err = glewInit();
if (!err) {
    exit(-1);
}

请注意,glewInit不会返回整数,而是返回GLenum.正确的错误检查应如下所示:

Note, that glewInit does not return an int but a GLenum. The correct error check should looks somehow like this:

GLenum err = glewInit();
if (GLEW_OK != err)
{
  /* Problem: glewInit failed, something is seriously wrong. */
  fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  ...
}

来源:GLEW文档

这篇关于使用glfw3 glew和opengl在Visual Studio社区中获取访问冲突异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 01:33