vertex array

Started by ug02070, November 18, 2004, 18:58:07

Previous topic - Next topic

ug02070

hi, i've been away and i've finished my raw loader and i got several
aliens to load and move around but i had poor performance problems and
after reading, i realize that using vertex arrays is a good way of
achieving speedup. i'm having problems getting anything to work now,
could you please take a look at the following code:-

private void loadAlien(){
      numAliens++;
      for (int x = 0;x<coords.size();x+=9)
      {
         for (x = 0;x<coords.size();x+=9) {
            float x1 = Float.parseFloat("" + coords.elementAt(x));
            float y1 = Float.parseFloat("" + coords.elementAt(x+1));
            float z1 = Float.parseFloat("" + coords.elementAt(x+2));

            float x2 = Float.parseFloat("" + coords.elementAt(x+3));
            float y2 = Float.parseFloat("" + coords.elementAt(x+4));
            float z2 = Float.parseFloat("" + coords.elementAt(x+5));

            float x3 = Float.parseFloat("" + coords.elementAt(x+6));
            float y3 = Float.parseFloat("" + coords.elementAt(x+7));
            float z3 = Float.parseFloat("" + coords.elementAt(x+8));
                        
            GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
            float vertices[] = {x1, y1, z1,
                                 x2, y2, z2,
                                 x3, y3, z3};
            FloatBuffer pointer = (new BufferUtils()).createFloatBuffer(coords.size());
            pointer.put(vertices);   
            pointer.flip();               
            GL11.glVertexPointer(3,0, pointer);
         
         //  GL11.glVertex3f(x1-(numAliens*4), y1, z1);
         //   GL11.glVertex3f(x2-(numAliens*4), y2, z2);
         //   GL11.glVertex3f(x3-(numAliens*4), y3, z3);
            
         }      
      }
   }
   

and tell me why i'm getting the following errors when i'm running my code:-


Exception in thread "main" java.lang.OutOfMemoryError
       at java.nio.Bits.reserveMemory(Bits.java:618)
       at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:95)
       at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:285)
       at org.lwjgl.BufferUtils.createByteBuffer(Unknown Source)
       at org.lwjgl.BufferUtils.createFloatBuffer(Unknown Source)
       at OpenGLWindow.loadAlien(OpenGLWindow.java:201)
       at OpenGLWindow.loadAliens(OpenGLWindow.java:240)
       at OpenGLWindow.render(OpenGLWindow.java:118)
       at OpenGLWindow.run(OpenGLWindow.java:49)
       at OpenGLWindow.main(OpenGLWindow.java:40)
Press any key to continue...

hope no-one minds me asking for help, promise that i have been at this for hours with no success
thanks guys

Fool Running

Hi Ug

One thing to realize is that OpenGL is meant to be fast and usually works best when sending as much data to the video card as possible in one call.  So what you need to do is create one array with all the vertexes for the entire alien in it.

looks something like this:
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

float[] floatCoords = new float[coords.size()];
for (x = 0;x<coords.size();x++) {
    floatCoords[x] = Float.parseFloat("" + coords.elementAt(x));
}

FloatBuffer pointer = BufferUtils.createFloatBuffer(coords.size());
pointer.put(floatCoords);
pointer.flip();
GL11.glVertexPointer(3,0, pointer);


You would only have to do this once if you stored pointer in a member variable and just called *whatever the name of the function is* to render it for each alien (I don't have a reference with me here.  I'll look it up later) :oops:

Using this approach you could also store the alien into a display list to get even more speed.  Then its only one line of code to call the list to display the entire alien. 8)

P.S. having the vertexes stored as strings is not very effecient.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Fool Running

To finish what I was saying (now that I have my game engine in front of me  :lol: ) the call to draw the alien would be something like:
GL11.glVertexPointer(3,0, pointer);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, coords.size());


Hope this helps.

P.S. you can also create arrays for your normals, colors, tex coords, etc.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D