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