Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ? Vertex Arrays - Index Out of Bounds issue ?  (Read 1898 times)
0 Members and 1 Guest are viewing this topic.
Offline Z-Knight

Senior Member


Medals: 1



« Posted 2011-05-10 18:10:13 »

When I try to render my model using Vertex Arrays I get the following exception

1  
2  
3  
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Required 1 remaining elements in buffer, only had 0
        at com.sun.gluegen.runtime.BufferFactory.rangeCheck(BufferFactory.java:247)
        at com.sun.opengl.impl.GLImpl.glNormalPointer(GLImpl.java:14963)


The exception points to this line of code...in my rendering method.

1  
        gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices[i]);


The weird things is that I only get this exception for one of my models, while another is perfectly fine. I can't figure out the problem.

Here is how I create the Vertex Arrays:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
private void createVertexArray() {
  vertices = new FloatBuffer[objects.size()];

  for (int i = 0; i < objects.size(); i++) {
    Obj tempObj = objects.get(i);

    // Fill a buffer with vertex coordinates corresponding to 3 times the number of faces.
   int totalBuffer = tempObj.numOfFaces * 3 * 3;
    vertices[i] = ByteBuffer.allocateDirect(totalBuffer * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();

    for (int j = 0; j < tempObj.numOfFaces; j++) {
      for (int whichVertex = 0; whichVertex < 3; whichVertex++) {
        int index = tempObj.faces[j].vertIndex[whichVertex];
        vertices[i].put(tempObj.verts[index].getX());
        vertices[i].put(tempObj.verts[index].getY());
        vertices[i].put(tempObj.verts[index].getZ());
      }
    }
    vertices[i].rewind();
  }
}


And during rendering I do the following:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
private void render(GL gl) {
  gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
  for (int i = 0; i < objects.size(); i++) {
    Obj tempObj = objects.get(i);
    int numFaceIndices = tempObj.numOfFaces * 3;
    gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices[i]);              // <===== IndexOutOfBoundsException here
   gl.glDrawArrays(GL.GL_TRIANGLES, 0, numFaceIndices);
  }
  gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
}


The only thing I could figure is that somehow my vertices floatbuffers are not being properly created - ie they are missing data? I have no clue why that would happen for one model and not the other. As a I said, one model works perfectly fine but a different one doesn't. I even stripped the code down to just the snippet above and the one model that works fine draws the faces as expected - I can see them, though they are all one color. On the other hand, the "bad" model gives me the exception: "IndexOutOfBoundsException: Required 1 remaining elements in buffer, only had 0"

What does that exception even mean?

Any thoughts?
Offline Z-Knight

Senior Member


Medals: 1



« Reply #1 - Posted 2011-05-10 18:19:49 »

Let me add one more thing...

I use the same 'createVertexArray()' to create the vertices FloatBuffer which I then use in rendering in VBO mode (as opposed to Vertex Array mode). And I have NO PROBLEMS rendering the VBOs. So it is weird that the buffers were created the same way and they work for VBOs but only sometimes for the Vertex Array....using the exact same 3D model.

I render it this way:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
private void createVBO(GL gl) {
    vbo = new int[objects.size()];

    // Generate the vertex buffer objects for the model
   gl.glGenBuffers(objects.size(), vbo, 0);

    // Bind the vertex buffers
   for (int i = 0; i < objects.size(); i++) {
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[i]);
      Obj tempObj = objects.get(i);
      int totalBuffer = tempObj.numOfFaces * 3 * 3;
      gl.glBufferData(GL.GL_ARRAY_BUFFER, totalBuffer * 4, vertices[i], GL.GL_STATIC_DRAW);
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
    }
}

private void renderVBO(GL gl) {
  gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
  for (int i = 0; i < objects.size(); i++) {
    Obj tempObj = objects.get(i);
    int numFaceIndices = tempObj.numOfFaces * 3;
    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[i]);                // <=== Bind the buffer
   gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);                      // <=== VertexPointer offset is 0 (zero)
   gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);                      // <=== Unbind the buffer
   gl.glDrawArrays(GL.GL_TRIANGLES, 0, numFaceIndices);
  }
  gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
}


The VBO rendering method is not much different from the Vertex Array rendering method in the previous post...only the binding is required and the VertexPointer has a zero offset.
Offline gouessej
« Reply #2 - Posted 2011-05-10 20:00:21 »

One of your direct NIO buffers is empty. What is strange is that it is supported with VBO but not with vertex arrays.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Z-Knight

Senior Member


Medals: 1



« Reply #3 - Posted 2011-05-10 20:02:46 »

A previous thread suggestions didn't work either.

Ken Russel suggested in http://www.java-gaming.org/index.php?;topic=12412.0 to perform a buffer rewind(). I already had a vertices.rewind() in the array creation method, and I even tried to add it to the render method but it had no benefit.

Further I even did:
1  
gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices[i].position(0));


without success. I'm doing this on Windows XP...it works fine for most of my 3DS models, but not for one. I can read that "bad" model just fine and can render it both in immediate mode and in VBO mode without any problems...but in this Vertex Array issue it failed.
Offline Z-Knight

Senior Member


Medals: 1



« Reply #4 - Posted 2011-05-10 20:03:43 »

One of your direct NIO buffers is empty. What is strange is that it is supported with VBO but not with vertex arrays.

I see...so I'll need to put some kind of check to skip empty buffers, I guess. Do you have a suggestion, am I correct? What's the best way to do it.

edit:  I did the following quick check before proceeding with rendering and it might be a good solution.
1  
2  
3  
if (normals[i].hasRemaining()) {
 // render
}
Offline gouessej
« Reply #5 - Posted 2011-05-13 09:39:00 »

One of your direct NIO buffers is empty. What is strange is that it is supported with VBO but not with vertex arrays.

I see...so I'll need to put some kind of check to skip empty buffers, I guess. Do you have a suggestion, am I correct? What's the best way to do it.

edit:  I did the following quick check before proceeding with rendering and it might be a good solution.
1  
2  
3  
if (normals[i].hasRemaining()) {
 // render
}

Sorry Sad it is a common "bug", I have found such bugs even on "good" graphics cards. Some strange things happen too when you draw nothing between a glBegin/glEnd pair.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!