gpt4 book ai didi

c++ - glBufferData 上的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 15:47:27 27 4
gpt4 key购买 nike

我正在尝试移植我的固定功能管线 openGL 代码以使用 GLSL,但我遇到了 glBufferData 的段错误。这一切都与固定功能的东西一起工作得很好(即我可以毫无问题地渲染加载的网格)。

这是加载顶点的代码(来自 AssImp 导入程序库):

在Mesh.h文件中定义:

...
glm::detail::uint32 vaoId_[];
glm::detail::uint32 vboIds_[];

std::vector< glm::vec3 > vertices_;
std::vector< glm::vec3 > normals_;
std::vector< glm::vec2 > textureCoordinates_;
std::vector< glm::vec4 > colors_;
...

在 Mesh.cpp 中实现:

...
Mesh::Mesh(const aiMesh* mesh) {
vaoId_[1];
vboIds_[4];

BOOST_LOG_TRIVIAL(debug) << "loading mesh...";

glm::detail::uint32 currentIndex = 0;

for (glm::detail::uint32 t = 0; t < mesh->mNumFaces; ++t) {
const aiFace* face = &mesh->mFaces[t];
GLenum face_mode;

switch(face->mNumIndices) {
case 1: face_mode = GL_POINTS; break;
case 2: face_mode = GL_LINES; break;
case 3: face_mode = GL_TRIANGLES; break;
default: face_mode = GL_POLYGON; break;
}

glm::detail::uint32 numIndices = face->mNumIndices;

vertices_.resize( currentIndex + numIndices );
normals_.resize( currentIndex + numIndices );
textureCoordinates_.resize( currentIndex + numIndices );
colors_.resize( currentIndex + numIndices );

//BOOST_LOG_TRIVIAL(debug) << "loading face: " << face->mNumIndices;
// go through all vertices in face
for(glm::detail::uint32 i = 0; i < numIndices; i++) {
// get group index for current index i
int vertexIndex = face->mIndices[i];

if (mesh->mNormals != 0) {
vertices_[currentIndex + i] = glm::vec3( mesh->mVertices[vertexIndex].x, mesh->mVertices[vertexIndex].y, mesh->mVertices[vertexIndex].z );
normals_[currentIndex + i] = glm::vec3( mesh->mNormals[vertexIndex].x, mesh->mNormals[vertexIndex].y, mesh->mNormals[vertexIndex].z );
}

if (mesh->HasTextureCoords(0)) {
textureCoordinates_[currentIndex + i] = glm::vec2( mesh->mTextureCoords[0][vertexIndex].x, mesh->mTextureCoords[0][vertexIndex].y );
}

//utilities::AssImpUtilities::color4_to_vec4(&mesh->mColors[0][vertexIndex], colors_[colors_.size() + i]);
if (mesh->mColors[0] != 0) {
colors_[currentIndex + i] = glm::vec4(
(float)mesh->mColors[0][vertexIndex].a,
(float)mesh->mColors[0][vertexIndex].b,
(float)mesh->mColors[0][vertexIndex].g,
(float)mesh->mColors[0][vertexIndex].r
);
}
}

currentIndex += 3;
}

BOOST_LOG_TRIVIAL(debug) << "loading mesh into video memory...";

BOOST_LOG_TRIVIAL(debug) << "blah: " << vertices_.size();
BOOST_LOG_TRIVIAL(debug) << "blah: " << ( sizeof(glm::vec3) );
BOOST_LOG_TRIVIAL(debug) << "blah: " << ( &vertices_[0] );

// create our vao
glGenVertexArrays(1, &vaoId_[0]);
glBindVertexArray(vaoId_[0]);

// create our vbos
//glGenBuffers(4, &vboIds_[0]);
glGenBuffers(1, &vboIds_[0]); // Using only '1' for now (otherwise causes seg fault)
glBindBuffer(GL_ARRAY_BUFFER, vboIds_[0]);
glBufferData(GL_ARRAY_BUFFER, vertices_.size() * sizeof(glm::vec3), &vertices_[0], GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

// Disable our Vertex Buffer Object
glBindVertexArray(0);

BOOST_LOG_TRIVIAL(debug) << "done loading mesh...";
}
...

我似乎无法弄清楚为什么当我调用 glBufferData(GL_ARRAY_BUFFER, vertices_.size() * sizeof(glm::vec3), &vertices_[0], GL_STATIC_DRAW);.

完整的错误是(在 linux 中使用 gdb):

Program received signal SIGSEGV, Segmentation fault. 0x00007fffefb1ae70 in ?? () from /usr/lib/x86_64-linux-gnu/libnvidia-glcore.so.304.64

有人有什么想法吗?

最佳答案

您需要在 glBufferData 之前调用 glBindBuffer

关于c++ - glBufferData 上的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14265969/

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