author | František Kučera <franta-hg@frantovo.cz> |
Wed, 29 Nov 2023 01:11:19 +0100 | |
branch | v_0 |
changeset 2 | 3faef2f5128e |
parent 1 | fb65455622b9 |
child 3 | 48dc4ae894b0 |
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" |
0 | 38 |
|
39 |
class Shark { |
|
40 |
private: |
|
41 |
||
42 |
struct { |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
43 |
GLint aVertexXYZ = -2; |
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
44 |
GLint aTextureXY = -2; |
0 | 45 |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
46 |
GLint fColor = -2; |
0 | 47 |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
48 |
GLint uModel = -2; |
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
49 |
GLint uView = -2; |
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
50 |
GLint uProjection = -2; |
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
51 |
GLint uTexture = -2; |
0 | 52 |
} ProgAttr; |
53 |
||
54 |
struct { |
|
55 |
float yaw = -90.f; |
|
56 |
float pitch = 0.f; |
|
57 |
float roll = 0.f; |
|
58 |
float fov = 45.0f; |
|
59 |
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); |
|
60 |
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); |
|
61 |
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); |
|
62 |
||
63 |
void adjustFov(float diff) { |
|
64 |
fov += diff; |
|
65 |
if (fov < 1.0f) fov = 1.0f; |
|
66 |
else if (fov > 120.0f) fov = 120.0f; |
|
67 |
std::cerr << "field of view: " << fov << " °" << std::endl; |
|
68 |
} |
|
69 |
||
70 |
void moveForward(const float cameraSpeed) { |
|
71 |
cameraPos += cameraSpeed * cameraFront; |
|
72 |
} |
|
73 |
||
74 |
void moveBackward(const float cameraSpeed) { |
|
75 |
cameraPos -= cameraSpeed * cameraFront; |
|
76 |
} |
|
77 |
||
78 |
void moveLeft(const float cameraSpeed) { |
|
79 |
cameraPos -= glm::normalize( |
|
80 |
glm::cross(cameraFront, cameraUp)) * cameraSpeed; |
|
81 |
} |
|
82 |
||
83 |
void moveRight(const float cameraSpeed) { |
|
84 |
cameraPos += glm::normalize( |
|
85 |
glm::cross(cameraFront, cameraUp)) * cameraSpeed; |
|
86 |
} |
|
87 |
||
88 |
void moveUp(const float cameraSpeed) { |
|
89 |
cameraPos += cameraSpeed * glm::normalize(cameraUp); |
|
90 |
} |
|
91 |
||
92 |
void moveDown(const float cameraSpeed) { |
|
93 |
cameraPos -= cameraSpeed * glm::normalize(cameraUp); |
|
94 |
} |
|
95 |
||
96 |
void updateCameraFrontAndUp() { |
|
97 |
std::cerr << "--- updateCameraFrontAndUp() --------" << std::endl; |
|
98 |
dump("pitch, yaw, roll", glm::vec3(pitch, yaw, roll)); |
|
99 |
dump("cameraPos", cameraPos); |
|
100 |
dump("cameraFront", cameraFront); |
|
101 |
const auto pitchR = glm::radians(pitch); // around X axis |
|
102 |
const auto yawR = glm::radians(yaw); // around Y axis |
|
103 |
const auto rollR = glm::radians(roll); // around Z axis |
|
104 |
||
105 |
cameraFront.x = cos(pitchR) * cos(yawR); |
|
106 |
cameraFront.y = sin(pitchR); |
|
107 |
cameraFront.z = cos(pitchR) * sin(yawR); |
|
108 |
cameraFront = glm::normalize(cameraFront); |
|
109 |
dump("cameraFront", cameraFront); |
|
110 |
dump("cameraUp", cameraUp); |
|
111 |
||
112 |
// TODO: review ROLL rotation and default angle |
|
113 |
glm::mat4 rollMatrix = glm::rotate( |
|
114 |
glm::mat4(1.0f), rollR, cameraFront); |
|
115 |
cameraUp = glm::mat3(rollMatrix) * glm::vec3(0., 1., 0.); |
|
116 |
dump("cameraUp", cameraUp); |
|
117 |
std::cerr << "-------------------------------------" << std::endl; |
|
118 |
} |
|
119 |
||
120 |
void limitPitch() { |
|
121 |
if (pitch > +89.0f) pitch = +89.0f; |
|
122 |
if (pitch < -89.0f) pitch = -89.0f; |
|
123 |
} |
|
124 |
||
125 |
void turnLeft(const float angleSpeed) { |
|
126 |
yaw -= angleSpeed; |
|
127 |
updateCameraFrontAndUp(); |
|
128 |
} |
|
129 |
||
130 |
void turnRight(const float angleSpeed) { |
|
131 |
yaw += angleSpeed; |
|
132 |
updateCameraFrontAndUp(); |
|
133 |
} |
|
134 |
||
135 |
void turnUp(const float angleSpeed) { |
|
136 |
pitch += angleSpeed; |
|
137 |
limitPitch(); |
|
138 |
updateCameraFrontAndUp(); |
|
139 |
} |
|
140 |
||
141 |
void turnDown(const float angleSpeed) { |
|
142 |
pitch -= angleSpeed; |
|
143 |
limitPitch(); |
|
144 |
updateCameraFrontAndUp(); |
|
145 |
} |
|
146 |
||
147 |
void rollLeft(const float angleSpeed) { |
|
148 |
roll += angleSpeed; |
|
149 |
updateCameraFrontAndUp(); |
|
150 |
} |
|
151 |
||
152 |
void rollRight(const float angleSpeed) { |
|
153 |
roll -= angleSpeed; |
|
154 |
updateCameraFrontAndUp(); |
|
155 |
} |
|
156 |
||
157 |
} initialCtx, ctx; |
|
158 |
||
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
159 |
class Texture { |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
160 |
public: |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
161 |
GLuint id; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
162 |
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
|
163 |
int width; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
164 |
int height; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
165 |
|
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
166 |
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
|
167 |
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
|
168 |
} |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
169 |
}; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
170 |
|
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
171 |
ImageLoader imageLoader; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
172 |
|
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
173 |
std::vector<Texture> textures; |
0 | 174 |
|
175 |
Configuration cfg; |
|
176 |
std::ostream& logOutput = std::cerr; |
|
177 |
||
178 |
void renderImmediateMode(); |
|
179 |
void runShaders(GLuint program); |
|
180 |
Window getRootWindow(Window defaultValue); |
|
181 |
void log(LogLevel level, std::string message); |
|
182 |
int setNonBlocking(int fd); |
|
183 |
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
|
184 |
Texture loadTexture(const std::string& fileName); |
0 | 185 |
void loadTextures(GLuint shaderProgram); |
186 |
GLuint loadShaders(); |
|
187 |
public: |
|
188 |
Shark(const Configuration& cfg); |
|
189 |
virtual ~Shark(); |
|
190 |
Shark(const Shark&) = delete; |
|
191 |
Shark& operator=(const Shark&) = delete; |
|
192 |
void run(); |
|
193 |
}; |