Author Topic: Textures on triangles and textures in general  (Read 3601 times)

Offline JavaMan

  • long
  • ***
  • Posts: 231
    • View Profile
Textures on triangles and textures in general
« on: November 09, 2007, 08:16:44 pm »
Hi I have a quick question for creating objects from triangles and applying textures to them. I created a "box" from primitives.getBox and I was able to apply a texture to it, but how do I do it to an object made from triangles? When I call object3D.setTexture(...) it will apply it to the box but not to the triangle! I also tried putting a TextureInfo refernece into the Object3D.addTriangle but it still doesn't work.

Also, I'm not that clear on textures anyway. Is there some post on this forum that textures were explained or could someone explain them to me? I know texture is what we see when the object is in view and every object has a dummy texture, but about environment mapping base texture.
Thanks
Hope this isn't too much for one post.
V

I try not put two posts so close together next time :-\
« Last Edit: November 09, 2007, 08:23:03 pm by JavaMan »

Kearnan

  • Guest
Re: Textures on triangles and textures in general
« Reply #1 on: November 10, 2007, 02:48:51 am »
This code works for adding a Primitive Pyramid and applying a texture...

    // Somewhere up in the code DNA
    private Object3D aPyramide = null;

    // The texture is scaled to the object
    // so texture/object size doesn't matter
    aPyramide = Primitives.getPyramide(10);
    // Your coords
    aPyramide.setOrigin(new SimpleVector(0, 0, 0));
    // Yadda yadda set collision stuff
    aPyramide.setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);
    // Yadda yadda set more collision stuff
    aPyramide.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
    // Set your texture
    aPyramide.setTexture("squaretriangle.jpg");
    // Build the object
    aPyramide.build();
    // calcTextureWrap after build.
    // It doesn't work before.
    // It just seems like it should.
    aPyramide.calcTextureWrap();
    // Add the object to the world
    theWorld.addObject(aPyramide);

OpenGL treats textures as they are; squares.  Once you get used to how it maps to a triangle you could simply make textures that are made for what you want to see. (Folding, Mapping, Texture Wrapping...whatever texture belief you subscribe to.  Most rendering engines treat textures as squares and, of-course, you always have the option of creating your own buffer method)
« Last Edit: November 10, 2007, 03:12:06 am by Kearnan »

Offline Jonas

  • byte
  • *
  • Posts: 41
    • View Profile
Re: Textures on triangles and textures in general
« Reply #2 on: November 10, 2007, 10:57:02 am »
hi

If you create your own object with triangles, you have to somehow tell the engine how to map a texture's picture information on to that object. This can be done by assigning UV coordiantes(UV really being x and y coordinates in % in a texture-> between 0 and 1) to the 3d geometry(vertices).

There several ways to assign UV coordiantes to your geometry:

You can do it like Kearnan, and let the engine calculate it for you(which might sometimes look a bit wierd), or by adding this information together with the vertices information of the triangle(often the case when using model loaders).

Following a simple example of a selfmade cube(probably the most awkward way of doing it heh). It only maps a texture on the two top triangles of the box. I'd go with automatic mapping though^^

Code: [Select]
/** Creates a box, which maps any given texture on to the top of the box */
   
    public static Object3D createBoxTextTop(float size, int texId)
    {
        Object3D box = new Object3D(12);
       
        SimpleVector[] sv = new SimpleVector[8];
        /* The 8 vertices of the box */
        sv[0] = new SimpleVector(0, 0, 0);
        sv[1] = new SimpleVector(size, 0, 0);
        sv[2] = new SimpleVector(size, size, 0);
        sv[3] = new SimpleVector(0, size, 0);
       
        sv[4] = new SimpleVector(0, 0, size);
        sv[5] = new SimpleVector(size, 0, size);
        sv[6] = new SimpleVector(size, size, size);
        sv[7] = new SimpleVector(0, size, size);
       
        /* Add the triangles counter-clockwise(so they face "outside") */
        box.addTriangle(sv[0],sv[2],sv[1]);
        box.addTriangle(sv[0],sv[3],sv[2]);
       
        box.addTriangle(sv[1],sv[6],sv[5]);
        box.addTriangle(sv[1],sv[2],sv[6]);
       
        box.addTriangle(sv[5],sv[7],sv[4]);
        box.addTriangle(sv[5],sv[6],sv[7]);
       
        box.addTriangle(sv[4],sv[3],sv[0]);
        box.addTriangle(sv[4],sv[7],sv[3]);

        /* These are the 2 top triangles, which have a texture assigned(texId is the texture ID returned from a texmanager).
             In this case its easy to map the texture, since the two triangles should show the whole texture
        */

        box.addTriangle(sv[4], 0f, 0f, sv[1], 1f, 1f, sv[5], 1f, 0f, texId);
        box.addTriangle(sv[4], 0f, 0f, sv[0], 0f, 1f, sv[1], 1f, 1f, texId);
       
        box.addTriangle(sv[3],sv[6],sv[2]);
        box.addTriangle(sv[3],sv[7],sv[6]);
         
        return box;
    }
Simple things should be simple, complex things should be possible - Alan Kay

Offline JavaMan

  • long
  • ***
  • Posts: 231
    • View Profile
Re: Textures on triangles and textures in general
« Reply #3 on: November 13, 2007, 01:19:00 am »
Thanks for the answers. I was able to put a texture on the triangle, and I understand about the UV coordinates.
Thanks!!! ;D
V