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