OHP3D.cpp
author František Kučera <franta-hg@frantovo.cz>
Fri, 12 Jan 2024 00:45:40 +0100
branchv_0
changeset 40 df1376ef9f6d
parent 38 808f05faab30
permissions -rw-r--r--
reload document: aspect ration or page count may change
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
/**
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
     2
 * OHP3D
0
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
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    18
#include <iostream>
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    19
#include <iomanip>
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    20
#include <string>
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    21
#include <charconv>
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    22
#include <memory>
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    23
#include <functional>
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    24
#include <sstream>
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    25
#include <vector>
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    26
#include <chrono>
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    27
#include <unistd.h>
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    28
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    29
#include <poppler/cpp/poppler-document.h>
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    30
#include <poppler/cpp/poppler-page.h>
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    31
#include <poppler/cpp/poppler-toc.h>
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    32
#include <sys/stat.h>
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    33
#include <poppler/cpp/poppler-page-renderer.h>
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    34
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    35
#include "x11.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    36
#include "opengl.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    37
#include "EPoll.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    38
#include "Logger.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    39
#include "MappedFile.h"
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
    40
#include "PageBuffer.h"
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    41
#include "Texture.h"
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    42
#include "Shader.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    43
#include "Program.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    44
#include "FileMonitor.h"
24
98d033d3ef7c xattr: XAttrs class that read/write/list extended attributes
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    45
#include "XAttrs.h"
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    46
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    47
#include "OHP3D.h"
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
    49
class OHP3D::Impl {
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    50
public:
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    51
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    52
	struct {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    53
		GLint aVertexXYZ = -2;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    54
		GLint aTextureXY = -2;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    55
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    56
		GLint fColor = -2;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    57
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    58
		GLint uModel = -2;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    59
		GLint uView = -2;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    60
		GLint uProjection = -2;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    61
		GLint uTexture = -2;
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
    62
		GLint uTextureScale = -2;
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    63
	} ProgAttr;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    64
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    65
	struct {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    66
		float yaw = -90.f;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    67
		float pitch = 0.f;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    68
		float roll = 0.f;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    69
		float fov = 45.0f;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    70
		glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    71
		glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    72
		glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    73
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    74
		void adjustFov(float diff) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    75
			fov += diff;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    76
			if (fov < 1.0f) fov = 1.0f;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    77
			else if (fov > 120.0f) fov = 120.0f;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    78
			std::cerr << "field of view: " << fov << " °" << std::endl;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    79
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    80
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    81
		void moveForward(const float cameraSpeed) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    82
			cameraPos += cameraSpeed * cameraFront;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    83
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    84
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    85
		void moveBackward(const float cameraSpeed) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    86
			cameraPos -= cameraSpeed * cameraFront;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    87
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    88
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    89
		void moveLeft(const float cameraSpeed) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    90
			cameraPos -= glm::normalize(
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    91
					glm::cross(cameraFront, cameraUp)) * cameraSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    92
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    93
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    94
		void moveRight(const float cameraSpeed) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    95
			cameraPos += glm::normalize(
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    96
					glm::cross(cameraFront, cameraUp)) * cameraSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    97
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    98
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    99
		void moveUp(const float cameraSpeed) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   100
			cameraPos += cameraSpeed * glm::normalize(cameraUp);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   101
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   102
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   103
		void moveDown(const float cameraSpeed) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   104
			cameraPos -= cameraSpeed * glm::normalize(cameraUp);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   105
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   106
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   107
		void updateCameraFrontAndUp() {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   108
			std::cerr << "--- updateCameraFrontAndUp() --------" << std::endl;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   109
			dump("pitch, yaw, roll", glm::vec3(pitch, yaw, roll));
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   110
			dump("cameraPos", cameraPos);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   111
			dump("cameraFront", cameraFront);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   112
			const auto pitchR = glm::radians(pitch); // around X axis
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   113
			const auto yawR = glm::radians(yaw); //     around Y axis
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   114
			const auto rollR = glm::radians(roll); //   around Z axis
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   115
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   116
			cameraFront.x = cos(pitchR) * cos(yawR);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   117
			cameraFront.y = sin(pitchR);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   118
			cameraFront.z = cos(pitchR) * sin(yawR);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   119
			cameraFront = glm::normalize(cameraFront);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   120
			dump("cameraFront", cameraFront);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   121
			dump("cameraUp", cameraUp);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   122
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   123
			// TODO: review ROLL rotation and default angle
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   124
			glm::mat4 rollMatrix = glm::rotate(
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   125
					glm::mat4(1.0f), rollR, cameraFront);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   126
			cameraUp = glm::mat3(rollMatrix) * glm::vec3(0., 1., 0.);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   127
			dump("cameraUp", cameraUp);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   128
			std::cerr << "-------------------------------------" << std::endl;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   129
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   130
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   131
		void limitPitch() {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   132
			if (pitch > +89.0f) pitch = +89.0f;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   133
			if (pitch < -89.0f) pitch = -89.0f;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   134
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   135
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   136
		void turnLeft(const float angleSpeed) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   137
			yaw -= angleSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   138
			updateCameraFrontAndUp();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   139
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   140
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   141
		void turnRight(const float angleSpeed) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   142
			yaw += angleSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   143
			updateCameraFrontAndUp();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   144
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   145
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   146
		void turnUp(const float angleSpeed) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   147
			pitch += angleSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   148
			limitPitch();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   149
			updateCameraFrontAndUp();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   150
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   151
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   152
		void turnDown(const float angleSpeed) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   153
			pitch -= angleSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   154
			limitPitch();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   155
			updateCameraFrontAndUp();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   156
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   157
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   158
		void rollLeft(const float angleSpeed) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   159
			roll += angleSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   160
			updateCameraFrontAndUp();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   161
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   162
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   163
		void rollRight(const float angleSpeed) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   164
			roll -= angleSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   165
			updateCameraFrontAndUp();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   166
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   167
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   168
	} initialCtx, ctx;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   169
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   170
	Display* dpy;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   171
	Window win;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   172
	XVisualInfo* vi;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   173
	GLXContext glc;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   174
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   175
	FileMonitor fileMonitor;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   176
	std::vector<WatchedFile> watchedFiles;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   177
	std::vector<std::shared_ptr<Shader>> shaders;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   178
	std::shared_ptr<Program> shaderProgram;
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   179
	std::vector<std::shared_ptr<Texture>> textures;
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   180
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   181
	Configuration cfg;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   182
	std::ostream& logOutput = std::cerr;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   183
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   184
	Impl(Configuration cfg) : cfg(cfg) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   185
	}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   186
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   187
	void run();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   188
	void clear();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   189
	void runShaders();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   190
	Window getRootWindow(Window defaultValue);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   191
	void log(LogLevel level, std::string message);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   192
	int setNonBlocking(int fd);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   193
	void loadVertices();
38
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   194
	void loadDocuments(bool first = true);
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   195
	bool reloadDocument(const std::string& fileName);
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   196
	void loadShaders();
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   197
	void updateVariableLocations();
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   198
	bool reloadShader(const std::string& fileName);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   199
	void setTitle(const std::string& suffix = "");
11
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   200
	static const std::string getDefaultFile(const std::string& relativePath);
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   201
34
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   202
	void goPage(int count);
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   203
	void goHome();
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   204
	void goEnd();
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   205
	void goPageMouse(XButtonEvent ev);
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   206
};
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   207
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   208
OHP3D::OHP3D(const Configuration& configuration) :
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   209
impl(new Impl(configuration)) {
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   210
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   211
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   212
OHP3D::~OHP3D() {
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   213
	impl->textures.clear();
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   214
	impl->shaders.clear();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   215
	impl->shaderProgram = nullptr;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   216
	XFree(impl->vi);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   217
	glXMakeCurrent(impl->dpy, None, NULL);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   218
	glXDestroyContext(impl->dpy, impl->glc);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   219
	XDestroyWindow(impl->dpy, impl->win);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   220
	XCloseDisplay(impl->dpy);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   221
	delete impl;
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   222
	// std::cerr << "~OHP3D()" << std::endl;
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   223
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   224
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   225
void OHP3D::Impl::setTitle(const std::string& suffix) {
8
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   226
	std::stringstream title;
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   227
	title << "OHP3D";
8
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   228
	if (suffix.size()) title << ": " << suffix.c_str();
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   229
	XStoreName(dpy, win, title.str().c_str());
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   230
	XFlush(dpy);
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   231
}
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   232
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   233
void OHP3D::run() {
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   234
	impl->run();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   235
}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   236
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   237
void OHP3D::Impl::run() {
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   238
	dpy = XOpenDisplay(NULL);
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
	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
   241
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   242
	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
   243
	vi = glXChooseVisual(dpy, 0, att);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   244
	Window root = DefaultRootWindow(dpy);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   245
	Window parent = cfg.rootWindow ? cfg.rootWindow : root;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   246
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   247
	XSetWindowAttributes swa;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   248
	swa.colormap = XCreateColormap(dpy, parent, vi->visual, AllocNone);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   249
	swa.event_mask = ExposureMask | KeyPressMask | PointerMotionMask
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   250
			| ButtonPressMask
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   251
			| StructureNotifyMask;
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
	bool full = false;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   254
	unsigned int width = 1600;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   255
	unsigned int height = 1200;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   256
	if (parent != root) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   257
		XWindowAttributes parentAttr;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   258
		XGetWindowAttributes(dpy, parent, &parentAttr);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   259
		width = parentAttr.width;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   260
		height = parentAttr.height;
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
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   263
	win = XCreateWindow(
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   264
			dpy, parent, 0, 0, width, height, 0,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   265
			vi->depth, InputOutput, vi->visual,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   266
			CWColormap | CWEventMask, &swa);
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
	XMapWindow(dpy, win);
8
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   269
	setTitle();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   270
	setX11PID(dpy, win);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   271
	// XSetWindowBackground(dpy, win, 0) vs. glClearColor()
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   272
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   273
	glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   274
	glXMakeCurrent(dpy, win, glc);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   275
15
1eb7cfefbeea transparency/alpha: blending
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   276
	glEnable(GL_DEPTH_TEST);
1eb7cfefbeea transparency/alpha: blending
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   277
	glEnable(GL_BLEND);
1eb7cfefbeea transparency/alpha: blending
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   278
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1eb7cfefbeea transparency/alpha: blending
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   279
3
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   280
	clear();
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   281
	glXSwapBuffers(dpy, win);
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   282
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   283
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   284
	loadShaders();
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   285
	loadDocuments();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   286
	loadVertices();
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
	auto toggleFullscreen = [&]() {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   289
		full = setFullscreen(dpy, win, !full);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   290
	};
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   291
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   292
	auto resetView = [&]() {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   293
		ctx = initialCtx;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   294
		ctx.updateCameraFrontAndUp();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   295
	};
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   296
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   297
	// root can reize our window
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   298
	// 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
   299
	bool listenToRootResizes = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   300
	if (listenToRootResizes) XSelectInput(dpy, parent, StructureNotifyMask);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   301
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   302
	bool keepRunningX11 = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   303
	int x11fd = XConnectionNumber(dpy);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   304
	EPoll epoll;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   305
	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
   306
	epoll.add(fileMonitor.getFD());
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   307
	try {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   308
		epoll.add(setNonBlocking(STDIN_FILENO));
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   309
	} catch (const EPoll::Exception& e) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   310
		logOutput << "Will not monitor events on STDIN: " << e.what() << "\n";
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   311
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   312
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   313
	// 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
   314
	runShaders();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   315
	glXSwapBuffers(dpy, win);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   316
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   317
	for (XEvent xev; keepRunningX11;) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   318
		int epollEventCount = epoll.wait();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   319
		//std::cout << "trace: epoll.wait() = " << epollEventCount << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   320
		for (int epollEvent = 0; epollEvent < epollEventCount; epollEvent++) {
8
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   321
			bool redraw = false;
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   322
			if (epoll[epollEvent].data.fd == x11fd) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   323
				if (!XPending(dpy)) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   324
					// otherwise STDIN events are held until the first X11 event
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   325
					logOutput << "trace: no pending X11 event" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   326
					break;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   327
				}
20
0899e966993e process all X11 event waiting in the queue in the same cycle
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   328
process_x11_event:
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   329
				XWindowAttributes gwa;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   330
				XNextEvent(dpy, &xev);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   331
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   332
				if (xev.type == Expose) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   333
					std::cout << "XEvent: Expose" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   334
					XGetWindowAttributes(dpy, win, &gwa);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   335
					glViewport(0, 0, gwa.width, gwa.height);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   336
					redraw = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   337
				} else if (xev.type == KeyPress) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   338
					DecodedKey key = decodeKeycode(dpy, xev.xkey.keycode);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   339
					std::cout << "XEvent: KeyPress:"
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   340
							<< " keycode=" << key.code
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   341
							<< " key=" << key.name
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   342
							<< std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   343
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   344
					const float cSp = 0.05f; // camera speed
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   345
					const float aSp = 5.f; // angle speed
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
					if (key.matches(XK_q, XK_Escape)) keepRunningX11 = false;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   348
					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
   349
					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
   350
					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
   351
					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
   352
					else if (key.matches(XK_w)) ctx.rollLeft(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   353
					else if (key.matches(XK_r)) ctx.rollRight(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   354
					else if (key.matches(XK_t)) ctx.turnUp(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   355
					else if (key.matches(XK_g)) ctx.turnDown(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   356
					else if (key.matches(XK_m)) ctx.moveLeft(cSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   357
					else if (key.matches(XK_comma)) ctx.moveRight(cSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   358
					else if (key.matches(XK_l)) ctx.moveUp(cSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   359
					else if (key.matches(XK_period)) ctx.moveDown(cSp);
33
4620bba4fa40 import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
   360
					else if (key.matches(XK_j, XK_Page_Up)) goPage(-1);
4620bba4fa40 import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
   361
					else if (key.matches(XK_k, XK_Page_Down)) goPage(+1);
4620bba4fa40 import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
   362
					else if (key.matches(XK_u, XK_Home)) goHome();
4620bba4fa40 import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
   363
					else if (key.matches(XK_i, XK_End)) goEnd();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   364
					else if (key.matches(XK_x)) resetView();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   365
					else if (key.matches(XK_F11, XK_y)) toggleFullscreen();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   366
					redraw = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   367
				} else if (xev.type == ButtonPress) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   368
					std::cout << "XEvent: ButtonPress:"
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   369
							<< " button=" << xev.xbutton.button
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   370
							<< std::endl;
33
4620bba4fa40 import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 32
diff changeset
   371
					if (xev.xbutton.button == 1) goPageMouse(xev.xbutton);
9
53ba7e52c67c mouse wheel: move camera, not object
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   372
					else if (xev.xbutton.button == 4) ctx.adjustFov(-1.0);
53ba7e52c67c mouse wheel: move camera, not object
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   373
					else if (xev.xbutton.button == 5) ctx.adjustFov(+1.0);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   374
					else if (xev.xbutton.button == 8) resetView();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   375
					else if (xev.xbutton.button == 9) keepRunningX11 = false;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   376
					redraw = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   377
				} else if (xev.type == MotionNotify) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   378
					// printCursorInfo(xev.xmotion);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   379
				} else if (xev.type == ConfigureNotify) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   380
					std::cout << "XEvent: ConfigureNotify:"
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   381
							<< " window=" << xev.xconfigure.window
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   382
							<< " height=" << xev.xconfigure.height
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   383
							<< " width=" << xev.xconfigure.width
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   384
							<< std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   385
					if (listenToRootResizes
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   386
							&& xev.xconfigure.window == parent) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   387
						XResizeWindow(dpy, win,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   388
								xev.xconfigure.width, xev.xconfigure.height);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   389
					}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   390
				} else if (xev.type == UnmapNotify) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   391
					std::cout << "XEvent: UnmapNotify" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   392
				} else if (xev.type == DestroyNotify) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   393
					std::cout << "XEvent: DestroyNotify → finish" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   394
					break;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   395
				} else {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   396
					std::cout << "XEvent: type=" << xev.type << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   397
				}
20
0899e966993e process all X11 event waiting in the queue in the same cycle
František Kučera <franta-hg@frantovo.cz>
parents: 19
diff changeset
   398
				if (XPending(dpy)) goto process_x11_event;
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   399
			} else if (epoll[epollEvent].data.fd == STDIN_FILENO) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   400
				int epollFD = epoll[epollEvent].data.fd;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   401
				logOutput << "other event: fd=" << epollFD << " data=";
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   402
				for (char ch; read(epollFD, &ch, 1) > 0;) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   403
					std::stringstream msg;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   404
					msg
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   405
							<< std::hex
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   406
							<< std::setfill('0')
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   407
							<< std::setw(2)
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   408
							<< (int) ch;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   409
					logOutput << msg.str();
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
				logOutput << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   412
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
			} 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
   414
				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
   415
				for (FileEvent fe; fileMonitor.readEvent(fe);) {
8
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   416
					logOutput << "   "
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   417
							<< " file=" << fe.fileName
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   418
							<< " mask=" << fe.mask
7
e6065118326f monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   419
							<< std::endl;
8
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   420
					try {
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   421
						redraw |= reloadShader(fe.fileName);
38
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   422
						redraw |= reloadDocument(fe.fileName);
8
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   423
						setTitle();
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   424
					} catch (const std::exception& e) {
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   425
						setTitle("[ERROR]");
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   426
						logOutput << "error while reloading '"
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   427
								<< fe.fileName.c_str()
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   428
								<< "': " << e.what() << std::endl;
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   429
					}
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
   430
				}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   431
			} else {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   432
				logOutput
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   433
						<< "error: event on an unexpected FD: "
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   434
						<< epoll[epollEvent].data.fd
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   435
						<< std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   436
			}
8
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   437
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   438
			if (redraw) {
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   439
				runShaders();
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   440
				glXSwapBuffers(dpy, win);
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   441
			}
0
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
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   444
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   445
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   446
void OHP3D::Impl::clear() {
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   447
	glClearColor(
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   448
			(cfg.backgroundColor >> 16 & 0xFF) / 256.,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   449
			(cfg.backgroundColor >> 8 & 0xFF) / 256.,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   450
			(cfg.backgroundColor & 0xFF) / 256.,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   451
			1.0);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   452
	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
   453
}
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   454
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   455
void OHP3D::Impl::runShaders() {
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   456
	shaderProgram->use();
3
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   457
	checkError(&std::cerr);
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   458
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   459
	clear();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   460
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   461
	GLint viewport[4];
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   462
	glGetIntegerv(GL_VIEWPORT, viewport);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   463
	GLfloat width = viewport[2];
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   464
	GLfloat height = viewport[3];
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   465
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   466
	glm::mat4 projection = glm::perspective(
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   467
			glm::radians(ctx.fov),
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   468
			width / height,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   469
			0.1f, 100.0f);
2
3faef2f5128e better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   470
	glUniformMatrix4fv(ProgAttr.uProjection, 1, GL_FALSE, &projection[0][0]);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   471
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   472
	glm::mat4 view = glm::lookAt(
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   473
			ctx.cameraPos,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   474
			ctx.cameraPos + ctx.cameraFront,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   475
			ctx.cameraUp);
2
3faef2f5128e better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   476
	glUniformMatrix4fv(ProgAttr.uView, 1, GL_FALSE, &view[0][0]);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   477
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   478
	// glBindVertexArray(vao);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   479
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   480
	glm::mat4 model = glm::mat4(1.0f); // identity matrix
2
3faef2f5128e better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   481
	glUniformMatrix4fv(ProgAttr.uModel, 1, GL_FALSE, &model[0][0]);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   482
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   483
	for (int i = 0; i < textures.size(); i++) {
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   484
		textures[i]->bind();
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   485
		glUniform1f(ProgAttr.uTextureScale, textures[i]->getScale());
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   486
		int pageVertices = 2 * 3; // see loadVertices()
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   487
		glDrawArrays(GL_TRIANGLES, i*pageVertices, pageVertices);
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   488
		// std::cerr << "GLSL: glDrawArrays() #" << i << std::endl;
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   489
	}
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   490
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   491
	std::cerr << "GLSL: glDrawArrays() " << textures.size() << "x" << std::endl;
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   492
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   493
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   494
void OHP3D::Impl::log(LogLevel level, std::string message) {
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   495
	::log(logOutput, level, message);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   496
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   497
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   498
int OHP3D::Impl::setNonBlocking(int fd) {
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   499
	int flags = fcntl(fd, F_GETFL, 0);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   500
	fcntl(fd, F_SETFL, flags | O_NONBLOCK);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   501
	return fd;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   502
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   503
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   504
void OHP3D::Impl::loadVertices() {
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   505
	std::vector<GLfloat> vertices;
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   506
	for (int i = 0; i < textures.size(); i++) {
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   507
		std::shared_ptr<Texture> tex = textures[i];
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   508
		GLfloat ratio = tex->getRatio();
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   509
		GLfloat offset = ratio * 1.8 * i;
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   510
		const std::vector<GLfloat> v = {
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   511
			// Vertex XYZ                               Texture XY
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   512
			-0.80f * ratio + offset, +0.80f, +0.0, /**/ 0.0, 0.0,
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   513
			+0.80f * ratio + offset, +0.80f, +0.0, /**/ 1.0, 0.0,
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   514
			-0.80f * ratio + offset, -0.80f, +0.0, /**/ 0.0, 1.0,
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   515
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   516
			-0.80f * ratio + offset, -0.80f, +0.0, /**/ 0.0, 1.0,
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   517
			+0.80f * ratio + offset, -0.80f, +0.0, /**/ 1.0, 1.0,
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   518
			+0.80f * ratio + offset, +0.80f, +0.0, /**/ 1.0, 0.0,
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   519
25
717653cedc4a set scaling filters according to the texture file extended attributes (xattr: shader-shark.texture.mag-filter = linear | nearest)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   520
			// see glDrawArrays(), where we set start offset and count
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   521
		};
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   522
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   523
		// TODO: reduce copying
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   524
		for (GLfloat f : v) vertices.push_back(f);
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   525
	}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   526
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   527
	// Vertex data:
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   528
	glVertexAttribPointer(ProgAttr.aVertexXYZ, 3, // vertex items
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   529
			GL_FLOAT, GL_FALSE, 5 * sizeof (float),
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   530
			(void*) 0);
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   531
	glEnableVertexAttribArray(ProgAttr.aVertexXYZ);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   532
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   533
	// Texture positions:
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   534
	glVertexAttribPointer(ProgAttr.aTextureXY, 2, // texture items
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   535
			GL_FLOAT, GL_FALSE, 5 * sizeof (float),
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   536
			(void*) (3 * sizeof (float)));
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   537
	glEnableVertexAttribArray(ProgAttr.aTextureXY);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   538
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   539
	glBufferData(GL_ARRAY_BUFFER,
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   540
			vertices.size() * sizeof (vertices[0]),
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   541
			vertices.data(),
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   542
			GL_STATIC_DRAW);
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   543
	// GL_STATIC_DRAW:
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   544
	//   The vertex data will be uploaded once
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   545
	//   and drawn many times(e.g. the world).
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   546
	// GL_DYNAMIC_DRAW:
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   547
	//   The vertex data will be created once, changed from
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   548
	// 	 time to time, but drawn many times more than that.
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   549
	// GL_STREAM_DRAW:
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   550
	//   The vertex data will be uploaded once and drawn once.
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   551
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   552
	// see also glBindBuffer(GL_ARRAY_BUFFER, vbo); where we set current VBO
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   553
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   554
11
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   555
const std::string
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   556
OHP3D::Impl::getDefaultFile(const std::string& relativePath) {
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   557
	const char* envName = "OHP3D_DATA_DIR";
11
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   558
	const char* envValue = ::getenv(envName);
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   559
	if (envValue) {
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   560
		return std::string(envValue) + "/" + relativePath;
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   561
	} else {
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   562
		throw std::invalid_argument(std::string("Configure $") + envName
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   563
				+ " in order to use defaults"
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   564
				" or specify textures and shaders as parameters");
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   565
	}
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   566
}
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   567
38
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   568
void OHP3D::Impl::loadDocuments(bool first) {
32
711d3d781143 default PDF document
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   569
	if (cfg.documents.empty())
711d3d781143 default PDF document
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   570
		cfg.documents.push_back({getDefaultFile("documents/default.pdf")});
711d3d781143 default PDF document
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   571
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   572
	for (const Configuration::File& document : cfg.documents) {
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   573
		std::cerr << "loading document: " << document.fileName.c_str() << "\n";
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   574
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   575
		MappedFile buffer(document.fileName);
25
717653cedc4a set scaling filters according to the texture file extended attributes (xattr: shader-shark.texture.mag-filter = linear | nearest)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   576
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   577
		namespace pp = poppler;
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   578
		using Document = pp::document;
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   579
32
711d3d781143 default PDF document
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   580
		std::shared_ptr<Document> doc = std::shared_ptr<Document>(Document::
711d3d781143 default PDF document
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   581
				load_from_raw_data(buffer.getData(), buffer.getSize()));
25
717653cedc4a set scaling filters according to the texture file extended attributes (xattr: shader-shark.texture.mag-filter = linear | nearest)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   582
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   583
		log(LogLevel::INFO, std::string("PDF parsed:")
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   584
				+ " pages=" + std::to_string(doc->pages()));
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   585
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   586
		log(LogLevel::INFO, "loadig pages to textures...");
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   587
		auto timingStart = std::chrono::steady_clock::now();
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   588
		pp::page_renderer renderer;
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   589
		renderer.set_image_format(pp::image::format_rgb24);
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   590
		double dpi = cfg.dpi;
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   591
		for (int i = 0, limit = doc->pages(); i < limit; i++) {
38
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   592
			{
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   593
				std::stringstream title;
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   594
				title << "[loading " << (i + 1) << "/" << limit << "]";
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   595
				setTitle(title.str());
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   596
			}
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   597
			std::shared_ptr<pp::page> page(doc->create_page(i));
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   598
			pp::image pageImage = renderer.render_page(page.get(), dpi, dpi);
25
717653cedc4a set scaling filters according to the texture file extended attributes (xattr: shader-shark.texture.mag-filter = linear | nearest)
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   599
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   600
			PageBuffer img(
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   601
					pageImage.data(),
32
711d3d781143 default PDF document
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   602
					pageImage.width() * pageImage.height() * 3,
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   603
					pageImage.width(), pageImage.height());
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   604
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   605
			textures.push_back(std::make_shared<Texture>(
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   606
					pageImage.width(), pageImage.height(),
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   607
					img, document.fileName));
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   608
			// textures.back()->setScale(8.); // TODO: fix scale and DPI
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   609
			log(LogLevel::INFO, "  page " + std::to_string(i + 1)
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   610
					+ "/" + std::to_string(limit));
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   611
		}
38
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   612
		setTitle("");
11
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   613
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   614
		auto timingEnd = std::chrono::steady_clock::now();
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   615
		auto timingTotal = std::chrono::duration_cast<std::chrono::microseconds>
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   616
				(timingEnd - timingStart).count();
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   617
38
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   618
		if (first) watchedFiles.push_back(fileMonitor.watch(document.fileName));
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   619
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   620
		std::stringstream timingMsg;
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   621
		timingMsg.imbue(std::locale(""));
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   622
		timingMsg << "PDF to texture load time:"
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   623
				<< " page average = " << (timingTotal / doc->pages()) << " µs,"
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   624
				<< " total = " << timingTotal << " µs ("
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   625
				<< doc->pages() << " pages)";
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   626
		log(LogLevel::INFO, timingMsg.str());
32
711d3d781143 default PDF document
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   627
		if (timingTotal < 400000) logOutput << ">> Doherty threshold met! <<\n";
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   628
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   629
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   630
38
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   631
bool OHP3D::Impl::reloadDocument(const std::string& fileName) {
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   632
	bool hasChanges = false;
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   633
	for (std::shared_ptr<Texture> tex : textures) {
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   634
		if (tex->getFileName() == fileName) {
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   635
			hasChanges = true;
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   636
			break;
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   637
		}
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   638
	}
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   639
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   640
	if (hasChanges) {
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   641
		// TODO: reload only changed document
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   642
		textures.clear();
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   643
		loadDocuments(false);
40
df1376ef9f6d reload document: aspect ration or page count may change
František Kučera <franta-hg@frantovo.cz>
parents: 38
diff changeset
   644
		loadVertices(); // aspect ration or page count may change
38
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   645
	}
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   646
	return hasChanges;
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   647
}
808f05faab30 reload document when changed
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   648
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   649
void OHP3D::Impl::loadShaders() {
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   650
	// Vertex Array Object (VAO)
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   651
	GLuint vao;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   652
	glGenVertexArrays(1, &vao);
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   653
	glBindVertexArray(vao);
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   654
	// VAO - something like context for bound data/variables
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   655
	// We can switch multiple VAOs. VAO can contain multiple VBOs.
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   656
	// what-are-vertex-array-objects
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   657
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   658
	// Vertex Buffer Object (VBO):
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   659
	GLuint vbo;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   660
	glGenBuffers(1, &vbo);
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   661
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   662
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   663
	{
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   664
		// Load default shaders if there are no configured:
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   665
		int vc = 0;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   666
		int fc = 0;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   667
		auto& ss = cfg.shaders;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   668
		for (const auto& s : ss) if (s.type == "vertex") vc++;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   669
		for (const auto& s : ss) if (s.type == "fragment") fc++;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   670
		auto& d = getDefaultFile;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   671
		if (vc == 0) ss.push_back({d("shaders/default.vert"), "vertex"});
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   672
		if (fc == 0) ss.push_back({d("shaders/default.frag"), "fragment"});
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   673
	}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   674
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   675
	shaderProgram = std::make_shared<Program>();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   676
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   677
	// glBindFragDataLocation(program, 0, "outColor");
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   678
	// glBindAttribLocation(program, LOC.input, "vertices");
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   679
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   680
	for (const Configuration::Shader definition : cfg.shaders) {
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   681
		Shader::Type type;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   682
		std::string fileName = definition.fileName;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   683
		if (definition.type == "fragment") type = Shader::Type::FRAGMENT;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   684
		else if (definition.type == "vertex") type = Shader::Type::VERTEX;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   685
		else throw std::invalid_argument("unsupported shader type");
6
fd93a46db15b support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   686
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   687
		MappedFile file(fileName);
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   688
		std::shared_ptr<Shader> shader = std::make_shared<Shader>(
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   689
				type, file, fileName);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   690
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   691
		shaderProgram->attachShader(*shader.get());
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   692
		shaders.push_back(shader);
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   693
		watchedFiles.push_back(fileMonitor.watch(fileName));
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   694
		std::cerr << "GLSL loaded: " << fileName.c_str() << std::endl;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   695
		// We may detach and delete shaders,
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   696
		// but our shaders are small, so we keep them for later reloading.
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   697
	}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   698
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   699
	shaderProgram->link();
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   700
	updateVariableLocations();
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   701
	// listVariables(program);
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   702
	std::cerr << "GLSL shader count: " << shaders.size() << std::endl;
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   703
}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   704
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   705
void OHP3D::Impl::updateVariableLocations() {
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   706
	// GLSL compiler does very efficient / aggressive optimization.
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   707
	// Attributes and uniforms that are not used in the shader are deleted.
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   708
	// And even if we e.g. read color from a texture and overwrite it,
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   709
	// the variable is still deleted and considered „inactive“.
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   710
	// Functions glGetAttribLocation() and glGetUniformLocation() return -1.
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   711
	ProgAttr.aVertexXYZ = shaderProgram->getAttribLocation("aVertexXYZ");
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   712
	ProgAttr.aTextureXY = shaderProgram->getAttribLocation("aTextureXY");
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   713
	ProgAttr.uModel = shaderProgram->getUniformLocation("uModel");
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   714
	ProgAttr.uView = shaderProgram->getUniformLocation("uView");
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   715
	ProgAttr.uProjection = shaderProgram->getUniformLocation("uProjection");
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   716
	ProgAttr.uTexture = shaderProgram->getUniformLocation("uTexture");
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   717
	ProgAttr.uTextureScale = shaderProgram->getUniformLocation("uTextureScale");
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   718
	ProgAttr.fColor = shaderProgram->getFragDataLocation("fColor");
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   719
	shaderProgram->bindFragDataLocation("fColor", ProgAttr.fColor);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   720
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   721
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   722
bool OHP3D::Impl::reloadShader(const std::string& fileName) {
8
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   723
	for (auto shader : shaders) {
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   724
		if (shader->getFileName() == fileName) {
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   725
			shader->update(MappedFile(fileName));
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   726
			shaderProgram->link();
28
4cbd9c0beb4c pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents: 27
diff changeset
   727
			updateVariableLocations();
8
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   728
			return true;
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   729
		}
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   730
	}
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   731
	return false;
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   732
}
34
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   733
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   734
void OHP3D::Impl::goPage(int count) {
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   735
	// TODO: support pages with different ratios
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   736
	ctx.moveRight(count * 1.8 * textures[0]->getRatio());
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   737
}
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   738
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   739
void OHP3D::Impl::goHome() {
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   740
	ctx.cameraFront = initialCtx.cameraFront;
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   741
	ctx.cameraPos = initialCtx.cameraPos;
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   742
	ctx.cameraUp = initialCtx.cameraUp;
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   743
}
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   744
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   745
void OHP3D::Impl::goEnd() {
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   746
	goHome();
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   747
	goPage(textures.size() - 1);
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   748
}
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   749
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   750
void OHP3D::Impl::goPageMouse(XButtonEvent ev) {
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   751
	XWindowAttributes gwa;
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   752
	XGetWindowAttributes(dpy, win, &gwa);
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   753
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   754
	bool top = ev.y < gwa.height / 2, bottom = !top;
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   755
	bool left = ev.x < gwa.width / 2, right = !left;
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   756
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   757
	if (top && left) goHome();
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   758
	else if (top && right) goEnd();
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   759
	else if (bottom && left) goPage(-1);
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   760
	else if (bottom && right) goPage(+1);
7ea796b00538 convert functions to methods: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents: 33
diff changeset
   761
}