简体   繁体   中英

Processing (Java/Android) Trying to draw shape using for loop

I am trying to draw some triangles in 3D using Processing for Android. When I hard code the triangles (say, a sample of 10 triangles), they are drawn perfectly. However, when I try to draw those same triangles using a for loop, only one triangle is drawn. ( I am trying to draw 100 connected triangles, so hardcoding is not a really good solution)

Here is my for loop within the draw() function:

    for(int j=0;j<triangles.size();j++){
        beginShape(TRIANGLES);
    vertex(triangles.get(j)[0],triangles.get(j)[1],triangles.get(j)[2]); //triangle pt 1
    vertex(triangles.get(j)[3],triangles.get(j)[4],triangles.get(j)[5]); //triangle pt 2
    vertex(triangles.get(j)[6],triangles.get(j)[7],triangles.get(j)[8]); //triangle pt 3
    endShape(CLOSE);
    }

triangles is an ArrayList of float[9], where float[0-2] represents the x,y and z coordinates of the first point of the first triangle, float[3-5] represents the x,y and z coords of the second point of the first triangle and so on.

Visually it can be represented as:

ArrayList{
Triangle1.firstpoint.x(),Triangle1.firstpoint.y(),Triangle1.firstpoint.z(), Triangle1.secondpoint.x()...Triangle1.thirdpoint.x()...
Triangle2.firstpoint.x()...
and so on...
}

j represents the triangle number. (first tri, second tri etc)

When I try to hardcode 10 triangles, however, it works perfectly.

Hardcoding like this works perfectly:

    vertex((float) 12.0,(float)123.6016745697409,(float)13.154318796014476);
    vertex( (float)0.0,(float)197.68424385921847,(float)23.124972474732054);
    vertex((float) 20.0,(float)143.86712094263626,(float)44.91428537210794);

    vertex((float) 0.0,(float)197.68424385921847,(float)23.124972474732054);
    vertex( (float)24.0,(float)214.61268970017602,(float)45.76677898884916);
    vertex( (float)32.0,(float)188.10247423402564,(float)28.491849521501333);

    vertex((float) 56.0,(float)88.25218560204328,(float)12.036878675294194);
    vertex((float) 60.0,(float)144.49729617140488,(float)46.869886728176354);
    vertex((float) 76.0,(float)101.60603981125132,(float)7.27525015602069);

    vertex( (float)60.0,(float)144.49729617140488,(float)46.869886728176354);
    vertex( (float)64.0,(float)184.21835869332358,(float)3.9402252833719453);
    vertex( (float)76.0,(float)101.60603981125132,(float)7.27525015602069);

    vertex((float) 4.0,(float)268.3209514804444,(float)18.52950131584495);
    vertex( (float)52.0,(float)349.93516427695124,(float)8.772515956684678);
    vertex( (float)72.0,(float)338.2777711567361,(float)21.563995998820793);

Putting beginShape(TRIANGLES) and endShape() outside of the for loop has no effect. Really puzzled by what's going on here :/

Silly me! It turns out that every triangle in the triangles ArrayList had the same value, thus in effect producing only 1 triangle during the display!

The issue was because in transferring values over to the triangles ArrayList, I did:

            float[] trianglesi=new float[9];
        for (int tt=0; tt<ntri; tt++)
    {

        trianglesi[0]=(float) points[triangles[tt].p1].x;
        trianglesi[1]=(float) points[triangles[tt].p1].y;
        trianglesi[2]=(float) points[triangles[tt].p1].z;

        trianglesi[3]=(float) points[triangles[tt].p2].x;
        trianglesi[4]=(float) points[triangles[tt].p2].y;
        trianglesi[5]=(float) points[triangles[tt].p2].z;

        trianglesi[6]=(float) points[triangles[tt].p3].x;
        trianglesi[7]=(float) points[triangles[tt].p3].y;
        trianglesi[8]=(float) points[triangles[tt].p3].z;

        triangles2.add(trianglesi);
    }

(Here triangles2 refer to the triangles ArrayList, and trianglesi refer to a "unit" triangle.)

However, I had to initiate the trianglesi within the for loop!

Thanks laalto for leading me to relook at my triangles implementation, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM