-
Notifications
You must be signed in to change notification settings - Fork 0
Scene view #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Scene view #8
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
edf8a1b
fix(BufferedImageRaster): fix strange frame rendering bug
nikitavbv dc0e1b2
feat(ControllableCamera): camera controllable by keyboard and mouse
nikitavbv 23cfe7b
feat(scenes): add DemoScene
nikitavbv ad7ed84
feat(troubleshooting): add troubleshooting notes
nikitavbv c69d679
refactor(tests): remove MainTests
nikitavbv 34c96ff
feat(Input): add tests
nikitavbv e76590c
feat(ControllableCamera): add tests
nikitavbv 21caa87
feat(scene): add scene tests
nikitavbv 1cfe846
feat(CameraTests): update with raster check
nikitavbv bf9c2d3
refactor(ControllableCamera): add KeyAndMouseMovementController for m…
nikitavbv 321c84d
feat(BasicRatraycingRender): add constructors taking scene or camera
nikitavbv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,39 @@ | ||
| package labs.introtoprogramming.lab5; | ||
|
|
||
| import labs.introtoprogramming.lab5.exception.NotImplementedException; | ||
| import labs.introtoprogramming.lab5.controllers.KeyAndMouseMovementController; | ||
| import labs.introtoprogramming.lab5.geometry.Vector3; | ||
| import labs.introtoprogramming.lab5.graphics.BufferedImageRaster; | ||
| import labs.introtoprogramming.lab5.graphics.Raster; | ||
| import labs.introtoprogramming.lab5.gui.SceneRendererWindow; | ||
| import labs.introtoprogramming.lab5.scene.Scene; | ||
| import labs.introtoprogramming.lab5.scene.Camera; | ||
| import labs.introtoprogramming.lab5.scene.Transform; | ||
| import labs.introtoprogramming.lab5.scenes.DemoScene; | ||
|
|
||
| public class Main { | ||
|
|
||
| private static final String APP_TILE = "ray tracing demo"; | ||
| private static final int SCREEN_WIDTH = 640; | ||
| private static final int SCREEN_HEIGHT = 640; | ||
|
|
||
| public static void main(String[] args) { | ||
| throw new NotImplementedException(); | ||
| new Main().run(); | ||
| } | ||
|
|
||
| private void run() { | ||
| Scene demoScene = new DemoScene(); | ||
| Camera camera = createCamera(); | ||
| demoScene.addSceneObjects(camera); | ||
| demoScene.addController(new KeyAndMouseMovementController(camera)); | ||
|
|
||
| SceneRendererWindow window = new SceneRendererWindow(demoScene); | ||
| window.setTitle(APP_TILE); | ||
| window.setDisplayFPS(true); | ||
| window.show(); | ||
| } | ||
|
|
||
| private Camera createCamera() { | ||
| Raster raster = new BufferedImageRaster(SCREEN_WIDTH, SCREEN_HEIGHT); | ||
| return new Camera(raster, new Transform(Vector3.ZERO)); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/labs/introtoprogramming/lab5/controllers/Controller.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package labs.introtoprogramming.lab5.controllers; | ||
|
|
||
| import labs.introtoprogramming.lab5.gui.Input; | ||
|
|
||
| public abstract class Controller { | ||
|
|
||
| public abstract void update(int delta, Input input); | ||
|
|
||
| } |
28 changes: 28 additions & 0 deletions
28
src/main/java/labs/introtoprogramming/lab5/controllers/KeyAndMouseMovementController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package labs.introtoprogramming.lab5.controllers; | ||
|
|
||
| import labs.introtoprogramming.lab5.gui.Input; | ||
| import labs.introtoprogramming.lab5.scene.SceneObject; | ||
|
|
||
| public class KeyAndMouseMovementController extends Controller { | ||
|
|
||
| private static double DEFAULT_MOVEMENT_SPEED = 0.0015; | ||
| private static double DEFAULT_MOUSE_SENSITIVITY = 0.006; | ||
| private double movementSpeed = DEFAULT_MOVEMENT_SPEED; | ||
| private double mouseSensitivity = DEFAULT_MOUSE_SENSITIVITY; | ||
|
|
||
| private SceneObject controllableObject; | ||
|
|
||
| public KeyAndMouseMovementController(SceneObject controllableObject) { | ||
| this.controllableObject = controllableObject; | ||
| } | ||
|
|
||
| @Override | ||
| public void update(int delta, Input input) { | ||
| controllableObject.getTransform().setPosition(controllableObject.getTransform().position().add( | ||
| input.movementVector().multiply(movementSpeed * delta) | ||
| )); | ||
| controllableObject.getTransform().setRotation(controllableObject.getTransform().rotation().add( | ||
| input.mouseMovement().multiply(mouseSensitivity * delta) | ||
| )); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package labs.introtoprogramming.lab5.gui; | ||
|
|
||
| import labs.introtoprogramming.lab5.geometry.Vector3; | ||
|
|
||
| public class Input { | ||
|
|
||
| private boolean forwardKeyDown = false; | ||
| private boolean backwardKeyDown = false; | ||
| private boolean leftKeyDown = false; | ||
| private boolean rightKeyDown = false; | ||
| private boolean upKeyDown = false; | ||
| private boolean downKeyDown = false; | ||
|
|
||
| private double mouseDeltaX = 0; | ||
| private double mouseDeltaY = 0; | ||
|
|
||
| public Vector3 movementVector() { | ||
| return new Vector3((leftKeyDown ? -1 : 0) + (rightKeyDown ? 1 : 0), | ||
| (upKeyDown ? 1 : 0) + (downKeyDown ? -1 : 0), | ||
| (forwardKeyDown ? -1 : 0) + (backwardKeyDown ? 1 : 0)); | ||
| } | ||
|
|
||
| public Vector3 mouseMovement() { | ||
| //noinspection SuspiciousNameCombination | ||
| return new Vector3(mouseDeltaY, mouseDeltaX, 0); | ||
| } | ||
|
|
||
| public void setMouseDeltaX(double mouseDeltaX) { | ||
| this.mouseDeltaX = mouseDeltaX; | ||
| } | ||
|
|
||
| public void setMouseDeltaY(double mouseDeltaY) { | ||
| this.mouseDeltaY = mouseDeltaY; | ||
| } | ||
|
|
||
| public void setForwardKeyDown(boolean forwardKeyDown) { | ||
| this.forwardKeyDown = forwardKeyDown; | ||
| } | ||
|
|
||
| public void setBackwardKeyDown(boolean backwardKeyDown) { | ||
| this.backwardKeyDown = backwardKeyDown; | ||
| } | ||
|
|
||
| public void setLeftKeyDown(boolean leftKeyDown) { | ||
| this.leftKeyDown = leftKeyDown; | ||
| } | ||
|
|
||
| public void setRightKeyDown(boolean rightKeyDown) { | ||
| this.rightKeyDown = rightKeyDown; | ||
| } | ||
|
|
||
| public void setUpKeyDown(boolean upKeyDown) { | ||
| this.upKeyDown = upKeyDown; | ||
| } | ||
|
|
||
| public void setDownKeyDown(boolean downKeyDown) { | ||
| this.downKeyDown = downKeyDown; | ||
| } | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
src/main/java/labs/introtoprogramming/lab5/gui/InputKeyListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package labs.introtoprogramming.lab5.gui; | ||
|
|
||
| import java.awt.event.KeyEvent; | ||
| import java.awt.event.KeyListener; | ||
|
|
||
| class InputKeyListener implements KeyListener { | ||
|
|
||
| private Input input; | ||
|
|
||
| InputKeyListener(Input input) { | ||
| this.input = input; | ||
| } | ||
|
|
||
| @Override | ||
| public void keyTyped(KeyEvent e) { | ||
| // ignore | ||
| } | ||
|
|
||
| @Override | ||
| public void keyPressed(KeyEvent e) { | ||
| keyCodeUpdate(e.getKeyCode(), true); | ||
| } | ||
|
|
||
| @Override | ||
| public void keyReleased(KeyEvent e) { | ||
| keyCodeUpdate(e.getKeyCode(), false); | ||
| } | ||
|
|
||
| private void keyCodeUpdate(int keyCode, boolean isDown) { | ||
| if (keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_UP) { | ||
| input.setForwardKeyDown(isDown); | ||
| } | ||
| if (keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN) { | ||
| input.setBackwardKeyDown(isDown); | ||
| } | ||
| if (keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT) { | ||
| input.setLeftKeyDown(isDown); | ||
| } | ||
| if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT) { | ||
| input.setRightKeyDown(isDown); | ||
| } | ||
| if (keyCode == KeyEvent.VK_Q) { | ||
| input.setUpKeyDown(isDown); | ||
| } | ||
| if (keyCode == KeyEvent.VK_Z) { | ||
| input.setDownKeyDown(isDown); | ||
| } | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
src/main/java/labs/introtoprogramming/lab5/gui/MouseController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package labs.introtoprogramming.lab5.gui; | ||
|
|
||
| import java.awt.AWTException; | ||
| import java.awt.Cursor; | ||
| import java.awt.MouseInfo; | ||
| import java.awt.Point; | ||
| import java.awt.Robot; | ||
| import java.awt.Toolkit; | ||
| import java.awt.image.BufferedImage; | ||
| import javax.swing.JFrame; | ||
|
|
||
| class MouseController { | ||
|
|
||
| private static final String BLANK_CURSOR_NAME = "blank cursor"; | ||
|
|
||
| private Input input; | ||
| private JFrame frame; | ||
|
|
||
| private Robot robot; | ||
| private Point prevLocation; | ||
|
|
||
| MouseController(Input input, JFrame frame) { | ||
| this.input = input; | ||
| this.frame = frame; | ||
| try { | ||
| this.robot = new Robot(); | ||
| } catch(AWTException e) { | ||
| throw new AssertionError(e); | ||
| } | ||
| } | ||
|
|
||
| void hideCursor() { | ||
| BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); | ||
| Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor( | ||
| cursorImg, new Point(0, 0), BLANK_CURSOR_NAME); | ||
| frame.setCursor(blankCursor); | ||
| } | ||
|
|
||
| void update() { | ||
| if (!frame.isFocused()) { | ||
| return; | ||
| } | ||
|
|
||
| int centerX = frame.getX() + frame.getWidth() / 2; | ||
| int centerY = frame.getY() + frame.getHeight() / 2; | ||
| Point mouseLocation = MouseInfo.getPointerInfo().getLocation(); | ||
|
|
||
| if (prevLocation != null) { | ||
| input.setMouseDeltaX(mouseLocation.getX() - centerX); | ||
| input.setMouseDeltaY(mouseLocation.getY() - centerY); | ||
| } | ||
|
|
||
| prevLocation = mouseLocation; | ||
| robot.mouseMove(centerX, centerY); | ||
| } | ||
|
|
||
| } |
68 changes: 68 additions & 0 deletions
68
src/main/java/labs/introtoprogramming/lab5/gui/SceneRendererWindow.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package labs.introtoprogramming.lab5.gui; | ||
|
|
||
| import java.util.Timer; | ||
| import java.util.TimerTask; | ||
| import labs.introtoprogramming.lab5.exception.NoCameraException; | ||
| import labs.introtoprogramming.lab5.scene.BasicRaytracingRender; | ||
| import labs.introtoprogramming.lab5.scene.Scene; | ||
| import labs.introtoprogramming.lab5.scene.SceneRender; | ||
|
|
||
| public class SceneRendererWindow extends Window { | ||
|
|
||
| private static final String UPDATE_LOOP_TIMER_NAME = "UpdateLoop"; | ||
| private static final int UPDATE_LOOP_DELAY = 100; // ms | ||
| private static final int TARGET_FPS = 60; | ||
| private static final int UPDATE_RATE = 1000/TARGET_FPS; | ||
|
|
||
| private SceneRender render; | ||
| private Scene scene; | ||
| private MouseController mouseController; | ||
| private Input input; | ||
|
|
||
| public SceneRendererWindow(Scene scene) { | ||
| this(new BasicRaytracingRender(scene), scene); | ||
| } | ||
|
|
||
| public SceneRendererWindow(SceneRender render, Scene scene) { | ||
| this.render = render; | ||
| this.scene = scene; | ||
| input = new Input(); | ||
| scene.getCamera().ifPresent(camera -> setRaster(camera.raster())); | ||
| mouseController = new MouseController(input, frame); | ||
| } | ||
|
|
||
| public void show() { | ||
| super.show(); | ||
| startUpdateLoop(); | ||
| setupKeyboardListener(); | ||
| mouseController.hideCursor(); | ||
| } | ||
|
|
||
| private void setupKeyboardListener() { | ||
| frame.addKeyListener(new InputKeyListener(input)); | ||
| } | ||
|
|
||
| private void startUpdateLoop() { | ||
| new Timer(UPDATE_LOOP_TIMER_NAME, true).scheduleAtFixedRate(new TimerTask() { | ||
| long previousTime = System.currentTimeMillis(); | ||
|
|
||
| @Override | ||
| public void run() { | ||
| long currentTime = System.currentTimeMillis(); | ||
| updateIteration((int) (currentTime - previousTime)); | ||
| previousTime = currentTime; | ||
| } | ||
| }, UPDATE_LOOP_DELAY, UPDATE_RATE); | ||
| } | ||
|
|
||
| private void updateIteration(int delta) { | ||
| this.mouseController.update(); | ||
| scene.update(delta, input); | ||
| render.render(scene); | ||
| this.update(); | ||
| } | ||
|
|
||
| private void setScene(Scene scene) { | ||
| this.scene = scene; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assignment is redundant. Is it made for clarity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, to make it clear that my default all keys are not down.