Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
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 4509 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: 45
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  
 
 

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

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

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

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

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

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

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

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

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

nelsongames (2247 views)
2018-04-24 18:14:32
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

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