0
votes

In opengl (lwjgl for java), using glu.Sphere.Sphere() can make it very easy to generate a sphere but after some tens of spheres with 32x32 resolution, it becomes very slow. Now Im thinking of using buffers to send vertices+colors+normals+indices only once and change them using opencl.

Question: If I take origin as (0,0,0) as center of sphere, can I choose a normal just equal to its vertex?

Example: North pole of sphere is the vertex=(0,1,0) so can its surface-normal be (0,1,0) again? Can we do same thing for all vertices? What about element array order if we can make it optimized? Guessing a triangle vertex can be used by many neighbour triangles. Im a begineer so I need some pointer to useage of efficient drawElementArray function and index array generation.

Note: Im keeping my cpu at 1400MHz so it cant handle much computing on host side and opengl does not accept multi-threaded drawing of spheres.

Following Valentin's suggestion, I opened inside of glu and found this to be drawing element by element. Will use this for buffer generation.

if (this.drawStyle == 100010)
        {
          GL11.glBegin(0);
          if (normals)
            GL11.glNormal3f(0.0F, 0.0F, nsign);
          GL11.glVertex3f(0.0F, 0.0F, radius);
          if (normals)
            GL11.glNormal3f(0.0F, 0.0F, -nsign);
          GL11.glVertex3f(0.0F, 0.0F, -radius);

          for (int i = 1; i < stacks - 1; i++) {
            float rho = i * drho;
            for (int j = 0; j < slices; j++) {
              float theta = j * dtheta;
              float x = cos(theta) * sin(rho);
              float y = sin(theta) * sin(rho);
              float z = cos(rho);
              if (normals)
                GL11.glNormal3f(x * nsign, y * nsign, z * nsign);
              GL11.glVertex3f(x * radius, y * radius, z * radius);
            }
          }
          GL11.glEnd();
1

1 Answers

2
votes

The reason it is getting slower and slower, is because the GLU geometry meshes rendered using GLU uses immediate mode for rendering.

You could download a Java Decompiler and decompile lwjgl_util.jar, then you just follow the package paths to glu.Sphere.Sphere() and then you can lookup the draw() method. Take a look and see how the code works, and then basically create you own method but instead of using glVertex, etc methods. Just add the values to a FloatBuffer, and then create a VBO of the Sphere.

Here is a free Java Decompiler!