【发布时间】:2020-08-09 18:12:34
【问题描述】:
我昨天整天都被这个问题困扰,无法弄清楚。代码如下,但通常我试图为 1.Postions 2.Indices 3.Normals 和 4.a 单个浮点值提供网格顶点属性。
这些值都存储在不同的 VBO 中,在绑定每个 vbo 后,我声明了vertexAttribPointer。我不能让法线和浮点值都工作。我看到的似乎浮点值的位置是前一个 vbo 中法线vec3 的 x y 或 z 部分。
GL4 gl = GLContext.getCurrentGL().getGL4();
int[] vaoids = new int[1];
gl.glGenVertexArrays(1,vaoids,0);
int[] vboids = new int[4];
gl.glGenBuffers(4,vboids,0);
gl.glBindVertexArray(vaoids[0]);
FloatBuffer verticesBuffer = FloatBuffer.allocate(mesh.vertices.length);
verticesBuffer.put(mesh.vertices);
verticesBuffer.flip();
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vboids[0]);
gl.glBufferData(gl.GL_ARRAY_BUFFER, mesh.vertices.length * 4 ,verticesBuffer,gl.GL_STATIC_DRAW);
gl.glEnableVertexAttribArray(0);
gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, false, 0, 0);
verticesBuffer.clear();
verticesBuffer = null;
//normal buffer
FloatBuffer normalBuffer = FloatBuffer.allocate(mesh.normals.length);
normalBuffer.put(mesh.normals);
normalBuffer.flip();
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vboids[2]);
gl.glBufferData(gl.GL_ARRAY_BUFFER, mesh.normals.length * 4 ,normalBuffer,gl.GL_STATIC_DRAW);
gl.glEnableVertexAttribArray(2);
gl.glVertexAttribPointer(2, 3, gl.GL_FLOAT, false, 0, 0);
normalBuffer.clear();
normalBuffer = null;
//color buffer
float[] colors = new float[mesh.vertices.length/3];
Arrays.fill(colors,255.0f);
FloatBuffer colorBuffer = FloatBuffer.allocate(colors.length);
colorBuffer.put(colors);
colorBuffer.flip();
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, vboids[3]);
gl.glBufferData(gl.GL_ARRAY_BUFFER, colors.length * 4 ,colorBuffer,gl.GL_STATIC_DRAW);
gl.glEnableVertexAttribArray(3);
gl.glVertexAttribPointer(3, 1, gl.GL_FLOAT,false, 0, 0);
colorBuffer.clear();
colorBuffer = null;
IntBuffer indicesBuffer = IntBuffer.allocate(mesh.indices.length);
indicesBuffer.put(mesh.indices);
indicesBuffer.flip();
gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, vboids[1]);
gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, mesh.indices.length * 4 ,indicesBuffer,gl.GL_STATIC_DRAW);
gl.glEnableVertexAttribArray(1);
gl.glVertexAttribPointer(1, mesh.type.equals(MeshType.TRIANGLE) ? 3 : mesh.type.equals(MeshType.LINE) ? 2 : mesh.type.equals(MeshType.POINT) ? 1:0, gl.GL_UNSIGNED_INT, false, 0, 0);
indicesBuffer.clear();
indicesBuffer = null;
//gl.glBindBuffer(gl.GL_ARRAY_BUFFER,0);
gl.glBindVertexArray(0);
这是声明 vao 和 vbos 的代码。我使用glDrawElements 渲染并在此之前启用所需的 VertexAttributeArray 索引。在我的着色器中,我访问的值如下:
layout (location=0) in vec3 position;
layout (location=2) in vec3 normal;
layout (location=3) in float color;
out vec3 normals;
out vec4 positionWorldSpace;
out flat float vertexColor;
还有片段着色器
in flat float color;
我可以让它们分别工作,但如果我声明它们的浮点值不再正确。然而,正常情况似乎是正确的。正如我所说,浮动中的值似乎是法线中的值。从普通 vbo 到浮动 vbo 会有某种溢出吗?经过数小时查看代码后,我无法发现错误。
【问题讨论】:
-
很抱歉,它是 Java。应该澄清
-
索引不是属性。索引缓冲区
GL_ELEMENT_ARRAY_BUFFER直接在 VAO 中声明。 -
我删除了对索引后指针的调用,并将剩余的属性移动到位置 0 1 2。但同样的问题仍然存在。
-
理想情况下,我希望它是 short 类型,但现在因为我无法让它工作,我使用 float 来缩小错误的来源。基本上它是一个索引,后来告诉我网格的一部分是由什么材料制成的。整个网格是带有行进立方体算法的三角点云。
-
属性不能使用
short,但可以使用int。对于整体属性,您必须使用glVertexAttribIPointer(关注中间的I)。无论如何,我在您的代码中看不到任何明显的问题。
标签: java opengl vbo vao vertex-array-object