-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
320 lines (222 loc) · 15.5 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
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
#include <iostream>
#include "GLWindow.h"
#include "Shaders.h"
#include "Quad.h"
#include "Resources.h"
#include "Camera.h"
#include "FlatCamera.h"
#include "Particles.h"
#include "Text.h"
#include "Model.h"
#include "Resources.h"
#include "Background.h"
#include "BGCircles.h"
#include "BGPlanes.h"
#include "BGTunnel.h"
#include "Slide.h"
#include "TextSlide.h"
#include "bcm_host.h"
Camera camera;
FlatCamera flatcamera;
//Particles *particles;
Text *text0;
bool knext = false, kprev = false;
void Render()
{
if(GLWindow::KeyPressed(BTN_MIDDLE) || GLWindow::KeyPressed(KEY_ESC))
GLWindow::BreakLoop();
if(GLWindow::KeyPressed(BTN_GEAR_UP))
{
flatcamera.Zoom(0.1f);
flatcamera.UpdateProjection();
}
if(GLWindow::KeyPressed(BTN_GEAR_DOWN))
{
flatcamera.Zoom(-0.1f);
flatcamera.UpdateProjection();
}
if(GLWindow::KeyPressed(BTN_LEFT))
Slide::Next();
if(GLWindow::KeyPressed(BTN_RIGHT))
Slide::Prev();
if(GLWindow::StrInput())
{
if(strcmp("quit\n", GLWindow::StrInput()) == 0)
GLWindow::BreakLoop();
if(strcmp("prev\n", GLWindow::StrInput()) == 0)
Slide::Prev();
if(strcmp("next\n", GLWindow::StrInput()) == 0)
Slide::Next();
unsigned n;
if(sscanf(GLWindow::StrInput(), "slide %u", &n) == 1)
Slide::Number(n);
}
Background::Update(GLWindow::TimeNS());
Slide::Update(GLWindow::TimeNS(), &flatcamera);
GLenum attachments[] =
{
GL_COLOR_EXT,
//GL_DEPTH_EXT,
//GL_STENCIL_EXT
};
Background::Render();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, GLWindow::ScreenWidth(), GLWindow::ScreenHeight());
glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
Background::Draw();
Slide::Draw();
}
int main(int argc, char *argv[])
{
bcm_host_init();
if(GLWindow::InitGL())
{
if(!Shader::InitAll())
return 0;
if(!Resources::Init())
return 0;
Quad::Init();
glEnableVertexAttribArray(0);
glDisable(GL_DITHER);
flatcamera.Reset();
flatcamera.AspectRatio(GLWindow::ScreenWidth(), GLWindow::ScreenHeight());
flatcamera.UpdateProjection();
camera.AspectRatio(GLWindow::ScreenWidth(), GLWindow::ScreenHeight());
camera.UpdateProjection();
if(!Background::Init(GLWindow::ScreenWidth() / 4, GLWindow::ScreenHeight() / 4))
{
std::cout << "Could not init buffers." << std::endl;
return 0;
}
Background::Add(new BGTunnel());
Background::Add(new BGCircles());
Background::Add(new BGPlanes());
Slide::Init();
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "Disclaimer:\n"
"This is a work of fiction. Names, characters,\n"
"places and incidents either are products of the\n"
"author's imagination or are used fictitiously.\n"
"Any resemblance to actual events or locales or\n"
"persons, living or dead, is entirely coincidental.", 250, 0.03f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "OpenGL ES 2.0 Programming\non a Raspberry Pi\n(!!!111!!one!!1!eleven!1!)", 70, 0.11f, true));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "Codebits 2012\n\n@xernobyl\nTiago Chagas Farto", 43, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "take 2 @ codebits", 17, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], " github.com/xernobyl ", 23, 0.1f)); // BUG
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "part I", 6, 0.125f, true));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "Why computer graphics?", 22, 0.125f, true));
// wireframe cube goes here
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "...books? ...tutorials?", 23, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], " ...code! ", 12, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "windows\nopengl\ndirectx\nlinux\nwindows again...", 45, 0.075f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "rarely copied code\nnever used an engine", 39, 0.1f, true));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], " \"click\" ", 11, 0.1f)); // BUG
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "deferred renderer\nlight pre pass renderer\nambient occlusion\nglobal illumination\nphysically based rendering", 106, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], " POWER ", 9, 0.2f)); // BUG
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "So I tried Android for a while...", 33, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], " ...and WebGL. ", 17, 0.1f)); // BUG
//Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "\"appealing to nonprofessional programmers, like Microsoft's Visual Basic\"", 7, 0.05f)); // BUG
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "I like control.", 15, 0.125f, true));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "libgml\nlibraspberrypi", 21, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "Raspberry Pi", 12, 0.15f, true));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "700Mhz CPU\n250Mhz GPU\n256MB RAM", 31, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "it's cheap!", 11, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], " Linux ", 11, 0.125f)); // BUG
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "the good part!", 14, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "...perhaps your own OS?", 23, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "OpenGL ES 2.0", 13, 0.15f, true));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "march 2007", 10, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "state machine", 13, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], ">121 functions!", 15, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "Buffers Viewport and clipping Reading pixels Vertices Texturing\nRasterization Pixel rectangles Shaders and programs Shader queries\nPer-fragment operations Special functions State and state Requests\nWhole framebuffer operations Framebuffer objects", 246, 0.04f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "...and a few extensions", 23, 0.125f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO],
"GL_OES_compressed_ETC1_RGB8_texture\n"
"GL_OES_compressed_paletted_texture\n"
"GL_OES_texture_npot\n"
"GL_OES_depth24\n"
"GL_OES_vertex_half_float\n"
"GL_OES_EGL_image\n"
"GL_OES_EGL_image_external\n"
"GL_EXT_discard_framebuffer\n"
"GL_OES_rgb8_rgba8\n"
"GL_OES_depth32\n"
"GL_OES_mapbuffer\n"
"GL_EXT_texture_format_BGRA8888\n"
"GL_APPLE_rgb_422\n"
"GL_EXT_debug_marker", 318, 0.0625f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], " asynchronous ", 14, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "context:\n\ncolor buffer\ndepth buffer\nstencil buffer", 50, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "...lets get moving", 18, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "do\n{\n"
"\tDrawFrame();\n"
"\teglSwapBuffers(display, surface);\n"
"} while(looping);", 72, 0.05f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "end of part I", 13, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "questions so far?", 17, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "A black screen\nis usually easy.", 31, 0.15f, true));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glClearColor(0.0f, 0.0f, 0.0f, 0.0f);\nglClear(GL_COLOR_BUFFER_BIT);", 67, 0.05f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "OK. What's next?", 16, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glDrawArrays(mode, first, count);\nglDrawElements(mode, count, type, *indices);", 78, 0.05f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "POINTS, TRIANGLES, LINES\nLINE_STRIP, LINE_LOOP", 46, 0.075f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], " vertices ", 10, 0.2f, true)); // BUG
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], " attributes ", 12, 0.1f)); // BUG
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "you need to know where you are", 30, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glVertexAttribPointer(n, size, type, normalize, stride, offset);\nglEnableVertexAttribArray(number);", 104, 0.04f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "vertex buffer objects", 21, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glGenBuffers(1, &vbo);\nglBindBuffer(GL_ARRAY_BUFFER, vbo);\nglBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);", 117, 0.04f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "STREAM\nSTATIC\nDYNAMIC", 21, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], " 2-manifold ", 12, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "a tetrahedron", 13, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "each vertex is shared by 3 faces", 32, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "the solution is to index", 24, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glGenBuffers(1, &ibo);\nglBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);\nglBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);", 133, 0.04f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "like the previous...\nbut with ELEMENT", 37, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "And speaking of EXTENSIONS...", 29, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "void * glMapBuffer(GLenum target, GLenum access);\nGLboolean glUnmapBuffer(GLenum target);", 89, 0.05f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "and since I've we're on 3d space", 32, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glEnable(GL_DEPTH_TEST);\nglEnable(GL_CULL_FACE);", 48, 0.1f));
//Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glClear(GL_DEPTH_BUFFER_BIT);", 29, 0.1f));
//Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glDisable(GL_DEPTH_TEST);\nglDisable(GL_CULL_FACE);", 50, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], " Shaders!!1!!11!! ", 18, 0.2f, true)); // BUG
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "are essentially a set of callbacks", 34, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "one for vertices", 16, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "another for fragments", 21, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "shader = glCreateShader(GL_VERTEX_SHADER); // FRAGMENT\nglShaderSource(shader, 1, shaderfile, shaderfilelen);\nglCompileShader(shader);", 146, 0.05f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "do it a few times", 17, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "program = glCreateProgram();\nglAttachShader(program, shader[i]);\nglBindAttribLocation(program, attrib_number, \"NAME\");\nglLinkProgram(program);", 149, 0.05f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "...and what exactly is a shader?", 32, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], " GLSL ", 10, 0.1f)); // BUG
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "...a vertex shader...", 21, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "attribute vec4 A0; // defaults to vec4(.0, .0, .0, 1.0)\nuniform mat4 a_matrix;\nvarying float Im_not_doing_anything;\n\nvoid main()\n{\n\t// js guys: that's the correct way to use parentheses\n\tgl_Position = a_matrix * A0;\n\tIm_not_doing_anything = 1337.0;\n}", 252, 0.05f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "...and a fragment shader...", 27, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "precision lowp float;\nvarying float Im_not_doing_anything;\n\nvoid main()\n{\n\tgl_FragColor = vec4(1.0, 0.0, 0.0, 0.0);\n}", 117, 0.05f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "You need all that to\nshow something on screen.", 46, 0.1f));
// RED CUBE
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "but there's not much more to know", 33, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "this concludes the 2nd part", 27, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "...any questions so far?", 24, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glEnable(GL_BLEND);\nglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);", 70, 0.05f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], " textures ", 12, 0.2f, true)); // BUG
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "OGLES2 supports 2 kinds", 23, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glGenTextures(1, &texture);\nglBindTexture(GL_TEXTURE_2D, texture);", 66, 0.075f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "void TexImage2D(target, level, internalformat, w, h, border, format, type, data);\nor\nvoid CompressedTexImage2D(target, level, internalformat, w, h, border, imageSize, data);", 260, 0.05f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);\nglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);", 145, 0.04f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "and to use them", 15, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glActiveTexture(GL_TEXTURE0 + look_at_me);\nglBindTexture(GL_TEXTURE_2D, texture);\nglUniformi(location, look_at_me)", 114, 0.05f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "draw stuff with textures", 24, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "only one major feature is missing", 33, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "framebuffer\nobjects", 19, 0.2f, true));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "color depth stencil", 19, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glGenRenderbuffers(1, &depthrenderbuffer);\nglBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);\nglRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height); // DEPTH_COMPONENT24_OES or 32\n\nglBindFramebuffer(GL_FRAMEBUFFER, framebuffer);\nglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,\ndepthrenderbuffer);\nglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,\nframebuffertexture, 0);", 455, 0.04f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);\n// framebuffer 0 is the main one\nglViewport(0, 0, width, height);", 113, 0.05f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "And that's it!", 14, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "A few performance notes...", 26, 0.1f, true));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "use the smallest possible formats\navoid overdraw\nstate changes\nreuse\n...", 72, 0.1f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], "OpenGL ES 2.0 Quick Reference\nopengl.org/wiki\nkhronos.org/opengles/sdk/docs/man\n\ncode.google.com/p/opengles-book-samples\n\nassimp.sourceforge.net\naras-p.info/blog/2010/09/29/glsl-optimizer\n\nraspberrypi.org", 204, 0.05f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], "Thank you.", 10, 0.15f));
Slide::Add(new TextSlide(&Resources::font[F_FUTURA], " Questions? ", 12, 0.10f));
Slide::Add(new TextSlide(&Resources::font[F_ANONYMOUSPRO], " github.com/xernobyl ", 23, 0.1f)); // BUG
GLWindow::Loop(Render);
}
return 0;
}