soc-2008-mxcurioni: first version of lib3ds code. It does NOT work yet and has to...
[blender.git] / source / blender / freestyle / intern / scene_graph / NodeCamera.cpp
1 #include "NodeCamera.h"
2 #include <math.h>
3
4 void loadIdentity(double * matrix){
5     int i;
6
7     // Build Identity matrix
8     for(i=0;i<16;++i){
9         double value ;
10         if((i % 5) == 0)
11             value = 1.0;
12         else
13             value = 0;
14         matrix[i] = value;
15     }
16 }
17
18 NodeCamera::NodeCamera(CameraType camera_type)
19 :camera_type_(camera_type){
20     loadIdentity(modelview_matrix_);
21     loadIdentity(projection_matrix_);
22 }
23
24 NodeCamera::NodeCamera(const NodeCamera& iBrother)
25 :camera_type_(iBrother.camera_type_){
26     memcpy(modelview_matrix_, iBrother.modelview_matrix_, 16*sizeof(double));
27     memcpy(projection_matrix_, iBrother.projection_matrix_, 16*sizeof(double));
28 }
29
30 void NodeCamera::accept(SceneVisitor& v){
31     v.visitNodeCamera(*this)   ;
32 }
33
34 void NodeCamera::setModelViewMatrix(double modelview_matrix[16]){
35     memcpy(modelview_matrix_, modelview_matrix,16*sizeof(double));
36 }
37
38 void NodeCamera::setProjectionMatrix(double projection_matrix[16]){
39     memcpy(projection_matrix_, projection_matrix,16*sizeof(double));
40 }
41
42 NodeOrthographicCamera::NodeOrthographicCamera()
43 :NodeCamera(NodeCamera::ORTHOGRAPHIC),
44 left_(0),right_(0),bottom_(0),top_(0),zNear_(0),zFar_(0){
45     loadIdentity(projection_matrix_);
46     loadIdentity(modelview_matrix_);
47 }
48
49 NodeOrthographicCamera::NodeOrthographicCamera(double left
50                                                 , double right
51                                                 , double bottom
52                                                 , double top
53                                                 , double zNear
54                                                 , double zFar
55                                                 )
56 :NodeCamera(NodeCamera::ORTHOGRAPHIC),
57 left_(left),
58 right_(right),
59 bottom_(bottom),
60 top_(top),
61 zNear_(zNear),
62 zFar_(zFar){
63
64     loadIdentity(projection_matrix_);
65
66     projection_matrix_[0] = 2.0/(right-left);
67     projection_matrix_[3] = -(right + left) / (right - left) ;
68     projection_matrix_[5] = 2.0/(top-bottom);
69     projection_matrix_[7] = -(top + bottom) / (top - bottom) ;
70     projection_matrix_[10] = -2.0/(zFar-zNear);
71     projection_matrix_[11] = -(zFar + zNear) / (zFar - zNear);
72
73 }
74
75 NodeOrthographicCamera::NodeOrthographicCamera(const NodeOrthographicCamera& iBrother)
76 :NodeCamera(iBrother),left_(iBrother.left_),right_(iBrother.right_),bottom_(iBrother.bottom_),top_(iBrother.top_),zNear_(iBrother.zNear_),zFar_(iBrother.zFar_){
77 }
78
79 NodePerspectiveCamera::NodePerspectiveCamera()
80 :NodeCamera(NodeCamera::PERSPECTIVE){
81 }
82
83 NodePerspectiveCamera::NodePerspectiveCamera(double fovy
84                                         , double aspect
85                                         , double zNear
86                                         , double zFar)
87 :NodeCamera(NodeCamera::PERSPECTIVE){
88     loadIdentity(projection_matrix_);
89
90     double f = cos(fovy/2.0)/sin(fovy/2.0); // cotangent
91
92     projection_matrix_[0] = f/aspect;
93     projection_matrix_[5] = f;
94     projection_matrix_[10] = (zNear+zFar)/(zNear-zFar);
95     projection_matrix_[11] = (2.0*zNear*zFar)/(zNear-zFar);
96     projection_matrix_[14] = -1.0;
97     projection_matrix_[15] = 0;
98
99 }
100
101 NodePerspectiveCamera::NodePerspectiveCamera(double left,
102                                         double right,
103                                         double bottom,
104                                         double top,
105                                         double zNear,
106                                         double zFar)
107 :NodeCamera(NodeCamera::PERSPECTIVE){
108     loadIdentity(projection_matrix_);
109
110     projection_matrix_[0] = (2.0*zNear)/(right-left);
111     projection_matrix_[2] = (right+left)/(right-left);
112     projection_matrix_[5] = (2.0*zNear)/(top-bottom);
113     projection_matrix_[6] = (top+bottom)/(top-bottom);
114     projection_matrix_[10] = - (zFar+zNear)/(zFar-zNear);
115     projection_matrix_[11] = - (2.0*zFar*zNear)/(zFar-zNear);
116     projection_matrix_[14] = -1.0;
117     projection_matrix_[15] = 0;
118 }
119