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