0

(Question is at bottom)Im learning opengl(using lwjgl) and done some drawing of flat shape through sending buffers. Now I need to draw many spheres in single buffer. In my last question, I was advised to use geometry instancing but I dont know how to use any shader language in java yet so I'm trying to make multiple objects in single buffer just like in the examples.

What I tried to generate two spheres by QUAD_STRIP style(using lwjgl's own GLU.Sphere() function to fill the buffers):

n=c1*(c2+1);
        float rr=(float) Math.random();
        float gg=(float) Math.random();
        float bb=(float) Math.random();
        float aa=(float) Math.random();

        positions = new float[c1 * (c2+1) * 3*2 *2];
        normals = new float[c1 * (c2+1) * 3*2 *2];
        colors = new float[c1 * (c2+1) * 4*2 *2];
        int counter=0;
        float drho = 3.141593F / 32.0f;
        float dtheta = 6.283186F / 32.0f;
        float ds = 1.0F / 32.0f;
        float dt = 1.0F / 32.0f;
        float t = 1.0F;

        /*first sphere*/
        for (int i = 0; i < 32; i++) {
            float rho = i * drho;

            float s = 0.0F;
            for (int j = 0; j <= 32; j++) {
              float theta = j == 32 ? 0.0F : j * dtheta;
              float x = (float) (-Math.sin(theta) * Math.sin(rho));
              float y = (float) (Math.cos(theta) * Math.sin(rho));
              float z = (float) (1.0f * Math.cos(rho));           

              normals[counter*3+0]=x*1.0f;normals[counter*3+1]=y*1.0f;normalscounter*3+2]=z*1.0f;

              colors[counter*4+0]=rr;colors[counter*4+1]=gg;colors[counter*4+2]=bb;colors[counter*4+3]=1.0f/*aa*/;  

              positions[counter*3+0]=x*r;positions[counter*3+1]=y*r;positions[counter*3+2]=z*r;
              counter++;
              x = (float) (-Math.sin(theta) * Math.sin(rho + drho));
              y = (float) (Math.cos(theta) * Math.sin(rho + drho));
              z = (float) (1.0f * Math.cos(rho + drho));

              normals[counter*3+0]=x*1.0f;normals[counter*3+1]=y*1.0f;normals[counter*3+2]=z*1.0f;

              colors[counter*4+0]=rr;colors[counter*4+1]=gg;colors[counter*4+2]=bb;colors[counter*4+3]=1.0f/*aa*/;  

              positions[counter*3+0]=x*r;positions[counter*3+1]=y*r;positions[counter*3+2]=z*r;
              counter++;
              s += ds;
            }

            t -= dt;
          }
/* first sphere end */
/* second sphere generation */
        {
            drho = 3.141593F / 32.0f;
            dtheta = 6.283186F / 32.0f;
            ds = 1.0F / 32.0f;
            dt = 1.0F / 32.0f;
            t = 1.0F;
            for (int i = 0; i < 32; i++) {
                float rho = i * drho;

                float s = 0.0F;
                for (int j = 0; j <= 32; j++) {
                  float theta = j == 32 ? 0.0F : j * dtheta;
                  float x = (float) (-Math.sin(theta) * Math.sin(rho));
                  float y = (float) (Math.cos(theta) * Math.sin(rho));
                  float z = (float) (1.0f * Math.cos(rho));           
                 normals[counter*3+0]=x*1.0f;normals[counter*3+1]=y*1.0f;normals[counter*3+2]=z*1.0f;

                  colors[counter*4+0]=rr;colors[counter*4+1]=gg;colors[counter*4+2]=bb;colors[counter*4+3]=1.0f/*aa*/;  

                  positions[counter*3+0]=x*r+1.0f;positions[counter*3+1]=y*r+1.0f;positions[counter*3+2]=z*r+1.0f;
                  counter++;
                  x = (float) (-Math.sin(theta) * Math.sin(rho + drho));
                  y = (float) (Math.cos(theta) * Math.sin(rho + drho));
                  z = (float) (1.0f * Math.cos(rho + drho));

                  normals[counter*3+0]=x*1.0f;normals[counter*3+1]=y*1.0f;normals[counter*3+2]=z*1.0f;

                  colors[counter*4+0]=rr;colors[counter*4+1]=gg;colors[counter*4+2]=bb;colors[counter*4+3]=1.0f/*aa*/;  

                  positions[counter*3+0]=x*r+1.0f;positions[counter*3+1]=y*r+1.0f;positions[counter*3+2]=z*r+1.0f;
                  counter++;
                  s += ds;
                }

                t -= dt;
              }
        }
        /*second sphere end*/

        positionsBuf=BufferUtils.createFloatBuffer(c1 * (c2+1) * 3*2  *2);
        positionsBuf.put(positions);
        positionsBuf.rewind();
        colorsBuf=BufferUtils.createFloatBuffer(c1 * (c2+1) * 4*2  *2);
        colorsBuf.put(colors);
        colorsBuf.rewind();
        normalsBuf=BufferUtils.createFloatBuffer(c1 * (c2+1) * 3*2  *2);
        normalsBuf.put(normals);
        normalsBuf.rewind();

As you can see, below image shows how two spheres are drawn. There is an unwanted link between two.

here

Most probably the rope is caused by the last point of first sphere and first point of second sphere. Is there some kind of delimiter/drawing-hint to separate two drawings in the same buffer?

Here is how they are drawn:

         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,sphereBufferCol.get(0));
         GL11.glColorPointer(4, GL11.GL_FLOAT, 0, 0);
         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, sphereBufferPos.get(0));
         GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, sphereBufferNormal.get(0));
         GL11.glNormalPointer(GL11.GL_FLOAT, 0, 0);



         GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
         GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
         GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);

         //Each sphere is generated 32 by 32 quadstriparray and each having two sets of two points and there are two spheres 
         GL11.glDrawArrays(GL11.GL_QUAD_STRIP, 0, 32*33*2 *2);



         GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
         GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
         GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);

         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

Question: How can I make that rope-like thing disappear without decreasing performance? Maybe putting zero to last and first points' alpha value can make it invisible but wouldnt that cause two holes on each sphere and decrease performance because of many lines on the screen?

All vertex values will be altered by opencl interoperability so single drawing call is needed to draw whole 10000+ spheres.

4

1 回答 1

4

似乎有很多选择:

如果您使用较新的硬件并想使用四边条,我更喜欢使用原始重启。

请注意,这只是快速评估和检查的结果(我个人不经常使用四边带甚至三边带;))。

于 2013-08-26T17:14:37.443 回答