c - 简单 OpenGL 应用程序中的 “Access violation reading location 0x000000000”

标签 c opengl

我在 openGL 中使用缓冲区对象存储顶点数组时遇到问题。 当我运行代码时,会出现一个显示消息“访问冲突读取位置 0x000000000”的窗口,当我将光标移动到 glGenBuufers 函数旁边的箭头时,我看到了描述:这是下一个语句当此线程从当前函数返回时执行

IDE snapshot

代码:

int frame=0;
void display();
void datasource();

 GLuint vbo;

void datasource()
{
    GLfloat vertex1[]={-1.000000 ,0.500000 ,-0.700000 ,0.500000 ,-1.000000 ,0.800000 ,-0.700000 ,0.800000 ,-0.400000 ,0.500000 ,-0.100000 ,0.500000 ,-0.400000 ,0.800000 ,-0.100000 ,0.200000 ,0.500000 ,0.500000 ,0.500000 ,0.200000 ,0.800000 ,0.500000,0.800000 ,0.800000 ,0.800000 ,0.500000 ,1.100000 ,0.500000 ,0.800000 ,0.800000 ,1.100000,0.800000  };

    glGenBuffers(1,&vbo);
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertex1),vertex1,GL_STATIC_DRAW);
}


void init(int arc,char **arch)
{
    glutInit(&arc,arch);
    glutInitWindowSize(800,800);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutCreateWindow("chess");
    glClearColor(0.0,0.0,0.0,0.0);
    glutDisplayFunc(display);
}

int main(int arc,char **arch)
{
    init(arc,arch);
    datasource();
    glutMainLoop();
    return 0;
}

void display()
{
    frame++;
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLE_STRIP,0,8);
    glutSwapBuffers();
    glutPostRedisplay();
}

我做错了什么?

最佳答案

glGenBuffers、glBindBuffer 和 glBufferData 是 Vertex Buffer Object extension 或 OpenGL-1.5 的函数。默认系统 ABI 仅适用于 OpenGL-1.2,因此您需要先加载扩展的 OpenGL 功能,然后才能使用它。仅包含 header 是不够的,因为它们只是为您提供未初始化的符号。

最简单的方法:

  1. http://glew.sf.net 下载并安装 GLEW
  2. 替换所有出现的#include <GL/gl.h>#include <GL/glew.h>
  3. 调用glewInit()glutCreateWindow之后并检查错误代码
  4. 检查 VBO 是否可用(即至少 GL_ARB_vertex_buffer_object 或 OpenGL 版本 >= 1.5)
  5. 将 GLEW 库添加到您的链接器设置

关于c - 简单 OpenGL 应用程序中的 “Access violation reading location 0x000000000”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14009726/

相关文章:

C++ - SDL2 和 OpenGL - Visual Studio 错误 LNK2019 : unresolved external symbol

opengl - gluPerspective 与 gluOrtho2D

c - 读取一串预期模式

c++ - 一个doxygen eclipse插件自动生成 stub 文档?

c - 缓冲区溢出 - 普通用户的 SegFaults

performance - 在AMD上写入非零FBO附件时OpenGL会降低性能

c - fork后Printf无法在父级和子级中打印

c - 需要有关代码片段的建议

opengl - 从 uint64_t 图像句柄创建 image2D

c++ - QOpenGLWidget绘制三角形