From 00afb42609ef4d60b035535c06a923d52feb0b41 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 5 Nov 2018 18:10:45 +0100 Subject: [PATCH] Refactored and implemented object fall physics * Added DisplayBuffer class * Removed viewBuffer from the Camera class drawing directly to DisplayBuffer now * Added PhysicsSettings class (contains physics settings of scenes) * Added object fall physics * Deleted ProfilerData class (Profiler gets data from engine) --- .settings/org.eclipse.jdt.core.prefs | 7 +- ProjectHistory | 6 + README.md | 2 +- pom.xml | 13 +- .../java/com/johnsproject/jpge/Engine.java | 191 +++++++++-- .../com/johnsproject/jpge/EngineSettings.java | 104 ------ .../java/com/johnsproject/jpge/Profiler.java | 112 ++++--- .../com/johnsproject/jpge/ProfilerData.java | 99 ------ .../com/johnsproject/jpge/dto/Camera.java | 37 +-- .../johnsproject/jpge/dto/DisplayBuffer.java | 112 +++++++ .../com/johnsproject/jpge/dto/Rigidbody.java | 49 ++- .../johnsproject/jpge/dto/SceneObject.java | 16 +- .../johnsproject/jpge/graphics/Animator.java | 13 +- .../johnsproject/jpge/graphics/Renderer.java | 53 ++- .../jpge/graphics/SceneWindow.java | 97 +++--- .../johnsproject/jpge/graphics/Shader.java | 61 ++-- .../johnsproject/jpge/io/KeyInputManager.java | 4 - .../jpge/io/MouseInputManager.java | 4 - .../jpge/physics/PhysicsAnimator.java | 30 ++ .../jpge/physics/PhysicsSettings.java | 6 +- .../johnsproject/jpge/utils/RenderUtils.java | 49 +-- .../com/johnsproject/jpge/ExampleScene.java | 29 +- .../com/johnsproject/jpge/TestShader.java | 312 +++++++++--------- .../jpge/graphics/SceneWindowTest.java | 6 +- 24 files changed, 758 insertions(+), 654 deletions(-) delete mode 100644 src/main/java/com/johnsproject/jpge/EngineSettings.java delete mode 100644 src/main/java/com/johnsproject/jpge/ProfilerData.java create mode 100644 src/main/java/com/johnsproject/jpge/dto/DisplayBuffer.java diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 6b5aebc..552c396 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,13 +1,14 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.compliance=1.6 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.7 +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/ProjectHistory b/ProjectHistory index 03f98b7..caf210b 100644 --- a/ProjectHistory +++ b/ProjectHistory @@ -231,6 +231,12 @@ v0.1 - 24/08/2018 * Moved Renderer from SceneWindow to Engine * Moved Animator from SceneWindow to Engine * Moved SceneWindow back to src/main/java (moved to src/test/java accidently in last commit) +05/11/2018 +* Added DisplayBuffer class +* Removed viewBuffer from the Camera class drawing directly to DisplayBuffer now +* Added PhysicsSettings class (contains physics settings of scenes) +* Added object fall physics +* Deleted ProfilerData class (Profiler gets data from engine) diff --git a/README.md b/README.md index 789b1a1..3ce47de 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,4 @@ John´s Project Game Engine (JPGE) is tiny software based Game Engine writen in # Versions v0.1 - beta -[![JPGE v0.1 - beta](https://i9.ytimg.com/vi/LnPAomKeT5o/mq2.jpg?sqp=CJSGwt4F&rs=AOn4CLBClKXfoGPC6TJ-T37VvLz5oSZ-JA)](https://www.youtube.com/watch?v=LnPAomKeT5o&t=1s) +[![JPGE v0.1 - beta](https://i9.ytimg.com/vi/LnPAomKeT5o/mq2.jpg?sqp=CKi89t4F&rs=AOn4CLASozT4nfVeVfR2FnIoxBwHSspHww)](https://www.youtube.com/watch?v=LnPAomKeT5o&t=1s) diff --git a/pom.xml b/pom.xml index 400aeb6..329890c 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ UTF-8 - 1.8 + 1.6 3.5 @@ -28,8 +28,8 @@ 2.17 - 1.8 - 1.8 + 1.6 + 1.6 @@ -114,6 +114,13 @@ + + org.apache.maven.plugins + maven-surefire-plugin + + false + + diff --git a/src/main/java/com/johnsproject/jpge/Engine.java b/src/main/java/com/johnsproject/jpge/Engine.java index b9318b6..5c75413 100644 --- a/src/main/java/com/johnsproject/jpge/Engine.java +++ b/src/main/java/com/johnsproject/jpge/Engine.java @@ -1,5 +1,6 @@ package com.johnsproject.jpge; +import com.johnsproject.jpge.dto.DisplayBuffer; import com.johnsproject.jpge.dto.Scene; import com.johnsproject.jpge.graphics.Animator; import com.johnsproject.jpge.graphics.Renderer; @@ -27,13 +28,25 @@ public static Engine getInstance() { private Thread inputThread; private Thread physicsThread; - private EngineSettings settings = new EngineSettings(); + private int graphicsUpdateRate = 30; + private int inputUpdateRate = 30; + private int physicsUpdateRate = 30; + + private long startTime = System.currentTimeMillis(); + private boolean playing = true; + + private int lastGraphicsTime = 0; + private int lastInputTime = 0; + private int lastPhysicsTime = 0; + private int lastRendereredFaces = 0; + private Scene scene = new Scene(); private Renderer renderer = new Renderer(); private Animator animator = new Animator(); private KeyInputManager keyManager = new KeyInputManager(); private MouseInputManager mouseManager = new MouseInputManager(); private PhysicsAnimator physicsAnimator = new PhysicsAnimator(); + private DisplayBuffer displayBuffer = new DisplayBuffer(320, 240); private SceneWindow sceneWindow = null; public Engine() { @@ -46,8 +59,8 @@ public Engine() { * Starts the engine. */ public void play() { - if (!settings.isPlaying()) { - settings.setPlaying(true); + if (!isPlaying()) { + setPlaying(true); startGraphicsThread(); startPhysicsThread(); } @@ -57,18 +70,31 @@ public void play() { * Pauses the engine. */ public void pause() { - if (settings.isPlaying()) { - settings.setPlaying(false); + if (isPlaying()) { + setPlaying(false); } } + private void setPlaying(boolean value) { + playing = value; + } + /** - * Returns the {@link EngineSettings settings} of this engine. + * Returns the time since the engine started in miliseconds. * - * @return {@link EngineSettings settings} of this engine. + * @return time since the engine started in miliseconds. */ - public EngineSettings getSettings() { - return settings; + public long getPlayTime() { + return System.currentTimeMillis() - startTime; + } + + /** + * Returns if the engine is running. + * + * @return if the engine is running. + */ + public boolean isPlaying() { + return playing; } /** @@ -134,6 +160,15 @@ public PhysicsAnimator getPhysicsAnimator() { return physicsAnimator; } + /** + * Returns the {@link DisplayBuffer} used by the engine. + * + * @return {@link DisplayBuffer} used by the engine. + */ + public DisplayBuffer getDisplayBuffer() { + return displayBuffer; + } + /** * Returns the {@link SceneWindow} used by the engine. * @@ -152,13 +187,108 @@ public void setSceneWindow(SceneWindow sceneWindow) { this.sceneWindow = sceneWindow; } + /** + * Returns how often graphics should be updated in a second. + * Default is 30. + * + * @return how often graphics should be updated in a second. + */ + public int getGraphicsUpdateRate() { + return graphicsUpdateRate; + } + + /** + * Sets how often graphics should be updated in a second. + * Default is 30. + * + * @param graphicsUpdateRate value to set. + */ + public void setGraphicsUpdateRate(int graphicsUpdateRate) { + this.graphicsUpdateRate = graphicsUpdateRate; + } + + /** + * Returns how often input should be updated in a second. + * Default is 30. + * + * @return how often input should be updated in a second. + */ + public int getInputUpdateRate() { + return inputUpdateRate; + } + + /** + * Sets how often input should be updated in a second. + * Default is 30. + * + * @param inputUpdateRate value to set. + */ + public void setInputUpdateRate(int inputUpdateRate) { + this.inputUpdateRate = inputUpdateRate; + } + + /** + * Returns how often physics should be updated in a second. + * Default is 30. + * + * @return how often physics should be updated in a second. + */ + public int getPhysicsUpdateRate() { + return physicsUpdateRate; + } + + /** + * Sets how often physics should be updated in a second. + * Default is 30. + * + * @param physicsUpdateRate value to set. + */ + public void setPhysicsUpdateRate(int physicsUpdateRate) { + this.physicsUpdateRate = physicsUpdateRate; + } + + /** + * Returns how long the graphics thread took in the last update. + * + * @return how long the graphics thread took in the last update. + */ + public int getLastGraphicsTime() { + return lastGraphicsTime; + } + + /** + * Returns how long the input thread took in the last update. + * + * @return how long the input thread took in the last update. + */ + public int getLastInputTime() { + return lastInputTime; + } + + /** + * Returns how long the physics thread took in the last update. + * + * @return how long the physics thread took in the last update. + */ + public int getLastPhysicsTime() { + return lastPhysicsTime; + } + + /** + * Returns the count of faces rendered in the last graphics update. + * + * @return count of faces rendered in the last graphics update. + */ + public int getLastRenderedFaces() { + return lastRendereredFaces; + } + private void startGraphicsThread() { graphicsThread = new Thread(new Runnable() { @Override public void run() { - int lastElapsed = 0; - while (settings.isPlaying()) { - lastElapsed = updateGraphics(lastElapsed); + while (isPlaying()) { + lastGraphicsTime = updateGraphics(lastGraphicsTime); } } }); @@ -169,9 +299,8 @@ private void startInputThread() { inputThread = new Thread(new Runnable() { @Override public void run() { - int lastElapsed = 0; while (true) { - lastElapsed = updateInput(lastElapsed); + lastInputTime = updateInput(lastInputTime); } } }); @@ -182,9 +311,8 @@ private void startPhysicsThread() { physicsThread = new Thread(new Runnable() { @Override public void run() { - int lastElapsed = 0; - while (settings.isPlaying()) { - lastElapsed = updatePhysics(lastElapsed); + while (isPlaying()) { + lastPhysicsTime = updatePhysics(lastPhysicsTime); } } }); @@ -192,16 +320,15 @@ public void run() { } private int updateGraphics(int lastElapsed) { - long before = System.nanoTime(); + long before = System.currentTimeMillis(); + animator.animate(scene); + lastRendereredFaces = renderer.render(scene, displayBuffer); if (sceneWindow != null) { - animator.animate(scene); - renderer.render(scene, sceneWindow.getDepthBuffer()); - sceneWindow.repaint(); + sceneWindow.draw(); } - int elapsed = (int)(System.nanoTime() - before); - Profiler.getInstance().getData().setGraphicsTime(elapsed); + int elapsed = (int)(System.currentTimeMillis() - before); try { - Thread.sleep(1000/settings.getGraphicsUpdateRate()); + Thread.sleep(1000/getGraphicsUpdateRate()); } catch (InterruptedException e) { e.printStackTrace(); } @@ -209,13 +336,12 @@ private int updateGraphics(int lastElapsed) { } private int updateInput(int lastElapsed) { - long before = System.nanoTime(); + long before = System.currentTimeMillis(); keyManager.update(); mouseManager.update(); - int elapsed = (int)(System.nanoTime() - before); - Profiler.getInstance().getData().setInputTime(elapsed); + int elapsed = (int)(System.currentTimeMillis() - before); try { - Thread.sleep(1000/settings.getInputUpdateRate()); + Thread.sleep(1000/getInputUpdateRate()); } catch (InterruptedException e) { e.printStackTrace(); } @@ -223,12 +349,11 @@ private int updateInput(int lastElapsed) { } private int updatePhysics(int lastElapsed) { - long before = System.nanoTime(); - - int elapsed = (int)(System.nanoTime() - before); - Profiler.getInstance().getData().setPhysicsTime(elapsed); + long before = System.currentTimeMillis(); + physicsAnimator.animate(scene); + int elapsed = (int)(System.currentTimeMillis() - before); try { - Thread.sleep(1000/settings.getPhysicsUpdateRate()); + Thread.sleep(1000/getPhysicsUpdateRate()); } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/src/main/java/com/johnsproject/jpge/EngineSettings.java b/src/main/java/com/johnsproject/jpge/EngineSettings.java deleted file mode 100644 index 73b1f42..0000000 --- a/src/main/java/com/johnsproject/jpge/EngineSettings.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.johnsproject.jpge; - -/** - * The EngineSettings class stores the settings of the {@link Engine}. - * - * @author John´s Project - John Konrad Ferraz Salomon - */ -public class EngineSettings { - - private int graphicsUpdateRate = 30; - private int inputUpdateRate = 30; - private int physicsUpdateRate = 30; - - private long startTime = System.nanoTime(); - private boolean playing = true; - - /** - * Returns how often graphics should be updated in a second. - * Default is 30. - * - * @return how often graphics should be updated in a second. - */ - public int getGraphicsUpdateRate() { - return graphicsUpdateRate; - } - - /** - * Sets how often graphics should be updated in a second. - * Default is 30. - * - * @param graphicsUpdateRate value to set. - */ - public void setGraphicsUpdateRate(int graphicsUpdateRate) { - this.graphicsUpdateRate = graphicsUpdateRate; - } - - /** - * Returns how often input should be updated in a second. - * Default is 30. - * - * @return how often input should be updated in a second. - */ - public int getInputUpdateRate() { - return inputUpdateRate; - } - - /** - * Sets how often input should be updated in a second. - * Default is 30. - * - * @param inputUpdateRate value to set. - */ - public void setInputUpdateRate(int inputUpdateRate) { - this.inputUpdateRate = inputUpdateRate; - } - - /** - * Returns how often physics should be updated in a second. - * Default is 30. - * - * @return how often physics should be updated in a second. - */ - public int getPhysicsUpdateRate() { - return physicsUpdateRate; - } - - /** - * Sets how often physics should be updated in a second. - * Default is 30. - * - * @param physicsUpdateRate value to set. - */ - public void setPhysicsUpdateRate(int physicsUpdateRate) { - this.physicsUpdateRate = physicsUpdateRate; - } - - /** - * Returns the time since the engine started in nanoseconds. - * - * @return time since the engine started in nanoseconds. - */ - public long getPlayTime() { - return System.nanoTime() - startTime; - } - - /** - * Returns if the engine is running. - * - * @return if the engine is running. - */ - public boolean isPlaying() { - return playing; - } - - /** - * Sets if the engine should run. - * - * @param playing value to set. - */ - public void setPlaying(boolean playing) { - this.playing = playing; - } - -} diff --git a/src/main/java/com/johnsproject/jpge/Profiler.java b/src/main/java/com/johnsproject/jpge/Profiler.java index 3d3f601..210d9d7 100644 --- a/src/main/java/com/johnsproject/jpge/Profiler.java +++ b/src/main/java/com/johnsproject/jpge/Profiler.java @@ -5,10 +5,12 @@ import java.awt.image.BufferedImage; import java.io.IOException; import java.lang.management.ManagementFactory; +import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; +import com.johnsproject.jpge.dto.SceneObject; import com.johnsproject.jpge.io.FileIO; import com.sun.management.OperatingSystemMXBean; @@ -17,9 +19,8 @@ * * @author John´s Project - John Konrad Ferraz Salomon */ -public class Profiler extends JPanel{ - - private static final long serialVersionUID = -6448985233395265490L; +public class Profiler{ + private static Profiler instance; /** @@ -34,21 +35,29 @@ public static Profiler getInstance() { return instance; } - private static final int WIDTH = 330, HEIGHT = 330; + private static final int WIDTH = 330; + private static final int HEIGHT = 330; + private static final int POS_X = 2; + private static final int POS_X2 = 120; + private static final int START_Y = 12; + private static final int STEP = 17; + + private boolean profile = false; private boolean log = false; - private ProfilerData data; private Thread profileThread; private OperatingSystemMXBean osxb = null; private JFrame frame; + private ProfilerPanel panel; + private BufferedImage backroundImage = null; /** * Creates a new instance of the profiler class. */ public Profiler () { - data = new ProfilerData(); osxb = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class); frame = new JFrame(); + panel = new ProfilerPanel(); } @@ -66,13 +75,12 @@ private void startProfiling() { } frame.setResizable(false); frame.setSize(WIDTH, HEIGHT); - this.setSize(WIDTH, HEIGHT); - this.setLocation(0, 0); + panel.setSize(WIDTH, HEIGHT); frame.setLayout(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.add(this); + frame.add(panel); try { - img = FileIO.loadImage(getClass().getResourceAsStream("/JohnsProject.png"), WIDTH, HEIGHT); + backroundImage = FileIO.loadImage(getClass().getResourceAsStream("/JohnsProject.png"), WIDTH, HEIGHT); } catch (IOException e) { e.printStackTrace(); } @@ -102,25 +110,7 @@ public void run() { } private void print() { - this.repaint(); - } - - private BufferedImage img = null; - private static final int POS_X = 2, POS_X2 = 120, START_Y = 15, STEP = 17; - @Override - public void paintComponent(Graphics g) { - super.paintComponent(g); - g.drawImage(img, 0, 0, null); - g.setColor(Color.WHITE); - int y = START_Y; - y = logCommom(g, y); - y += STEP + 10; - y = logGraphics(g, y); - y += STEP + 10; - y = logInput(g, y); - y += STEP + 10; - y = logPhysics(g, y); - if (isLogging()) System.out.println("-------------------------------------------------"); + panel.repaint(); } private int logCommom(Graphics g, int y) { @@ -151,11 +141,22 @@ private int logCommom(Graphics g, int y) { } private int logGraphics(Graphics g, int y) { - int renderedPolys = getData().getRenderedFaces(); - int maxPolys = getData().getMaxFaces(); - String fps = "" + 1000/(((getData().getGraphicsTime()) / 1000000)+1); - String rTime = "" + (getData().getGraphicsTime() / 1000000) + " ms"; - String rRes = "" + getData().getWidth() + " x " + getData().getHeight(); + Engine engine = Engine.getInstance(); + int renderedPolys = engine.getLastRenderedFaces(); + int maxPolys = 0; + synchronized (engine.getScene()) { + List sceneObjects = engine.getScene().getSceneObjects(); + for (int i = 0; i < sceneObjects.size(); i++) { + maxPolys += sceneObjects.get(i).getMesh().getFaces().length; + } + } + String fps = "" + 1000/(engine.getLastGraphicsTime()+1); + String rTime = "" + engine.getLastGraphicsTime() + " ms"; + String rRes = "" + engine.getDisplayBuffer().getWidth() + " x " + engine.getDisplayBuffer().getHeight(); + String wRes = ""; + if (engine.getSceneWindow() != null) + wRes += engine.getSceneWindow().getWidth() + " x " + engine.getSceneWindow().getHeight(); + else wRes += "no SceneWindow"; String rTris = "" + getBar(renderedPolys, maxPolys) + renderedPolys + " / " + maxPolys; g.drawString("GRAPHICS DATA", POS_X, y); y += STEP; @@ -168,6 +169,9 @@ private int logGraphics(Graphics g, int y) { g.drawString("- Rendering res. : ", POS_X, y); g.drawString(rRes, POS_X2, y); y += STEP; + g.drawString("- Window size : ", POS_X, y); + g.drawString(wRes, POS_X2, y); + y += STEP; g.drawString("- Rendered Faces : ", POS_X, y); g.drawString(rTris, POS_X2, y); if (isLogging()) { @@ -175,15 +179,17 @@ private int logGraphics(Graphics g, int y) { System.out.println("- FPS :\t\t\t" + fps); System.out.println("- Render time :\t\t" + rTime); System.out.println("- Rendering res. :\t" + rRes); + System.out.println("- Window size : \t" + wRes); System.out.println("- Rendered Faces :\t" + rTris); } return y; } private int logInput(Graphics g, int y) { - String iTime = "" + (getData().getInputTime() / 1000000) + " ms"; - String key = "" + getData().getKeyData(); - String mouse ="" + getData().getMouseData(); + Engine engine = Engine.getInstance(); + String iTime = "" + engine.getLastInputTime() + " ms"; + String key = ""; + String mouse = ""; g.drawString("INPUT DATA", POS_X, y); y += STEP; g.drawString("- Input time : ", POS_X, y); @@ -204,7 +210,8 @@ private int logInput(Graphics g, int y) { } private int logPhysics(Graphics g, int y) { - String pTime = "" + (getData().getPhysicsTime() / 1000000) + " ms"; + Engine engine = Engine.getInstance(); + String pTime = "" + engine.getLastPhysicsTime() + " ms"; g.drawString("PHYSICS DATA", POS_X, y); y += STEP; g.drawString("- Physics time : ", POS_X, y); @@ -280,13 +287,26 @@ public void stopLogging() { log = false; } - /** - * Returns the data of this profiler. - * This is used by the engine to update the profiler value so its not recommended to change the values of it. - * - * @return data of this profiler. - */ - public ProfilerData getData() { - return data; + + private class ProfilerPanel extends JPanel{ + + private static final long serialVersionUID = 1L; + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + g.drawImage(backroundImage, 0, 0, null); + g.setColor(Color.WHITE); + int y = START_Y; + y = logCommom(g, y); + y += STEP + 5; + y = logGraphics(g, y); + y += STEP + 5; + y = logInput(g, y); + y += STEP + 5; + y = logPhysics(g, y); + if (isLogging()) System.out.println("-------------------------------------------------"); + } } + } diff --git a/src/main/java/com/johnsproject/jpge/ProfilerData.java b/src/main/java/com/johnsproject/jpge/ProfilerData.java deleted file mode 100644 index 9ff7461..0000000 --- a/src/main/java/com/johnsproject/jpge/ProfilerData.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.johnsproject.jpge; - -public class ProfilerData { - - // Graphics data - private int graphicsTime = 1; - private int renderedFaces = 1, maxFaces = 1; - private int cameras = 1; - private int width = 1, height = 1; - - // Input data - private int inputTime = 1; - private String keyData = ""; - private String mouseData = ""; - - // Physics data - private int physicsTime = 1; - - public int getGraphicsTime() { - return graphicsTime; - } - - public void setGraphicsTime(int graphicsTime) { - this.graphicsTime = graphicsTime; - } - - public int getRenderedFaces() { - return renderedFaces; - } - - public void setRenderedFaces(int renderedPolys) { - this.renderedFaces = renderedPolys; - } - - public int getMaxFaces() { - return maxFaces; - } - - public void setMaxFaces(int maxPolys) { - this.maxFaces = maxPolys; - } - - public int getCameras() { - return cameras; - } - - public void setCameras(int cameras) { - this.cameras = cameras; - } - - public int getWidth() { - return width; - } - - public void setWidth(int width) { - this.width = width; - } - - public int getHeight() { - return height; - } - - public void setHeight(int height) { - this.height = height; - } - - public int getInputTime() { - return inputTime; - } - - public void setInputTime(int inputTime) { - this.inputTime = inputTime; - } - - public int getPhysicsTime() { - return physicsTime; - } - - public void setPhysicsTime(int physicsTime) { - this.physicsTime = physicsTime; - } - - public String getKeyData() { - return keyData; - } - - public void setKeyData(String keyData) { - this.keyData = keyData; - } - - public String getMouseData() { - return mouseData; - } - - public void setMouseData(String mouseData) { - this.mouseData = mouseData; - } - -} diff --git a/src/main/java/com/johnsproject/jpge/dto/Camera.java b/src/main/java/com/johnsproject/jpge/dto/Camera.java index a9a0f42..f918316 100644 --- a/src/main/java/com/johnsproject/jpge/dto/Camera.java +++ b/src/main/java/com/johnsproject/jpge/dto/Camera.java @@ -1,8 +1,5 @@ package com.johnsproject.jpge.dto; -import java.awt.image.BufferedImage; -import java.awt.image.DataBufferInt; - import com.johnsproject.jpge.graphics.Renderer; import com.johnsproject.jpge.graphics.SceneWindow; @@ -24,11 +21,9 @@ public class Camera{ private int halfHeight = 0; private int FieldOfView = 60; private int nearClippingPlane = 300; - private int farClippingPlane = 50000; + private int farClippingPlane = 100000; private int scaleFactor; private Transform transform; - private BufferedImage viewBuffer; - private int[] viewBufferData; private boolean changed = false; /** @@ -49,38 +44,8 @@ public Camera(String name, Transform transform, int x, int y, int width, int hei this.scaleFactor = ((width + height) >> 7) + 1; this.x = x; this.y = y; - this.viewBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE); - this.viewBufferData = ((DataBufferInt)viewBuffer.getRaster().getDataBuffer()).getData(); this.changed = true; } - - /** - * Tells this camera to clear its view buffer. - */ - public void clearViewBuffer() { - for (int i = 0; i < viewBufferData.length; i++) { - viewBufferData[i] = 0; - } - } - - /** - * Returns the {@link BufferedImage view buffer} of this camera. - * - * @return {@link BufferedImage view buffer} of this camera. - */ - public BufferedImage getViewBuffer() { - return viewBuffer; - } - - /** - * Returns the data of the {@link BufferedImage view buffer} of this camera. - * The data is an int[] containing color data of all pixels of the view buffer. - * - * @return data of the {@link BufferedImage view buffer} of this camera. - */ - public int[] getViewBufferData() { - return viewBufferData; - } /** * Returns the width of this camera in screen. diff --git a/src/main/java/com/johnsproject/jpge/dto/DisplayBuffer.java b/src/main/java/com/johnsproject/jpge/dto/DisplayBuffer.java new file mode 100644 index 0000000..dacd396 --- /dev/null +++ b/src/main/java/com/johnsproject/jpge/dto/DisplayBuffer.java @@ -0,0 +1,112 @@ +package com.johnsproject.jpge.dto; + +import java.awt.image.BufferedImage; +import java.awt.image.DataBufferInt; + +import com.johnsproject.jpge.Engine; +import com.johnsproject.jpge.graphics.Renderer; +import com.johnsproject.jpge.graphics.SceneWindow; + +/** + * The DisplayBuffer class contains a frameBuffer and a depthBuffer. + * The {@link Renderer} draws on it at the rendering process and after that + * this buffer is drawn on the {@link SceneWindow} used by the {@link Engine} if available. + * + * @author John´s Project - John Konrad Ferraz Salomon + */ +public class DisplayBuffer { + + private int width; + private int height; + private BufferedImage frameBuffer; + private int[] frameBufferData; + private int[] depthBuffer; + + /** + * Creates a display buffer with the given size. + * + * @param width width to set. + * @param height height to set. + */ + public DisplayBuffer(int width, int height) { + setSize(width, height); + } + + /** + * Returns the {@link BufferedImage frameBuffer} of this DisplayBuffer. + * + * @return {@link BufferedImage frameBuffer} of this DisplayBuffer. + */ + public BufferedImage getFrameBuffer() { + return frameBuffer; + } + + /** + * Returns the pixel data of the {@link BufferedImage frameBuffer} of this DisplayBuffer. + * + * @return pixel data of the {@link BufferedImage frameBuffer} of this DisplayBuffer. + */ + public int[] getFrameBufferData() { + return frameBufferData; + } + + /** + * Returns the depthBuffer of this DisplayBuffer. + * The DepthBuffer contains the depth value of all pixels in the frameBuffer. + * + * @return depthBuffer of this DisplayBuffer. + */ + public int[] getDepthBuffer() { + return depthBuffer; + } + + /** + * Tells this DisplayBuffer to clear its frameBuffer. + */ + public void clearFrameBuffer() { + for (int i = 0; i < frameBufferData.length; i++) { + frameBufferData[i] = 0; + } + } + + /** + * Tells this DisplayBuffer to clear its depthBuffer. + */ + public void clearDepthBuffer() { + for (int i = 0; i < depthBuffer.length; i++) { + depthBuffer[i] = Integer.MAX_VALUE; + } + } + + /** + * Resizes this DisplayBuffer. + * + * @param width width to set. + * @param height height to set. + */ + public void setSize(int width, int height) { + this.width = width; + this.height = height; + this.frameBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE); + this.frameBufferData = ((DataBufferInt)frameBuffer.getRaster().getDataBuffer()).getData(); + this.depthBuffer = new int[width*height]; + } + + /** + * Returns the width of this DisplayBuffer. + * + * @return width of this DisplayBuffer. + */ + public int getWidth() { + return width; + } + + /** + * Returns the height of this DisplayBuffer. + * + * @return height of this DisplayBuffer. + */ + public int getHeight() { + return height; + } +} diff --git a/src/main/java/com/johnsproject/jpge/dto/Rigidbody.java b/src/main/java/com/johnsproject/jpge/dto/Rigidbody.java index 637baf4..1054919 100644 --- a/src/main/java/com/johnsproject/jpge/dto/Rigidbody.java +++ b/src/main/java/com/johnsproject/jpge/dto/Rigidbody.java @@ -1,7 +1,54 @@ package com.johnsproject.jpge.dto; +import com.johnsproject.jpge.utils.Vector3MathUtils; +import com.johnsproject.jpge.utils.VectorUtils; + public class Rigidbody { - private int mass = 1; + private static final int vx = VectorUtils.X, vy = VectorUtils.Y, vz = VectorUtils.Z; + + private int mass = 10; + private int[] velocity = new int[3]; + private int collisionTime = (int)System.currentTimeMillis(); + + public int getMass() { + return mass; + } + + public void setMass(int mass) { + this.mass = mass; + } + + public int[] getVelocity() { + return velocity; + } + public void addForce(int[] force) { + Vector3MathUtils.divide(force, mass, force); + Vector3MathUtils.add(this.velocity, force, this.velocity); + } + + public void addForce(int x, int y, int z) { + this.velocity[vx] += x / mass; + this.velocity[vy] += y / mass; + this.velocity[vz] += z / mass; + } + + public void accelerate(int[] value) { + Vector3MathUtils.add(this.velocity, value, this.velocity); + } + + public void accelerate(int x, int y, int z) { + this.velocity[vx] += x; + this.velocity[vy] += y; + this.velocity[vz] += z; + } + + public int getCollisionTime() { + return collisionTime; + } + + public void setCollisionTime(int collisionTime) { + this.collisionTime = collisionTime; + } } diff --git a/src/main/java/com/johnsproject/jpge/dto/SceneObject.java b/src/main/java/com/johnsproject/jpge/dto/SceneObject.java index 6e55b5f..7f0480f 100644 --- a/src/main/java/com/johnsproject/jpge/dto/SceneObject.java +++ b/src/main/java/com/johnsproject/jpge/dto/SceneObject.java @@ -13,6 +13,7 @@ public class SceneObject { private Transform transform; private Mesh mesh; private Shader shader; + private Rigidbody rigidbody; private boolean changed = false; private boolean active = true; @@ -29,6 +30,7 @@ public SceneObject(String name, Transform transform, Mesh mesh){ this.mesh = mesh; this.changed = true; this.shader = new Shader(); + this.rigidbody = new Rigidbody(); } /** @@ -107,6 +109,15 @@ public void setShader(Shader shader) { this.shader = shader; } + /** + * Returns the {@link Rigidbody} used by this scene object. + * + * @return {@link Rigidbody} used by this scene object. + */ + public Rigidbody getRigidbody() { + return rigidbody; + } + /** * Returns if this object is active or not. * @@ -124,10 +135,5 @@ public boolean isActive() { */ public void setActive(boolean active) { this.active = active; - } - - @Override - public String toString() { - return "SceneObject [name=" + name + ", transform=" + transform.toString() + ", mesh=" + mesh.toString() + "]"; } } diff --git a/src/main/java/com/johnsproject/jpge/graphics/Animator.java b/src/main/java/com/johnsproject/jpge/graphics/Animator.java index a548bce..823e6a3 100644 --- a/src/main/java/com/johnsproject/jpge/graphics/Animator.java +++ b/src/main/java/com/johnsproject/jpge/graphics/Animator.java @@ -18,11 +18,14 @@ public class Animator { * @param scene {@link Scene} to update. */ public void animate(Scene scene) { - for (SceneObject sceneObject : scene.getSceneObjects()) { - Animation animation = sceneObject.getMesh().getCurrentAnimation(); - animation.setCurrentFrame(animation.getCurrentFrame()); - if (animation.getCurrentFrame() >= animation.getFramesCount()) { - animation.setCurrentFrame(0); + synchronized (scene.getSceneObjects()) { + for (int i = 0; i < scene.getSceneObjects().size(); i++) { + SceneObject sceneObject = scene.getSceneObjects().get(i); + Animation animation = sceneObject.getMesh().getCurrentAnimation(); + animation.setCurrentFrame(animation.getCurrentFrame()); + if (animation.getCurrentFrame() >= animation.getFramesCount()) { + animation.setCurrentFrame(0); + } } } } diff --git a/src/main/java/com/johnsproject/jpge/graphics/Renderer.java b/src/main/java/com/johnsproject/jpge/graphics/Renderer.java index 8bcce3c..c11d06a 100644 --- a/src/main/java/com/johnsproject/jpge/graphics/Renderer.java +++ b/src/main/java/com/johnsproject/jpge/graphics/Renderer.java @@ -2,9 +2,9 @@ import java.util.List; -import com.johnsproject.jpge.Profiler; import com.johnsproject.jpge.dto.Animation; import com.johnsproject.jpge.dto.Camera; +import com.johnsproject.jpge.dto.DisplayBuffer; import com.johnsproject.jpge.dto.Face; import com.johnsproject.jpge.dto.Light; import com.johnsproject.jpge.dto.Mesh; @@ -26,32 +26,31 @@ public class Renderer { * Tells this scene renderer to render the given {@link Scene}. * * @param scene {@link Scene} to render. - * @param zBuffer depth buffer to use. + * @param displayBuffer {@link DisplayBuffer} to use. + * + * @return rendered {@link Face faces}. (faces that are not culled) */ - public void render(Scene scene, int[] zBuffer) { - // reseting profiler data - Profiler.getInstance().getData().setMaxFaces(0); - Profiler.getInstance().getData().setRenderedFaces(0); - synchronized (scene.getCameras()) { + public int render(Scene scene, DisplayBuffer displayBuffer) { + synchronized (scene) { + int rendFaces = 0; + displayBuffer.clearFrameBuffer(); for (int i = 0; i < scene.getCameras().size(); i++) { - // reset z buffer - resetZBuffer(zBuffer); + displayBuffer.clearDepthBuffer(); Camera camera = scene.getCameras().get(i); for (int j = 0; j < scene.getSceneObjects().size(); j++) { SceneObject sceneObject = scene.getSceneObjects().get(j); // check if object is active or has changed (no need to render if its the same) if (sceneObject.isActive() && (sceneObject.changed() || camera.changed())) { - render(sceneObject, camera, scene.getLights(), zBuffer); + rendFaces = render(sceneObject, camera, scene.getLights(), displayBuffer); } } camera.changed(false); } - } - synchronized (scene.getSceneObjects()) { for (int i = 0; i < scene.getSceneObjects().size(); i++) { SceneObject sceneObject = scene.getSceneObjects().get(i); sceneObject.changed(false); } + return rendFaces; } } @@ -62,9 +61,11 @@ public void render(Scene scene, int[] zBuffer) { * @param sceneObject {@link SceneObject} to render. * @param camera {@link Camera} that the {@link SceneObject} will be rendered to. * @param lights {@link Light Lights} influencing the {@link SceneObject}. - * @param zBuffer depth buffer to use. + * @param displayBuffer {@link DisplayBuffer} to use. + * + * @return rendered {@link Face faces}. (faces that are not culled) */ - void render(SceneObject sceneObject, Camera camera, List lights, int[] zBuffer) { + int render(SceneObject sceneObject, Camera camera, List lights, DisplayBuffer displayBuffer) { Mesh mesh = sceneObject.getMesh(); Animation animation = mesh.getCurrentAnimation(); Shader shader = sceneObject.getShader(); @@ -74,31 +75,15 @@ void render(SceneObject sceneObject, Camera camera, List lights, int[] zB for (int i = 0; i < mesh.getBufferedVertexes().length; i++) { Vertex vertex = mesh.getBufferedVertex(i); vertex = RenderUtils.animate(vertex, animation); - vertex = shader.shadeVertex(vertex, mesh, camera, sceneObject.getTransform(), lights); + vertex = shader.shadeVertex(vertex, mesh, sceneObject.getTransform(), lights, camera); } - // get profiler values to update - int maxFaces = Profiler.getInstance().getData().getMaxFaces(); - int rendFaces = Profiler.getInstance().getData().getRenderedFaces(); + int rendFaces = 0; // shade faces for (int i = 0; i < mesh.getFaces().length; i++) { Face face = mesh.getFace(i); - face = shader.shadeFace(face, mesh, camera, zBuffer, sceneObject.getTransform(), lights); - maxFaces++; + face = shader.shadeFace(face, mesh, sceneObject.getTransform(), lights, camera, displayBuffer); if (!face.isCulled()) rendFaces++; } - // update profiler values - Profiler.getInstance().getData().setMaxFaces(maxFaces); - Profiler.getInstance().getData().setRenderedFaces(rendFaces); - } - - /** - * Resets the depth buffer of this scene renderer. - * - * @param zBuffer depth buffer to use. - */ - public void resetZBuffer(int[] zBuffer) { - for (int i = 0; i < zBuffer.length; i++) { - zBuffer[i] = Integer.MAX_VALUE; - } + return rendFaces; } } diff --git a/src/main/java/com/johnsproject/jpge/graphics/SceneWindow.java b/src/main/java/com/johnsproject/jpge/graphics/SceneWindow.java index 35c782e..05c8624 100644 --- a/src/main/java/com/johnsproject/jpge/graphics/SceneWindow.java +++ b/src/main/java/com/johnsproject/jpge/graphics/SceneWindow.java @@ -7,8 +7,7 @@ import javax.swing.JPanel; import com.johnsproject.jpge.Engine; -import com.johnsproject.jpge.Profiler; -import com.johnsproject.jpge.dto.Camera; +import com.johnsproject.jpge.dto.DisplayBuffer; import com.johnsproject.jpge.dto.Scene; import com.johnsproject.jpge.io.FileIO; @@ -17,76 +16,80 @@ * * @author John´s Project - John Konrad Ferraz Salomon */ -public class SceneWindow extends JPanel{ +public class SceneWindow extends JFrame{ - private static final long serialVersionUID = -841144266539311921L; + private static final long serialVersionUID = 1L; + private int width = 0; private int height = 0; - private JFrame frame; - private int[] zBuffer = null; + private ScenePanel panel; + private DisplayBuffer displayBuffer; /** * Creates a new instance of the SceneWindow class filled with the given values. * - * @param width scene frame width. - * @param height scene frame height. + * @param width scene this width. + * @param height scene this height. */ public SceneWindow (int width, int height){ this.width = width; this.height = height; - frame = new JFrame(); - frame.setSize(width, height); - this.setSize(width, height); - initializeFrame(); + panel = new ScenePanel(); + panel.setSize(width, height); + displayBuffer = Engine.getInstance().getDisplayBuffer(); + setSize(width, height); + try { + this.setIconImage(FileIO.loadImage(getClass().getResourceAsStream("/JohnsProjectLogo.png"))); + } catch (IOException e) { + e.printStackTrace(); + } + this.setResizable(false); + this.setVisible(true); + this.setTitle("JPGE"); + this.setLayout(null); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.add(panel); } /** - * Initializes the components and values of this scene frame. + * Tells this SceneWindow to draw the scene being used by the {@link Engine}. */ - void initializeFrame(){ - zBuffer = new int[width * height]; - Profiler.getInstance().getData().setWidth(width); - Profiler.getInstance().getData().setHeight(height); - try { - frame.setIconImage(FileIO.loadImage(getClass().getResourceAsStream("/JohnsProjectLogo.png"))); - } catch (IOException e) { - e.printStackTrace(); + public void draw() { + panel.repaint(); + if (this.getWidth() != width || this.getHeight() != height) { + width = this.getWidth(); + height = this.getHeight(); + panel.setSize(width, height); } - frame.setResizable(false); - frame.setVisible(true); - frame.setTitle("JPGE"); - frame.setLayout(null); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.add(this); } /** - * Returns the depth buffer of this scene frame. + * Returns the screen position of this scene window in the x axis. * - * @return depth buffer of this scene frame. + * @return screen position of this scene window in the x axis. */ - public int[] getDepthBuffer() { - return zBuffer; + public int getPositionX() { + return (int)this.getLocationOnScreen().getX(); } - @Override - public void setSize(int width, int height) { - super.setSize(width, height); - zBuffer = new int[width * height]; - Profiler.getInstance().getData().setWidth(width); - Profiler.getInstance().getData().setHeight(height); + /** + * Returns the screen position of this scene window in the y axis. + * + * @return screen position of this scene window in the y axis. + */ + public int getPositionY() { + return (int)this.getLocationOnScreen().getY(); } - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - Scene scene = Engine.getInstance().getScene(); - synchronized (scene.getCameras()) { - for (int i = 0; i < scene.getCameras().size(); i++) { - Camera camera = scene.getCameras().get(i); - g.drawImage(camera.getViewBuffer(), camera.getPositionX(), camera.getPositionY(), null); - camera.clearViewBuffer(); - } + private class ScenePanel extends JPanel{ + + private static final long serialVersionUID = 1L; + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + g.drawImage(displayBuffer.getFrameBuffer(), 0, 0, width, height, null); } } + } diff --git a/src/main/java/com/johnsproject/jpge/graphics/Shader.java b/src/main/java/com/johnsproject/jpge/graphics/Shader.java index b8f1523..ad5f953 100644 --- a/src/main/java/com/johnsproject/jpge/graphics/Shader.java +++ b/src/main/java/com/johnsproject/jpge/graphics/Shader.java @@ -3,6 +3,7 @@ import java.util.List; import com.johnsproject.jpge.dto.Camera; +import com.johnsproject.jpge.dto.DisplayBuffer; import com.johnsproject.jpge.dto.Face; import com.johnsproject.jpge.dto.Light; import com.johnsproject.jpge.dto.Mesh; @@ -43,12 +44,12 @@ public class Shader { * * @param vertex {@link Vertex} to shade. * @param mesh {@link Mesh} this face belongs to. - * @param camera {@link Camera} being rendered to. * @param objectTransform {@link Transform} of the {@link SceneObject} this vertex belongs to. * @param lights All {@link Light Lights} of the {@link Scene} being rendered. + * @param camera {@link Camera} being rendered to. * @return shaded vertex. */ - public Vertex shadeVertex(Vertex vertex, Mesh mesh, Camera camera, Transform objectTransform, List lights) { + public Vertex shadeVertex(Vertex vertex, Mesh mesh, Transform objectTransform, List lights, Camera camera) { Transform objt = objectTransform; Transform camt = camera.getTransform(); int[] vector = vertex.getPosition(); @@ -82,20 +83,20 @@ public Vertex shadeVertex(Vertex vertex, Mesh mesh, Camera camera, Transform obj * * @param face face to shade. * @param mesh {@link Mesh} this face belongs to. - * @param camera {@link Camera} being rendered to. - * @param zBuffer depth buffer used at the drawing process. * @param objectTransform {@link Transform} of the {@link SceneObject} this face belongs to. * @param lights All {@link Light Lights} of the {@link Scene} being rendered. + * @param camera {@link Camera} being rendered to. + * @param displayBuffer {@link DisplayBuffer} to use. * @return shaded polygon. */ - public Face shadeFace(Face face, Mesh mesh, Camera camera, int[] zBuffer, Transform objectTransform, List lights) { + public Face shadeFace(Face face, Mesh mesh, Transform objectTransform, List lights, Camera camera, DisplayBuffer displayBuffer) { // view frustum culling if (!RenderUtils.isInsideViewFrustum(face, mesh, camera)) { - // get vertexes - Vertex vt1 = mesh.getBufferedVertex(face.getVertex1()); - Vertex vt2 = mesh.getBufferedVertex(face.getVertex2()); - Vertex vt3 = mesh.getBufferedVertex(face.getVertex3()); if (!RenderUtils.isBackface(face, mesh)) { + // get vertexes + Vertex vt1 = mesh.getBufferedVertex(face.getVertex1()); + Vertex vt2 = mesh.getBufferedVertex(face.getVertex2()); + Vertex vt3 = mesh.getBufferedVertex(face.getVertex3()); if (shadingType == SHADE_FLAT) { // calculate shaded color for 1 vertex int result = shade(lights, objectTransform.getPosition(), vt1.getNormal()); @@ -111,19 +112,19 @@ public Face shadeFace(Face face, Mesh mesh, Camera camera, int[] zBuffer, Transf // draw based on the cameras rendering type switch (drawingType) { case DRAW_VERTEX: - drawVertex(vt1, vt2, vt3, shadedColor, zBuffer, camera); + drawVertex(vt1, vt2, vt3, shadedColor, camera, displayBuffer); break; case DRAW_WIREFRAME: - drawWireframe(vt1, vt2, vt3, shadedColor, zBuffer, camera); + drawWireframe(vt1, vt2, vt3, shadedColor, camera, displayBuffer); break; case DRAW_FLAT: - RenderUtils.drawFaceGouraud(face, mesh, zBuffer, this, camera); + RenderUtils.drawFaceGouraud(face, mesh, this, camera, displayBuffer); break; case DRAW_TEXTURED: - RenderUtils.drawFaceAffine(face, mesh, zBuffer, this, camera); + RenderUtils.drawFaceAffine(face, mesh, this, camera, displayBuffer); break; } } @@ -139,17 +140,21 @@ public Face shadeFace(Face face, Mesh mesh, Camera camera, int[] zBuffer, Transf * @param z position of pixel in the z axis. * @param color color of pixel. * @param camera {@link Camera} to set pixel. - * @param zBuffer buffer containing depth of the pixels of the {@link Camera}. + * @param displayBuffer {@link DisplayBuffer} to use. */ - public void shadePixel (int x, int y, int z, int color, int[] zBuffer, Camera camera) { + public void shadePixel (int x, int y, int z, int color, Camera camera, DisplayBuffer displayBuffer) { // check if pixel is inside camera if (x > 0 && x < camera.getWidth() && y > 0 && y < camera.getHeight()) { // calculate 1D array position of 2D xy position - int pos = x + (y * camera.getWidth()); - // z test - if (zBuffer[pos] > z) { - zBuffer[pos] = z; - camera.getViewBufferData()[pos] = color; + int pos = (camera.getPositionX() + x) + ((camera.getPositionY() + y) * displayBuffer.getWidth()); + int[] depthBuffer = displayBuffer.getDepthBuffer(); + // check if pixel is inside displayBuffer + if (pos < depthBuffer.length) { + // z test + if (depthBuffer[pos] > z) { + depthBuffer[pos] = z; + displayBuffer.getFrameBufferData()[pos] = color; + } } } } @@ -172,7 +177,7 @@ private int shade(List lights, int[] vector, int[] normal) { return ColorUtils.lerpRBG(ColorUtils.convert(255, 255, 255), 0, -factor-50); } - private void drawVertex(Vertex vt1, Vertex vt2, Vertex vt3, int color, int[] zBuffer, Camera camera) { + private void drawVertex(Vertex vt1, Vertex vt2, Vertex vt3, int color, Camera camera, DisplayBuffer displayBuffer) { // get position of vertexes int[] vp1 = vt1.getPosition(); int[] vp2 = vt2.getPosition(); @@ -182,12 +187,12 @@ private void drawVertex(Vertex vt1, Vertex vt2, Vertex vt3, int color, int[] zBu x3 = vp3[vx], y3 = vp3[vy], z3 = vp3[vz]; // color used if rendering type is wireframe or vertex int shadedColor = ColorUtils.lerpRBG(color, vt1.getColor(), -255); - shadePixel(x1, y1, z1, shadedColor, zBuffer, camera); - shadePixel(x2, y2, z2, shadedColor, zBuffer, camera); - shadePixel(x3, y3, z3, shadedColor, zBuffer, camera); + shadePixel(x1, y1, z1, shadedColor, camera, displayBuffer); + shadePixel(x2, y2, z2, shadedColor, camera, displayBuffer); + shadePixel(x3, y3, z3, shadedColor, camera, displayBuffer); } - private void drawWireframe(Vertex vt1, Vertex vt2, Vertex vt3, int color, int[] zBuffer, Camera camera) { + private void drawWireframe(Vertex vt1, Vertex vt2, Vertex vt3, int color, Camera camera, DisplayBuffer displayBuffer) { // get position of vertexes int[] vp1 = vt1.getPosition(); int[] vp2 = vt2.getPosition(); @@ -197,9 +202,9 @@ private void drawWireframe(Vertex vt1, Vertex vt2, Vertex vt3, int color, int[] x3 = vp3[vx], y3 = vp3[vy], z3 = vp3[vz]; // color used if rendering type is wireframe or vertex int shadedColor = ColorUtils.lerpRBG(color, vt1.getColor(), -255); - RenderUtils.drawLine(x1, y1, x2, y2, z1, shadedColor, zBuffer, this, camera); - RenderUtils.drawLine(x2, y2, x3, y3, z2, shadedColor, zBuffer, this, camera); - RenderUtils.drawLine(x3, y3, x1, y1, z3, shadedColor, zBuffer, this, camera); + RenderUtils.drawLine(x1, y1, x2, y2, z1, shadedColor, this, camera, displayBuffer); + RenderUtils.drawLine(x2, y2, x3, y3, z2, shadedColor, this, camera, displayBuffer); + RenderUtils.drawLine(x3, y3, x1, y1, z3, shadedColor, this, camera, displayBuffer); } /** diff --git a/src/main/java/com/johnsproject/jpge/io/KeyInputManager.java b/src/main/java/com/johnsproject/jpge/io/KeyInputManager.java index e511f42..40cc92c 100644 --- a/src/main/java/com/johnsproject/jpge/io/KeyInputManager.java +++ b/src/main/java/com/johnsproject/jpge/io/KeyInputManager.java @@ -54,17 +54,13 @@ public boolean dispatchKeyEvent(KeyEvent e) { public void update() { synchronized (keyListeners) { synchronized (pressedKeys) { - String data = ""; for (int key : pressedKeys.keySet()) { char keyChar = pressedKeys.get(key).charValue(); - data += "(" + keyChar + ", " + key + "), "; for (int i = 0; i < keyListeners.size(); i++) { JPGEKeyListener keyListener = keyListeners.get(i); keyListener.keyPressed(new JPGEKeyEvent(keyChar, key)); } } - if (data.equals("")) data = "no keys pressed"; - Profiler.getInstance().getData().setKeyData(data); } } } diff --git a/src/main/java/com/johnsproject/jpge/io/MouseInputManager.java b/src/main/java/com/johnsproject/jpge/io/MouseInputManager.java index 3a5cb8a..2d41c7e 100644 --- a/src/main/java/com/johnsproject/jpge/io/MouseInputManager.java +++ b/src/main/java/com/johnsproject/jpge/io/MouseInputManager.java @@ -46,9 +46,7 @@ public void eventDispatched(AWTEvent event) { public void update() { synchronized (mouseListeners) { synchronized (pressedKeys) { - String data = ""; for (int key : pressedKeys.keySet()) { - data += key + ", "; for (int i = 0; i < mouseListeners.size(); i++) { JPGEMouseListener mouseListener = mouseListeners.get(i); if (key == LEFT) @@ -66,8 +64,6 @@ public void update() { mouseListener.positionUpdate(new JPGEMouseEvent(cache)); } pressedKeys.clear(); - if (data.equals("")) data = "no mouse clicks"; - Profiler.getInstance().getData().setMouseData(data); } } } diff --git a/src/main/java/com/johnsproject/jpge/physics/PhysicsAnimator.java b/src/main/java/com/johnsproject/jpge/physics/PhysicsAnimator.java index 8fc8e82..32045df 100644 --- a/src/main/java/com/johnsproject/jpge/physics/PhysicsAnimator.java +++ b/src/main/java/com/johnsproject/jpge/physics/PhysicsAnimator.java @@ -1,7 +1,37 @@ package com.johnsproject.jpge.physics; +import com.johnsproject.jpge.dto.Rigidbody; +import com.johnsproject.jpge.dto.Scene; +import com.johnsproject.jpge.dto.SceneObject; +import com.johnsproject.jpge.dto.Transform; +import com.johnsproject.jpge.utils.VectorUtils; + public class PhysicsAnimator { + private static final int vx = VectorUtils.X, vy = VectorUtils.Y, vz = VectorUtils.Z; + + public void animate(Scene scene) { + synchronized (scene.getSceneObjects()) { + for (int i = 0; i < scene.getSceneObjects().size(); i++) { + SceneObject sceneObject = scene.getSceneObjects().get(i); + Transform transform = sceneObject.getTransform(); + Rigidbody rigidbody = sceneObject.getRigidbody(); + PhysicsSettings settings = scene.getPhysicsSettings(); + applyGravity(rigidbody, settings.getGravity()); + transform.translate(rigidbody.getVelocity()); + } + } + } + void applyGravity (Rigidbody rigidbody, int[] gravity) { + long fallTime = System.currentTimeMillis() - rigidbody.getCollisionTime(); + int t = (int)(fallTime * fallTime); + // ((Gravity * Time / 2) / 1024) / 1024 + // use bitshifting because its faster and has no need to be so precise + int x = (((gravity[vx] * t) >> 1) >> 10) >> 10; + int y = (((gravity[vy] * t) >> 1) >> 10) >> 10; + int z = (((gravity[vz] * t) >> 1) >> 10) >> 10; + rigidbody.accelerate(x, y, z); + } } diff --git a/src/main/java/com/johnsproject/jpge/physics/PhysicsSettings.java b/src/main/java/com/johnsproject/jpge/physics/PhysicsSettings.java index e90a8f3..24890d1 100644 --- a/src/main/java/com/johnsproject/jpge/physics/PhysicsSettings.java +++ b/src/main/java/com/johnsproject/jpge/physics/PhysicsSettings.java @@ -2,13 +2,13 @@ public class PhysicsSettings { - private int gravity = 10; + private int[] gravity = new int[] {0, 10, 0}; - public int getGravity() { + public int[] getGravity() { return gravity; } - public void setGravity(int gravity) { + public void setGravity(int[] gravity) { this.gravity = gravity; } } diff --git a/src/main/java/com/johnsproject/jpge/utils/RenderUtils.java b/src/main/java/com/johnsproject/jpge/utils/RenderUtils.java index fa63c39..a9d901a 100644 --- a/src/main/java/com/johnsproject/jpge/utils/RenderUtils.java +++ b/src/main/java/com/johnsproject/jpge/utils/RenderUtils.java @@ -2,6 +2,7 @@ import com.johnsproject.jpge.dto.Animation; import com.johnsproject.jpge.dto.Camera; +import com.johnsproject.jpge.dto.DisplayBuffer; import com.johnsproject.jpge.dto.Face; import com.johnsproject.jpge.dto.Mesh; import com.johnsproject.jpge.dto.Texture; @@ -103,7 +104,7 @@ public static boolean isInsideViewFrustum(Face face, Mesh mesh, Camera camera) { int y = (v1[vy] + v2[vy] + v3[vy]) / 3; int z = (v1[vz] + v2[vz] + v3[vz]) / 3; // tolerance - int t = 100; + int t = 50; // check if its inside view if (x > -t && x < w + t && y > -t && y < h + t && z > ncp && z < fcp) { return false; @@ -147,12 +148,12 @@ public static boolean isBackface(Face face, Mesh mesh) { * @param y2 end y position. * @param z z position used by the zBuffer. * @param color color of the line. - * @param zBuffer zBuffer to use. * @param shader {@link Shader} to use. * @param camera {@link Camera} to draw on. + * @param displayBuffer {@link DisplayBuffer} to use. */ public static void drawLine(int x1, int y1, int x2, int y2, int z, int color, - int[] zBuffer, Shader shader, Camera camera) { + Shader shader, Camera camera, DisplayBuffer displayBuffer) { int w = x2 - x1; int h = y2 - y1; // deltas @@ -173,7 +174,7 @@ public static void drawLine(int x1, int y1, int x2, int y2, int z, int color, int numerator = longest >> 1; for (int i = 0; i < longest; i++, numerator += shortest) { // set pixel - shader.shadePixel(x1, y1, z, color, zBuffer, camera); + shader.shadePixel(x1, y1, z, color, camera, displayBuffer); // increase deltas if (numerator > longest) { numerator -= longest; @@ -193,11 +194,11 @@ public static void drawLine(int x1, int y1, int x2, int y2, int z, int color, * * @param face {@link Face} to draw. * @param mesh {@link Mesh} that contains the given {@link Face}. - * @param zBuffer zBuffer to use. * @param shader {@link Shader} to use. * @param camera {@link Camera} to draw on. + * @param displayBuffer {@link DisplayBuffer} to use. */ - public static void drawFaceGouraud(Face face, Mesh mesh, int[] zBuffer, Shader shader, Camera camera) { + public static void drawFaceGouraud(Face face, Mesh mesh, Shader shader, Camera camera, DisplayBuffer diplayBuffer) { Vertex vt1 = mesh.getBufferedVertex(face.getVertex1()); Vertex vt2 = mesh.getBufferedVertex(face.getVertex2()); Vertex vt3 = mesh.getBufferedVertex(face.getVertex3()); @@ -243,12 +244,12 @@ public static void drawFaceGouraud(Face face, Mesh mesh, int[] zBuffer, Shader s tmp = vc2; vc2 = vc1; vc1 = tmp;} if (y1 == y2) y2++; if (y2 == y3) y3++; - face(x1, y1, z1, vc1, x2, y2, z2, vc2, x3, y3, z3, vc3, zBuffer, color, shader, camera); + face(x1, y1, z1, vc1, x2, y2, z2, vc2, x3, y3, z3, vc3, color, shader, camera, diplayBuffer); } // face drawing and filling with fixed point scanline algorithm that supports gouraud shading static void face(int x1, int y1, int z1, int vc1, int x2, int y2, int z2, int vc2, int x3, int y3, int z3, int vc3, - int[] zBuffer, int color, Shader shader, Camera cam) { + int color, Shader shader, Camera cam, DisplayBuffer diplayBuffer) { // get color values int r1 = ColorUtils.getRed(vc1), g1 = ColorUtils.getGreen(vc1), b1 = ColorUtils.getBlue(vc1), a1 = ColorUtils.getAlpha(vc1); int r2 = ColorUtils.getRed(vc2), g2 = ColorUtils.getGreen(vc2), b2 = ColorUtils.getBlue(vc2), a2 = ColorUtils.getAlpha(vc2); @@ -316,7 +317,7 @@ static void face(int x1, int y1, int z1, int vc1, int x2, int y2, int z2, int vc } for (; sy < y2; sy++) { // bitshift right to get right values - drawHLine(sx >> SHIFT, ex >> SHIFT, sz, dz, sr, dr, sg, dg, sb, db, sa, da, sy, color, zBuffer, shader, cam); + drawHLine(sx >> SHIFT, ex >> SHIFT, sz, dz, sr, dr, sg, dg, sb, db, sa, da, sy, color, shader, cam, diplayBuffer); // increase left and right values by the calculated delta sx += dx2; ex += dx1; sz += dz2; @@ -336,7 +337,7 @@ static void face(int x1, int y1, int z1, int vc1, int x2, int y2, int z2, int vc da = ((da3 - da2) << SHIFT) / (dxdx); } for (; sy < y3; sy++) { - drawHLine(sx >> SHIFT, ex >> SHIFT, sz, dz, sr, dr, sg, dg, sb, db, sa, da, sy, color, zBuffer, shader, cam); + drawHLine(sx >> SHIFT, ex >> SHIFT, sz, dz, sr, dr, sg, dg, sb, db, sa, da, sy, color, shader, cam, diplayBuffer); sx += dx2; ex += dx3; sz += dz2; sr += dr2; @@ -354,7 +355,7 @@ static void face(int x1, int y1, int z1, int vc1, int x2, int y2, int z2, int vc da = ((da2 - da1) << SHIFT) / (dxdx); } for (; sy < y2; sy++) { - drawHLine(sx >> SHIFT, ex >> SHIFT, sz, dz, sr, dr, sg, dg, sb, db, sa, da, sy, color, zBuffer, shader, cam); + drawHLine(sx >> SHIFT, ex >> SHIFT, sz, dz, sr, dr, sg, dg, sb, db, sa, da, sy, color, shader, cam, diplayBuffer); sx += dx1; ex += dx2; sz += dz1; sr += dr1; @@ -372,7 +373,7 @@ static void face(int x1, int y1, int z1, int vc1, int x2, int y2, int z2, int vc da = ((da2 - da3) << SHIFT) / (dxdx); } for (; sy < y3; sy++) { - drawHLine(sx >> SHIFT, ex >> SHIFT, sz, dz, sr, dr, sg, dg, sb, db, sa, da, sy, color, zBuffer, shader, cam); + drawHLine(sx >> SHIFT, ex >> SHIFT, sz, dz, sr, dr, sg, dg, sb, db, sa, da, sy, color, shader, cam, diplayBuffer); sx += dx3; ex += dx2; sz += dz3; sr += dr3; @@ -384,11 +385,11 @@ static void face(int x1, int y1, int z1, int vc1, int x2, int y2, int z2, int vc } static void drawHLine(int sx, int ex, int sz, int dz, int sr, int dr, int sg, int dg, int sb, int db, int sa, int da, - int sy, int color, int[] zBuffer, Shader shader, Camera camera) { + int sy, int color, Shader shader, Camera camera, DisplayBuffer diplayBuffer) { for (; sx < ex; sx++) { int iColor = ColorUtils.convert(sr >> SHIFT, sg >> SHIFT, sb >> SHIFT, sa >> SHIFT); // no need to shift sz as the z test is just check if its a higher value - shader.shadePixel(sx, sy, sz, ColorUtils.lerpRBG(iColor, color, 500), zBuffer, camera); + shader.shadePixel(sx, sy, sz, ColorUtils.lerpRBG(iColor, color, 500), camera, diplayBuffer); sz += dz; sr += dr; sg += dg; @@ -403,11 +404,11 @@ static void drawHLine(int sx, int ex, int sz, int dz, int sr, int dr, int sg, in * * @param face {@link Face} to draw. * @param mesh {@link Mesh} that contains the given {@link Face}. - * @param zBuffer zBuffer to use. * @param shader {@link Shader} to use. * @param camera {@link Camera} to draw on. + * @param displayBuffer {@link DisplayBuffer} to use. */ - public static void drawFaceAffine(Face face, Mesh mesh, int[] zBuffer, Shader shader, Camera camera) { + public static void drawFaceAffine(Face face, Mesh mesh, Shader shader, Camera camera, DisplayBuffer diplayBuffer) { Vertex vt1 = mesh.getBufferedVertex(face.getVertex1()); Vertex vt2 = mesh.getBufferedVertex(face.getVertex2()); Vertex vt3 = mesh.getBufferedVertex(face.getVertex3()); @@ -453,13 +454,13 @@ public static void drawFaceAffine(Face face, Mesh mesh, int[] zBuffer, Shader sh tmp = vc2; vc2 = vc1; vc1 = tmp;} if (y1 == y2) y2++; if (y2 == y3) y3++; - faceAffine(x1, y1, z1, vc1, x2, y2, z2, vc2, x3, y3, z3, vc3, u1, v1, u2, v2, u3, v3, img, zBuffer, shader, camera); + faceAffine(x1, y1, z1, vc1, x2, y2, z2, vc2, x3, y3, z3, vc3, u1, v1, u2, v2, u3, v3, img, shader, camera, diplayBuffer); } // face drawing and filling with fixed point scanline algorithm that supports gouraud shading static void faceAffine(int x1, int y1, int z1, int vc1, int x2, int y2, int z2, int vc2, int x3, int y3, int z3, int vc3, int u1, int v1, int u2, int v2, int u3, int v3, - Texture img, int[] zBuffer, Shader shader, Camera cam) { + Texture img, Shader shader, Camera cam, DisplayBuffer diplayBuffer) { // interpolate uv coordinates int w = img.getWidth() - 1, h = img.getHeight() - 1; int sh = 7; @@ -546,7 +547,7 @@ static void faceAffine(int x1, int y1, int z1, int vc1, int x2, int y2, int z2, } for (; sy < y2; sy++) { // bitshift right to get right values - drawHLineAffine(sx >> SHIFT, ex >> SHIFT, sz, dz, su, du, sv, dv, sr, dr, sg, dg, sb, db, sa, da, sy, img, zBuffer, shader, cam); + drawHLineAffine(sx >> SHIFT, ex >> SHIFT, sz, dz, su, du, sv, dv, sr, dr, sg, dg, sb, db, sa, da, sy, img, shader, cam, diplayBuffer); // increase left and right values by the calculated delta sx += dx2; ex += dx1; sz += dz2; @@ -570,7 +571,7 @@ static void faceAffine(int x1, int y1, int z1, int vc1, int x2, int y2, int z2, da = ((da3 - da2) << SHIFT) / (dxdx); } for (; sy < y3; sy++) { - drawHLineAffine(sx >> SHIFT, ex >> SHIFT, sz, dz, su, du, sv, dv, sr, dr, sg, dg, sb, db, sa, da, sy, img, zBuffer, shader, cam); + drawHLineAffine(sx >> SHIFT, ex >> SHIFT, sz, dz, su, du, sv, dv, sr, dr, sg, dg, sb, db, sa, da, sy, img, shader, cam, diplayBuffer); sx += dx2; ex += dx3; sz += dz2; su += du2; @@ -592,7 +593,7 @@ static void faceAffine(int x1, int y1, int z1, int vc1, int x2, int y2, int z2, da = ((da2 - da1) << SHIFT) / (dxdx); } for (; sy < y2; sy++) { - drawHLineAffine(sx >> SHIFT, ex >> SHIFT, sz, dz, su, du, sv, dv, sr, dr, sg, dg, sb, db, sa, da, sy, img, zBuffer, shader, cam); + drawHLineAffine(sx >> SHIFT, ex >> SHIFT, sz, dz, su, du, sv, dv, sr, dr, sg, dg, sb, db, sa, da, sy, img, shader, cam, diplayBuffer); sx += dx1; ex += dx2; sz += dz1; su += du1; @@ -614,7 +615,7 @@ static void faceAffine(int x1, int y1, int z1, int vc1, int x2, int y2, int z2, da = ((da2 - da3) << SHIFT) / (dxdx); } for (; sy < y3; sy++) { - drawHLineAffine(sx >> SHIFT, ex >> SHIFT, sz, dz, su, du, sv, dv, sr, dr, sg, dg, sb, db, sa, da, sy, img, zBuffer, shader, cam); + drawHLineAffine(sx >> SHIFT, ex >> SHIFT, sz, dz, su, du, sv, dv, sr, dr, sg, dg, sb, db, sa, da, sy, img, shader, cam, diplayBuffer); sx += dx3; ex += dx2; sz += dz3; su += du3; @@ -629,13 +630,13 @@ static void faceAffine(int x1, int y1, int z1, int vc1, int x2, int y2, int z2, static void drawHLineAffine(int sx, int ex, int sz, int dz, int su, int du, int sv, int dv, int sr, int dr, int sg, int dg, int sb, int db, int sa, int da, int sy, - Texture img, int[] zBuffer, Shader shader, Camera camera) { + Texture img, Shader shader, Camera camera, DisplayBuffer diplayBuffer) { for (; sx < ex; sx++) { // get texture pixel / texel color int color = img.getPixel(su >> SHIFT, sv >> SHIFT); int iColor = ColorUtils.convert(sr >> SHIFT, sg >> SHIFT, sb >> SHIFT, sa >> SHIFT); // no need to shift sz as the z test is just check if its a higher value - shader.shadePixel(sx, sy, sz, ColorUtils.lerpRBG(iColor, color, 500), zBuffer, camera); + shader.shadePixel(sx, sy, sz, ColorUtils.lerpRBG(iColor, color, 500), camera, diplayBuffer); sz += dz; su += du; sv += dv; diff --git a/src/test/java/com/johnsproject/jpge/ExampleScene.java b/src/test/java/com/johnsproject/jpge/ExampleScene.java index 63e466e..3438576 100644 --- a/src/test/java/com/johnsproject/jpge/ExampleScene.java +++ b/src/test/java/com/johnsproject/jpge/ExampleScene.java @@ -18,16 +18,16 @@ public static void main(String[] args){ new ExampleScene(); } - SceneWindow sceneWindow; SceneObject sceneObject, sceneObject2; Camera camera, camera2; Light light, light2; Mesh mesh1, mesh2; - int w = 1024, h = 720; + int renderWidth = 213, renderHeight = 160; + int windowWidth = 640, windowHeight = 480; public ExampleScene() { try { - mesh1 = SOMImporter.load("/home/john/Development/test.som"); -// mesh1 = SOMImporter.load(getClass().getResourceAsStream("/cube.som")); +// mesh1 = SOMImporter.load("/home/john/Development/test.som"); + mesh1 = SOMImporter.load(getClass().getResourceAsStream("/cube.som")); mesh2 = SOMImporter.load(getClass().getResourceAsStream("/monkey.som")); } catch (ImportExeption e) { e.printStackTrace(); @@ -45,18 +45,20 @@ public ExampleScene() { } catch (IOException e) { e.printStackTrace(); } - camera = new Camera("testCam", new Transform(new int[] {0, 0, -10000}, new int[] {0, 0, 0}, new int[] {1, 1, 1}), 0, 0, w, h); - camera2 = new Camera("testCam2", new Transform(new int[] {0, -10000, 0}, new int[] {90, 0, 0}, new int[] {1, 1, 1}), w-(w/3), 0, w/3, h/3); + camera = new Camera("testCam", new Transform(new int[] {0, 0, -10000}, new int[] {0, 0, 0}, new int[] {1, 1, 1}), 0, 0, renderWidth, renderHeight); + camera2 = new Camera("testCam2", new Transform(new int[] {0, -10000, 0}, new int[] {90, 0, 0}, new int[] {1, 1, 1}), renderWidth-(renderWidth/3), 0, renderWidth/3, renderHeight/3); light = new Light("testLight", new Transform(new int[] {0, 0, 0}, new int[] {0, 0, 0}, new int[] {1, 1, 1})); - sceneWindow = new SceneWindow(w, h); + Engine.getInstance().setSceneWindow(new SceneWindow(windowWidth, windowHeight)); + Engine.getInstance().getDisplayBuffer().setSize(renderWidth, renderHeight); + Engine.getInstance().getScene().getPhysicsSettings().setGravity(new int[3]); Engine.getInstance().getScene().addLight(light); Engine.getInstance().getScene().addSceneObject(sceneObject); Engine.getInstance().getScene().addSceneObject(sceneObject2); Engine.getInstance().getScene().addCamera(camera); Engine.getInstance().getScene().addCamera(camera2); - Engine.getInstance().setSceneWindow(sceneWindow); Engine.getInstance().getKeyInputManager().addKeyListener(this); Engine.getInstance().getMouseInputManager().addMouseListener(this); + Profiler.getInstance().start(); } public void keyPressed(JPGEKeyEvent event) { @@ -173,6 +175,7 @@ public void keyPressed(JPGEKeyEvent event) { if (event.getKeyCode() == 39) { light.getTransform().translate(-1, 0, 0); } +// System.out.println(event.getKeyCode()); } public void keyReleased(JPGEKeyEvent event) { @@ -199,13 +202,13 @@ public void rightClick(JPGEMouseEvent event) { @Override public void positionUpdate(JPGEMouseEvent event) { if (dragged) { - int fx = (int)sceneWindow.getLocationOnScreen().getX(); - int fy = (int)sceneWindow.getLocationOnScreen().getY(); - int x = (event.getPosition()[VectorUtils.X]-fx) - (w/2); - int y = (event.getPosition()[VectorUtils.Y]-fy) - (h/2); + int fx = (int)Engine.getInstance().getSceneWindow().getPositionX(); + int fy = (int)Engine.getInstance().getSceneWindow().getPositionY(); + int x = (event.getPosition()[VectorUtils.X]-fx) - (windowWidth/2); + int y = (event.getPosition()[VectorUtils.Y]-fy) - (windowHeight/2); // int z = camera.getTransform().getRotation()[VectorUtils.Z]; //System.out.println("x " + x + ", y " + y + ", z " + z); - camera.getTransform().rotate(y/(h>>3), x/(w>>3), 0); + camera.getTransform().rotate(y/(windowHeight>>3), x/(windowWidth>>3), 0); } } } diff --git a/src/test/java/com/johnsproject/jpge/TestShader.java b/src/test/java/com/johnsproject/jpge/TestShader.java index 5598437..e933068 100644 --- a/src/test/java/com/johnsproject/jpge/TestShader.java +++ b/src/test/java/com/johnsproject/jpge/TestShader.java @@ -1,156 +1,156 @@ -package com.johnsproject.jpge; - -import java.util.List; - -import com.johnsproject.jpge.dto.Camera; -import com.johnsproject.jpge.dto.Face; -import com.johnsproject.jpge.dto.Light; -import com.johnsproject.jpge.dto.Mesh; -import com.johnsproject.jpge.dto.Transform; -import com.johnsproject.jpge.dto.Vertex; -import com.johnsproject.jpge.graphics.Shader; -import com.johnsproject.jpge.utils.ColorUtils; -import com.johnsproject.jpge.utils.RenderUtils; -import com.johnsproject.jpge.utils.Vector3MathUtils; -import com.johnsproject.jpge.utils.VectorUtils; - -public class TestShader extends Shader{ - - private static final int vx = VectorUtils.X, vy = VectorUtils.Y, vz = VectorUtils.Z; - private static final int PROJECT_ORTHOGRAPHIC = 0; - private static final int PROJECT_PERSPECTIVE = 1; - private static final int SHADE_FLAT = 0; - private static final int SHADE_GOURAUD = 1; - private static final int DRAW_VERTEX = 0; - private static final int DRAW_WIREFRAME = 1; - private static final int DRAW_FLAT = 2; - private static final int DRAW_TEXTURED = 3; - - private int projectionType = PROJECT_PERSPECTIVE; - private int shadingType = SHADE_GOURAUD; - private int drawingType = DRAW_TEXTURED; - - @Override - public Vertex shadeVertex(Vertex vertex, Mesh mesh, Camera camera, Transform objectTransform, List lights) { - Transform objt = objectTransform; - Transform camt = camera.getTransform(); - int[] pos = vertex.getPosition(); - int[] normal = vertex.getNormal(); - // transform vertex in object space - pos = Vector3MathUtils.movePointByScale(pos, objt.getScale(), pos); - pos = Vector3MathUtils.movePointByAnglesXYZ(pos, objt.getRotation(), pos); - // transform vertex to world space - pos = Vector3MathUtils.add(pos, objt.getPosition(), pos); - // transform vertex to camera space - pos = Vector3MathUtils.subtract(pos, camt.getPosition(), pos); - pos = Vector3MathUtils.movePointByAnglesXYZ(pos, camt.getRotation(), pos); - // transform / project vertex to screen space - if(projectionType == PROJECT_ORTHOGRAPHIC) { - pos = RenderUtils.orthographicProject(pos, camera); - } - if(projectionType == PROJECT_PERSPECTIVE) { - pos = RenderUtils.perspectiveProject(pos, camera); - } - if(shadingType == SHADE_GOURAUD) { - // transform normal in object space - normal = Vector3MathUtils.movePointByAnglesXYZ(normal, objt.getRotation(), normal); - // calculate shaded color for every vertex - vertex.setColor(shade(lights, objt.getPosition(), normal)); - } - return vertex; - } - - @Override - public Face shadeFace(Face face, Mesh mesh, Camera camera, int[] zBuffer, Transform objectTransform, List lights) { - // view frustum culling - if (!RenderUtils.isInsideViewFrustum(face, mesh, camera)) { - // get vertexes - Vertex vt1 = mesh.getBufferedVertex(face.getVertex1()); - Vertex vt2 = mesh.getBufferedVertex(face.getVertex2()); - Vertex vt3 = mesh.getBufferedVertex(face.getVertex3()); - if (!RenderUtils.isBackface(face, mesh)) { - if(shadingType == SHADE_FLAT) { - // calculate shaded color for 1 vertex - int result = shade(lights, objectTransform.getPosition(), vt1.getNormal()); - // and apply that color to all vertexes - vt1.setColor(result); - vt2.setColor(result); - vt3.setColor(result); - } - // draw face - int color = mesh.getMaterial(face.getMaterial()).getColor(); - // color used if rendering type is wireframe or vertex - int shadedColor = ColorUtils.lerpRBG(color, vt1.getColor(), -255); - // draw based on the cameras rendering type - switch (drawingType) { - case DRAW_VERTEX: - drawVertex(vt1, vt2, vt3, shadedColor, zBuffer, camera); - break; - - case DRAW_WIREFRAME: - drawWireframe(vt1, vt2, vt3, shadedColor, zBuffer, camera); - break; - - case DRAW_FLAT: - RenderUtils.drawFaceGouraud(face, mesh, zBuffer, this, camera); - break; - - case DRAW_TEXTURED: - RenderUtils.drawFaceAffine(face, mesh, zBuffer, this, camera); - break; - } - } - } - return face; - } - - private int shade(List lights, int[] objectPos, int[] normal) { - int factor = 0; - int lightColor = 0; - for (int i = 0; i < lights.size(); i++) { - Light light = lights.get(i); - int[] lightPosition = light.getTransform().getPosition(); - switch (light.getType()) { - case LIGHT_DIRECTIONAL: - factor += Vector3MathUtils.dotProduct(lightPosition, normal); - factor /= light.getStrength(); - break; - } - lightColor = ColorUtils.lerpRBG(light.getColor(), lightColor, -100); - } - // return color - return ColorUtils.lerpRBG(lightColor, 0, -factor); - } - - private void drawVertex(Vertex vt1, Vertex vt2, Vertex vt3, int color, int[] zBuffer, Camera camera) { - // get position of vertexes - int[] vp1 = vt1.getPosition(); - int[] vp2 = vt2.getPosition(); - int[] vp3 = vt3.getPosition(); - int x1 = vp1[vx], y1 = vp1[vy], z1 = vp1[vz], - x2 = vp2[vx], y2 = vp2[vy], z2 = vp2[vz], - x3 = vp3[vx], y3 = vp3[vy], z3 = vp3[vz]; - // color used if rendering type is wireframe or vertex - int shadedColor = ColorUtils.lerpRBG(color, vt1.getColor(), -255); - shadePixel(x1, y1, z1, shadedColor, zBuffer, camera); - shadePixel(x2, y2, z2, shadedColor, zBuffer, camera); - shadePixel(x3, y3, z3, shadedColor, zBuffer, camera); - } - - - private void drawWireframe(Vertex vt1, Vertex vt2, Vertex vt3, int color, int[] zBuffer, Camera camera) { - // get position of vertexes - int[] vp1 = vt1.getPosition(); - int[] vp2 = vt2.getPosition(); - int[] vp3 = vt3.getPosition(); - int x1 = vp1[vx], y1 = vp1[vy], z1 = vp1[vz], - x2 = vp2[vx], y2 = vp2[vy], z2 = vp2[vz], - x3 = vp3[vx], y3 = vp3[vy], z3 = vp3[vz]; - // color used if rendering type is wireframe or vertex - int shadedColor = ColorUtils.lerpRBG(color, vt1.getColor(), -255); - RenderUtils.drawLine(x1, y1, x2, y2, z1, shadedColor, zBuffer, this, camera); - RenderUtils.drawLine(x2, y2, x3, y3, z2, shadedColor, zBuffer, this, camera); - RenderUtils.drawLine(x3, y3, x1, y1, z3, shadedColor, zBuffer, this, camera); - } - -} +//package com.johnsproject.jpge; +// +//import java.util.List; +// +//import com.johnsproject.jpge.dto.Camera; +//import com.johnsproject.jpge.dto.Face; +//import com.johnsproject.jpge.dto.Light; +//import com.johnsproject.jpge.dto.Mesh; +//import com.johnsproject.jpge.dto.Transform; +//import com.johnsproject.jpge.dto.Vertex; +//import com.johnsproject.jpge.graphics.Shader; +//import com.johnsproject.jpge.utils.ColorUtils; +//import com.johnsproject.jpge.utils.RenderUtils; +//import com.johnsproject.jpge.utils.Vector3MathUtils; +//import com.johnsproject.jpge.utils.VectorUtils; +// +//public class TestShader extends Shader{ +// +// private static final int vx = VectorUtils.X, vy = VectorUtils.Y, vz = VectorUtils.Z; +// private static final int PROJECT_ORTHOGRAPHIC = 0; +// private static final int PROJECT_PERSPECTIVE = 1; +// private static final int SHADE_FLAT = 0; +// private static final int SHADE_GOURAUD = 1; +// private static final int DRAW_VERTEX = 0; +// private static final int DRAW_WIREFRAME = 1; +// private static final int DRAW_FLAT = 2; +// private static final int DRAW_TEXTURED = 3; +// +// private int projectionType = PROJECT_PERSPECTIVE; +// private int shadingType = SHADE_GOURAUD; +// private int drawingType = DRAW_TEXTURED; +// +// @Override +// public Vertex shadeVertex(Vertex vertex, Mesh mesh, Camera camera, Transform objectTransform, List lights) { +// Transform objt = objectTransform; +// Transform camt = camera.getTransform(); +// int[] pos = vertex.getPosition(); +// int[] normal = vertex.getNormal(); +// // transform vertex in object space +// pos = Vector3MathUtils.movePointByScale(pos, objt.getScale(), pos); +// pos = Vector3MathUtils.movePointByAnglesXYZ(pos, objt.getRotation(), pos); +// // transform vertex to world space +// pos = Vector3MathUtils.add(pos, objt.getPosition(), pos); +// // transform vertex to camera space +// pos = Vector3MathUtils.subtract(pos, camt.getPosition(), pos); +// pos = Vector3MathUtils.movePointByAnglesXYZ(pos, camt.getRotation(), pos); +// // transform / project vertex to screen space +// if(projectionType == PROJECT_ORTHOGRAPHIC) { +// pos = RenderUtils.orthographicProject(pos, camera); +// } +// if(projectionType == PROJECT_PERSPECTIVE) { +// pos = RenderUtils.perspectiveProject(pos, camera); +// } +// if(shadingType == SHADE_GOURAUD) { +// // transform normal in object space +// normal = Vector3MathUtils.movePointByAnglesXYZ(normal, objt.getRotation(), normal); +// // calculate shaded color for every vertex +// vertex.setColor(shade(lights, objt.getPosition(), normal)); +// } +// return vertex; +// } +// +// @Override +// public Face shadeFace(Face face, Mesh mesh, Camera camera, int[] zBuffer, Transform objectTransform, List lights) { +// // view frustum culling +// if (!RenderUtils.isInsideViewFrustum(face, mesh, camera)) { +// // get vertexes +// Vertex vt1 = mesh.getBufferedVertex(face.getVertex1()); +// Vertex vt2 = mesh.getBufferedVertex(face.getVertex2()); +// Vertex vt3 = mesh.getBufferedVertex(face.getVertex3()); +// if (!RenderUtils.isBackface(face, mesh)) { +// if(shadingType == SHADE_FLAT) { +// // calculate shaded color for 1 vertex +// int result = shade(lights, objectTransform.getPosition(), vt1.getNormal()); +// // and apply that color to all vertexes +// vt1.setColor(result); +// vt2.setColor(result); +// vt3.setColor(result); +// } +// // draw face +// int color = mesh.getMaterial(face.getMaterial()).getColor(); +// // color used if rendering type is wireframe or vertex +// int shadedColor = ColorUtils.lerpRBG(color, vt1.getColor(), -255); +// // draw based on the cameras rendering type +// switch (drawingType) { +// case DRAW_VERTEX: +// drawVertex(vt1, vt2, vt3, shadedColor, zBuffer, camera); +// break; +// +// case DRAW_WIREFRAME: +// drawWireframe(vt1, vt2, vt3, shadedColor, zBuffer, camera); +// break; +// +// case DRAW_FLAT: +// RenderUtils.drawFaceGouraud(face, mesh, zBuffer, this, camera); +// break; +// +// case DRAW_TEXTURED: +// RenderUtils.drawFaceAffine(face, mesh, zBuffer, this, camera); +// break; +// } +// } +// } +// return face; +// } +// +// private int shade(List lights, int[] objectPos, int[] normal) { +// int factor = 0; +// int lightColor = 0; +// for (int i = 0; i < lights.size(); i++) { +// Light light = lights.get(i); +// int[] lightPosition = light.getTransform().getPosition(); +// switch (light.getType()) { +// case LIGHT_DIRECTIONAL: +// factor += Vector3MathUtils.dotProduct(lightPosition, normal); +// factor /= light.getStrength(); +// break; +// } +// lightColor = ColorUtils.lerpRBG(light.getColor(), lightColor, -100); +// } +// // return color +// return ColorUtils.lerpRBG(lightColor, 0, -factor); +// } +// +// private void drawVertex(Vertex vt1, Vertex vt2, Vertex vt3, int color, int[] zBuffer, Camera camera) { +// // get position of vertexes +// int[] vp1 = vt1.getPosition(); +// int[] vp2 = vt2.getPosition(); +// int[] vp3 = vt3.getPosition(); +// int x1 = vp1[vx], y1 = vp1[vy], z1 = vp1[vz], +// x2 = vp2[vx], y2 = vp2[vy], z2 = vp2[vz], +// x3 = vp3[vx], y3 = vp3[vy], z3 = vp3[vz]; +// // color used if rendering type is wireframe or vertex +// int shadedColor = ColorUtils.lerpRBG(color, vt1.getColor(), -255); +// shadePixel(x1, y1, z1, shadedColor, zBuffer, camera); +// shadePixel(x2, y2, z2, shadedColor, zBuffer, camera); +// shadePixel(x3, y3, z3, shadedColor, zBuffer, camera); +// } +// +// +// private void drawWireframe(Vertex vt1, Vertex vt2, Vertex vt3, int color, int[] zBuffer, Camera camera) { +// // get position of vertexes +// int[] vp1 = vt1.getPosition(); +// int[] vp2 = vt2.getPosition(); +// int[] vp3 = vt3.getPosition(); +// int x1 = vp1[vx], y1 = vp1[vy], z1 = vp1[vz], +// x2 = vp2[vx], y2 = vp2[vy], z2 = vp2[vz], +// x3 = vp3[vx], y3 = vp3[vy], z3 = vp3[vz]; +// // color used if rendering type is wireframe or vertex +// int shadedColor = ColorUtils.lerpRBG(color, vt1.getColor(), -255); +// RenderUtils.drawLine(x1, y1, x2, y2, z1, shadedColor, zBuffer, this, camera); +// RenderUtils.drawLine(x2, y2, x3, y3, z2, shadedColor, zBuffer, this, camera); +// RenderUtils.drawLine(x3, y3, x1, y1, z3, shadedColor, zBuffer, this, camera); +// } +// +//} diff --git a/src/test/java/com/johnsproject/jpge/graphics/SceneWindowTest.java b/src/test/java/com/johnsproject/jpge/graphics/SceneWindowTest.java index 9cbc991..9af6ead 100644 --- a/src/test/java/com/johnsproject/jpge/graphics/SceneWindowTest.java +++ b/src/test/java/com/johnsproject/jpge/graphics/SceneWindowTest.java @@ -4,9 +4,5 @@ public class SceneWindowTest { - @Test - public void SceneWindowInitialzingTest() throws Exception { - SceneWindow frame = new SceneWindow(640, 480); - assert(frame.getDepthBuffer() != null); - } + }