java OpenGL构造形状的困难
我正在为我的3D引擎开发一个简单的形状批处理系统,它可以让我画直线和矩形等等。。。以较低的抽签次数。我想我已经大致了解了基本的想法,但是当我尝试绘制多个对象时,我遇到了一些问题(目前只绘制了一些可以指定厚度的线)
我使用带glDrawElements
的索引渲染和两个VBO来表示顶点数据——一个用于位置,一个用于颜色。
我通过指定起点和终点为我的形状批处理程序构造一条线,如下所示:
shapeRenderer.begin();
shapeRenderer.setViewMatrix(viewMatrix);
shapeRenderer.setProjectionMatrix(projectionMatrix);
shapeRenderer.setCurrentColour(0, 1f, 0);
shapeRenderer.drawLine(2, 2, 5, 2);
shapeRenderer.setCurrentColour(0, 1f, 1f);
shapeRenderer.drawLine(2, 5, 5, 5);
shapeRenderer.end();
第一行在截图中用绿色表示,显示得非常完美。如果我只画一条线,就完全可以了。如果我只画第二条线,它也会完美地显示出来
当我调用drawLine
时,执行以下代码,我使用这些代码计算方向和法线:
private Vector2f temp2fA = new Vector2f();
private Vector2f temp2fB = new Vector2f();
private Vector2f temp2fDir = new Vector2f();
private Vector2f temp2fNrm = new Vector2f();
private Vector2f temp2fTMP = new Vector2f();
private boolean flip = false;
public void drawLine(float xStart, float yStart, float xEnd, float yEnd){
resetLineStates();
temp2fA.set(xStart, yStart);
temp2fB.set(xEnd, yEnd);
v2fDirection(temp2fA, temp2fB, temp2fDir);
v2fNormal(temp2fDir, temp2fNrm);
float halfThickness = currentLineThickness / 2;
//System.out.println("new line called");
v2fScaleAndAdd(temp2fB, temp2fNrm, -halfThickness, temp2fTMP);
pushVertex(temp2fTMP);
v2fScaleAndAdd(temp2fB, temp2fNrm, halfThickness, temp2fTMP);
pushVertex(temp2fTMP);
v2fScaleAndAdd(temp2fA, temp2fNrm, halfThickness, temp2fTMP);
pushVertex(temp2fTMP);
v2fScaleAndAdd(temp2fA, temp2fNrm, -halfThickness, temp2fTMP);
pushVertex(temp2fTMP);
//System.out.println(indexCount + " before rendering.");
int index = indexCount;
pushIndices(index, index + 1, index + 3);
pushIndices(index + 1, index + 2, index + 3);
//System.out.println(indexCount + " after rendering.");
}
private void resetLineStates(){
temp2fA.set(0);
temp2fB.set(0);
temp2fDir.set(0);
temp2fNrm.set(0);
temp2fTMP.set(0);
}
pushIndices
是以下函数:
private void pushIndices(int i1, int i2, int i3){
shapeIndices.add(i1);
shapeIndices.add(i2);
shapeIndices.add(i3);
indexCount += 3;
}
而且pushVertex
的工作原理是这样的:
private void pushVertex(float x, float y, float z){
shapeVertexData[vertexDataOffset] = x;
shapeColourData[vertexDataOffset] = currentShapeColour.x;
shapeVertexData[vertexDataOffset + 1] = y;
shapeColourData[vertexDataOffset + 1] = currentShapeColour.y;
shapeVertexData[vertexDataOffset + 2] = z;
shapeColourData[vertexDataOffset + 2] = currentShapeColour.z;
//System.out.println("\tpushed vertex: " + data.x + ", " + data.y + ", 0");
vertexDataOffset += 3;
}
我使用以下字段来存储顶点数据,当我刷新批处理时,这些数据都被子缓冲到VBO中。如果顶点数据数组的大小不必增长,我会将它们分缓冲到各自的VBO中,与元素缓冲区类似,否则如果它们必须增长,我会重新缓冲VBO以适应
private float[] shapeVertexData;
private float[] shapeColourData;
private int vertexDataOffset;
private ArrayList<Integer> shapeIndices;
private int indexCount;
当我在IDEA中使用调试器时,顶点数据在我正在构建的数组中看起来完全正确,但当我在RenderDoc中探索它时,它是错误的。我不明白为了得到这些结果我做错了什么,很明显,前两个顶点即使对于第二个矩形也是完全正确的,但是其他的都是完全错误的。
我相信我的着色器不是问题所在,因为它们非常简单,但它们是:
shape_render.vs
(顶点着色器):
#version 330
layout (location = 0) in vec3 aPosition;
layout (location = 1) in vec3 aColour;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
flat out vec3 shapeFill;
void main(){
shapeFill = aColour;
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(aPosition.x, aPosition.y, 0.0, 1.0);
}
shape_render.fs
(片段着色器):
#version 330
layout (location = 0) out vec4 fragColour;
in vec3 shapeFill;
void main(){
fragColour = vec4(shapeFill, 1);
}
我想我已经尽我所知解释过了,如果有任何见解,我将不胜感激。我已经检查并确定我正在启用必要的顶点数组,等等。。。并呈现正确数量的索引(12):
非常感谢你帮我看这个
共 (0) 个答案