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