Paste number 50721: gl matrix multiplication

Index of paste annotations: 3 | 2 | 1

Paste number 50721: gl matrix multiplication
Pasted by: baggles
When:2 years, 4 months ago
Share:Tweet this! | http://paste.lisp.org/+134X
Channel:#lispcafe
Paste contents:
Raw Source | XML | Display As

(defun gl-matrix-multiply (matrix vertex)
  "multiplies vector by matrix, returns a list vector"
  (declare (type vector matrix))
  (declare (type list vertex))
  (loop for result-row below 4 collecting
       (loop for v in vertex 
          for mi from result-row by 4 summing
            (* v (aref matrix mi)))))

Annotations for this paste:

Annotation number 3: not right
Pasted by: baggles
When:2 years, 4 months ago
Share:Tweet this! | http://paste.lisp.org/+134X/3
Paste contents:
Raw Source | Display As
(defun gl-matrix-times-matrix (matrix1 matrix2)
  "Multiplies two opengl-style column-major-matrices."
  (declare (type vector matrix1 matrix2))
  (make-array 
   16 :element-type 'single-float :initial-contents
   (loop for ri below 16 collecting
        (progn
          (format t "~&cell ~a: summing" ri)
          (loop 
             for i1 from (mod ri 4) by 4 below 16
             for i2 from (floor ri 4)
             summing
             (progn
               (format t " m1[~a]Śm2[~a]" i1 i2)
               (* (aref matrix1 i1)
                  (aref matrix2 i2))))))))

CS> (maths:gl-matrix-times-matrix #(1.0 0.0 0.0 0.0
                                    0.0 1.0 0.0 0.0
                                    0.0 0.0 1.0 0.0
                                    0.0 0.0 0.0 1.0)
                                  #(1.0 0.0 0.0 0.0
                                    0.0 1.0 0.0 0.0
                                    0.0 0.0 1.0 0.0
                                    0.0 0.0 0.0 1.0))
cell 0: summing m1[0]Śm2[0] m1[4]Śm2[1] m1[8]Śm2[2] m1[12]Śm2[3]
cell 1: summing m1[1]Śm2[0] m1[5]Śm2[1] m1[9]Śm2[2] m1[13]Śm2[3]
cell 2: summing m1[2]Śm2[0] m1[6]Śm2[1] m1[10]Śm2[2] m1[14]Śm2[3]
cell 3: summing m1[3]Śm2[0] m1[7]Śm2[1] m1[11]Śm2[2] m1[15]Śm2[3]
cell 4: summing m1[0]Śm2[1] m1[4]Śm2[2] m1[8]Śm2[3] m1[12]Śm2[4]
cell 5: summing m1[1]Śm2[1] m1[5]Śm2[2] m1[9]Śm2[3] m1[13]Śm2[4]
cell 6: summing m1[2]Śm2[1] m1[6]Śm2[2] m1[10]Śm2[3] m1[14]Śm2[4]
cell 7: summing m1[3]Śm2[1] m1[7]Śm2[2] m1[11]Śm2[3] m1[15]Śm2[4]
cell 8: summing m1[0]Śm2[2] m1[4]Śm2[3] m1[8]Śm2[4] m1[12]Śm2[5]
cell 9: summing m1[1]Śm2[2] m1[5]Śm2[3] m1[9]Śm2[4] m1[13]Śm2[5]
cell 10: summing m1[2]Śm2[2] m1[6]Śm2[3] m1[10]Śm2[4] m1[14]Śm2[5]
cell 11: summing m1[3]Śm2[2] m1[7]Śm2[3] m1[11]Śm2[4] m1[15]Śm2[5]
cell 12: summing m1[0]Śm2[3] m1[4]Śm2[4] m1[8]Śm2[5] m1[12]Śm2[6]
cell 13: summing m1[1]Śm2[3] m1[5]Śm2[4] m1[9]Śm2[5] m1[13]Śm2[6]
cell 14: summing m1[2]Śm2[3] m1[6]Śm2[4] m1[10]Śm2[5] m1[14]Śm2[6]
cell 15: summing m1[3]Śm2[3] m1[7]Śm2[4] m1[11]Śm2[5] m1[15]Śm2[6]#(1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0)
CS> 

Annotation number 2: Matrices, the ZabaQ way
Pasted by: ZabaQ
When:2 years, 4 months ago
Share:Tweet this! | http://paste.lisp.org/+134X/2
Paste contents:
Raw Source | Display As

(defmacro transform-vector3d (matrix33 vector3d)
  `(with-vector3d 
       ,vector3d (x y z)
       (with-matrix44 
           ,matrix44 (e00 e01 e02 e03
                      e10 e11 e12 e13
                      e20 e21 e22 e23
                      e30 e31 e32 e33)
           (values
            (+ (* x e00) (* y e01) (* z e02))
            (+ (* x e10) (* y e11) (* z e02))
            (+ (* x e20) (* y e21) (* z e02))
            (+ (* x e30) (* y e31) (* z e32))))))


(defmacro matrix44-product (m0 m1)
  `(with-matrix44 ,m0
       (e000 e001 e002 e003
        e010 e011 e012 e013
        e020 e021 e022 e023
        e030 e031 e032 e033)
     (with-matrix44 ,m1
         (e100 e101 e102 e103
          e110 e111 e112 e113
          e120 e121 e122 e123
          e130 e131 e132 e133)
       (values
        (matrix-dot 4 0 0)
        (matrix-dot 4 0 1)
        (matrix-dot 4 0 2)
        (matrix-dot 4 0 3)

        (matrix-dot 4 1 0)
        (matrix-dot 4 1 1)
        (matrix-dot 4 1 2)
        (matrix-dot 4 1 3)

        (matrix-dot 4 2 0)
        (matrix-dot 4 2 1)
        (matrix-dot 4 2 2)
        (matrix-dot 4 2 3)

        (matrix-dot 4 3 0)
        (matrix-dot 4 3 1)
        (matrix-dot 4 3 2)
        (matrix-dot 4 3 3)))))

Annotation number 1: improved
Pasted by: baggles
When:2 years, 4 months ago
Share:Tweet this! | http://paste.lisp.org/+134X/1
Paste contents:
Raw Source | Display As

(defun gl-matrix-times-vertex (matrix vertex)
  "Multiplies vertex by an opengl-style column-major matrix,
 returns a list vertex"
  (declare (type vector matrix))
  (declare (type list vertex))
  (loop for result-row below (length vertex) collecting
       (loop for v in vertex 
          for mi from result-row by 4 summing
            (* v (aref matrix mi)))))

(defun gl-matrix-times-matrix (matrix1 matrix2)
  "Multiplies two opengl-style column-major-matrices."
  (declare (type vector matrix1 matrix2))
  (loop for ri below 16 collecting
       (loop repeat 4
          for i1 from (mod ri 4) by 4
          for i2 from (floor ri 4)
          summing (* (aref matrix1 i1)
                     (aref matrix2 i2)))))

Colorize as:
Show Line Numbers
Index of paste annotations: 3 | 2 | 1

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.