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