Main repository of MikuMikuStudio
修订版 | 43740aa7143d09fb7e81aba0b7a19f18aae0bf8a (tree) |
---|---|
时间 | 2003-12-03 05:08:09 |
作者 | mojomonkey <mojomonkey@75d0...> |
Commiter | mojomonkey |
added quaternion to matrix code.
Allow spatial to be rotated via quaternions.
git-svn-id: http://jmonkeyengine.googlecode.com/svn/trunk@160 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
@@ -41,7 +41,7 @@ package com.jme.math; | ||
41 | 41 | * {x y z w}. |
42 | 42 | * |
43 | 43 | * @author Mark Powell |
44 | - * @version $Id: Quaternion.java,v 1.1 2003-11-24 15:07:44 mojomonkey Exp $ | |
44 | + * @version $Id: Quaternion.java,v 1.2 2003-12-02 20:08:09 mojomonkey Exp $ | |
45 | 45 | */ |
46 | 46 | public class Quaternion { |
47 | 47 | public float x, y, z, w; |
@@ -217,6 +217,33 @@ public class Quaternion { | ||
217 | 217 | } |
218 | 218 | |
219 | 219 | } |
220 | + | |
221 | + /** | |
222 | + * | |
223 | + * <code>toRotationMatrix</code> converts this quaternion to a rotational | |
224 | + * matrix. | |
225 | + * @return the rotation matrix representation of this quaternion. | |
226 | + */ | |
227 | + public Matrix3f toRotationMatrix( ) { | |
228 | + | |
229 | + Matrix3f matrix = new Matrix3f(); | |
230 | + matrix.set(0, 0, (1.0f - 2.0f * (y * y + z * z))); | |
231 | + matrix.set(0, 1, (2.0f * (x * y - w * z))); | |
232 | + matrix.set(0, 2, (2.0f * (x * z + w * y))); | |
233 | + | |
234 | + // Second row | |
235 | + matrix.set(1, 0, (2.0f * (x * y + w * z))); | |
236 | + matrix.set(1, 1, (1.0f - 2.0f * (x * x + z * z))); | |
237 | + matrix.set(1, 2, (2.0f * (y * z - w * x))); | |
238 | + | |
239 | + // Third row | |
240 | + matrix.set(2, 0, (2.0f * (x * z - w * y))); | |
241 | + matrix.set(2, 1, (2.0f * (y * z + w * x))); | |
242 | + matrix.set(2, 2, (1.0f - 2.0f * (x * x + y * y))); | |
243 | + | |
244 | + return matrix; | |
245 | + | |
246 | + } | |
220 | 247 | |
221 | 248 | /** |
222 | 249 | * <code>fromAngleAxis</code> sets this quaternion to the values |
@@ -74,7 +74,7 @@ import com.jme.util.LoggingSystem; | ||
74 | 74 | * <code>Renderer</code> interface using the LWJGL API. |
75 | 75 | * @see com.jme.renderer.Renderer |
76 | 76 | * @author Mark Powell |
77 | - * @version $Id: LWJGLRenderer.java,v 1.6 2003-12-01 13:18:58 mojomonkey Exp $ | |
77 | + * @version $Id: LWJGLRenderer.java,v 1.7 2003-12-02 20:08:09 mojomonkey Exp $ | |
78 | 78 | */ |
79 | 79 | public class LWJGLRenderer implements Renderer { |
80 | 80 | //clear color |
@@ -689,6 +689,7 @@ public class LWJGLRenderer implements Renderer { | ||
689 | 689 | Matrix3f rotation = t.getWorldRotation(); |
690 | 690 | Vector3f translation = t.getWorldTranslation(); |
691 | 691 | float scale = t.getWorldScale(); |
692 | + | |
692 | 693 | float[] modelToWorld = |
693 | 694 | { |
694 | 695 | scale * rotation.get(0, 0), |
@@ -34,6 +34,7 @@ package com.jme.scene; | ||
34 | 34 | import java.io.Serializable; |
35 | 35 | |
36 | 36 | import com.jme.math.Matrix3f; |
37 | +import com.jme.math.Quaternion; | |
37 | 38 | import com.jme.math.Vector3f; |
38 | 39 | import com.jme.renderer.Camera; |
39 | 40 | import com.jme.renderer.Renderer; |
@@ -45,7 +46,7 @@ import com.jme.scene.state.RenderState; | ||
45 | 46 | * transforms. All other nodes, such as <code>Node</code> and |
46 | 47 | * <code>Geometry</code> are subclasses of <code>Spatial</code>. |
47 | 48 | * @author Mark Powell |
48 | - * @version $Id: Spatial.java,v 1.4 2003-10-23 21:24:18 mojomonkey Exp $ | |
49 | + * @version $Id: Spatial.java,v 1.5 2003-12-02 20:08:09 mojomonkey Exp $ | |
49 | 50 | */ |
50 | 51 | public abstract class Spatial implements Serializable { |
51 | 52 | //rotation matrices |
@@ -308,6 +309,16 @@ public abstract class Spatial implements Serializable { | ||
308 | 309 | public void setLocalRotation(Matrix3f localRotation) { |
309 | 310 | this.localRotation = localRotation; |
310 | 311 | } |
312 | + | |
313 | + /** | |
314 | + * | |
315 | + * <code>setLocalRotation</code> sets the local rotation of this node, using | |
316 | + * a quaterion to build the matrix. | |
317 | + * @param quaternion the quaternion that defines the matrix. | |
318 | + */ | |
319 | + public void setLocalRotation(Quaternion quaternion) { | |
320 | + this.localRotation = quaternion.toRotationMatrix(); | |
321 | + } | |
311 | 322 | |
312 | 323 | /** |
313 | 324 | * <code>getLocalScale</code> retrieves the local scale of this node. |
@@ -37,6 +37,7 @@ import com.jme.input.FirstPersonController; | ||
37 | 37 | import com.jme.input.InputController; |
38 | 38 | import com.jme.light.DirectionalLight; |
39 | 39 | import com.jme.light.SpotLight; |
40 | +import com.jme.math.Quaternion; | |
40 | 41 | import com.jme.math.Vector3f; |
41 | 42 | import com.jme.renderer.Camera; |
42 | 43 | import com.jme.renderer.ColorRGBA; |
@@ -59,7 +60,7 @@ import com.jme.util.Timer; | ||
59 | 60 | /** |
60 | 61 | * <code>TestLightState</code> |
61 | 62 | * @author Mark Powell |
62 | - * @version $Id: TestTimer.java,v 1.3 2003-12-01 13:18:58 mojomonkey Exp $ | |
63 | + * @version $Id: TestTimer.java,v 1.4 2003-12-02 20:08:09 mojomonkey Exp $ | |
63 | 64 | */ |
64 | 65 | public class TestTimer extends AbstractGame { |
65 | 66 | private TriMesh t; |
@@ -70,6 +71,9 @@ public class TestTimer extends AbstractGame { | ||
70 | 71 | private InputController input; |
71 | 72 | private Thread thread; |
72 | 73 | private Timer timer; |
74 | + private Quaternion rotQuat; | |
75 | + private float angle = 0; | |
76 | + private Vector3f axis; | |
73 | 77 | |
74 | 78 | /** |
75 | 79 | * Entry point for the test, |
@@ -93,9 +97,18 @@ public class TestTimer extends AbstractGame { | ||
93 | 97 | * @see com.jme.app.AbstractGame#update() |
94 | 98 | */ |
95 | 99 | protected void update() { |
100 | + if(timer.getTimePerFrame() < 1) { | |
101 | + angle = angle + (timer.getTimePerFrame() * 1); | |
102 | + if(angle > 360) { | |
103 | + angle = 0; | |
104 | + } | |
105 | + } | |
106 | + rotQuat.fromAngleAxis(angle, axis); | |
96 | 107 | timer.update(); |
97 | 108 | input.update(timer.getTimePerFrame()); |
98 | 109 | text.print("Frame Rate: " + timer.getFrameRate()); |
110 | + scene.setLocalRotation(rotQuat); | |
111 | + scene.updateGeometricState(0.0f, true); | |
99 | 112 | |
100 | 113 | |
101 | 114 | } |
@@ -136,7 +149,7 @@ public class TestTimer extends AbstractGame { | ||
136 | 149 | ColorRGBA blackColor = new ColorRGBA(0, 0, 0, 1); |
137 | 150 | display.getRenderer().setBackgroundColor(blackColor); |
138 | 151 | cam.setFrustum(1.0f, 1000.0f, -0.55f, 0.55f, 0.4125f, -0.4125f); |
139 | - Vector3f loc = new Vector3f(0.0f, 0.0f, 4.0f); | |
152 | + Vector3f loc = new Vector3f(0.0f, 0.0f, 75.0f); | |
140 | 153 | Vector3f left = new Vector3f(-1.0f, 0.0f, 0.0f); |
141 | 154 | Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f); |
142 | 155 | Vector3f dir = new Vector3f(0.0f, 0f, -1.0f); |
@@ -149,6 +162,8 @@ public class TestTimer extends AbstractGame { | ||
149 | 162 | timer = Timer.getTimer("LWJGL"); |
150 | 163 | |
151 | 164 | display.getRenderer().setCullingMode(Renderer.CULL_BACK); |
165 | + rotQuat = new Quaternion(); | |
166 | + axis = new Vector3f(1,1,1); | |
152 | 167 | |
153 | 168 | } |
154 | 169 |