2

I've been trying to load a bmp picture to use it as a texture at my program I've used a IOStream class to extend DataInputStream to read the pixels at the photo with this code based on a texture loader code for C++:

//class Data members
public static int BMPtextures[];
public static int BMPtexCount = 30;
public static int currentTextureID = 0;
//loading methode
static int loadBMPTexture(int index, String fileName, GL gl)
    {
        try
        {
            IOStream wdis = new IOStream(fileName);
            wdis.skipBytes(18);
            int width = wdis.readIntW(); 
            int height = wdis.readIntW();
            wdis.skipBytes(28);
            byte buf[] = new byte[wdis.available()];
            wdis.read(buf);
            wdis.close();
            gl.glBindTexture(GL.GL_TEXTURE_2D, BMPtextures[index]);
            gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, 3, width, height, 0, GL.GL_BGR, GL.GL_UNSIGNED_BYTE, buf);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
            gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
            currentTextureID = index; 
            return currentTextureID;
    }
        catch (IOException ex)
        {
            // Utils.msgBox("File Error\n" + fileName, "Error", Utils.MSG_WARN);
            return -1;
        }
    }

and IOStream code :

public class IOStream extends DataInputStream {

    public IOStream(String file) throws FileNotFoundException {
        super(new FileInputStream(file));
    }

    public short readShortW() throws IOException {
        return (short)(readUnsignedByte() + readUnsignedByte() * 256);
    }

    public int readIntW() throws IOException {
        return readShortW() + readShortW() * 256 * 256;
    }

    void read(Buffer[] buf) {

    }
}

and the calling:

GTexture.loadBMPTexture(1,"/BasicJOGL/src/basicjogl/data/Font.bmp",gl);

after debugging I figured out that when it come to this line:

IOStream wdis = new IOStream(fileName);

an IOExeption occurred and it's a DispatchException What's this supposed to mean, and how can I solve it?

I tried to:

  1. use \ and \\ and / and //
  2. change the path of the photo and take all the path from c:\ to the photoname.bmp
  3. rename the photo using numbers like 1.bmp

None worked.

Nour
  • 21
  • 1
  • 3
  • From reading this I can't tell if the texture is located in a jar or the filesystem. Can you elaborate on this? – Jason Nichols Jan 21 '10 at 18:16
  • in a fileSystem I've created a Package "data" and added the photo to it but after changing the loader code it works and loads the texture perfectly "I tried to print the info of the pic and it works ".. but then when it came to binding the texture nothing happened it draws a white square !! – Nour Jan 22 '10 at 09:01

3 Answers3

1

Judging by your latest comment, you are no longer getting the IOException but are still having troubles getting the texture to actually render (just getting a white square).

I noticed the following are not in the code you posted here (but could be elsewhere):

gl.glGenTextures 

You need to generate places for your textures before binding them. Also, make sure you have enabled texturing:

gl.glEnable(GL.GL_TEXTURE2D);

For additional information / tutorials on getting started with OpenGL texturing, I recommend taking a read of NeHe Productions: OpenGL Lesson #06. Also, down the bottom of the page you will find JOGL sample code to help you convert the concepts from C to Java.

Anyway, hope this gives a few new ideas to try.

Clinton
  • 3,638
  • 3
  • 26
  • 33
  • unfortunately .. it's written somewhere else & also the enabling line i asked some friend and they told me that this maybe causes by a hardware problem at my computer graphics card is that possible ? and if this is the problem is there anyway to solve it ? – Nour Jan 24 '10 at 11:27
  • @Nour - Assuming you are running windows or OSX, you can try the OpenGL Extensions viewer to see exactly what your video card supports: http://www.realtech-vr.com/glview/ If you have an old video card I would recommend attempting to load a texture with dimensions exactly of 256*256 pixels. – Clinton Jan 24 '10 at 23:41
0

Here is a simple way of loading a texture in JOGL. It works with BMP as well.

public static Texture loadTexture(String file) throws GLException, IOException
{
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(ImageIO.read(new File(file)), "png", os);
    InputStream fis = new ByteArrayInputStream(os.toByteArray());
    return TextureIO.newTexture(fis, true, TextureIO.PNG);
}

also dont forget to enable and bind, and set texture-coordinates.

...
  gl.glEnableClientState(GL2ES1.GL_TEXTURE_COORD_ARRAY);
  if(myTexture == null)  
    myTexture = loadTexture("filename.png");
  myTexture.enable(gl);
  myTexture.bind(gl);
  gl.glTexCoordPointer(2, GL2ES1.GL_FLOAT, 0, textureCoords);
...
dac2009
  • 3,521
  • 1
  • 22
  • 22
0

Probably don't need help on this anymore, but I noticed that IOStream extends DataInputStream but when it comes to actually implementing read() it's been left blank. so regardless you're never actually reading anything into buf which might explain why your texture is blank but you don't get any other problems.

badcodenotreat
  • 226
  • 4
  • 11