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