zercha avatar zercha committed 83f01b9

Fixed smooth openGLCamera, fine tuning the speed is possible with CTRL and SHIFT.

Comments (0)

Files changed (2)

src/frontend/Startup.java

 public class Startup {
 
 	private static final String[] modules = {
-	//	"modules.opengl.OpenGLModule",
+		"modules.opengl.OpenGLModule",
 		"modules.raytracer.RaytracerModule",
 		"modules.frontend.SettingsModule"
 	};
 		moduleManager.loadModules(modules);
 
 		final Settings settings = new Settings();
-		final Scene scene = getScene(settings);
+		final Scene scene = getScene();
 		FrontendLink link = new FrontendLink() {
 
 			@Override
 		m3.setColor(new Color(150,150,150));
 		plane.setMaterial(m3);
 		
-		//front wall
-		Plane plane2 = new Plane(new Vector3d(28, 0, 0), new Vector3d(-1, 0, 0));
-		plane2.setMaterial(Material.getMirror());
-		
 		//left wall
 		Plane plane3 = new Plane(new Vector3d(0, 0, 28), new Vector3d(0, 0, -1));
 		Material m5 = new Material();
 		m5.setColor(new Color(150,150,150));
 		plane3.setMaterial(Material.getMirror());
+		
+		//front wall
+		Plane plane2 = new Plane(new Vector3d(28, 0, 0), new Vector3d(-1, 0, 0));
+		plane2.setMaterial(m5);
 	
 		//back wall
 		Plane plane4 = new Plane(new Vector3d(-28, 0, 0), new Vector3d(1, 0, 0));
 		Plane plane5 = new Plane(new Vector3d(0, 0, -28), new Vector3d(0, 0, 1));
 		Material m7 = new Material();
 		m7.setColor(new Color(150,150,150));
-		plane5.setMaterial(m7);
+		plane5.setMaterial(Material.getMirror());
 		
 		//roof
 		Plane plane6 = new Plane(new Vector3d(0, 28, 0), new Vector3d(0, -1, 0));
 		Quad cuboid = new Quad(new Vector3d(8,-2,0), new Vector3d(-1,0,0),4,4);
 		Material red = new Material();
 		red.setColor(Color.red);
-		red.setRefractiveIndex(1);
 		red.setRefractionRatio(0.7);
-		red.setReflectionRatio(0.3);
+//		red.setReflectionRatio(0.3);
 		cuboid.setMaterial(red);
 		
 		Quad cuboid2 = new Quad(new Vector3d(4,-2,0), new Vector3d(-1,0,0),3,3);
 		Material green = new Material();
 		green.setColor(Color.green);
-		green.setRefractiveIndex(1);
 		green.setRefractionRatio(0.7);
 		cuboid2.setMaterial(green);
 		
+		Quad cuboid3 = new Quad(new Vector3d(16,-2,0), new Vector3d(-1,0,0),2,7);
+		Material blue = new Material();
+		blue.setColor(Color.blue);
+		blue.setRefractionRatio(0.3);
+		cuboid3.setMaterial(blue);
+		
 		//front light
 		Sphere light = new Sphere(new Vector3d(0,-1,0),0.1);//(new Vector3d(-5,3,3),0.2);
 		light.setMaterial(Material.getStandardLight());
 		Cuboid light2 = new Cuboid(new Vector3d(-2,-1,0), new Vector3d(0.1,1,1));
 		light2.setMaterial(Material.getStandardLight());
 		
+		Sphere sRed = new Sphere(new Vector3d(0,7,3),1);
+		Sphere sGreen = new Sphere(new Vector3d(0,7,0),1);
+		Sphere sBlue = new Sphere(new Vector3d(0,7,-3),1);
+		
+		Material m97 = new Material();
+		Material m98 = new Material();
+		Material m99 = new Material();
+		
+		m97.setRefractionRatio(0.7);
+		m97.setReflectionRatio(0.4);
+		m98.setRefractionRatio(0.7);
+		m98.setReflectionRatio(0.4);
+		m99.setRefractionRatio(0.7);
+		m99.setReflectionRatio(0.4);
+		m97.setColor(Color.red);
+		m98.setColor(Color.green);
+		m99.setColor(Color.blue);
+		m97.setRefractiveIndex(1.33);
+		m98.setRefractiveIndex(1.33);
+		m99.setRefractiveIndex(1.33);
+		
+		sRed.setMaterial(m97);
+		sGreen.setMaterial(m98);
+		sBlue.setMaterial(m99);
+		
+		
 		scene.setCamera(cam);
 		
 		scene.addShape(plane);
 		scene.addShape(plane4);
 		scene.addShape(plane5);
 		scene.addShape(plane6);
-//		scene.addShape(cuboid);
-//		scene.addShape(cuboid2);
-//		scene.addShape(sphere2);
+		scene.addShape(cuboid);
+		scene.addShape(cuboid2);
+		scene.addShape(cuboid3);
+		scene.addShape(sphere2);
 		scene.addShape(sphere3);
+		scene.addShape(sRed);
+		scene.addShape(sBlue);
+		scene.addShape(sGreen);
 //		scene.addShape(light2);
 		scene.addShape(light);
 

src/opengl/OpenGLInputListener.java

 package opengl;
 
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseMotionListener;
+import java.util.Timer;
+import java.util.TimerTask;
 
 /**
  *
 	private OpenGLPanel panel;
 	private Camera camera;
 	private final MouseGrip mg;
+	
+	private boolean holdMoveKey[] = new boolean[]{false,false,false,false,false,false};
+	private Timer moveTimer = new Timer();
+	private int inputModifiers = 0;
 
 	public OpenGLInputListener(OpenGLPanel panel, Camera camera) {
 		this.camera = camera;
 
 		mg = new MouseGrip(camera, MouseGrip.MouseGripMode.NONE);
 		mg.setGrip(true);
+		moveTimer.scheduleAtFixedRate(new TimerTask(){
+
+			@Override
+			public void run() {
+				double speed = 0;
+				if(inputModifiers == 0)
+					speed = 0.25;
+				else if(inputModifiers == 1)
+					speed = 0.02;
+				else if(inputModifiers == 2)
+					speed = 3;
+
+				Camera camera = OpenGLInputListener.this.camera;
+				double angleX = Math.toRadians(camera.getRotation().y + 90);
+				double angleY = Math.toRadians(camera.getRotation().y);
+				
+				if(holdMoveKey[0]){
+					camera.setPosition(camera.getPosition().addZ(-Math.cos(angleX)*speed));
+					camera.setPosition(camera.getPosition().addX(-Math.sin(angleX)*speed));
+				}
+				if(holdMoveKey[1]){
+					camera.setPosition(camera.getPosition().addZ( Math.cos(angleX)*speed));
+					camera.setPosition(camera.getPosition().addX( Math.sin(angleX)*speed));
+				}
+				if(holdMoveKey[2]){
+					camera.setPosition(camera.getPosition().addX(camera.getDirection().x*speed));
+					camera.setPosition(camera.getPosition().addY(camera.getDirection().y*speed));
+					camera.setPosition(camera.getPosition().addZ(camera.getDirection().z*speed));
+				}
+				if(holdMoveKey[3]){
+					camera.setPosition(camera.getPosition().addZ( Math.cos(angleY) * speed));
+					camera.setPosition(camera.getPosition().addX( Math.sin(angleY) * speed));
+				}
+				if(holdMoveKey[4]){
+					camera.setPosition(camera.getPosition().addY(-speed));
+				}
+				if(holdMoveKey[5]){
+					camera.setPosition(camera.getPosition().addY(speed));
+				}
+
+				
+			}
+			
+		}, 0, 10);
 	}
 
 	public void keyPressed(KeyEvent e) {
 		double speed = 1;
 
-		double angleX = Math.toRadians(camera.getRotation().y + 90);
-		double angleY = Math.toRadians(camera.getRotation().y);
-		
-		if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {
-			camera.setPosition(camera.getPosition().addZ(-Math.cos(angleX)*speed));
-			camera.setPosition(camera.getPosition().addX(-Math.sin(angleX)*speed));
+		if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A && !holdMoveKey[0]) {
+			holdMoveKey[0] = true;
 		} 
-		if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
-			camera.setPosition(camera.getPosition().addZ( Math.cos(angleX)*speed));
-			camera.setPosition(camera.getPosition().addX( Math.sin(angleX)*speed));
+		if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D && !holdMoveKey[1]) {
+			holdMoveKey[1] = true;
 		} 
-		if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
-			camera.setPosition(camera.getPosition().addX(camera.getDirection().x*speed));
-			camera.setPosition(camera.getPosition().addY(camera.getDirection().y*speed));
-			camera.setPosition(camera.getPosition().addZ(camera.getDirection().z*speed));
-//			camera.setPosition(camera.getPosition().addZ(-Math.cos(angleY) * speed));
-//			camera.setPosition(camera.getPosition().addX(-Math.sin(angleY) * speed));
+		if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W && !holdMoveKey[2]) {
+			holdMoveKey[2] = true;
 		} 
-		if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
-			camera.setPosition(camera.getPosition().addZ( Math.cos(angleY) * speed));
-			camera.setPosition(camera.getPosition().addX( Math.sin(angleY) * speed));
+		if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S && !holdMoveKey[3]) {
+			holdMoveKey[3] = true;
 		} 
-		if (e.getKeyCode() == KeyEvent.VK_ADD || e.getKeyCode() == KeyEvent.VK_Q) {
-			camera.setPosition(camera.getPosition().addY(-speed));
+		if (e.getKeyCode() == KeyEvent.VK_ADD || e.getKeyCode() == KeyEvent.VK_Q && !holdMoveKey[4]) {
+			holdMoveKey[4] = true;
 		} 
-		if (e.getKeyCode() == KeyEvent.VK_SUBTRACT || e.getKeyCode() == KeyEvent.VK_E) {
-			camera.setPosition(camera.getPosition().addY( speed));
+		if (e.getKeyCode() == KeyEvent.VK_SUBTRACT || e.getKeyCode() == KeyEvent.VK_E && !holdMoveKey[5]) {
+			holdMoveKey[5] = true;
 		} 
 		if (e.getKeyCode() == KeyEvent.VK_NUMPAD8) {
 			camera.setPosition(camera.getPosition().addX( speed));
 		if (e.getKeyCode() == KeyEvent.VK_NUMPAD6) {
 			camera.setRotation(camera.getRotation().addY(-speed));
 		} 
+		if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
+			inputModifiers = 1;
+		}
+		if (e.getKeyCode() == KeyEvent.VK_CONTROL){
+			inputModifiers = 2;
+		}
 		else if (e.getKeyCode() == KeyEvent.VK_M) {
 			// Wireframe
 		} 
 			panel.syncCamera();
 		}
 	}
-	
-//	public void keyPressed(KeyEvent e) {
-//		double speed = 1;
-//
-//		double angleX = Math.toRadians(camera.getRotation().y + 90);
-//		double angleY = Math.toRadians(camera.getRotation().y);
-//		
-//		if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {
-//			camera.setPosition(camera.getPosition().addZ(-Math.cos(angleX)*speed));
-//			camera.setPosition(camera.getPosition().addX(-Math.sin(angleX)*speed));
-//		} else if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
-//			camera.setPosition(camera.getPosition().addZ( Math.cos(angleX)*speed));
-//			camera.setPosition(camera.getPosition().addX( Math.sin(angleX)*speed));
-//		} else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
-//			camera.setPosition(camera.getPosition().addZ(-Math.cos(angleY) * speed));
-//			camera.setPosition(camera.getPosition().addX(-Math.sin(angleY) * speed));
-//		} else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
-//			camera.setPosition(camera.getPosition().addZ( Math.cos(angleY) * speed));
-//			camera.setPosition(camera.getPosition().addX( Math.sin(angleY) * speed));
-//		} else if (e.getKeyCode() == KeyEvent.VK_ADD || e.getKeyCode() == KeyEvent.VK_Q) {
-//			camera.setPosition(camera.getPosition().addY(-speed));
-//		} else if (e.getKeyCode() == KeyEvent.VK_SUBTRACT || e.getKeyCode() == KeyEvent.VK_E) {
-//			camera.setPosition(camera.getPosition().addY( speed));
-//		} else if (e.getKeyCode() == KeyEvent.VK_NUMPAD8) {
-//			camera.setPosition(camera.getPosition().addX( speed));
-//		} else if (e.getKeyCode() == KeyEvent.VK_NUMPAD2) {
-//			camera.setPosition(camera.getPosition().addX(-speed));
-//		} else if (e.getKeyCode() == KeyEvent.VK_NUMPAD4) {
-//			camera.setRotation(camera.getRotation().addY( speed));
-//		} else if (e.getKeyCode() == KeyEvent.VK_NUMPAD6) {
-//			camera.setRotation(camera.getRotation().addY(-speed));
-//		} else if (e.getKeyCode() == KeyEvent.VK_M) {
-//			// Wireframe
-//		} else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
-//			panel.syncCamera();
-//		}
-//	}
 
 	public void keyReleased(KeyEvent e) {
-		// Nothing...
+		if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {
+			holdMoveKey[0] = false;
+		} 
+		if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
+			holdMoveKey[1] = false;
+		} 
+		if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
+			holdMoveKey[2] = false;
+		} 
+		if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
+			holdMoveKey[3] = false;
+		} 
+		if (e.getKeyCode() == KeyEvent.VK_ADD || e.getKeyCode() == KeyEvent.VK_Q) {
+			holdMoveKey[4] = false;
+		} 
+		if (e.getKeyCode() == KeyEvent.VK_SUBTRACT || e.getKeyCode() == KeyEvent.VK_E) {
+			holdMoveKey[5] = false;
+		} 
+		if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
+			inputModifiers = 0;
+		}
+		if (e.getKeyCode() == KeyEvent.VK_CONTROL){
+			inputModifiers = 0;
+		}
+
 	}
 
 	public void keyTyped(KeyEvent e) {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.