OSDN Git Service

[added] new BaseCamera, OrthographicCamera, PerspectiveCamera and Frustum class....
authorbadlogicgames <badlogicgames@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Mon, 14 Feb 2011 16:02:02 +0000 (16:02 +0000)
committerbadlogicgames <badlogicgames@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Mon, 14 Feb 2011 16:02:02 +0000 (16:02 +0000)
gdx/src/com/badlogic/gdx/graphics/tmp/BaseCamera.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/graphics/tmp/Frustum.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/graphics/tmp/OrthographicCamera.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/graphics/tmp/PerspectiveCamera.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/math/Matrix4.java
tests/gdx-tests/src/com/badlogic/gdx/tests/IsoCamTest.java
tests/gdx-tests/src/com/badlogic/gdx/tests/ObjTest.java
tests/gdx-tests/src/com/badlogic/gdx/tests/SplineTest.java

diff --git a/gdx/src/com/badlogic/gdx/graphics/tmp/BaseCamera.java b/gdx/src/com/badlogic/gdx/graphics/tmp/BaseCamera.java
new file mode 100644 (file)
index 0000000..be194b2
--- /dev/null
@@ -0,0 +1,81 @@
+package com.badlogic.gdx.graphics.tmp;\r
+\r
+import com.badlogic.gdx.math.Matrix4;\r
+import com.badlogic.gdx.math.Vector3;\r
+import com.badlogic.gdx.math.collision.Ray;\r
+\r
+public abstract class BaseCamera {                             \r
+       /** the position of the camera **/\r
+       public final Vector3 position = new Vector3();\r
+       /** the unit length direction vector of the camera **/\r
+       public final Vector3 direction = new Vector3(0, 0, -1);\r
+       /** the unit length up vector of the camera **/\r
+       public final Vector3 up = new Vector3(0, 1, 0);\r
+       \r
+       /** the projection matrix **/\r
+       public final Matrix4 projection = new Matrix4();\r
+       /** the view matrix **/\r
+       public final Matrix4 view = new Matrix4();\r
+       /** the combined projection and view matrix **/\r
+       public final Matrix4 combined = new Matrix4();\r
+       \r
+       /** the near clipping plane distance, has to be positive **/\r
+       public float near = 1;  \r
+       /** the far clipping plane distance, has to be positive **/\r
+       public float far = 100;\r
+       \r
+       /** the viewport width **/\r
+       public float viewportWidth = 0;\r
+       /** the viewport height **/\r
+       public float viewportHeight = 0;\r
+       \r
+       /** the frustum **/\r
+       public final Frustum frustum = new Frustum();\r
+       \r
+       private final Matrix4 tmpMat = new Matrix4();\r
+       private final Vector3 tmpVec = new Vector3();\r
+       \r
+       /**\r
+        * Recalculates the projection and view matrix of this\r
+        * camera and the frustum planes. Use this after you've manipulated\r
+        * any of the attributes of the camera.\r
+        */\r
+       public abstract void update();  \r
+       \r
+       /**\r
+        * Recalculates the direction of the camera to look at the point\r
+        * (x, y, z).\r
+        * @param x the x-coordinate of the point to look at\r
+        * @param y the x-coordinate of the point to look at\r
+        * @param z the x-coordinate of the point to look at\r
+        */\r
+       public void lookAt(float x, float y, float z) { \r
+               direction.set(x, y, z).sub(position).nor();\r
+       }\r
+       \r
+       /**\r
+        * Rotates the direction and up vector of this camera by the\r
+        * given angle around the given axis. The direction and up\r
+        * vector will not be orthogonalized.\r
+        * \r
+        * @param angle the angle\r
+        * @param axisX the x-component of the axis\r
+        * @param axisY the y-component of the axis\r
+        * @param axisZ the z-component of the axis\r
+        */\r
+       public void rotate(float angle, float axisX, float axisY, float axisZ) {                \r
+               tmpMat.setToRotation(tmpVec.set(axisX, axisY, axisZ), angle);\r
+               direction.mul(tmpMat).nor();\r
+               up.mul(tmpMat).nor();\r
+       }\r
+       \r
+       /**\r
+        * Moves the camera by the given amount on each axis.\r
+        * @param x the displacement on the x-axis\r
+        * @param y the displacement on the y-axis\r
+        * @param z the displacement on the z-axis\r
+        */\r
+       public void translate(float x, float y, float z) {\r
+               position.add(x, y, z);\r
+       }\r
+}\r
diff --git a/gdx/src/com/badlogic/gdx/graphics/tmp/Frustum.java b/gdx/src/com/badlogic/gdx/graphics/tmp/Frustum.java
new file mode 100644 (file)
index 0000000..592d74d
--- /dev/null
@@ -0,0 +1,47 @@
+package com.badlogic.gdx.graphics.tmp;\r
+\r
+import com.badlogic.gdx.math.Matrix4;\r
+import com.badlogic.gdx.math.Plane;\r
+import com.badlogic.gdx.math.Vector3;\r
+\r
+public class Frustum {\r
+       protected static final Vector3[] clipSpacePlanePoints = { new Vector3(-1, -1, 1), new Vector3(1, -1, 1), new Vector3(1, 1, 1), new Vector3(-1, 1, 1), // near clip\r
+                                                                                                                  new Vector3(-1, -1, -1), new Vector3(1, -1, -1), new Vector3(1, 1, -1), new Vector3(-1, 1, -1) }; // far clip\r
+       \r
+       /** the six clipping planes, near, far, left, right, top, bottm **/\r
+       public final Plane[] planes = new Plane[6];     \r
+       \r
+       /** eight points making up the near and far clipping "rectangles". order is counter clockwise, starting at bottom left **/\r
+       protected final Vector3[] planePoints = { new Vector3(), new Vector3(), new Vector3(), new Vector3(), \r
+                       new Vector3(), new Vector3(), new Vector3(), new Vector3() \r
+       };      \r
+               \r
+       public Frustum() {\r
+               for(int i = 0; i < 6; i++) {\r
+                       planes[i] = new Plane(new Vector3(), 0);\r
+               }\r
+       }\r
+       \r
+       final Matrix4 invProjectionView = new Matrix4();\r
+       /**\r
+        * Updates the clipping plane's based on the given combined projection and view\r
+        * matrix, e.g. from an {@link OrthographicCamera} or {@link PerspectiveCamera}.\r
+        * @param projectionView the combined projection and view matrices.\r
+        */\r
+       public void update(Matrix4 projectionView) {\r
+               invProjectionView.set(projectionView);\r
+               invProjectionView.inv();\r
+               \r
+               for(int i = 0; i < 8; i++) {\r
+                       Vector3 point = planePoints[i].set(clipSpacePlanePoints[i]);\r
+                       point.prj(invProjectionView);                   \r
+               }\r
+               \r
+               planes[0].set(planePoints[1], planePoints[0], planePoints[2]);\r
+               planes[1].set(planePoints[4], planePoints[5], planePoints[7]);\r
+               planes[2].set(planePoints[0], planePoints[4], planePoints[3]);\r
+               planes[3].set(planePoints[5], planePoints[1], planePoints[6]);\r
+               planes[4].set(planePoints[2], planePoints[3], planePoints[6]);\r
+               planes[5].set(planePoints[4], planePoints[0], planePoints[1]);\r
+       }       \r
+}\r
diff --git a/gdx/src/com/badlogic/gdx/graphics/tmp/OrthographicCamera.java b/gdx/src/com/badlogic/gdx/graphics/tmp/OrthographicCamera.java
new file mode 100644 (file)
index 0000000..46572ff
--- /dev/null
@@ -0,0 +1,25 @@
+package com.badlogic.gdx.graphics.tmp;\r
+\r
+import java.util.Arrays;\r
+\r
+import com.badlogic.gdx.math.Vector3;\r
+import com.badlogic.gdx.math.collision.Ray;\r
+\r
+public class OrthographicCamera extends BaseCamera {\r
+       public float zoom = 1;\r
+       \r
+       public OrthographicCamera(float viewportWidth, float viewportHeight) {\r
+               this.viewportWidth = viewportWidth;\r
+               this.viewportHeight = viewportHeight;           \r
+               this.near = 0;\r
+       }\r
+       \r
+       private final Vector3 tmp = new Vector3();\r
+       @Override       \r
+       public void update() {\r
+               projection.setToOrtho(-viewportWidth / 2, viewportWidth / 2, -viewportHeight / 2, viewportHeight / 2, -Math.abs(near), -Math.abs(far));\r
+               view.setToLookAt(position, tmp.set(position).add(direction), up);       \r
+               combined.set(projection).mul(view);\r
+               frustum.update(combined);\r
+       }\r
+}\r
diff --git a/gdx/src/com/badlogic/gdx/graphics/tmp/PerspectiveCamera.java b/gdx/src/com/badlogic/gdx/graphics/tmp/PerspectiveCamera.java
new file mode 100644 (file)
index 0000000..09a21e8
--- /dev/null
@@ -0,0 +1,40 @@
+package com.badlogic.gdx.graphics.tmp;\r
+\r
+import java.util.Arrays;\r
+\r
+import com.badlogic.gdx.math.Vector3;\r
+\r
+public class PerspectiveCamera extends BaseCamera {    \r
+       /** the field of view in degrees **/\r
+       public float fieldOfView = 67;\r
+       \r
+       /**\r
+        * Constructs a new {@link PerspectiveCamera} with the given field of view and viewport\r
+        * size. The apsect ratio is derrived from the viewport size.\r
+        * \r
+        * @param fieldOfView the field of view in degrees\r
+        * @param viewportWidth the viewport width\r
+        * @param viewportHeight the viewport height\r
+        */\r
+       public PerspectiveCamera(float fieldOfView, float viewportWidth, float viewportHeight) {\r
+               this.fieldOfView = fieldOfView;\r
+               this.viewportWidth = viewportWidth;\r
+               this.viewportHeight = viewportHeight;\r
+       }\r
+       \r
+       final Vector3 tmp = new Vector3();\r
+       @Override\r
+       public void update() {\r
+               float aspect = viewportWidth / viewportHeight;                          \r
+               projection.setToProjection(Math.abs(near), Math.abs(far), fieldOfView, aspect);\r
+               view.setToLookAt(position, tmp.set(position).add(direction), up);       \r
+               combined.set(projection).mul(view);\r
+               frustum.update(combined);\r
+       }\r
+       \r
+       public static void main(String[] argv) {\r
+               PerspectiveCamera cam = new PerspectiveCamera(90, 1, 1);\r
+               cam.update();\r
+               System.out.println(Arrays.toString(cam.frustum.planes));\r
+       }\r
+}\r
index 397f5c6..f49bab9 100644 (file)
@@ -413,6 +413,7 @@ public class Matrix4 implements Serializable {
                val[M13] = 0;\r
                val[M23] = l_a2;\r
                val[M33] = 0;\r
+               \r
                return this;\r
        }\r
 \r
index ad36093..1d3bc65 100644 (file)
@@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.Mesh;
 import com.badlogic.gdx.graphics.Texture;\r
 import com.badlogic.gdx.graphics.VertexAttribute;\r
 import com.badlogic.gdx.graphics.VertexAttributes.Usage;\r
+import com.badlogic.gdx.graphics.tmp.OrthographicCamera;\r
 import com.badlogic.gdx.math.Matrix4;\r
 import com.badlogic.gdx.math.Vector3;\r
 import com.badlogic.gdx.tests.utils.GdxTest;\r
@@ -18,8 +19,9 @@ public class IsoCamTest extends GdxTest {
 \r
        Texture texture;\r
        Mesh mesh;\r
-       Matrix4 projection;\r
-       Matrix4 view;\r
+       OrthographicCamera cam;\r
+//     Matrix4 projection;\r
+//     Matrix4 view;\r
        \r
        @Override public void create() {\r
                mesh = new Mesh(true, 4, 6, new VertexAttribute(Usage.Position, 3, "a_pos"));\r
@@ -29,20 +31,20 @@ public class IsoCamTest extends GdxTest {
                         1, 0, -1,\r
                        -1, 0, -1\r
                });\r
-               mesh.setIndices(new short[] { 0, 1, 2, 2, 3, 0 });\r
-               \r
-               projection = new Matrix4();\r
-               projection.setToOrtho(-5, 5, -5, 5, -1, -100);\r
-               view = new Matrix4();\r
-               view.setToLookAt(new Vector3(3, 3, 3), new Vector3(0,0,0), new Vector3(0, 1, 0));\r
+               mesh.setIndices(new short[] { 0, 1, 2, 2, 3, 0 });              \r
+               cam = new OrthographicCamera(10, 10);           \r
+               cam.position.set(3, 3, 3);\r
+               cam.lookAt(0, 0, 0);                    \r
        }\r
        \r
        @Override public void render() {\r
                Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);\r
+               \r
+               cam.update();           \r
                Gdx.gl10.glMatrixMode(GL10.GL_PROJECTION);\r
-               Gdx.gl10.glLoadMatrixf(projection.val, 0);\r
+               Gdx.gl10.glLoadMatrixf(cam.projection.val, 0);\r
                Gdx.gl10.glMatrixMode(GL10.GL_MODELVIEW);\r
-               Gdx.gl10.glLoadMatrixf(view.val, 0);\r
+               Gdx.gl10.glLoadMatrixf(cam.view.val, 0);\r
                \r
                mesh.render(GL10.GL_TRIANGLES);\r
        }\r
index bee5a2e..ed7d429 100644 (file)
@@ -21,8 +21,8 @@ import com.badlogic.gdx.graphics.Mesh;
 import com.badlogic.gdx.graphics.Texture;\r
 import com.badlogic.gdx.graphics.Texture.TextureFilter;\r
 import com.badlogic.gdx.graphics.Texture.TextureWrap;\r
-import com.badlogic.gdx.graphics.g3d.PerspectiveCamera;\r
 import com.badlogic.gdx.graphics.g3d.loaders.ModelLoader;\r
+import com.badlogic.gdx.graphics.tmp.PerspectiveCamera;\r
 import com.badlogic.gdx.tests.utils.GdxTest;\r
 \r
 public class ObjTest extends GdxTest implements InputProcessor {\r
@@ -42,9 +42,9 @@ public class ObjTest extends GdxTest implements InputProcessor {
                texture = new Texture(Gdx.files.internal("data/badlogic.jpg"), true);\r
                texture.setFilter(TextureFilter.MipMap, TextureFilter.Linear);\r
 \r
-               cam = new PerspectiveCamera();\r
-               cam.getPosition().set(3, 3, 3);\r
-               cam.getDirection().set(-1, -1, -1);\r
+               cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());\r
+               cam.position.set(3, 3, 3);\r
+               cam.direction.set(-1, -1, -1);\r
                Gdx.input.setInputProcessor(this);\r
        }\r
 \r
@@ -58,7 +58,11 @@ public class ObjTest extends GdxTest implements InputProcessor {
                gl.glEnable(GL10.GL_COLOR_MATERIAL);\r
                gl.glEnable(GL10.GL_TEXTURE_2D);\r
 \r
-               cam.setMatrices();\r
+               cam.update();\r
+               Gdx.gl10.glMatrixMode(GL10.GL_PROJECTION);\r
+               Gdx.gl10.glLoadMatrixf(cam.projection.val, 0);\r
+               Gdx.gl10.glMatrixMode(GL10.GL_MODELVIEW);\r
+               Gdx.gl10.glLoadMatrixf(cam.view.val, 0);\r
 \r
                gl.glEnable(GL10.GL_LIGHT0);\r
                gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightColor, 0);\r
index 79bf9f4..850c6ee 100644 (file)
@@ -6,8 +6,8 @@ import java.util.List;
 \r
 import com.badlogic.gdx.Gdx;\r
 import com.badlogic.gdx.graphics.GL10;\r
-import com.badlogic.gdx.graphics.g2d.OrthographicCamera;\r
 import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer;\r
+import com.badlogic.gdx.graphics.tmp.OrthographicCamera;\r
 import com.badlogic.gdx.math.CatmullRomSpline;\r
 import com.badlogic.gdx.math.Vector2;\r
 import com.badlogic.gdx.math.Vector3;\r
@@ -27,9 +27,8 @@ public class SplineTest extends GdxTest {
        \r
 \r
        @Override public void create () {\r
-               cam = new OrthographicCamera();\r
-               cam.setViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());\r
-               cam.getPosition().set(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0);\r
+               cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());\r
+               cam.position.set(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0);\r
                renderer = new ImmediateModeRenderer();\r
                spline = new CatmullRomSpline();\r
                float x = 0;\r
@@ -48,7 +47,11 @@ public class SplineTest extends GdxTest {
        \r
        @Override public void render () {\r
                Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);               \r
-               cam.setMatrices();              \r
+               cam.update();\r
+               Gdx.gl10.glMatrixMode(GL10.GL_PROJECTION);\r
+               Gdx.gl10.glLoadMatrixf(cam.projection.val, 0);\r
+               Gdx.gl10.glMatrixMode(GL10.GL_MODELVIEW);\r
+               Gdx.gl10.glLoadMatrixf(cam.view.val, 0);\r
                \r
                renderer.begin(GL10.GL_TRIANGLES);\r
                for(int i = 0; i < path.length - 1; i++) {\r
@@ -85,24 +88,24 @@ public class SplineTest extends GdxTest {
        \r
        Vector3 point = new Vector3();\r
        private void processInput() {\r
-               if(Gdx.input.isTouched()) {                     \r
-                       Vector3 nearest = null;\r
-                       float nearestDist = Float.MAX_VALUE;\r
-                       point.set(cam.getScreenToWorldX(Gdx.input.getX()), \r
-                                cam.getScreenToWorldY(Gdx.input.getY()),\r
-                                0);                    \r
-                       \r
-                       for(int i = 0; i < spline.getControlPoints().size(); i++) {\r
-                               Vector3 controlPoint = spline.getControlPoints().get(i);\r
-                               float dist = Math.abs(point.x - controlPoint.x);\r
-                               if(dist < nearestDist) {\r
-                                       nearest = controlPoint;\r
-                                       nearestDist = dist; \r
-                               }                                                               \r
-                       }\r
-                       \r
-                       nearest.y += (point.y - nearest.y) * Gdx.graphics.getDeltaTime();               \r
-                       spline.getPath(path, 5);\r
-               }\r
+//             if(Gdx.input.isTouched()) {                     \r
+//                     Vector3 nearest = null;\r
+//                     float nearestDist = Float.MAX_VALUE;\r
+//                     point.set(cam.getScreenToWorldX(Gdx.input.getX()), \r
+//                              cam.getScreenToWorldY(Gdx.input.getY()),\r
+//                              0);                    \r
+//                     \r
+//                     for(int i = 0; i < spline.getControlPoints().size(); i++) {\r
+//                             Vector3 controlPoint = spline.getControlPoints().get(i);\r
+//                             float dist = Math.abs(point.x - controlPoint.x);\r
+//                             if(dist < nearestDist) {\r
+//                                     nearest = controlPoint;\r
+//                                     nearestDist = dist; \r
+//                             }                                                               \r
+//                     }\r
+//                     \r
+//                     nearest.y += (point.y - nearest.y) * Gdx.graphics.getDeltaTime();               \r
+//                     spline.getPath(path, 5);\r
+//             }\r
        }\r
 }\r