Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OpenGL-构造形状的困难_Java_Graphics_3d_Lwjgl - Fatal编程技术网

Java OpenGL-构造形状的困难

Java OpenGL-构造形状的困难,java,graphics,3d,lwjgl,Java,Graphics,3d,Lwjgl,我开始为我的3D引擎设计一个简单的形状批处理系统,它可以让我画直线和矩形等等。。。以较低的抽签呼叫计数。我想我已经大致了解了基本的想法,但是当我尝试绘制多个对象时(目前仅绘制具有可以指定厚度的线)会遇到问题 下面是一个屏幕截图,向您展示我的意思: 我正在使用带有glpaurements的索引渲染,以及两个VBO来表示顶点数据—一个用于位置,一个用于颜色。 我通过指定起点和终点为形状批处理程序构造一条线,如下所示: shapeRenderer.begin(); shapeRende

我开始为我的3D引擎设计一个简单的形状批处理系统,它可以让我画直线和矩形等等。。。以较低的抽签呼叫计数。我想我已经大致了解了基本的想法,但是当我尝试绘制多个对象时(目前仅绘制具有可以指定厚度的线)会遇到问题

下面是一个屏幕截图,向您展示我的意思:

我正在使用带有
glpaurements
的索引渲染,以及两个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);
}
pushindex
是以下功能:

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;
shape\u render.fs
(片段着色器):

我想我已经尽我所知解释过了,任何见解都将不胜感激。我已经检查并确定我正在启用必要的顶点数组,等等。。。并呈现正确数量的索引(12):


非常感谢你帮我看了这张照片。

我想了很久才明白。这与我如何指定索引有关。我使用了正确数量的索引,但是没有正确指定它们。 为了构造三角形,第一个三角形的索引计数为0,如果有四个顶点,则索引为1,2,3和2,3,1(例如)。然而,对于每个新三角形,我从旧计数加上6开始索引计数,这对于寻址数组是有意义的,但是因为每个矩形只指定四个顶点,所以我将索引指向不存在的数据。 因此,我将获取当前的顶点计数,并从中构建索引,而不是每次按索引时使用indexCount+=3

#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);
}
#version 330
layout (location = 0) out vec4 fragColour;

in vec3 shapeFill;

void main(){
    fragColour = vec4(shapeFill, 1);
}