Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (606)
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  
  [SOLVED] [LWJGL] Render multiple VBO's  (Read 1152 times)
0 Members and 1 Guest are viewing this topic.
Offline orange451

JGO Coder


Medals: 18
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Posted 2013-11-12 15:57:31 »

Hello!

I am trying to render multiple VBO's in my current Java project.

Currently, I have my own model format  which creates a vertex buffer object from its vertex coordinates, texture coordinates, and normal coordinates. When I draw my first model, it draws perfectly fine, however, when I draw my second model it draws the first model's VBO.

Here is how I am drawing the models:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
//Enable the ability to use VBO's
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

//setup the model pointers
int bytes_per_variable = 4;
int stride = 8 * bytes_per_variable;
GL11.glVertexPointer(3, GL11.GL_FLOAT, stride, 0);
GL11.glNormalPointer(GL11.GL_FLOAT, stride, (3 * bytes_per_variable));
GL11.glTexCoordPointer(2, GL11.GL_FLOAT, stride, (6 * bytes_per_variable));

//Draw the model
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, vertex_data_array.length / 8);

//Disable VBO's
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);


Here is how I am creating the VBO:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
this.vertex_data_array = new float[amountVertices * 8];
int counter = 0;
//Pack the new array with data from the model
for (int i = 0; i < amountVertices; i++) {
   vertex_data_array[counter] = (float) vertX[i]; counter++;
   vertex_data_array[counter] = (float) vertY[i]; counter++;
   vertex_data_array[counter] = (float) vertZ[i]; counter++;
   vertex_data_array[counter] = (float) norX[i]; counter++;
   vertex_data_array[counter] = (float) norY[i]; counter++;
   vertex_data_array[counter] = (float) norZ[i]; counter++;
   vertex_data_array[counter] = (float) texX[i]; counter++;
   vertex_data_array[counter] = (float) texY[i]; counter++;
}

//Setup the floatbuffer
FloatBuffer vertex_buffer_data = BufferUtils.createFloatBuffer(vertex_data_array.length);
vertex_buffer_data.put(vertex_data_array);
vertex_buffer_data.rewind();

//Bind buffer to id
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertex_buffer_id);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertex_buffer_data, GL15.GL_STATIC_DRAW);


Here is how I generate the vertex buffer id:
1  
2  
3  
IntBuffer buffer = BufferUtils.createIntBuffer(1);
GL15.glGenBuffers(buffer);
this.vertex_buffer_id = buffer.get(0);



Any help would be appreciated!

Thanks,
Andrew

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #1 - Posted 2013-11-12 23:59:09 »

You'll need to unbind the buffer first by calling glBindBuffer with an I'd of 0.

Offline orange451

JGO Coder


Medals: 18
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #2 - Posted 2013-11-13 02:00:42 »

You'll need to unbind the buffer first by calling glBindBuffer with an I'd of 0.

I still cannot seem to get it to work...

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  
//Bind buffer to id
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertex_buffer_id);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertex_buffer_data, GL15.GL_STATIC_DRAW);

//Enable the ability to use VBO's
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

//setup the model pointers
int bytes_per_variable = 4;
int stride = 8 * bytes_per_variable;
GL11.glVertexPointer(3, GL11.GL_FLOAT, stride, 0);
GL11.glNormalPointer(GL11.GL_FLOAT, stride, (3 * bytes_per_variable));
GL11.glTexCoordPointer(2, GL11.GL_FLOAT, stride, (6 * bytes_per_variable));

//Draw the model
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, vertex_data_array.length / 8);

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

//Disable VBO's
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);


Am I doing it correctly?




[EDIT]
My apologies, I found that the model was in fact drawing at (0, 0, 0). Now to figure out how to position a VBO mmodel Smiley

Thanks!

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #3 - Posted 2013-11-13 02:04:31 »

Positioning is easy, just specify the coordinates when you create the vbo data, and then add the size to the coordinates. DO NOT use glTranslate for large amounts of data, just do the math yourself!

Offline orange451

JGO Coder


Medals: 18
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #4 - Posted 2013-11-13 02:16:32 »

Wouldn't I have to recreate all of the vertex data array data? If so, wouldn't that defeat the purpose of using a VBO (assuming this model is going to always be moving)?

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #5 - Posted 2013-11-13 02:31:52 »

True, I was thinking about creating terrain data. Never mind!

If you are going to be moving lots of vertices around every second (I.E. its not a static VBO), then I would seriously recommend looking into shaders. Getting closer to the metal when programming is always good, it just might be harder. If your model isn't too high - poly, I guess it wouldn't matter if you use glTranslate.

Offline orange451

JGO Coder


Medals: 18
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #6 - Posted 2013-11-13 02:33:31 »

Well I use glTranslate for my camera, and when I translate the model as well, it moves the camera!

Does this have anything to do with glPushMatrix?

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #7 - Posted 2013-11-13 02:37:58 »

If you need to only move one vertex, or set of vertices, you need to wrap it in glPushMatrix and glPopMatrix. For example:

1  
2  
3  
4  
5  
6  
camera.translate();
glLoadIdentity();

glPushMatrix();
model.translate();
glPopMatrix();


Basically, pushing and popping a matrix is pushing the top matrix off the matrix stack, modifying it, then sticking it back on top, so that only the vertices that were translated are affected. This means that only your model will translate while all other vertices will stay at the same coordinates.

Offline orange451

JGO Coder


Medals: 18
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #8 - Posted 2013-11-13 03:03:11 »

If you need to only move one vertex, or set of vertices, you need to wrap it in glPushMatrix and glPopMatrix. For example:

1  
2  
3  
4  
5  
6  
camera.translate();
glLoadIdentity();

glPushMatrix();
model.translate();
glPopMatrix();


Basically, pushing and popping a matrix is pushing the top matrix off the matrix stack, modifying it, then sticking it back on top, so that only the vertices that were translated are affected. This means that only your model will translate while all other vertices will stay at the same coordinates.

I see...
I tried doing this, but it appears that now the camera ceases to move. Am I missing a step? I'm still quite new to lwjgl, please excuse my "newbieness"

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #9 - Posted 2013-11-13 03:05:34 »

Here, post some code, I'd be glad to help! You should be translating the camera, then drawing your model, and then calling glLoadIdentity after all of that. Sorry, I had the order a little mixed up!

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

JGO Coder


Medals: 18
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #10 - Posted 2013-11-13 03:18:00 »

Main threads render method:
1  
2  
3  
Game.getSingleplayerGame().getPlayerController().render();
Game.getSingleplayerGame().getWorld().render();
GL11.glLoadIdentity();


Player Controller (camera) rendering method:
1  
2  
3  
4  
5  
6  
7  
8  
float yaw = player.getLocation().getYaw();
float pitch = player.getLocation().getPitch();

GL11.glRotatef(pitch - 90, 1, 0, 0);
GL11.glRotatef(0,     0, 1, 0);
GL11.glRotatef(yaw  , 0, 0, 1);

GL11.glTranslatef(-player.getLocation().getX(), -player.getLocation().getY(), -player.getLocation().getZ());


World rendering method:
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  
29  
30  
31  
32  
GL11.glPushMatrix();
GL11.glTranslated(-x, -y, -z);

//Bind buffer to id
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertex_buffer_id);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertex_buffer_data, GL15.GL_DYNAMIC_DRAW);

//Enable the ability to use VBO's
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
//GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

//setup the model pointers
int bytes_per_variable = 4;
int stride = 8 * bytes_per_variable;
GL11.glVertexPointer(3, GL11.GL_FLOAT, stride, 0);
GL11.glNormalPointer(GL11.GL_FLOAT, stride, (3 * bytes_per_variable));
//GL11.glColorPointer(4, GL11.GL_DOUBLE, stride, 6 * bytes_double);
GL11.glTexCoordPointer(2, GL11.GL_FLOAT, stride, (6 * bytes_per_variable));

//Draw the model
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, vertex_data_array.length / 8);

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

//Disable VBO's
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

GL11.glPopMatrix();


Note, the world rendering method calls the bove cods for the world map, and all the entities in it (which have models).


After changing the loadIdentity to after drawing everything, the camera goes back to being stuck at 0,0,0. If I put it right after translating the camera, then it does the same thing.

[EDIT]
If I ONLY draw ONE vbo, then there are no issues. But once I start drawing multiple vbo's, the problem is created.

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline opiop65

JGO Kernel


Medals: 161
Projects: 7
Exp: 4 years


JumpButton Studios


« Reply #11 - Posted 2013-11-13 03:43:27 »

That's because you should be translating the data after you create the VBO. Right now you're translating before. So, remove the push and pop matrix calls, the translate call, and translate the vbo after its created. Wrap the translate function in the push and pop calls!

Offline orange451

JGO Coder


Medals: 18
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #12 - Posted 2013-11-13 12:31:58 »

Well I appear to have everything working! Smiley
Thanks a bunch!

I do have one more question, however... I tried using glRotate() on my model (before translating it), and it appears to rotate it as if it was already translated, so the model is miles away from where it should be.

the setup:
glpushmatrix();
glrotate(...);
gltranslate(...);
draw();
glpopmatrix();

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Online SHC
« Reply #13 - Posted 2013-11-13 13:01:56 »

For rotation of model, first translate and then rotate. It should work then.

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.

Elsealabs (20 views)
2014-12-28 10:39:27

CopyableCougar4 (21 views)
2014-12-28 02:10:29

BurntPizza (25 views)
2014-12-27 22:38:51

Mr.CodeIt (15 views)
2014-12-27 04:03:04

TheDudeFromCI (20 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (60 views)
2014-12-15 09:26:44

Mr.CodeIt (48 views)
2014-12-14 19:50:38

BurntPizza (98 views)
2014-12-09 22:41:13

BurntPizza (117 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!