Incidentally , my ultimate goal is Making the world back to the initial state,Namely,back to the state object loaded.
now ,i have reseted the camera in world.
now ,i have reseted the camera in world.
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.
Show posts Menu
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) {
}
}
int x=Mouse.getDX();
int y=Mouse.getDY();
if (Mouse.isButtonDown(0)) {
// Rotate camera around object by holding down the left button
SimpleVector line=new SimpleVector(x, 0, y);
Matrix m=line.normalize().getRotationMatrix();
m.rotateAxis(m.getXAxis(), (float) -Math.PI/2f);
cam.moveCamera(Camera.CAMERA_MOVEIN, distance);
cam.rotateAxis(m.invert3x3().getXAxis(), line.length() / 200f);
cam.moveCamera(Camera.CAMERA_MOVEOUT, distance);
}
frame = new JFrame("MouseTest");
frame.setSize(600, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addMouseWheelListener(new MyMouseWheelListener(this));
frame.addMouseListener(new MyMouseListener(this));
frame.addMouseMotionListener(new MyMouseMotionListener(this));
Page created in 0.021 seconds with 12 queries.