summaryrefslogtreecommitdiffstats
path: root/Matrix.h
blob: f8c178fb86a1ea04c336b0e3e1caee5a41410383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef _MATRIX_H_
#define _MATRIX_H_

#include "gl.h"
#include "Vector.h"
#include "Vertex.h"

class Matrix
{
  public:
    Matrix(GLenum pname = GL_MODELVIEW_MATRIX) {
      store(pname);
    }
    
    Matrix(float in[16]) {
      for(int i = 0; i < 16; ++i)
        f[i] = in[i];
    }
    
    Vertex operator*(const Vertex &v) const {
      Vector r(v.getX()*m[0][0] + v.getY()*m[1][0] + v.getZ()*m[2][0] + m[3][0],
	             v.getX()*m[0][1] + v.getY()*m[1][1] + v.getZ()*m[2][1] + m[3][1],
	             v.getX()*m[0][2] + v.getY()*m[1][2] + v.getZ()*m[2][2] + m[3][2]);
	    float w = v.getX()*m[0][3] + v.getY()*m[1][3] + v.getZ()*m[2][3] + m[3][3];
	
	    return r/w;
    }
    
    void load() {
      glLoadMatrixf(f);
    }
    
    void store(GLenum pname = GL_MODELVIEW_MATRIX) {
      glGetFloatv(pname, f);
    }
    
  private:
    union {
      float f[16];
      float m[4][4];
    };
};

#endif /*_MATRIX_H_*/