Author Topic: I can texture any primitive except a plane. Why?  (Read 1691 times)

Offline apchar

  • byte
  • *
  • Posts: 3
    • View Profile
I can texture any primitive except a plane. Why?
« on: December 08, 2010, 09:45:14 am »
Consider this bit o' code:
Code: [Select]
import com.threed.jpct.*;

public class PrimitiveTests2 {

private World world;
private FrameBuffer buffer;
private Object3D floor, part;

public static void main(String[] args) throws Exception {
new PrimitiveTests2().loop();
}

public PrimitiveTests2() throws Exception {
world = new World();
world.setAmbientLight(0, 255, 0);

TextureManager.getInstance().addTexture("expo_bumps", new Texture("images/expo_bumps.png"));

                floor = Primitives.getPlane(5, 1f);
                floor.setTexture("expo_bumps");
                floor.setEnvmapped(Object3D.ENVMAP_ENABLED);
                floor.build();
                world.addObject(floor);

                part = Primitives.getSphere(10, 1f);
                part.setTexture("expo_bumps");
                part.setEnvmapped(Object3D.ENVMAP_ENABLED);
                part.build();
                world.addObject(part);

world.getCamera().setPosition(5, 5, -10);
world.getCamera().lookAt(SimpleVector.ORIGIN);
}

private void loop() throws Exception {
buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_NORMAL);
buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
buffer.enableRenderer(IRenderer.RENDERER_OPENGL);

while (!org.lwjgl.opengl.Display.isCloseRequested()) {
buffer.clear(java.awt.Color.BLUE);
world.renderScene(buffer);
world.draw(buffer);
buffer.update();
buffer.displayGLOnly();
Thread.sleep(10);
}
buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
buffer.dispose();
System.exit(0);
}
}

The sphere primitive takes the texture (as do all the other primitives) but the plane doesn't. Why?  ???

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: I can texture any primitive except a plane. Why?
« Reply #1 on: December 08, 2010, 01:32:00 pm »
The u/v coordinates for the primitives are not well defined (they are mainly for testing purposes).  I generally create my own primitives problematically and set the u/v coordinates the way I like them (particularly for planes and cubes).  Here is an example you might find useful:

Code: [Select]
        floor = new Object3D( 2 );
        float offset = scale;  // some float for sizing

        floor.addTriangle( new SimpleVector( -offset, -offset, 0 ),
                           0, 0,
                           new SimpleVector( -offset, offset, 0 ), 0, 1,
                           new SimpleVector( offset, offset, -0 ), 1, 1,
                           TextureManager.getInstance().getTextureID(
                                                             "expo_bumps" ) );
        floor.addTriangle( new SimpleVector( offset, offset, 0 ),
                           1, 1,
                           new SimpleVector( offset, -offset, 0 ), 1, 0,
                           new SimpleVector( -offset, -offset, 0 ), 0, 0,
                           TextureManager.getInstance().getTextureID(
                                                             "expo_bumps" ) );
This is the basic case that creates a 2-poly quad.  For more polys (such as a large terrain), you can put something similar to this inside a couple for loops and use the varriables to calculate "u" and "v" (float values between 0 - 1 representing positions on the texture for each vertice).

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: I can texture any primitive except a plane. Why?
« Reply #2 on: December 08, 2010, 01:35:40 pm »
Albeit this is exactly the opposite case...the only Primitive with well-defined uv-coordinates is the plane. In your code, you are enabling environment mapping for both. I would think that enviroment mapping on a plane just doesn't look good. Try how it looks if you don't enable it for the plane.

Offline apchar

  • byte
  • *
  • Posts: 3
    • View Profile
Re: I can texture any primitive except a plane. Why?
« Reply #3 on: December 09, 2010, 02:16:55 am »
Both solutions worked. Thanks guys.
I guess I don't understand  environment mapping. I thought it yielded the same results only slower. No?
Is the plane supposed to be invisible from the other side? Is there a way to texture both sides without putting 2 planes back-to-back?
I get the impression primitives are rarely used or even discouraged. Why? Are they less efficient than a custom mesh?

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: I can texture any primitive except a plane. Why?
« Reply #4 on: December 09, 2010, 02:23:44 am »
You can disable backface culling by calling setCulling(false) for the Object3D in question.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: I can texture any primitive except a plane. Why?
« Reply #5 on: December 09, 2010, 07:10:38 am »
I get the impression primitives are rarely used or even discouraged. Why? Are they less efficient than a custom mesh?
I consider them to be useful for quick and dirty solutions where you just need a simple object to show something. I don't think that they are suitable for a "real" application....maybe except for the plane, which i'm actually using in some applications.