Author Topic: Texturing blender models  (Read 1901 times)

Offline Yatoom

  • byte
  • *
  • Posts: 8
    • View Profile
Texturing blender models
« on: January 31, 2013, 05:15:55 pm »
Hello, I am new to jPCT. I have some experience with LWJGL and JOGL though.

I am trying to load a house with an animated door, created with Blender into a world.

Rendered with Blender:


When imported by the object-loader:


I think something goes wrong with the back-face culling and the coordinates of the textures, and I'm not sure what to do with setEnvmapped() and  setEnvmapMode(). Furthermore, when I try to enable blending (thing.setBlending(true);), I get an ArrayIndexOutOfBoundsException at the world.draw(buffer); line.

Code: [Select]
import com.threed.jpct.*;
import javax.swing.*;


public class HouseTest {

    private String[] textures = {"door", "wall", "roof", "tile2"};
    private String thingName = "hous1";
    private int thingScale = 1;
    private World world;
    private FrameBuffer buffer;
    private Object3D thing;
    private JFrame frame;
    private int an = 2;//do a walk animation
    private float ind = 0;
    public int height = 600;
    public int width = 800;

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

    public HouseTest() throws Exception {
        frame = new JFrame("Blender Model Loading");
        frame.setSize(width, height);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        world = new World();
        world.setAmbientLight(150, 150, 150);
        for (int i = 0; i < textures.length; ++i) {
            TextureManager.getInstance().addTexture(textures[i] + ".jpg", new Texture("res/" + textures[i] + ".jpg"));
        }

        thing = loadModel("res/" + thingName + ".3ds", thingScale);

        Animation anim = new Animation(4);
        anim.createSubSequence("idle");
        anim.addKeyFrame(thing.getMesh());
       
        anim.createSubSequence("house");
        anim.addKeyFrame(loadModel("res/" + "hous1.3ds", 1).getMesh());
        anim.addKeyFrame(loadModel("res/" + "hous2.3ds", 1).getMesh());
        anim.addKeyFrame(loadModel("res/" + "hous3.3ds", 1).getMesh());
 
        thing.setAnimationSequence(anim);
        world.addObject(thing);     
        world.getCamera().setPosition(0, 0, -20);
        world.getCamera().lookAt(thing.getTransformedCenter());

        thing.setEnvmapped(true);
        thing.setEnvmapMode(false);
    }

    private void loop() throws Exception {
        buffer = new FrameBuffer(width, height, FrameBuffer.SAMPLINGMODE_NORMAL);

        while (frame.isShowing()) {
            doAnim(); 
            buffer.clear(java.awt.Color.BLUE);           
            world.renderScene(buffer);
         
            world.draw(buffer);
            buffer.update();
            buffer.display(frame.getGraphics());
            Thread.sleep(10);
        }
        buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
        buffer.dispose();
        frame.dispose();
        System.exit(0);
    }

    private Object3D loadModel(String filename, float scale) {
        Loader.setVertexOptimization(false);
        Object3D[] model = Loader.load3DS(filename, scale);

        Object3D o3d = new Object3D(0);

        Object3D temp = null;

        for (int i = 0; i < model.length; i++) {
            temp = model[i];
            temp.setCenter(SimpleVector.ORIGIN);
            temp.rotateX((float)( -.5*Math.PI));
            temp.rotateMesh();
            temp.setRotationMatrix(new Matrix());
            o3d = Object3D.mergeObjects(o3d, temp);
            o3d.build();
        }
        return o3d;
    }
    public void doAnim() {
        {
            ind += 0.018f;
            if (ind > 1f) {
                ind -= 1f;
            }
        }
        thing.animate(ind, an);
    }
}

EDIT: Don't mind the door. I forgot the texture.
« Last Edit: January 31, 2013, 05:18:39 pm by Yatoom »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Texturing blender models
« Reply #1 on: January 31, 2013, 05:42:47 pm »
You enable environment mapping on the obj. Don't do that... ;)

Offline Yatoom

  • byte
  • *
  • Posts: 8
    • View Profile
Re: Texturing blender models
« Reply #2 on: January 31, 2013, 07:13:03 pm »
You enable environment mapping on the obj. Don't do that... ;)

Thanks. But I don't think that is the real problem in my case.
When I disable it, it still does not look good.

But, I also experienced a difference between the software renderer and the OpenGL renderer.

Software Renderer:


OpenGL Renderer:

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Texturing blender models
« Reply #3 on: January 31, 2013, 08:07:14 pm »
Thanks. But I don't think that is the real problem in my case.
When I disable it, it still does not look good.
It's just not what you want, so keep it disabled anyway. In your screen shots, it looks like as if no proper texture coordinates are present. Make sure that the export from blender exports them properly. The difference between the renderers comes from inaccuracies in the software renderer when no real uv-coordinates are present. They will go away once you have those.

Offline Yatoom

  • byte
  • *
  • Posts: 8
    • View Profile
Re: Texturing blender models
« Reply #4 on: January 31, 2013, 10:33:18 pm »
Make sure that the export from blender exports them properly.

Thanks! This was the problem. It took me quite some time to figure out what I did wrong and how to fix it. I will explain what I did in short (for people with the same problem).

Initially, I tried to select an object, then in the material-tab (Properties Window), I applied a standard surface-material on it and then, in the texture-tab, select type: Image or Movie and load the desired image file. Then in the Mapping section, I took Coordinates: Global and Projection: Cube. But this when you export the 3ds model, the coordinates are not exported with it.

Fix Coordinates:
Set coordinates to UV and get to the UV-editor in edit mode. Select all faces of the object (press A), then go to mesh --> UV-unwrap --> unwrap. In the UV-editor, scale until you have a nice mapping. Make sure that you apply the scaling you did in object mode to editing mode: select in Object Mode, Object --> Apply --> Scale.

Fix Normals:
Go to view-->properties (or press N), then scroll down to Mesh Display and then under normals, click on the buttons to show the normals of the faces and the normals of the vertices. Then, fix the normals that are incorrect by ctrl-f --> flip normals.

Fix Transparancy:
Continue here: http://www.jpct.net/forum2/index.php/topic,3203.0.html
« Last Edit: February 04, 2013, 02:26:46 am by Yatoom »