|
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" |
|
37 |
|
38 class Shark { |
|
39 private: |
|
40 |
|
41 struct { |
|
42 GLint vertexXYZ = -2; |
|
43 GLint textureXY = -2; |
|
44 |
|
45 GLint color = -2; |
|
46 |
|
47 GLint model = -2; |
|
48 GLint view = -2; |
|
49 GLint projection = -2; |
|
50 GLint jazz = -2; |
|
51 } ProgAttr; |
|
52 |
|
53 struct { |
|
54 float yaw = -90.f; |
|
55 float pitch = 0.f; |
|
56 float roll = 0.f; |
|
57 float fov = 45.0f; |
|
58 glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f); |
|
59 glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); |
|
60 glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); |
|
61 |
|
62 void adjustFov(float diff) { |
|
63 fov += diff; |
|
64 if (fov < 1.0f) fov = 1.0f; |
|
65 else if (fov > 120.0f) fov = 120.0f; |
|
66 std::cerr << "field of view: " << fov << " °" << std::endl; |
|
67 } |
|
68 |
|
69 void moveForward(const float cameraSpeed) { |
|
70 cameraPos += cameraSpeed * cameraFront; |
|
71 } |
|
72 |
|
73 void moveBackward(const float cameraSpeed) { |
|
74 cameraPos -= cameraSpeed * cameraFront; |
|
75 } |
|
76 |
|
77 void moveLeft(const float cameraSpeed) { |
|
78 cameraPos -= glm::normalize( |
|
79 glm::cross(cameraFront, cameraUp)) * cameraSpeed; |
|
80 } |
|
81 |
|
82 void moveRight(const float cameraSpeed) { |
|
83 cameraPos += glm::normalize( |
|
84 glm::cross(cameraFront, cameraUp)) * cameraSpeed; |
|
85 } |
|
86 |
|
87 void moveUp(const float cameraSpeed) { |
|
88 cameraPos += cameraSpeed * glm::normalize(cameraUp); |
|
89 } |
|
90 |
|
91 void moveDown(const float cameraSpeed) { |
|
92 cameraPos -= cameraSpeed * glm::normalize(cameraUp); |
|
93 } |
|
94 |
|
95 void updateCameraFrontAndUp() { |
|
96 std::cerr << "--- updateCameraFrontAndUp() --------" << std::endl; |
|
97 dump("pitch, yaw, roll", glm::vec3(pitch, yaw, roll)); |
|
98 dump("cameraPos", cameraPos); |
|
99 dump("cameraFront", cameraFront); |
|
100 const auto pitchR = glm::radians(pitch); // around X axis |
|
101 const auto yawR = glm::radians(yaw); // around Y axis |
|
102 const auto rollR = glm::radians(roll); // around Z axis |
|
103 |
|
104 cameraFront.x = cos(pitchR) * cos(yawR); |
|
105 cameraFront.y = sin(pitchR); |
|
106 cameraFront.z = cos(pitchR) * sin(yawR); |
|
107 cameraFront = glm::normalize(cameraFront); |
|
108 dump("cameraFront", cameraFront); |
|
109 dump("cameraUp", cameraUp); |
|
110 |
|
111 // TODO: review ROLL rotation and default angle |
|
112 glm::mat4 rollMatrix = glm::rotate( |
|
113 glm::mat4(1.0f), rollR, cameraFront); |
|
114 cameraUp = glm::mat3(rollMatrix) * glm::vec3(0., 1., 0.); |
|
115 dump("cameraUp", cameraUp); |
|
116 std::cerr << "-------------------------------------" << std::endl; |
|
117 } |
|
118 |
|
119 void limitPitch() { |
|
120 if (pitch > +89.0f) pitch = +89.0f; |
|
121 if (pitch < -89.0f) pitch = -89.0f; |
|
122 } |
|
123 |
|
124 void turnLeft(const float angleSpeed) { |
|
125 yaw -= angleSpeed; |
|
126 updateCameraFrontAndUp(); |
|
127 } |
|
128 |
|
129 void turnRight(const float angleSpeed) { |
|
130 yaw += angleSpeed; |
|
131 updateCameraFrontAndUp(); |
|
132 } |
|
133 |
|
134 void turnUp(const float angleSpeed) { |
|
135 pitch += angleSpeed; |
|
136 limitPitch(); |
|
137 updateCameraFrontAndUp(); |
|
138 } |
|
139 |
|
140 void turnDown(const float angleSpeed) { |
|
141 pitch -= angleSpeed; |
|
142 limitPitch(); |
|
143 updateCameraFrontAndUp(); |
|
144 } |
|
145 |
|
146 void rollLeft(const float angleSpeed) { |
|
147 roll += angleSpeed; |
|
148 updateCameraFrontAndUp(); |
|
149 } |
|
150 |
|
151 void rollRight(const float angleSpeed) { |
|
152 roll -= angleSpeed; |
|
153 updateCameraFrontAndUp(); |
|
154 } |
|
155 |
|
156 } initialCtx, ctx; |
|
157 |
|
158 const struct { |
|
159 int width = 640; |
|
160 int height = 398; |
|
161 GLfloat ratio = (GLfloat) width / (GLfloat) height; |
|
162 } TEX; |
|
163 |
|
164 Configuration cfg; |
|
165 std::ostream& logOutput = std::cerr; |
|
166 |
|
167 void renderImmediateMode(); |
|
168 void runShaders(GLuint program); |
|
169 Window getRootWindow(Window defaultValue); |
|
170 void log(LogLevel level, std::string message); |
|
171 int setNonBlocking(int fd); |
|
172 void loadVertices(); |
|
173 GLuint loadTexture(const std::string& fileName, int width, int height); |
|
174 void loadTextures(GLuint shaderProgram); |
|
175 GLuint loadShaders(); |
|
176 public: |
|
177 Shark(const Configuration& cfg); |
|
178 virtual ~Shark(); |
|
179 Shark(const Shark&) = delete; |
|
180 Shark& operator=(const Shark&) = delete; |
|
181 void run(); |
|
182 }; |