Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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  
  VBO troubles, crash when calling glDrawArrays  (Read 1901 times)
0 Members and 1 Guest are viewing this topic.
Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Posted 2012-10-15 13:20:57 »

Hello, im trying to implement VBO's but for 2 days i still have not found a solution.
Im sure its a simple mistake, but i cant seem to find it.
Between Init and Init buffer the buffer gets filled, all numbers are right.

The program crashes on this line:
1  
glDrawArrays(GL_QUADS, 0, maxobjects);

Even a try / catch statement wont stop this crash, so i guess its a native crash.

Init:
1  
2  
3  
4  
5  
6  
7  
8  
    public VBO_Object(int objects){
       
        maxobjects = objects;
        stride = (3 + 3 + 2) * 4 * objects; // 3 for vertex, 3 for colour and 2 for texture coordinates. * 4 for bytes
       buffersize = (3 + 3 + 2) * objects;
       
        buffer = new float[buffersize];
    }


Init buffer
1  
2  
3  
4  
5  
6  
7  
IntBuffer buffer = BufferUtils.createIntBuffer(1);
        ARBVertexBufferObject.glGenBuffersARB(buffer);
        id= buffer.get(0);

                data = BufferUtils.createFloatBuffer(buffer.length);
                data.put(buffer);
                data.flip();


Draw call:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
    public void Draw(){       
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        glBindBufferARB(GL_ARRAY_BUFFER_ARB, id);
        glBufferDataARB(GL_ARRAY_BUFFER_ARB, data, GL_STATIC_DRAW_ARB);
        glVertexPointer(3, GL_FLOAT, stride, /* offset **/0); // float at index 0
       glColorPointer(3, GL_FLOAT, stride, /* offset **/(3*1) << 2); // float at index 3
       glTexCoordPointer(2, GL_FLOAT, stride, /* offset **/(3*2) << 2); // float at index 6
       glDrawArrays(GL_QUADS, 0, maxobjects);

        glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);

        glDisableClientState(GL_COLOR_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    }
Offline Danny02
« Reply #1 - Posted 2012-10-15 14:13:03 »

I seee several things wrong here,

- first you mix local variables and fields (buffer), buffer as your float data and the IntBuffer to get the BufferObject ID.
- you upload (glBufferData) the data each frame(in the Draw command), if you are not changeing the data each frame there is no need to upload the data more then once
Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #2 - Posted 2012-10-15 14:43:44 »

yes the code is not really nice, but thats becaus i tried a lot of stuff before posting it here.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-10-15 15:16:29 »

glDrawArrays() expects a vertex count, not an "object" count though I guess that might just be a weird variable name.

I can't really see anything wrong. A minor unrelated note is that GL_STATIC_DRAW_ARB is used for update-once data, so you should use GL_STREAM_DRAW_ARB since you're updating the buffer every frame, but that's just a hint so it shouldn't cause a crash. Other than that I can only tell you to simplify the program (make a new test program / strip out unrelated stuff, the first one is preferred). Most likely you're leaking some OpenGL state. Are you 100% sure that it's crashing on that line? Maybe you call glDrawArrays() somewhere else and it crashes there? Does it crash on the first call to it or the second?

Myomyomyo.
Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #4 - Posted 2012-10-15 15:39:51 »

its indeed just a weird name, ill rename it to vcount.
I called it a object, because its a vertex + color + texcoord, but vcount is more clear i guess.

What you see is all what happens, so there cant be anything leaking out.
This is a new test class, so i cant make it more basic i think.

When commenting out the line it does not crash.
Also, when i call glDrawArrays(GL_QUADS, 0, 4), it does not crash, but does not show anything eighter.
Offline StumpyStrust
« Reply #5 - Posted 2012-10-15 16:03:57 »

Well I have run into things like this alot when using VBOs and nothing "seems" to be wrong here. Is the maxobjects in your drawArrays how many quads you want? if so you need to * that by 4. My guess is that your pointers could be wrong or your buffer itself. Make sure you are filling it properly. You also might be trying to draw with too many vertices which as caused crashes for me.

Your off sets should be 0, 12, 24. I know I followed a tut that did offsets like that and it did not work for me.

Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #6 - Posted 2012-10-15 18:48:26 »

maxobjects is the amount of vertrices i want (including color and texture coords).
My fill loop (ignore weird names, its just a test class):

1  
2  
3  
4  
5  
6  
7  
8  
        obj = new VBO_Object(numstars*4);
       
        for(int i = 0; i < numstars; i ++){
            stars[i] = new BackgroundObject(this);
            stars[i].updateVBO(obj);
        }
       
        obj.create();


Then each stars fills the float array[] (for simplicity, in the future i want to write directly to the buffer)
The format is VCT (vertex, color and textcoords).
The following code works, the float array gets filled correctly, so this is probably not the problem.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
        vbo.AddVertex(x, y, 0);
        vbo.AddColor(1, 1, 1);
        vbo.AddText(0, 0);
       
        vbo.AddVertex(x, y+height, 0);
        vbo.AddColor(1, 1, 1);
        vbo.AddText(0, ty);
       
        vbo.AddVertex(x+width, y+height, 0);
        vbo.AddColor(1, 1, 1);
        vbo.AddText(tx, ty);
       
        vbo.AddVertex(x+width, y, 0);
        vbo.AddColor(1, 1, 1);
        vbo.AddText(tx, 0);


Offsets should return those values, but i replaced it with your static values, still crashes.

Do i maybe need to enable some tuff with glEnable?
Offline theagentd
« Reply #7 - Posted 2012-10-15 19:36:34 »

Can you put together a small standalone test program so we can test it? Everything looks fine to me... >_<

Myomyomyo.
Offline StumpyStrust
« Reply #8 - Posted 2012-10-15 22:03:58 »

Your VBO is interleaved right?

Is it VCTVCTVCT or VVVCCCTTT? If it is the former then your stride is wrong. Other then that I can only agree with the broh above me. Need more code.

Offline theagentd
« Reply #9 - Posted 2012-10-16 09:38:12 »

Oh, god, yes. Stride is the size of ONE vertex, not all vertices. It's reading the first vertex's data, then skipping the rest of the VBO and reading outside it ---> crash.

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

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #10 - Posted 2012-10-16 09:54:03 »

Awsome!
Works like a charm.
I knew it would be a simple mistake.

Thanks alot guys Cheesy
Now back to my game.
Offline RobinB

JGO Knight


Medals: 37
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #11 - Posted 2012-10-16 11:22:37 »

One more question;

Is it better to update each entry of the floatbuffer apart, or to save these changes to the float array and load them all at once into the buffer.
Offline Danny02
« Reply #12 - Posted 2012-10-16 11:45:20 »

it depends  what you are doing, if you are changing nearly all of the data you can do the changes first your lcoal float array and then push everything to the card.

On the other hand if the data you need to update is little in comperision to the size of the hole array you can either update only a part of the gpu memory with glSubBufferData(when the data which gets updated is a chunck) or map the GPU data to a local ByteBuffer with glMapBuffer and do in that buffer all the updates.

PS: I read some time ago that the fastest way to updated the hole buffer is to first call glBufferData with the memory size you need but null as data and then call glSubBufferData for all the data you want to push to the card
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.

xsi3rr4x (28 views)
2014-04-15 18:08:23

BurntPizza (25 views)
2014-04-15 03:46:01

UprightPath (40 views)
2014-04-14 17:39:50

UprightPath (22 views)
2014-04-14 17:35:47

Porlus (38 views)
2014-04-14 15:48:38

tom_mai78101 (62 views)
2014-04-10 04:04:31

BurntPizza (121 views)
2014-04-08 23:06:04

tom_mai78101 (221 views)
2014-04-05 13:34:39

trollwarrior1 (188 views)
2014-04-04 12:06:45

CJLetsGame (195 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!