author | František Kučera <franta-hg@frantovo.cz> |
Wed, 06 Dec 2023 22:54:24 +0100 | |
branch | v_0 |
changeset 21 | bcc53967ab3c |
parent 20 | 0899e966993e |
child 24 | 98d033d3ef7c |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* ShaderShark |
|
3 |
* Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
||
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
18 |
#include <iostream> |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
19 |
#include <iomanip> |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
20 |
#include <string> |
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
21 |
#include <memory> |
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
22 |
#include <functional> |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
23 |
#include <sstream> |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
24 |
#include <vector> |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
25 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
26 |
#include "x11.h" |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
27 |
#include "opengl.h" |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
28 |
#include "EPoll.h" |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
29 |
#include "Logger.h" |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
30 |
#include "MappedFile.h" |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
31 |
#include "ImageLoader.h" |
19 | 32 |
#include "Texture.h" |
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
33 |
#include "Shader.h" |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
34 |
#include "Program.h" |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
35 |
#include "FileMonitor.h" |
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
36 |
|
0 | 37 |
#include "Shark.h" |
38 |
||
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
39 |
class Shark::Impl { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
40 |
public: |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
41 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
42 |
struct { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
43 |
GLint aVertexXYZ = -2; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
44 |
GLint aTextureXY = -2; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
45 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
46 |
GLint fColor = -2; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
47 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
48 |
GLint uModel = -2; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
49 |
GLint uView = -2; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
50 |
GLint uProjection = -2; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
51 |
GLint uTexture = -2; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
52 |
} ProgAttr; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
53 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
54 |
struct { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
55 |
float yaw = -90.f; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
56 |
float pitch = 0.f; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
57 |
float roll = 0.f; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
58 |
float fov = 45.0f; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
59 |
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
60 |
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
61 |
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
62 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
63 |
void adjustFov(float diff) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
64 |
fov += diff; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
65 |
if (fov < 1.0f) fov = 1.0f; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
66 |
else if (fov > 120.0f) fov = 120.0f; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
67 |
std::cerr << "field of view: " << fov << " °" << std::endl; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
68 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
69 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
70 |
void moveForward(const float cameraSpeed) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
71 |
cameraPos += cameraSpeed * cameraFront; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
72 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
73 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
74 |
void moveBackward(const float cameraSpeed) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
75 |
cameraPos -= cameraSpeed * cameraFront; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
76 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
77 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
78 |
void moveLeft(const float cameraSpeed) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
79 |
cameraPos -= glm::normalize( |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
80 |
glm::cross(cameraFront, cameraUp)) * cameraSpeed; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
81 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
82 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
83 |
void moveRight(const float cameraSpeed) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
84 |
cameraPos += glm::normalize( |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
85 |
glm::cross(cameraFront, cameraUp)) * cameraSpeed; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
86 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
87 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
88 |
void moveUp(const float cameraSpeed) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
89 |
cameraPos += cameraSpeed * glm::normalize(cameraUp); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
90 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
91 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
92 |
void moveDown(const float cameraSpeed) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
93 |
cameraPos -= cameraSpeed * glm::normalize(cameraUp); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
94 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
95 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
96 |
void updateCameraFrontAndUp() { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
97 |
std::cerr << "--- updateCameraFrontAndUp() --------" << std::endl; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
98 |
dump("pitch, yaw, roll", glm::vec3(pitch, yaw, roll)); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
99 |
dump("cameraPos", cameraPos); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
100 |
dump("cameraFront", cameraFront); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
101 |
const auto pitchR = glm::radians(pitch); // around X axis |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
102 |
const auto yawR = glm::radians(yaw); // around Y axis |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
103 |
const auto rollR = glm::radians(roll); // around Z axis |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
104 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
105 |
cameraFront.x = cos(pitchR) * cos(yawR); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
106 |
cameraFront.y = sin(pitchR); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
107 |
cameraFront.z = cos(pitchR) * sin(yawR); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
108 |
cameraFront = glm::normalize(cameraFront); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
109 |
dump("cameraFront", cameraFront); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
110 |
dump("cameraUp", cameraUp); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
111 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
112 |
// TODO: review ROLL rotation and default angle |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
113 |
glm::mat4 rollMatrix = glm::rotate( |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
114 |
glm::mat4(1.0f), rollR, cameraFront); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
115 |
cameraUp = glm::mat3(rollMatrix) * glm::vec3(0., 1., 0.); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
116 |
dump("cameraUp", cameraUp); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
117 |
std::cerr << "-------------------------------------" << std::endl; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
118 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
119 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
120 |
void limitPitch() { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
121 |
if (pitch > +89.0f) pitch = +89.0f; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
122 |
if (pitch < -89.0f) pitch = -89.0f; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
123 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
124 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
125 |
void turnLeft(const float angleSpeed) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
126 |
yaw -= angleSpeed; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
127 |
updateCameraFrontAndUp(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
128 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
129 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
130 |
void turnRight(const float angleSpeed) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
131 |
yaw += angleSpeed; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
132 |
updateCameraFrontAndUp(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
133 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
134 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
135 |
void turnUp(const float angleSpeed) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
136 |
pitch += angleSpeed; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
137 |
limitPitch(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
138 |
updateCameraFrontAndUp(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
139 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
140 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
141 |
void turnDown(const float angleSpeed) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
142 |
pitch -= angleSpeed; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
143 |
limitPitch(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
144 |
updateCameraFrontAndUp(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
145 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
146 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
147 |
void rollLeft(const float angleSpeed) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
148 |
roll += angleSpeed; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
149 |
updateCameraFrontAndUp(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
150 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
151 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
152 |
void rollRight(const float angleSpeed) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
153 |
roll -= angleSpeed; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
154 |
updateCameraFrontAndUp(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
155 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
156 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
157 |
} initialCtx, ctx; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
158 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
159 |
Display* dpy; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
160 |
Window win; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
161 |
XVisualInfo* vi; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
162 |
GLXContext glc; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
163 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
164 |
FileMonitor fileMonitor; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
165 |
std::vector<WatchedFile> watchedFiles; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
166 |
ImageLoader imageLoader; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
167 |
std::vector<std::shared_ptr<Shader>> shaders; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
168 |
std::shared_ptr<Program> shaderProgram; |
19 | 169 |
std::vector<std::shared_ptr<Texture>> textures; |
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
170 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
171 |
Configuration cfg; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
172 |
std::ostream& logOutput = std::cerr; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
173 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
174 |
Impl(Configuration cfg) : cfg(cfg) { |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
175 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
176 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
177 |
void run(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
178 |
void clear(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
179 |
void runShaders(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
180 |
Window getRootWindow(Window defaultValue); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
181 |
void log(LogLevel level, std::string message); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
182 |
int setNonBlocking(int fd); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
183 |
void loadVertices(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
184 |
bool reloadTexture(const std::string& fileName); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
185 |
void loadTextures(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
186 |
std::shared_ptr<Program> loadShaders(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
187 |
bool reloadShader(const std::string& fileName); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
188 |
void setTitle(const std::string& suffix = ""); |
11
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
189 |
static const std::string getDefaultFile(const std::string& relativePath); |
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
190 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
191 |
}; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
192 |
|
0 | 193 |
Shark::Shark(const Configuration& configuration) : |
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
194 |
impl(new Impl(configuration)) { |
0 | 195 |
} |
196 |
||
197 |
Shark::~Shark() { |
|
19 | 198 |
impl->textures.clear(); |
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
199 |
impl->shaders.clear(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
200 |
impl->shaderProgram = nullptr; |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
201 |
XFree(impl->vi); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
202 |
glXMakeCurrent(impl->dpy, None, NULL); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
203 |
glXDestroyContext(impl->dpy, impl->glc); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
204 |
XDestroyWindow(impl->dpy, impl->win); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
205 |
XCloseDisplay(impl->dpy); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
206 |
delete impl; |
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
207 |
// std::cerr << "~Shark()" << std::endl; |
0 | 208 |
} |
209 |
||
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
210 |
void Shark::Impl::setTitle(const std::string& suffix) { |
8
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
211 |
std::stringstream title; |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
212 |
title << "ShaderShark"; |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
213 |
if (suffix.size()) title << ": " << suffix.c_str(); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
214 |
XStoreName(dpy, win, title.str().c_str()); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
215 |
XFlush(dpy); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
216 |
} |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
217 |
|
0 | 218 |
void Shark::run() { |
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
219 |
impl->run(); |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
220 |
} |
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
221 |
|
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
222 |
void Shark::Impl::run() { |
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
223 |
dpy = XOpenDisplay(NULL); |
0 | 224 |
|
225 |
if (dpy == NULL) throw std::logic_error("Unable to connect to X server"); |
|
226 |
||
227 |
GLint att[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None}; |
|
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
228 |
vi = glXChooseVisual(dpy, 0, att); |
0 | 229 |
Window root = DefaultRootWindow(dpy); |
230 |
Window parent = cfg.rootWindow ? cfg.rootWindow : root; |
|
231 |
||
232 |
XSetWindowAttributes swa; |
|
233 |
swa.colormap = XCreateColormap(dpy, parent, vi->visual, AllocNone); |
|
234 |
swa.event_mask = ExposureMask | KeyPressMask | PointerMotionMask |
|
235 |
| ButtonPressMask |
|
236 |
| StructureNotifyMask; |
|
237 |
||
238 |
bool full = false; |
|
239 |
unsigned int width = 1600; |
|
240 |
unsigned int height = 1200; |
|
241 |
if (parent != root) { |
|
242 |
XWindowAttributes parentAttr; |
|
243 |
XGetWindowAttributes(dpy, parent, &parentAttr); |
|
244 |
width = parentAttr.width; |
|
245 |
height = parentAttr.height; |
|
246 |
} |
|
247 |
||
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
248 |
win = XCreateWindow( |
0 | 249 |
dpy, parent, 0, 0, width, height, 0, |
250 |
vi->depth, InputOutput, vi->visual, |
|
251 |
CWColormap | CWEventMask, &swa); |
|
252 |
||
253 |
XMapWindow(dpy, win); |
|
8
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
254 |
setTitle(); |
0 | 255 |
setX11PID(dpy, win); |
256 |
// XSetWindowBackground(dpy, win, 0) vs. glClearColor() |
|
257 |
||
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
258 |
glc = glXCreateContext(dpy, vi, NULL, GL_TRUE); |
0 | 259 |
glXMakeCurrent(dpy, win, glc); |
260 |
||
15
1eb7cfefbeea
transparency/alpha: blending
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
261 |
glEnable(GL_DEPTH_TEST); |
1eb7cfefbeea
transparency/alpha: blending
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
262 |
glEnable(GL_BLEND); |
1eb7cfefbeea
transparency/alpha: blending
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
263 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
1eb7cfefbeea
transparency/alpha: blending
František Kučera <franta-hg@frantovo.cz>
parents:
12
diff
changeset
|
264 |
|
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
265 |
clear(); |
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
266 |
glXSwapBuffers(dpy, win); |
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
267 |
|
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
268 |
|
0 | 269 |
// Load GLSL shaders: |
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
270 |
shaderProgram = loadShaders(); |
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
271 |
loadTextures(); |
0 | 272 |
loadVertices(); |
273 |
||
274 |
auto toggleFullscreen = [&]() { |
|
275 |
full = setFullscreen(dpy, win, !full); |
|
276 |
}; |
|
277 |
||
278 |
auto resetView = [&]() { |
|
279 |
ctx = initialCtx; |
|
280 |
ctx.updateCameraFrontAndUp(); |
|
281 |
}; |
|
282 |
||
283 |
// root can reize our window |
|
284 |
// or we can listen to root resize and then resize our window ourselves |
|
285 |
bool listenToRootResizes = true; |
|
286 |
if (listenToRootResizes) XSelectInput(dpy, parent, StructureNotifyMask); |
|
287 |
||
288 |
bool keepRunningX11 = true; |
|
289 |
int x11fd = XConnectionNumber(dpy); |
|
290 |
EPoll epoll; |
|
291 |
epoll.add(x11fd); |
|
7
e6065118326f
monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
292 |
epoll.add(fileMonitor.getFD()); |
0 | 293 |
try { |
294 |
epoll.add(setNonBlocking(STDIN_FILENO)); |
|
295 |
} catch (const EPoll::Exception& e) { |
|
296 |
logOutput << "Will not monitor events on STDIN: " << e.what() << "\n"; |
|
297 |
} |
|
298 |
||
299 |
// rended the 3D scene even before the first event: |
|
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
300 |
runShaders(); |
0 | 301 |
glXSwapBuffers(dpy, win); |
302 |
||
303 |
for (XEvent xev; keepRunningX11;) { |
|
304 |
int epollEventCount = epoll.wait(); |
|
305 |
//std::cout << "trace: epoll.wait() = " << epollEventCount << std::endl; |
|
306 |
for (int epollEvent = 0; epollEvent < epollEventCount; epollEvent++) { |
|
8
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
307 |
bool redraw = false; |
0 | 308 |
if (epoll[epollEvent].data.fd == x11fd) { |
309 |
if (!XPending(dpy)) { |
|
310 |
// otherwise STDIN events are held until the first X11 event |
|
311 |
logOutput << "trace: no pending X11 event" << std::endl; |
|
312 |
break; |
|
313 |
} |
|
20
0899e966993e
process all X11 event waiting in the queue in the same cycle
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
314 |
process_x11_event: |
0 | 315 |
XWindowAttributes gwa; |
316 |
XNextEvent(dpy, &xev); |
|
317 |
||
318 |
if (xev.type == Expose) { |
|
319 |
std::cout << "XEvent: Expose" << std::endl; |
|
320 |
XGetWindowAttributes(dpy, win, &gwa); |
|
321 |
glViewport(0, 0, gwa.width, gwa.height); |
|
322 |
redraw = true; |
|
323 |
} else if (xev.type == KeyPress) { |
|
324 |
DecodedKey key = decodeKeycode(dpy, xev.xkey.keycode); |
|
325 |
std::cout << "XEvent: KeyPress:" |
|
326 |
<< " keycode=" << key.code |
|
327 |
<< " key=" << key.name |
|
328 |
<< std::endl; |
|
329 |
||
330 |
const float cSp = 0.05f; // camera speed |
|
331 |
const float aSp = 5.f; // angle speed |
|
332 |
||
333 |
if (key.matches(XK_q, XK_Escape)) keepRunningX11 = false; |
|
334 |
else if (key.matches(XK_Left, XK_s)) ctx.turnLeft(aSp); |
|
335 |
else if (key.matches(XK_Right, XK_f)) ctx.turnRight(aSp); |
|
336 |
else if (key.matches(XK_Up, XK_e)) ctx.moveForward(cSp); |
|
337 |
else if (key.matches(XK_Down, XK_d)) ctx.moveBackward(cSp); |
|
338 |
else if (key.matches(XK_w)) ctx.rollLeft(aSp); |
|
339 |
else if (key.matches(XK_r)) ctx.rollRight(aSp); |
|
340 |
else if (key.matches(XK_t)) ctx.turnUp(aSp); |
|
341 |
else if (key.matches(XK_g)) ctx.turnDown(aSp); |
|
342 |
else if (key.matches(XK_m)) ctx.moveLeft(cSp); |
|
343 |
else if (key.matches(XK_comma)) ctx.moveRight(cSp); |
|
344 |
else if (key.matches(XK_l)) ctx.moveUp(cSp); |
|
345 |
else if (key.matches(XK_period)) ctx.moveDown(cSp); |
|
346 |
else if (key.matches(XK_j)) ctx.moveLeft(cSp * 5); |
|
347 |
else if (key.matches(XK_k)) ctx.moveRight(cSp * 5); |
|
348 |
else if (key.matches(XK_u)) ctx.moveLeft(cSp * 10); |
|
349 |
else if (key.matches(XK_i)) ctx.moveRight(cSp * 10); |
|
350 |
else if (key.matches(XK_x)) resetView(); |
|
351 |
else if (key.matches(XK_F11, XK_y)) toggleFullscreen(); |
|
352 |
redraw = true; |
|
353 |
} else if (xev.type == ButtonPress) { |
|
354 |
std::cout << "XEvent: ButtonPress:" |
|
355 |
<< " button=" << xev.xbutton.button |
|
356 |
<< std::endl; |
|
357 |
if (xev.xbutton.button == 1); |
|
9
53ba7e52c67c
mouse wheel: move camera, not object
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
358 |
else if (xev.xbutton.button == 4) ctx.adjustFov(-1.0); |
53ba7e52c67c
mouse wheel: move camera, not object
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
359 |
else if (xev.xbutton.button == 5) ctx.adjustFov(+1.0); |
0 | 360 |
else if (xev.xbutton.button == 8) resetView(); |
361 |
else if (xev.xbutton.button == 9) keepRunningX11 = false; |
|
362 |
redraw = true; |
|
363 |
} else if (xev.type == MotionNotify) { |
|
364 |
// printCursorInfo(xev.xmotion); |
|
365 |
} else if (xev.type == ConfigureNotify) { |
|
366 |
std::cout << "XEvent: ConfigureNotify:" |
|
367 |
<< " window=" << xev.xconfigure.window |
|
368 |
<< " height=" << xev.xconfigure.height |
|
369 |
<< " width=" << xev.xconfigure.width |
|
370 |
<< std::endl; |
|
371 |
if (listenToRootResizes |
|
372 |
&& xev.xconfigure.window == parent) { |
|
373 |
XResizeWindow(dpy, win, |
|
374 |
xev.xconfigure.width, xev.xconfigure.height); |
|
375 |
} |
|
376 |
} else if (xev.type == UnmapNotify) { |
|
377 |
std::cout << "XEvent: UnmapNotify" << std::endl; |
|
378 |
} else if (xev.type == DestroyNotify) { |
|
379 |
std::cout << "XEvent: DestroyNotify → finish" << std::endl; |
|
380 |
break; |
|
381 |
} else { |
|
382 |
std::cout << "XEvent: type=" << xev.type << std::endl; |
|
383 |
} |
|
20
0899e966993e
process all X11 event waiting in the queue in the same cycle
František Kučera <franta-hg@frantovo.cz>
parents:
19
diff
changeset
|
384 |
if (XPending(dpy)) goto process_x11_event; |
0 | 385 |
} else if (epoll[epollEvent].data.fd == STDIN_FILENO) { |
386 |
int epollFD = epoll[epollEvent].data.fd; |
|
387 |
logOutput << "other event: fd=" << epollFD << " data="; |
|
388 |
for (char ch; read(epollFD, &ch, 1) > 0;) { |
|
389 |
std::stringstream msg; |
|
390 |
msg |
|
391 |
<< std::hex |
|
392 |
<< std::setfill('0') |
|
393 |
<< std::setw(2) |
|
394 |
<< (int) ch; |
|
395 |
logOutput << msg.str(); |
|
396 |
} |
|
397 |
logOutput << std::endl; |
|
398 |
||
7
e6065118326f
monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
399 |
} else if (epoll[epollEvent].data.fd == fileMonitor.getFD()) { |
e6065118326f
monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
400 |
std::cout << "FileMonitor event:" << std::endl; |
e6065118326f
monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
401 |
for (FileEvent fe; fileMonitor.readEvent(fe);) { |
8
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
402 |
logOutput << " " |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
403 |
<< " file=" << fe.fileName |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
404 |
<< " mask=" << fe.mask |
7
e6065118326f
monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
405 |
<< std::endl; |
8
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
406 |
try { |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
407 |
redraw |= reloadTexture(fe.fileName); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
408 |
redraw |= reloadShader(fe.fileName); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
409 |
setTitle(); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
410 |
} catch (const std::exception& e) { |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
411 |
setTitle("[ERROR]"); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
412 |
logOutput << "error while reloading '" |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
413 |
<< fe.fileName.c_str() |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
414 |
<< "': " << e.what() << std::endl; |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
415 |
} |
7
e6065118326f
monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
416 |
} |
0 | 417 |
} else { |
418 |
logOutput |
|
419 |
<< "error: event on an unexpected FD: " |
|
420 |
<< epoll[epollEvent].data.fd |
|
421 |
<< std::endl; |
|
422 |
} |
|
8
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
423 |
|
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
424 |
if (redraw) { |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
425 |
runShaders(); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
426 |
glXSwapBuffers(dpy, win); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
427 |
} |
0 | 428 |
} |
429 |
} |
|
430 |
} |
|
431 |
||
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
432 |
void Shark::Impl::clear() { |
0 | 433 |
glClearColor( |
434 |
(cfg.backgroundColor >> 16 & 0xFF) / 256., |
|
435 |
(cfg.backgroundColor >> 8 & 0xFF) / 256., |
|
436 |
(cfg.backgroundColor & 0xFF) / 256., |
|
437 |
1.0); |
|
438 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
439 |
} |
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
440 |
|
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
441 |
void Shark::Impl::runShaders() { |
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
442 |
shaderProgram->use(); |
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
443 |
checkError(&std::cerr); |
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
444 |
|
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
445 |
clear(); |
0 | 446 |
|
447 |
GLint viewport[4]; |
|
448 |
glGetIntegerv(GL_VIEWPORT, viewport); |
|
449 |
GLfloat width = viewport[2]; |
|
450 |
GLfloat height = viewport[3]; |
|
451 |
||
452 |
glm::mat4 projection = glm::perspective( |
|
453 |
glm::radians(ctx.fov), |
|
454 |
width / height, |
|
455 |
0.1f, 100.0f); |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
456 |
glUniformMatrix4fv(ProgAttr.uProjection, 1, GL_FALSE, &projection[0][0]); |
0 | 457 |
|
458 |
glm::mat4 view = glm::lookAt( |
|
459 |
ctx.cameraPos, |
|
460 |
ctx.cameraPos + ctx.cameraFront, |
|
461 |
ctx.cameraUp); |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
462 |
glUniformMatrix4fv(ProgAttr.uView, 1, GL_FALSE, &view[0][0]); |
0 | 463 |
|
464 |
// glBindVertexArray(vao); |
|
465 |
||
466 |
glm::mat4 model = glm::mat4(1.0f); // identity matrix |
|
467 |
// model = glm::translate(model, glm::vec3(0., 0., 0.)); |
|
468 |
// float angle = 20.0f; |
|
469 |
// glm::vec3 xxx = glm::vec3(1.0f, 0.3f, 0.5f); |
|
470 |
// model = glm::rotate(model, glm::radians(angle), xxx); |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
471 |
glUniformMatrix4fv(ProgAttr.uModel, 1, GL_FALSE, &model[0][0]); |
0 | 472 |
|
473 |
glDrawArrays(GL_TRIANGLES, 0, 2 * 3); // viz loadVertices() kde plníme data |
|
474 |
std::cerr << "GLSL: glDrawArrays()" << std::endl; |
|
475 |
} |
|
476 |
||
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
477 |
void Shark::Impl::log(LogLevel level, std::string message) { |
0 | 478 |
::log(logOutput, level, message); |
479 |
} |
|
480 |
||
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
481 |
int Shark::Impl::setNonBlocking(int fd) { |
0 | 482 |
int flags = fcntl(fd, F_GETFL, 0); |
483 |
fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
|
484 |
return fd; |
|
485 |
} |
|
486 |
||
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
487 |
void Shark::Impl::loadVertices() { |
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
488 |
for (int i = 0; i < textures.size(); i++) { |
19 | 489 |
std::shared_ptr<Texture> tex = textures[i]; |
490 |
GLfloat ratio = tex->getRatio(); |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
491 |
const std::vector<GLfloat> vertices = { |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
492 |
// Vertex XYZ Texture XY |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
493 |
-0.80f * ratio, +0.80f, +0.0, /**/ 0.0, 0.0, |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
494 |
+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0, |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
495 |
-0.80f * ratio, -0.80f, +0.0, /**/ 0.0, 1.0, |
0 | 496 |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
497 |
-0.80f * ratio, -0.80f, +0.0, /**/ 0.0, 1.0, |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
498 |
+0.80f * ratio, -0.80f, +0.0, /**/ 1.0, 1.0, |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
499 |
+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0, |
0 | 500 |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
501 |
// viz glDrawArrays(), kde vybereme počátek a počet hodnot |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
502 |
}; |
0 | 503 |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
504 |
// Vertex data: |
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
505 |
glVertexAttribPointer(ProgAttr.aVertexXYZ, 3, // vertex items |
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
506 |
GL_FLOAT, GL_FALSE, 5 * sizeof (float), |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
507 |
(void*) 0); |
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
508 |
glEnableVertexAttribArray(ProgAttr.aVertexXYZ); |
0 | 509 |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
510 |
// Texture positions: |
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
511 |
glVertexAttribPointer(ProgAttr.aTextureXY, 2, // texture items |
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
512 |
GL_FLOAT, GL_FALSE, 5 * sizeof (float), |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
513 |
(void*) (3 * sizeof (float))); |
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
514 |
glEnableVertexAttribArray(ProgAttr.aTextureXY); |
0 | 515 |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
516 |
glBufferData(GL_ARRAY_BUFFER, |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
517 |
vertices.size() * sizeof (vertices[0]), |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
518 |
vertices.data(), |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
519 |
GL_STATIC_DRAW); |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
520 |
// GL_STATIC_DRAW: |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
521 |
// The vertex data will be uploaded once |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
522 |
// and drawn many times(e.g. the world). |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
523 |
// GL_DYNAMIC_DRAW: |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
524 |
// The vertex data will be created once, changed from |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
525 |
// time to time, but drawn many times more than that. |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
526 |
// GL_STREAM_DRAW: |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
527 |
// The vertex data will be uploaded once and drawn once. |
0 | 528 |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
529 |
// see also glBindBuffer(GL_ARRAY_BUFFER, vbo); where we set current VBO |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
530 |
} |
0 | 531 |
} |
532 |
||
11
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
533 |
const std::string |
19 | 534 |
Shark::Impl::getDefaultFile(const std::string& relativePath) { |
11
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
535 |
const char* envName = "SHADER_SHARK_DATA_DIR"; |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
536 |
const char* envValue = ::getenv(envName); |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
537 |
if (envValue) { |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
538 |
return std::string(envValue) + "/" + relativePath; |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
539 |
} else { |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
540 |
throw std::invalid_argument(std::string("Configure $") + envName |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
541 |
+ " in order to use defaults" |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
542 |
" or specify textures and shaders as parameters"); |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
543 |
} |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
544 |
} |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
545 |
|
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
546 |
void Shark::Impl::loadTextures() { |
11
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
547 |
// Load default texture if there is no configured: |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
548 |
if (cfg.textures.empty()) |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
549 |
cfg.textures.push_back({getDefaultFile("textures/default.png")}); |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
550 |
|
0 | 551 |
for (const Configuration::Texture& tex : cfg.textures) { |
19 | 552 |
std::shared_ptr<ImageLoader::ImageBuffer> |
553 |
img(imageLoader.loadImage(MappedFile(tex.fileName))); |
|
554 |
textures.push_back(std::make_shared<Texture>( |
|
555 |
img->width, img->height, *img, tex.fileName)); |
|
7
e6065118326f
monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
556 |
watchedFiles.push_back(fileMonitor.watch(tex.fileName)); |
0 | 557 |
// TODO: review texture loading and binding |
558 |
// works even without this - default texture |
|
559 |
// glUniform1i(ProgAttr.jazz, jazz); |
|
560 |
// checkError(&std::cerr); |
|
561 |
} |
|
562 |
} |
|
563 |
||
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
564 |
bool Shark::Impl::reloadTexture(const std::string& fileName) { |
19 | 565 |
for (std::shared_ptr<Texture> tex : textures) { |
566 |
if (tex->getFileName() == fileName) { |
|
567 |
std::shared_ptr<ImageLoader::ImageBuffer> |
|
568 |
img(imageLoader.loadImage(MappedFile(fileName))); |
|
569 |
tex->update(img->width, img->height, *img); |
|
12
076e3b2d97ac
monitor texture and shader file writes using inotify: reload textures
František Kučera <franta-hg@frantovo.cz>
parents:
11
diff
changeset
|
570 |
loadVertices(); |
8
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
571 |
return true; |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
572 |
} |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
573 |
} |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
574 |
return false; |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
575 |
} |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
576 |
|
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
577 |
std::shared_ptr<Program> Shark::Impl::loadShaders() { |
0 | 578 |
try { |
579 |
// Vertex Array Object (VAO) |
|
580 |
GLuint vao; |
|
581 |
glGenVertexArrays(1, &vao); |
|
582 |
glBindVertexArray(vao); |
|
583 |
// VAO - something like context for bound data/variables |
|
584 |
// We can switch multiple VAOs. VAO can contain multiple VBOs. |
|
585 |
// See also https://stackoverflow.com/questions/11821336/ |
|
586 |
// what-are-vertex-array-objects |
|
587 |
||
588 |
// Vertex Buffer Object (VBO): |
|
589 |
GLuint vbo; |
|
590 |
glGenBuffers(1, &vbo); |
|
591 |
glBindBuffer(GL_ARRAY_BUFFER, vbo); |
|
592 |
||
11
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
593 |
{ |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
594 |
// Load default shaders if there are no configured: |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
595 |
int vc = 0; |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
596 |
int fc = 0; |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
597 |
auto& ss = cfg.shaders; |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
598 |
for (const auto& s : ss) if (s.type == "vertex") vc++; |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
599 |
for (const auto& s : ss) if (s.type == "fragment") fc++; |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
600 |
auto& d = getDefaultFile; |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
601 |
if (vc == 0) ss.push_back({d("shaders/default.vert"), "vertex"}); |
0aeedc35ebed
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents:
10
diff
changeset
|
602 |
if (fc == 0) ss.push_back({d("shaders/default.frag"), "fragment"}); |
6
fd93a46db15b
support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
603 |
} |
0 | 604 |
|
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
605 |
std::shared_ptr<Program> program = std::make_shared<Program>(); |
0 | 606 |
|
607 |
// glBindFragDataLocation(program, 0, "outColor"); |
|
608 |
// glBindAttribLocation(program, LOC.input, "vertices"); |
|
609 |
||
6
fd93a46db15b
support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
610 |
for (const Configuration::Shader definition : cfg.shaders) { |
fd93a46db15b
support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
611 |
Shader::Type type; |
fd93a46db15b
support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
612 |
std::string fileName = definition.fileName; |
fd93a46db15b
support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
613 |
if (definition.type == "fragment") type = Shader::Type::FRAGMENT; |
fd93a46db15b
support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
614 |
else if (definition.type == "vertex") type = Shader::Type::VERTEX; |
fd93a46db15b
support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
615 |
else throw std::invalid_argument("unsupported shader type"); |
fd93a46db15b
support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
616 |
|
0 | 617 |
MappedFile file(fileName); |
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
618 |
std::shared_ptr<Shader> shader = std::make_shared<Shader>( |
6
fd93a46db15b
support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
619 |
type, file, fileName); |
0 | 620 |
|
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
621 |
program->attachShader(*shader.get()); |
0 | 622 |
shaders.push_back(shader); |
7
e6065118326f
monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
623 |
watchedFiles.push_back(fileMonitor.watch(fileName)); |
0 | 624 |
std::cerr << "GLSL loaded: " << fileName.c_str() << std::endl; |
8
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
625 |
// We may detach and delete shaders, |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
626 |
// but our shaders are small, so we keep them for later reloading. |
0 | 627 |
} |
628 |
||
629 |
// GLSL compiler does very efficient / aggressive optimization. |
|
630 |
// Attributes and uniforms that are not used in the shader are deleted. |
|
631 |
// And even if we e.g. read color from a texture and overwrite it, |
|
632 |
// the variable is still deleted and considered „inactive“. |
|
633 |
// Functions glGetAttribLocation() and glGetUniformLocation() return -1. |
|
634 |
||
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
635 |
program->link(); |
0 | 636 |
|
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
637 |
ProgAttr.aVertexXYZ = program->getAttribLocation("aVertexXYZ"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
638 |
ProgAttr.aTextureXY = program->getAttribLocation("aTextureXY"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
639 |
ProgAttr.uModel = program->getUniformLocation("uModel"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
640 |
ProgAttr.uView = program->getUniformLocation("uView"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
641 |
ProgAttr.uProjection = program->getUniformLocation("uProjection"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
642 |
ProgAttr.uTexture = program->getUniformLocation("uTexture"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
643 |
ProgAttr.fColor = program->getFragDataLocation("fColor"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
644 |
program->bindFragDataLocation("fColor", ProgAttr.fColor); |
0 | 645 |
// listVariables(program); |
646 |
std::cerr << "GLSL shader count: " << shaders.size() << std::endl; |
|
647 |
return program; |
|
648 |
} catch (const std::exception& e) { |
|
649 |
std::cerr << "Error while loading shaders: " << e.what() << std::endl; |
|
650 |
} catch (...) { |
|
651 |
std::cerr << "Error while loading shaders: unknown" << std::endl; |
|
652 |
} |
|
653 |
throw std::logic_error("GLSL: loadShaders() failed"); |
|
654 |
} |
|
655 |
||
10
8382173bfc35
private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
656 |
bool Shark::Impl::reloadShader(const std::string& fileName) { |
8
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
657 |
for (auto shader : shaders) { |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
658 |
if (shader->getFileName() == fileName) { |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
659 |
shader->update(MappedFile(fileName)); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
660 |
shaderProgram->link(); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
661 |
return true; |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
662 |
} |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
663 |
} |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
664 |
return false; |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
665 |
} |