[svn:parrot] r39701 - in trunk: examples/opengl runtime/parrot/library/OpenGL
japhb at svn.parrot.org
japhb at svn.parrot.org
Mon Jun 22 04:52:43 UTC 2009
Author: japhb
Date: Mon Jun 22 04:52:42 2009
New Revision: 39701
URL: https://trac.parrot.org/parrot/changeset/39701
Log:
[OpenGL] Math: Macro tweaks; first vec-num binop; vec.normalize; refactor example
* OpenGL/Math.pir:
+ Rename vec-vec binop param extraction macros
+ Add vec-num binop param extraction macros
+ Add first vec-num binop (div_num, name subject to change)
+ Add vector.normalize()
* math.pir example:
+ Refactor
+ Improve output slightly
+ Add vector.normalize() test
Modified:
trunk/examples/opengl/math.pir
trunk/runtime/parrot/library/OpenGL/Math.pir
Modified: trunk/examples/opengl/math.pir
==============================================================================
--- trunk/examples/opengl/math.pir Mon Jun 22 04:48:36 2009 (r39700)
+++ trunk/examples/opengl/math.pir Mon Jun 22 04:52:42 2009 (r39701)
@@ -25,35 +25,65 @@
load_bytecode 'OpenGL/Math.pbc'
load_bytecode 'dumper.pbc'
+ # Create some sample data
+ .local pmc vec1, vec2
+ (vec1, vec2) = make_test_vectors()
+
+ # Run a few simple tests
+ sanity(vec1, vec2)
+ normalize(vec1)
+.end
+
+.sub make_test_vectors
# Test some basics
+ $P0 = new 'FixedFloatArray'
+ $P0 = 4
+ $P0[0] = 0.5
+ $P0[1] = 1.0
+ $P0[2] = 2.0
+ $P0[3] = 4.0
+
$P1 = new 'FixedFloatArray'
$P1 = 4
- $P1[0] = 0.5
- $P1[1] = 1.0
- $P1[2] = 2.0
+ $P1[0] = 1.0
+ $P1[1] = 2.0
+ $P1[2] = 3.0
$P1[3] = 4.0
- $P2 = new 'FixedFloatArray'
- $P2 = 4
- $P2[0] = 1.0
- $P2[1] = 2.0
- $P2[2] = 3.0
- $P2[3] = 4.0
-
.local pmc Vec4
Vec4 = get_class ['OpenGL';'Math';'Vec4']
+ $P2 = new Vec4
+ $P2.'set_vals'($P0)
+
$P3 = new Vec4
$P3.'set_vals'($P1)
- $P4 = new Vec4
- $P4.'set_vals'($P2)
+ .return ($P2, $P3)
+.end
+
+.sub sanity
+ .param pmc vec1
+ .param pmc vec2
+
+ .local pmc vec3
+ vec3 = vec1.'mult'(vec2)
+
+ say "vec1 * vec2 = vec3"
+ _dumper(vec1, 'vec1')
+ _dumper(vec2, 'vec2')
+ _dumper(vec3, 'vec3')
+.end
+
+.sub normalize
+ .param pmc vector
- $P5 = $P3.'mult'($P4)
+ .local pmc normalized
+ normalized = vector.'normalize'()
- _dumper($P3, 'Vec4')
- _dumper($P4, 'Vec4')
- _dumper($P5, 'Vec4')
+ say "normalize(vector) = normalized"
+ _dumper(vector, 'vector')
+ _dumper(normalized, 'normalized')
.end
# Local Variables:
Modified: trunk/runtime/parrot/library/OpenGL/Math.pir
==============================================================================
--- trunk/runtime/parrot/library/OpenGL/Math.pir Mon Jun 22 04:48:36 2009 (r39700)
+++ trunk/runtime/parrot/library/OpenGL/Math.pir Mon Jun 22 04:52:42 2009 (r39701)
@@ -143,8 +143,8 @@
=cut
-# Standard header for binop methods
-.macro vec4_extract_self_plus_arg
+# Standard header for vec4-vec4 binop methods
+.macro vec4_extract_self_plus_vec_arg
.param pmc vec2
.local pmc v1, v2
@@ -162,6 +162,48 @@
$N23 = v2[3]
.endm
+# Standard header for vec3-vec3 binop methods
+.macro vec3_extract_self_plus_vec_arg
+ .param pmc vec2
+
+ .local pmc v1, v2
+ v1 = getattribute self, 'vals'
+ v2 = getattribute vec2, 'vals'
+
+ $N10 = v1[0]
+ $N11 = v1[1]
+ $N12 = v1[2]
+
+ $N20 = v2[0]
+ $N21 = v2[1]
+ $N22 = v2[2]
+.endm
+
+# Standard header for vec4-num binop methods
+.macro vec4_extract_self_plus_num_arg
+ .param num N
+
+ .local pmc v1
+ v1 = getattribute self, 'vals'
+
+ $N10 = v1[0]
+ $N11 = v1[1]
+ $N12 = v1[2]
+ $N13 = v1[3]
+.endm
+
+# Standard header for vec3-num binop methods
+.macro vec3_extract_self_plus_num_arg
+ .param num N
+
+ .local pmc v1
+ v1 = getattribute self, 'vals'
+
+ $N10 = v1[0]
+ $N11 = v1[1]
+ $N12 = v1[2]
+.endm
+
# Standard footer for binop methods returning a vec4
.macro vec4_return_new_result
.local pmc v3
@@ -210,7 +252,7 @@
=cut
.sub add :method
- .vec4_extract_self_plus_arg
+ .vec4_extract_self_plus_vec_arg
$N30 = $N10 + $N20
$N31 = $N11 + $N21
@@ -229,7 +271,7 @@
=cut
.sub sub :method
- .vec4_extract_self_plus_arg
+ .vec4_extract_self_plus_vec_arg
$N30 = $N10 - $N20
$N31 = $N11 - $N21
@@ -248,7 +290,7 @@
=cut
.sub mult :method
- .vec4_extract_self_plus_arg
+ .vec4_extract_self_plus_vec_arg
$N30 = $N10 * $N20
$N31 = $N11 * $N21
@@ -268,7 +310,7 @@
=cut
.sub div :method
- .vec4_extract_self_plus_arg
+ .vec4_extract_self_plus_vec_arg
$N30 = $N10 / $N20
$N31 = $N11 / $N21
@@ -287,7 +329,7 @@
=cut
.sub mod :method
- .vec4_extract_self_plus_arg
+ .vec4_extract_self_plus_vec_arg
$N30 = $N10 % $N20
$N31 = $N11 % $N21
@@ -298,6 +340,26 @@
.end
+=item Vec4 result = vector.div_num(num N)
+
+Calculate the elementwise division C<vector / N> and return a new C<Vec4>
+vector C<result>. No attempt is made to prevent division by zero,
+XXX - SO WHAT HAPPENS?
+
+=cut
+
+.sub div_num :method
+ .vec4_extract_self_plus_num_arg
+
+ $N30 = $N10 / N
+ $N31 = $N11 / N
+ $N32 = $N12 / N
+ $N33 = $N13 / N
+
+ .vec4_return_new_result
+.end
+
+
=back
@@ -314,7 +376,7 @@
=cut
.sub cross :method
- .vec4_extract_self_plus_arg
+ .vec3_extract_self_plus_vec_arg
$N0 = $N11 * $N22
$N1 = $N21 * $N12
@@ -339,7 +401,7 @@
=cut
.sub dot :method
- .vec4_extract_self_plus_arg
+ .vec4_extract_self_plus_vec_arg
$N30 = $N10 * $N20
$N31 = $N11 * $N21
@@ -363,7 +425,7 @@
=cut
.sub dot3 :method
- .vec4_extract_self_plus_arg
+ .vec3_extract_self_plus_vec_arg
$N30 = $N10 * $N20
$N31 = $N11 * $N21
@@ -399,7 +461,7 @@
=cut
-.sub length :method
+.sub length3 :method
$N0 = self.'dot3'(self)
$N1 = sqrt $N0
@@ -407,6 +469,20 @@
.end
+=item Vec4 result = vector.normalize()
+
+Calculate a normalized version of C<vector> as C<vector / length(vector)>,
+returning the result as a new C<Vec4>.
+
+=cut
+
+.sub normalize :method
+ $N0 = self.'length'()
+ $P0 = self.'div_num'($N0)
+
+ .return($P0)
+.end
+
=item num dist = vec1.distance(vec2)
Treat two vectors C<vec1> and C<vec2> as points and determine the distance
More information about the parrot-commits
mailing list