-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.hpp
325 lines (256 loc) · 11.1 KB
/
render.hpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "globals.hpp"
#pragma once
class Gene;
std::vector<std::vector<Gene>> ecosystem;
void drawTile(uint32_t x, uint16_t y, uint32_t color, GLubyte *pixels){
int index = x * 3 + (y * 3 * stg.map_width);
pixels[index++] = ((uint8_t*)&color)[0];
pixels[index++] = ((uint8_t*)&color)[1];
pixels[index++] = ((uint8_t*)&color)[2];
}
// GLfloat vertices[12] = {-1.0f, 1.0f,
// -1.0f, -1.0f,
// 1.0f, -1.0f,
// -1.0f, -1.0f,
// 1.0f, 1.0f,
// 1.0f, -1.0f};
GLfloat vertices[12] = {-1.0f, 1.0f,
-1.0f, -1.0f,
1.0f, 1.0f,
1.0f, 1.0f,
1.0f, -1.0f,
-1.0f, -1.0f};
GLfloat quadVertices[] = {
// positions // colors
-0.05f, 0.05f, 1.0f, 0.0f, 0.0f,
0.05f, -0.05f, 0.0f, 1.0f, 0.0f,
-0.05f, -0.05f, 0.0f, 0.0f, 1.0f,
-0.05f, 0.05f, 1.0f, 0.0f, 0.0f,
0.05f, -0.05f, 0.0f, 1.0f, 0.0f,
0.05f, 0.05f, 0.0f, 1.0f, 1.0f
};
// Shader sources
static std::string read_shader_file
(
const std::__fs::filesystem::path::value_type *shader_file)
{
std::ifstream ifs;
auto ex = ifs.exceptions();
ex |= std::ios_base::badbit | std::ios_base::failbit;
ifs.exceptions(ex);
ifs.open(shader_file);
ifs.ignore(std::numeric_limits<std::streamsize>::max());
auto size = ifs.gcount();
// if (size > 0x10000) // 64KiB sanity check:
// return false;
ifs.clear();
ifs.seekg(0, std::ios_base::beg);
return std::string {std::istreambuf_iterator<char> {ifs}, {}};
}
// an example of something we will control from the javascript side
bool background_is_black = true;
void handleEvents(v2d *cursor, float *zoomPhysics, v2d *panPhysics, float scaleClipStart, float scaleClipEnd){
SDL_Event event;
while( SDL_PollEvent( &event ) ){
// std::cout << event.type << '\n';
int x,y;
switch (event.type){
case SDL_MOUSEWHEEL:
// stg.scale += event.wheel.y > 0 ? (float)event.wheel.y / 2000.0f : (float)event.wheel.y / 2000.0f;
// zoomPhysics[0] += zoomPhysics[2] > scaleClipEnd || zoomPhysics[2] < scaleClipStart ? copysign(1.0, event.wheel.y) * 0.001 * -1.0 : ((float)event.wheel.y / 16000.0f);
// if(zoomPhysics[2] > scaleClipEnd || zoomPhysics[2] < scaleClipStart){
// zoomPhysics[0] = 0;
// zoomPhysics[1] = 0;
// zoomPhysics[2] += copysign(1.0, event.wheel.y) * 10;
// }
// else
if(firefox)
zoomPhysics[0] += ((float)event.wheel.y / 1000.0f);
else
zoomPhysics[0] += ((float)event.wheel.y / 20000.0f);
SDL_GetMouseState(&x, &y);
cursor->set(x,y);
break;
case SDL_MULTIGESTURE:
zoomPhysics[0] += ((float)event.mgesture.dDist / 4);
SDL_GetMouseState(&x, &y);
cursor->set(x,y);
break;
case SDL_MOUSEBUTTONDOWN:
isMouseDown = true;
SDL_GetRelativeMouseState(nullptr, nullptr);
break;
case SDL_MOUSEBUTTONUP:
isMouseDown = false;
break;
}
}
if(isMouseDown){
int x,y;
SDL_GetRelativeMouseState(&x, &y);
panPhysics[2] += v2d(-x,y);
// panPhysics[1] += v2d(-event.motion.xrel, event.motion.yrel)/100000;
}
}
float* processPhysics(float *physics, float drag, float clipStart, float clipEnd){
float temp = physics[2];
physics[1] += physics[0];
physics[0] /= drag;
// Clip Position
physics[2] = physics[2] > clipEnd ? clipEnd : physics[2];
// pos += vel
float adjVel = physics[1] * physics[2];
physics[2] += adjVel;
physics[1] /= drag;
if(physics[2] < clipStart){
physics[0] = 0;
physics[1] = 0;
physics[2] = clipStart;
}
return physics;
}
v2d* processPhysics(v2d *physics, v2d drag, v2d clipStart, v2d clipEnd){
v2d temp = physics[2];
physics[1] += physics[0];
physics[0] /= drag;
// Clip Position
physics[2].x = physics[2].x > clipEnd.x ? clipEnd.x : physics[2].x;
physics[2].y = physics[2].y > clipEnd.y ? clipEnd.y : physics[2].y;
// pos += vel
v2d adjVel = physics[1] * physics[2];
physics[2] += adjVel;
physics[1] /= drag;
if(physics[2].x < clipStart.x){
physics[0].x = 0;
physics[1].x = 0;
physics[2].x = clipStart.x;
}
if(physics[2].y < clipStart.y){
physics[0].y = 0;
physics[1].y = 0;
physics[2].y = clipStart.y;
}
return physics;
}
// the function called by the javascript code
extern "C" void EMSCRIPTEN_KEEPALIVE toggle_background_color() { background_is_black = !background_is_black; }
void setCanvas(){
getScreenSize();
SDL_SetWindowSize(window, (int)screen_width, (int)screen_height);
SDL_FlushEvent(SDL_WINDOWEVENT);
}
void drawGrid(){
glUseProgram(gridProgram);
GLint uniform_Resolution = glGetUniformLocation(gridProgram, "resolution");
glUniform2f(uniform_Resolution, screen_width, screen_height);
GLint uniform_Scale = glGetUniformLocation(gridProgram, "scale");
glUniform1f(uniform_Scale, stg.scale);
GLint uniform_Offset = glGetUniformLocation(gridProgram, "offset");
glUniform2f(uniform_Offset, offset.x, offset.y);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
// Draw a triangle fan for the quad
}
void drawTiles(){
glUseProgram(tilesProgram);
GLint uniform_ScaleTiles = glGetUniformLocation(tilesProgram, "scale");
glUniform1f(uniform_ScaleTiles, (1/stg.scale)*504.8);
GLint uniform_OffsetTiles = glGetUniformLocation(tilesProgram, "offset");
glUniform2f(uniform_OffsetTiles, offset.x, offset.y);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
}
void drawTiles2(){
constexpr int power = 10;
// int tilesX = (screen_width/scale);
// int tilesY = screen_height/scale;
constexpr int tilesX = ipow(2, power);
constexpr int tilesY = ipow(2, power);
// auto pixels = new GLubyte[tilesX*tilesY*3];
//auto pixels = new GLubyte[tilesX*tilesY*3];
// GLubyte*** pixels = new GLubyte**[tilesX];
// for(int i = 0; i < tilesX; ++i)
// pixels[i] = (GLubyte**)new GLubyte[tilesY][3];
// // pixels[i][0][0] = (GLubyte) i%255;
// pixels[i][0][1] = (GLubyte) i%255;
// pixels[i][0][2] = (GLubyte) i%255;
// auto pixels = new GLubyte[tilesX * tilesY * 3];
// std::cout << (int)pixels[2][2][2] << '\n';
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, stg.map_width, stg.map_height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
glUseProgram(tiles2Program);
GLint uniform_Resolution = glGetUniformLocation(tiles2Program, "resolution");
glUniform2f(uniform_Resolution, screen_width, screen_height);
GLint uniform_ScaleTiles = glGetUniformLocation(tiles2Program, "scale");
glUniform1f(uniform_ScaleTiles, stg.scale);
GLint uniform_OffsetTiles = glGetUniformLocation(tiles2Program, "offset");
glUniform2f(uniform_OffsetTiles, offset.x, offset.y);
GLint uniform_tileDims = glGetUniformLocation(tiles2Program, "tileDims");
glUniform2f(uniform_tileDims, stg.map_width, stg.map_height);
GLint uniform_texture = glGetUniformLocation(tiles2Program, "texture");
glUniform1i(uniform_texture, 0);
// std::cout << stg.scale << '\n';
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
// for (int i = 0; i < tilesX; ++i)
// {
// delete[](pixels[i]);
// }
// delete[] pixels;
// glDeleteTextures(1, &textureID);
}
GLuint compileShaders(std::string vertShader, std::string fragShader){
GLchar *gridVertexSource = (char*)vertShader.c_str();
GLchar *gridFragmentSource = (char*)fragShader.c_str();
// std::cout << gridVertexSource << "\n";
// Create a Vertex Buffer Object and copy the vertex data to it
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Create and compile the vertex shader
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &gridVertexSource, nullptr);
glCompileShader(vertexShader);
// Create and compile the fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &gridFragmentSource, nullptr);
glCompileShader(fragmentShader);
// Link the vertex and fragment shader into a shader program
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
return shaderProgram;
}
void initGL(SDL_Window *window){
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
auto gridVertexSourceStr = read_shader_file("shaders/grid.vert");
auto gridFragmentSourceStr = read_shader_file("shaders/grid.frag");
gridProgram = compileShaders(gridVertexSourceStr, gridFragmentSourceStr);
auto tilesVertexSourceStr = read_shader_file("shaders/tiles.vert");
auto tilesFragmentSourceStr = read_shader_file("shaders/tiles.frag");
tilesProgram = compileShaders(tilesVertexSourceStr, tilesFragmentSourceStr);
auto tiles2VertexSourceStr = read_shader_file("shaders/texture.vert");
auto tiles2FragmentSourceStr = read_shader_file("shaders/texture.frag");
tiles2Program = compileShaders(tiles2VertexSourceStr, tiles2FragmentSourceStr);
glUseProgram(gridProgram);
// Specify the layout of the vertex data
GLint posAttribGrid = glGetAttribLocation(gridProgram, "position");
glEnableVertexAttribArray(posAttribGrid);
glVertexAttribPointer(posAttribGrid, 2, GL_FLOAT, GL_FALSE, 0, 0);
GLint uniform_WindowSizeGrid = glGetUniformLocation(gridProgram, "resolution");
glUniform2f(uniform_WindowSizeGrid, screen_width, screen_height);
glUseProgram(tilesProgram);
GLint posAttribTiles = glGetAttribLocation(tilesProgram, "position");
glEnableVertexAttribArray(posAttribTiles);
glVertexAttribPointer(posAttribTiles, 2, GL_FLOAT, GL_FALSE, 0, 0);
GLint uniform_WindowSizeTiles = glGetUniformLocation(tilesProgram, "resolution");
glUniform2f(uniform_WindowSizeTiles, screen_width, screen_height);
GLint uniform_ScaleTiles = glGetUniformLocation(tilesProgram, "scale");
glUniform1f(uniform_ScaleTiles, stg.scale);
glGenTextures(1, &textureID);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}