Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Basic Textures In JOGl  (Read 7616 times)
0 Members and 1 Guest are viewing this topic.
Offline chamster

Senior Newbie




Me - The Chamster


« Posted 2005-09-24 10:38:27 »

I'm trying to put some textures in my 3D-world and the problem i encounter is that all the info i find seems to be rather advanced. Now, i understand that's the most convienient way when doing some serious work. However, at this point, i'd like to simply get my "grass.gif" and put it visible somewhere in my world. From there, i can start laborating.

So - anybody willing to point me to a the source code for the *most simple* operation that loads an image from a file (i'm guessing it will be an instance of BufferedImage) and actually put that thing into an existing and working "display-method"?
Offline Ken Russell

JGO Coder




Java games rock!


« Reply #1 - Posted 2005-09-25 01:39:09 »

Have you looked at the JOGL demos? Most of them load textures in some format or another and create an OpenGL texture from the result.
Offline chamster

Senior Newbie




Me - The Chamster


« Reply #2 - Posted 2005-09-25 11:45:33 »

Yes, of course. That was the very first thing. The thing is that i got more confused by it. There's too much to digest in there. I'm looking for a minimal example of how to get from a graphical file to a "something" on the screen. From there i can work by myself.

As it is now, i understand i need three steps.
1. Read from a file to a BufferedImage.
2. Convert BufferedImage-object as an array of byte.
3. Represent the contents of the array on the screen.
Is the above correct?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ken Russell

JGO Coder




Java games rock!


« Reply #3 - Posted 2005-09-25 16:13:20 »

Look at makeRGBTexture() in the VertexProgRefract demo. This shows how to load the BufferedImage into an OpenGL texture object. Once you have your OpenGL texture you just need to draw a pair of triangles with the appropriate texture coordinates in order to get it on the screen.
Offline girvine

Junior Member




Java games rock!


« Reply #4 - Posted 2005-09-26 11:01:48 »

There's also an entire thread on this forum somewhere covering a TextureManager for loading and managing textures.  It was being extended some time ago to include avi and quicktime stuff as well but it's been a quiet thread for some time.
Offline endolf

JGO Coder


Medals: 7
Exp: 15 years


Current project release date: sometime in 3003


« Reply #5 - Posted 2005-09-26 11:29:13 »

Hi

Jogl is a binding for OpenGL, NeHe has a large range of good OpenGL tutorials, articles and examples.

HTH

Endolf

Offline nnevatie

Junior Member




It is slightly better to be simple than correct.


« Reply #6 - Posted 2005-09-26 19:42:49 »

To get you going with super-simple texture loading, you could try out this:

http://www.g0dmode.com/javastuff/jogl-textureio.zip

Use the TextureIO class to load textures from files. Then call initialize and bind methods for the created texture to use it.



Awards:
- Nobel Prize in Physics for inventing his Doomsday Machine
- Nobel Peace Prize for not using it

http://www.g0dmode.com -- a collection of things not real
Offline chamster

Senior Newbie




Me - The Chamster


« Reply #7 - Posted 2005-09-28 16:39:57 »

Thanks for the replies. I'm still not there yet but on the way. Just a little bit more of the whining and i'll be happy. So, let's whine.

Whine #1
I was checking The Blue Book to find glBindTexture and guess what - no such thing as far as i can see. What's up with that?

Whine#2
I have my byte[] but i still get nada on the screen. My nada is created by this code.
...
byte[] data = ((DataBufferByte) img.getRaster ().getDataBuffer ()).getData ();
this.gl.glTexImage2D (target, 0, GL.GL_RGB, img.getWidth (), img.getHeight (), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, data);
...
this.gl.glBegin (GL.GL_TRIANGLE_STRIP);
this.loadPNGFile ("grass.png");
this.gl.glEnable (GL.GL_TEXTURE_2D);
this.gl.glBindTexture (GL.GL_TEXTURE_2D, 0);
this.gl.glTexCoord3d (0, 0, 0);
this.gl.glTexCoord3d (30, 0, 0);
this.gl.glTexCoord3d (0, 30, 0);
this.gl.glTexCoord3d (30, 30, 0);
this.gl.glEnd ();
...
I have tried the above with and without drawing triangle using glVertex3d and enabling/disabling GL_TEXTURE_2D. I also tried to scream a little bit until the neibs started yelling.   Smiley

Whine #3
What did you mean by "a pair of triangles with the appropriate texture coordinates"? I assumed i had to draw the triangles and then SOMEHOW put the texture on them. Now, that SOMEHOW is a little bit unclear to me. I looked into examples you mentioned and it helped this far but no further, i'm affraid.

Whine #4
How do i go from having a byte[] (size 256^2) to a graphical representation on the screen (i tried the glBindTexture(...) but it's still something wrong there). Please reply as if you were talking to a retarded banana. Rest assured you won't hit very far from the truth in that aim.   Smiley

Offline tom
« Reply #8 - Posted 2005-09-28 19:40:00 »

Thanks for the replies. I'm still not there yet but on the way. Just a little bit more of the whining and i'll be happy. So, let's whine.

Whine #1
I was checking The Blue Book to find glBindTexture and guess what - no such thing as far as i can see. What's up with that?

Looks like the Blue Book that is online on opengl.org is so old that it don't contain glBindTexture. Might have been a 1.1 feature. Anyway, here is a link to slightly more complete doc:
OpenGL Man Pages

Quote
Whine#2
I have my byte[] but i still get nada on the screen. My nada is created by this code.
...
byte[] data = ((DataBufferByte) img.getRaster ().getDataBuffer ()).getData ();
this.gl.glTexImage2D (target, 0, GL.GL_RGB, img.getWidth (), img.getHeight (), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, data);
...
this.gl.glBegin (GL.GL_TRIANGLE_STRIP);
this.loadPNGFile ("grass.png");
this.gl.glEnable (GL.GL_TEXTURE_2D);
this.gl.glBindTexture (GL.GL_TEXTURE_2D, 0);
this.gl.glTexCoord3d (0, 0, 0);
this.gl.glTexCoord3d (30, 0, 0);
this.gl.glTexCoord3d (0, 30, 0);
this.gl.glTexCoord3d (30, 30, 0);
this.gl.glEnd ();
...
I have tried the above with and without drawing triangle using glVertex3d and enabling/disabling GL_TEXTURE_2D. I also tried to scream a little bit until the neibs started yelling.   Smiley

That code don't make much sense. Please read the Red Book. Any help we try to give will be useless unless you know the basics. In your case the Texture Mapping chapter would be the most important one. Although the online version is missing some important parts.

1) "this.loadPNGFile ("grass.png");" should be done once at startup, not in the rendering loop.
2) Nothing is drawn unless you call glVertex. It is at this call the data is copied.
3) Use 1 as texture coordinates instead of 30. Texture coordinates is normailized and 1 means the full width or height.
 
Quote
Whine #3
What did you mean by "a pair of triangles with the appropriate texture coordinates"? I assumed i had to draw the triangles and then SOMEHOW put the texture on them. Now, that SOMEHOW is a little bit unclear to me. I looked into examples you mentioned and it helped this far but no further, i'm affraid.

Textures has its own coordinate system that you have to use to tell what part of the texture you will use. This is explained in the Red Book, I'm sure.

Quote
Whine #4
How do i go from having a byte[] (size 256^2) to a graphical representation on the screen (i tried the glBindTexture(...) but it's still something wrong there). Please reply as if you were talking to a retarded banana. Rest assured you won't hit very far from the truth in that aim.   Smiley

First the texture uploaded to the card. Since you no longer have the data, you use a id/name instead. This is what glBindTexture does. It sets what texture id to use.

To use textures in opengl you do:

At startup:
1) bind a id with glBindTexture. This can be hardcoded or you can get a unused id with glGenTextures.
2) Upload the texture data to the card using glTexImage2D. The texture is now on the card and is bound the the id you used in 1)

When rendering:
1) Bind the id of the texture you want to use.
2) specify the triangles with texture coordinates


Offline pepijnve

Junior Member




Java games rock!


« Reply #9 - Posted 2005-09-30 11:24:51 »

I second Endolf's advice regarding the nehe stuff. I suggest you check out the demos on drawing a polygon, drawing 3D shapes and texture mapping in particular.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline loic_sephiroth

Junior Newbie





« Reply #10 - Posted 2005-10-01 00:57:57 »

Hi there,

I'm using the Texture and TextureIO classes pointed by nnevatie as in
Quote
To get you going with super-simple texture loading, you could try out this:

http://www.g0dmode.com/javastuff/jogl-textureio.zip

Use the TextureIO class to load textures from files. Then call initialize and bind methods for the created texture to use it.

So, i load the png image into a Texture object. I then put the texture object into a HashMap (to be able to give a string name to the texture when searching for it), and i then use this hashmap when i'm doing the binding before drawing a quad that will contain the texture. However, when the application runs, i only get a black square, no texture rendered.
The code is:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public void display(GLDrawable drawable)
    {
        GL gl = drawable.getGL();

        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();

        //We draw the alien
       ((Texture)this.texturesTable.get("ship")).bind(gl);
        gl.glBegin(GL.GL_QUADS);
        {
           gl.glTexCoord2f(0.0f,1.0f);
           gl.glVertex3i(500,700,0);
           gl.glTexCoord2f(1.0f,1.0f);
           gl.glVertex3i(700,700,0);            
           gl.glTexCoord2f(1.0f,0.0f);
           gl.glVertex3i(700,500,0);
           gl.glTexCoord2f(0.0f,0.0f);
            gl.glVertex3i(500,500,0);
        }gl.glEnd();        
    }


The init code is
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  
   public void init(GLDrawable drawable)
    {
       GL gl = drawable.getGL();
       GLU glu = drawable.getGLU();
       
       //GL init
      {                
          gl.glClearColor(1.0f,1.0f,1.0f,1.0f);
           
          gl.glColor3f(0f,0f,0f);
           gl.glPointSize(4.0f);
           
           gl.glEnable(gl.GL_TEXTURE_2D);          
       }
       
        //Texture loading
       {
           try
           {
              Texture ship = TextureIO.createTexture("ship.png");
              ship.initialize(gl,glu);
              this.texturesTable.put("ship",ship);
              Texture alien = TextureIO.createTexture("alien.gif");
              alien.initialize(gl,glu);
              this.texturesTable.put("alien",alien);
           }
           catch(IOException e){System.out.println("Error while loading textures: "+e.getMessage());}
        }
       
    }    


¿What may the problem be? ¿Why shouldn't it render correctly the ship image?
Offline loic_sephiroth

Junior Newbie





« Reply #11 - Posted 2005-10-01 10:10:29 »

Well, i have been doing some tests this morning, and if i add color to all vertices of the quad, the image (of the ship) gets displayed within the quad...

1  
2  
3  
4  
5  
6  
7  
8  
        ((Texture)this.tablaTexturas.get("ship")).bind(gl);
        gl.glBegin(GL.GL_QUADS);
        {
           gl.glColor3f(1.0f,1.0f,1.0f); gl.glTexCoord2f(0.0f,1.0f); gl.glVertex2i(500,550);
           gl.glColor3f(1.0f,1.0f,1.0f); gl.glTexCoord2f(1.0f,1.0f); gl.glVertex2i(550,550);            
           gl.glColor3f(1.0f,1.0f,1.0f); gl.glTexCoord2f(1.0f,0.0f); gl.glVertex2i(550,500);
           gl.glColor3f(1.0f,1.0f,1.0f); gl.glTexCoord2f(0.0f,0.0f); gl.glVertex2i(500,500);
        }gl.glEnd();


The color here acts as some kind of light source, that doesn't show up the texture if not activated. I haven't read anywhere, that i should add color to the vertices of the polygon i'm rendering the texture in, in order to render the texture itself, so...¿does someone know why this is happening?
Offline nnevatie

Junior Member




It is slightly better to be simple than correct.


« Reply #12 - Posted 2005-10-01 10:12:30 »

¿What may the problem be? ¿Why shouldn't it render correctly the ship image?

I think that the call gl.glColor3f(0f,0f,0f) causes them to appear black - this happens because of the texture-color modulation.

Simplest solution is to set the color to white (1, 1, 1).
You may also disable the texture modulation with the following code:

1  
gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE); 

Awards:
- Nobel Prize in Physics for inventing his Doomsday Machine
- Nobel Peace Prize for not using it

http://www.g0dmode.com -- a collection of things not real
Offline loic_sephiroth

Junior Newbie





« Reply #13 - Posted 2005-10-01 11:05:56 »

Thanks a lot for the answer, nnevatie, it worked perfectly. The texture-color modulation disabling, i mean.

The images are in png format. ¿Do you know where i should look to get the transparent parts of the texture not rendered in black?

I'm trying the blending approach with, in my init code:
1  
2  
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ONE);


yet when i enable the BlendFunc, the texture does not get rendered. In fact, the whole quad doesn't get rendered.

I've tried to put a color4f function before the rendering of the quad, with the alpha componente set to 1.0f, 0.0f, 0.5f, and it doesn't seems to have any effect. My real problem here is that i don't understand what the parameters to the BlendFunc do.
Offline loic_sephiroth

Junior Newbie





« Reply #14 - Posted 2005-10-01 11:51:59 »

Ok, now i have my textures well rendered, including their alpha transparency.

All i had to do is to set the Blend Function factors to (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA) (with a call to glBlendFunc). Why this does work, i have no idea. I've read the blending basic tutorial, yet i fail to grasp why GL_SRC_ALPHA works and GL_ONE does not.
Offline chamster

Senior Newbie




Me - The Chamster


« Reply #15 - Posted 2005-10-02 10:45:29 »

Thank you very much for the info. We've read the manual part once again and that, combined with the tips you gave us, lead to a better understanding. However, our texture doesn't seem to know that and keeps refusing to show up. So, here's a follow up to your kind reply.

In init we do something like this.
1  
2  
3  
4  
5  
6  
this.gl.glEnable (GL.GL_TEXTURE_2D);
BufferedImage img = ImageIO.read (new File ("grass.gif"));
byte[] data = ((DataBufferByte) img.getRaster ().getDataBuffer ()).getData ();
this.gl.glBindTexture (GL.GL_TEXTURE_2D, 0);
this.gl.glTexImage2D (target, 0, GL.GL_RGB, img.getWidth (), img.getHeight (), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, data);
this.gl.glTexEnvf (GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_DECAL);

We've checked that the byte[] contains something and, in fact, the right number of something's, namely 2562 elements. So, give or take that the file itself is screwed up, we still got a set of 65536 elements between 0 and 255. It should be suitable for texturing. Please point out any mistakes we've done here.

In display we do something like this.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
this.gl.glBegin (GL.GL_QUADS);
this.gl.glColor3d (1.0, 0.9, 0.1);
this.gl.glBindTexture (GL.GL_TEXTURE_2D, 0);

this.gl.glTexCoord2d (0, 0); this.gl.glVertex3d (0, 0, 0);
this.gl.glTexCoord2d (1, 0); this.gl.glVertex3d (30, 0, 0);
this.gl.glTexCoord2d (1, 1); this.gl.glVertex3d (30, 30, 0);
this.gl.glTexCoord2d (0, 1); this.gl.glVertex3d (0, 30, 0);

this.gl.glEnd ();


We use glBindTexture (GL.GL_TEXTURE_2D, 0) twice. Once in init, to get the byte[] to the graphic card and assign it an ID (namely zero), and then a second time in display to make it the current texture.

The result is still a yellow square with hardly any grass on it (unless we're regarding some really hot and dry country where all grass actually is yellow). What did we done stupidishly this time?
Offline turquoise3232

Junior Member




Java (games) rock!


« Reply #16 - Posted 2005-10-02 11:51:39 »

Hi there,

You must call gl.glGenTextures to have a valid texture id since (quote from glBindTexture ) :
"Texture names are unsigned integers. The value 0 is reserved to represent the default texture for each texture target. "

What you want to do is to bind a new texture, not the default one...
Offline chamster

Senior Newbie




Me - The Chamster


« Reply #17 - Posted 2005-10-02 14:12:37 »

You must call gl.glGenTextures to have a valid texture id...
What you want to do is to bind a new texture, not the default one...

OK, i did the following.
1  
2  
3  
4  
5  
private int[] tex = new int[10];
...
byte[] data = ((DataBufferByte) img.getRaster ().getDataBuffer ()).getData ();
this.gl.glGenTextures (1, this.tex);
this.gl.glBindTexture (GL.GL_TEXTURE_2D, this.tex[0]);


Still not working, though. I only have one texture and i refer to it's ID as this.tex[0] in both init and display. More hints, please.

I'm assuming two things here.
  • I only have to call glGenTex (int, int[]) once, right before glBindTexture (...).
  • The use of an int[] (which is supposed to be an object) leads in Java to be regarded as a call by reference. Id est, i actually get some changes done in the array i send in.

Offline tom
« Reply #18 - Posted 2005-10-02 16:31:22 »

Using 1 as id is fine.

You must have glBindTexture outside glBegin()/glEnd(). You can not change state inside begin/end, you can only call geomtry functions like glColor, glVertex etc.

The color should be:
1  
this.gl.glColor4d (1, 1, 1, 1);


That way you get exactly the colors of the original texture.

Offline chamster

Senior Newbie




Me - The Chamster


« Reply #19 - Posted 2005-10-02 18:50:33 »

You must have glBindTexture outside glBegin()/glEnd(). You can not change state inside begin/end, you can only call geomtry functions like glColor, glVertex etc.
The color should be:
1  
this.gl.glColor4d (1, 1, 1, 1);


That way you get exactly the colors of the original texture.

We made those changes you recommended but the only visible difference is that we got a white, instead of a yellow square. Still no texture in the vicinity. Is texture mapping somehow platform dependent? We use WinXP and we assumed that if JOGl existed for our machine, it would include ALL the fun stuff (including the most fun stuff of all - texturing).    Grin

Since we're rather noobs when it comes to OpenGL, we have kind of limited resources to error-search the program. We've made sure that the byte[] isn't empty (it has a wide range of values - all from 250 down to 5), has right number of elements. We've made sure that the returned ID is something good (it was 1, doh). As of now, we're sitting and praying for somebody to pull us out of the misery or shoot us so we don't need to suffer.    Cry

Any hints are greatly appreciated.

Offline tom
« Reply #20 - Posted 2005-10-02 22:03:11 »

We've checked that the byte[] contains something and, in fact, the right number of something's, namely 2562 elements. So, give or take that the file itself is screwed up, we still got a set of 65536 elements between 0 and 255. It should be suitable for texturing. Please point out any mistakes we've done here.

Well it should be 256*256*3 since it's RGB, but I'm sure it's fine.

I've made a complete texturing example so that you've got something to look at. The only problem is that it uses LWJGL instead of JOGL, but I'm sure it can be of some help:

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  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
import java.nio.*;
import org.lwjgl.opengl.*;

public class TextureTest {
   
   public static void main(String args[]) throws Exception {
      try {
         Display.setDisplayMode(new DisplayMode(640, 480));
         Display.create();
         GL11.glClearColor(0.0f, 0.0f, 1.0f, 0.0f);

         // generate a pretty texture in RGBA format
        int width = 256;
         int height = 256;
         ByteBuffer textureData = ByteBuffer.allocateDirect(4 * width * height);
         for (int y=0; y<height; y++) {
            for (int x=0; x<width; x++) {
               textureData.put((byte)(x|y)).put((byte)(x&y)).put((byte)(x^y)).put((byte)0xff);
            }
         }
         textureData.rewind();
         
         // transfer texture to the card
        int textureId = 1;
         GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
         GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, textureData);
         
         // IMPORTANT: gets white quad without this, and I don't know why. Mipmapping issue pherhaps.
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
         
         while (!Display.isCloseRequested()) {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
           
            // we want it to be white
           GL11.glColor4f(1, 1, 1, 1);

            // enable texturing and bind our pretty image
           GL11.glEnable(GL11.GL_TEXTURE_2D);
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
                           
            // draw a 256x256 quad
           int size = 256;
            GL11.glBegin(GL11.GL_QUADS);
            GL11.glTexCoord2f(0, 0);
            GL11.glVertex2i(200, 200);
            GL11.glTexCoord2f(0, 1);
            GL11.glVertex2i(200, 200+size);
            GL11.glTexCoord2f(1, 1);
            GL11.glVertex2i(200+size, 200+size);
            GL11.glTexCoord2f(1, 0);
            GL11.glVertex2i(200+size, 200);
            GL11.glEnd();      
           
            Display.update();
            Thread.yield();
         }
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         Display.destroy();     
      }
   }
}


The only thing that stands out is this line:
1  
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST); 


Good luck!

Offline chamster

Senior Newbie




Me - The Chamster


« Reply #21 - Posted 2005-10-03 17:16:40 »

Whoa! Finally got it to work. It turns out, some of the files we had were corrupted or something like that. Once we got rid of them it started to roll. Still, it was very educational and we'd like to thank all of you guys. You've been a real rock.
Offline Antilles58

Senior Newbie





« Reply #22 - Posted 2005-12-06 04:48:41 »

i'm also trying to get a texture set up, but it just straight up doesn't work.  i'm using the TextureIO class specified here and have done everything that sephiroth got to work, but I don't seem to have any luck at all.  here is the code that's supposed to be drawing the texture, but I just get a box displayed gray (the previous color that glColor3f was called to)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
tex.bind(gl);
     
      gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
     
      gl.glBegin(GL.GL_QUADS);

         gl.glTexCoord2d(0.0, 0.0); gl.glVertex2d(x-quadWidth/2,y-quadHeight/2);
         gl.glTexCoord2d(1.0, 0.0); gl.glVertex2d(x+quadWidth/2,y-quadHeight/2);
         gl.glTexCoord2d(1.0, 1.0); gl.glVertex2d(x+quadWidth/2,y+quadHeight/2);
         gl.glTexCoord2d(0.0, 1.0); gl.glVertex2d(x-quadWidth/2,y+quadHeight/2);
         
      gl.glEnd();


the 'tex' variable is my Texture, and quadWidth/quadHeight are just doubles that specify the location.  The quad is drawn in the correct place on the screen, so i know these are working.  i just get no texture.  what's up?!
Offline turquoise3232

Junior Member




Java (games) rock!


« Reply #23 - Posted 2005-12-06 07:02:58 »

Hi,

Did you call glEnable with GL_TEXTURE_2D ?
Offline Antilles58

Senior Newbie





« Reply #24 - Posted 2005-12-06 21:18:16 »

No, I hadn't done that, since I thought it was done explicitly by the Texture class.  I was wrong about that.

Now, however, still just get a black quad drawn on the screen, so it's no longer gray and also not my texture.  My texture image doesn't have any black in it at all.

Does the fact that I'm creating the image myself using a BufferedImage so i can draw text on the screen have anything to do with it?  Here's my BufferedImage creation code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
// draw the string to a BufferedImage
     data = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
      Graphics2D g2d = (Graphics2D) data.getGraphics();
      g2d.setColor(Color.white);
      //g2d.setComposite(AlphaComposite.Clear);
     g2d.fillRect(0,0,width,height);
     
      g2d.setFont(font);
      g2d.setColor(Color.red);
      //g2d.setComposite(AlphaComposite.SrcOver);
     g2d.drawString(text,0,str_baseline);
     
      tex = TextureIO.createTexture(data);


The alpha stuff is commented out so that I could just get a texture working before messing with all that, but I'd like to eventually put that in also if you have any comments on how to make that work.
Offline tom
« Reply #25 - Posted 2005-12-06 21:42:35 »

The texture will be multiplied by the current color. Try setting it to white by calling "glColor4f(1,1,1,1)" before rendering the quad.

Offline Antilles58

Senior Newbie





« Reply #26 - Posted 2005-12-06 21:58:18 »

Did that and all i get is still a black square.  I am reading the red book chapter on texture mapping now, but i'm pretty sure i understand it I just don't know what's going on in this situation.  especially given that I'm using the Texture class to do all the texture loading.  I'm sure i'm doing something wrong with my bufferedImage, but idk.
Offline Antilles58

Senior Newbie





« Reply #27 - Posted 2005-12-12 20:57:31 »

Ok, I've done a lot of reading on textures, played around with it more, and I still have no idea what's wrong.  Here's all of my code involved with the textures.  Somebody please help!

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  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
import mvt.util.Texture;
import mvt.util.TextureIO;

import java.awt.*;
import java.awt.geom.*;

import java.awt.image.*;
import java.io.*;
import java.nio.*;
import javax.imageio.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.utils.*;

public class TextObject extends GraphicsComponent {

    private String text;
    private Point2D.Double pt;
    private Plot2DPanel parent = null;
    private Color color;
   private Font font;
   private BufferedImage data;
   private int texture;
   private boolean textureMade;
   private Texture tex;

    /**
     * Creates a new <code>TextObject</code> instance.
     *
     * @param pt a <code>Point2D.Double</code> object representing
     * the location of the text.
     * @param text a <code>String</code> object which is to be
     * printed on the screen.
     * @param plotPanel a <code>Plot2DPanel</code> where this
     * object will be showed on
     * @param color the <code>Color</code> of the cross
     */

   
    public TextObject(Point2D.Double pt, String text,
            Plot2DPanel plotPanel, Color color, Font font){
           
      super();
      this.text=text;
      this.pt = pt;
      this.parent = plotPanel;
      this.color = color;
      this.font = font;
     
      textureMade = false;
     
      BufferedImage temp = new BufferedImage(1,1, BufferedImage.TYPE_3BYTE_BGR);
      Graphics g = temp.getGraphics();
      g.setFont(font);
      int str_width = g.getFontMetrics().stringWidth(text)+2;
      int str_height = g.getFontMetrics().getHeight()+2;
      int str_baseline = g.getFontMetrics().getHeight()-g.getFontMetrics().getMaxDescent();
     
      // create an image with dimensions that are powers of two
     // which is large enough to hold the desired text
     int width, height;
      for (width = 2; width<str_width; width *= 2)
         ;
      for (height = 2; height<str_height; height *= 2)
         ;
     
      // draw the string to a BufferedImage
     data = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
      Graphics g2d = data.createGraphics();
      g2d.setColor(Color.white);
      //g2d.setComposite(AlphaComposite.Clear);
     g2d.fillRect(0,0,width,height);
     
      g2d.setFont(font);
      g2d.setColor(Color.red);
      //g2d.setComposite(AlphaComposite.SrcOver);
     g2d.drawString(text,0,str_baseline);
     
      tex = TextureIO.createTexture(data);

    }

   public void glDraw(GL gl) {
      System.out.println("TextObject");
     
      // create the texture on the first time through only
     if (!textureMade) {
         System.out.println("Create Texture");
         tex.initialize(false, true, GL.GL_LINEAR, GL.GL_LINEAR, gl, new GLU());
         textureMade = true;
      }
     
      double xmin = parent.getXMin();
      double xmax = parent.getXMax();
      double ymin = parent.getYMin();
      double ymax = parent.getYMax();
     
      double x = pt.getX();
      double y = pt.getY();
     
      // figure out how big the image is in our current GL coordinate system
     double quadWidth = (double)(data.getWidth()*(xmax-xmin)/parent.getWidth());
      double quadHeight = (double)(data.getHeight()*(ymax-ymin)/parent.getHeight());

      gl.glEnable(GL.GL_TEXTURE_2D);
      tex.bind(gl);
      gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_DECAL);
     
      gl.glBegin(GL.GL_QUADS);
     
         gl.glColor4f(1.0f,1f,1.0f,1.0f);
         gl.glTexCoord2d(0.0, 0.0); gl.glVertex2d(x-quadWidth/2,y-quadHeight/2);
         gl.glTexCoord2d(1.0, 0.0); gl.glVertex2d(x+quadWidth/2,y-quadHeight/2);
         gl.glTexCoord2d(1.0, 1.0); gl.glVertex2d(x+quadWidth/2,y+quadHeight/2);
         gl.glTexCoord2d(0.0, 1.0); gl.glVertex2d(x-quadWidth/2,y+quadHeight/2);
         
      gl.glEnd();
     
      gl.glDisable(GL.GL_TEXTURE_2D);
   }
   
   public void setLocation(Point2D.Double newPt) {
   
      pt = newPt;
     
   }

}


The Texture and TextureIO classes included in the first few lines are the same texture and textureio classes linked to earlier in this thread.  GLDraw() is called by the display() method of something that implements GL event listener, and it passes in the GL context of it.  The black square is drawn correctly on that screen in the right place... the only issue is i want a texture with a string written on it, not a black square!
Offline tom
« Reply #28 - Posted 2005-12-12 21:40:22 »

Quote
GLDraw() is called by the display() method of something that implements GL event listener, and it passes in the GL context of it.

The TextObject constructor must also be called from the OpenGL thread because TextureIO.createTexture uses GL functions.

Offline Antilles58

Senior Newbie





« Reply #29 - Posted 2005-12-12 21:49:03 »

I do have it being called from one of the GL context functions, but actually I don't think that matters since createTexture() doesn't use any gl functions, just a few constants.  I thought maybe the textures weren't getting binded correctly, but that is in fact all working, so I just don't know.
Pages: [1] 2
  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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (34 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!