Difference between revisions of "Mouse follow demo"

From JPCT
Jump to: navigation, search
(Created page with '=== Mouse follow demo === <pre> import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import javax.swin…')
 
Line 25: Line 25:
  
 
private static final long serialVersionUID = 1L;
 
private static final long serialVersionUID = 1L;
+
 
 
private Graphics g = null;
 
private Graphics g = null;
 
private FrameBuffer fb = null;
 
private FrameBuffer fb = null;
Line 37: Line 37:
 
private boolean doloop = true;
 
private boolean doloop = true;
  
private int x=320;
+
private int x = 320;
private int y=240;
+
private int y = 240;
  
 
public MouseFollowDemo() {
 
public MouseFollowDemo() {
Line 52: Line 52:
  
 
public void mouseMoved(MouseEvent m) {
 
public void mouseMoved(MouseEvent m) {
x=m.getX();
+
x = m.getX();
y=m.getY();
+
y = m.getY();
 
}
 
}
  
public void mouseDragged(MouseEvent m) {}
+
public void mouseDragged(MouseEvent m) {
 +
}
  
 
private void initStuff() {
 
private void initStuff() {
Line 67: Line 68:
 
plane = Primitives.getPlane(20, 10);
 
plane = Primitives.getPlane(20, 10);
 
plane.setAdditionalColor(Color.GREEN);
 
plane.setAdditionalColor(Color.GREEN);
sphere=Primitives.getSphere(30);
+
sphere = Primitives.getSphere(30);
 
sphere.setAdditionalColor(Color.CYAN);
 
sphere.setAdditionalColor(Color.CYAN);
sphere.translate(-50,10,50);
+
sphere.translate(-50, 10, 50);
cube2=Primitives.getCube(20);
+
cube2 = Primitives.getCube(20);
 
cube2.setAdditionalColor(Color.ORANGE);
 
cube2.setAdditionalColor(Color.ORANGE);
cube2.translate(60,-20,60);
+
cube2.translate(60, -20, 60);
  
 
plane.rotateX((float) Math.PI / 2f);
 
plane.rotateX((float) Math.PI / 2f);
Line 100: Line 101:
  
 
private void follow() {
 
private void follow() {
SimpleVector ray=Interact2D.reproject2D3D(world.getCamera(), fb, x, y);
+
SimpleVector ray = Interact2D.reproject2D3DWS(world.getCamera(), fb, x, y);
if (ray!=null) {
+
if (ray != null) {
 
+
ray = ray.normalize();
SimpleVector norm=ray.normalize();
+
Matrix mat=world.getCamera().getBack();
+
mat=mat.invert3x3();
+
norm.matMul(mat);
+
  
float f=world.calcMinDistance(world.getCamera().getPosition(), norm, 1000);
+
float f = world.calcMinDistance(world.getCamera().getPosition(), ray, 1000);
if (f!=Object3D.COLLISION_NONE) {
+
if (f != Object3D.COLLISION_NONE) {
SimpleVector offset=new SimpleVector(norm);
+
SimpleVector offset = new SimpleVector(ray);
norm.scalarMul(f);
+
ray.scalarMul(f);
norm=norm.calcSub(offset);
+
ray = ray.calcSub(offset);
 
cube.getTranslationMatrix().setIdentity();
 
cube.getTranslationMatrix().setIdentity();
cube.translate(norm);
+
cube.translate(ray);
 
cube.translate(world.getCamera().getPosition());
 
cube.translate(world.getCamera().getPosition());
 
}
 
}
Line 128: Line 125:
 
world.getLights().setOverbrightLighting(Lights.OVERBRIGHT_LIGHTING_DISABLED);
 
world.getLights().setOverbrightLighting(Lights.OVERBRIGHT_LIGHTING_DISABLED);
  
while(doloop) {
+
while (doloop) {
 
follow();
 
follow();
 
fb.clear();
 
fb.clear();
Line 146: Line 143:
 
cd.doIt();
 
cd.doIt();
 
}
 
}
 +
}
 
</pre>
 
</pre>
  
 
[[Category:jPCT]]
 
[[Category:jPCT]]
 
[[Category:jPCT-AE]]
 
[[Category:jPCT-AE]]

Revision as of 18:45, 9 May 2016

Mouse follow demo


import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.IRenderer;
import com.threed.jpct.Interact2D;
import com.threed.jpct.Lights;
import com.threed.jpct.Matrix;
import com.threed.jpct.Object3D;
import com.threed.jpct.Primitives;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.World;
import com.threed.jpct.util.Light;

public class MouseFollowDemo extends JFrame implements MouseMotionListener {

	private static final long serialVersionUID = 1L;

	private Graphics g = null;
	private FrameBuffer fb = null;
	private World world = null;
	private Object3D plane = null;
	private Object3D ramp = null;
	private Object3D cube = null;
	private Object3D cube2 = null;
	private Object3D sphere = null;

	private boolean doloop = true;

	private int x = 320;
	private int y = 240;

	public MouseFollowDemo() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setSize(640, 480);
		setResizable(false);
		setLocationRelativeTo(null);
		setVisible(true);
		this.addMouseMotionListener(this);
		g = getGraphics();
	}

	public void mouseMoved(MouseEvent m) {
		x = m.getX();
		y = m.getY();
	}

	public void mouseDragged(MouseEvent m) {
	}

	private void initStuff() {
		fb = new FrameBuffer(640, 480, FrameBuffer.SAMPLINGMODE_NORMAL);
		world = new World();
		fb.enableRenderer(IRenderer.RENDERER_SOFTWARE);

		ramp = Primitives.getCube(20);
		ramp.setAdditionalColor(Color.red);
		plane = Primitives.getPlane(20, 10);
		plane.setAdditionalColor(Color.GREEN);
		sphere = Primitives.getSphere(30);
		sphere.setAdditionalColor(Color.CYAN);
		sphere.translate(-50, 10, 50);
		cube2 = Primitives.getCube(20);
		cube2.setAdditionalColor(Color.ORANGE);
		cube2.translate(60, -20, 60);

		plane.rotateX((float) Math.PI / 2f);
		ramp.rotateX((float) Math.PI / 2f);

		cube = Primitives.getCube(2);

		plane.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
		ramp.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
		sphere.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
		cube2.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);

		world.addObject(plane);
		world.addObject(ramp);
		world.addObject(cube);
		world.addObject(sphere);
		world.addObject(cube2);

		cube.translate(-50, -10, -50);

		Light light = new Light(world);
		light.setPosition(new SimpleVector(0, -80, 0));
		light.setIntensity(40, 25, 22);

		world.buildAllObjects();
	}

	private void follow() {
		SimpleVector ray = Interact2D.reproject2D3DWS(world.getCamera(), fb, x, y);
		if (ray != null) {
			ray = ray.normalize();

			float f = world.calcMinDistance(world.getCamera().getPosition(), ray, 1000);
			if (f != Object3D.COLLISION_NONE) {
				SimpleVector offset = new SimpleVector(ray);
				ray.scalarMul(f);
				ray = ray.calcSub(offset);
				cube.getTranslationMatrix().setIdentity();
				cube.translate(ray);
				cube.translate(world.getCamera().getPosition());
			}
		}
	}

	private void doIt() throws Exception {
		Camera cam = world.getCamera();
		cam.moveCamera(Camera.CAMERA_MOVEOUT, 100);
		cam.moveCamera(Camera.CAMERA_MOVEUP, 160);
		cam.lookAt(plane.getTransformedCenter());

		world.getLights().setOverbrightLighting(Lights.OVERBRIGHT_LIGHTING_DISABLED);

		while (doloop) {
			follow();
			fb.clear();
			world.renderScene(fb);
			world.draw(fb);
			fb.update();
			fb.display(g);
			Thread.sleep(10);
		}
		fb.disableRenderer(IRenderer.RENDERER_OPENGL);
		System.exit(0);
	}

	public static void main(String[] args) throws Exception {
		MouseFollowDemo cd = new MouseFollowDemo();
		cd.initStuff();
		cd.doIt();
	}
}