JOGL 2D Texture Example

I’m sorry, but I’ve been trying to find an example of how to do 2D textures with JOGL and just keep failing. My code looks like this:


In reshape:
        gl.glViewport(0, 0, width, height); // Reset The Current Viewport
        gl.glMatrixMode(GL2.GL_PROJECTION); // Select The Projection Matrix
        gl.glLoadIdentity(); // Reset The Projection Matrix
        // hard coded values - this needs to change based on screen size
        gl.glOrthof(0, GL_WIDTH, GL_HEIGHT, 0, -1, 1);
        gl.glMatrixMode(GL2.GL_MODELVIEW); // Select The Modelview Matrix
        gl.glLoadIdentity(); // Reset The Modelview Matrix

In draw:
        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        gl.glClear(GL2.GL_DEPTH_BUFFER_BIT | GL2.GL_COLOR_BUFFER_BIT);

        gl.glEnable(GL2.GL_TEXTURE_2D);
        gl.glEnable(GL2.GL_BLEND);
        gl.glBlendFunc(GL2.GL_ONE, GL2.GL_ONE_MINUS_SRC_ALPHA);
        gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
        gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
        gl.glLoadIdentity();

            final Texture mTest = TextureIO.newTexture(getClass().getResource("/images/glass.png"), false, TextureIO.PNG);
            final float[] coordData = {
                    0, 0, //
                    0, 1, //
                    1, 0, //
                    1, 1,
            };
            final float[] vertices = {
                    0.0f, 0.0f, 0, // Left Bottom
                    0.0f, 128, 0, // Left Top
                    128, 0.0f, 0, // Right Bottom
                    128, 128, 0
            };
            // Setup the vertices into the buffer
            verts = Buffers.newDirectFloatBuffer(vertices.length);
            verts.put(vertices).position(0);

            // Setup the texture coordinates
            coords = Buffers.newDirectFloatBuffer(coordData.length);
            coords.put(coordData).position(0);

            mTest.enable(gl);
            mTest.bind(gl);
            gl.glLoadIdentity();
            gl.glTranslatef(100, 100, 0);
            gl.glVertexPointer(3, GL2.GL_FLOAT, 0, verts);
            gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, coords);
            gl.glDrawArrays(GL2.GL_TRIANGLE_STRIP, 0, 4);

I get a “Invalid memory access of location 0x0 rip=0x11cfae8ae” when it executes gl.glDrawArrays, and I just don’t understand what I’m doing wrong.

I’m almost positive I have the latest version, as I donwloaded from http://jogamp.org/ just the other day.

Any help would be greatly appreciated.

Thank you.

gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
gl.glEnableClientState(gl.GL_TEX_COORD_ARRAY);

Also remember to disable them afterwards.

Sorry, I missed some of the initialization steps in the copy+paste.

That’s how I’ve setup the surface.

Then the crash might be because you don’t disable them later.

When do I have to disable them? Before glDrawArrays? Because if I don’t call glDrawArrays, it doesn’t crash, but, of course, nothing is displayed. If I call glDrawArrays, it crashes, but it feels… like calling disable before drawing wouldn’t work?

Agh, silly me.

gl.glVertexPointer(3, GL2.GL_FLOAT, 0, verts);

Where are your 3D coordinates, mate? It reads outside the bounds of the vertex array since it assumes that it has 3 coordinates per vertex, not 2. Change the 3 to 2 and it should work.

Updated, but still the same problem.

Since it’s saying it’s accessing memory address 0x0, I don’t think the bounds problem would be it? It’s like it’s not even getting the buffers in the first place.

Hi

There are several wrong things in your code. You should not recreate the direct NIO buffers at each draw.

Edit.: There is an example here:

Edit2.: theagentd is right, you have to call gl.glEnableClientState(GL2.GL_TEX_COORD_ARRAY); but don’t call gl.glEnableClientState(GL2.GL_COLOR_ARRAY); as you don’t provide colors in your vertex arrays, the main problem come from that line.

Edit3.: Rather post on the official JogAmp forum, you will probably get more replies.