author | František Kučera <franta-hg@frantovo.cz> |
Fri, 01 Dec 2023 20:25:49 +0100 | |
branch | v_0 |
changeset 5 | ee4ba9f5a053 |
parent 3 | 48dc4ae894b0 |
child 6 | fd93a46db15b |
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 |
||
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
18 |
#include <memory> |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
19 |
|
0 | 20 |
#include "Shark.h" |
21 |
||
22 |
Shark::Shark(const Configuration& configuration) : |
|
23 |
cfg(configuration) { |
|
24 |
} |
|
25 |
||
26 |
Shark::~Shark() { |
|
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
27 |
// TODO: more SBRM |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
28 |
shaders.clear(); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
29 |
shaderProgram = nullptr; |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
30 |
XFree(vi); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
31 |
// for (auto page : pdfTextures) glDeleteTextures(1, &page.texture); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
32 |
glXMakeCurrent(dpy, None, NULL); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
33 |
glXDestroyContext(dpy, glc); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
34 |
XDestroyWindow(dpy, win); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
35 |
XCloseDisplay(dpy); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
36 |
// std::cerr << "~Shark()" << std::endl; |
0 | 37 |
} |
38 |
||
39 |
void Shark::run() { |
|
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
40 |
dpy = XOpenDisplay(NULL); |
0 | 41 |
|
42 |
if (dpy == NULL) throw std::logic_error("Unable to connect to X server"); |
|
43 |
||
44 |
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
|
45 |
vi = glXChooseVisual(dpy, 0, att); |
0 | 46 |
Window root = DefaultRootWindow(dpy); |
47 |
Window parent = cfg.rootWindow ? cfg.rootWindow : root; |
|
48 |
||
49 |
XSetWindowAttributes swa; |
|
50 |
swa.colormap = XCreateColormap(dpy, parent, vi->visual, AllocNone); |
|
51 |
swa.event_mask = ExposureMask | KeyPressMask | PointerMotionMask |
|
52 |
| ButtonPressMask |
|
53 |
| StructureNotifyMask; |
|
54 |
||
55 |
bool full = false; |
|
56 |
unsigned int width = 1600; |
|
57 |
unsigned int height = 1200; |
|
58 |
if (parent != root) { |
|
59 |
XWindowAttributes parentAttr; |
|
60 |
XGetWindowAttributes(dpy, parent, &parentAttr); |
|
61 |
width = parentAttr.width; |
|
62 |
height = parentAttr.height; |
|
63 |
} |
|
64 |
||
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
65 |
win = XCreateWindow( |
0 | 66 |
dpy, parent, 0, 0, width, height, 0, |
67 |
vi->depth, InputOutput, vi->visual, |
|
68 |
CWColormap | CWEventMask, &swa); |
|
69 |
||
70 |
XMapWindow(dpy, win); |
|
71 |
XStoreName(dpy, win, "ShaderShark"); |
|
72 |
setX11PID(dpy, win); |
|
73 |
// XSetWindowBackground(dpy, win, 0) vs. glClearColor() |
|
74 |
||
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
75 |
glc = glXCreateContext(dpy, vi, NULL, GL_TRUE); |
0 | 76 |
glXMakeCurrent(dpy, win, glc); |
77 |
||
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
78 |
clear(); |
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
79 |
glXSwapBuffers(dpy, win); |
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
80 |
|
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
81 |
|
0 | 82 |
// Load GLSL shaders: |
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
83 |
shaderProgram = loadShaders(); |
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
84 |
loadTextures(); |
0 | 85 |
loadVertices(); |
86 |
||
87 |
auto toggleFullscreen = [&]() { |
|
88 |
full = setFullscreen(dpy, win, !full); |
|
89 |
}; |
|
90 |
||
91 |
auto resetView = [&]() { |
|
92 |
ctx = initialCtx; |
|
93 |
ctx.updateCameraFrontAndUp(); |
|
94 |
}; |
|
95 |
||
96 |
// root can reize our window |
|
97 |
// or we can listen to root resize and then resize our window ourselves |
|
98 |
bool listenToRootResizes = true; |
|
99 |
if (listenToRootResizes) XSelectInput(dpy, parent, StructureNotifyMask); |
|
100 |
||
101 |
bool keepRunningX11 = true; |
|
102 |
int x11fd = XConnectionNumber(dpy); |
|
103 |
EPoll epoll; |
|
104 |
epoll.add(x11fd); |
|
105 |
try { |
|
106 |
epoll.add(setNonBlocking(STDIN_FILENO)); |
|
107 |
} catch (const EPoll::Exception& e) { |
|
108 |
logOutput << "Will not monitor events on STDIN: " << e.what() << "\n"; |
|
109 |
} |
|
110 |
||
111 |
// 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
|
112 |
runShaders(); |
0 | 113 |
glXSwapBuffers(dpy, win); |
114 |
||
115 |
for (XEvent xev; keepRunningX11;) { |
|
116 |
int epollEventCount = epoll.wait(); |
|
117 |
//std::cout << "trace: epoll.wait() = " << epollEventCount << std::endl; |
|
118 |
for (int epollEvent = 0; epollEvent < epollEventCount; epollEvent++) { |
|
119 |
if (epoll[epollEvent].data.fd == x11fd) { |
|
120 |
if (!XPending(dpy)) { |
|
121 |
// otherwise STDIN events are held until the first X11 event |
|
122 |
logOutput << "trace: no pending X11 event" << std::endl; |
|
123 |
break; |
|
124 |
} |
|
125 |
XWindowAttributes gwa; |
|
126 |
XNextEvent(dpy, &xev); |
|
127 |
bool redraw = false; |
|
128 |
||
129 |
if (xev.type == Expose) { |
|
130 |
std::cout << "XEvent: Expose" << std::endl; |
|
131 |
XGetWindowAttributes(dpy, win, &gwa); |
|
132 |
glViewport(0, 0, gwa.width, gwa.height); |
|
133 |
redraw = true; |
|
134 |
} else if (xev.type == KeyPress) { |
|
135 |
DecodedKey key = decodeKeycode(dpy, xev.xkey.keycode); |
|
136 |
std::cout << "XEvent: KeyPress:" |
|
137 |
<< " keycode=" << key.code |
|
138 |
<< " key=" << key.name |
|
139 |
<< std::endl; |
|
140 |
||
141 |
const float cSp = 0.05f; // camera speed |
|
142 |
const float aSp = 5.f; // angle speed |
|
143 |
||
144 |
if (key.matches(XK_q, XK_Escape)) keepRunningX11 = false; |
|
145 |
else if (key.matches(XK_Left, XK_s)) ctx.turnLeft(aSp); |
|
146 |
else if (key.matches(XK_Right, XK_f)) ctx.turnRight(aSp); |
|
147 |
else if (key.matches(XK_Up, XK_e)) ctx.moveForward(cSp); |
|
148 |
else if (key.matches(XK_Down, XK_d)) ctx.moveBackward(cSp); |
|
149 |
else if (key.matches(XK_w)) ctx.rollLeft(aSp); |
|
150 |
else if (key.matches(XK_r)) ctx.rollRight(aSp); |
|
151 |
else if (key.matches(XK_t)) ctx.turnUp(aSp); |
|
152 |
else if (key.matches(XK_g)) ctx.turnDown(aSp); |
|
153 |
else if (key.matches(XK_m)) ctx.moveLeft(cSp); |
|
154 |
else if (key.matches(XK_comma)) ctx.moveRight(cSp); |
|
155 |
else if (key.matches(XK_l)) ctx.moveUp(cSp); |
|
156 |
else if (key.matches(XK_period)) ctx.moveDown(cSp); |
|
157 |
else if (key.matches(XK_j)) ctx.moveLeft(cSp * 5); |
|
158 |
else if (key.matches(XK_k)) ctx.moveRight(cSp * 5); |
|
159 |
else if (key.matches(XK_u)) ctx.moveLeft(cSp * 10); |
|
160 |
else if (key.matches(XK_i)) ctx.moveRight(cSp * 10); |
|
161 |
else if (key.matches(XK_x)) resetView(); |
|
162 |
else if (key.matches(XK_F11, XK_y)) toggleFullscreen(); |
|
163 |
redraw = true; |
|
164 |
} else if (xev.type == ButtonPress) { |
|
165 |
std::cout << "XEvent: ButtonPress:" |
|
166 |
<< " button=" << xev.xbutton.button |
|
167 |
<< std::endl; |
|
168 |
if (xev.xbutton.button == 1); |
|
169 |
else if (xev.xbutton.button == 4) ctx.adjustFov(+1.0); |
|
170 |
else if (xev.xbutton.button == 5) ctx.adjustFov(-1.0); |
|
171 |
else if (xev.xbutton.button == 8) resetView(); |
|
172 |
else if (xev.xbutton.button == 9) keepRunningX11 = false; |
|
173 |
redraw = true; |
|
174 |
} else if (xev.type == MotionNotify) { |
|
175 |
// printCursorInfo(xev.xmotion); |
|
176 |
} else if (xev.type == ConfigureNotify) { |
|
177 |
std::cout << "XEvent: ConfigureNotify:" |
|
178 |
<< " window=" << xev.xconfigure.window |
|
179 |
<< " height=" << xev.xconfigure.height |
|
180 |
<< " width=" << xev.xconfigure.width |
|
181 |
<< std::endl; |
|
182 |
if (listenToRootResizes |
|
183 |
&& xev.xconfigure.window == parent) { |
|
184 |
XResizeWindow(dpy, win, |
|
185 |
xev.xconfigure.width, xev.xconfigure.height); |
|
186 |
} |
|
187 |
} else if (xev.type == UnmapNotify) { |
|
188 |
std::cout << "XEvent: UnmapNotify" << std::endl; |
|
189 |
} else if (xev.type == DestroyNotify) { |
|
190 |
std::cout << "XEvent: DestroyNotify → finish" << std::endl; |
|
191 |
break; |
|
192 |
} else { |
|
193 |
std::cout << "XEvent: type=" << xev.type << std::endl; |
|
194 |
} |
|
195 |
||
196 |
if (redraw) { |
|
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
197 |
runShaders(); |
0 | 198 |
glXSwapBuffers(dpy, win); |
199 |
} |
|
200 |
} else if (epoll[epollEvent].data.fd == STDIN_FILENO) { |
|
201 |
int epollFD = epoll[epollEvent].data.fd; |
|
202 |
logOutput << "other event: fd=" << epollFD << " data="; |
|
203 |
for (char ch; read(epollFD, &ch, 1) > 0;) { |
|
204 |
std::stringstream msg; |
|
205 |
msg |
|
206 |
<< std::hex |
|
207 |
<< std::setfill('0') |
|
208 |
<< std::setw(2) |
|
209 |
<< (int) ch; |
|
210 |
logOutput << msg.str(); |
|
211 |
} |
|
212 |
logOutput << std::endl; |
|
213 |
||
214 |
} else { |
|
215 |
logOutput |
|
216 |
<< "error: event on an unexpected FD: " |
|
217 |
<< epoll[epollEvent].data.fd |
|
218 |
<< std::endl; |
|
219 |
} |
|
220 |
} |
|
221 |
} |
|
222 |
} |
|
223 |
||
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
224 |
void Shark::clear() { |
0 | 225 |
glClearColor( |
226 |
(cfg.backgroundColor >> 16 & 0xFF) / 256., |
|
227 |
(cfg.backgroundColor >> 8 & 0xFF) / 256., |
|
228 |
(cfg.backgroundColor & 0xFF) / 256., |
|
229 |
1.0); |
|
230 |
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
|
231 |
} |
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
232 |
|
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
233 |
void Shark::runShaders() { |
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
234 |
shaderProgram->use(); |
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
235 |
checkError(&std::cerr); |
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
236 |
|
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
237 |
clear(); |
0 | 238 |
|
239 |
GLint viewport[4]; |
|
240 |
glGetIntegerv(GL_VIEWPORT, viewport); |
|
241 |
GLfloat width = viewport[2]; |
|
242 |
GLfloat height = viewport[3]; |
|
243 |
||
244 |
glm::mat4 projection = glm::perspective( |
|
245 |
glm::radians(ctx.fov), |
|
246 |
width / height, |
|
247 |
0.1f, 100.0f); |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
248 |
glUniformMatrix4fv(ProgAttr.uProjection, 1, GL_FALSE, &projection[0][0]); |
0 | 249 |
|
250 |
glm::mat4 view = glm::lookAt( |
|
251 |
ctx.cameraPos, |
|
252 |
ctx.cameraPos + ctx.cameraFront, |
|
253 |
ctx.cameraUp); |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
254 |
glUniformMatrix4fv(ProgAttr.uView, 1, GL_FALSE, &view[0][0]); |
0 | 255 |
|
256 |
// glBindVertexArray(vao); |
|
257 |
||
258 |
glm::mat4 model = glm::mat4(1.0f); // identity matrix |
|
259 |
// model = glm::translate(model, glm::vec3(0., 0., 0.)); |
|
260 |
// float angle = 20.0f; |
|
261 |
// glm::vec3 xxx = glm::vec3(1.0f, 0.3f, 0.5f); |
|
262 |
// model = glm::rotate(model, glm::radians(angle), xxx); |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
263 |
glUniformMatrix4fv(ProgAttr.uModel, 1, GL_FALSE, &model[0][0]); |
0 | 264 |
|
265 |
glDrawArrays(GL_TRIANGLES, 0, 2 * 3); // viz loadVertices() kde plníme data |
|
266 |
std::cerr << "GLSL: glDrawArrays()" << std::endl; |
|
267 |
} |
|
268 |
||
269 |
void Shark::log(LogLevel level, std::string message) { |
|
270 |
::log(logOutput, level, message); |
|
271 |
} |
|
272 |
||
273 |
int Shark::setNonBlocking(int fd) { |
|
274 |
int flags = fcntl(fd, F_GETFL, 0); |
|
275 |
fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
|
276 |
return fd; |
|
277 |
} |
|
278 |
||
279 |
void Shark::loadVertices() { |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
280 |
for (int i = 0; i < textures.size(); i++) { |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
281 |
const Texture& tex = textures[i]; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
282 |
GLfloat ratio = tex.getRatio(); |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
283 |
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
|
284 |
// 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
|
285 |
-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
|
286 |
+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
|
287 |
-0.80f * ratio, -0.80f, +0.0, /**/ 0.0, 1.0, |
0 | 288 |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
289 |
-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
|
290 |
+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
|
291 |
+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0, |
0 | 292 |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
293 |
// viz glDrawArrays(), kde vybereme počátek a počet hodnot |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
294 |
}; |
0 | 295 |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
296 |
// Vertex data: |
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
297 |
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
|
298 |
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
|
299 |
(void*) 0); |
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
300 |
glEnableVertexAttribArray(ProgAttr.aVertexXYZ); |
0 | 301 |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
302 |
// Texture positions: |
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
303 |
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
|
304 |
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
|
305 |
(void*) (3 * sizeof (float))); |
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
306 |
glEnableVertexAttribArray(ProgAttr.aTextureXY); |
0 | 307 |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
308 |
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
|
309 |
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
|
310 |
vertices.data(), |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
311 |
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
|
312 |
// 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
|
313 |
// 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
|
314 |
// 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
|
315 |
// 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
|
316 |
// 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
|
317 |
// 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
|
318 |
// 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
|
319 |
// The vertex data will be uploaded once and drawn once. |
0 | 320 |
|
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
321 |
// 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
|
322 |
} |
0 | 323 |
} |
324 |
||
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
325 |
Shark::Texture Shark::loadTexture(const 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
|
326 |
Texture tex; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
327 |
tex.fileName = fileName; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
328 |
MappedFile file(tex.fileName); |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
329 |
|
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
330 |
std::shared_ptr<ImageLoader::ImageBuffer> img(imageLoader.loadImage(file)); |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
331 |
|
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
332 |
tex.width = img->width; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
333 |
tex.height = img->height; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
334 |
|
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
335 |
glGenTextures(1, &tex.id); |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
336 |
glBindTexture(GL_TEXTURE_2D, tex.id); |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
337 |
auto GLT2D = GL_TEXTURE_2D; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
338 |
glTexImage2D(GLT2D, 0, GL_RGBA, |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
339 |
tex.width, tex.height, |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
340 |
0, GL_RGBA, GL_UNSIGNED_BYTE, |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
341 |
img->getData()); |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
342 |
glTexParameteri(GLT2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
343 |
glTexParameteri(GLT2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
344 |
glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
345 |
glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
346 |
glGenerateMipmap(GLT2D); |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
347 |
std::cerr << "loadTexture(\"" << fileName.c_str() << "\", " |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
348 |
<< tex.width << ", " << tex.height << ") = " << tex.id << std::endl; |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
349 |
checkError(&std::cerr); |
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
350 |
return tex; |
0 | 351 |
} |
352 |
||
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
353 |
void Shark::loadTextures() { |
0 | 354 |
for (const Configuration::Texture& tex : cfg.textures) { |
1
fb65455622b9
load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
355 |
textures.push_back(loadTexture(tex.fileName)); |
0 | 356 |
// TODO: review texture loading and binding |
357 |
// works even without this - default texture |
|
358 |
// glUniform1i(ProgAttr.jazz, jazz); |
|
359 |
// checkError(&std::cerr); |
|
360 |
} |
|
361 |
} |
|
362 |
||
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
363 |
std::shared_ptr<Program> Shark::loadShaders() { |
0 | 364 |
try { |
365 |
// Vertex Array Object (VAO) |
|
366 |
GLuint vao; |
|
367 |
glGenVertexArrays(1, &vao); |
|
368 |
glBindVertexArray(vao); |
|
369 |
// VAO - something like context for bound data/variables |
|
370 |
// We can switch multiple VAOs. VAO can contain multiple VBOs. |
|
371 |
// See also https://stackoverflow.com/questions/11821336/ |
|
372 |
// what-are-vertex-array-objects |
|
373 |
||
374 |
// Vertex Buffer Object (VBO): |
|
375 |
GLuint vbo; |
|
376 |
glGenBuffers(1, &vbo); |
|
377 |
glBindBuffer(GL_ARRAY_BUFFER, vbo); |
|
378 |
||
379 |
std::vector<std::string> fileNames = { |
|
380 |
"shaders/first.vert", |
|
381 |
"shaders/first.frag", |
|
382 |
}; |
|
383 |
||
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
384 |
std::shared_ptr<Program> program = std::make_shared<Program>(); |
0 | 385 |
|
386 |
// glBindFragDataLocation(program, 0, "outColor"); |
|
387 |
// glBindAttribLocation(program, LOC.input, "vertices"); |
|
388 |
||
389 |
for (const std::string& fileName : fileNames) { |
|
390 |
MappedFile file(fileName); |
|
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
391 |
std::shared_ptr<Shader> shader = std::make_shared<Shader>( |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
392 |
toShaderType(fileName), file, fileName); |
0 | 393 |
|
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
394 |
program->attachShader(*shader.get()); |
0 | 395 |
shaders.push_back(shader); |
396 |
std::cerr << "GLSL loaded: " << fileName.c_str() << std::endl; |
|
397 |
} |
|
398 |
||
399 |
// GLSL compiler does very efficient / aggressive optimization. |
|
400 |
// Attributes and uniforms that are not used in the shader are deleted. |
|
401 |
// And even if we e.g. read color from a texture and overwrite it, |
|
402 |
// the variable is still deleted and considered „inactive“. |
|
403 |
// Functions glGetAttribLocation() and glGetUniformLocation() return -1. |
|
404 |
||
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
405 |
program->link(); |
0 | 406 |
|
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
407 |
ProgAttr.aVertexXYZ = program->getAttribLocation("aVertexXYZ"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
408 |
ProgAttr.aTextureXY = program->getAttribLocation("aTextureXY"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
409 |
ProgAttr.uModel = program->getUniformLocation("uModel"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
410 |
ProgAttr.uView = program->getUniformLocation("uView"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
411 |
ProgAttr.uProjection = program->getUniformLocation("uProjection"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
412 |
ProgAttr.uTexture = program->getUniformLocation("uTexture"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
413 |
ProgAttr.fColor = program->getFragDataLocation("fColor"); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
414 |
program->bindFragDataLocation("fColor", ProgAttr.fColor); |
0 | 415 |
// listVariables(program); |
416 |
std::cerr << "GLSL shader count: " << shaders.size() << std::endl; |
|
417 |
return program; |
|
418 |
} catch (const std::exception& e) { |
|
419 |
std::cerr << "Error while loading shaders: " << e.what() << std::endl; |
|
420 |
} catch (...) { |
|
421 |
std::cerr << "Error while loading shaders: unknown" << std::endl; |
|
422 |
} |
|
423 |
throw std::logic_error("GLSL: loadShaders() failed"); |
|
424 |
} |
|
425 |