Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  JOGL 2D Texture Example  (Read 5448 times)
0 Members and 1 Guest are viewing this topic.
Offline niya

Junior Newbie





« Posted 2012-08-21 20:26:04 »

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:

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  
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.
Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #1 - Posted 2012-08-21 20:58:41 »

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

Also remember to disable them afterwards.

Myomyomyo.
Offline niya

Junior Newbie





« Reply #2 - Posted 2012-08-21 21:11:19 »

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

That's how I've setup the surface.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #3 - Posted 2012-08-21 21:12:25 »

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

Myomyomyo.
Offline niya

Junior Newbie





« Reply #4 - Posted 2012-08-21 21:20:28 »

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?
Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #5 - Posted 2012-08-21 21:25:08 »

Agh, silly me.
1  
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.

Myomyomyo.
Offline niya

Junior Newbie





« Reply #6 - Posted 2012-08-21 21:31:51 »

Agh, silly me.
1  
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.
Offline gouessej
« Reply #7 - Posted 2012-08-21 21:50:39 »

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:
https://github.com/sgothel/jogl-demos/blob/master/src/redbook/src/glredbook11/varray.java

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.

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.

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (29 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (76 views)
2014-11-29 21:32:03

toopeicgaming1999 (137 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!