Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
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  
  load cubemap with one bytebuffer  (Read 1296 times)
0 Members and 1 Guest are viewing this topic.
Offline deepthought
« Posted 2012-12-15 01:44:43 »

If i save a cubemap texture in an image like so:

Positive_x
Negative_X
positive_y
negative_y
positive_z
negative_z

could i load it with the code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
int subheight=img_height/6;
ByteBuffer buf = ByteBuffer.allocate(img_width*img_height*bpp);

//load image into buf here

public static void glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X,0,format,img_width,subheight,0,format,datatype,buf);
public static void glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X,0,format,img_width,subheight,0,format,datatype,buf);
public static void glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y,0,format,img_width,subheight,0,format,datatype,buf);
public static void glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,0,format,img_width,subheight,0,format,datatype,buf);
public static void glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z,0,format,img_width,subheight,0,format,datatype,buf);
public static void glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,0,format,img_width,subheight,0,format,datatype,buf);


Or will i have to use separate buffers?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline davedes
« Reply #1 - Posted 2012-12-15 03:17:43 »

All of your pixels for a single image need to be aligned, e.g.
1  
2  
3  
4  
5  
 { 
    all pixels of image 1,
    all pixels of image 2,
    all pixels of image 3,
    etc }


Here is an example of using a single buffer which contains multiple image data:
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  
int width = 1, height = 1;
int bpp = 4;
int count = 2;

tex1 = new Texture(width, height, Texture.NEAREST);
tex2 = new Texture(width, height, Texture.NEAREST);

ByteBuffer buf = BufferUtils.createByteBuffer(width*height*bpp*count);
final byte FF = (byte)0xFF;

//tex one (white)
buf.put(FF).put(FF).put(FF).put(FF);

//tex two (red)
buf.put(FF).put((byte)0x00).put((byte)0x00).put(FF);

//flip the buffer
buf.flip();

//set buffer to image position 0
//(not necessary since we just flipped; only included for clarity)
buf.position( width*height*bpp * 0 ); //image 0

//glTexImage2D with same width, height
tex1.upload(GL_RGBA, buf);

//set buffer to image position 1
buf.position( width*height*bpp * 1 );

//glTexImage2D with same width, height
tex2.upload(GL_RGBA, buf);


So in theory it should work if you ensure each image is aligned after the other.

For more reading on textures and buffers:
https://github.com/mattdesl/lwjgl-basics/wiki/Textures
https://github.com/mattdesl/lwjgl-basics/wiki/Java-NIO-Buffers

Offline deepthought
« Reply #2 - Posted 2012-12-15 15:04:14 »

glteximage starts from the current buffer position, correct?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2012-12-15 16:14:43 »

I thought so too but my above code only worked with buffer.position(x).  Huh

Offline deepthought
« Reply #4 - Posted 2012-12-15 17:17:33 »

I've looked through the lwjgl code and it appears that using glteximage2d doesn't actually change the buffer position.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline VeaR

Junior Devvie





« Reply #5 - Posted 2012-12-15 23:56:28 »

I'd suggest trying out PBOs. You can upload your data directly to the OpenGL driver in one go, after that you can define your textures from that buffer however you like. Its faster since you upload to OpenGL directly, you don't have to deal with the direct ByteBuffer becoming garbage at some point since the upload memory is provided by the OGL driver itself, you can upload all your data as one chunk and split it up later as you like. Its also available since OGL 1.5, so it should work even on older cards.
Offline deepthought
« Reply #6 - Posted 2012-12-16 05:00:12 »

Thank You. I will do that.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline deepthought
« Reply #7 - Posted 2012-12-16 20:13:15 »

If i map a PBO and get a buffer from the driver, can it be written to from another thread?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline VeaR

Junior Devvie





« Reply #8 - Posted 2012-12-16 20:59:48 »

Yes, after you map the buffer, you can hand it over to another thread to fill it. After filling it, hand it back to the first thread to unmap and glTeximage it. I suggest the following loop:

Step 1: unmap PBO and glTeximage loaded textures
Step 2: update the scene, collect the list of textures to be loaded
Step 3: map the PBO buffer(s), hand them to the second thread
Step 4: draw the scene
Step 5: repeat

This way the GPU will asynchronously upload textures while the program is doing CPU heavy work (updating the scene).
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.

xFryIx (62 views)
2014-11-13 12:34:49

digdugdiggy (41 views)
2014-11-12 21:11:50

digdugdiggy (36 views)
2014-11-12 21:10:15

digdugdiggy (30 views)
2014-11-12 21:09:33

kovacsa (53 views)
2014-11-07 19:57:14

TehJavaDev (56 views)
2014-11-03 22:04:50

BurntPizza (55 views)
2014-11-03 18:54:52

moogie (70 views)
2014-11-03 06:22:04

CopyableCougar4 (71 views)
2014-11-01 23:36:41

DarkCart (156 views)
2014-11-01 14:51:03
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

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!