OHP3D.cpp
author František Kučera <franta-hg@frantovo.cz>
Wed, 27 Dec 2023 12:50:47 +0100
branchv_0
changeset 32 711d3d781143
parent 31 ed3caeea978a
child 33 4620bba4fa40
permissions -rw-r--r--
default PDF document
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();
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   194
	void loadDocuments();
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
   195
	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
   196
	void updateVariableLocations();
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   197
	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
   198
	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
   199
	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
   200
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   201
};
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   202
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   203
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
   204
impl(new Impl(configuration)) {
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   205
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   206
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   207
OHP3D::~OHP3D() {
19
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   208
	impl->textures.clear();
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   209
	impl->shaders.clear();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   210
	impl->shaderProgram = nullptr;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   211
	XFree(impl->vi);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   212
	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
   213
	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
   214
	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
   215
	XCloseDisplay(impl->dpy);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   216
	delete impl;
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   217
	// std::cerr << "~OHP3D()" << std::endl;
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   218
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   219
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   220
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
   221
	std::stringstream title;
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   222
	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
   223
	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
   224
	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
   225
	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
   226
}
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   227
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   228
void OHP3D::run() {
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   229
	impl->run();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   230
}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   231
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   232
void OHP3D::Impl::run() {
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   233
	dpy = XOpenDisplay(NULL);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   234
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   235
	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
   236
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   237
	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
   238
	vi = glXChooseVisual(dpy, 0, att);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   239
	Window root = DefaultRootWindow(dpy);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   240
	Window parent = cfg.rootWindow ? cfg.rootWindow : root;
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
	XSetWindowAttributes swa;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   243
	swa.colormap = XCreateColormap(dpy, parent, vi->visual, AllocNone);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   244
	swa.event_mask = ExposureMask | KeyPressMask | PointerMotionMask
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   245
			| ButtonPressMask
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   246
			| StructureNotifyMask;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   247
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   248
	bool full = false;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   249
	unsigned int width = 1600;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   250
	unsigned int height = 1200;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   251
	if (parent != root) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   252
		XWindowAttributes parentAttr;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   253
		XGetWindowAttributes(dpy, parent, &parentAttr);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   254
		width = parentAttr.width;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   255
		height = parentAttr.height;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   256
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   257
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   258
	win = XCreateWindow(
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   259
			dpy, parent, 0, 0, width, height, 0,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   260
			vi->depth, InputOutput, vi->visual,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   261
			CWColormap | CWEventMask, &swa);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   262
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   263
	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
   264
	setTitle();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   265
	setX11PID(dpy, win);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   266
	// XSetWindowBackground(dpy, win, 0) vs. glClearColor()
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   267
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   268
	glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   269
	glXMakeCurrent(dpy, win, glc);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   270
15
1eb7cfefbeea transparency/alpha: blending
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   271
	glEnable(GL_DEPTH_TEST);
1eb7cfefbeea transparency/alpha: blending
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   272
	glEnable(GL_BLEND);
1eb7cfefbeea transparency/alpha: blending
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   273
	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
   274
3
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   275
	clear();
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   276
	glXSwapBuffers(dpy, win);
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   277
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   278
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
   279
	loadShaders();
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   280
	loadDocuments();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   281
	loadVertices();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   282
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   283
	auto toggleFullscreen = [&]() {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   284
		full = setFullscreen(dpy, win, !full);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   285
	};
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   286
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   287
	auto resetView = [&]() {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   288
		ctx = initialCtx;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   289
		ctx.updateCameraFrontAndUp();
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
	// root can reize our window
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   293
	// 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
   294
	bool listenToRootResizes = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   295
	if (listenToRootResizes) XSelectInput(dpy, parent, StructureNotifyMask);
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
	bool keepRunningX11 = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   298
	int x11fd = XConnectionNumber(dpy);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   299
	EPoll epoll;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   300
	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
   301
	epoll.add(fileMonitor.getFD());
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   302
	try {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   303
		epoll.add(setNonBlocking(STDIN_FILENO));
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   304
	} catch (const EPoll::Exception& e) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   305
		logOutput << "Will not monitor events on STDIN: " << e.what() << "\n";
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   306
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   307
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   308
	// 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
   309
	runShaders();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   310
	glXSwapBuffers(dpy, win);
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
	for (XEvent xev; keepRunningX11;) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   313
		int epollEventCount = epoll.wait();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   314
		//std::cout << "trace: epoll.wait() = " << epollEventCount << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   315
		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
   316
			bool redraw = false;
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   317
			if (epoll[epollEvent].data.fd == x11fd) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   318
				if (!XPending(dpy)) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   319
					// otherwise STDIN events are held until the first X11 event
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   320
					logOutput << "trace: no pending X11 event" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   321
					break;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   322
				}
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
   323
process_x11_event:
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   324
				XWindowAttributes gwa;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   325
				XNextEvent(dpy, &xev);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   326
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   327
				if (xev.type == Expose) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   328
					std::cout << "XEvent: Expose" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   329
					XGetWindowAttributes(dpy, win, &gwa);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   330
					glViewport(0, 0, gwa.width, gwa.height);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   331
					redraw = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   332
				} else if (xev.type == KeyPress) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   333
					DecodedKey key = decodeKeycode(dpy, xev.xkey.keycode);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   334
					std::cout << "XEvent: KeyPress:"
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   335
							<< " keycode=" << key.code
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   336
							<< " key=" << key.name
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   337
							<< std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   338
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   339
					const float cSp = 0.05f; // camera speed
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   340
					const float aSp = 5.f; // angle speed
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   341
					const float pSp = 1.8 * textures[0]->getRatio(); // page
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   342
					// TODO: support pages with different ratios
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   343
					// TODO: import original controls from the private prototype
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   344
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   345
					if (key.matches(XK_q, XK_Escape)) keepRunningX11 = false;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   346
					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
   347
					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
   348
					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
   349
					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
   350
					else if (key.matches(XK_w)) ctx.rollLeft(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   351
					else if (key.matches(XK_r)) ctx.rollRight(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   352
					else if (key.matches(XK_t)) ctx.turnUp(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   353
					else if (key.matches(XK_g)) ctx.turnDown(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   354
					else if (key.matches(XK_m)) ctx.moveLeft(cSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   355
					else if (key.matches(XK_comma)) ctx.moveRight(cSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   356
					else if (key.matches(XK_l)) ctx.moveUp(cSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   357
					else if (key.matches(XK_period)) ctx.moveDown(cSp);
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   358
					else if (key.matches(XK_j)) ctx.moveLeft(pSp);
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   359
					else if (key.matches(XK_k)) ctx.moveRight(pSp);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   360
					else if (key.matches(XK_u)) ctx.moveLeft(cSp * 10);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   361
					else if (key.matches(XK_i)) ctx.moveRight(cSp * 10);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   362
					else if (key.matches(XK_x)) resetView();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   363
					else if (key.matches(XK_F11, XK_y)) toggleFullscreen();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   364
					redraw = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   365
				} else if (xev.type == ButtonPress) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   366
					std::cout << "XEvent: ButtonPress:"
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   367
							<< " button=" << xev.xbutton.button
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   368
							<< std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   369
					if (xev.xbutton.button == 1);
9
53ba7e52c67c mouse wheel: move camera, not object
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   370
					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
   371
					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
   372
					else if (xev.xbutton.button == 8) resetView();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   373
					else if (xev.xbutton.button == 9) keepRunningX11 = false;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   374
					redraw = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   375
				} else if (xev.type == MotionNotify) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   376
					// printCursorInfo(xev.xmotion);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   377
				} else if (xev.type == ConfigureNotify) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   378
					std::cout << "XEvent: ConfigureNotify:"
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   379
							<< " window=" << xev.xconfigure.window
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   380
							<< " height=" << xev.xconfigure.height
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   381
							<< " width=" << xev.xconfigure.width
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   382
							<< std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   383
					if (listenToRootResizes
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   384
							&& xev.xconfigure.window == parent) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   385
						XResizeWindow(dpy, win,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   386
								xev.xconfigure.width, xev.xconfigure.height);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   387
					}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   388
				} else if (xev.type == UnmapNotify) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   389
					std::cout << "XEvent: UnmapNotify" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   390
				} else if (xev.type == DestroyNotify) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   391
					std::cout << "XEvent: DestroyNotify → finish" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   392
					break;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   393
				} else {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   394
					std::cout << "XEvent: type=" << xev.type << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   395
				}
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
   396
				if (XPending(dpy)) goto process_x11_event;
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   397
			} else if (epoll[epollEvent].data.fd == STDIN_FILENO) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   398
				int epollFD = epoll[epollEvent].data.fd;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   399
				logOutput << "other event: fd=" << epollFD << " data=";
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   400
				for (char ch; read(epollFD, &ch, 1) > 0;) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   401
					std::stringstream msg;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   402
					msg
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   403
							<< std::hex
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   404
							<< std::setfill('0')
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   405
							<< std::setw(2)
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   406
							<< (int) ch;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   407
					logOutput << msg.str();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   408
				}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   409
				logOutput << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   410
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
   411
			} 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
   412
				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
   413
				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
   414
					logOutput << "   "
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   415
							<< " 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
   416
							<< " 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
   417
							<< 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
   418
					try {
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   419
						redraw |= reloadShader(fe.fileName);
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   420
						setTitle();
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   421
					} 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
   422
						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
   423
						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
   424
								<< 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
   425
								<< "': " << 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
   426
					}
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
   427
				}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   428
			} else {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   429
				logOutput
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   430
						<< "error: event on an unexpected FD: "
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   431
						<< epoll[epollEvent].data.fd
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   432
						<< std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   433
			}
8
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   434
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   435
			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
   436
				runShaders();
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   437
				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
   438
			}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   439
		}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   440
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   441
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   442
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   443
void OHP3D::Impl::clear() {
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   444
	glClearColor(
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   445
			(cfg.backgroundColor >> 16 & 0xFF) / 256.,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   446
			(cfg.backgroundColor >> 8 & 0xFF) / 256.,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   447
			(cfg.backgroundColor & 0xFF) / 256.,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   448
			1.0);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   449
	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
   450
}
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   451
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   452
void OHP3D::Impl::runShaders() {
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   453
	shaderProgram->use();
3
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   454
	checkError(&std::cerr);
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   455
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   456
	clear();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   457
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   458
	GLint viewport[4];
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   459
	glGetIntegerv(GL_VIEWPORT, viewport);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   460
	GLfloat width = viewport[2];
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   461
	GLfloat height = viewport[3];
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   462
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   463
	glm::mat4 projection = glm::perspective(
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   464
			glm::radians(ctx.fov),
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   465
			width / height,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   466
			0.1f, 100.0f);
2
3faef2f5128e better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   467
	glUniformMatrix4fv(ProgAttr.uProjection, 1, GL_FALSE, &projection[0][0]);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   468
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   469
	glm::mat4 view = glm::lookAt(
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   470
			ctx.cameraPos,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   471
			ctx.cameraPos + ctx.cameraFront,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   472
			ctx.cameraUp);
2
3faef2f5128e better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   473
	glUniformMatrix4fv(ProgAttr.uView, 1, GL_FALSE, &view[0][0]);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   474
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   475
	// glBindVertexArray(vao);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   476
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   477
	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
   478
	glUniformMatrix4fv(ProgAttr.uModel, 1, GL_FALSE, &model[0][0]);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   479
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   480
	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
   481
		textures[i]->bind();
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   482
		glUniform1f(ProgAttr.uTextureScale, textures[i]->getScale());
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   483
		int pageVertices = 2 * 3; // see loadVertices()
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   484
		glDrawArrays(GL_TRIANGLES, i*pageVertices, pageVertices);
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   485
		// std::cerr << "GLSL: glDrawArrays() #" << i << std::endl;
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   486
	}
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
   487
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   488
	std::cerr << "GLSL: glDrawArrays() " << textures.size() << "x" << std::endl;
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   489
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   490
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   491
void OHP3D::Impl::log(LogLevel level, std::string message) {
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   492
	::log(logOutput, level, message);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   493
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   494
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   495
int OHP3D::Impl::setNonBlocking(int fd) {
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   496
	int flags = fcntl(fd, F_GETFL, 0);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   497
	fcntl(fd, F_SETFL, flags | O_NONBLOCK);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   498
	return fd;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   499
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   500
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   501
void OHP3D::Impl::loadVertices() {
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   502
	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
   503
	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
   504
		std::shared_ptr<Texture> tex = textures[i];
262828ae9682 OOP for Texture
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   505
		GLfloat ratio = tex->getRatio();
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   506
		GLfloat offset = ratio * 1.8 * i;
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   507
		const std::vector<GLfloat> v = {
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   508
			// Vertex XYZ                               Texture XY
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   509
			-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
   510
			+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
   511
			-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
   512
31
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, /**/ 0.0, 1.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, /**/ 1.0, 1.0,
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   515
			+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
   516
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
   517
			// 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
   518
		};
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   519
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   520
		// TODO: reduce copying
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   521
		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
   522
	}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   523
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   524
	// Vertex data:
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   525
	glVertexAttribPointer(ProgAttr.aVertexXYZ, 3, // vertex items
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   526
			GL_FLOAT, GL_FALSE, 5 * sizeof (float),
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   527
			(void*) 0);
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   528
	glEnableVertexAttribArray(ProgAttr.aVertexXYZ);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   529
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   530
	// Texture positions:
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   531
	glVertexAttribPointer(ProgAttr.aTextureXY, 2, // texture items
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   532
			GL_FLOAT, GL_FALSE, 5 * sizeof (float),
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   533
			(void*) (3 * sizeof (float)));
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   534
	glEnableVertexAttribArray(ProgAttr.aTextureXY);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   535
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   536
	glBufferData(GL_ARRAY_BUFFER,
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   537
			vertices.size() * sizeof (vertices[0]),
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   538
			vertices.data(),
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   539
			GL_STATIC_DRAW);
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   540
	// GL_STATIC_DRAW:
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   541
	//   The vertex data will be uploaded once
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   542
	//   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
   543
	// GL_DYNAMIC_DRAW:
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   544
	//   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
   545
	// 	 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
   546
	// GL_STREAM_DRAW:
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   547
	//   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
   548
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   549
	// 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
   550
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   551
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
   552
const std::string
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   553
OHP3D::Impl::getDefaultFile(const std::string& relativePath) {
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   554
	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
   555
	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
   556
	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
   557
		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
   558
	} 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
   559
		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
   560
				+ " 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
   561
				" 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
   562
	}
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
}
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
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   565
void OHP3D::Impl::loadDocuments() {
32
711d3d781143 default PDF document
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   566
	if (cfg.documents.empty())
711d3d781143 default PDF document
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   567
		cfg.documents.push_back({getDefaultFile("documents/default.pdf")});
711d3d781143 default PDF document
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   568
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   569
	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
   570
		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
   571
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   572
		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
   573
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   574
		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
   575
		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
   576
32
711d3d781143 default PDF document
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   577
		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
   578
				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
   579
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   580
		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
   581
				+ " 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
   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, "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
   584
		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
   585
		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
   586
		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
   587
		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
   588
		for (int i = 0, limit = doc->pages(); i < limit; i++) {
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   589
			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
   590
			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
   591
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   592
			PageBuffer img(
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   593
					pageImage.data(),
32
711d3d781143 default PDF document
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   594
					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
   595
					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
   596
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   597
			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
   598
					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
   599
					img, document.fileName));
31
ed3caeea978a render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents: 30
diff changeset
   600
			// 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
   601
			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
   602
					+ "/" + 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
   603
		}
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
   604
30
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   605
		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
   606
		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
   607
				(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
   608
02972f051744 import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   609
		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
   610
		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
   611
		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
   612
				<< " 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
   613
				<< " 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
   614
				<< 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
   615
		log(LogLevel::INFO, timingMsg.str());
32
711d3d781143 default PDF document
František Kučera <franta-hg@frantovo.cz>
parents: 31
diff changeset
   616
		if (timingTotal < 400000) logOutput << ">> Doherty threshold met! <<\n";
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   617
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   618
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   619
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   620
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
   621
	// 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
   622
	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
   623
	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
   624
	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
   625
	// 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
   626
	// 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
   627
	// what-are-vertex-array-objects
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   628
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
   629
	// 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
   630
	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
   631
	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
   632
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   633
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
   634
	{
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
   635
		// 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
   636
		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
   637
		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
   638
		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
   639
		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
   640
		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
   641
		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
   642
		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
   643
		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
   644
	}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   645
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
   646
	shaderProgram = std::make_shared<Program>();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   647
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
   648
	// 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
   649
	// glBindAttribLocation(program, LOC.input, "vertices");
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   650
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
   651
	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
   652
		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
   653
		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
   654
		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
   655
		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
   656
		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
   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
		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
   659
		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
   660
				type, file, fileName);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   661
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
   662
		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
   663
		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
   664
		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
   665
		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
   666
		// 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
   667
		// 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
   668
	}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   669
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
   670
	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
   671
	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
   672
	// 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
   673
	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
   674
}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   675
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   676
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
   677
	// 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
   678
	// 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
   679
	// 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
   680
	// 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
   681
	// 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
   682
	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
   683
	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
   684
	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
   685
	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
   686
	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
   687
	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
   688
	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
   689
	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
   690
	shaderProgram->bindFragDataLocation("fColor", ProgAttr.fColor);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   691
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   692
29
dc3c102e1264 derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents: 28
diff changeset
   693
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
   694
	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
   695
		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
   696
			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
   697
			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
   698
			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
   699
			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
   700
		}
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   701
	}
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   702
	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
   703
}