author | František Kučera <franta-hg@frantovo.cz> |
Sat, 02 Dec 2023 15:43:51 +0100 | |
branch | v_0 |
changeset 8 | 80ad08521091 |
parent 7 | e6065118326f |
child 10 | 8382173bfc35 |
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 |
||
18 |
#pragma once |
|
19 |
||
20 |
#include <iostream> |
|
21 |
#include <iomanip> |
|
22 |
#include <string> |
|
23 |
#include <memory> |
|
24 |
#include <functional> |
|
25 |
#include <sstream> |
|
26 |
#include <vector> |
|
27 |
#include <chrono> |
|
28 |
#include <unistd.h> |
|
29 |
#include <sys/stat.h> |
|
30 |
||
31 |
#include "Configuration.h" |
|
32 |
#include "x11.h" |
|
33 |
#include "opengl.h" |
|
34 |
#include "EPoll.h" |
|
35 |
#include "Logger.h" |
|
36 |
#include "MappedFile.h" |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
37 |
#include "ImageLoader.h" |
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
38 |
#include "Shader.h" |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
39 |
#include "Program.h" |
7
e6065118326f
monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
40 |
#include "FileMonitor.h" |
0 | 41 |
|
42 |
class Shark { |
|
43 |
private: |
|
44 |
||
45 |
struct { |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
46 |
GLint aVertexXYZ = -2; |
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
47 |
GLint aTextureXY = -2; |
0 | 48 |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
49 |
GLint fColor = -2; |
0 | 50 |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
51 |
GLint uModel = -2; |
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
52 |
GLint uView = -2; |
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
53 |
GLint uProjection = -2; |
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
54 |
GLint uTexture = -2; |
0 | 55 |
} ProgAttr; |
56 |
||
57 |
struct { |
|
58 |
float yaw = -90.f; |
|
59 |
float pitch = 0.f; |
|
60 |
float roll = 0.f; |
|
61 |
float fov = 45.0f; |
|
62 |
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); |
|
63 |
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); |
|
64 |
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); |
|
65 |
||
66 |
void adjustFov(float diff) { |
|
67 |
fov += diff; |
|
68 |
if (fov < 1.0f) fov = 1.0f; |
|
69 |
else if (fov > 120.0f) fov = 120.0f; |
|
70 |
std::cerr << "field of view: " << fov << " °" << std::endl; |
|
71 |
} |
|
72 |
||
73 |
void moveForward(const float cameraSpeed) { |
|
74 |
cameraPos += cameraSpeed * cameraFront; |
|
75 |
} |
|
76 |
||
77 |
void moveBackward(const float cameraSpeed) { |
|
78 |
cameraPos -= cameraSpeed * cameraFront; |
|
79 |
} |
|
80 |
||
81 |
void moveLeft(const float cameraSpeed) { |
|
82 |
cameraPos -= glm::normalize( |
|
83 |
glm::cross(cameraFront, cameraUp)) * cameraSpeed; |
|
84 |
} |
|
85 |
||
86 |
void moveRight(const float cameraSpeed) { |
|
87 |
cameraPos += glm::normalize( |
|
88 |
glm::cross(cameraFront, cameraUp)) * cameraSpeed; |
|
89 |
} |
|
90 |
||
91 |
void moveUp(const float cameraSpeed) { |
|
92 |
cameraPos += cameraSpeed * glm::normalize(cameraUp); |
|
93 |
} |
|
94 |
||
95 |
void moveDown(const float cameraSpeed) { |
|
96 |
cameraPos -= cameraSpeed * glm::normalize(cameraUp); |
|
97 |
} |
|
98 |
||
99 |
void updateCameraFrontAndUp() { |
|
100 |
std::cerr << "--- updateCameraFrontAndUp() --------" << std::endl; |
|
101 |
dump("pitch, yaw, roll", glm::vec3(pitch, yaw, roll)); |
|
102 |
dump("cameraPos", cameraPos); |
|
103 |
dump("cameraFront", cameraFront); |
|
104 |
const auto pitchR = glm::radians(pitch); // around X axis |
|
105 |
const auto yawR = glm::radians(yaw); // around Y axis |
|
106 |
const auto rollR = glm::radians(roll); // around Z axis |
|
107 |
||
108 |
cameraFront.x = cos(pitchR) * cos(yawR); |
|
109 |
cameraFront.y = sin(pitchR); |
|
110 |
cameraFront.z = cos(pitchR) * sin(yawR); |
|
111 |
cameraFront = glm::normalize(cameraFront); |
|
112 |
dump("cameraFront", cameraFront); |
|
113 |
dump("cameraUp", cameraUp); |
|
114 |
||
115 |
// TODO: review ROLL rotation and default angle |
|
116 |
glm::mat4 rollMatrix = glm::rotate( |
|
117 |
glm::mat4(1.0f), rollR, cameraFront); |
|
118 |
cameraUp = glm::mat3(rollMatrix) * glm::vec3(0., 1., 0.); |
|
119 |
dump("cameraUp", cameraUp); |
|
120 |
std::cerr << "-------------------------------------" << std::endl; |
|
121 |
} |
|
122 |
||
123 |
void limitPitch() { |
|
124 |
if (pitch > +89.0f) pitch = +89.0f; |
|
125 |
if (pitch < -89.0f) pitch = -89.0f; |
|
126 |
} |
|
127 |
||
128 |
void turnLeft(const float angleSpeed) { |
|
129 |
yaw -= angleSpeed; |
|
130 |
updateCameraFrontAndUp(); |
|
131 |
} |
|
132 |
||
133 |
void turnRight(const float angleSpeed) { |
|
134 |
yaw += angleSpeed; |
|
135 |
updateCameraFrontAndUp(); |
|
136 |
} |
|
137 |
||
138 |
void turnUp(const float angleSpeed) { |
|
139 |
pitch += angleSpeed; |
|
140 |
limitPitch(); |
|
141 |
updateCameraFrontAndUp(); |
|
142 |
} |
|
143 |
||
144 |
void turnDown(const float angleSpeed) { |
|
145 |
pitch -= angleSpeed; |
|
146 |
limitPitch(); |
|
147 |
updateCameraFrontAndUp(); |
|
148 |
} |
|
149 |
||
150 |
void rollLeft(const float angleSpeed) { |
|
151 |
roll += angleSpeed; |
|
152 |
updateCameraFrontAndUp(); |
|
153 |
} |
|
154 |
||
155 |
void rollRight(const float angleSpeed) { |
|
156 |
roll -= angleSpeed; |
|
157 |
updateCameraFrontAndUp(); |
|
158 |
} |
|
159 |
||
160 |
} initialCtx, ctx; |
|
161 |
||
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
162 |
class Texture { |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
163 |
public: |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
164 |
GLuint id; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
165 |
std::string fileName; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
166 |
int width; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
167 |
int height; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
168 |
|
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
169 |
GLfloat getRatio() const { |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
170 |
return (GLfloat) width / (GLfloat) height; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
171 |
} |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
172 |
}; |
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
173 |
|
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
174 |
Display* dpy; |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
175 |
Window win; |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
176 |
XVisualInfo* vi; |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
177 |
GLXContext glc; |
7
e6065118326f
monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
178 |
|
e6065118326f
monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
179 |
FileMonitor fileMonitor; |
e6065118326f
monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
180 |
std::vector<WatchedFile> watchedFiles; |
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
181 |
ImageLoader imageLoader; |
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
182 |
std::vector<std::shared_ptr<Shader>> shaders; |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
183 |
std::shared_ptr<Program> shaderProgram; |
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
184 |
std::vector<Texture> textures; |
0 | 185 |
|
186 |
Configuration cfg; |
|
187 |
std::ostream& logOutput = std::cerr; |
|
188 |
||
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
189 |
void clear(); |
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
190 |
void runShaders(); |
0 | 191 |
Window getRootWindow(Window defaultValue); |
192 |
void log(LogLevel level, std::string message); |
|
193 |
int setNonBlocking(int fd); |
|
194 |
void loadVertices(); |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
195 |
Texture loadTexture(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
|
196 |
bool reloadTexture(const std::string& fileName); |
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
197 |
void loadTextures(); |
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
198 |
std::shared_ptr<Program> loadShaders(); |
8
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
199 |
bool reloadShader(const std::string& fileName); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
200 |
void setTitle(const std::string& suffix = ""); |
0 | 201 |
public: |
202 |
Shark(const Configuration& cfg); |
|
203 |
virtual ~Shark(); |
|
204 |
Shark(const Shark&) = delete; |
|
205 |
Shark& operator=(const Shark&) = delete; |
|
206 |
void run(); |
|
207 |
}; |