views:

698

answers:

2

hi

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 this impose to mean ?? and how can I solve it ?

by the way 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

but nothing seems to work :(

+1  A: 

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
Nour
@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
A: 

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