Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  Texture is not applied to OBJ model properly  (Read 3340 times)
0 Members and 1 Guest are viewing this topic.
Offline p_ooya

Junior Newbie





« Posted 2011-10-07 16:45:36 »

Hello everyone.
I'm relatively new to java3d and I'm having a problem. I want to assign a texture (an image) to an obj 3d model. The problem is, when I run the code the model is loaded correctly but the texture is not assigned correctly. More accurately, the whole 3d model has a single color instead of an image as texture. Mentioned color, is the color of the pixel at bottom-left of the image. Here is the code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
Shape3D torus=null;
Scene t1 = getSceneFromFile("Some local path\torus.obj");
\\Grabbing the model from the scene
BranchGroup branchGroup = t1.getSceneGroup();
torus = (Shape3D) branchGroup.getChild(0);
\\loading the image, generating the texture and making the appearance
TextureLoader textureLoader=new TextureLoader("Another local path\myImage.jpg",null);
ImageComponent2D image=textureLoader.getImage();
Texture2D texture=new Texture2D(Texture.BASE_LEVEL,Texture.RGBA,image.getWidth(),image.getHeight());
texture.setImage(0, image);

Appearance app = new Appearance();
app.setTexture(texture);

torus.setAppearance(app);


How to solve this issue?
Offline theagentd
« Reply #1 - Posted 2011-10-07 20:29:57 »

I'm pretty sure Java3D is pretty ancient now. You should try something else, like LWJGL.  Wink

Myomyomyo.
Offline p_ooya

Junior Newbie





« Reply #2 - Posted 2011-10-08 06:13:18 »

For some reasons I have to use java3d. Do you have any suggestions that solves the problem in java3d?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2011-10-08 09:09:28 »

No, sorry... Because of Java3D being so old, I don't think you'll be able to get much help here, but I may be wrong.  Undecided I've only ever used LWJGL for 3D stuff... Do you have to enable texturing in some way? OpenGL has glEnable(GL_TEXTURE_2D), or you only get a single color on everything...

Myomyomyo.
Offline gouessej
« Reply #4 - Posted 2011-10-12 12:57:40 »

Hi

Have you tried to open your model with a 3D modeler like Blender?

Are you sure the path is correct? Are you sure you use the appropriate color component type? As far as I know, the loading of the image is asynchronous and you don't use any observer.

Try to use TextureLoader.getTexture(). If it returns null, it means the loading of the image failed.

You say it displays the color of the pixel in the bottom left corner, maybe your texture coordinates are wrong.

I'm sorry, I'm not a specialist of Java3D. As theagentd said, Java3D is obsolete. You should rather use an API which looks like Java3D but which is still maintained like Xith3D. This engine has a JOGL renderer and a LWJGL renderer.

Offline Atran

Junior Newbie





« Reply #5 - Posted 2012-01-28 19:16:15 »

You have to set TextureCoordinates or let them generate by the engine for you (which doesn't produce good results most of the time).
Here is a simple method for generating an appearance object with a given texture:

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  
   /**
    * Generate Apperance with given texture
    * @param withTexCoordGeneration
    * @param texture
    * @param wrapTexture
    * @return Appearance
    */

   private Appearance generateAppearanceWithTexture(Texture2D texture, boolean withTexCoordGeneration) {
      // Create the appearance that will use the texture
      Appearance appearance = new Appearance();
      appearance.setTexture(texture);

      // If TexCoordGeneration is wanted then have it
      if (withTexCoordGeneration) {
         TexCoordGeneration texCoordGeneration = new TexCoordGeneration(TexCoordGeneration.NORMAL_MAP,
               TexCoordGeneration.TEXTURE_COORDINATE_2);
         appearance.setTexCoordGeneration(texCoordGeneration);
      }

      // Define how the texture will be mapped onto the surface
      // by creating the appropriate texture attributes
      TextureAttributes textAttr = new TextureAttributes();
      textAttr.setTextureMode(TextureAttributes.REPLACE);

      PolygonAttributes polygonAttributes = new PolygonAttributes();
      polygonAttributes.setCullFace(PolygonAttributes.CULL_NONE);
      appearance.setPolygonAttributes(polygonAttributes);

      appearance.setTextureAttributes(textAttr);
      return appearance;
   }


Or another example which generates a quadarray (for a plane) with setting the TexCoodinates manually (see the .setTextureCoordinate() calls):
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  
   /**
    * Generate a QuadArray (for a plane) by giving the side of a cuboid and the dimension for all 3
    * directions
    * @param side
    * @param x
    * @param y
    * @param z
    * @param texCoordPoint (if null then TexCoords for full size will be set)
    * @return QuadArray
    */

   public static final QuadArray makePlaneForCuboid(int side, float x, float y, float z, Point2f texCoordPoint) {
      QuadArray plane = new QuadArray(24, GeometryArray.COORDINATES | QuadArray.TEXTURE_COORDINATE_2);

      Point3f pa = new Point3f(-1.0f * x, 1.0f * y, 1.0f * z); // = upper corner
      Point3f pb = new Point3f(-1.0f * x, -1.0f * y, 1.0f * z);
      Point3f pc = new Point3f(1.0f * x, -1.0f * y, 1.0f * z);
      Point3f pd = new Point3f(1.0f * x, 1.0f * y, 1.0f * z);
      Point3f pe = new Point3f(-1.0f * x, 1.0f * y, -1.0f * z);
      Point3f pf = new Point3f(-1.0f * x, -1.0f * y, -1.0f * z);
      Point3f pg = new Point3f(1.0f * x, -1.0f * y, -1.0f * z); // = lower corner
      Point3f ph = new Point3f(1.0f * x, 1.0f * y, -1.0f * z);

      switch (side) {
      case 1:
         plane.setCoordinate(0, pa);
         plane.setCoordinate(1, pb);
         plane.setCoordinate(2, pc);
         plane.setCoordinate(3, pd);
         break;

      case 2:
         plane.setCoordinate(0, pe);
         plane.setCoordinate(1, pf);
         plane.setCoordinate(2, pg);
         plane.setCoordinate(3, ph);
         break;

      case 3:
         plane.setCoordinate(0, pe);
         plane.setCoordinate(1, pf);
         plane.setCoordinate(2, pb);
         plane.setCoordinate(3, pa);
         break;

      case 4:
         plane.setCoordinate(0, pd);
         plane.setCoordinate(1, pc);
         plane.setCoordinate(2, pg);
         plane.setCoordinate(3, ph);
         break;

      case 5:
         plane.setCoordinate(0, pe);
         plane.setCoordinate(1, pa);
         plane.setCoordinate(2, pd);
         plane.setCoordinate(3, ph);
         break;

      case 6:
         plane.setCoordinate(0, pf);
         plane.setCoordinate(1, pb);
         plane.setCoordinate(2, pc);
         plane.setCoordinate(3, pg);
         break;

      default:
         plane.setCoordinate(0, pa);
         plane.setCoordinate(1, pb);
         plane.setCoordinate(2, pc);
         plane.setCoordinate(3, pd);
         break;
      }

      if (texCoordPoint == null) {
        plane.setTextureCoordinate(0, 0, new TexCoord2f(0f, 1f));
        plane.setTextureCoordinate(0, 1, new TexCoord2f(0f, 0f));
        plane.setTextureCoordinate(0, 2, new TexCoord2f(1f, 0f));
        plane.setTextureCoordinate(0, 3, new TexCoord2f(1f, 1f));
      } else {
        plane.setTextureCoordinate(0, 0, new TexCoord2f(0f, texCoordPoint.y));
        plane.setTextureCoordinate(0, 1, new TexCoord2f(0f, 0f));
        plane.setTextureCoordinate(0, 2, new TexCoord2f(texCoordPoint.x, 0f));
        plane.setTextureCoordinate(0, 3, new TexCoord2f(texCoordPoint.x, texCoordPoint.y));
      }

      return plane;
   }

Offline sproingie

JGO Kernel


Medals: 202



« Reply #6 - Posted 2012-01-28 20:44:23 »

You should rather use an API which looks like Java3D but which is still maintained like Xith3D.

Xith3D hasn't made a stable release since 2006, a beta since 2008, or a "cooker" since 2010.  As the Magic 8-Ball says, Outlook Not So Good.
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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!