-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerspective.cpp
91 lines (76 loc) · 2.26 KB
/
Perspective.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <cmath>
#include <iostream>
#include <GL/gl.h>
#include "3D.h"
#include "Perspective.hh"
#include "Util.hh"
Perspective::Perspective(){}
Perspective::~Perspective(){}
std::string
Perspective::perspective(std::vector<std::string> paramList){
if(paramList.size()<=4){
if(paramList.size() <4){
return "too few parameters";
}
float fov = 0;
float a = 0;
float n = 0;
float f = 0;
try{
fov = std::stod(paramList[0]);
a = std::stod(paramList[1]);
n = std::stod(paramList[2]);
f = std::stod(paramList[3]);
if(fov>=180 || fov <=0){
return "fov is should be >0 and <180";
}
}
catch(...){
return "illegal params";
}
perspflag = 1;
/*
matrix_unit new_perspect= {
{ {-n*(t-b)/(r-l), 0. , 0 , 0 },
{0. , -n , 0. , 0 },
{0. , 0. , (f+n)/(f-n) , -2*n*f/(f-n) },
{0. , 0. , -1.0 , 0 } },
};
*/
float t = tan(fov*M_PI/(2.0*180))*n;
float b = -t;
float r = t * a;
float l = -t * a;
Near = -n;
Far = -f;
matrix_unit new_perspect= {
{ {2.0*n/(r-l) , 0. , (l+r)/(l-r) , 0 },
{0. , 2.0*n/(t-b) , 0. , 0 },
{0. , 0. , (f+n)/(n-f) , 2.0*f*n/(f-n)},
{0. , 0. , 1.0 , 0 } },
};
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT,viewport);
width = abs(viewport[2]-viewport[0]);
height = abs(viewport[3]-viewport[1]);
Util::debug_head("Perspective.cpp");
std::cout<<"width:"<<width<<", height:"<<height<<std::endl;
Util::debug_tail();
matrix_unit viewport_mat = {
{ {width/2.0, 0. , 0. , (width-1) /2.0},
{0. , height/2.0, 0. , (height-1)/2.0},
{0. , 0. , 1 , 0 },
{0. , 0. , 0. , 1 } },
};
Mult_mat(&viewport_mat, &new_perspect, &perspect);
//Copy_mat(&new_perspect, &perspect);
Util::debug_head("Perspective.cpp");
std::cout<<"perspect:"<<std::endl;
Util::print_mat(&perspect);
Util::debug_tail();
return "Perspective Done";
}
else{
return "too many parameters";
}
}