crashes :(

hey guys :slight_smile:

I’m having some problems with random crashes, and hope someone here can help me out

My gameloop is not handled by an animator, autoswapbuffer mode is 0, and each tick (after processing game logic), drawable.display() is called which calls display(), which calls the draw callbacks (and loads textures etc). after stuff is rendered, the game loop sleeps until end of tick (1/60 second) and swaps the buffer.

the loading routine for the texture is this:


    private void makeTexture(GL gl) {

        // binds this texture, id is generated earlier
        bind(gl);
        
        int srcPixelFormat;
        if (bi.getColorModel().hasAlpha())
            srcPixelFormat = GL.GL_RGBA;
        else
            srcPixelFormat = GL.GL_RGB;

        // convert that image into a byte buffer of texture data
        ByteBuffer textureBuffer = convertImageData(bi, texWidth, texHeight);

        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);

        // produce a texture from the byte buffer
        gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, texWidth, texHeight, 0, srcPixelFormat, GL.GL_UNSIGNED_BYTE, textureBuffer);

    }

    private static ByteBuffer convertImageData(BufferedImage bi, int texWidth, int texHeight) {
        ByteBuffer imageBuffer = null;
        WritableRaster raster;
        BufferedImage texImage;

        // we need an ogl-compatible raster
        if (bi.getColorModel().hasAlpha()) {
            raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 4, null);
            texImage = new BufferedImage(glAlphaColorModel, raster, false, null);
        } else {
            raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 3, null);
            texImage = new BufferedImage(glColorModel, raster, false, null);
        }

        // copy the source image into the produced image
        Graphics2D g = (Graphics2D) texImage.getGraphics();
        g.setColor(new Color(0f, 0f, 0f, 0f));
        g.fillRect(0, 0, texWidth, texHeight);
        g.drawImage(bi, 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;
    }

texwidth and texheight are the closest potence of 2 of the image’s width/height, glColorModel/glAlphacolorModel are obviously the color models needed for the conversion, as you can see a good part of the code is taken from the known tutorials :stuck_out_tongue:

the crashes occur at the glTexImage2D call, error is this:

ideas, anyone? :-\

I have the same problem, if i try to load bitmaps grater than 256^2.
Try a smaller image size. I use 256^2 at the moment.

it’s 64x64 :frowning:

JOGL now uses the position and limit of the buffers to determine what to use. So unless you rewind the buffers, or in some other way set the position and limit, you’ll pass an invalid address to OpenGL. So always rewind buffers before calling gl functions.

yay, it doesn’t crash anymore :smiley:

is this common knowledge I just missed?? I mean, this invalidates all jogl tutorials etc and I didn’t find any note about this, not even in the release notes… might want to fix that to avoid more questions :frowning:

Doesn’t JOGL check this [buffer.remaining()] by itself, like LWJGL?

I think any native library should live by the rule of not crashing, whatever is passed as an argument.

I personally don’t like the check in LWJGL - I mean I am sure it’s a fast call, but it’s an unnecessary check if your code is correct. This is what things like the JOGL debug wrapper should be for (maybe it does test it?) …would be nice to have one of those for LWJGL too - then it’s the best of both worlds.

it you really want without the checks, lwjgl can be told to generate without those - we just only supply the “debug” libs, because the hit is very low.