Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (788)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (863)
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 VBOs - How do do memory optimizations?  (Read 1284 times)
0 Members and 1 Guest are viewing this topic.
Offline DesertCookie

Junior Newbie


Exp: 5 years



« Posted 2019-03-23 10:54:21 »

I recently came across this article: https://www.khronos.org/opengl/wiki/Vertex_Specification_Best_Practices#Attribute_sizes, claiming some memory optimizations could be done by, for example, using
1  
GL_SHORT
to store texture coordinates in VBOs.
As I understand it, this has to be set when calling
1  
glVertexAttribPointer
. Using anything other than
1  
GL_FLOAT
results in distorted colors/texture coordinates/the model not showing at all, depending on which data (vertices, colors, normals, ...) I tried to store using a different format.

Do I have to convert my data stored in the VBOs as
1  
float[]
somehow?

Thanks for your help.
Offline KaiHH

JGO Kernel


Medals: 661



« Reply #1 - Posted 2019-03-23 12:18:41 »

For integral data types, use GL30.glVertexAttribIPointer (note the capital "i" in the name). When using glVertexAttribPointer, the type specifies the host/client-side type and everything will be cast/converted to float for the shader invocation.
See: https://stackoverflow.com/questions/28014864/why-do-different-variations-of-glvertexattribpointer-exist#answer-28014920
Offline DesertCookie

Junior Newbie


Exp: 5 years



« Reply #2 - Posted 2019-03-23 16:36:06 »

From glVertexAttribLPointer() I take that after all it's very well possible to use doubles in OGL for vertex positions and in the end matrices too?

Also, how would I prepare my data? It's nice that OGL can handle texture coordinates as shorts, but obviously, we cannot give it a Java short array because we need digit precision.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KaiHH

JGO Kernel


Medals: 661



« Reply #3 - Posted 2019-03-23 18:26:41 »

When you use GL_UNSIGNED_SHORT you can only have a precision of exactly 65536 possible values, linearly distributed between 0.0 and 1.0 when you use normalized=true for glVertexAttribPointer. So a Java short value of (short)0 will map to <float>0.0 in the shader and a value of (short)~0 (=65535) will map to <float>1.0 in the shader.
Likewise, when you want texture coordinates above 1.0 (for texture repeat/mirroring) you can specify normalized=false in glVertexAttribPointer and then the short values will be converted to float values for the shader without normalization to the 0.0...1.0 range.
Offline DesertCookie

Junior Newbie


Exp: 5 years



« Reply #4 - Posted 2019-03-23 21:41:12 »

That precision is more than enough for texture coordinates I figured.
I can't really get it to work properly. In theory, I only need to change normalized of glVertexAttribPointer to be true and enable GL_NORMALIZE so that the values get transformed before passing them to the shaders, correct? I'm still storing my usual float array in the VBO.
The texture coordinates seem somewhat distorted, showing only a white line going across the textured area where usually the texture containing a text would be displayed correctly.
Offline KaiHH

JGO Kernel


Medals: 661



« Reply #5 - Posted 2019-03-23 21:50:19 »

No. When you want to store shorts, you need to store shorts - not floats. Obviously.
So:
1  
2  
3  
ShortBuffer sb = <ShortBuffer filled with shorts>;
glBufferData(GL_ARRAY_BUFFER, sb, GL_STATIC_DRAW);
glVertexAttribPointer(attributeIndex, 2, GL_UNSIGNED_SHORT, true, 0, 0L);

GL_NORMALIZE is completely orthogonal to what you are doing. GL_NORMALIZE is used to normalize normal vectors specified via glNormal or glNormalPointer, so is old Fixed-Function Pipeline stuff and completely does not apply to what you do.
Do research/lookup the stuff you are using: https://www.khronos.org/registry/OpenGL-Refpages/es1.1/xhtml/glEnable.xml (search for GL_NORMALIZE)
Offline DesertCookie

Junior Newbie


Exp: 5 years



« Reply #6 - Posted 2019-03-23 21:55:51 »

Thanks for the link; that will be very useful in the future!
In what range would I put my texture coordinates them in the ShortBuffer? Obviously, I cannot store usual 0.0F to 1.0F as I'd usually do. Would I actually be storing pixel perfect coordinates and OGL would take over normalizing them to values between 0.0F and 1.0F for use in shaders (kind of misphrased it before but that is and was my main issue understanding this)?

Thanks for your help so far, it's been great and I at least got the indices (glDrawElements) working using GL_UNSIGNED_SHORTs.
Offline KaiHH

JGO Kernel


Medals: 661



« Reply #7 - Posted 2019-03-23 21:56:49 »

In what range would I put my texture coordinates them in the ShortBuffer? Obviously, I cannot store usual 0.0F to 1.0F as I'd usually do. Would I actually be storing pixel perfect coordinates and OGL would take over normalizing them to values between 0.0F and 1.0F for use in shaders (kind of misphrased it before but that is and was my main issue understanding this)?
I already detailed this in: http://www.java-gaming.org/topics/lwjgl-vbos-how-do-do-memory-optimizations/38876/msg/371062/view.html#msg371062
Offline DesertCookie

Junior Newbie


Exp: 5 years



« Reply #8 - Posted 2019-03-23 22:03:34 »

My stupidity will be on public display for all of eternity. Thank you. I kind of didn't get it because it didn't make sense to me at all that you'd suddenly be declaring, for example, texture coordinates as values between 0 and 65535 just to save on some memory.
Offline KaiHH

JGO Kernel


Medals: 661



« Reply #9 - Posted 2019-03-23 22:08:43 »

You'd probably want to write some utility method to still work with float values and let the method convert to the appropriate short value. Something like this:
1  
2  
3  
4  
5  
6  
7  
public static short f2us(float f) {
  return (short) (f * 0xFFFF);
}

ShortBuffer sb = ...;
sb.put(f2us(0.0f)).put(f2us(0.5f));
...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DesertCookie

Junior Newbie


Exp: 5 years



« Reply #10 - Posted 2019-03-23 22:15:39 »

...working on that right now   Pointing
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (2192 views)
2018-11-16 13:46:03

hadezbladez (823 views)
2018-11-16 13:41:33

hadezbladez (2150 views)
2018-11-16 13:35:35

hadezbladez (425 views)
2018-11-16 13:32:03

EgonOlsen (3462 views)
2018-06-10 19:43:48

EgonOlsen (3696 views)
2018-06-10 19:43:44

EgonOlsen (2280 views)
2018-06-10 19:43:20

DesertCoockie (3015 views)
2018-05-13 18:23:11

nelsongames (3089 views)
2018-04-24 18:15:36

nelsongames (3908 views)
2018-04-24 18:14:32
Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20
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!