Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (113)
games submitted by our members
Games in WIP (562)
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 rendering  (Read 529 times)
0 Members and 1 Guest are viewing this topic.
Offline DazKins
« Posted 2013-05-31 10:41:50 »

Ive got some code thats rendering objects to the screen using a vbo, but i feel it's very inneficient

For example, if i wanted to render a cube i would need (6 faces * 4 vertices) 24 vertices to make one cube when in reality it only has 8

Is there anyway to sort this out?

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline quew8

JGO Coder


Medals: 29



« Reply #1 - Posted 2013-05-31 10:55:09 »

Use index buffers (GL_ELEMENT_ARRAY_BUFFER) and glDrawElements as well. Example:

Construct ibo.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
//gen vbo

int[] indices = new int[] { //This uses quads and depends on the order of the vertices in the vbo but you get the
   0, 1, 2, 3, //Face 1        picture
   4, 5, 6, 7, //Face 2
   0, 1, 5, 4, //Face 3
   3, 2, 6, 7, //Face 4
   0, 4, 7, 3, //Face 5
   1, 5, 6, 2  //Face 6
};

IntBuffer indicesData = createIntBuffer(indices);

int iboId = glGenBuffer();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesData, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);


Rendering the quad
1  
2  
3  
4  
5  
bindVBO();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboId);
setupPointers();
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, 0); //Arguments are: render mode, number indices, type of indices
                                                                          //                     and byte offset into indices to start at.


That's all folks.
Offline DazKins
« Reply #2 - Posted 2013-06-01 09:21:42 »

But, what if in some cases i didn't want to render then entire quad? What if i wanted to not render the backface of one cube and maybe not the bottom face of one. I don't see how using an element array buffer could make that work?

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #3 - Posted 2013-06-01 09:25:11 »

Then you have to leave out the face you don't want to draw:

1  
2  
3  
4  
5  
6  
7  
8  
int[] indices = new int[] { //This uses quads and depends on the order of the vertices in the vbo but you get the 
   0, 1, 2, 3, //Face 1        picture
   4, 5, 6, 7, //Face 2
//  0, 1, 5, 4, //Face 3 <- leave it out, don't draw it
   3, 2, 6, 7, //Face 4
   0, 4, 7, 3, //Face 5
   1, 5, 6, 2  //Face 6
};


You really need to understand indices. Try some tutorials out there Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline DazKins
« Reply #4 - Posted 2013-06-01 09:29:04 »

But here:

1  
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, 0);


You've told OpenGL to render 24 vertices not 20, so surely this wouldn't come out right

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #5 - Posted 2013-06-01 09:30:35 »

Ok. So, simply put in 20? Or even better:
indices.length
.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline DazKins
« Reply #6 - Posted 2013-06-01 09:31:30 »

But what if certain quads in that vbo need to have a different number of faces rendered each?

(sorry if im being pickety here, i just like to make sure im not writing horribly inefficient code  Grin)

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #7 - Posted 2013-06-01 09:37:22 »

You might be confusing some terminologies here. A quad is a face, actually. Or in other words: A quad has 4 vertices.

So a quad only consists of one face...  persecutioncomplex

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline quew8

JGO Coder


Medals: 29



« Reply #8 - Posted 2013-06-01 09:57:10 »

You can if you wish draw each face individually using the offset parameter of glDrawElements:

1  
2  
3  
4  
5  
6  
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, 0);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, 16); //16 is 4 indices of integers, integers are 4 bytes each.
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, 32); //2 * 16
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, 48); //3 * 16
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, 64); //4 * 16
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, 80); //5 * 16


There is also a glMultDrawElements method but if you're using LWJGL, this method is not available (too much messing around with pointers) and presumably that means it is not available to LibGDX as well.
Offline DazKins
« Reply #9 - Posted 2013-06-01 11:54:26 »

matheus23 : Apologies, i meant to say cube

quew8 : I can see what you're trying to say here but say i have lots of cubes stored in one vbo, i don't want to be rendering all the faces individually

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline quew8

JGO Coder


Medals: 29



« Reply #10 - Posted 2013-06-01 14:14:18 »

WARNING LONG DESCRIPTION AHEAD.

The way I often do it is to store an array of "handles" to objects (so in your case it could be a face,a cube, a group of cubes or whatever, you could also have the ability to access a whole cube or a particular face in that cube etc). Each handle stores the starting offset in the index buffer and the number of indices needed to be be drawn.

Each frame I find the first handle in the array that needs to be drawn, and store its offset. Then I iterate through, adding the number of indices to a counter until I find one that shouldn't be drawn. Then I call glDrawElements with the stored offset and the counter for number of indices. Then I keep going until the next one that needs to be drawn.

I know this seems like a waste, but IMHO, it is the best way to have complete choice over what is rendered and also minimize OpenGL calls which are generally the slow bits. You can group whole lots of things together (like a whole room or a character mesh) so I find it works quite well with occlusion detection when store things in a tree/scene graph type thing. You can eliminate an entire branch and half the iterations.

As ever computer graphics are an art form, not a science or religion, and there is no one true path. This suits me well but may not you.
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.

Dwinin (15 views)
2014-09-12 09:08:26

Norakomi (45 views)
2014-09-10 13:57:51

TehJavaDev (57 views)
2014-09-10 06:39:09

Tekkerue (26 views)
2014-09-09 02:24:56

mitcheeb (49 views)
2014-09-08 06:06:29

BurntPizza (33 views)
2014-09-07 01:13:42

Longarmx (19 views)
2014-09-07 01:12:14

Longarmx (21 views)
2014-09-07 01:11:22

Longarmx (20 views)
2014-09-07 01:10:19

mitcheeb (30 views)
2014-09-04 23:08:59
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!