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