Implementing Rotation of the camera by mouse input
This commit is contained in:
		
							parent
							
								
									7acf9fd703
								
							
						
					
					
						commit
						98b6208c74
					
				| @ -8,12 +8,14 @@ import core.engine.model.TexturedModel; | |||||||
| import core.engine.renderer.MasterRenderer; | import core.engine.renderer.MasterRenderer; | ||||||
| import core.engine.terrain.Terrain; | import core.engine.terrain.Terrain; | ||||||
| import core.engine.textures.ModelTexture; | import core.engine.textures.ModelTexture; | ||||||
|  | import org.lwjgl.BufferUtils; | ||||||
| import org.lwjgl.Version; | import org.lwjgl.Version; | ||||||
| import org.lwjgl.glfw.*; | import org.lwjgl.glfw.*; | ||||||
| import org.lwjgl.opengl.GL; | import org.lwjgl.opengl.GL; | ||||||
| import org.lwjgl.system.MemoryStack; | import org.lwjgl.system.MemoryStack; | ||||||
| import utils.vectors.Vector3f; | import utils.vectors.Vector3f; | ||||||
| 
 | 
 | ||||||
|  | import java.nio.DoubleBuffer; | ||||||
| import java.nio.IntBuffer; | import java.nio.IntBuffer; | ||||||
| 
 | 
 | ||||||
| import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks; | import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks; | ||||||
| @ -30,6 +32,10 @@ public class Engine { | |||||||
|     private final Loader loader = new Loader(); |     private final Loader loader = new Loader(); | ||||||
|     public static int WINDOW_WIDTH = 1280; |     public static int WINDOW_WIDTH = 1280; | ||||||
|     public static int WINDOW_HEIGHT = 720; |     public static int WINDOW_HEIGHT = 720; | ||||||
|  |     private static boolean isMouseWheelPressed = false; | ||||||
|  |     private static DoubleBuffer lastCursorX = BufferUtils.createDoubleBuffer(1); | ||||||
|  |     private static DoubleBuffer lastCursorY = BufferUtils.createDoubleBuffer(1); | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
|     public Engine() { |     public Engine() { | ||||||
| 
 | 
 | ||||||
| @ -112,41 +118,7 @@ public class Engine { | |||||||
|         glClearColor(1.0f, 0.0f, 0.0f, 0.0f); |         glClearColor(1.0f, 0.0f, 0.0f, 0.0f); | ||||||
|         Camera camera = new Camera(); |         Camera camera = new Camera(); | ||||||
| 
 | 
 | ||||||
|         GLFWScrollCallback glfwScrollCallback = new GLFWScrollCallback() { |         input(camera); | ||||||
|             @Override |  | ||||||
|             public void invoke(long window, double xOffset, double yOffset) { |  | ||||||
|                 if(yOffset > 0) { |  | ||||||
|                     camera.zoomIn(); |  | ||||||
|                 } else if(yOffset < 0) { |  | ||||||
|                     camera.zoomOut(); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
| 
 |  | ||||||
|         glfwSetScrollCallback(window, glfwScrollCallback); |  | ||||||
|         glfwSetKeyCallback(window, glfwKeyCallback = new GLFWKeyCallback() { |  | ||||||
|             @Override |  | ||||||
|             public void invoke(long window, int key, int scancode, int action, int mods) { |  | ||||||
|                 if(key == GLFW_KEY_A) { |  | ||||||
|                     //move camera to left |  | ||||||
|                     camera.moveLeft(); |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 if(key == GLFW_KEY_D) { |  | ||||||
|                     //move camera to right |  | ||||||
|                     camera.moveRight(); |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 if(key == GLFW_KEY_W) { |  | ||||||
|                     camera.moveForward(); |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 if(key == GLFW_KEY_S) { |  | ||||||
|                     camera.moveBackward(); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         }); |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
|         Light light = new Light(new Vector3f(0,0,-20), new Vector3f(1,1,1)); |         Light light = new Light(new Vector3f(0,0,-20), new Vector3f(1,1,1)); | ||||||
|         RawModel model = OBJLoader.loadOBJModel("dragon", loader); |         RawModel model = OBJLoader.loadOBJModel("dragon", loader); | ||||||
| @ -177,4 +149,89 @@ public class Engine { | |||||||
|         loader.cleanUp(); |         loader.cleanUp(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     private void input(Camera camera) { | ||||||
|  | 
 | ||||||
|  |         GLFWScrollCallback glfwScrollCallback = new GLFWScrollCallback() { | ||||||
|  |             @Override | ||||||
|  |             public void invoke(long window, double xOffset, double yOffset) { | ||||||
|  |                 if(yOffset > 0) { | ||||||
|  |                     camera.zoomIn(); | ||||||
|  |                 } else if(yOffset < 0) { | ||||||
|  |                     camera.zoomOut(); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|  |         GLFWMouseButtonCallback glfwMouseButtonCallback = new GLFWMouseButtonCallback() { | ||||||
|  |             @Override | ||||||
|  |             public void invoke(long window, int button, int action, int mods) { | ||||||
|  |                 if (button == GLFW.GLFW_MOUSE_BUTTON_MIDDLE && action == GLFW.GLFW_PRESS) { | ||||||
|  |                     // Middle mouse button pressed | ||||||
|  |                     isMouseWheelPressed = true; | ||||||
|  |                     glfwGetCursorPos(window, lastCursorX, lastCursorY); | ||||||
|  |                 } else if (button == GLFW.GLFW_MOUSE_BUTTON_MIDDLE && action == GLFW.GLFW_RELEASE) { | ||||||
|  |                     // Middle mouse button released | ||||||
|  |                     isMouseWheelPressed = false; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|  |         // Define the cursor position callback function | ||||||
|  |         GLFWCursorPosCallback cursorPosCallback = new GLFWCursorPosCallback() { | ||||||
|  |             @Override | ||||||
|  |             public void invoke(long window, double xpos, double ypos) { | ||||||
|  |                 if (isMouseWheelPressed) { | ||||||
|  |                     // Calculate the offset | ||||||
|  |                     double xOffset = xpos - lastCursorX.get(0); | ||||||
|  |                     double yOffset = ypos - lastCursorY.get(0); | ||||||
|  | 
 | ||||||
|  |                     // Use xOffset and yOffset for your desired functionality | ||||||
|  |                     if(yOffset > 0) { | ||||||
|  |                         camera.decreasePitch(); | ||||||
|  |                     } else if(yOffset < 0) { | ||||||
|  |                         camera.increasePitch(); | ||||||
|  |                     } | ||||||
|  | 
 | ||||||
|  |                     if(xOffset > 0) { | ||||||
|  |                         camera.decreaseYaw(); | ||||||
|  |                     } else if(xOffset < 0) { | ||||||
|  |                         camera.increaseYaw(); | ||||||
|  |                     } | ||||||
|  | 
 | ||||||
|  |                     // Update the last cursor position | ||||||
|  |                     lastCursorX.put(0, xpos); | ||||||
|  |                     lastCursorY.put(0, ypos); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|  |         glfwSetMouseButtonCallback(window, glfwMouseButtonCallback); | ||||||
|  |         glfwSetCursorPosCallback(window, cursorPosCallback); | ||||||
|  | 
 | ||||||
|  |         glfwSetScrollCallback(window, glfwScrollCallback); | ||||||
|  |         glfwSetKeyCallback(window, glfwKeyCallback = new GLFWKeyCallback() { | ||||||
|  |             @Override | ||||||
|  |             public void invoke(long window, int key, int scancode, int action, int mods) { | ||||||
|  |                 if(key == GLFW_KEY_A) { | ||||||
|  |                     //move camera to left | ||||||
|  |                     camera.moveLeft(); | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |                 if(key == GLFW_KEY_D) { | ||||||
|  |                     //move camera to right | ||||||
|  |                     camera.moveRight(); | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |                 if(key == GLFW_KEY_W) { | ||||||
|  |                     camera.moveForward(); | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |                 if(key == GLFW_KEY_S) { | ||||||
|  |                     camera.moveBackward(); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -33,6 +33,22 @@ public class Camera { | |||||||
|         position.y -= 0.5f; |         position.y -= 0.5f; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public void increasePitch() { | ||||||
|  |         this.pitch += .5f; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void decreasePitch() { | ||||||
|  |         this.pitch -= .5f; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void increaseYaw() { | ||||||
|  |         this.yaw += 0.5f; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void decreaseYaw() { | ||||||
|  |         this.yaw -= 0.5f; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     public Vector3f getPosition() { |     public Vector3f getPosition() { | ||||||
|         return position; |         return position; | ||||||
|     } |     } | ||||||
| @ -48,4 +64,16 @@ public class Camera { | |||||||
|     public float getRoll() { |     public float getRoll() { | ||||||
|         return roll; |         return roll; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     public void setPitch(float pitch) { | ||||||
|  |         this.pitch += pitch; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setYaw(float yaw) { | ||||||
|  |         this.yaw += yaw; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setRoll(float roll) { | ||||||
|  |         this.roll += roll; | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user