-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hello_world.cpp
192 lines (165 loc) · 5.1 KB
/
test_hello_world.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdio.h>
#include "sgl.h"
using namespace sgl;
int w = 800, h = 600;
SDL_Window* pWindow;
SDL_Surface* pWindowSurface;
bool keystate[SDL_NUM_SCANCODES];
Pipeline pipeline;
Uniforms uniforms;
VertexBuffer_t vertices;
IndexBuffer_t indices;
Texture color_texture, depth_texture;
Texture image_texture;
void
init_env(int argc, char* argv[]) {
SDL_SetMainReady();
/* Initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0)
exit(1);
/* Create window */
pWindow = SDL_CreateWindow("SGL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_SHOWN);
if (pWindow == NULL)
exit(1);
SDL_ShowWindow(pWindow);
pWindowSurface = SDL_GetWindowSurface(pWindow);
const char* SDL2_surface_native_format = SDL_GetPixelFormatName(pWindowSurface->format->format);
SDL_UpdateWindowSurface(pWindow);
for (uint32_t i_key = 0; i_key < SDL_NUM_SCANCODES; i_key++)
keystate[i_key] = false;
/* set current working directory */
set_cwd(gd(argv[0]));
}
void
destroy_env() {
SDL_DestroyWindow(pWindow);
SDL_Quit();
}
void
process_key(SDL_KeyboardEvent *key) {
bool is_press = (key->type == SDL_KEYDOWN);
/* scancode is based on QWERTY layout,
* while keycode generated from the same key position
* can be different from different keyboard layouts. */
uint32_t scancode = key->keysym.scancode;
uint32_t keycode = key->keysym.sym;
std::string keyname = SDL_GetKeyName(keycode);
/* record key state */
keystate[scancode] = is_press ? true : false;
}
Mat4x4
compute_projection_matrix(double w, double h, double near, double far, double field_of_view) {
double aspect_ratio = double(w) / double(h);
double inv_aspect = double(1.0) / aspect_ratio;
double left = -tan(field_of_view / double(2.0)) * near;
double right = -left;
double top = inv_aspect * right;
double bottom = -top;
return
Mat4x4(2 * near / (right - left), 0, (right + left) / (right - left), 0,
0, 2 * near / (top - bottom), (top + bottom) / (top - bottom), 0,
0, 0, -(far + near) / (far - near),
-2 * far * near / (far - near), 0, 0, -1.0, 0);
}
void
init_render() {
/* Step 1: Setup resources. */
color_texture.create(w, h,
PixelFormat::pixel_format_BGRA8888,
TextureSampling::texture_sampling_point);
depth_texture.create(w, h,
PixelFormat::pixel_format_float64,
TextureSampling::texture_sampling_point);
/* rotate model along x axis by -55 degrees */
Mat4x4 model(quat_to_mat3x3(Quat::rot_x(degrees_to_radians(-55.0))));
/* translate model along z axis by -3 units */
Mat4x4 view(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, -3,
0, 0, 0, 1
);
/* near = 0.1, far = 10.0, field of view = 45 degrees */
Mat4x4 projection = compute_projection_matrix(w, h, 0.1, 10.0, degrees_to_radians(45));
/* initialize resources and render pipeline */
image_texture = sgl::load_texture("textures/checker_256.png", PixelFormat::pixel_format_BGRA8888);
uniforms.model = model;
uniforms.view = view;
uniforms.projection = projection;
uniforms.in_textures[0] = &image_texture;
pipeline.set_render_targets(&color_texture, &depth_texture);
pipeline.clear_render_targets(&color_texture, &depth_texture, Vec4(0.5, 0.5, 0.5, 1.0));
pipeline.set_shaders(default_VS, default_FS);
pipeline.disable_backface_culling();
Vertex v;
/*
v.p: position (x,y,z)
v.t: texture coords (u,v)
*/
v.p = Vec3(0.5, 0.5, 0.0);
v.t = Vec2(1.0, 1.0);
vertices.push_back(v);
v.p = Vec3(0.5, -0.5, 0.0);
v.t = Vec2(1.0, 0.0);
vertices.push_back(v);
v.p = Vec3(-0.5, -0.5, 0.0);
v.t = Vec2(0.0, 0.0);
vertices.push_back(v);
v.p = Vec3(-0.5, 0.5, 0.0);
v.t = Vec2(0.0, 1.0);
vertices.push_back(v);
/* triangle 1 */
indices.push_back(0);
indices.push_back(1);
indices.push_back(3);
/* triangle 2 */
indices.push_back(1);
indices.push_back(2);
indices.push_back(3);
}
void
render_frame() {
pipeline.draw(vertices, indices, uniforms);
/* save first frame to disk */
static bool saved = false;
if (!saved) {
color_texture.save_png("test_hello_world.png");
saved = true;
}
}
int
main(int argc, char* argv[]) {
/* initialization */
init_env(argc, argv);
init_render();
/* Start main loop */
SDL_Event e;
Timer global_timer, frame_timer;
double T_global = 0.0, T_frame = 0.0;
int frameid = 0;
while (true) {
/* window message handling */
SDL_PollEvent(&e);
if (e.type == SDL_QUIT || keystate[SDL_SCANCODE_ESCAPE])
break;
else if (e.type == SDL_KEYDOWN || e.type == SDL_KEYUP)
process_key(&e.key);
/* timing */
frameid++;
frame_timer.tick();
T_global += global_timer.tick();
/* render the whole frame */
render_frame();
/* logging */
double frame_time = frame_timer.tick();
T_frame += frame_time;
sgl::SDL2::sgl_texture_to_SDL2_surface(&color_texture, pWindowSurface);
SDL_UpdateWindowSurface(pWindow);
char buf[64];
sprintf(buf, "%.2lfms, T=%.2lfs", T_frame / frameid * 1000.0, T_global);
std::string title = std::string("SGL | ") + buf + " | FPS=" + std::to_string(int(1.0 / frame_time));
SDL_SetWindowTitle(pWindow, title.c_str());
}
destroy_env();
return 0;
}