forked from drahosp/ppgso
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgl6_mesh.cpp
133 lines (106 loc) · 4.09 KB
/
gl6_mesh.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
// Example gl6_mesh
// - Displays geometry that is loaded from Wavefront OBJ files encapsulated in a mesh object
// - Implements object transformation based on mouse movement
// - Combines parallel and orthographic camera projection
// - SPACE stops/resumes the animation
#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 for displaying multiple meshes projections
*/
class MeshWindow : public ppgso::Window {
private:
// Initialize resources
ppgso::Shader program = {texture_vert_glsl, texture_frag_glsl};
ppgso::Texture sphereTexture = {ppgso::image::loadBMP("sphere.bmp")};
ppgso::Texture cursorTexture = {ppgso::image::loadBMP("lena.bmp")};
ppgso::Mesh sphere = {"sphere.obj"};
ppgso::Mesh cursor = {"quad.obj"};
ppgso::Mesh cube = {"cube.obj"};
bool animationEnabled = true;
double cursorX = 0.0;
double cursorY = 0.0;
float time = 0.0f;
public:
/*!
* Construct our new custom window and set up OpenGL
*/
MeshWindow() : Window{"gl6_mesh", SIZE, SIZE} {
hideCursor();
// Enable Z-buffer
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
// Enable polygon culling
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
}
/*!
* Handles pressed key when the window is focused
* @param key Key code of the key being pressed/released
* @param scanCode Scan code of the key being pressed/released
* @param action Action indicating the key state change
* @param mods Additional modifiers to consider
*/
void onKey(int key, int scanCode, int action, int mods) override {
if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) {
animationEnabled = !animationEnabled;
}
}
/*!
* Handle cursor position changes
* @param cursorX Mouse horizontal position in window coordinates
* @param cursorY Mouse vertical position in window coordinates
*/
void onCursorPos(double cursorX, double cursorY) override {
// Convert position from screen coordinates <0, SIZE> to OpenGL viewport coordinates <-1,1>
this->cursorX = (cursorX / ((double) SIZE) * 2) - 1;
this->cursorY = -((cursorY / ((double) SIZE) * 2) - 1);
}
/*!
* Window update implementation that will be called automatically from pollEvents
*/
void onIdle() override {
// Animate using time when activated
if (animationEnabled) time = (float) glfwGetTime();
// Set gray background
glClearColor(.5f, .5f, .5f, 0);
// Clear depth and color buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Create object matrices
auto cubeMat = rotate(glm::mat4{1.0f}, time, {0.5f, 1.0f, 0.0f});
auto sphereMat = translate(glm::mat4{1.0f}, {sin(time), cos(time), 0});
sphereMat = scale(sphereMat, {0.5f, 0.5f, 0.5f});
// Camera position/rotation - for example, translate camera a bit backwards (positive value in Z axis), so we can see the objects
auto cameraMat = translate(glm::mat4{1.0f}, {0.0f, 0.0f, -2.5f});
program.setUniform("ViewMatrix", cameraMat);
// Update camera position with perspective projection
program.setUniform("ProjectionMatrix", glm::perspective((ppgso::PI / 180.f) * 60.0f, 1.0f, 0.1f, 10.0f));
// Render objects
program.setUniform("Texture", sphereTexture);
// Central box
program.setUniform("ModelMatrix", cubeMat);
cube.render();
// Smaller sphere
program.setUniform("ModelMatrix", sphereMat);
sphere.render();
// Draw cursor using orthographic/parallel projection
program.setUniform("ProjectionMatrix", glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, 1000.0f, -1000.0f));
// Create object matrix
auto cursorMat = translate(glm::mat4{1.0f}, {cursorX, cursorY, 0.0f}) * scale(glm::mat4{1.0f}, {0.1f, 0.1f, 0.1f});
// Render objects
program.setUniform("Texture", cursorTexture);
program.setUniform("ModelMatrix", cursorMat);
cursor.render();
}
};
int main() {
// Create instance of our mesh window
MeshWindow window;
// Poll events in loop
while (window.pollEvents()) {}
return EXIT_SUCCESS;
}