带有TriangleStrip的jMonkey Quad无法更改参数

时间:2013-06-04 18:43:36

标签: java graphics jmonkeyengine

我第一次使用jMonkey。 我的目的是创建一个由一个具有不同细分的单个三角形条带组成的四边形。 所以我可以说Subdivision x == 4; Subdivision y == 4; 在这种情况下,有3行三角形和3个柱子。

到目前为止这是有效的,但只要我更改细分,例如x==5y==5 代码崩溃了。我希望有人可以帮助我。

P.S。:对不起我的英语,现在很累啊^^

/ *      *要更改此模板,请选择“工具”|模板      *并在编辑器中打开模板。      * /     包jme3.objekte;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.VertexBuffer;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.util.BufferUtils;

/**
 *
 * @author Ronny
 */
public class Rechteck2 extends SimpleApplication {

    public static void main(String[] args) {
        Rechteck2 app = new Rechteck2();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        Mesh mesh = new Mesh();
        int x_value = 4; //width_verteces
        int y_value = 4; //heigth_verteces
        int heigth = 3; //heigth
        int width = 3; //width
        float deltaX = width/(x_value-1);
        float deltaY = heigth/(y_value-1);
        int currentVertex = 0;

        Vector3f[] vertices = new Vector3f[x_value*y_value];
          for (int y=0; y<y_value; y++){    //x als nummer des aktuellen vertex
              for (int x=0; x<x_value; x++){
                  vertices[currentVertex] = new Vector3f( x*deltaX, y*deltaY, 0);
                  currentVertex++;
              }
          }
        Vector2f[] texCoord = new Vector2f[4];
        texCoord[0] = new Vector2f(0,0);
        texCoord[1] = new Vector2f(1,0);
        texCoord[2] = new Vector2f(0,1);
        texCoord[3] = new Vector2f(1,1);

        final int maxIndex = ((x_value-1)*(y_value-1)*6) + (y_value * 6);
        int[] indexes = new int[maxIndex];         //[(x_value-1)*(y_value-1)*6+(y_value-1)*9];
        int a = 0;
        int b = 1;  
        int c = x_value;       
        boolean direction = true;
        boolean dirchange = false;

        for (int i=0; i<(maxIndex/3); i++){     //((x_value-1)*(y_value-1)*6+((y_value-1)*9)); i++){

            if (direction){

                if (dirchange==true){
                   indexes[i*3] = a;
                   indexes[i*3+1] = a+1;
                   indexes[i*3+2] = c;
                   a = indexes[i*3];
                   b = indexes[i*3+1];
                   c = indexes[i*3+2];
                   dirchange = false;
                }
                else{
                indexes[i*3] = a;
                indexes[i*3+1] = b;
                indexes[i*3+2] = c;
                }
                if(i%2==1){
                    a = b;
                    b = a+1;
                    c = c;
                }
                else{
                    a = c;
                    b = b;
                    c = a+1;
                }
                if (b%x_value==0){
                    i++;
                    indexes[i*3] = a;
                    indexes[i*3+1] = c;
                    indexes[i*3+2] = c;
                    i++;
                    indexes[i*3] = c;
                    indexes[i*3+1] = c;
                    indexes[i*3+2] = c;
                    if ((c+x_value)<= (x_value*y_value-1)){
                        i++;
                        indexes[i*3] = c;
                        indexes[i*3+1] = c;
                        indexes[i*3+2] = c+x_value;
                        a = c;
                        b = c;
                        c = c+x_value;
                    }
                    direction = false;
                    dirchange = true;
                }
            }
            else{
                if(dirchange==true){
                    indexes[i*3] = c;
                    indexes[i*3+1] = c-1;
                    indexes[i*3+2] = a;
                    a = indexes[i*3];
                    b = c-1;
                    c = indexes[i*3+2];
                    dirchange = false;
                }
                else{
                    indexes[i*3] = a;
                    indexes[i*3+1] = b;
                    indexes[i*3+2] = c;
                }
                if(i%2==0){
                    a = b;
                    b = a-1;
                    c = c;
                }
                else{
                    a = c;
                    b = b;
                    c = a-1;
                }
                if (a%x_value==0){
                    i++;
                    indexes[i*3] = c;
                    indexes[i*3+1] = a;
                    indexes[i*3+2] = a;
                    i++;
                    indexes[i*3] = a;
                    indexes[i*3+1] = a;
                    indexes[i*3+2] = a;
                    if ((b+x_value) <= (x_value*y_value-1)){
                        i++;
                        indexes[i*3] = a;
                        indexes[i*3+1] = a;
                        indexes[i*3+2] = a+x_value;
                        a = a;
                        b = a;
                        c = a+x_value;
                    }
                    direction = true;
                    dirchange = true;
                } 
            }
        }
        mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
        mesh.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord)); 
        mesh.setBuffer(Type.Index, 3, BufferUtils.createIntBuffer(indexes));

        Geometry geo = new Geometry("MyMesh", mesh);
        Material mat1 = new Material(assetManager,
            "Common/MatDefs/Misc/Unshaded.j3md");
        mat1.setColor("Color", ColorRGBA.Green);
        mat1.getAdditionalRenderState().setWireframe(true);
        geo.setMaterial(mat1);

        //mesh.setMode(Mesh.Mode.TriangleStrip);
        mesh.updateBound();
        mesh.setStatic();

        Geometry lines = new Geometry("MyMesh", mesh);
        lines.setMaterial(mat1);

        rootNode.attachChild(lines);
    }

}

1 个答案:

答案 0 :(得分:1)

我测试了你的代码。

只需编写此代码int x_value = 9; //width_verteces int y_value = 10; //heigth_verteces int heigth = y_value-1; //heigth int width = x_value-1; //width 工作正常