Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  BufferedImage to lwjgl texture  (Read 1090 times)
0 Members and 1 Guest are viewing this topic.
Offline PaidGEEK

JGO n00b
*

Posts: 17
Medals: 1



« on: 2012-01-14 10:56:59 »

Hi guys!
I have a simple question about converting a bufferedimage from java.awt library to lwjgl Texture. Basically i want to bind this texture to opengl ("texture.bind()") after i do some cropping from the main image (using "image.getSubImage(x,y,width,height)" or cropImageFilter()"). It's supposed to make a few textures from a bigger image just like minecraft does for it's fonts and so on. So can anyone give me a snippet how could i do this?

Improvisational programmer.
Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #1 on: 2012-01-14 12:43:24 »

1. Load image with ImageIO or any other method.
2. Crop out the different parts you want to smaller BufferedImages using getSubImage(...)
For each sub image:
   
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
        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() * BYTES_PER_PIXEL); //4 for RGBA, 3 for RGB
       
        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 >> 16) & 0xFF));     // Red component
               buffer.put((byte) ((pixel >> 8) & 0xFF));      // Green component
               buffer.put((byte) (pixel & 0xFF));               // Blue component
               buffer.put((byte) ((pixel >> 24) & 0xFF));    // Alpha component. Only for RGBA
           }
        }

        buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS

        // You now have a ByteBuffer filled with the color data of each pixel.
       // Now just create a texture ID and bind it. Then you can load it using
       // whatever OpenGL method you want, for example:
       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8A, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

There is no god.
Offline PaidGEEK

JGO n00b
*

Posts: 17
Medals: 1



« Reply #2 on: 2012-01-14 19:09:56 »

I can't get it to work. I get alot of errors like invalid enum or null pointer location using various methods in opengl. At the end i tryed displaying this buffer with drawPixels() method which kinda worked but image was completly messed up. Can you please show me how to bind this buffer, to gl_quads for example, correctly? I don't know exactly what im doing couse i started learning java about a month ago. Thanks for your help!

Improvisational programmer.
Games published by our own members! Go get 'em!
Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #3 on: 2012-01-14 19:24:37 »

I can't get it to work. I get alot of errors like invalid enum or null pointer location using various methods in opengl. At the end i tryed displaying this buffer with drawPixels() method which kinda worked but image was completly messed up. Can you please show me how to bind this buffer, to gl_quads for example, correctly? I don't know exactly what im doing couse i started learning java about a month ago. Thanks for your help!

If you're new to Java, then I recommend you to stay away from OpenGL and stick to Java2D which is easier to use.

Anyway, if you are using LWJGL I can give you a working example program, but if you're using JOGL you'll have to adapt it slightly yourself. Which one are you using?

There is no god.
Offline PaidGEEK

JGO n00b
*

Posts: 17
Medals: 1



« Reply #4 on: 2012-01-14 19:49:22 »

Im using LWJGL. I mean i made some pretty cool stuff already in opengl using textures mapping i just didn't get the chance to learn using this buffers yet.

Improvisational programmer.
Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #5 on: 2012-01-14 21:05:07 »

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  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
package YOUR.PACKAGE.HERE;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL12;

import static org.lwjgl.opengl.GL11.*;

public class BasicTextureTest {
   
   private static final int WIDTH = 800, HEIGHT = 600;
   
   public static void main(String[] args){
     
      try{
         Display.setDisplayMode(new DisplayMode(800, 600));
         Display.create();
      }catch(LWJGLException e){
         e.printStackTrace();
      }
     
      glMatrixMode(GL_PROJECTION);
      glOrtho(0, WIDTH, HEIGHT, 0, -1, 1); //2D projection matrix
     glMatrixMode(GL_MODELVIEW);
     
      glClearColor(0, 1, 0, 0); //Green clear color
     
     
      //Generate a small test image by drawing to a BufferedImage
     //It's of course also possible to just load an image using ImageIO.load()
     BufferedImage test = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2d = test.createGraphics();

      g2d.setColor(new Color(1.0f, 1.0f, 1.0f, 0.5f));
      g2d.fillRect(0, 0, 128, 128); //A transparent white background
     
      g2d.setColor(Color.red);
      g2d.drawRect(0, 0, 127, 127); //A red frame around the image
     g2d.fillRect(10, 10, 10, 10); //A red box
     
      g2d.setColor(Color.blue);
      g2d.drawString("Test image", 10, 64); //Some blue text
     
      int textureID = loadTexture(test);
     
      glEnable(GL_TEXTURE_2D); //Enable texturing
     
     
      while(!Display.isCloseRequested()){
         glClear(GL_COLOR_BUFFER_BIT);
         
         //Enable blending so the green background can be seen through the texture
        glEnable(GL_BLEND);
         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
         
         glPushMatrix();
         glTranslatef(100, 100, 0);
         glBindTexture(GL_TEXTURE_2D, textureID);
         glBegin(GL_QUADS);
         {
            glTexCoord2f(0, 0);
            glVertex2f(0, 0);
           
            glTexCoord2f(1, 0);
            glVertex2f(128, 0);
           
            glTexCoord2f(1, 1);
            glVertex2f(128, 128);
           
            glTexCoord2f(0, 1);
            glVertex2f(0, 128);
         }
         glEnd();
         glPopMatrix();
         
         Display.update();
      }
   }
   
   private static final int BYTES_PER_PIXEL = 4;
   public static int loadTexture(BufferedImage image){
     
      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() * BYTES_PER_PIXEL); //4 for RGBA, 3 for RGB
       
        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 >> 16) & 0xFF));     // Red component
               buffer.put((byte) ((pixel >> 8) & 0xFF));      // Green component
               buffer.put((byte) (pixel & 0xFF));               // Blue component
               buffer.put((byte) ((pixel >> 24) & 0xFF));    // Alpha component. Only for RGBA
           }
        }

        buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS

        // You now have a ByteBuffer filled with the color data of each pixel.
       // Now just create a texture ID and bind it. Then you can load it using
       // whatever OpenGL method you want, for example:

      int textureID = glGenTextures(); //Generate texture ID
       glBindTexture(GL_TEXTURE_2D, textureID); //Bind texture ID
       
        //Setup wrap mode
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

        //Setup texture scaling filtering
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       
        //Send texel data to OpenGL
       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
     
        //Return the texture ID so we can bind it later again
     return textureID;
   }
}

You should be able to extract what you want from this program. Note that I generate an image by drawing to a BufferedImage.

There is no god.
Offline PaidGEEK

JGO n00b
*

Posts: 17
Medals: 1



« Reply #6 on: 2012-01-15 06:05:30 »

It works great! I load it to gluPerspective and it finaly works. Thank you so much! Smiley

Improvisational programmer.
Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #7 on: 2012-01-15 06:47:34 »

No problem! =D Increasing the OpenGL population is something I enjoy doing!

There is no god.
Offline roland

Full Member
**

Posts: 235
Medals: 6



« Reply #8 on: 2012-01-24 23:18:38 »

 Shocked I was just looking for this. Thanks theagentd!!!!  Grin

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.09 seconds with 20 queries.