Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Converting TextureLoader to JSR231  (Read 1933 times)
0 Members and 1 Guest are viewing this topic.
Offline zingbat

Senior Duke




Java games rock!


« Posted 2006-04-07 22:02:40 »

Im trying to convert the code from SpaceInvaders103 tutorial to JSR213 and the only problem i have is with TextureLoader.java and Texture.java

I get this exceptions when i try to run spaceinvaders and honestly the error messages in jogl could be a bit more friendly. Any clue what this means:

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  
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Required 131072 remaining bytes in buffer, only had 0
   at com.sun.gluegen.runtime.BufferFactory.rangeCheckBytes(BufferFactory.java:274)
   at com.sun.opengl.impl.GLImpl.glTexImage2D(GLImpl.java:19579)
   at org.newdawn.spaceinvaders.jogl.TextureLoader.getTexture(TextureLoader.java:155)
   at org.newdawn.spaceinvaders.jogl.TextureLoader.getTexture(TextureLoader.java:97)
   at org.newdawn.spaceinvaders.jogl.JoglSprite.<init>(JoglSprite.java:35)
   at org.newdawn.spaceinvaders.ResourceFactory.getSprite(ResourceFactory.java:116)
   at org.newdawn.spaceinvaders.Game.initialise(Game.java:89)
   at org.newdawn.spaceinvaders.jogl.JoglGameWindow.init(JoglGameWindow.java:175)
   at com.sun.opengl.impl.GLDrawableHelper.init(GLDrawableHelper.java:71)
   at javax.media.opengl.GLCanvas$InitAction.run(GLCanvas.java:245)
   at com.sun.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:123)
   at javax.media.opengl.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:239)
   at javax.media.opengl.GLCanvas.display(GLCanvas.java:130)
   at javax.media.opengl.GLCanvas.paint(GLCanvas.java:142)
   at sun.awt.RepaintArea.paintComponent(Unknown Source)
   at sun.awt.RepaintArea.paint(Unknown Source)
   at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
   at java.awt.Component.dispatchEventImpl(Unknown Source)
   at java.awt.Component.dispatchEvent(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "Thread-2" javax.media.opengl.GLException: java.lang.NullPointerException
   at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:205)
   at javax.media.opengl.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:237)
   at javax.media.opengl.GLCanvas.display(GLCanvas.java:130)
   at com.sun.opengl.util.Animator.display(Animator.java:144)
   at com.sun.opengl.util.Animator$MainLoop.run(Animator.java:181)
   at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
   at org.newdawn.spaceinvaders.Game.frameRendering(Game.java:277)
   at org.newdawn.spaceinvaders.jogl.JoglGameWindow.display(JoglGameWindow.java:198)
   at com.sun.opengl.impl.GLDrawableHelper.display(GLDrawableHelper.java:77)
   at javax.media.opengl.GLCanvas$DisplayAction.run(GLCanvas.java:262)
   at com.sun.opengl.impl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:128)
   at javax.media.opengl.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java:279)
   at java.awt.event.InvocationEvent.dispatch(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)

Offline zingbat

Senior Duke




Java games rock!


« Reply #1 - Posted 2006-04-07 22:03:30 »

This is the code for TextureLoader.java

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  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
package org.newdawn.spaceinvaders.jogl;

import java.awt.Color;
import java.awt.Graphics;
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.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;

/**
 * A utility class to load textures for JOGL. This source is based
 * on a texture that can be found in the Java Gaming (www.java-gaming.org)
 * Wiki. It has been simplified slightly for explicit 2D graphics use.
 *
 * OpenGL uses a particular image format. Since the images that are
 * loaded from disk may not match this format this loader introduces
 * a intermediate image which the source image is copied into. In turn,
 * this image is used as source for the OpenGL texture.
 *
 * @author Kevin Glass
 */

public class TextureLoader {
    /** The table of textures that have been loaded in this loader */
    private HashMap table = new HashMap();
    /** The GL context used to load textures */
    private GL gl;
    /** The colour model including alpha for the GL image */
    private ColorModel glAlphaColorModel;
    /** The colour model for the GL image */
    private ColorModel glColorModel;
   
    /**
     * Create a new texture loader based on the game panel
     *
     * @param gl The GL content in which the textures should be loaded
     */

    public TextureLoader(GL gl) {
       this.gl = gl;
       
        glAlphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                                            new int[] {8,8,8,8},
                                            true,
                                            false,
                                            ComponentColorModel.TRANSLUCENT,
                                            DataBuffer.TYPE_BYTE);
                                           
        glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                                            new int[] {8,8,8,0},
                                            false,
                                            false,
                                            ComponentColorModel.OPAQUE,
                                            DataBuffer.TYPE_BYTE);
    }
   
    /**
     * Create a new texture ID
     *
     * @return A new texture ID
     */

    private int createTextureID()
    {
       int[] tmp = new int[1];
       gl.glGenTextures(1, tmp, 0);
       return tmp[0];
    }
   
    /**
     * Load a texture
     *
     * @param resourceName The location of the resource to load
     * @return The loaded texture
     * @throws IOException Indicates a failure to access the resource
     */

    public Texture getTexture(String resourceName) throws IOException {
        Texture tex = (Texture) table.get(resourceName);
       
        if (tex != null) {
            return tex;
        }
       
        tex = getTexture(resourceName,
                         GL.GL_TEXTURE_2D, // target
                         GL.GL_RGBA,     // dst pixel format
                         GL.GL_LINEAR, // min filter (unused)
                         GL.GL_LINEAR);
       
        table.put(resourceName,tex);
       
        return tex;
    }
   
    /**
     * Load a texture into OpenGL from a image reference on
     * disk.
     *
     * @param resourceName The location of the resource to load
     * @param target The GL target to load the texture against
     * @param dstPixelFormat The pixel format of the screen
     * @param minFilter The minimising filter
     * @param magFilter The magnification filter
     * @return The loaded texture
     * @throws IOException Indicates a failure to access the resource
     */

    public Texture getTexture(String resourceName,
                              int target,
                              int dstPixelFormat,
                              int minFilter,
                              int magFilter) throws IOException
    {
        int srcPixelFormat = 0;
       
        // create the texture ID for this texture
        int textureID = createTextureID();
        Texture texture = new Texture(target,textureID);
       
        // bind this texture
        gl.glBindTexture(target, textureID);
 
        BufferedImage bufferedImage = loadImage(resourceName);
        texture.setWidth(bufferedImage.getWidth());
        texture.setHeight(bufferedImage.getHeight());
       
        if (bufferedImage.getColorModel().hasAlpha()) {
            srcPixelFormat = GL.GL_RGBA;
        } else {
            srcPixelFormat = GL.GL_RGB;
        }

        // convert that image into a byte buffer of texture data
        ByteBuffer textureBuffer = convertImageData(bufferedImage,texture);
       
        if (target == GL.GL_TEXTURE_2D)
        {
           gl.glTexParameteri(target, GL.GL_TEXTURE_MIN_FILTER, minFilter);
            gl.glTexParameteri(target, GL.GL_TEXTURE_MAG_FILTER, magFilter);
        }
 
        // produce a texture from the byte buffer
        gl.glTexImage2D(target,
                      0,
                      dstPixelFormat,
                      get2Fold(bufferedImage.getWidth()),
                      get2Fold(bufferedImage.getHeight()),
                      0,
                      srcPixelFormat,
                      GL.GL_UNSIGNED_BYTE,
                      textureBuffer );
       
        return texture;
    }
   
    /**
     * Get the closest greater power of 2 to the fold number
     *
     * @param fold The target number
     * @return The power of 2
     */

    private int get2Fold(int fold) {
        int ret = 2;
        while (ret < fold) {
            ret *= 2;
        }
        return ret;
    }
   
    /**
     * Convert the buffered image to a texture
     *
     * @param bufferedImage The image to convert to a texture
     * @param texture The texture to store the data into
     * @return A buffer containing the data
     */

    private ByteBuffer convertImageData(BufferedImage bufferedImage,Texture texture) {
        ByteBuffer imageBuffer = null;
        WritableRaster raster;
        BufferedImage texImage;
       
        int texWidth = 2;
        int texHeight = 2;
       
        // find the closest power of 2 for the width and height
        // of the produced texture
        while (texWidth < bufferedImage.getWidth()) {
            texWidth *= 2;
        }
        while (texHeight < bufferedImage.getHeight()) {
            texHeight *= 2;
        }
       
        texture.setTextureHeight(texHeight);
        texture.setTextureWidth(texWidth);
       
        // create a raster that can be used by OpenGL as a source
        // for a texture
        if (bufferedImage.getColorModel().hasAlpha()) {
            raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,4,null);
            texImage = new BufferedImage(glAlphaColorModel,raster,false,new Hashtable());
        } else {
            raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,3,null);
            texImage = new BufferedImage(glColorModel,raster,false,new Hashtable());
        }
           
        // copy the source image into the produced image
        Graphics g = texImage.getGraphics();
        g.setColor(new Color(0f,0f,0f,0f));
        g.fillRect(0,0,texWidth,texHeight);
        g.drawImage(bufferedImage,0,0,null);
       
        // build a byte buffer from the temporary image
        // that be used by OpenGL to produce a texture.
        byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();

        imageBuffer = ByteBuffer.allocateDirect(data.length);
        imageBuffer.order(ByteOrder.nativeOrder());
        imageBuffer.put(data, 0, data.length);
       
        return imageBuffer;
    }
   
    /**
     * Load a given resource as a buffered image
     *
     * @param ref The location of the resource to load
     * @return The loaded buffered image
     * @throws IOException Indicates a failure to find a resource
     */

    private BufferedImage loadImage(String ref) throws IOException
    {
        URL url = TextureLoader.class.getClassLoader().getResource(ref);
       
        if (url == null) {
            throw new IOException("Cannot find: "+ref);
        }
       
        BufferedImage bufferedImage = ImageIO.read(new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(ref)));
 
        return bufferedImage;
    }
}


Offline zingbat

Senior Duke




Java games rock!


« Reply #2 - Posted 2006-04-07 22:04:11 »

And the code for Texture.java

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  
145  
146  
147  
148  
149  
150  
151  
package org.newdawn.spaceinvaders.jogl;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;

/**
 * A texture to be bound within JOGL. This object is responsible for
 * keeping track of a given OpenGL texture and for calculating the
 * texturing mapping coordinates of the full image.
 *
 * Since textures need to be powers of 2 the actual texture may be
 * considerably bigged that the source image and hence the texture
 * mapping coordinates need to be adjusted to matchup drawing the
 * sprite against the texture.
 *
 * @author Kevin Glass
 */

public class Texture {
    /** The GL target type */
    private int target;
    /** The GL texture ID */
    private int textureID;
    /** The height of the image */
    private int height;
    /** The width of the image */
    private int width;
    /** The width of the texture */
    private int texWidth;
    /** The height of the texture */
    private int texHeight;
    /** The ratio of the width of the image to the texture */
    private float widthRatio;
    /** The ratio of the height of the image to the texture */
    private float heightRatio;
   
    /**
     * Create a new texture
     *
     * @param target The GL target
     * @param textureID The GL texture ID
     */

    public Texture(int target,int textureID) {
        this.target = target;
        this.textureID = textureID;
    }
   
    /**
     * Bind the specified GL context to a texture
     *
     * @param gl The GL context to bind to
     */

    public void bind(GL gl) {
        gl.glBindTexture(target, textureID);
    }
   
    /**
     * Set the height of the image
     *
     * @param height The height of the image
     */

    public void setHeight(int height) {
        this.height = height;
        setHeight();
    }
   
    /**
     * Set the width of the image
     *
     * @param width The width of the image
     */

    public void setWidth(int width) {
        this.width = width;
        setWidth();
    }
   
    /**
     * Get the height of the original image
     *
     * @return The height of the original image
     */

    public int getImageHeight() {
        return height;
    }
   
    /**
     * Get the width of the original image
     *
     * @return The width of the original image
     */

    public int getImageWidth() {
        return width;
    }
   
    /**
     * Get the height of the physical texture
     *
     * @return The height of physical texture
     */

    public float getHeight() {
        return heightRatio;
    }
   
    /**
     * Get the width of the physical texture
     *
     * @return The width of physical texture
     */

    public float getWidth() {
        return widthRatio;
    }
   
    /**
     * Set the height of this texture
     *
     * @param texHeight The height of the texture
     */

    public void setTextureHeight(int texHeight) {
        this.texHeight = texHeight;
        setHeight();
    }
   
    /**
     * Set the width of this texture
     *
     * @param texWidth The width of the texture
     */

    public void setTextureWidth(int texWidth) {
        this.texWidth = texWidth;
        setWidth();
    }
   
    /**
     * Set the height of the texture. This will update the
     * ratio also.
     */

    private void setHeight() {
        if (texHeight != 0) {
            heightRatio = ((float) height)/texHeight;
        }
    }
   
    /**
     * Set the width of the texture. This will update the
     * ratio also.
     */

    private void setWidth() {
        if (texWidth != 0) {
            widthRatio = ((float) width)/texWidth;
        }
    }
}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ken Russell

JGO Coder




Java games rock!


« Reply #3 - Posted 2006-04-07 22:11:23 »

You probably need to rewind() a Buffer somewhere.
Offline zingbat

Senior Duke




Java games rock!


« Reply #4 - Posted 2006-04-08 11:16:22 »

You probably need to rewind() a Buffer somewhere.

I have added:

imageBuffer.rewind();

To this code in convertImageData method:

imageBuffer = ByteBuffer.allocateDirect(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);

It works.

So the old api was doing rewind autmatically and now we have to rewind any buffer argument we use before any gl call right?
Offline GKW

Senior Duke




Revenge is mine!


« Reply #5 - Posted 2006-04-08 15:43:01 »

Close.  The old version of JOGL always used an index of zero whether you wanted to use the data there or not.
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.

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

Norakomi (30 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (57 views)
2014-10-14 00:39:48

TehJavaDev (58 views)
2014-10-14 00:35:47

TehJavaDev (48 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
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!