Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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  
  Stuck - newbies & textures  (Read 1538 times)
0 Members and 1 Guest are viewing this topic.
Offline Falken224

Senior Newbie




Ooooooh!  Look at the pretty Stars.


« Posted 2003-06-07 12:48:49 »

Okay . . . I've done as much research as I can, looked at a bunch of tutorials and am baffled why I can't texture a simple polygon.  I created a 64x64 texture in .png format and took some demo code I'd already used and adapted it to map a texture onto my polygon.  There's two variations where I get 2 different results.

Variation 1 - I insert two lines of code when I'm loading the texture
1  
2  
gl.texParameterf(GL.TEXTURE_2D,GL.TEXTURE_MIN_FILTER,GL.LINEAR);
gl.texParameterf(GL.TEXTURE_2D,GL.TEXTURE_MAG_FILTER,GL.LINEAR);

In this variation, I can tell from the logs that the program runs just fine, but I get a black screen.  I would assume it's displaying a beautiful black polygon at me.

Variation 2 - In this variation, I remove those two lines and VOILA, I get a nifty white polygon bouncing around my screen

I'm positive my texture is loading from the file okay, because in the logs I see the right sizes for the height and width.  I've tried 64x64, 128x128 and 256x256, and when I change the image, it shows up in the logs.

I'm hoping wiser heads than mine might make something of this.

If anybody wants to see it, here's the relevant code below.  If there's any important bits missing that may be important, let me know and I'll post that too.

(sorry my code's displaying all nasty . . . can't get the indentations to work right.)

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  
  private void glInit()
  {
    if(log.isDebugEnabled()) log.debug("glInit()");
 
    gl.determineAvailableExtensions();
    gl.enable(GL.TEXTURE_2D);
    gl.shadeModel(GL.SMOOTH);
    gl.matrixMode(GL.PROJECTION);
    gl.loadIdentity();
    glu.ortho2D(0,mode.width,0,mode.height);
    gl.matrixMode(GL.MODELVIEW);
    gl.loadIdentity();
    gl.viewport(0,0,mode.width,mode.height);
    gl.clearColor(0.0f,0.0f,0.0f,0.0f);
    if(GL.WGL_EXT_swap_control)
    {
      if(log.isDebugEnabled()) log.debug("swapcontrol");
      GL.wglSwapIntervalEXT(1);
    }
  }

  private Texture loadTexture(String filename)
  {
    BufferedImage newImage=null;
    File newFile=new File(filename);
    try
    {
      newImage=ImageIO.read(newFile);
    }
    catch(IOException e)
    {
      log.error("Could not load image in file: "+filename);
    }
    if(newImage==null && log.isDebugEnabled())log.debug("Texture didn't load correctly");
    Texture newTex=new Texture(newImage);
    return newTex;
  }

  protected void makeGLTextures(String[] newImages)
  {
    IntBuffer tempBuf=ByteBuffer.allocateDirect(4*(newImages.length)).asIntBuffer();
    Texture[] texList=new Texture[newImages.length];
    for(int loop=0; loop<newImages.length; loop++)
    {
      texList[loop]=loadTexture(newImages[loop]);
    }
    gl.genTextures(texList.length,Sys.getDirectBufferAddress(tempBuf));
    for(int loop=0; loop<texList.length; loop++)
    {
       if(log.isDebugEnabled()) log.debug("binding texture to name="+tempBuf.get(loop));
       if(log.isDebugEnabled()) log.debug("w="+texList[loop].getWidth());
       if(log.isDebugEnabled()) log.debug("h="+texList[loop].getHeight());
       if(log.isDebugEnabled()) log.debug("p="+texList[loop].getPtr());
       gl.bindTexture(GL.TEXTURE_2D,tempBuf.get(loop));
       gl.texParameterf(GL.TEXTURE_2D,GL.TEXTURE_MIN_FILTER,GL.LINEAR);
       gl.texParameterf(GL.TEXTURE_2D,GL.TEXTURE_MAG_FILTER,GL.LINEAR);
       gl.texImage2D(GL.TEXTURE_2D,0,GL.RGB,
                    texList[loop].getWidth(),texList[loop].getHeight(),0,GL.RGB,
                    GL.UNSIGNED_BYTE,texList[loop].getPtr());
if(log.isDebugEnabled()) log.debug("texture "+tempBuf.get(loop)+" successfully bound.");
    }
    addToTexIndex(tempBuf); //my own method defined elsewhere.  It works okay.
 }

Finally, my display code:
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  
    halfx=size.x/2;
    halfy=size.y/2;
    gl.pushMatrix();
      gl.translatef(position.x,position.y,0);
      gl.rotatef(angle,0.0f,0.0f,1.0f);
      if(textured)
      {
        gl.bindTexture(GL.TEXTURE_2D,texName);
        if(log.isDebugEnabled()) log.debug("Binding Texture "+texName);
      }
      gl.begin(GL.QUADS);
        if(textured)
        {
          if(log.isDebugEnabled()) log.debug("textured");
          gl.texCoord2f(0.0f,0.0f);
          gl.vertex2f(-halfx,-halfy);
          gl.texCoord2f(1.0f,0.0f);
          gl.vertex2f(halfx,-halfy);
          gl.texCoord2f(1.0f,1.0f);
          gl.vertex2f(halfx,halfy);
          gl.texCoord2f(0.0f,1.0f);
          gl.vertex2f(-halfx,halfy);
        } else {
          if(log.isDebugEnabled()) log.debug("untextured");
          gl.vertex2f(-halfx,-halfy);
          gl.vertex2f(halfx,-halfy);
          gl.vertex2f(halfx,halfy);
          gl.vertex2f(-halfx,halfy);
        }
        gl.end();
      gl.popMatrix();

-NateBob
 Brother Of Phil
Offline Mojomonkey

Senior Member




ooh ooh eee eeee


« Reply #1 - Posted 2003-06-07 15:08:36 »

What's the value of texName in the rendering portion of the code?

Don't send a man to do a monkey's work.
Offline Falken224

Senior Newbie




Ooooooh!  Look at the pretty Stars.


« Reply #2 - Posted 2003-06-07 18:11:05 »

texName is the openGL texture name of the texture I'm assigning.  It's an int . . . I'm assuming that's just a pointer to where openGL has decided to store my texture.   At least, that's my best guess given that I don't really understand the architecture yet.

Basically, if you take a look in the loadGLTextures() method there's a series of commands:

1  
2  
3  
4  
5  
    IntBuffer tempBuf=ByteBuffer.allocateDirect(4*(newImages.length)).asIntBuffer();

    gl.genTextures(texList.length,Sys.getDirectBufferAddress(tempBuf));

    addToTexIndex(tempBuf); //my own method defined elsewhere.  It works okay.


That just takes the set of texture names I've just generated and dumped them into a specific IntBuffer where I can get at them later in a more organized fashion.  That's the addToTexIndex(tempBuf) command.

I've looked at the logs and in the rendering code when I'm using the variable texName, texName==tempBuf.get(0)  It's the same value.  If you actually want the integer representaion of that, I could prolly dig it out of the logs.  I'd be surprised if it's the same value every time, tho.

Any other questions?

-Nate

-NateBob
 Brother Of Phil
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline elias

Senior Member





« Reply #3 - Posted 2003-06-07 18:24:49 »

When you allocate direct buffers for use with LWJGL _always_ remember to use the native byte ordering, that is

ByteBuffer.allocateDirect(blah).order(ByteOrder.nativeOrder()).asIntBuffer()

when allocating the texture names buffer (or other places where you use buffers).

- elias

Offline Falken224

Senior Newbie




Ooooooh!  Look at the pretty Stars.


« Reply #4 - Posted 2003-06-07 19:13:16 »

Quote
_always_ remember to use the native byte ordering


Okay . . . good to know.  What exactly does that do again?  Suddenly my texture name is 1 every time . . . that's prolly a good thing.  I'm assuming I can't always count on OpenGL giving me texture name in numerical order, right?

Unfortunately, that still doesn't solve my problem . . . wait . . . nope . . . still getting black and . . . still getting white.

Any other thoughts?

-Nate

-NateBob
 Brother Of Phil
Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #5 - Posted 2003-06-07 20:41:35 »

Quote
Okay . . . good to know.  What exactly does that do again?

It makes Java output data in the underlying platform's native byte order.  As you're pushing data straight to the native layer, without any translations being done by LWJGL, you need to pass it in the correct format.

Quote
I'm assuming I can't always count on OpenGL giving me texture name in numerical order, right?

Correct.  It probably will be, but there's absolutely no reason why it should.

Quote
Any other thoughts?

Are you sure you're getting a black poly there?  Change the gl.clearColor values to (1.0f, 0.0f, 0.0f, 1.0f) and see if it's black or just not displayed.

What is the texture you're expecting to see?  You're sure you're enabling and disabling texturing in the correct places?

Hellomynameis Charlie Dobbie.
Offline Falken224

Senior Newbie




Ooooooh!  Look at the pretty Stars.


« Reply #6 - Posted 2003-06-08 22:10:31 »

Quote
You're sure you're enabling and disabling texturing in the correct places?


Well, now that's an interesting question.  Where are the appropriate places?  I'm enabling texturing in my glInit() method, but not really anywhere else that I know of.  Is there something other than this line that needs to be done?

1  
gl.enable(GL.TEXTURE_2D);


I'll go ahead and test out to see what color polygon I'm getting.  I'm not sure I ever do actually set the clearcolor, so I'll play around with that and post the results.  Thanks tho.

-Nate

-NateBob
 Brother Of Phil
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.

CopyableCougar4 (15 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (38 views)
2014-08-06 19:49:38
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!