Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - terminal.disease

Pages: [1]
1
Support / Help! My stick figure doesn't do what I want it to
« on: February 02, 2007, 11:07:37 pm »
First and foremost, sorry for the long post.   Now...

Hello all, I have been perusing the forum quite a bit trying to learn jPCT and put it to test.  I must say that I am a total noob to the 3D world, but not to Java.  So to get my feet wet I grabbed Blender and followed one of their tutorials (not to completion, I must admit, as I already had somewhat of a "thing" that didn't look as pathetic as a simple cube); then I downloaded the 1.13 beta so I could import the .obj file, and off I went.

What I am trying to achieve is this:

1. Load a 3D model from an .obj that may contain different "areas" (forgive my non-3D vocabulary)
2. Apply different textures to different areas, and allow the user to change them at runtime
3. Rotate the image on the Y axis so a user can view the stick figure as if the camera was rotating 360 degrees around it.
4. I don't want a continuous loop for rendering the world.  My requirement is to have a static view of the model and only when the user wants to pan around it can that be done (by dragging the mouse, for instance)

So I started by picking off code that I found on the forum, and retrofitted it to suit my needs.  jPCT loads the model fine but when I apply the texture the stick figure only gets darker (perhaps there is something wrong with my primitive model?).  Further, I can't quite get the rotation part figured out for the life of anything.  My stick figure rotates, but not necessarily as I want it to.

I do believe that I can achieve what I want by using jPCT; I've seen the demos and the rendering it does, so I am sure displaying a model, applying/changing textures, rotating the model is not out of its league.

I hope someone can throw me a bone because I am starting to get to the point where frustration is starting to set in.  General questions I still have are:

a) How could I group the Object3D objects that result from Loader.loadOBJ() into "logical" groups"  Say, I want to be able to apply different pant textures to the legs, and different shirt textures to the torso.
b) What is the best way to make my model fit the frame I created?  Is there a method I can call for this or do I have to do the math and figure it out?  If so, how do the dimensions (in 3D) of the stick figure translate to the frame's XY coordinates?

I'll stop myself short here before this gets too insanely long and people decide not to help  :-)  URL, code and model follow, thanks!

The person tutorial...
http://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/Detailing_Your_Simple_Person_1

The code...
Code: [Select]

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import com.threed.jpct.*;

class ModelLoader extends JFrame
{
   private FrameBuffer _buffer = null;
   private World _world = null;
   private TextureManager _texMgr = null;
   private Camera _camera = null;
   private int _width = 800;
   private int _height = 600;
   private Graphics _graphics = null;
   private int _titleBarHeight = 0;
   private int _leftBorderWidth = 0;
   private Object3D[] _model = null;
   private ClassLoader _classLoader = getClass().getClassLoader();

   public ModelLoader()
   {
      super("jPCT " + Config.getVersion());
      Config.glFullscreen = false;
      Config.glMipmap = true;
      Config.glColorDepth = 16;
      Config.maxPolysVisible = 64000;
      Config.fadeoutLight = false;
           
      _world = new World();
      _world.getLights().setOverbrightLighting(Lights.OVERBRIGHT_LIGHTING_DISABLED);
      _world.getLights().setRGBScale(Lights.RGB_SCALE_2X);
      _world.setAmbientLight(25, 30, 30);
      _world.addLight(new SimpleVector(-1000, -150, 1000), 22, 5, 4);
      _world.addLight(new SimpleVector(1000, -150, -1000), 4, 2, 22);      

      _texMgr = TextureManager.getInstance();
      _texMgr.addTexture("tex", new Texture(_classLoader.getResourceAsStream("model/Mat.jpg")));

      _model = Loader.loadOBJ(_classLoader.getResourceAsStream("model/person.obj"),
                              _classLoader.getResourceAsStream("model/person.mtl"),
                              2.0f);
     
      for (int i = 0; i < _model.length; i++)
      {
         _world.addObject(_model[i]);
         _model[i].setTexture("tex");
      }
     
      _camera = _world.getCamera();
      _camera.setPosition(-12, 90, 0);  // ARGGGHH  how can I center the camera over the stick figure?
      _camera.lookAt(_model[0].getTransformedCenter());

      _world.buildAllObjects();
      Config.tuneForOutdoor();

      Insets insets = getInsets();
      _titleBarHeight = insets.top;
      _leftBorderWidth = insets.left;
      setSize(_width + _leftBorderWidth + insets.right, _height + _titleBarHeight + insets.bottom);
      setResizable(false);
      setVisible(true);    
     
      _graphics = getGraphics();

      addMouseMotionListener(new MouseDragListener());

      _buffer = new FrameBuffer(_width, _height, FrameBuffer.SAMPLINGMODE_NORMAL);
      _buffer.enableRenderer(IRenderer.RENDERER_SOFTWARE);
      _buffer.setBoundingBoxMode(FrameBuffer.BOUNDINGBOX_NOT_USED);
      _buffer.optimizeBufferAccess();
      _buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
     
      drawWorld();      
   }

   public static void main(String[] args)
   {
      new ModelLoader();
   }

   private void drawWorld()
   {
      _buffer.clear();
      _world.renderScene(_buffer);
      _world.draw(_buffer);
      _buffer.update();
      _buffer.display(_graphics, _leftBorderWidth, _titleBarHeight);
   }  
   
   private class MouseDragListener implements MouseMotionListener
   {
      final float speed = 0.05f;
      int lastX = 0;
      int lastY = 0;
     
      public void mouseDragged(MouseEvent me)
      {
         int currentX = me.getX();
         int currentY = me.getY();
         
         if (currentX > lastX)
         {
            // the mouse is going to the right
            _model[0].rotateX(speed);
         }
         else if (currentX < lastX)
         {
            // the moust is going to the left
            _model[0].rotateX(-speed);
         }
                 
         if (currentY > lastY)
         {
            // we are going down on the screen
            _model[0].rotateY(speed);
         }
         else if (currentY < lastY)
         {
            // we are going up on the screen
            _model[0].rotateY(-speed);
         }
         
         lastX = currentX;
         lastY = currentY;
         drawWorld();
      }

      public void mouseMoved(MouseEvent arg0)
      {
      }
   }
}


person.mtl
Code: [Select]

# Blender3D MTL File: person.blend
# Material Count: 1
newmtl Material
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2

Pages: [1]