A problem with the textures.

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

A problem with the textures.

dfewfew
How i can load the textures in java 3d.I tried several times but so far I have only colored the objects in some random color.

TextureLoader loader = new TextureLoader("rujda.bmp",this);
Texture3D brick = (Texture3D)loader.getTexture();
TextureAttributes ta = new TextureAttributes();      
Appearance ap = new Appearance();
   ap.setTextureAttributes(ta);
   ap.setTexture(brick);
Reply | Threaded
Open this post in threaded view
|

Re: A problem with the textures.

gouessej
Administrator
Hi

What's wrong exactly with your code? Do you get an error message? Do you set some texture coordinates?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: A problem with the textures.

dfewfew
package com.daltonfilho.tutorials.java3d;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileNotFoundException;
import java.util.Enumeration;

import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnCollisionEntry;
import javax.media.j3d.WakeupOnCollisionExit;
import javax.media.j3d.WakeupOnCollisionMovement;
import javax.media.j3d.WakeupOnElapsedTime;
import javax.media.j3d.WakeupOr;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.ConfiguredUniverse;
import com.sun.j3d.utils.universe.SimpleUniverse;

class sss
{
        public void aa()
        {}
        }

public class themain extends Applet implements KeyListener {
        public static void main(String[] args)
        {
                themain aa=  new themain();
                new MainFrame(aa,400,400);
                aa.addKeyListener(aa);
        }
        class colision extends Behavior
        {
               
                WakeupCriterion yawn ;
                WakeupCriterion[]
                kolizia =new WakeupCriterion[3];
                 protected WakeupOr oredCriteria;
       Shape3D shape;
                public colision(Shape3D shape,Bounds granici){
                this.shape=shape;
                        System.out.println("bbbaaa");
                        setSchedulingBounds(granici);
                }
                public void initialize() {
                    kolizia[0]=new WakeupOnCollisionEntry(shape);
                kolizia[1] = new WakeupOnCollisionExit(shape);
                kolizia[2] = new WakeupOnCollisionMovement(shape);
                oredCriteria = new WakeupOr(kolizia);
                wakeupOn(oredCriteria);
               
                }
                public  void processStimulus(Enumeration criteria) {
                          WakeupCriterion theCriterion = (WakeupCriterion) criteria.nextElement();
                            if (theCriterion instanceof WakeupOnCollisionEntry) {
                         themain.this.update(true);
                              System.out.println("Collided with ");
                            } else if (theCriterion instanceof WakeupOnCollisionExit) {
                             themain.this.update(true);
                              System.out.println("Stopped colliding with  "
                                );
                            } else {
                             themain.this.update(true);
                              System.out.println("Moved whilst colliding with "
                            );
                            }
                            wakeupOn(oredCriteria);

                       
                }
                }
       
        class main extends Behavior
        {
                WakeupCriterion yawn ;

                public main(){
       
                        System.out.println("bbb");
                        yawn  = new WakeupOnElapsedTime(30);

                }
                public void initialize() {

                        wakeupOn(yawn);
                }
                public  void processStimulus(Enumeration criteria) {
       
                themain.this.update(false);
                        wakeupOn(yawn);
                       
                }
                }
       

        float posokaz = 0.0f;
        float posokay=0.0f;
        float posokax=5.0f;
        float posoka2z = 10.0f;
        float posoka2y=0.0f;
        float posoka2x=0.0f;
    double zawurtane = 0;
         Transform3D transform;
         Vector3f mqsto;
         Transform3D transform2;
         Transform3D transform3;
         Vector3f mqstomishena;
         View gledka;
         TransformGroup view;
         TransformGroup tg;
         TransformGroup tg2;
          DirectionalLight light1;
          Shape3D korab;
          Background background =null;
          Shape3D mishena;
          Shape3D truba;
          Transform3D lookat ;
          ConfiguredUniverse ss;
          ConfiguredUniverse universe ;
           ObjectFile f;
           Scene s = null;
           BranchGroup b ;
public themain ()
{
        //javax.media.j3d.View.setFrontClipDistance(22);
       

        setLayout(new BorderLayout());
         GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
         
         Canvas3D canvas = new Canvas3D(config);
        // canvas.setBounds(10000, 10000, 10000, 10000);
         universe = new ConfiguredUniverse(canvas);
 
  tg = new TransformGroup();
  tg2 =new TransformGroup();

  tg2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  transform = new Transform3D();
  transform2 = new Transform3D();
  transform3 = new Transform3D();
  lookat= new Transform3D();
  View view = canvas.getView();
  view.setBackClipDistance(300f);
   mqsto =new Vector3f(posokax,posokay,posokaz);
   mqstomishena =new Vector3f(0,0,0);
  // mqstomishena =new Vector3f(20,0,0);
   add( canvas);
   canvas.addKeyListener(this);
   transform.setTranslation(mqsto);
   background=new Background();
   TextureAttributes texAttr = new TextureAttributes();
   texAttr.setTextureMode(TextureAttributes.MODULATE);
   //background.setColor(1.0f, 0.0f, 0.0f);
   background.setCapability(Background.ALLOW_APPLICATION_BOUNDS_WRITE);
   gledka=new View();
   lookat.lookAt(new Point3d(0,0,0), new Point3d(mqsto.getX(),mqsto.getY(),mqsto.getZ()), new Vector3d() );
   lookat.setTranslation(mqsto);
   transform2.setTranslation(mqstomishena);
   tg.setTransform(transform);
   gledka.setBackClipDistance(3);
   gledka.setFrontClipDistance(30);
//   gledka.addCanvas3D(canvas);
   tg2.setTransform(transform);
   String s1 = "samolet.obj";
   String s2 = "mishena.obj";
   String s3 = "truba.obj";

TextureLoader loader = new TextureLoader("rujda.jpg",this);
Texture2D brick = (Texture2D)loader.getTexture();
brick.setEnable(true);
TextureAttributes ta = new TextureAttributes();    

   BranchGroup group = new BranchGroup();
   korab=loader(s1);
   b.removeChild(0);
   mishena = loader(s2);
   b.removeChild(0);
   truba=loader(s3);
   b.removeChild(0);
   Color3f light1Color = new Color3f(0f, 255f, 1f);
   BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0,
                      0.0), 100.0);
   bounds.setRadius(Float.MAX_VALUE);
   Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
    light1 = new DirectionalLight(light1Color, light1Direction);
   light1.setInfluencingBounds(bounds);
   background.setApplicationBounds(bounds);
   background.setName("rujda.jpg");
   
   AmbientLight ambientLightNode = new AmbientLight (light1Color);
        ambientLightNode.setInfluencingBounds (bounds);
   Appearance ap = new Appearance();
    Color3f col = new Color3f(0.0f, 0.0f, 22.0f);
   ap.setTextureAttributes(texAttr);
   ap.setTexture(brick);
   mishena.setAppearance(ap);
   truba.setAppearance(ap);
 
   korab.setAppearance(ap);
   tg.addChild(mishena);
   tg.addChild(truba);
   tg2.addChild(korab);
   Sphere mySphere = new Sphere(0.7f,Primitive.GENERATE_TEXTURE_COORDS,ap);
tg2.addChild(mySphere);
   //tg.addChild(view);
   tg.addChild(ambientLightNode);
   MouseRotate myMouseRotate = new MouseRotate();
   myMouseRotate.setTransformGroup(tg);
   myMouseRotate.setSchedulingBounds(new BoundingSphere());
    group.addChild(tg);
   group.addChild(background);
    group.addChild(tg2);
 
    group.addChild(light1);
    main loop = new main();
    colision colisions = new colision(korab,bounds);
    loop.setSchedulingBounds(bounds);
    colisions.setSchedulingBounds(bounds);
    group.addChild(loop);
    group.addChild(colisions);
 
   
  universe.getViewingPlatform().setNominalViewingTransform();
 universe.getViewingPlatform().getViewPlatformTransform().setTransform(transform);
   universe.addBranchGraph(group);
   this.setVisible(true);
}
public static Texture getTexture(String path)
{
       TextureLoader texLoader = new TextureLoader(path, null);
       Texture texture =  texLoader.getTexture();
       if (texture == null)
         System.out.println("Cannot load texture from " + path);
       else {
         System.out.println("Loaded texture from " + path);
       //  texture.setEnable(true);
       }
       return texture;
}
public void keyPressed(KeyEvent e){
        if(e.getKeyCode()==KeyEvent.VK_NUMPAD2)
        {
                System.out.println("aa");
         
posokax=posokax+10.f;
mqsto.setX(posokax);
transform.setTranslation(mqsto);
tg.setTransform(transform);
        }
        if(e.getKeyCode()==KeyEvent.VK_A)
        {
                System.out.println("aa");
         
zawurtane=zawurtane+0.01;
transform2.rotZ(zawurtane);
tg2.setTransform(transform2);
        }
        if(e.getKeyCode()==KeyEvent.VK_D)
        {

zawurtane=zawurtane-0.01;
transform2.rotZ(zawurtane);
tg2.setTransform(transform2);
        }
        if(e.getKeyCode()==KeyEvent.VK_NUMPAD2)
        {
                System.out.println("aa");
         
posokax=posokax+10.f;
mqsto.setX(posokax);
transform.setTranslation(mqsto);
tg.setTransform(transform);
        }
       
        if(e.getKeyCode()==KeyEvent.VK_NUMPAD8)
        {
                System.out.println("aa");
posokax=posokax-10.f;
mqsto.setX(posokax);
transform.setTranslation(mqsto);
tg.setTransform(transform);
        }
        if(e.getKeyCode()==KeyEvent.VK_NUMPAD4)
        {
                System.out.println("aa");
posokay=posokay+10.f;
mqsto.setY(posokay);
transform.setTranslation(mqsto);
tg.setTransform(transform);
zawurtane=zawurtane+0.05;
transform.rotZ(zawurtane);
tg.setTransform(transform);
        }
        if(e.getKeyCode()==KeyEvent.VK_NUMPAD6)
        {
                System.out.println("aa");
posokay=posokay-10.f;
mqsto.setY(posokay);
transform.setTranslation(mqsto);
tg.setTransform(transform);
zawurtane=zawurtane-0.05;
transform.rotZ(zawurtane);
tg.setTransform(transform);
        }

        }
public void keyReleased(KeyEvent e){

         }
public void keyTyped(KeyEvent e){

         }

public Shape3D loader(String s1)
{
          f = new ObjectFile ();
           try{
                        s = f.load (s1);
                   }
                   catch(FileNotFoundException e)
                   {
                           System.out.println(e);
                   }
       b = s.getSceneGroup();
           return (Shape3D) b.getChild(0);
}
public  void update(boolean kol)
{
        if (kol==true)
        {
                transform.setTranslation(new Vector3f(0,0,0));
                tg.setTransform(transform);
                posokaz=0f;
        }
        else{
        posokaz=posokaz+100f;
       
transform.rotY(0);
transform.setTranslation(new Vector3f(posokay,posokax,posokaz));
tg.setTransform(transform);
        }
transform2.setTranslation(new Vector3f(posoka2y,posoka2x,10000));
tg2.setTransform(transform2);

//lookat.lookAt(new Point3d(0,0,0), new Point3d(mqsto.getX(),mqsto.getY(),mqsto.getZ()), new Vector3d() );
universe.getViewingPlatform().getViewPlatformTransform().setTransform(transform2);

}

        }
Reply | Threaded
Open this post in threaded view
|

Re: A problem with the textures.

dfewfew
In reply to this post by gouessej
Reply | Threaded
Open this post in threaded view
|

Re: A problem with the textures.

gouessej
Administrator
I can't see your image because I'm at work and lots of images are blocked. Only your sphere has texture coordinates. If your path is wrong, Java3D won't be able to create a texture with your image.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: A problem with the textures.

dfewfew
How i got it.I have a texture but only the pixel from the bottom left corner.I mean i this pixel is blue everything is blue.
Reply | Threaded
Open this post in threaded view
|

Re: A problem with the textures.

gouessej
Administrator
You need to set texture coordinates to meshes to use textures with them, there is an example here:
http://www.vrupl.evl.uic.edu/LabAccidents/java3d/lesson05/index.html

As you see at the very beginning, the polygons are attached to a shape, the appearance is attached to the same shape. These polygons have texture coordinates, that's why it works. Have you tried to use build-in loaders to load existing 3D models with their texture coordinates?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: A problem with the textures.

dfewfew
And how to set texture coordinates on shape3D ?
Reply | Threaded
Open this post in threaded view
|

Re: A problem with the textures.

gouessej
Administrator
A Shape3D instance is created with a Geometry and an Appearance. If your Geometry is a GeometryArray, then you can set texture coordinates to it. In the example, a subclass of GeometryArray (QuadArray) is used:
polygon1.setTextureCoordinate (0, new Point2f(0.0f,0.0f));
polygon1.setTextureCoordinate (1, new Point2f(1.0f,0.0f));
polygon1.setTextureCoordinate (2, new Point2f(1.0f,1.0f));
polygon1.setTextureCoordinate (3, new Point2f(0.0f,1.0f));
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: A problem with the textures.

dfewfew
But if i have a Shape3D?What to do?Is there a way to load an apply the textures from the scene?
Reply | Threaded
Open this post in threaded view
|

Re: A problem with the textures.

gouessej
Administrator
I have looked at your source code once more, you use the loader for the WaveFront OBJ format. If your model already has texture coordinates, you shouldn't have to set them, just set the texture to the appearance of your Shape3D.

If you don't know whether your model has correct texture coordinates, import it in a modeler (Blender, Wings3D, Art Of Illusion, etc...), use its MTL file during the import to indicate to this modeler which texture you would like to use and view it. If the texture coordinates are wrong or if there is no texture coordinate in your OBJ file, you can edit your model in the modeler and export it as an OBJ file in order to reimport it later with Java3D. You can set dummy texture coordinates in the code too, just use Shape3D.getGeometry() and call setTextureCoordinates() on the returned object.

Java3D API online documentation is here, the public API has not been modified in Java3D 1.6 pre:
http://download.java.net/media/java3d/javadoc/1.5.0/index.html

If you need some working examples relying on this API, look at Arabian Flights:
http://arabian.sourceforge.net/

I started my own game project about 6 years ago by modifying an existing free open source project ;)

Edit.: I plan to write a tutorial about Java3D 1.6 pre next month.
Julien Gouesse | Personal blog | Website