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