Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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  
  Create OpenGL Texture from ByteBuffer  (Read 3406 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 5
Projects: 2


Nathan Kramber


« Posted 2012-10-04 00:41:17 »

So, is there a quick-and-easy way to go about this?

Here's what I have so far:

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  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
public TextureSheet(String path, int id, int tw, int th) {
      try {
         sheet = ImageIO.read(TextureSheet.class.getClassLoader().getResourceAsStream(path));
         
         int rows = sheet.getHeight() / th;
         int cols = sheet.getWidth() / tw;
         
         BufferedImage[] sprites = new BufferedImage[rows * cols];
         
         for (int x = 0; x < rows; x++) {
            for (int y = 0; y < cols; y++) {
               sprites[x * y] = sheet.getSubimage(x * tw, y * th, tw, th);
            }
         }
         
         for (int i = 0; i < sprites.length; i++) {
            BufferedImage image = sprites[i];
            int[] pixels = new int[image.getWidth() * image.getHeight()];
            image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
           
            ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);
           
            for (int y = 0; y < image.getHeight(); y++) {
               for (int x = 0; x < image.getWidth(); x++) {
                  int pixel = pixels[y * image.getWidth() + x];
                  buffer.put((byte) ((pixel >> 24) & 0xFF)); // alpha
                  buffer.put((byte) ((pixel >> 16) & 0xFF)); // red
                      buffer.put((byte) ((pixel >> 8) & 0xFF));  // green
                      buffer.put((byte) (pixel & 0xFF));         // blue
               }
            }
           
            buffer.flip();
           
            //TODO Add code to convert ByteBuffer to OpenGL Texture
           
         }
         
      } catch (IOException e) {
         e.printStackTrace();
      }
   }


I'll keep looking into this on my own and will update this thread with the solution if I find one.

Thanks in advance,

 | Nathan

p.s. For some reason the formatting gets messed up when I paste in from Eclipse; it looks perfect in there. Oh well.

Check out my website!
Offline davedes
« Reply #1 - Posted 2012-10-04 01:17:12 »

This is probably the easiest/best technique; the thread also outlines a few other suggestions.

(Not sure why GL_ALPHA_BIAS is necessary there...)


EDIT:
This assumes you already know how to set up an OpenGL texture, e.g. wrap mode, filtering, etc.

If you don't yet know these things, I'd suggest using Matthias Mann's texture loader instead of relying on AWT. Slick also has a texture loader, although not quite as powerful.

If you'd rather get to the nitty gritty and reinvent the wheel, here is an example:
http://www.java-gaming.org/topics/painless-way-of-loading-textures/25856/msg/224562/view.html#msg224562

Further reading for texturing in OpenGL:
http://www.gamedev.net/page/resources/_/technical/opengl/opengl-texture-mapping-an-introduction-r947
http://www.flipcode.com/archives/Advanced_OpenGL_Texture_Mapping.shtml

Modern approaches:
http://open.gl/textures
http://www.arcsynthesis.org/gltut/

Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #2 - Posted 2012-10-04 08:54:48 »

1  
    GL11.glTexImage2D(target, 0, dstPixelFormat, image.getWidth(), image.getHeight(), 0, srcPixelFormat, GL11.GL_UNSIGNED_BYTE, buffer); 


Just be sure the image dimensions are an power of 2 number.
ex: 2 4 8 16 32... etc
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nsigma
« Reply #3 - Posted 2012-10-04 09:51:45 »

(Not sure why GL_ALPHA_BIAS is necessary there...)

It's there if you're using a BufferedImage of type INT_RGB, and grabbing the data array directly - ie.

1  
int[] data = ((DataBufferInt) image.getRaster().getDataBuffer()).getData()


This is faster then using getRGB(), but the data will have a zero alpha channel, hence using alpha bias to correct it.  Minor use case which I should probably have explained a bit more.  In some cases you could also just flag the texture and be careful about how you use blending when drawing it, or just upload to an opaque texture (assuming the alpha channel is ignored in this case?).

Just be sure the image dimensions are an power of 2 number.
ex: 2 4 8 16 32... etc

Or have an OpenGL 2+ capable card?!  Wink

Praxis LIVE - hybrid visual IDE for (live) creative coding
Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (2 views)
2017-11-21 16:10:57

Ecumene (113 views)
2017-09-30 02:57:34

theagentd (148 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (241 views)
2017-08-02 12:18:09

Sralse (256 views)
2017-07-25 17:13:48

Archive (874 views)
2017-04-27 17:45:51

buddyBro (1026 views)
2017-04-05 03:38:00

CopyableCougar4 (1578 views)
2017-03-24 15:39:42
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!