13
« on: March 25, 2009, 10:53:22 am »
I don't use lwjgl ,my code is
package org.test;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.File;
import com.threed.jpct.*;
import javax.swing.*;
public class MouseEventDemo {
private World world;
private FrameBuffer buffer;
private JFrame frame;
private TextureManager texMan = null;
private static float distance = 70;
Canvas canvas;
public static void main(String[] args) throws Exception {
MouseEventDemo d=new MouseEventDemo();
d.loop();
}
public MouseEventDemo() throws Exception {
Config.fadeoutLight = true;
Config.maxPolysVisible = 1000000;
frame = new JFrame("MouseTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
world = new World();
world.setAmbientLight(211, 211, 211);
addTexture();
load3dFile();
world.getCamera().lookAt(new SimpleVector(0, 0, 0));
}
private void loop() throws Exception {
buffer = new FrameBuffer(600, 600, FrameBuffer.SAMPLINGMODE_NORMAL);
canvas = buffer.enableGLCanvasRenderer();
canvas.addMouseMotionListener(new MyMouseMotionListener(this));
frame.getContentPane().add(canvas,BorderLayout.CENTER);
frame.setSize(600, 620);
frame.setVisible(true);
while (frame.isShowing()) {
buffer.clear(java.awt.Color.LIGHT_GRAY);
world.renderScene(buffer);
world.draw(buffer);
buffer.update();
buffer.display(canvas.getGraphics());
Thread.sleep(10);
}
buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
buffer.dispose();
frame.dispose();
System.exit(0);
}
public void addTexture() {
texMan = TextureManager.getInstance();
String pathTextura = "impreza/";
File dir = new File(pathTextura);
String texturas[] = dir.list();
for (int i = 0; i < texturas.length; i++) {
if (texturas[i].endsWith(".JPG"))
texMan.addTexture(texturas[i].toUpperCase(), new Texture(
pathTextura + texturas[i]));
}
}
public void load3dFile() {
Object3D[] cars = Loader.loadOBJ("impreza/impreza.obj",
"impreza/impreza.mtl", 2f);
for (Object3D part : cars) {
part.build();
world.getCamera().setPosition(5, 15, 10);
world.addObject(part);
}
}
public void mouse_dragged(MouseEvent e) {
float x= world.getCamera().getDirection().x;
float y= world.getCamera().getDirection().y;
SimpleVector line=new SimpleVector(x, 0, y);
Matrix m=line.normalize().getRotationMatrix();
m.rotateAxis(m.getXAxis(), (float) -Math.PI/2f);
world.getCamera().moveCamera(Camera.CAMERA_MOVEIN, distance);
world.getCamera().rotateAxis(m.invert3x3().getXAxis(), line.length() / 200f);
world.getCamera().moveCamera(Camera.CAMERA_MOVEOUT, distance);
}
}
class MyMouseMotionListener implements MouseMotionListener {
MouseEventDemo demo;
MyMouseMotionListener(MouseEventDemo demo) {
this.demo = demo;
}
public void mouseDragged(MouseEvent e) {
demo.mouse_dragged(e);
}
public void mouseMoved(MouseEvent e) {
}
}
how to rotate object? i.e how to rotate object by rotating camera?Do I have to use lwjgl?