-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
102 lines (81 loc) · 2.57 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
#include "canvas.h"
#include "camera.h"
#include "maths.h"
#include "mesh.h"
#include "light.h"
#include "utils.h"
#include <thread>
//Global Setup
Canvas* canvas;
Camera* camera;
Mesh* mesh;
Light* light;
float deltaTime;
maths::mat4f view;
maths::mat4f projection;
maths::mat4f viewport;
maths::mat4f world_to_screennorm;
maths::mat4f screennorm_to_device;
bool isTransform=false;
bool isFirstRender=true;
void processArrowKeys(int key, int x, int y){
light->processKeyboard(key,deltaTime);
isTransform = true;
}
void processKeys(unsigned char key, int x, int y){
camera->processKeyboard(key,deltaTime);
mesh->processKeyboard(key,deltaTime);
light->processKeyboard(key, deltaTime);
projection = maths::perspective(maths::radians(camera->zoom), (float)canvas->scrWidth/canvas->scrHeight);
view = camera->getViewMatrix();
world_to_screennorm = maths::mul(projection,view);
screennorm_to_device = maths::mul(viewport,world_to_screennorm);
mesh->setTransform(screennorm_to_device);
isTransform = true;
}
void renderer(){
//Calculate deltatime and framePerSecond
static float lastFrame = 0;
float currentFrame = glutGet(GLUT_ELAPSED_TIME);
deltaTime = (currentFrame - lastFrame)/1000;
lastFrame = currentFrame;
std::cout << 1/deltaTime <<"\n";
int size = mesh->triangles.size();
if (isFirstRender){
view = camera->getViewMatrix();
projection=maths::perspective(maths::radians(camera->zoom), (float)canvas->scrWidth/canvas->scrHeight);
viewport = maths::mul(maths::scale(0.5*canvas->scrWidth,0.5*canvas->scrHeight,1.0),maths::translate(1.0,1.0,0.0));
world_to_screennorm = maths::mul(projection,view);
screennorm_to_device = maths::mul(viewport,world_to_screennorm);
mesh->setTransform(screennorm_to_device);
mesh->render(0,size);
canvas->update();
canvas->display();
isFirstRender = false;
}
if (isTransform){
canvas->cleargrid();
mesh->render(0,size);
canvas->update();
canvas->display();
}
}
int main(int argc, char** argv){
//Canvas setup
canvas = new Canvas(argc, argv);
//Camera setup
camera = new Camera();
//Setup Lights
light = new Light();
//Creating mesh
mesh=new Mesh(canvas);
mesh->light = light;
mesh->camera = camera;
mesh->parse("../res/lowpoly.obj");
// mesh->parse("../res/sphere.obj");
//Glut specific functions
glutKeyboardFunc(processKeys);
glutSpecialFunc(processArrowKeys);
glutDisplayFunc(renderer);
glutMainLoop();
}