Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (715)
Games in Android Showcase (214)
games submitted by our members
Games in WIP (788)
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  
  [LWJGL] [JOML] Some instancing queries  (Read 578 times)
0 Members and 1 Guest are viewing this topic.
Offline EnderNinja7

Senior Newbie


Exp: 1 year



« Posted 2017-03-20 05:40:23 »

I am using lwjgl with joml to make a voxel engine (mainly for infinity and destruction). I have experience in opengl programming in java using lwjgl but am new in the instancing

1. There is a SimplexNoise class in JOML. How can I use it to get transform values for the cubes?

2. After getting the transform values, they must be stored somewhere. According to me it should be an array of model matrices. But Matrix4f[] returns a lot of errors and MatrixStackf does not provide Matrix array calculations.

3. Now, how will I get my matrices to a buffer for shader use?

4. I need a simplified VAO/VBO initialization/binding/attrib code for the instanced arrays.

Thanks in advance  Smiley     
Offline KaiHH

JGO Kernel


Medals: 390



« Reply #1 - Posted 2017-03-20 10:13:41 »

Quote
1. There is a SimplexNoise class in JOML. How can I use it to get transform values for the cubes?
You do not use noise to generate the transformation (i.e. "position") of a cube, but you use the noise function as a density function describing at every lattice point of your world whether or not that position contains a cube (and probably what kind of cube).

Quote
2. After getting the transform values, they must be stored somewhere. According to me it should be an array of model matrices. But Matrix4f[] returns a lot of errors and MatrixStackf does not provide Matrix array calculations.
You do not need matrices at all. They're overkill for what you are doing. Just assign each visible cube a 3-component vector denoting its position.

Quote
3. Now, how will I get my matrices to a buffer for shader use?
Like above, you do not need matrices. But if you wanted to, did you consider reading JOML's README.md?

Quote
4. I need a simplified VAO/VBO initialization/binding/attrib code for the instanced arrays.
Google for "OpenGL instancing" and you'll find very good explanations and examples.
Offline EnderNinja7

Senior Newbie


Exp: 1 year



« Reply #2 - Posted 2017-03-21 04:30:49 »

Thanks, I made a Vector3f array and stored the transform values in it. I also made a UBO to transfer it to the shader. Now, how can I multiply the vec3 in such a way that it processes as if I made a model matrix for each instance ?

Normal vertex shader code

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
#version 330 core;

layout(location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout(location = 2) in mat4 model;

uniform mat4 view;
uniform mat4 perspective;

out vec3 excolor;

void main(){
gl_Position = perspective * view * model * vec4(position, 1.0);
excolor = color;
}


How should I change the algorithm now there is gl_InstanceId and a vec3 instead of mat4?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KaiHH

JGO Kernel


Medals: 390



« Reply #3 - Posted 2017-03-21 07:55:54 »

Now, how can I multiply the vec3 in such a way that it processes as if I made a model matrix for each instance ?
Think about it for a second. You have your vertices whose 'position' values are (hopefully) all around (0, 0, 0), like with a unit cube.
And then you have your 'model' positions which define where in the world that particular cube is located at. That means, the 'model' position (x', y', z') should _translate_ a cube (which was previously around (0, 0, 0)) to have its center at (x', y', z'). Now, you should be able to figure out what to do so that this happens.

By the way, if you manually multiply your vertex with a model matrix that you've built with Matrix4f.translate(), then you'll find that you end up with exactly the same calculation that you would do above with a single 'model' position instead of a matrix.

Other great resources which I strongly recommend you look at:
- http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/
- https://open.gl/transformations
Offline EnderNinja7

Senior Newbie


Exp: 1 year



« Reply #4 - Posted 2017-03-21 11:59:02 »

Ah, I should have known!. Anyway I got my work done, and also found out that gl_InstanceId is bad practice. So I use the gl4.2 method.
Offline h.pernpeintner

JGO Coder


Medals: 51



« Reply #5 - Posted 2017-03-21 14:01:03 »

and also found out that gl_InstanceId is bad practice. So I use the gl4.2 method.

Hm? Who says that using gl_InstanceId is bad practice?? I'm using it and it works perfectly fine. What should the gl4.2 method be!?
Offline EnderNinja7

Senior Newbie


Exp: 1 year



« Reply #6 - Posted 2017-03-22 03:01:28 »

According to learnopengl.com instancing tutorial https://learnopengl.com/#!Advanced-OpenGL/Instancing(and many other websites), the gl_Position with uniform vecs is fine for a few instances, but if we hit  a lot more than 100 instances, it may make a performance issue. The new method is that an instance offsets VBO is created together with glVertexAttribDivisor(2, 1); . We now let opengl handle the shader iteration.
Offline EnderNinja7

Senior Newbie


Exp: 1 year



« Reply #7 - Posted 2017-03-22 06:39:28 »

Hmmm, another problem. While setting up the VBO code of offsets, floatbuffer does not accept an array of vector3fs . How do I get past this? without passing the vec3s instancing arrays would not execute.
Offline KaiHH

JGO Kernel


Medals: 390



« Reply #8 - Posted 2017-03-22 09:16:23 »

floatbuffer does not accept an array of vector3fs . How do I get past this?
1  
2  
3  
1. v.get(floatBuffer)
2. vs[i].get(3 * i, floatBuffer)
3. floatBuffer.put(v.x).put(v.y).put(v.z)

where 'v' has type Vector3f, 'vs' has type Vector3f[], and floatBuffer has type FloatBuffer

Make sure you carefully read the JavaDocs of all methods involved, especially that of case 1.
Offline EnderNinja7

Senior Newbie


Exp: 1 year



« Reply #9 - Posted 2017-03-22 11:51:40 »

Thanks. Now I use this method :-

1  
2  
3  
4  
5  
6  
7  
8  
9  
//BEFORE: init Vector3f[16] chunk_transforms etc. 

FloatBuffer fb = BufferUtils.createFloatBuffer(sizeOf(Vector3f) * CHUNK_SIZE);
for(int c = 0; c =< CHUNK_SIZE; c++){
       fb.put(chunk_transforms[c].x()).put(chunk_transforms[c].y()).put(chunk_transforms[c].z());
}
fb.flip();

//After : VBO/VAO code etc.


EDIT: Code typo
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline h.pernpeintner

JGO Coder


Medals: 51



« Reply #10 - Posted 2017-03-22 12:16:04 »

According to learnopengl.com instancing tutorial https://learnopengl.com/#!Advanced-OpenGL/Instancing(and many other websites), the gl_Position with uniform vecs is fine for a few instances, but if we hit  a lot more than 100 instances, it may make a performance issue. The new method is that an instance offsets VBO is created together with glVertexAttribDivisor(2, 1); . We now let opengl handle the shader iteration.

Sorry, I'm not quite sure if I understand you. Okay, maybe if you only deal with a vec3 and nothing more, it might be better performancewhise to use instanced attributes... but "in general", I would say that gl_InstanceId is not deprecated but even the "cleaner" way. Because first, you have more than a vec3 position for your entity. Modern way of handling this is to have large uniform buffer or ssbo. In most cases, it would be at least a 4x4 matrix with a transformation. In my case, I have 24 values per entity - Having this as instanced attribute would mess up the strucutre pretty much. And the more important thing: What does my vertex structure have to do with how my entity is instanced?? My rendering pipeline lets me adjust instance count on the fly..all I have to do is change a single value in my command buffer and I have another instance. Okay, plus a change in my entity uniform buffer, where I add another entry and maybe have to rebuffer. But since there are stored transformations, the data is probably dynamically updated so or else, while my vertex data is completely static. Another advantage: You can have the vertices stored once and simple push an isntanced render call with indexing into your uniform array.... I'm pushing 4 mio vertices with 340000 cubes (instances) with this pipeline on my GTX 1060 at 60 fps, so I doubt that there really is a performance penalty at all - maybe on older hardware...
Offline EnderNinja7

Senior Newbie


Exp: 1 year



« Reply #11 - Posted 2017-03-23 04:18:12 »

I can use your setup for characters and NPCs. I don't think I should use this for land as :-

1. My voxels are VERY small compared to Minecraft/Cubeworld/Other projects here. It is about this size:-
https://drive.google.com/file/d/0B-37T28tGwC7Z3lneWJxSnhNeU0/view?usp=sharing

2. Since there are very small voxels, there would be a lot of instances as compared to the standard textured voxel engine ( The chunk size is being debated on). And according to KaiHH :-
Quote
You do not need matrices at all. They're overkill for what you are doing. Just assign each visible cube a 3-component vector denoting its position.
After all I only need positions for the land cubes.

Thanks.
Pages: [1]
  ignore  |  Print  
 
 

 
CopyableCougar4 (214 views)
2017-03-24 15:39:42

theagentd (196 views)
2017-03-24 15:32:08

Rule (255 views)
2017-03-19 12:43:22

Rule (237 views)
2017-03-19 12:42:17

Rule (242 views)
2017-03-19 12:36:21

theagentd (261 views)
2017-03-16 05:07:07

theagentd (257 views)
2017-03-15 22:37:06

theagentd (186 views)
2017-03-15 22:32:18

theagentd (180 views)
2017-03-15 22:31:11

ral0r2 (166 views)
2017-03-03 11:52:41
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!