Author Topic: LWJGL applet loader Texture problem  (Read 4004 times)

Offline .jayderyu

  • long
  • ***
  • Posts: 116
    • View Profile
LWJGL applet loader Texture problem
« on: November 17, 2008, 12:49:21 am »
Ok problem with textures. I'm using LWJGL Applet loader to get the hardware acceleration. Ok that works. The problem i'm having is with textures. They don't display.

Heres an object

http://sre.hopto.org/images/badtexture.jpg

Heres the texture

http://sre.hopto.org/images/expo_stones.png

Heres the relevent texture code
Code: [Select]
  public void init(){
    world = new World();
    World.setDefaultThread( Thread.currentThread() );

    // your pick, same result
    //TextureManager.getInstance().addTexture("stone", new Texture(getResource("expo_stones.png")));
    //TextureManager.getInstance().addTexture("stone", new Texture(getDocumentBase(),"expo_stones.png"));
   
    camera = world.getCamera();
    camera.setPosition(80f,-50f,0f);
    camera.lookAt(new SimpleVector(0,0,0)); // this is invalid. this should always look at player. modify soon
   
    world.getLights().setOverbrightLighting ( Lights.OVERBRIGHT_LIGHTING_DISABLED );
    world.setAmbientLight( 50, 50, 50 );
    world.addLight( new SimpleVector( 50, -50, 30 ), 120, 120, 120 );

    Object3D object;
    object = Primitives.getBox(50f, 0.01f);
    object.setTexture("stone");
    object.setEnvmapped(Object3D.ENVMAP_ENABLED);
    object.build();
    world.addObject(object);

    buffer = new FrameBuffer( width, height, FrameBuffer.SAMPLINGMODE_HARDWARE_ONLY );
    buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
    canvas = buffer.enableGLCanvasRenderer();
    add( canvas, BorderLayout.CENTER);
    canvas.setVisible( true );
    world.buildAllObjects();
    camera = world.getCamera();
    new Thread(this).start();
  }

  private InputStream getResource( String resourceName ){
    return getClass().getClassLoader().getResourceAsStream( resourceName );
  }

the texture is at the root of the .jar file.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: LWJGL applet loader Texture problem
« Reply #1 on: November 17, 2008, 01:15:03 am »
Does the textures display when you don't use the LWJGL Applet loader (software mode instead), or is the problem only happening when loading the applet with the applet loader?

One thing I was thinking could be a possibility is that maybe since you are getting your object from Primitives.getBox(), it might not have uv texture coordinates set up for it by default (Egon would know if that is the case or not).  I've had some experience with defining uv coordinates programatically in jPCT, or you could try using a model that you know has uv coordinates defined - something created in a modeling program, just to see if that is the source of the problem.
« Last Edit: November 17, 2008, 01:53:13 am by paulscode »

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: LWJGL applet loader Texture problem
« Reply #2 on: November 17, 2008, 02:54:33 am »
I did some tests, and it does appear that cube primitives do not have any uv texture coordinates on them initially.  You could use something like this to apply the texture to your cube:

Code: [Select]
        TextureInfo polyTexture;
        int stoneID = TextureManager.getInstance().getTextureID( "stone" );
       
        polyTexture = new TextureInfo( stoneID, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f );
        object.getPolygonManager().setPolygonTexture( 0, polyTexture );
        object.getPolygonManager().setPolygonTexture( 5, polyTexture );
        polyTexture = new TextureInfo( stoneID, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 1.0f );
        object.getPolygonManager().setPolygonTexture( 1, polyTexture );
        object.getPolygonManager().setPolygonTexture( 4, polyTexture );
        polyTexture = new TextureInfo( stoneID, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.5f );
        object.getPolygonManager().setPolygonTexture( 2, polyTexture );
        object.getPolygonManager().setPolygonTexture( 3, polyTexture );
        polyTexture = new TextureInfo( stoneID, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 1.0f );
        object.getPolygonManager().setPolygonTexture( 6, polyTexture );
        object.getPolygonManager().setPolygonTexture( 7, polyTexture );
        polyTexture = new TextureInfo( stoneID, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f );
        object.getPolygonManager().setPolygonTexture( 8, polyTexture );
        object.getPolygonManager().setPolygonTexture( 10, polyTexture );
        object.getPolygonManager().setPolygonTexture( 12, polyTexture );
        object.getPolygonManager().setPolygonTexture( 14, polyTexture );
        polyTexture = new TextureInfo( stoneID, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f );
        object.getPolygonManager().setPolygonTexture( 9, polyTexture );
        object.getPolygonManager().setPolygonTexture( 11, polyTexture );
        object.getPolygonManager().setPolygonTexture( 13, polyTexture );
        object.getPolygonManager().setPolygonTexture( 15, polyTexture );

This is not a perfect uv map for the cube - I just guessed on the vertice order for each poly ID.  It should get you started, though.  I would work on one poly at a time, get the uv coordinates correct for it, then move on to the next poly, and so on.

As you can see, it is generally much easier to just load a model that already has uv coordinates defined - something created in a modeling program, rather than doing it inside your program.  Of course if you are creating an object programatically (such as generating a terrain), then you have to manually define the uv coordinates for each polygon.
« Last Edit: November 17, 2008, 03:13:05 am by paulscode »

Offline .jayderyu

  • long
  • ***
  • Posts: 116
    • View Profile
Re: LWJGL applet loader Texture problem
« Reply #3 on: November 17, 2008, 03:25:24 am »
Thanks for you responce and research. ouch. Is this something new to JPCT newer version. I remember using an older version where I could texture an sphere, but non of my cubes have worked at all.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: LWJGL applet loader Texture problem
« Reply #4 on: November 17, 2008, 08:13:06 am »
Non of the primitives except for the plane has proper coordinates. That is caused by the way they are build. You can call createTextureCoords() or createTextureCoordsSpherical() on them. Depending on the object, the generated coordinates may be sufficient.

Offline .jayderyu

  • long
  • ***
  • Posts: 116
    • View Profile
Re: LWJGL applet loader Texture problem
« Reply #5 on: November 18, 2008, 09:13:43 am »
hey thanks that helps. though when I tried using a plane the plane didn't show. might have to do with the angle though. thank you

umm where is createTextureCoords. I can't find them under Texture, TextureManager or Object3D. Anyother places?
« Last Edit: November 18, 2008, 09:16:07 am by .jayderyu »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: LWJGL applet loader Texture problem
« Reply #6 on: November 18, 2008, 09:33:09 am »
umm where is createTextureCoords. I can't find them under Texture, TextureManager or Object3D. Anyother places?
That's because i'm stupid and mixed up the names in my post. The correct names are calcTextureWrap() and calcTextureWrapSpherical() and they can be found in Object3D.

Offline .jayderyu

  • long
  • ***
  • Posts: 116
    • View Profile
Re: LWJGL applet loader Texture problem
« Reply #7 on: November 21, 2008, 05:33:01 pm »
thank you :)