glDrawElements trouble


package GeomUtils;

import org.lwjgl.opengl.*;
import java.nio.*;

import Math.*;

public class Torus{

  private static final int     SLICE   = 64,
                               STACK   = 64;

  private static float    OUTER_RADIUS =  1.0f,
                          INNER_RADIUS =  0.5f;

  private static int         numberOfIndices = 0,
                             numberElements;

  private static String      name       = "";

  private static IntBuffer   indices  = null;

  private static FloatBuffer vertexData = null,
                             normalData = null;

  static{
    Tuple3f torusVertices[] = new Tuple3f[(SLICE + 1)],
            torusNormals [] = new Tuple3f[(SLICE + 1)],
            temporary       = new Tuple3f();

    float  x          = 0,
           z          = 0,
           sliceStep  = 2f*FastTrig.PI/SLICE,
           stackStep  = 2f*FastTrig.PI/STACK,
           sliceAngle = 0,
           stackAngle = 0;


    vertexData   = createFloatBuffer((SLICE+1)*(STACK+1));

    normalData   = createFloatBuffer((SLICE+1)*(STACK+1));

numberOfIndices = SLICE*STACK*6;
    indices      = createIntBuffer(numberOfIndices);

    for(int i = 0; i<(SLICE + 1); i++){
      if(i == SLICE )
        sliceAngle = 0;

      x = FastTrig.sin(sliceAngle);
      z = FastTrig.cos(sliceAngle);

      torusVertices[i] = new Tuple3f(OUTER_RADIUS - INNER_RADIUS*x,
                                                 0               ,
                                            INNER_RADIUS*z       );
      vertexData.put(torusVertices[i].x);
      vertexData.put(torusVertices[i].y);
      vertexData.put(torusVertices[i].z);

      torusNormals[i]  = new Tuple3f(-x,0,z);

      normalData.put(-x);
      normalData.put( 0);
      normalData.put( z);

      sliceAngle += sliceStep;
    }


    for(int a = 1, p = SLICE + 1; a < STACK + 1; a++){
      stackAngle += stackStep;
      if(a == STACK)
        stackAngle = 0;

      for(int i = 0; i< SLICE +1 ; i++, p++){

        rotateTupleZ(temporary, torusVertices[i], stackAngle);
        vertexData.put(temporary.x);
        vertexData.put(temporary.y);
        vertexData.put(temporary.z);

        rotateTupleZ(temporary, torusNormals[i] , stackAngle);
        normalData.put(temporary.x);
        normalData.put(temporary.y);
        normalData.put(temporary.z);
      }
    }

    for(int j=0; j<STACK; j++)
      for(int i=0; i<SLICE; i++){
        indices.put(((j*SLICE+i)*2)*3+0, j*(SLICE+1)+i);
        indices.put(((j*SLICE+i)*2)*3+1, (j+1)*(SLICE+1)+i);
        indices.put(((j*SLICE+i)*2)*3+2, j*(SLICE+1)+i+1);
        indices.put(((j*SLICE+i)*2+1)*3+0, j*(SLICE+1)+i+1);
        indices.put(((j*SLICE+i)*2+1)*3+1, (j+1)*(SLICE+1)+i);
        indices.put(((j*SLICE+i)*2+1)*3+2, (j+1)*(SLICE+1)+i+1);
      }
    indices.flip();
    vertexData.flip();
    normalData.flip();
  }

  public static final void drawTorus(int   textureID,
                                     float rotx, float roty     ,
                                     boolean reflectionMap){

    int generationMode = reflectionMap ? GL13.GL_NORMAL_MAP  : GL13.GL_REFLECTION_MAP;


    GL11.glTexGeni(GL11.GL_S, GL11.GL_TEXTURE_GEN_MODE, generationMode);
    GL11.glTexGeni(GL11.GL_T, GL11.GL_TEXTURE_GEN_MODE, generationMode);
    GL11.glTexGeni(GL11.GL_R, GL11.GL_TEXTURE_GEN_MODE, generationMode);
    GL11.glEnable(GL11.GL_TEXTURE_GEN_S);
    GL11.glEnable(GL11.GL_TEXTURE_GEN_T);
    GL11.glEnable(GL11.GL_TEXTURE_GEN_R);
    GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, textureID);
    GL11.glEnable(GL13.GL_TEXTURE_CUBE_MAP);
    GL11.glColor3f(1.0f,1.0f,10f);
    
    GL11.glPushMatrix();
    GL11.glTranslatef(0.0f, 0.0f, -2.5f);
    GL11.glRotatef(rotx, 1, 0, 0);
    GL11.glRotatef(roty, 0, 1, 0);

    GL11.glVertexPointer(3, GL11.GL_FLOAT, vertexData);
    GL11.glNormalPointer(   GL11.GL_FLOAT, normalData);
    GL11.glDrawElements (GL11.GL_TRIANGLES, indices); 

    GL11.glDisable(GL13.GL_TEXTURE_CUBE_MAP);
    GL11.glDisable(GL11.GL_TEXTURE_GEN_S);
    GL11.glDisable(GL11.GL_TEXTURE_GEN_T);
    GL11.glDisable(GL11.GL_TEXTURE_GEN_R);
    GL11.glPopMatrix();
  }

  private static void rotateTupleZ(Tuple3f t1, Tuple3f t2, float angle){
    float sin = FastTrig.sin(angle),
          cos = FastTrig.cos(angle);

    t1.set( t2.x*cos - t2.y*sin, t2.x*sin + t2.y*cos, t2.z);
  }

  private static FloatBuffer createFloatBuffer(int numElements){
    return ByteBuffer.allocateDirect(4*3*numElements).order(
           ByteOrder.nativeOrder()).asFloatBuffer();
  }

  private static IntBuffer createIntBuffer(int numElements){
    return ByteBuffer.allocateDirect(4*numElements).order(
           ByteOrder.nativeOrder()).asIntBuffer();
  }
}

^
Nothing gets drawn to the screen even though I’m positive all the right coordinates/normals are there (tried breaking it into pieces)

The following code I used in JoGL works perfectly though


package GeomUtils;

import net.java.games.jogl.GL;

import java.nio.FloatBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import Math.*;

public class Torus{

  private static final int     SLICE   = 64,
                               STACK   = 64;

  private static float    OUTER_RADIUS =  1.0f,
                          INNER_RADIUS =  0.5f;

  private static int        indices[]  = null;

  private static FloatBuffer vertexData = null,
                             normalData = null;

  static{
    Tuple3f torusVertices[] = new Tuple3f[(SLICE + 1)],
            torusNormals [] = new Tuple3f[(SLICE + 1)],
            temporary       = new Tuple3f();

    float  x          = 0,
           z          = 0,
           sliceStep  = 2f*FastTrig.PI/SLICE,
           stackStep  = 2f*FastTrig.PI/STACK,
           sliceAngle = 0,
           stackAngle = 0;

    vertexData   = createFloatBuffer((SLICE+1)*(STACK+1));

    normalData   = createFloatBuffer((SLICE+1)*(STACK+1));

    indices      = new int[SLICE*STACK*6];

    for(int i = 0; i<(SLICE + 1); i++){
      if(i == SLICE )
        sliceAngle = 0;

      x = FastTrig.sin(sliceAngle);
      z = FastTrig.cos(sliceAngle);

      torusVertices[i] = new Tuple3f(OUTER_RADIUS - INNER_RADIUS*x,
                                                 0               ,
                                            INNER_RADIUS*z       );
      vertexData.put(torusVertices[i].x);
      vertexData.put(torusVertices[i].y);
      vertexData.put(torusVertices[i].z);

      torusNormals[i]  = new Tuple3f(-x,0,z);

      normalData.put(-x);
      normalData.put( 0);
      normalData.put( z);

      sliceAngle += sliceStep;
    }


    for(int a = 1, p = SLICE + 1; a < STACK + 1; a++){
      stackAngle += stackStep;
      if(a == STACK)
        stackAngle = 0;

      for(int i = 0; i< SLICE +1 ; i++, p++){

        rotateTupleZ(temporary, torusVertices[i], stackAngle);
        vertexData.put(temporary.x);
        vertexData.put(temporary.y);
        vertexData.put(temporary.z);

        rotateTupleZ(temporary, torusNormals[i] , stackAngle);
        normalData.put(temporary.x);
        normalData.put(temporary.y);
        normalData.put(temporary.z);
      }
    }

    for(int j=0; j<STACK; j++)
      for(int i=0; i<SLICE; i++){
        indices[((j*SLICE+i)*2)*3+0]  = j*(SLICE+1)+i;
        indices[((j*SLICE+i)*2)*3+1]  = (j+1)*(SLICE+1)+i;
        indices[((j*SLICE+i)*2)*3+2]  = j*(SLICE+1)+i+1;
        indices[((j*SLICE+i)*2+1)*3+0]= j*(SLICE+1)+i+1;
        indices[((j*SLICE+i)*2+1)*3+1]= (j+1)*(SLICE+1)+i;
        indices[((j*SLICE+i)*2+1)*3+2]= (j+1)*(SLICE+1)+i+1;
      }
  }

  public static final void drawTorus(GL    gl  , int   textureID,
                                     float rotx, float roty     ,
                                     boolean reflectionMap){

    int generationMode = reflectionMap ? GL.GL_NORMAL_MAP_ARB : GL.GL_REFLECTION_MAP_ARB;

    gl.glColor3f(1.0f, 1.0f, 1.0f);
    gl.glTexGeni(GL.GL_S, GL.GL_TEXTURE_GEN_MODE, generationMode);
    gl.glTexGeni(GL.GL_T, GL.GL_TEXTURE_GEN_MODE, generationMode);
    gl.glTexGeni(GL.GL_R, GL.GL_TEXTURE_GEN_MODE, generationMode);
    gl.glEnable(GL.GL_TEXTURE_GEN_S);
    gl.glEnable(GL.GL_TEXTURE_GEN_T);
    gl.glEnable(GL.GL_TEXTURE_GEN_R);
    gl.glBindTexture(GL.GL_TEXTURE_CUBE_MAP_ARB, textureID);
    gl.glEnable(GL.GL_TEXTURE_CUBE_MAP_ARB);

    gl.glPushMatrix();
    gl.glTranslatef(0.0f, 0.0f, -2.5f);

    gl.glRotatef(rotx, 1, 0, 0);
    gl.glRotatef(roty, 0, 1, 0);

    gl.glVertexPointer(3, gl.GL_FLOAT, 0, vertexData);
    gl.glNormalPointer(   gl.GL_FLOAT, 0, normalData);
    gl.glDrawElements (gl.GL_TRIANGLES,indices.length, gl.GL_UNSIGNED_INT, indices);

    gl.glPopMatrix();
    gl.glDisable(GL.GL_TEXTURE_CUBE_MAP_ARB);
    gl.glDisable(GL.GL_TEXTURE_GEN_S);
    gl.glDisable(GL.GL_TEXTURE_GEN_T);
    gl.glDisable(GL.GL_TEXTURE_GEN_R);
  }

  private static void rotateTupleZ(Tuple3f t1, Tuple3f t2, float angle){
    float sin = FastTrig.sin(angle),
          cos = FastTrig.cos(angle);

    t1.set( t2.x*cos - t2.y*sin, t2.x*sin + t2.y*cos, t2.z);
  }

  private static FloatBuffer createFloatBuffer(int numElements){
    return ByteBuffer.allocateDirect(12*numElements).order(
           ByteOrder.nativeOrder()).asFloatBuffer();

  }
}

meh, at work now - will look at it later…

The trouble is in the gl*Pointer calls, take glVertexPointer in LWJGL, which is declared like this:

glVertexPointer(int size, int stride, FloatBuffer buffer)

Note that GL_FLOAT type argument is inferred from the FloatBuffer type, so it has been dropped. Instead, you need to specify the stride (which is 0 in your case).

  • elias

fwiw, I tried to get you demo to work with the data setup you provide and failed as well. When I used my own data however, your display code (with a few modifications) worked fine… You might recheck that static setup block.

here’s what I changed in drawTorus:


    GL.glEnableClientState(GL.GL_VERTEX_ARRAY); //**added
    GL.glVertexPointer(3, 0, vertexData);

    GL.glEnableClientState(GL.GL_NORMAL_ARRAY); //**added
    GL.glNormalPointer(0, normalData);

    GL.glDrawElements (GL.GL_TRIANGLES, indices);

Torus still did not display…
Changed the static init block to look like this: (simple quad)

  static{
        Vector3f[] verts = new Vector3f[4];
        Vector3f[] norms = new Vector3f[4];
        int[] indicesA = new int[6];

        verts[0] = new Vector3f(-1f, 1f, 0);
        verts[1] = new Vector3f(-1f, -1f, 0);
        verts[2] = new Vector3f(1f, -1f, 0);
        verts[3] = new Vector3f(1f, 1f, 0);

        norms[0] = new Vector3f(0,0,1);
        norms[1] = new Vector3f(0,0,1);
        norms[2] = new Vector3f(0,0,1);
        norms[3] = new Vector3f(0,0,1);

        indicesA[0] = 0;
        indicesA[1] = 1;
        indicesA[2] = 2;
        indicesA[3] = 0;
        indicesA[4] = 2;
        indicesA[5] = 3;

        float[] buffer = new float[verts.length * 3];
        vertexData =
            ByteBuffer
                .allocateDirect(4 * buffer.length)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();

        for (int i = 0; i < verts.length; i++) {
            buffer[i * 3] = verts[i].x;
            buffer[i * 3 + 1] = verts[i].y;
            buffer[i * 3 + 2] = verts[i].z;
        }

        vertexData.clear();
        vertexData.put(buffer);
        vertexData.flip();

        buffer = new float[verts.length * 3];
        normalData =
            ByteBuffer
                .allocateDirect(4 * buffer.length)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        for (int i = 0; i < verts.length; i++) {
            buffer[i * 3] = norms[i].x;
            buffer[i * 3 + 1] = norms[i].y;
            buffer[i * 3 + 2] = norms[i].z;
        }

        normalData.clear();
        normalData.put(buffer);
        normalData.flip();

        indices   = createIntBuffer(indicesA.length);
        indices.clear();
        indices.put(indicesA);
        indices.flip();
}

white square is displayed.

Edit: cleaned up format

The reason that worked is because with a single poly, the broken stride parameter is never used :slight_smile: Elias is right, it should be 0, not GL.GL_FLOAT.

Would be nice to use proper enums wouldn’t it? But I fear that would bloat stuff rather a lot :slight_smile:

Cas :slight_smile:

Tired changing the value to 0 and yet nothing jumps on the screen
/me walks away all pissed off
Well I actually I have a midterm, so I’ll check it out later

I didn’t see anywhere where you’d enabled client state for vertex arrays there…

Cas :slight_smile:

You’re adding data to your “indices” buffer with absolute buffer positions. This doesn’t change the buffer’s position(), does it? I suspect the position stays at zero, then your flip() sets the limit to zero as well, so the call to nglDrawElements causes GL to draw nothing - buffer.remaining() is used to automatically determine the number of indices to draw.

Charlie’s right too.

It’s just One Big Pile of Bugs!

Cas :slight_smile:

::slight_smile:

Apart from the client-state problem, all the bugs stem from LWJGL messing with the well-known OpenGL method signatures. So I think this is more of a learning experience than a buggy application. :stuck_out_tongue:

(But more interestingly, how does JOGL get away with not having to enable the correct pointers?)

Methinks LWJGL needs a ‘Converting from C-style OpenGL to LWJGL-style OpenGL’. Buffers seem to continuously trip people up with the same sort of problems. Then again, is the interface actually stable enough yet?

[quote]The reason that worked is because with a single poly, the broken stride parameter is never used :slight_smile: Elias is right, it should be 0, not GL.GL_FLOAT.
[/quote]
Thanks Cas, but if you look at my posting, the display code WAS correctly changed to use 0, and I did activate the client states. Then you’ll notice I write “Torus still did not display…” Not only that, but if I had left it as GL.GL_FLOAT, my code would also have failed since it uses more than one geometric element. (If you read through my code, you’ll notice it is still TRIANGLES, 2 of them.)

Obviously its a data issue and like cfm I am wondering how it works on JOGL. Perhaps JavaCoolDude reorganized his code for this posting and in doing so the data setup was broken?

Soz, didn’t read thru your post renanse :slight_smile:
JOGL works because you explicitly pass in the number of indices and it assumes that they begin at the start of the buffer. In LWJGL you implicitly specify these with position() and limit(). LWJGL is the secure way :slight_smile:

Cas :slight_smile:

[quote]You’re adding data to your “indices” buffer with absolute buffer positions. This doesn’t change the buffer’s position(), does it? I suspect the position stays at zero, then your flip() sets the limit to zero as well, so the call to nglDrawElements causes GL to draw nothing - buffer.remaining() is used to automatically determine the number of indices to draw.
[/quote]
We have a winner :slight_smile:


    for(int j=0; j<STACK; j++)
      for(int i=0; i<SLICE; i++){
        indices.put( j*(SLICE+1)+i);
        indices.put((j+1)*(SLICE+1)+i);
        indices.put( j*(SLICE+1)+i+1);
        indices.put( j*(SLICE+1)+i+1);
        indices.put((j+1)*(SLICE+1)+i);
        indices.put((j+1)*(SLICE+1)+i+1);
      }

http://www.users.xith.org/JavaCoolDude/Pictures/LWJGLModel.jpg

Badass LWJGL 8)
/me will be starting “The JWS thread (LWJGL only)” soon ;D

phworr, looking good sir :smiley:

[quote]We have a winner :slight_smile:
[/quote]
Naturally! ;D

JCD, could you confirm for me that the JOGL version of that code doesn’t need to enable client state for the various pointers? If it really doesn’t need them it sounds like a JOGL bug, but if it’s trying to be intelligent, this needs to be documented somewhere obvious because I can’t see how they can predict what’s needed with 100% accuracy…