Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
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 Loading Textures..  (Read 236 times)
0 Members and 1 Guest are viewing this topic.
Offline trollwarrior1
« Posted 2014-02-07 07:57:04 »

So since the beginning of my time with LWJGL I have always struggled and couldn't make my own texture loader.. So I found some already made one and modified it for my own needs.

Today I decided its finally time to figure out how to make my own texture loader.

So I made wrote this code and it doesn't display any texture. Any ideas what I could be doing wrong? The problem is probably somewhere at glTexImage2d


Ummmm this is the code of my texture loader that actually works.
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  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.swing.ImageIcon;

import org.lwjgl.opengl.GL11;

public class SpriteSheet {

   private static List<SpriteSheet> sheets = new ArrayList<SpriteSheet>();
   private static ColorModel glAlphaColorModel = new ComponentColorModel(
         ColorSpace.getInstance(ColorSpace.CS_sRGB),
            new int[] {8,8,8,8},
            true,
            false,
            ComponentColorModel.TRANSLUCENT,
            DataBuffer.TYPE_BYTE);
   private static int target = GL11.GL_TEXTURE_2D;
   

   public static SpriteSheet sheet = new SpriteSheet("/image.png");
   
   public int id = 0;
   public int width, height;
   public String name;
   
   public SpriteSheet(String name) {
     
      this.name = name;
     
      try {
     
         BufferedImage image = loadBufferedImage(name);
         ByteBuffer imageByteBuffer = convertImageData(image);
         
         id = GL11.glGenTextures();
         
         bind();
         
         int filter = GL11.GL_NEAREST;
         GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, filter);
         GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, filter);
         
         this.width = image.getWidth();
         this.height = image.getHeight();
         
         GL11.glTexImage2D(
               target,
               0,
               GL11.GL_RGBA,
               image.getWidth(),
               image.getHeight(),
               0,
               GL11.GL_RGBA,
               GL11.GL_UNSIGNED_BYTE,
               imageByteBuffer);
         
         
         sheets.add(this);
     
      }catch(IOException e) {
         e.printStackTrace();
      }
   }
   
   public SpriteSheet(int id, int width, int height) {
      this.id = id;
      this.width = width;
      this.height = height;
      this.name = "none";
   }
   
   @SuppressWarnings("rawtypes")
   private ByteBuffer convertImageData(BufferedImage image){
     
      ByteBuffer textureByteBuffer = null;
      WritableRaster raster = null;
      BufferedImage bufImage = null;
     
      raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, image.getWidth(), image.getHeight(), 4, null);
      bufImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable());
     
      Graphics g = bufImage.getGraphics();
      g.drawImage(image, 0, 0, null);
     
      byte[] imageBytes = ((DataBufferByte) bufImage.getRaster().getDataBuffer()).getData();
     
      textureByteBuffer = ByteBuffer.allocateDirect(imageBytes.length);
      textureByteBuffer.order(ByteOrder.nativeOrder());
      textureByteBuffer.put(imageBytes, 0, imageBytes.length);
      textureByteBuffer.flip();
     
      return textureByteBuffer;
   }
   
   private BufferedImage loadBufferedImage(String name) throws IOException {
     
      URL url = SpriteSheet.class.getResource(name);
     
      Image img = new ImageIcon(url).getImage();
     
      BufferedImage image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
       
      Graphics g = image.getGraphics();
     
      g.drawImage(img, 0, 0, image.getWidth(), image.getHeight(), null);
     
      g.dispose();
     
      int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
     
//      System.out.println(pixels[0]);
     
      Color color = new Color(0, 0, 0, 0);
      for(int i=0;i<pixels.length;i++){
         if(pixels[i] == -65281 || pixels[i] == -2719745) image.setRGB(i % image.getWidth(), i / image.getWidth(), color.getRGB()); // Replace certain colors with alpha
      }
     
      return image;
     
   }
   
   
   public void bind() {
      GL11.glBindTexture(target, id);
   }
   
}






This is the code of the one I'm trying to write. It really does seem a lot simpler to go with intbuffer, but I have no idea why it doesn't work..
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  
public class SpriteSheet {

   public static final int target = GL_TEXTURE_2D;

   public static SpriteSheet sheet = new SpriteSheet("/image.png");
   
   public String name;
   public int width, height;
   public int handle = 0;
   
   public SpriteSheet(String name) {
      this.name = name;
      load();
   }
   
   public void load() {
     
      handle = glGenTextures();
      bind();
     
      BufferedImage image = loadImage(name);
      this.width=image.getWidth();
      this.height=image.getHeight();
      IntBuffer data = convertImageData(image);
     
      glTexImage2D(target, 0, GL_INT, width, height, 0, GL_INT, GL_INT, data);
     
   }
   
   public void bind() {
      glBindTexture(target, handle);
   }
   
   private IntBuffer convertImageData(BufferedImage image) {
     
      IntBuffer buffer=BufferUtils.createIntBuffer(width*height);
     
      for(int y=0;y<height;y++) {
         for(int x=0;x<width;x++) {
            buffer.put(image.getRGB(x, y));
         }
      }
     
      buffer.flip();
     
      return buffer;
   }
   
   private BufferedImage loadImage(String name) {
      try {
         return ImageIO.read(SpriteSheet.class.getResource(name));
      } catch (IOException e) {
         e.printStackTrace();
         return null;
      }
   }
   

}
Offline trollwarrior1
« Reply #1 - Posted 2014-02-07 08:16:27 »

Ow yeeeeeeeeee I have actually done it somehow Cheesy No idea how Cheesy Here is the working code.

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  
public class SpriteSheet {

   public static final int target = GL_TEXTURE_2D;

   public static SpriteSheet sheet = new SpriteSheet("/image.png");

   public String name;
   public int width, height;
   public int handle = 0;

   public SpriteSheet(String name) {
      this.name = name;
      load();
   }

   public void load() {

      handle = glGenTextures();
      bind();

      BufferedImage image = loadImage(name);
      this.width = image.getWidth();
      this.height = image.getHeight();
      ByteBuffer data = convertImageData(image);

      int filter = GL_NEAREST;
      glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
      glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);

      glTexImage2D(target, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

   }

   public void bind() {
      glBindTexture(target, handle);
   }

   private ByteBuffer convertImageData(BufferedImage image) {

      ByteBuffer byteBuffer = ByteBuffer.allocateDirect(width * height * 4);

      byte[] bytes = new byte[width * height * 4];

      for (int y = 0; y < height; y++) {
         for (int x = 0; x < width; x++) {
            int color = image.getRGB(x, y);
            int i = (x + y * width) * 4;
            bytes[i + 0] = (byte) ((color >> 16) & 0xff);
            bytes[i + 1] = (byte) ((color >> 8) & 0xff);
            bytes[i + 2] = (byte) ((color) & 0xff);
            bytes[i + 3] = (byte) ((color >> 24) & 0xff);
         }
      }

      byteBuffer.put(bytes);

      byteBuffer.rewind();

      return byteBuffer;
   }

   private BufferedImage loadImage(String name) {
      try {
         return ImageIO.read(SpriteSheet.class.getResource(name));
      } catch (IOException e) {
         e.printStackTrace();
         return null;
      }
   }

}
Offline nerb
« Reply #2 - Posted 2014-02-07 09:19:26 »

Just for reference, I think your initial problem was with using GL_INT in the glTexImage2D() method. (I discovered this myself the hard way a month or two ago).

GL_INT appears to tell OpenGL to expect an int per channel, per pixel. I.e. Four ints per pixel when using RGBA. In your case, your colours were still packed in to a single int. As such, I'm guessing you would've got an error when calling glTexImage2D() about the size of your buffer? It would've been less than what it was expecting. Obviously now that you are unpacking the bytes yourself, you aren't running in to this problem. (Note that even if you could push packed ints through to OpenGL, Java's BufferedImage.getRGB() spits out colours as ARGB, not the RGBA that OpenGL is expecting).

Anyway, good job solving your problem.
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.

DarkCart (15 views)
2014-10-31 21:44:48

DarkCart (19 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (30 views)
2014-10-27 03:27:51

DarkCart (44 views)
2014-10-26 19:37:11

Luminem (27 views)
2014-10-26 10:17:50

Luminem (30 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

Longarmx (64 views)
2014-10-17 03:59:02

Norakomi (62 views)
2014-10-16 15:22:06
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!