reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofMatrix4x4.h
Go to the documentation of this file.
1/*
2 * ofMatrix4x4.h
3 *
4 * Created by Aaron Meyers on 6/22/09 -- modified by Arturo Castro, Zach Lieberman, Memo Akten
5 * based on code from OSG -
6 * see OSG license for more details:
7 * http://www.openscenegraph.org/projects/osg/wiki/Legal
8 *
9 */
10
11#pragma once
12
13#include "ofVec4f.h"
14#include "ofQuaternion.h"
15#include "ofMathConstants.h"
16#include "ofConstants.h"
17#include "glm/mat4x4.hpp"
18#include <cmath>
19
20class ofVec3f;
21
59public:
60// float _mat[4][4];
61
65 // Should this be moved to private?
67
70
75
76 ofMatrix4x4( const glm::mat4 & mat) {
77 *this = reinterpret_cast<const ofMatrix4x4&>(mat);
78 }
79
80 operator glm::mat4() const{
81 return *reinterpret_cast<const glm::mat4*>(this);
82 }
83
90 ofMatrix4x4( float const * const ptr ) {
91 set(ptr);
92 }
93
95 ofMatrix4x4( const ofQuaternion& quat ) {
97 }
98
102 ofMatrix4x4( float a00, float a01, float a02, float a03,
103 float a10, float a11, float a12, float a13,
104 float a20, float a21, float a22, float a23,
105 float a30, float a31, float a32, float a33);
106
108
111
116
118 void makeIdentityMatrix();
119
125 void makeScaleMatrix( const ofVec3f& );
126 void makeScaleMatrix( float, float, float );
128
134 void makeTranslationMatrix( const ofVec3f& );
135 void makeTranslationMatrix( float, float, float );
137
144 void makeRotationMatrix( const ofVec3f& from, const ofVec3f& to );
147 void makeRotationMatrix( float angle, const ofVec3f& axis );
152 void makeRotationMatrix( float angle, float x, float y, float z );
154 void makeRotationMatrix( const ofQuaternion& quaternion );
160 void makeRotationMatrix( float angle1, const ofVec3f& axis1,
161 float angle2, const ofVec3f& axis2,
162 float angle3, const ofVec3f& axis3);
164
165
168
170 bool makeInvertOf( const ofMatrix4x4& rhs);
171
178 void makeOrthoNormalOf(const ofMatrix4x4& rhs);
179
181 void makeFromMultiplicationOf( const ofMatrix4x4&, const ofMatrix4x4& );
182
184
190
197 void makeOrthoMatrix(double left, double right,
198 double bottom, double top,
199 double zNear, double zFar);
200
206 void makeOrtho2DMatrix(double left, double right,
207 double bottom, double top);
208
217 void makeFrustumMatrix(double left, double right,
218 double bottom, double top,
219 double zNear, double zFar);
220
231 void makePerspectiveMatrix(double fovy, double aspectRatio,
232 double zNear, double zFar);
233
244 void makeLookAtMatrix(const ofVec3f& eye, const ofVec3f& center, const ofVec3f& up);
245
259 void makeLookAtViewMatrix(const ofVec3f& eye, const ofVec3f& center, const ofVec3f& up);
260
262
264
271
273 inline static ofMatrix4x4 newIdentityMatrix( void );
274
276 inline static ofMatrix4x4 newScaleMatrix( const ofVec3f& sv);
277 inline static ofMatrix4x4 newScaleMatrix( float sx, float sy, float sz);
278
280 inline static ofMatrix4x4 newTranslationMatrix( const ofVec3f& dv);
281 inline static ofMatrix4x4 newTranslationMatrix( float x, float y, float z);
282
284 inline static ofMatrix4x4 newRotationMatrix( const ofVec3f& from, const ofVec3f& to);
285 inline static ofMatrix4x4 newRotationMatrix( float angle, float x, float y, float z);
286 inline static ofMatrix4x4 newRotationMatrix( float angle, const ofVec3f& axis);
287 inline static ofMatrix4x4 newRotationMatrix( float angle1, const ofVec3f& axis1,
288 float angle2, const ofVec3f& axis2,
289 float angle3, const ofVec3f& axis3);
290 inline static ofMatrix4x4 newRotationMatrix( const ofQuaternion& quat);
291
294
296 inline static ofMatrix4x4 newOrthoMatrix(double left, double right,
297 double bottom, double top,
298 double zNear, double zFar);
299
301 inline static ofMatrix4x4 newOrtho2DMatrix(double left, double right,
302 double bottom, double top);
303
305 inline static ofMatrix4x4 newFrustumMatrix(double left, double right,
306 double bottom, double top,
307 double zNear, double zFar);
308
310 inline static ofMatrix4x4 newPerspectiveMatrix(double fovy, double aspectRatio,
311 double zNear, double zFar);
312
314 inline static ofMatrix4x4 newLookAtMatrix(const ofVec3f& eye,
315 const ofVec3f& center,
316 const ofVec3f& up);
317
319
320 //---------------------
323
325 float& operator()(std::size_t row, std::size_t col) {
326 return _mat[row][col];
327 }
328
330 float operator()(std::size_t row, std::size_t col) const {
331 return _mat[row][col];
332 }
333
335 ofVec3f getRowAsVec3f(std::size_t i) const {
336 return ofVec3f(_mat[i][0], _mat[i][1], _mat[i][2]);
337 }
338
340 ofVec4f getRowAsVec4f(std::size_t i) const {
341 return _mat[i];
342 }
343
345 friend std::ostream& operator<<(std::ostream& os, const ofMatrix4x4& M);
346 friend std::istream& operator>>(std::istream& is, ofMatrix4x4& M);
348
351 float * getPtr() {
352 return (float*)_mat;
353 }
354 const float * getPtr() const {
355 return (const float *)_mat;
356 }
357
359
360 //---------------------
363
365 bool isValid() const {
366 return !isNaN();
367 }
368
370 bool isNaN() const;
371
373 bool isIdentity() const;
374
376
377 //---------------------
380
383
384
388 void set(const ofMatrix4x4& rhs);
389 void set(float const * const ptr);
390 void set(double const * const ptr);
391 void set(float a00, float a01, float a02, float a03,
392 float a10, float a11, float a12, float a13,
393 float a20, float a21, float a22, float a23,
394 float a30, float a31, float a32, float a33);
395
397
398 //---------------------
401
403 ofMatrix4x4 getInverse() const;
404
408 bool getOrtho(double& left, double& right,
409 double& bottom, double& top,
410 double& zNear, double& zFar) const;
411
413 bool getFrustum(double& left, double& right,
414 double& bottom, double& top,
415 double& zNear, double& zFar) const;
416
428 bool getPerspective(double& fovy, double& aspectRatio,
429 double& zNear, double& zFar) const;
430
434 void getLookAt(ofVec3f& eye, ofVec3f& center, ofVec3f& up,
435 float lookDistance = 1.0f) const;
436
439 void decompose( ofVec3f& translation,
440 ofQuaternion& rotation,
441 ofVec3f& scale,
442 ofQuaternion& so ) const;
443
445
447 inline static ofMatrix4x4 getInverseOf( const ofMatrix4x4& matrix);
449 inline static ofMatrix4x4 getTransposedOf( const ofMatrix4x4& matrix);
451 inline static ofMatrix4x4 getOrthoNormalOf(const ofMatrix4x4& matrix);
452
453
455
456 //---------------------
459
511
515 inline ofVec3f postMult( const ofVec3f& v ) const;
516 inline ofVec4f postMult( const ofVec4f& v ) const;
517
521 void postMult( const ofMatrix4x4& );
522
526 inline ofVec3f preMult( const ofVec3f& v ) const;
527 inline ofVec4f preMult( const ofVec4f& v ) const;
528
532 void preMult( const ofMatrix4x4& );
533
539 inline void operator *= ( const ofMatrix4x4& other ) {
540 if ( this == &other ) {
541 ofMatrix4x4 temp(other);
542 postMult( temp );
543 } else postMult( other );
544 }
545
547 inline ofMatrix4x4 operator * ( const ofMatrix4x4 &m ) const {
548 ofMatrix4x4 r;
549 r.makeFromMultiplicationOf(*this, m);
550 return r;
551 }
552
556 inline ofVec3f operator* (const ofVec3f& v) const {
557 return postMult(v);
558 }
559
560 inline ofVec4f operator* (const ofVec4f& v) const {
561 return postMult(v);
562 }
563
566
568 inline void postMultTranslate( const ofVec3f& v );
570 inline void postMultScale( const ofVec3f& v );
572 inline void postMultRotate( const ofQuaternion& q );
573
574 inline void postMultTranslate(float x, float y, float z);
575 inline void postMultRotate(float angle, float x, float y, float z);
576 inline void postMultScale(float x, float y, float z);
577
579 inline void preMultScale( const ofVec3f& v );
581 inline void preMultTranslate( const ofVec3f& v );
583 inline void preMultRotate( const ofQuaternion& q );
584
586
587 //---------------------
590
596 void setRotate(const ofQuaternion& q);
597 void setTranslation( float tx, float ty, float tz );
598 void setTranslation( const ofVec3f& v );
600
609
611 void rotate(float angle, float x, float y, float z);
613 void rotateRad(float angle, float x, float y, float z);
615 void rotate(const ofQuaternion& q);
617 void translate( float tx, float ty, float tz );
619 void translate( const ofVec3f& v );
621 void scale(float x, float y, float z);
623 void scale( const ofVec3f& v );
625
632
634 void glRotate(float angle, float x, float y, float z);
636 void glRotateRad(float angle, float x, float y, float z);
638 void glRotate(const ofQuaternion& q);
640 void glTranslate( float tx, float ty, float tz );
642 void glTranslate( const ofVec3f& v );
644 void glScale(float x, float y, float z);
646 void glScale( const ofVec3f& v );
648
656 ofQuaternion getRotate() const;
660
662 inline static ofVec3f transform3x3(const ofVec3f& v, const ofMatrix4x4& m);
663
665 inline static ofVec3f transform3x3(const ofMatrix4x4& m, const ofVec3f& v);
666
668
669};
670
672
673//--------------------------------------------------
674// implementation of inline methods
675
676inline bool ofMatrix4x4::isNaN() const {
677 return std::isnan(_mat[0][0]) || std::isnan(_mat[0][1]) || std::isnan(_mat[0][2]) || std::isnan(_mat[0][3]) ||
678 std::isnan(_mat[1][0]) || std::isnan(_mat[1][1]) || std::isnan(_mat[1][2]) || std::isnan(_mat[1][3]) ||
679 std::isnan(_mat[2][0]) || std::isnan(_mat[2][1]) || std::isnan(_mat[2][2]) || std::isnan(_mat[2][3]) ||
680 std::isnan(_mat[3][0]) || std::isnan(_mat[3][1]) || std::isnan(_mat[3][2]) || std::isnan(_mat[3][3]);
681}
682
683
684
685std::ostream& operator<<(std::ostream& os, const ofMatrix4x4& M);
686
687std::istream& operator>>(std::istream& is, ofMatrix4x4& M);
688
689
691 if ( &rhs == this ) return *this;
692 set(rhs.getPtr());
693 return *this;
694}
695
696inline void ofMatrix4x4::set(const ofMatrix4x4& rhs) {
697 set(rhs.getPtr());
698}
699
700inline void ofMatrix4x4::set(float const * const ptr) {
701 float* local_ptr = (float*)_mat;
702 for (std::size_t i = 0;i < 16;++i) local_ptr[i] = (float)ptr[i];
703}
704
705inline void ofMatrix4x4::set(double const * const ptr) {
706 float* local_ptr = (float*)_mat;
707 for (std::size_t i = 0;i < 16;++i) local_ptr[i] = (float)ptr[i];
708}
709
710inline bool ofMatrix4x4::isIdentity() const {
711 return _mat[0][0] == 1.0f && _mat[0][1] == 0.0f && _mat[0][2] == 0.0f && _mat[0][3] == 0.0f &&
712 _mat[1][0] == 0.0f && _mat[1][1] == 1.0f && _mat[1][2] == 0.0f && _mat[1][3] == 0.0f &&
713 _mat[2][0] == 0.0f && _mat[2][1] == 0.0f && _mat[2][2] == 1.0f && _mat[2][3] == 0.0f &&
714 _mat[3][0] == 0.0f && _mat[3][1] == 0.0f && _mat[3][2] == 0.0f && _mat[3][3] == 1.0f;
715}
716
717inline void ofMatrix4x4::makeOrtho2DMatrix(double left, double right,
718 double bottom, double top) {
719 makeOrthoMatrix(left, right, bottom, top, -1.0, 1.0);
720}
721
723 return ofVec3f(_mat[3][0], _mat[3][1], _mat[3][2]);
724}
725
726inline ofVec3f ofMatrix4x4::getScale() const {
727 ofVec3f x_vec(_mat[0][0], _mat[1][0], _mat[2][0]);
728 ofVec3f y_vec(_mat[0][1], _mat[1][1], _mat[2][1]);
729 ofVec3f z_vec(_mat[0][2], _mat[1][2], _mat[2][2]);
730 return ofVec3f(x_vec.length(), y_vec.length(), z_vec.length());
731}
732
733//static utility methods
735 ofMatrix4x4 m;
737 return m;
738}
739
740inline ofMatrix4x4 ofMatrix4x4::newScaleMatrix(float sx, float sy, float sz) {
741 ofMatrix4x4 m;
742 m.makeScaleMatrix(sx, sy, sz);
743 return m;
744}
745
747 return newScaleMatrix(v.x, v.y, v.z );
748}
749
750inline ofMatrix4x4 ofMatrix4x4::newTranslationMatrix(float tx, float ty, float tz) {
751 ofMatrix4x4 m;
753 return m;
754}
755
757 return newTranslationMatrix(v.x, v.y, v.z );
758}
759
761 return ofMatrix4x4(q);
762}
763inline ofMatrix4x4 ofMatrix4x4::newRotationMatrix(float angle, float x, float y, float z ) {
764 ofMatrix4x4 m;
765 m.makeRotationMatrix(angle, x, y, z);
766 return m;
767}
768inline ofMatrix4x4 ofMatrix4x4::newRotationMatrix(float angle, const ofVec3f& axis ) {
769 ofMatrix4x4 m;
770 m.makeRotationMatrix(angle, axis);
771 return m;
772}
773inline ofMatrix4x4 ofMatrix4x4::newRotationMatrix( float angle1, const ofVec3f& axis1,
774 float angle2, const ofVec3f& axis2,
775 float angle3, const ofVec3f& axis3) {
776 ofMatrix4x4 m;
777 m.makeRotationMatrix(angle1, axis1, angle2, axis2, angle3, axis3);
778 return m;
779}
780inline ofMatrix4x4 ofMatrix4x4::newRotationMatrix(const ofVec3f& from, const ofVec3f& to ) {
781 ofMatrix4x4 m;
782 m.makeRotationMatrix(from, to);
783 return m;
784}
785
787 ofMatrix4x4 m;
788 m.makeInvertOf(matrix);
789 return m;
790}
791
793 ofMatrix4x4 m(matrix._mat[0][0], matrix._mat[1][0], matrix._mat[2][0],
794 matrix._mat[3][0], matrix._mat[0][1], matrix._mat[1][1], matrix._mat[2][1],
795 matrix._mat[3][1], matrix._mat[0][2], matrix._mat[1][2], matrix._mat[2][2],
796 matrix._mat[3][2], matrix._mat[0][3], matrix._mat[1][3], matrix._mat[2][3],
797 matrix._mat[3][3]);
798 return m;
799}
800
802 ofMatrix4x4 m;
803 m.makeOrthoNormalOf(matrix);
804 return m;
805}
806
807inline ofMatrix4x4 ofMatrix4x4::newOrthoMatrix(double left, double right,
808 double bottom, double top,
809 double zNear, double zFar) {
810 ofMatrix4x4 m;
811 m.makeOrthoMatrix(left, right, bottom, top, zNear, zFar);
812 return m;
813}
814
815inline ofMatrix4x4 ofMatrix4x4::newOrtho2DMatrix(double left, double right,
816 double bottom, double top) {
817 ofMatrix4x4 m;
818 m.makeOrtho2DMatrix(left, right, bottom, top);
819 return m;
820}
821
822inline ofMatrix4x4 ofMatrix4x4::newFrustumMatrix(double left, double right,
823 double bottom, double top,
824 double zNear, double zFar) {
825 ofMatrix4x4 m;
826 m.makeFrustumMatrix(left, right, bottom, top, zNear, zFar);
827 return m;
828}
829
830inline ofMatrix4x4 ofMatrix4x4::newPerspectiveMatrix(double fovy, double aspectRatio,
831 double zNear, double zFar) {
832 ofMatrix4x4 m;
833 m.makePerspectiveMatrix(fovy, aspectRatio, zNear, zFar);
834 return m;
835}
836
837inline ofMatrix4x4 ofMatrix4x4::newLookAtMatrix(const ofVec3f& eye, const ofVec3f& center, const ofVec3f& up) {
838 ofMatrix4x4 m;
839 m.makeLookAtMatrix(eye, center, up);
840 return m;
841}
842
843inline ofVec3f ofMatrix4x4::postMult( const ofVec3f& v ) const {
844 float d = 1.0f / (_mat[3][0] * v.x + _mat[3][1] * v.y + _mat[3][2] * v.z + _mat[3][3]) ;
845 return ofVec3f( (_mat[0][0]*v.x + _mat[0][1]*v.y + _mat[0][2]*v.z + _mat[0][3])*d,
846 (_mat[1][0]*v.x + _mat[1][1]*v.y + _mat[1][2]*v.z + _mat[1][3])*d,
847 (_mat[2][0]*v.x + _mat[2][1]*v.y + _mat[2][2]*v.z + _mat[2][3])*d) ;
848}
849
850inline ofVec3f ofMatrix4x4::preMult( const ofVec3f& v ) const {
851 float d = 1.0f / (_mat[0][3] * v.x + _mat[1][3] * v.y + _mat[2][3] * v.z + _mat[3][3]) ;
852 return ofVec3f( (_mat[0][0]*v.x + _mat[1][0]*v.y + _mat[2][0]*v.z + _mat[3][0])*d,
853 (_mat[0][1]*v.x + _mat[1][1]*v.y + _mat[2][1]*v.z + _mat[3][1])*d,
854 (_mat[0][2]*v.x + _mat[1][2]*v.y + _mat[2][2]*v.z + _mat[3][2])*d);
855}
856
859inline ofVec4f ofMatrix4x4::postMult( const ofVec4f& v ) const {
860 return ofVec4f( (_mat[0][0]*v.x + _mat[0][1]*v.y + _mat[0][2]*v.z + _mat[0][3]*v.w),
861 (_mat[1][0]*v.x + _mat[1][1]*v.y + _mat[1][2]*v.z + _mat[1][3]*v.w),
862 (_mat[2][0]*v.x + _mat[2][1]*v.y + _mat[2][2]*v.z + _mat[2][3]*v.w),
863 (_mat[3][0]*v.x + _mat[3][1]*v.y + _mat[3][2]*v.z + _mat[3][3]*v.w)) ;
864}
865
868inline ofVec4f ofMatrix4x4::preMult( const ofVec4f& v ) const {
869 return ofVec4f( (_mat[0][0]*v.x + _mat[1][0]*v.y + _mat[2][0]*v.z + _mat[3][0]*v.w),
870 (_mat[0][1]*v.x + _mat[1][1]*v.y + _mat[2][1]*v.z + _mat[3][1]*v.w),
871 (_mat[0][2]*v.x + _mat[1][2]*v.y + _mat[2][2]*v.z + _mat[3][2]*v.w),
872 (_mat[0][3]*v.x + _mat[1][3]*v.y + _mat[2][3]*v.z + _mat[3][3]*v.w));
873}
874
877inline ofVec3f ofMatrix4x4::transform3x3(const ofVec3f& v, const ofMatrix4x4& m) {
878 return ofVec3f( (m._mat[0][0]*v.x + m._mat[1][0]*v.y + m._mat[2][0]*v.z),
879 (m._mat[0][1]*v.x + m._mat[1][1]*v.y + m._mat[2][1]*v.z),
880 (m._mat[0][2]*v.x + m._mat[1][2]*v.y + m._mat[2][2]*v.z));
881}
882
885inline ofVec3f ofMatrix4x4::transform3x3(const ofMatrix4x4& m, const ofVec3f& v) {
886 return ofVec3f( (m._mat[0][0]*v.x + m._mat[0][1]*v.y + m._mat[0][2]*v.z),
887 (m._mat[1][0]*v.x + m._mat[1][1]*v.y + m._mat[1][2]*v.z),
888 (m._mat[2][0]*v.x + m._mat[2][1]*v.y + m._mat[2][2]*v.z) ) ;
889}
890
893inline void ofMatrix4x4::preMultTranslate( const ofVec3f& v ) {
894 for (unsigned i = 0; i < 3; ++i) {
895 float tmp = v.getPtr()[i];
896 if (tmp == 0)
897 continue;
898 _mat[3][0] += tmp * _mat[i][0];
899 _mat[3][1] += tmp * _mat[i][1];
900 _mat[3][2] += tmp * _mat[i][2];
901 _mat[3][3] += tmp * _mat[i][3];
902 }
903}
904
907inline void ofMatrix4x4::postMultTranslate( const ofVec3f& v ) {
908 for (std::size_t i = 0; i < 3; ++i) {
909 float tmp = v.getPtr()[i];
910 if (tmp == 0)
911 continue;
912 _mat[0][i] += tmp * _mat[0][3];
913 _mat[1][i] += tmp * _mat[1][3];
914 _mat[2][i] += tmp * _mat[2][3];
915 _mat[3][i] += tmp * _mat[3][3];
916 }
917}
918
919// AARON METHOD
921inline void ofMatrix4x4::postMultTranslate( float x, float y, float z) {
922 if (x != 0) {
923 _mat[0][0] += x * _mat[0][3];
924 _mat[1][0] += x * _mat[1][3];
925 _mat[2][0] += x * _mat[2][3];
926 _mat[3][0] += x * _mat[3][3];
927 }
928 if (y != 0) {
929 _mat[0][1] += y * _mat[0][3];
930 _mat[1][1] += y * _mat[1][3];
931 _mat[2][1] += y * _mat[2][3];
932 _mat[3][1] += y * _mat[3][3];
933 }
934 if (z != 0) {
935 _mat[0][2] += z * _mat[0][3];
936 _mat[1][2] += z * _mat[1][3];
937 _mat[2][2] += z * _mat[2][3];
938 _mat[3][2] += z * _mat[3][3];
939 }
940}
941
944inline void ofMatrix4x4::preMultScale( const ofVec3f& v ) {
945 _mat[0][0] *= v.getPtr()[0];
946 _mat[0][1] *= v.getPtr()[0];
947 _mat[0][2] *= v.getPtr()[0];
948 _mat[0][3] *= v.getPtr()[0];
949 _mat[1][0] *= v.getPtr()[1];
950 _mat[1][1] *= v.getPtr()[1];
951 _mat[1][2] *= v.getPtr()[1];
952 _mat[1][3] *= v.getPtr()[1];
953 _mat[2][0] *= v.getPtr()[2];
954 _mat[2][1] *= v.getPtr()[2];
955 _mat[2][2] *= v.getPtr()[2];
956 _mat[2][3] *= v.getPtr()[2];
957}
958
961inline void ofMatrix4x4::postMultScale( const ofVec3f& v ) {
962 _mat[0][0] *= v.getPtr()[0];
963 _mat[1][0] *= v.getPtr()[0];
964 _mat[2][0] *= v.getPtr()[0];
965 _mat[3][0] *= v.getPtr()[0];
966 _mat[0][1] *= v.getPtr()[1];
967 _mat[1][1] *= v.getPtr()[1];
968 _mat[2][1] *= v.getPtr()[1];
969 _mat[3][1] *= v.getPtr()[1];
970 _mat[0][2] *= v.getPtr()[2];
971 _mat[1][2] *= v.getPtr()[2];
972 _mat[2][2] *= v.getPtr()[2];
973 _mat[3][2] *= v.getPtr()[2];
974}
975
977inline void ofMatrix4x4::rotate(const ofQuaternion& q){
979}
980
982inline void ofMatrix4x4::rotate(float angle, float x, float y, float z){
983 postMultRotate(angle,x,y,z);
984}
985
987inline void ofMatrix4x4::rotateRad(float angle, float x, float y, float z){
988 postMultRotate(ofRadToDeg(angle),x,y,z);
989}
990
992inline void ofMatrix4x4::translate( float tx, float ty, float tz ){
994}
995
997inline void ofMatrix4x4::translate( const ofVec3f& v ){
999}
1000
1002inline void ofMatrix4x4::scale(float x, float y, float z){
1003 postMultScale(x,y,z);
1004}
1005
1007inline void ofMatrix4x4::scale( const ofVec3f& v ){
1008 postMultScale(v);
1009}
1010
1012inline void ofMatrix4x4::glRotate(float angle, float x, float y, float z){
1013 preMultRotate(ofQuaternion(angle,ofVec3f(x,y,z)));
1014}
1015
1016inline void ofMatrix4x4::glRotateRad(float angle, float x, float y, float z){
1018}
1019
1020inline void ofMatrix4x4::glRotate(const ofQuaternion& q){
1021 preMultRotate(q);
1022}
1023
1024inline void ofMatrix4x4::glTranslate( float tx, float ty, float tz ){
1026}
1027
1028inline void ofMatrix4x4::glTranslate( const ofVec3f& v ){
1030}
1031
1032inline void ofMatrix4x4::glScale(float x, float y, float z){
1033 preMultScale(ofVec3f(x,y,z));
1034}
1035
1036inline void ofMatrix4x4::glScale( const ofVec3f& v ){
1037 preMultScale(v);
1038}
1039
1040// AARON METHOD
1041inline void ofMatrix4x4::postMultScale( float x, float y, float z ) {
1042 _mat[0][0] *= x;
1043 _mat[1][0] *= x;
1044 _mat[2][0] *= x;
1045 _mat[3][0] *= x;
1046 _mat[0][1] *= y;
1047 _mat[1][1] *= y;
1048 _mat[2][1] *= y;
1049 _mat[3][1] *= y;
1050 _mat[0][2] *= z;
1051 _mat[1][2] *= z;
1052 _mat[2][2] *= z;
1053 _mat[3][2] *= z;
1054}
1055
1056
1057inline void ofMatrix4x4::preMultRotate( const ofQuaternion& q ) {
1058 if (q.zeroRotation())
1059 return;
1060 ofMatrix4x4 r;
1061 r.setRotate(q);
1062 preMult(r);
1063}
1064
1065inline void ofMatrix4x4::postMultRotate( const ofQuaternion& q ) {
1066 if (q.zeroRotation())
1067 return;
1068 ofMatrix4x4 r;
1069 r.setRotate(q);
1070 postMult(r);
1071}
1072
1073// AARON METHOD
1074inline void ofMatrix4x4::postMultRotate(float angle, float x, float y, float z) {
1075 ofMatrix4x4 r;
1076 r.makeRotationMatrix(angle, x, y, z);
1077 postMult(r);
1078}
1079
1081inline ofVec3f operator* (const ofVec3f& v, const ofMatrix4x4& m ) {
1082 return m.preMult(v);
1083}
1085inline ofVec4f operator* (const ofVec4f& v, const ofMatrix4x4& m ) {
1086 return m.preMult(v);
1087}
1088
1089
The ofMatrix4x4 is the big class of the math part of openFrameworks.
Definition ofMatrix4x4.h:58
ofMatrix4x4 operator*(const ofMatrix4x4 &m) const
creates a new matrix from the product of two matrices.
Definition ofMatrix4x4.h:547
bool isValid() const
Checks if the matrix is valid by ensuring its items are numbers.
Definition ofMatrix4x4.h:365
ofVec3f preMult(const ofVec3f &v) const
Vector * matrix multiplication.
void makeFromMultiplicationOf(const ofMatrix4x4 &, const ofMatrix4x4 &)
Matrix becomes the result of the multiplication of two other matrices.
Definition ofMatrix4x4.cpp:440
ofMatrix4x4 getInverse() const
Gets the inverse matrix.
Definition ofMatrix4x4.cpp:586
bool isIdentity() const
Checks if the matrix is the identity matrix.
void makeTranslationMatrix(const ofVec3f &)
Definition ofMatrix4x4.cpp:382
ofMatrix4x4(float const *const ptr)
Construct with a pointer.
Definition ofMatrix4x4.h:90
void operator*=(const ofMatrix4x4 &other)
The *= operation for matrices.
Definition ofMatrix4x4.h:539
void glRotateRad(float angle, float x, float y, float z)
void set(double const *const ptr)
void makeOrtho2DMatrix(double left, double right, double bottom, double top)
Matrix becomes a 2D orthographic projection matrix.
void glTranslate(const ofVec3f &v)
ofVec3f getScale() const
void translate(const ofVec3f &v)
Translates along the vector.
void makeScaleMatrix(const ofVec3f &)
Definition ofMatrix4x4.cpp:369
void set(float const *const ptr)
void set(const ofMatrix4x4 &rhs)
Set the data of the matrix.
ofMatrix4x4 & operator=(const ofMatrix4x4 &rhs)
Copy a matrix using = operator.
static ofMatrix4x4 newPerspectiveMatrix(double fovy, double aspectRatio, double zNear, double zFar)
bool getPerspective(double &fovy, double &aspectRatio, double &zNear, double &zFar) const
Get the frustum settings of a symmetric perspective projection matrix.
Definition ofMatrix4x4.cpp:902
const float * getPtr() const
Definition ofMatrix4x4.h:354
ofVec4f _mat[4]
The values of the matrix, stored in row-major order.
Definition ofMatrix4x4.h:63
static ofMatrix4x4 newOrtho2DMatrix(double left, double right, double bottom, double top)
void scale(const ofVec3f &v)
Scales each axis by the corresponding x, y, z of the vector.
void makeRotationMatrix(const ofVec3f &from, const ofVec3f &to)
Definition ofMatrix4x4.cpp:394
void rotateRad(float angle, float x, float y, float z)
Rotates by angle (radians) around the given x, y, z axis.
void postMultTranslate(const ofVec3f &v)
Equivalent to postMult(newTranslationMatrix(v)).
ofMatrix4x4(const ofQuaternion &quat)
Rotation matrices can be constructed from a quaternion.
Definition ofMatrix4x4.h:95
void decompose(ofVec3f &translation, ofQuaternion &rotation, ofVec3f &scale, ofQuaternion &so) const
Decompose the matrix into translation, rotation, scale and scale orientation.
Definition ofMatrix4x4.cpp:1500
static ofMatrix4x4 getInverseOf(const ofMatrix4x4 &matrix)
Create new matrices as transformation of another.
static ofMatrix4x4 newOrthoMatrix(double left, double right, double bottom, double top, double zNear, double zFar)
bool makeInvertOf(const ofMatrix4x4 &rhs)
Matrix becomes the inverse of the provided matrix.
Definition ofMatrix4x4.cpp:581
void makeLookAtViewMatrix(const ofVec3f &eye, const ofVec3f &center, const ofVec3f &up)
Matrix becomes a combination of an inverse translation and rotation.
Definition ofMatrix4x4.cpp:918
void postMultRotate(const ofQuaternion &q)
Equivalent to postMult(newRotationMatrix(q)).
void glScale(const ofVec3f &v)
void getLookAt(ofVec3f &eye, ofVec3f &center, ofVec3f &up, float lookDistance=1.0f) const
Gets the lookAt determiners of the matrix.
Definition ofMatrix4x4.cpp:942
bool getFrustum(double &left, double &right, double &bottom, double &top, double &zNear, double &zFar) const
Gets the perspective components for a frustum projection matrix.
Definition ofMatrix4x4.cpp:870
ofVec3f getTranslation() const
static ofMatrix4x4 newTranslationMatrix(float x, float y, float z)
static ofMatrix4x4 newTranslationMatrix(const ofVec3f &dv)
static ofMatrix4x4 newRotationMatrix(float angle, const ofVec3f &axis)
ofVec3f postMult(const ofVec3f &v) const
Matrix * vector multiplication.
void makeLookAtMatrix(const ofVec3f &eye, const ofVec3f &center, const ofVec3f &up)
Matrix becomes a combination of translation and rotation.
Definition ofMatrix4x4.cpp:930
void makePerspectiveMatrix(double fovy, double aspectRatio, double zNear, double zFar)
Matrix becomes a perspective projection matrix.
Definition ofMatrix4x4.cpp:890
ofVec4f postMult(const ofVec4f &v) const
void translate(float tx, float ty, float tz)
Translates by tx, ty, tz.
static ofMatrix4x4 newRotationMatrix(float angle, float x, float y, float z)
void makeIdentityMatrix()
Matrix becomes the identity matrix.
Definition ofMatrix4x4.cpp:361
static ofMatrix4x4 newRotationMatrix(const ofVec3f &from, const ofVec3f &to)
static ofMatrix4x4 getOrthoNormalOf(const ofMatrix4x4 &matrix)
Makes a new matrix which is the given matrix, normalized.
void makeFrustumMatrix(double left, double right, double bottom, double top, double zNear, double zFar)
Matrix becomes a perspective projection matrix.
Definition ofMatrix4x4.cpp:855
void preMultScale(const ofVec3f &v)
Equivalent to preMult(newScaleMatrix(v)).
void postMultTranslate(float x, float y, float z)
ofVec3f getRowAsVec3f(std::size_t i) const
returns a copy of row i
Definition ofMatrix4x4.h:335
void rotate(const ofQuaternion &q)
Rotates based on the quarternion.
static ofMatrix4x4 newScaleMatrix(float sx, float sy, float sz)
static ofMatrix4x4 newLookAtMatrix(const ofVec3f &eye, const ofVec3f &center, const ofVec3f &up)
float & operator()(std::size_t row, std::size_t col)
Write data with matrix(row,col)=number
Definition ofMatrix4x4.h:325
void setTranslation(float tx, float ty, float tz)
Definition ofMatrix4x4.cpp:346
static ofMatrix4x4 newIdentityMatrix(void)
static ofMatrix4x4 getTransposedOf(const ofMatrix4x4 &matrix)
Makes a new matrix which is the transpose of the given matrix.
void makeOrthoNormalOf(const ofMatrix4x4 &rhs)
Matrix becomes an orthonormalized version of the provided matrix.
Definition ofMatrix4x4.cpp:515
static ofMatrix4x4 newScaleMatrix(const ofVec3f &sv)
static ofVec3f transform3x3(const ofMatrix4x4 &m, const ofVec3f &v)
Apply a 3x3 transform (no translation) of M * v.
void preMultTranslate(const ofVec3f &v)
Equivalent to preMult(newTranslationMatrix(v)).
void postMultScale(const ofVec3f &v)
Equivalent to postMult(scale(v)).
void scale(float x, float y, float z)
Scales each axis by the corresponding x, y, z.
bool isNaN() const
Checks if the matrix contains items that are not numbers.
void makeOrthoMatrix(double left, double right, double bottom, double top, double zNear, double zFar)
Matrix becomes an orthographic projection matrix.
Definition ofMatrix4x4.cpp:822
void postMultScale(float x, float y, float z)
static ofMatrix4x4 newRotationMatrix(const ofQuaternion &quat)
void setRotate(const ofQuaternion &q)
Definition ofMatrix4x4.cpp:61
void glRotate(float angle, float x, float y, float z)
ofMatrix4x4(const glm::mat4 &mat)
Definition ofMatrix4x4.h:76
ofQuaternion getRotate() const
Definition ofMatrix4x4.cpp:268
void postMultRotate(float angle, float x, float y, float z)
void glScale(float x, float y, float z)
static ofMatrix4x4 newFrustumMatrix(double left, double right, double bottom, double top, double zNear, double zFar)
static ofMatrix4x4 newRotationMatrix(float angle1, const ofVec3f &axis1, float angle2, const ofVec3f &axis2, float angle3, const ofVec3f &axis3)
ofMatrix4x4()
The default constructor provides an identity matrix.
Definition ofMatrix4x4.h:72
void preMultRotate(const ofQuaternion &q)
Equivalent to preMult(newRotationMatrix(q)).
static ofVec3f transform3x3(const ofVec3f &v, const ofMatrix4x4 &m)
Apply a 3x3 transform (no translation) of v * M.
ofVec4f getRowAsVec4f(std::size_t i) const
returns a copy of row i
Definition ofMatrix4x4.h:340
float * getPtr()
Access the internal data in float* format useful for opengl matrix transformations.
Definition ofMatrix4x4.h:351
ofVec4f preMult(const ofVec4f &v) const
void glRotate(const ofQuaternion &q)
~ofMatrix4x4()
destructor.
Definition ofMatrix4x4.h:110
void rotate(float angle, float x, float y, float z)
Rotates by angle (degrees) around the given x, y, z axis.
float operator()(std::size_t row, std::size_t col) const
Read data with matrix(row, col)
Definition ofMatrix4x4.h:330
bool getOrtho(double &left, double &right, double &bottom, double &top, double &zNear, double &zFar) const
Get the perspective components from a matrix.
Definition ofMatrix4x4.cpp:836
void glTranslate(float tx, float ty, float tz)
Definition ofQuaternion.h:27
bool zeroRotation() const
return true if the Quat represents a zero rotation, and therefore can be ignored in computations.
Definition ofQuaternion.h:343
ofVec3f is a class for storing a three dimensional vector.
Definition ofVec3f.h:79
float x
Stores the X component of this vector.
Definition ofVec3f.h:86
float y
Stores the Y component of this vector.
Definition ofVec3f.h:89
float z
Stores the Z component of this vector.
Definition ofVec3f.h:92
float * getPtr()
Returns a pointer to the memory position of the first element of the vector ('x'); the other elements...
Definition ofVec3f.h:156
Definition ofVec4f.h:11
float w
Definition ofVec4f.h:20
float x
Definition ofVec4f.h:17
float z
Definition ofVec4f.h:19
float y
Definition ofVec4f.h:18
float ofRadToDeg(float radians)
Convert radians to degrees.
Definition ofMath.cpp:137
std::ostream & operator<<(std::ostream &os, const ofMatrix3x3 &M)
Definition ofMatrix3x3.cpp:304
std::istream & operator>>(std::istream &is, ofMatrix3x3 &M)
Definition ofMatrix3x3.cpp:323
#define d
#define tx
#define ty
#define tz
glm::vec3 operator*(const glm::vec3 &v1, const ofVec3f &v2)
Definition ofVectorMath.h:302