-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtinygl-cpp.h
More file actions
714 lines (621 loc) · 20.8 KB
/
Copy pathtinygl-cpp.h
File metadata and controls
714 lines (621 loc) · 20.8 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#ifndef tinygl_cpp_H_
#define tinygl_cpp_H_
#include <stdio.h>
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#if ( (defined(__MACH__)) && (defined(__APPLE__)) )
#define GLFW_INCLUDE_GLCOREARB
#include <GLFW/glfw3.h>
#include <OpenGL/gl3ext.h>
#else
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#endif
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
namespace tinygl {
static void error_callback(int error, const char* description) {
fputs("\n", stderr);
fputs(description, stderr);
fputs("\n", stderr);
}
const GLchar* vertexShader[] =
{
"#version 400\n"
"uniform vec4 inColor;"
"uniform vec3 inSize;"
"uniform vec3 inPos;"
"uniform float inScreenWidth;"
"uniform float inScreenHeight;"
"in vec3 VertexPosition;"
"out vec2 uv;"
"out vec4 color;"
"void main() {"
" color = inColor;"
" uv = (VertexPosition + 0.5).xy;"
" vec4 x = vec4(2.0 / inScreenWidth, 0.0, 0.0, 0.0);"
" vec4 y = vec4(0.0, 2.0 / inScreenHeight, 0.0, 0.0);"
" vec4 z = vec4(0.0, 0.0, -2.0 / 2000, 0.0);"
" vec4 d = vec4(-1.0, -1.0, 0.0, 1.0);"
" mat4 projection = mat4(x, y, z, d);"
" vec3 pos = inSize * VertexPosition + inPos;"
" gl_Position = projection * vec4(pos, 1.0);"
"}"
};
const GLchar* fragmentShader[] =
{
"#version 400\n"
"uniform sampler2D inImage;"
"in vec4 color;"
"in vec2 uv;"
"out vec4 FragColor;"
"void main() { FragColor = color * texture(inImage, uv); }"
};
static void PrintShaderErrors(GLuint id, const std::string& label)
{
std::cerr << label << " failed\n";
GLint logLen;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &logLen);
if (logLen > 0)
{
char* log = (char*)malloc(logLen);
GLsizei written;
glGetShaderInfoLog(id, logLen, &written, log);
std::cerr << "Shader log: " << log << std::endl;
free(log);
}
}
static class Window* theInstance = 0;
/**
* @brief Manages the window and user input.
*
* Override this class to create a custom application.
*
* The scene size corresponds to the window width and height.
* All coordinates are in screen coordinates.
* Y corresponds to the vertical axis; X corresponds to the horizontal axis.
* Colors are RGBA, with each component in the range [0,1]
*
* Override setup() to do initialization at the beginning of the application.
* setup() is called once when the application starts.
*
* Call circle(), square(), background(), etc. in draw() to draw different primitives.
* draw() is called once per frame.
*
* Override mouseUp(), mouseDown(), keyUp(), etc. to respond to inputs
* Poll input using mouseX(), mouseY(), isKeyDown(), etc.
*
* @verbinclude main.cpp
*/
class Window {
public:
/**
* @brief Constructor.
* @param width The screen width (in pixels)
* @param height The screen height (in pixels)
*
* Override this class to create a custom application.
*/
Window(int width, int height) :
_windowWidth(width),
_windowHeight(height),
_elapsedTime(0.0),
_dt(-1.0) {
theInstance = this;
glfwSetErrorCallback(error_callback);
if (!glfwInit()) {
fprintf(stderr, "ERROR: Cannot initialize GLFW\n");
return;
}
// Set the GLFW window creation hints - these are optional
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_SAMPLES, 4); // Request 4x antialiasing
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
_window = glfwCreateWindow(_windowWidth, _windowHeight, "TinyGL C++ Window", 0, 0);
if (!_window) {
fprintf(stderr, "ERROR: Cannot initialize GLFW window\n");
glfwTerminate();
return;
}
glfwMakeContextCurrent(_window);
glfwSetKeyCallback(_window, Window::onKeyboardCb);
glfwSetMouseButtonCallback(_window, Window::onMouseButtonCb);
glfwSetCursorPosCallback(_window, Window::onMouseMotionCb);
glfwSetScrollCallback(_window, Window::onScrollCb);
#ifndef APPLE
if (glewInit() != GLEW_OK)
{
std::cout << "Cannot initialize GLEW\n";
return;
}
#endif
// Initialize openGL and set default values
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Alpha blend
glCullFace(GL_BACK);
background(0, 0, 0);
const float triangle[] =
{
-0.5, -0.5, 0,
0.5, -0.5, 0,
0.0, 0.5, 0
};
const float square[] =
{
-0.5, -0.5, 0,
0.5, -0.5, 0,
0.5, 0.5, 0,
-0.5, -0.5, 0,
0.5, 0.5, 0,
-0.5, 0.5, 0
};
// numVerts = numTris + 2 -> Set numTris = 16
float* circle = new float[3*(numTris+2)]; // numFloats = numVerts * 3 = 36
circle[0] = circle[1] = circle[2] = 0.0;
float deltaAngle = 2.0f * 3.1415926535897932384626433832795f / numTris;
for (int i = 1; i < numTris+2; i++) {
float angle = (i-1) * deltaAngle;
circle[i*3+0] = cos(angle);
circle[i*3+1] = sin(angle);
circle[i*3+2] = 0;
}
// default texture
glEnable(GL_TEXTURE0);
glActiveTexture(GL_TEXTURE0);
GLuint texId;
glGenTextures(1, &texId);
_textures["default"] = Texture{texId, 1, 1};
unsigned char defaultTex[4] = { 255,255,255,255 };
glBindTexture(GL_TEXTURE_2D, texId);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 1, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1,
GL_RGBA, GL_UNSIGNED_BYTE, defaultTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GLuint vboId;
glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), triangle, GL_STATIC_DRAW);
glGenVertexArrays(1, &_triVao);
glBindVertexArray(_triVao);
glEnableVertexAttribArray(0); // 0 -> activate sending VertexPositions to the active shader
glBindBuffer(GL_ARRAY_BUFFER, vboId); // as a habit -> always bind before setting data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float), square, GL_STATIC_DRAW);
glGenVertexArrays(1, &_squareVao);
glBindVertexArray(_squareVao);
glEnableVertexAttribArray(0); // 0 -> activate sending VertexPositions to the active shader
glBindBuffer(GL_ARRAY_BUFFER, vboId); // as a habit -> always bind before setting data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, 3*(numTris+2) * sizeof(float), circle, GL_STATIC_DRAW);
glGenVertexArrays(1, &_circleVao);
glBindVertexArray(_circleVao);
glEnableVertexAttribArray(0); // 0 -> activate sending VertexPositions to the active shader
glBindBuffer(GL_ARRAY_BUFFER, vboId); // as a habit -> always bind before setting data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);
delete[] circle;
GLint result;
GLuint vshaderId = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vshaderId, 1, vertexShader, NULL);
glCompileShader(vshaderId);
glGetShaderiv(vshaderId, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
PrintShaderErrors(vshaderId, "Vertex shader");
}
GLuint fshaderId = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fshaderId, 1, fragmentShader, NULL);
glCompileShader(fshaderId);
glGetShaderiv(fshaderId, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
PrintShaderErrors(fshaderId, "Fragment shader");
}
GLuint shaderId = glCreateProgram();
glAttachShader(shaderId, vshaderId);
glAttachShader(shaderId, fshaderId);
glLinkProgram(shaderId);
glGetShaderiv(shaderId, GL_LINK_STATUS, &result);
if (result == GL_FALSE)
{
PrintShaderErrors(shaderId, "Shader link");
}
glUseProgram(shaderId);
_colorUniform = glGetUniformLocation(shaderId, "inColor");
_sizeUniform = glGetUniformLocation(shaderId, "inSize");
_posUniform = glGetUniformLocation(shaderId, "inPos");
GLuint widthUniform = glGetUniformLocation(shaderId, "inScreenWidth");
glUniform1f(widthUniform, _windowWidth);
GLuint heightUniform = glGetUniformLocation(shaderId, "inScreenHeight");
glUniform1f(heightUniform, _windowHeight);
_imageUniform = glGetUniformLocation(shaderId, "inImage");
glUniform1i(_imageUniform, 0); // texture slot 0
}
virtual ~Window() {
glfwTerminate();
}
/**
* @brief Opens the window and starts the main application loop.
*
* This function should typically called from main after creating the Window
* object. This function invokes the user's setup() function and then
* repeatedly calls the user's draw() function. This function returns when
* the user closes the window (via the escape key or close menu button)
*/
void run() {
if (!_window) return; // window wasn't initialized
setup();
while (!glfwWindowShouldClose(_window)) {
float time = glfwGetTime();
_dt = time - _elapsedTime;
_elapsedTime = time;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw(); // user function
glfwPollEvents();
glfwSwapBuffers(_window);
}
}
protected:
/** @name Draw commands
*/
///@{
/**
* @brief Loads a texture to be shown on a sprite
* @param name The name tp associate with the texture
* @param filename The file containing the texture image
*
* This function should be called once during `setup()`
*/
void loadSprite(const std::string& name, const std::string& filename) {
int w, h, n;
unsigned char* data = stbi_load(filename.c_str(), &w, &h, &n, 4);
if (!data) {
std::cout << "ERROR: Cannot load texture " << filename<< std::endl;
return;
}
glEnable(GL_TEXTURE0);
glActiveTexture(GL_TEXTURE0);
GLuint texId;
if (_textures.count(name) == 0) {
glGenTextures(1, &texId);
_textures[name] = Texture{texId, w, h};
} else {
texId = _textures[name].texId;
}
glBindTexture(GL_TEXTURE_2D, texId);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, w, h);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h,
GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
stbi_image_free(data);
}
/**
* @brief Draws a sprite with the current color in pixel coordinates
* @param textureName The texture to show on the sprite.
* @param x The horizontal position of the center
* @param y The vertical position of the center
* @param scale Specify a scale to display the sprite as larger or smaller
* than the loaded image size.
*
* Calling this function draws a sprite on the screen at the given (x,y)
* coordinate. The texture name must have been initialized previously
* with a call to `loadSprite` in setup(). The size of the sprite is
* determined by the size of the image. For example, a 32x32 sprite image
* will fill 32x32 pixels on the screen unless it is resized using the scale.
* For example, setting the scale to 0.5 will
* display the sprite as 16x16 pixels, or half as big.
*/
void sprite(const std::string& textureName, float x, float y, float scale = 1) {
assert(_textures.count(textureName) > 0);
Texture& tex = _textures[textureName];
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex.texId);
glUniform1i(_imageUniform, 0);
glUniform3f(_posUniform, x, y, 0);
glUniform3f(_sizeUniform, tex.width*scale, tex.height*scale, 1);
glBindVertexArray(_squareVao);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
/**
* @brief Draws a square with the current color in pixel coordinates
* @param x The horizontal position of the center
* @param y The vertical position of the center
* @param width
* @param height
*
*/
void square(float x, float y, float width, float height) {
Texture& tex = _textures["default"];
glBindTexture(GL_TEXTURE_2D, tex.texId);
glUniform3f(_posUniform, x, y, 0);
glUniform3f(_sizeUniform, width, height, 1);
glBindVertexArray(_squareVao);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
/**
* @brief Draw a triangle with the current color using pixel coordinates
* @param x The horizontal position of the center
* @param y The vertical position of the center
* @param width of the triangle base
* @param height of the triangle
*
*/
void triangle(int x, int y, float width, float height) {
Texture& tex = _textures["default"];
glBindTexture(GL_TEXTURE_2D, tex.texId);
glUniform3f(_posUniform, x, y, 0);
glUniform3f(_sizeUniform, width, height, 1);
glBindVertexArray(_triVao);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
/**
* @brief Draws a circle with the current color in pixel coordinates
* @param x The horizontal position of the center
* @param y The vertical position of the center
* @param diameter The width of the circle
*
*/
void circle(int x, int y, float diameter) {
ellipsoid(x, y, diameter, diameter);
}
/**
* @brief Draws an ellipse with the current color in pixel coordinates
* @param x The horizontal position of the center
* @param y The vertical position of the center
* @param width The width of the ellipse
* @param height The height of the ellipse
*
*/
void ellipsoid(int x, int y, float width, float height) {
Texture& tex = _textures["default"];
glBindTexture(GL_TEXTURE_2D, tex.texId);
glUniform3f(_posUniform, x, y, 0);
glUniform3f(_sizeUniform, width, height, 1);
glBindVertexArray(_circleVao);
glDrawArrays(GL_TRIANGLE_FAN, 0, numTris+2);
}
/**
* @brief Set the current RGB color
* @param r The red component (range [0,1])
* @param g The red component (range [0,1])
* @param b The red component (range [0,1])
*
*/
void color(float r, float g, float b, float a = 1.0f) {
glUniform4f(_colorUniform, r, g, b, a);
}
///@}
/** @name Respond to events
*/
///@{
/**
* @brief Override this method to perform setup before the main application
* loop
*/
virtual void setup() {}
/**
* @brief Override this method to draw
*/
virtual void draw() {}
/**
* @brief Override this method to respond to mouse movement
* @param x The current x position
* @param y The current y position
* @param dx The change in x since the last mouse event (in pixels)
* @param dy The change in y since the last mouse event (in pixels)
*
* Screen coordinates are in pixels. x values are always between 0 and width.
* y values are always between 0 and height.
*/
virtual void mouseMotion(int x, int y, int dx, int dy) {}
/**
* @brief Override this method to respond to mouse press (button down)
* @param button The mouse button that is pressed, e.g. GLFW_MOUSE_BUTTON_LEFT
* @param mods Modifiers that are pressed (e.g. shift, control, etc)
*
* @ref https://www.glfw.org/docs/latest/input_guide.html
* @see mouseUp(int, int)
* @see mouseIsDown(int)
*/
virtual void mouseDown(int button, int mods) {}
/**
* @brief Override this method to respond to mouse press (button up)
*
* @see mouseDown(int, int)
* @see mouseIsDown(int)
*/
virtual void mouseUp(int button, int mods) {}
/**
* @brief Override this method to respond to scrolling the middle mouse
* button
* @param dx The change in the x direction (in scroll coordinates)
* @param dy The change in the x direction (in scroll coordinates)
*
*/
virtual void scroll(float dx, float dy) {}
/**
* @brief Override this method to respond to key presses (button up)
* @param key The key, e.g. as either a constant or capital ascii letter,
* such as 'P'.
* @param mods Modifiers that are pressed (e.g. shift, control, etc)
*
* @ref https://www.glfw.org/docs/latest/group__keys.html
* @see keyDown(int,int)
* @see keyIsDown(int)
*/
virtual void keyUp(int key, int mods) {}
/**
* @brief Override this method to respond to key presses (button down)
* @param key The key, e.g. as either a constant or capital ascii letter,
* such as 'P'.
* @param mods Modifiers that are pressed (e.g. shift, control, etc)
*
* @ref https://www.glfw.org/docs/latest/group__keys.html
* @see keyUp(int,int)
*/
virtual void keyDown(int key, int mods) {}
/** @name Query input and time
*/
///@{
/**
* @brief Query whether the given key is down
* @key The key to test
*
* More than one key can be pressed at once. The key can be an ascii
* capital letter or a GLFW constant
* @ref https://www.glfw.org/docs/latest/input_guide.html
*/
bool keyIsDown(int key) const {
int state = glfwGetKey(_window, key);
return (state == GLFW_PRESS);
}
/**
* @brief Query whether the given mouse button is down
* @param button The button to test
*
* More than one button can be pressed at once. The button should be a GLFW
* constant, such as GLFW_MOUSE_BUTTON_LEFT
* @ref https://www.glfw.org/docs/latest/input.html
*/
bool mouseIsDown(int button) const {
int state = glfwGetMouseButton(_window, button);
return (state == GLFW_PRESS);
}
/**
* @brief Return the current mouse position X coordinate (in screen coordinates)
*/
float mouseX() const {
double xpos, ypos;
glfwGetCursorPos(_window, &xpos, &ypos);
return static_cast<float>(xpos);
}
/**
* @brief Return the current mouse position Y coordinate (in screen coordinates)
*/
float mouseY() const {
double xpos, ypos;
glfwGetCursorPos(_window, &xpos, &ypos);
return static_cast<float>(ypos);
}
/**
* @brief Return the amount of time since the previous frame (in seconds)
*
* If the frame rate is 30 frames per second, dt would be approximately 1/30
* = 0.033333 seconds each frame
*/
float dt() const {
return _dt;
}
/**
* @brief Return the amount of time since the setup() was called (in seconds)
*/
float elapsedTime() const {
return _elapsedTime;
}
/**
* @brief Return the window height in pixels
*/
float height() const {
return static_cast<float>(_windowHeight);
}
/**
* @brief Return the window width in pixels
*/
float width() const {
return static_cast<float>(_windowWidth);
}
///@}
/**
* @brief Stop the main application loop
*
* Call this function to terminate the main loop, either from setup() or
* draw()
*/
void noLoop() {
glfwSetWindowShouldClose(_window, GL_TRUE);
}
/**
* @brief Set the background color
*
* This function can be called from draw and will additionally clear the
* color and depth buffers
*/
void background(float r, float g, float b) {
glClearColor(r, g, b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
private:
static void onScrollCb(GLFWwindow* w, double xoffset, double yoffset) {
theInstance->scroll(
static_cast<float>(xoffset),
static_cast<float>(yoffset));
}
static void onMouseMotionCb(GLFWwindow* w, double x, double y) {
theInstance->onMouseMotion(static_cast<int>(x), static_cast<int>(y));
}
static void onMouseButtonCb(GLFWwindow* w, int button, int action, int mods) {
theInstance->onMouseButton(button, action, mods);
}
static void onKeyboardCb(GLFWwindow* w, int key, int code, int action, int mods) {
theInstance->onKeyboard(key, code, action, mods);
}
void onMouseMotion(int x, int y) {
int dx = mouseX() - _lastx;
int dy = mouseY() - _lasty;
mouseMotion(x, height()-y, dx, -dy); // user hook
}
void onMouseButton(int button, int action, int mods) {
double xpos, ypos;
glfwGetCursorPos(_window, &xpos, &ypos);
if (action == GLFW_PRESS) {
_lastx = xpos;
_lasty = ypos;
mouseDown(button, mods);
} else if (action == GLFW_RELEASE) {
mouseUp(button, mods);
}
onMouseMotion(static_cast<float>(xpos), static_cast<float>(ypos));
}
void onKeyboard(int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(_window, GL_TRUE);
}
if (action == GLFW_PRESS) {
keyDown(key, mods);
} else if (action == GLFW_RELEASE) {
keyUp(key, mods);
}
}
private:
int _windowWidth, _windowHeight;
float _elapsedTime;
float _dt;
float _lastx, _lasty;
struct GLFWwindow* _window = 0;
GLuint _triVao, _squareVao, _circleVao;
GLuint _colorUniform, _sizeUniform, _posUniform, _imageUniform;
const int numTris = 16; // for circles
// textures
struct Texture {
GLuint texId;
int width;
int height;
};
std::map<std::string, Texture> _textures;
protected:
inline GLFWwindow* window() const { return _window; }
};
} // namespace tinygl
#endif