LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Fool Running on January 03, 2004, 17:22:39

Title: EXCEPTION_ACCESS_VIOLATION with glDrawArrays()
Post by: Fool Running on January 03, 2004, 17:22:39
I have a game engine I wrote using GL4Java and am porting it to lwjgl to see how it is.  When I use the glDrawArrays() I get the EXCEPTION_ACCESS_VIOLATION error thrown.  If anyone knows what might cause this it would be nice :D

Heres the code snipet (sorry its a little messy  :oops: )

       // setup buffers
       ByteBuffer temp = ByteBuffer.allocateDirect(polyCount * vertexCount * 3 * 4);
       temp.order(ByteOrder.nativeOrder());
       vertexBuffer = temp.asFloatBuffer().put(vertexArray);
       vertexBuffer.flip();
       temp = ByteBuffer.allocateDirect(polyCount * vertexCount * 4 * 4);
       temp.order(ByteOrder.nativeOrder());
       colorBuffer = temp.asFloatBuffer().put(colorArray);
       colorBuffer.flip();
       temp = ByteBuffer.allocateDirect(polyCount * vertexCount * 3 * 4);
       temp.order(ByteOrder.nativeOrder());
       normalBuffer = temp.asFloatBuffer().put(normalArray);
       normalBuffer.flip();
       temp = ByteBuffer.allocateDirect(polyCount * vertexCount * 2 * 4);
       temp.order(ByteOrder.nativeOrder());
       texBuffer = temp.asFloatBuffer().put(texArray);
       texBuffer.flip();
       // setup for array drawing
       GL.glVertexPointer(3, GL.GL_FLOAT, vertexBuffer);
       GL.glColorPointer(4, GL.GL_FLOAT, colorBuffer);
       GL.glNormalPointer(GL.GL_FLOAT, normalBuffer);
       GL.glTexCoordPointer(2, GL.GL_FLOAT, texBuffer);

       GL.glDrawArrays(GL.GL_TRIANGLES, 0, polyCount * vertexCount);

The GL.glDrawArrays(...) is called creating a displaylist (if it matters  :roll: )
Title: additional info
Post by: Fool Running on January 03, 2004, 17:27:44
polyCount is the number of polygons to render.
vertexCount is the number of vertexes per polygon.
Title: EXCEPTION_ACCESS_VIOLATION with glDrawArrays()
Post by: elias on January 03, 2004, 18:08:41
Have you enabled the arrays with glEnabledClientState()? And are you sure that the arrays you put() have size = vc*pc*(element size)?

- elias
Title: EXCEPTION_ACCESS_VIOLATION with glDrawArrays()
Post by: Fool Running on January 03, 2004, 20:26:36
QuoteHave you enabled the arrays with glEnabledClientState()? And are you sure that the arrays you put() have size = vc*pc*(element size)?

Yeah, all the client states are initialized and the arrays and the buffers all have the correct sizes.

The exact same code (aside from the Buffers) runs fine in GL4Java so I assume it has something to do with the Buffers (I'm new to the Buffer thing  :lol: ).

Here is the entire method if it might help:

       int vertexCount = getNumOfVertexesPerPolygon();
       float[] vertexArray = new float[polyCount * vertexCount * 3];
       float[] normalArray = new float[polyCount * vertexCount * 3];
       float[] colorArray = new float[polyCount * vertexCount * 4];
       float[] texArray = new float[polyCount * vertexCount * 2];
       vertexPlace = normalPlace = colorPlace = texPlace = 0;
       // add data to arrays
       for(int i = 0; i < polyCount; i++){
           piece[i].addVertexesToArray(vertexArray, vertexPlace);
           piece[i].addColorsToArray(colorArray, colorPlace);
           piece[i].addNormalsToArray(normalArray, normalPlace);
           piece[i].addTexCoordsToArray(texArray, texPlace);
           vertexPlace += vertexCount * 3;
           colorPlace += vertexCount * 4;
           normalPlace += vertexCount * 3;
           texPlace += vertexCount * 2;
       }
       // setup buffers
       ByteBuffer temp = ByteBuffer.allocateDirect(polyCount * vertexCount * 3 * 4);
       temp.order(ByteOrder.nativeOrder());
       vertexBuffer = temp.asFloatBuffer().put(vertexArray);
       vertexBuffer.flip();
       temp = ByteBuffer.allocateDirect(polyCount * vertexCount * 4 * 4);
       temp.order(ByteOrder.nativeOrder());
       colorBuffer = temp.asFloatBuffer().put(colorArray);
       colorBuffer.flip();
       temp = ByteBuffer.allocateDirect(polyCount * vertexCount * 3 * 4);
       temp.order(ByteOrder.nativeOrder());
       normalBuffer = temp.asFloatBuffer().put(normalArray);
       normalBuffer.flip();
       temp = ByteBuffer.allocateDirect(polyCount * vertexCount * 2 * 4);
       temp.order(ByteOrder.nativeOrder());
       texBuffer = temp.asFloatBuffer().put(texArray);
       texBuffer.flip();
       // setup for array drawing
       GL.glVertexPointer(3, GL.GL_FLOAT, vertexBuffer);
       GL.glColorPointer(4, GL.GL_FLOAT, colorBuffer);
       GL.glNormalPointer(GL.GL_FLOAT, normalBuffer);
       GL.glTexCoordPointer(2, GL.GL_FLOAT, texBuffer);

       // create the display list
       id = GL.glGenLists(1);
       GL.glNewList(id, GL.GL_COMPILE);
           if(texture != null && material.getShininess() != 0){
               material.setSelfOnlySpecular();
               GL.glDisable(GL.GL_TEXTURE_2D);
               GL.glDrawArrays(GL.GL_TRIANGLES, 0, polyCount * vertexCount);

               GL.glBlendFunc(GL.GL_ONE, GL.GL_ONE);
               material.setSelfNotSpecular();
               GL.glEnable(GL.GL_TEXTURE_2D);
               texture.bindSelf();
               GL.glDrawArrays(GL.GL_TRIANGLES, 0, polyCount * vertexCount);
               GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
           }else{
               material.setSelf();
               if(texture != null){
                   GL.glEnable(GL.GL_TEXTURE_2D);
                   texture.bindSelf();
               }else{
                   GL.glDisable(GL.GL_TEXTURE_2D);
               }
               GL.glDrawArrays(GL.GL_TRIANGLES, 0, polyCount * vertexCount);
           }
       GL.glEndList();


The code path drops into the else at the bottom  :D
Title: GL4Java code
Post by: Fool Running on January 03, 2004, 20:30:28
Here is the original GL4Java code that runs fine:


       int vertexCount = getNumOfVertexesPerPolygon();
       vertexArray = new float[polyCount * vertexCount * 3];
       normalArray = new float[polyCount * vertexCount * 3];
       colorArray = new float[polyCount * vertexCount * 4];
       texArray = new float[polyCount * vertexCount * 2];
       vertexPlace = normalPlace = colorPlace = texPlace = 0;
       // add data to arrays
       for(int i = 0; i < polyCount; i++){
           piece[i].addVertexesToArray(vertexArray, vertexPlace);
           piece[i].addColorsToArray(colorArray, colorPlace);
           piece[i].addNormalsToArray(normalArray, normalPlace);
           piece[i].addTexCoordsToArray(texArray, texPlace);
           vertexPlace += vertexCount * 3;
           colorPlace += vertexCount * 4;
           normalPlace += vertexCount * 3;
           texPlace += vertexCount * 2;
       }
       // setup for array drawing
       NovaGL.GL.glVertexPointer(3, NovaGL.GL_FLOAT, 0, vertexArray);
       NovaGL.GL.glColorPointer(4, NovaGL.GL_FLOAT, 0, colorArray);
       NovaGL.GL.glNormalPointer(NovaGL.GL_FLOAT, 0, normalArray);
       NovaGL.GL.glTexCoordPointer(2, NovaGL.GL_FLOAT, 0, texArray);
       // create the display list
       id = NovaGL.GL.glGenLists(1);
       NovaGL.GL.glNewList(id, NovaGL.GL_COMPILE);
           if(texture != null && material.getShininess() != 0){
               material.setSelfOnlySpecular();
               NovaGL.GL.glDisable(NovaGL.GL_TEXTURE_2D);
               NovaGL.GL.glDrawArrays(NovaGL.GL_TRIANGLES, 0, polyCount * vertexCount);

               NovaGL.GL.glBlendFunc(NovaGL.GL_ONE, NovaGL.GL_ONE);
               material.setSelfNotSpecular();
               NovaGL.GL.glEnable(NovaGL.GL_TEXTURE_2D);
               texture.bindSelf();
               NovaGL.GL.glDrawArrays(NovaGL.GL_TRIANGLES, 0, polyCount * vertexCount);
               NovaGL.GL.glBlendFunc(NovaGL.GL_SRC_ALPHA, NovaGL.GL_ONE_MINUS_SRC_ALPHA);
           }else{
               material.setSelf();
               if(texture != null){
                   NovaGL.GL.glEnable(NovaGL.GL_TEXTURE_2D);
                   texture.bindSelf();
               }else{
                   NovaGL.GL.glDisable(NovaGL.GL_TEXTURE_2D);
               }
               NovaGL.GL.glDrawArrays(NovaGL.GL_TRIANGLES, 0, polyCount * vertexCount);
           }
       NovaGL.GL.glEndList();
Title: EXCEPTION_ACCESS_VIOLATION with glDrawArrays()
Post by: elias on January 03, 2004, 20:40:07
* deleted obviously wrong advice *

- elias
Title: I got it!!!!!!!!
Post by: Fool Running on January 03, 2004, 21:23:08
Well, I found the problem  :oops:

       GL.glVertexPointer(3, GL.GL_FLOAT, vertexBuffer);
should be
       GL.glVertexPointer(3, 0, vertexBuffer);

The middle param is the stride  :oops:


Well, thanks, guys for your help  :D
Title: EXCEPTION_ACCESS_VIOLATION with glDrawArrays()
Post by: princec on January 04, 2004, 19:55:15
If it wasn't so horribly inefficient from JNI we'd provide typesafe enums for the GL parameters and this kind of problem wouldn't happen.
On the other hand, we might still be able to do it from the Java side without any loss of efficiency.
We would most likely only be able to provide a single GL enum class however so we wouldn't be able to check all the parameters nicely :(

Cas :)