gpt4 book ai didi

opengl - 初始化 vao 时出错(glCreateVertexArrays())

转载 作者:行者123 更新时间:2023-12-02 21:09:00 31 4
gpt4 key购买 nike

为了了解如何使用两个缓冲区并将其绑定(bind)到 vao,我试图显示一个带有某种颜色的三角形。我收到这样的错误

ConsoleApplication12.exe 中 0x00000000 处的首次机会异常:0xC0000005:执行位置 0x00000000 时出现访问冲突。

当我尝试调试时,此错误是由代码中的行引起的

glCreateVertexArrays(1, &vao);

我确实初始化了vao,但我不明白为什么会发生错误。请帮助我..

这是代码

#include <stdlib.h>
#include<iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "glm/glm.hpp"
#define BUFFER_OFFSET(offset) ((GLvoid *) offset)
GLuint program;
static int timeFor =0;
GLuint vPos;
GLuint buffer[2];
GLuint vao = 0;
void init()
{
static const GLfloat positions[] = { 0.1f, 0.2f, 0.3f, 0.0f};

static const GLfloat colors[] = { 1.0f, 0.0f, 0.0f, 1.0f};

glCreateVertexArrays(1, &vao);
glCreateBuffers(2, &buffer[0]);
glNamedBufferStorage(buffer[0], sizeof(positions), positions, 0);
glVertexArrayVertexBuffer(vao, 0, buffer[0], 0, sizeof(glm::vec4));
glVertexArrayAttribFormat(vao, 0, 4, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(vao, 0, 0);

glNamedBufferStorage(buffer[1], sizeof(colors), colors, 0);
glVertexArrayVertexBuffer(vao, 1, buffer[1], 0, sizeof(glm::vec4));
glVertexArrayAttribFormat(vao, 1, 4, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(vao, 1, 1);


const char* vSource = {
"#version 330\n"
"layout (location = 0) in vec4 offset;"
"layout (location = 1) in vec4 color;"
"out vec4 fColor;"
"void main() {"
"const vec4 vertices[3] = vec4[3](vec4( 0.25, -0.25, 0.5, 1.0), vec4( -0.25, -0.25, 0.5, 1.0), vec4( 0.25, 0.25, 0.5, 1.0));"
"gl_Position = vertices[gl_VertexID] + offset;"
"fColor = color;"
"}"
};

const char* fSource = {
"#version 330\n"
"in vec4 fColor;"
"out vec4 fragColor;"
"void main() {"
"fragColor = fColor;"
"}"
};


GLuint vShader, fShader;
vShader = glCreateShader(GL_VERTEX_SHADER);
fShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vShader, 1, &vSource, NULL);
glShaderSource(fShader, 1, &fSource, NULL);
glCompileShader(vShader);
glCompileShader(fShader);
program = glCreateProgram();
glAttachShader(program, vShader);
glAttachShader(program, fShader);
glLinkProgram(program);
glClearColor(1.0f, 1.0f, 0.0f, 1.0f);

}

void reshape(int width, int height)
{
glViewport(0, 0, width, height);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();

}

void main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow(argv[0]);
glewInit();
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}

最佳答案

我检查了我的 openGL 版本是 4.4,其中不支持 glCreateVertexArrays,而必须使用 glGenVertexArrays。

关于opengl - 初始化 vao 时出错(glCreateVertexArrays()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34578388/

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