-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.cpp
More file actions
153 lines (118 loc) · 3.77 KB
/
Copy pathApplication.cpp
File metadata and controls
153 lines (118 loc) · 3.77 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <memory>
#include "./src/platform/win32.h"
#include "./src/include/SkyBox.h"
#include "./src/include/camera.h"
#include "./src/include/params.h"
#include "./src/include/pipeline.h"
using namespace std;
void clear_zbuffer(int width, int height, float* zbuffer);
void clear_framebuffer(int width, int height, unsigned char* framebuffer);
Model* createSmileface(const char* filepath);
int num_frames = 0;
int main(){
int width = WINDOW_WIDTH, height = WINDOW_HEIGHT;
float* zbuffer = (float*)malloc(sizeof(float) * width * height);
unsigned char* framebuffer = (unsigned char*)malloc(sizeof(unsigned char) * 4 * width * height);
shared_ptr<Camera> camera = make_shared<Camera>(Eye, Target, Up, Aspect, FOV, Nearplane, Farplane);
// initialize window
window_init(width, height, "softRenderer");
// render loop
// -----------
float print_time = platform_get_time();
std::vector<shared_ptr<Model>> models;
models.push_back(make_shared<Model>("../src/resource/backpack/backpack.obj"));
//models.push_back(shared_ptr<Model>(createSmileface("../src/resource/awesomeface.jpg")));
shared_ptr<IShader> shader = make_shared<PhongShader>();
shader->payload.camera = camera;
CubeMap* cubemap = new CubeMap("../src/resource/skybox/");
shared_ptr<SkyBox> skybox = make_shared<SkyBox>(cubemap);
while (!window->is_close)
{
//clear buffer
clear_zbuffer(width, height, zbuffer);
clear_framebuffer(width, height, framebuffer);
//handle events
camera->handle_event();
//update matrix
shader->payload.camera_perp_matrix = shader->payload.camera->get_Perspective();
shader->payload.mvp_matrix = shader->payload.camera->get_VPMatrix();
shader->payload.model_matrix = mat4::identity();
shader->payload.model_matrix_inv_trans = mat4::identity();
//draw
for (int i = 0; i < models.size(); ++i) {
shader->payload.model = models[i];
for (int j = 0; j < models[i]->nfaces(); j++)
{
draw_triangles(framebuffer, zbuffer, shader.get(), j);
}
}
draw_skybox(framebuffer, zbuffer, skybox.get(), camera.get());
//calculate frames and time
++num_frames;
float cur_time = platform_get_time();
if (cur_time - print_time >= 1) {
int sum_millis = static_cast<int>((cur_time - print_time) * 1000);
int avg_millis = sum_millis / num_frames;
printf("fps: %3d, avg: %3d ms\n", num_frames, avg_millis);
num_frames = 0;
print_time = cur_time;
}
//reset mouse info
window->mouse_info.wheel_delta = 0;
window->mouse_info.orbit_delta = vec2(0, 0);
window->mouse_info.fv_delta = vec2(0, 0);
//send framebuffer to window
window_draw(framebuffer);
msg_dispatch();
}
//free mem
free(zbuffer);
free(framebuffer);
window_destroy();
system("pause");
return 0;
}
void clear_zbuffer(int width, int height, float* zbuffer) {
for (int i = 0; i < width * height; ++i) {
#ifdef ViewZ
zbuffer[i] = Farplane;
#else
zbuffer[i] = 1.0;
#endif
}
}
void clear_framebuffer(int width, int height, unsigned char* framebuffer) {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
int ind = (i * width + j) * 4;
framebuffer[ind] = 56; //r
framebuffer[ind + 1] = 56; //g
framebuffer[ind + 2] = 80; //b
}
}
}
Model* createSmileface(const char * filepath) {
std::vector<vec3> myverts = {
{0.5f, 0.5f, 0.0f}, // top right
{ 0.5f, -0.5f, 0.0f}, // bottom right
{-0.5f, -0.5f, 0.0f}, // bottom left
{-0.5f, 0.5f, 0.0f} // top left
};
std::vector<vec2> myuv = {
{1,1},
{1,0},
{0,0},
{0,1}
};
std::vector<vec3> mynormal = {
{1,0,0}
};
std::vector<std::vector<int>> myface = {
{0,0,0, 3,3,0, 1,1,0}, // first triangle
{1,1,0, 3,3,0, 2,2,0} // second triangle
};
Model* model = new Model(myverts, myuv, mynormal, myface);
model->diffusemap = new Texture(filepath);
return model;
}