我将代码从DisplayLists更改为VBO / VAO。 但是当我运行应用程序时它会崩溃。
在第一次尝试绘制VAO时崩溃。我正在使用不使用着色器(因此它不会导致问题)。
一个VAO中最多有12个面(12 * 3个顶点),纹理坐标最多。有多达500 000个VAO。
我如何创造一个面孔:
tData.add(new float[]{textureX + 0.1249f, textureY+ 0.1249f});
vData.add(new float[]{x, y, z});
tData.add(new float[]{textureX+ 0.1249f, textureY+0.0001f});
vData.add(new float[]{x, y+1, z+1});
tData.add(new float[]{textureX+0.0001f, textureY+0.0001f});
vData.add(new float[]{x+1, y+1, z+1});
创建VBO / VAO:
if(vData.isEmpty())
return;
int vaoHandle = glGenVertexArrays();
glBindVertexArray(vaoHandle);
int vertexDataSize = vData.size() * 3;
int textureDataSize = tData.size() * 2;
FloatBuffer vertexData = BufferUtils.createFloatBuffer(vData.size() * 3);
FloatBuffer textureData = BufferUtils.createFloatBuffer(tData.size() * 2);
while(!vData.isEmpty())
{
vertexData.put(vData.remove(0));
}
while(!tData.isEmpty())
{
textureData.put(tData.remove(0));
}
vertexData.flip();
textureData.flip();
int vertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glVertexAttribPointer(0, vertexDataSize, GL_FLOAT, false, 0, 0);
int textureHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, textureHandle);
glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
glVertexAttribPointer(1, textureDataSize, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
renderEngine.vaos.add(new VAO(vaoHandle, (int)((float)vertexDataSize / 3f)));
渲染VBO / VAO:
glUseProgram(0);//TODO:REMOVE
for(VAO vao : vaos)
{
glBindVertexArray(vao.getHandle());
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
System.out.println(vao.getVertices());//correct numeber
glDrawArrays(GL_TRIANGLES, 0, vao.getVertices());//<-- CRASH at first time called
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
}
glBindVertexArray(0);
这是错误:
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000000b3fb610, pid=7736, tid=6224
#
# JRE version: Java(TM) SE Runtime Environment (8.0_20-b26) (build 1.8.0_20-b26)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.20-b23 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [ig75icd64.dll+0x8b610]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\Geosearchef\workspaces\workspaceLWJGL\OrangeJuiceVBO\hs_err_pid7736.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
我不认为在这里发布整个错误是有意义的。 你知道为什么会这样吗?我找不到任何有关维也纳国际组织的错误。
答案 0 :(得分:0)
您没有正确设置顶点属性指针:
int vertexDataSize = vData.size() * 3;
int textureDataSize = tData.size() * 2;
[...]
glVertexAttribPointer(0, vertexDataSize, GL_FLOAT, false, 0, 0);
[...]
glVertexAttribPointer(1, textureDataSize, GL_FLOAT, false, 0, 0);
size
参数定义每个顶点向量中的元素数,且必须在1到4的范围内。
你的代码只会产生GL错误 - 你应该明确地添加一些错误检查,至少是为了调试 - 并使属性指针保持未初始化。
此处的另一个问题是:您使用通用顶点属性0和1,但不使用着色器。那不行。规范只保证属性索引0将映射到经典glVertex
属性,但属性1可能是任何东西,或根本不起作用。