forked from drahosp/ppgso
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gl3_animate.cpp
99 lines (80 loc) · 2.89 KB
/
gl3_animate.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
// Example gl3_animate
// - Demonstrates the use of a dynamically generated texture content on the CPU
// - Displays the generated content as texture on a quad using OpenGL
// - Basic animation achieved by incrementing a parameter used in the image generation
#include <iostream>
#include <cmath>
#include <glm/glm.hpp>
#include <ppgso/ppgso.h>
#include <shaders/texture_vert_glsl.h>
#include <shaders/texture_frag_glsl.h>
const unsigned int SIZE = 512;
/*!
* Custom window that will update its contents to create animation
*/
class AnimateWindow : public ppgso::Window {
private:
// Create shading program from included shader sources
ppgso::Shader program = {texture_vert_glsl, texture_frag_glsl};
// Load a quad mesh
ppgso::Mesh quad = {"quad.obj"};
// Initialize texture
ppgso::Texture texture = {SIZE, SIZE};
/*!
* Update OpenGL texture with new animation frame
* @param texture Texture to update
* @param time Time to generate animation frame for
*/
void updateTexture(ppgso::Texture &texture, double time) {
// Draw something to the texture
double cx = sin(time);
double cy = cos(time * 0.9);
#pragma omp parallel for
for (int y = 0; y < texture.image.height; y++) {
for (int x = 0; x < texture.image.width; x++) {
auto& pixel = texture.image.getPixel(x, y);
double fx = (float) x / (float) (texture.image.width) - .5;
double fy = (float) y / (float) (texture.image.height) - .5;
double dist = sqrt(pow(fx - cx, 2.0) + pow(fy - cy, 2.0));
pixel.r = (uint8_t) (sin(dist * 45.0) * 127 + 128);
pixel.g = (uint8_t) (sin(dist * 44.0) * 127 + 128);
pixel.b = (uint8_t) (sin(dist * 46.0) * 127 + 128);
}
}
// Update the OpenGL texture content
texture.update();
}
public:
/*!
* Construct a new Window and initialize shader uniform variables
*/
AnimateWindow() : ppgso::Window{"gl3_animate", SIZE, SIZE} {
// Pass the texture to the program as uniform input called "Texture"
program.setUniform("Texture", texture);
// Set Matrices to identity so there are no projections/transformations applied in the vertex shader
program.setUniform("ModelMatrix", glm::mat4{1.0f});
program.setUniform("ViewMatrix", glm::mat4{1.0f});
program.setUniform("ProjectionMatrix", glm::mat4{1.0f});
}
/*!
* Window update implementation that will be called automatically from pollEvents
*/
void onIdle() override {
// Generate texture content
auto time = glfwGetTime();
updateTexture(texture, time);
// Set gray background
glClearColor(.5f, .5f, .5f, 0);
// Clear depth and color buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Render the quad geometry
quad.render();
}
};
int main() {
// Create a window with OpenGL 3.3 enabled
AnimateWindow window;
// Main execution loop
while (window.pollEvents()) {}
return EXIT_SUCCESS;
}