OpenGL在尝试渲染高度图时在glDrawElements上崩溃
OpenGL crash on glDrawElements while trying to render Height Map
我正在编写一个类,使用现代OpenGL技术从32位bmp渲染高度图。我让它与即时模式一起工作,但想尝试将其转换为现代 OpenGL 上下文。我正在使用一个元素数组缓冲区,其中包含地图的索引数据和具有顶点数据的 vbo,其中 y 分量取自位图的高度(像素颜色)。
现在,我在抽奖调用中收到未经处理的异常/访问冲突。这是我的代码:
void HeightMap::initHeightMap(const char* filePath, float size, float height){
SDL_Surface* image = SDL_LoadBMP(filePath);
if (!image)
{
smodgine::fatalError("Image could not be loaded!");
}
std::vector<float> tmp;
for (int y = 0; y< image->h; y++)
{
tmp.clear();
for (int x = 0; x < image->w; x++)
{
Uint32 pixel = ((Uint32*)image->pixels)[y * image->pitch/4 + x];
Uint8 r, g, b;
SDL_GetRGB(pixel, image->format, &r, &g, &b);
tmp.push_back((float)r / 255.0f);
}
heights.push_back(tmp);
}
for (int z = 0; z < heights.size(); z++)
{
for (int x = 0; x < heights[0].size(); x++)
{
vertices.push_back(x * size);
vertices.push_back(heights[z][x] * height);
vertices.push_back(z * size);
}
}
if (_vbo == 0) {
glGenBuffers(1, &_vbo);
}
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
_numRows = heights.size() - 1;
_numColumns = heights[0].size() - 1;
SDL_FreeSurface(image);
createElementBuffer();
}
void HeightMap::renderHeightMap()
{
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _eleBuffer);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, 0);
}
void HeightMap::createElementBuffer()
{
for (int z = 0; z < _numRows; z++)
{
for (int x = 0; x < _numColumns; x++)
{
//top left
indices.push_back(z * _numRows + x);
//bottom left
indices.push_back((z + 1) * _numRows + x);
//top right
indices.push_back(z * _numRows + (x + 1));
//top right
indices.push_back(z * _numRows + (x + 1));
//bottom left
indices.push_back((z + 1) * _numRows + x);
//bottom right
indices.push_back((z + 1) * _numRows + (x + 1));
}
}
if (_eleBuffer == 0){
glGenBuffers(1, &_eleBuffer);
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _eleBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
我只使用现代OpenGL大约一周左右,任何帮助都值得赞赏:D
你对 std::vector
的实例使用 sizeof
运算符,期望它能给你向量中数据的大小:
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices.data(), GL_STATIC_DRAW);
...
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices.data(), GL_STATIC_DRAW);
这不会产生预期的结果。 sizeof
为您提供vector
对象在内存中的大小,而不是矢量中存储的数据的大小。该数据是动态分配的,而不是存储在vector
实例本身中。
要获得正确的大小,您可以使用:
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(vertices[0]), vertices.data(), GL_STATIC_DRAW);
...
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(indices[0]), indices.data(), GL_STATIC_DRAW);
我想
通了,除了 Reto Koradi 帮助我解决的错误之外,在我的代码的另一部分(在主函数期间)我不小心调用了 glEnableVertexAttribArray(1)
相关文章:
- glDrawElements GL 错误类型 = 0x824c,严重性 = 0x9146,消息 = GL_INVALID
- 使用 glDrawElements 绘制一个 std::vector
- GLSL C++ glVertexAttribPointer & glDrawElements return GL_INVALID_OPERATION
- glDrawElements成功,但glDrawElementsInstance失败并GL_INVALID_OPERAT
- 将VAO与glDrawElements一起使用
- glDrawElements 具有每个刻面颜色
- glDrawElements() 不起作用
- iOS GL ES exc_bad_access on glDrawElements
- OSX上的Gldrawelements具有很高的CPU使用情况
- 无法让 openGL 的 glDrawElements 与几何着色器一起使用
- 调用glDrawElements时出现Segfault
- glDrawElements not drawing
- glDrawElements 在驱动程序中崩溃 | 调试提示
- 在gldrawelements中,索引参数的含义是什么
- glDrawElements只会导致与GL_PATCHES的访问冲突
- glDrawElements中的第四个论点是WHAT
- glDrawElements 失败并出现错误GL_INVALID_OPERATION
- gldrawelements仅画一半四分之一
- OpenGL and GL_Blend with glDrawElements
- 如何在使用glDrawElements时将法线传递到GLSL中的顶点着色器