author | František Kučera <franta-hg@frantovo.cz> |
Wed, 27 Dec 2023 23:25:06 +0100 | |
branch | v_0 |
changeset 33 | 4620bba4fa40 |
parent 32 | 711d3d781143 |
child 34 | 7ea796b00538 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
29
dc3c102e1264
derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
2 |
* OHP3D |
0 | 3 |
* Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info) |
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
||
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 | 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 | 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 | 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 | 205 |
} |
206 |
||
29
dc3c102e1264
derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
207 |
OHP3D::~OHP3D() { |
19 | 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 | 218 |
} |
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 | 234 |
|
235 |
if (dpy == NULL) throw std::logic_error("Unable to connect to X server"); |
|
236 |
||
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 | 239 |
Window root = DefaultRootWindow(dpy); |
240 |
Window parent = cfg.rootWindow ? cfg.rootWindow : root; |
|
241 |
||
242 |
XSetWindowAttributes swa; |
|
243 |
swa.colormap = XCreateColormap(dpy, parent, vi->visual, AllocNone); |
|
244 |
swa.event_mask = ExposureMask | KeyPressMask | PointerMotionMask |
|
245 |
| ButtonPressMask |
|
246 |
| StructureNotifyMask; |
|
247 |
||
248 |
bool full = false; |
|
249 |
unsigned int width = 1600; |
|
250 |
unsigned int height = 1200; |
|
251 |
if (parent != root) { |
|
252 |
XWindowAttributes parentAttr; |
|
253 |
XGetWindowAttributes(dpy, parent, &parentAttr); |
|
254 |
width = parentAttr.width; |
|
255 |
height = parentAttr.height; |
|
256 |
} |
|
257 |
||
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
258 |
win = XCreateWindow( |
0 | 259 |
dpy, parent, 0, 0, width, height, 0, |
260 |
vi->depth, InputOutput, vi->visual, |
|
261 |
CWColormap | CWEventMask, &swa); |
|
262 |
||
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 | 265 |
setX11PID(dpy, win); |
266 |
// XSetWindowBackground(dpy, win, 0) vs. glClearColor() |
|
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 | 269 |
glXMakeCurrent(dpy, win, glc); |
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 | 281 |
loadVertices(); |
282 |
||
283 |
auto toggleFullscreen = [&]() { |
|
284 |
full = setFullscreen(dpy, win, !full); |
|
285 |
}; |
|
286 |
||
287 |
auto resetView = [&]() { |
|
288 |
ctx = initialCtx; |
|
289 |
ctx.updateCameraFrontAndUp(); |
|
290 |
}; |
|
291 |
||
292 |
// root can reize our window |
|
293 |
// or we can listen to root resize and then resize our window ourselves |
|
294 |
bool listenToRootResizes = true; |
|
295 |
if (listenToRootResizes) XSelectInput(dpy, parent, StructureNotifyMask); |
|
296 |
||
297 |
bool keepRunningX11 = true; |
|
298 |
int x11fd = XConnectionNumber(dpy); |
|
299 |
EPoll epoll; |
|
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 | 302 |
try { |
303 |
epoll.add(setNonBlocking(STDIN_FILENO)); |
|
304 |
} catch (const EPoll::Exception& e) { |
|
305 |
logOutput << "Will not monitor events on STDIN: " << e.what() << "\n"; |
|
306 |
} |
|
307 |
||
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 | 310 |
glXSwapBuffers(dpy, win); |
311 |
||
33
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
312 |
auto goPage = [&](int count) { |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
313 |
// TODO: support pages with different ratios |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
314 |
ctx.moveRight(count * 1.8 * textures[0]->getRatio()); |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
315 |
}; |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
316 |
|
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
317 |
auto goHome = [&]() { |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
318 |
ctx.cameraFront = initialCtx.cameraFront; |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
319 |
ctx.cameraPos = initialCtx.cameraPos; |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
320 |
ctx.cameraUp = initialCtx.cameraUp; |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
321 |
}; |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
322 |
|
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
323 |
auto goEnd = [&]() { |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
324 |
goHome(); |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
325 |
goPage(textures.size() - 1); |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
326 |
}; |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
327 |
|
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
328 |
auto goPageMouse = [&](XButtonEvent ev) { |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
329 |
XWindowAttributes gwa; |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
330 |
XGetWindowAttributes(dpy, win, &gwa); |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
331 |
|
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
332 |
bool top = ev.y < gwa.height / 2, bottom = !top; |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
333 |
bool left = ev.x < gwa.width / 2, right = !left; |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
334 |
|
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
335 |
if (top && left) goHome(); |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
336 |
else if (top && right) goEnd(); |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
337 |
else if (bottom && left) goPage(-1); |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
338 |
else if (bottom && right) goPage(+1); |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
339 |
}; |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
340 |
|
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
341 |
|
0 | 342 |
for (XEvent xev; keepRunningX11;) { |
343 |
int epollEventCount = epoll.wait(); |
|
344 |
//std::cout << "trace: epoll.wait() = " << epollEventCount << std::endl; |
|
345 |
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
|
346 |
bool redraw = false; |
0 | 347 |
if (epoll[epollEvent].data.fd == x11fd) { |
348 |
if (!XPending(dpy)) { |
|
349 |
// otherwise STDIN events are held until the first X11 event |
|
350 |
logOutput << "trace: no pending X11 event" << std::endl; |
|
351 |
break; |
|
352 |
} |
|
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
|
353 |
process_x11_event: |
0 | 354 |
XWindowAttributes gwa; |
355 |
XNextEvent(dpy, &xev); |
|
356 |
||
357 |
if (xev.type == Expose) { |
|
358 |
std::cout << "XEvent: Expose" << std::endl; |
|
359 |
XGetWindowAttributes(dpy, win, &gwa); |
|
360 |
glViewport(0, 0, gwa.width, gwa.height); |
|
361 |
redraw = true; |
|
362 |
} else if (xev.type == KeyPress) { |
|
363 |
DecodedKey key = decodeKeycode(dpy, xev.xkey.keycode); |
|
364 |
std::cout << "XEvent: KeyPress:" |
|
365 |
<< " keycode=" << key.code |
|
366 |
<< " key=" << key.name |
|
367 |
<< std::endl; |
|
368 |
||
369 |
const float cSp = 0.05f; // camera speed |
|
370 |
const float aSp = 5.f; // angle speed |
|
371 |
||
372 |
if (key.matches(XK_q, XK_Escape)) keepRunningX11 = false; |
|
373 |
else if (key.matches(XK_Left, XK_s)) ctx.turnLeft(aSp); |
|
374 |
else if (key.matches(XK_Right, XK_f)) ctx.turnRight(aSp); |
|
375 |
else if (key.matches(XK_Up, XK_e)) ctx.moveForward(cSp); |
|
376 |
else if (key.matches(XK_Down, XK_d)) ctx.moveBackward(cSp); |
|
377 |
else if (key.matches(XK_w)) ctx.rollLeft(aSp); |
|
378 |
else if (key.matches(XK_r)) ctx.rollRight(aSp); |
|
379 |
else if (key.matches(XK_t)) ctx.turnUp(aSp); |
|
380 |
else if (key.matches(XK_g)) ctx.turnDown(aSp); |
|
381 |
else if (key.matches(XK_m)) ctx.moveLeft(cSp); |
|
382 |
else if (key.matches(XK_comma)) ctx.moveRight(cSp); |
|
383 |
else if (key.matches(XK_l)) ctx.moveUp(cSp); |
|
384 |
else if (key.matches(XK_period)) ctx.moveDown(cSp); |
|
33
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
385 |
else if (key.matches(XK_j, XK_Page_Up)) goPage(-1); |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
386 |
else if (key.matches(XK_k, XK_Page_Down)) goPage(+1); |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
387 |
else if (key.matches(XK_u, XK_Home)) goHome(); |
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
388 |
else if (key.matches(XK_i, XK_End)) goEnd(); |
0 | 389 |
else if (key.matches(XK_x)) resetView(); |
390 |
else if (key.matches(XK_F11, XK_y)) toggleFullscreen(); |
|
391 |
redraw = true; |
|
392 |
} else if (xev.type == ButtonPress) { |
|
393 |
std::cout << "XEvent: ButtonPress:" |
|
394 |
<< " button=" << xev.xbutton.button |
|
395 |
<< std::endl; |
|
33
4620bba4fa40
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()
František Kučera <franta-hg@frantovo.cz>
parents:
32
diff
changeset
|
396 |
if (xev.xbutton.button == 1) goPageMouse(xev.xbutton); |
9
53ba7e52c67c
mouse wheel: move camera, not object
František Kučera <franta-hg@frantovo.cz>
parents:
8
diff
changeset
|
397 |
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
|
398 |
else if (xev.xbutton.button == 5) ctx.adjustFov(+1.0); |
0 | 399 |
else if (xev.xbutton.button == 8) resetView(); |
400 |
else if (xev.xbutton.button == 9) keepRunningX11 = false; |
|
401 |
redraw = true; |
|
402 |
} else if (xev.type == MotionNotify) { |
|
403 |
// printCursorInfo(xev.xmotion); |
|
404 |
} else if (xev.type == ConfigureNotify) { |
|
405 |
std::cout << "XEvent: ConfigureNotify:" |
|
406 |
<< " window=" << xev.xconfigure.window |
|
407 |
<< " height=" << xev.xconfigure.height |
|
408 |
<< " width=" << xev.xconfigure.width |
|
409 |
<< std::endl; |
|
410 |
if (listenToRootResizes |
|
411 |
&& xev.xconfigure.window == parent) { |
|
412 |
XResizeWindow(dpy, win, |
|
413 |
xev.xconfigure.width, xev.xconfigure.height); |
|
414 |
} |
|
415 |
} else if (xev.type == UnmapNotify) { |
|
416 |
std::cout << "XEvent: UnmapNotify" << std::endl; |
|
417 |
} else if (xev.type == DestroyNotify) { |
|
418 |
std::cout << "XEvent: DestroyNotify → finish" << std::endl; |
|
419 |
break; |
|
420 |
} else { |
|
421 |
std::cout << "XEvent: type=" << xev.type << std::endl; |
|
422 |
} |
|
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
|
423 |
if (XPending(dpy)) goto process_x11_event; |
0 | 424 |
} else if (epoll[epollEvent].data.fd == STDIN_FILENO) { |
425 |
int epollFD = epoll[epollEvent].data.fd; |
|
426 |
logOutput << "other event: fd=" << epollFD << " data="; |
|
427 |
for (char ch; read(epollFD, &ch, 1) > 0;) { |
|
428 |
std::stringstream msg; |
|
429 |
msg |
|
430 |
<< std::hex |
|
431 |
<< std::setfill('0') |
|
432 |
<< std::setw(2) |
|
433 |
<< (int) ch; |
|
434 |
logOutput << msg.str(); |
|
435 |
} |
|
436 |
logOutput << std::endl; |
|
437 |
||
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
|
438 |
} 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
|
439 |
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
|
440 |
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
|
441 |
logOutput << " " |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
442 |
<< " 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
|
443 |
<< " 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
|
444 |
<< 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
|
445 |
try { |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
446 |
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
|
447 |
setTitle(); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
448 |
} 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
|
449 |
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
|
450 |
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
|
451 |
<< 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
|
452 |
<< "': " << 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
|
453 |
} |
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
|
454 |
} |
0 | 455 |
} else { |
456 |
logOutput |
|
457 |
<< "error: event on an unexpected FD: " |
|
458 |
<< epoll[epollEvent].data.fd |
|
459 |
<< std::endl; |
|
460 |
} |
|
8
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
461 |
|
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
462 |
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
|
463 |
runShaders(); |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
464 |
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
|
465 |
} |
0 | 466 |
} |
467 |
} |
|
468 |
} |
|
469 |
||
29
dc3c102e1264
derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
470 |
void OHP3D::Impl::clear() { |
0 | 471 |
glClearColor( |
472 |
(cfg.backgroundColor >> 16 & 0xFF) / 256., |
|
473 |
(cfg.backgroundColor >> 8 & 0xFF) / 256., |
|
474 |
(cfg.backgroundColor & 0xFF) / 256., |
|
475 |
1.0); |
|
476 |
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
|
477 |
} |
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
478 |
|
29
dc3c102e1264
derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
479 |
void OHP3D::Impl::runShaders() { |
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
3
diff
changeset
|
480 |
shaderProgram->use(); |
3
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
481 |
checkError(&std::cerr); |
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
482 |
|
48dc4ae894b0
clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
483 |
clear(); |
0 | 484 |
|
485 |
GLint viewport[4]; |
|
486 |
glGetIntegerv(GL_VIEWPORT, viewport); |
|
487 |
GLfloat width = viewport[2]; |
|
488 |
GLfloat height = viewport[3]; |
|
489 |
||
490 |
glm::mat4 projection = glm::perspective( |
|
491 |
glm::radians(ctx.fov), |
|
492 |
width / height, |
|
493 |
0.1f, 100.0f); |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
494 |
glUniformMatrix4fv(ProgAttr.uProjection, 1, GL_FALSE, &projection[0][0]); |
0 | 495 |
|
496 |
glm::mat4 view = glm::lookAt( |
|
497 |
ctx.cameraPos, |
|
498 |
ctx.cameraPos + ctx.cameraFront, |
|
499 |
ctx.cameraUp); |
|
2
3faef2f5128e
better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
500 |
glUniformMatrix4fv(ProgAttr.uView, 1, GL_FALSE, &view[0][0]); |
0 | 501 |
|
502 |
// glBindVertexArray(vao); |
|
503 |
||
504 |
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
|
505 |
glUniformMatrix4fv(ProgAttr.uModel, 1, GL_FALSE, &model[0][0]); |
0 | 506 |
|
31
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
507 |
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
|
508 |
textures[i]->bind(); |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
509 |
glUniform1f(ProgAttr.uTextureScale, textures[i]->getScale()); |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
510 |
int pageVertices = 2 * 3; // see loadVertices() |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
511 |
glDrawArrays(GL_TRIANGLES, i*pageVertices, pageVertices); |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
512 |
// std::cerr << "GLSL: glDrawArrays() #" << i << std::endl; |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
513 |
} |
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
|
514 |
|
31
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
515 |
std::cerr << "GLSL: glDrawArrays() " << textures.size() << "x" << std::endl; |
0 | 516 |
} |
517 |
||
29
dc3c102e1264
derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
518 |
void OHP3D::Impl::log(LogLevel level, std::string message) { |
0 | 519 |
::log(logOutput, level, message); |
520 |
} |
|
521 |
||
29
dc3c102e1264
derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
522 |
int OHP3D::Impl::setNonBlocking(int fd) { |
0 | 523 |
int flags = fcntl(fd, F_GETFL, 0); |
524 |
fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
|
525 |
return fd; |
|
526 |
} |
|
527 |
||
29
dc3c102e1264
derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
528 |
void OHP3D::Impl::loadVertices() { |
31
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
529 |
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
|
530 |
for (int i = 0; i < textures.size(); i++) { |
19 | 531 |
std::shared_ptr<Texture> tex = textures[i]; |
532 |
GLfloat ratio = tex->getRatio(); |
|
31
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
533 |
GLfloat offset = ratio * 1.8 * i; |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
534 |
const std::vector<GLfloat> v = { |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
535 |
// Vertex XYZ Texture XY |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
536 |
-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
|
537 |
+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
|
538 |
-0.80f * ratio + offset, -0.80f, +0.0, /**/ 0.0, 1.0, |
0 | 539 |
|
31
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
540 |
-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
|
541 |
+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
|
542 |
+0.80f * ratio + offset, +0.80f, +0.0, /**/ 1.0, 0.0, |
0 | 543 |
|
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
|
544 |
// 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
|
545 |
}; |
0 | 546 |
|
31
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
547 |
// TODO: reduce copying |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
548 |
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
|
549 |
} |
0 | 550 |
|
31
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
551 |
// Vertex data: |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
552 |
glVertexAttribPointer(ProgAttr.aVertexXYZ, 3, // vertex items |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
553 |
GL_FLOAT, GL_FALSE, 5 * sizeof (float), |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
554 |
(void*) 0); |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
555 |
glEnableVertexAttribArray(ProgAttr.aVertexXYZ); |
0 | 556 |
|
31
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
557 |
// Texture positions: |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
558 |
glVertexAttribPointer(ProgAttr.aTextureXY, 2, // texture items |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
559 |
GL_FLOAT, GL_FALSE, 5 * sizeof (float), |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
560 |
(void*) (3 * sizeof (float))); |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
561 |
glEnableVertexAttribArray(ProgAttr.aTextureXY); |
0 | 562 |
|
31
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
563 |
glBufferData(GL_ARRAY_BUFFER, |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
564 |
vertices.size() * sizeof (vertices[0]), |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
565 |
vertices.data(), |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
566 |
GL_STATIC_DRAW); |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
567 |
// GL_STATIC_DRAW: |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
568 |
// The vertex data will be uploaded once |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
569 |
// 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
|
570 |
// GL_DYNAMIC_DRAW: |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
571 |
// 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
|
572 |
// 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
|
573 |
// GL_STREAM_DRAW: |
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
574 |
// 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
|
575 |
|
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
576 |
// see also glBindBuffer(GL_ARRAY_BUFFER, vbo); where we set current VBO |
0 | 577 |
} |
578 |
||
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
|
579 |
const std::string |
29
dc3c102e1264
derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
580 |
OHP3D::Impl::getDefaultFile(const std::string& relativePath) { |
dc3c102e1264
derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
581 |
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
|
582 |
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
|
583 |
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
|
584 |
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
|
585 |
} 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
|
586 |
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
|
587 |
+ " 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
|
588 |
" 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
|
589 |
} |
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
|
590 |
} |
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
|
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 |
void OHP3D::Impl::loadDocuments() { |
32
711d3d781143
default PDF document
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
593 |
if (cfg.documents.empty()) |
711d3d781143
default PDF document
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
594 |
cfg.documents.push_back({getDefaultFile("documents/default.pdf")}); |
711d3d781143
default PDF document
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
595 |
|
30
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
596 |
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
|
597 |
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
|
598 |
|
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
599 |
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
|
600 |
|
30
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
601 |
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
|
602 |
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
|
603 |
|
32
711d3d781143
default PDF document
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
604 |
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
|
605 |
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
|
606 |
|
30
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
607 |
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
|
608 |
+ " 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
|
609 |
|
30
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
610 |
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
|
611 |
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
|
612 |
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
|
613 |
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
|
614 |
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
|
615 |
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
|
616 |
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
|
617 |
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
|
618 |
|
30
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
619 |
PageBuffer img( |
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
620 |
pageImage.data(), |
32
711d3d781143
default PDF document
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
621 |
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
|
622 |
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
|
623 |
|
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
624 |
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
|
625 |
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
|
626 |
img, document.fileName)); |
31
ed3caeea978a
render all PDF pages
František Kučera <franta-hg@frantovo.cz>
parents:
30
diff
changeset
|
627 |
// 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
|
628 |
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
|
629 |
+ "/" + 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
|
630 |
} |
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
|
631 |
|
30
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
632 |
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
|
633 |
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
|
634 |
(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
|
635 |
|
02972f051744
import PDF loading code from the OHP3D private prototype
František Kučera <franta-hg@frantovo.cz>
parents:
29
diff
changeset
|
636 |
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
|
637 |
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
|
638 |
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
|
639 |
<< " 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
|
640 |
<< " 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
|
641 |
<< 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
|
642 |
log(LogLevel::INFO, timingMsg.str()); |
32
711d3d781143
default PDF document
František Kučera <franta-hg@frantovo.cz>
parents:
31
diff
changeset
|
643 |
if (timingTotal < 400000) logOutput << ">> Doherty threshold met! <<\n"; |
0 | 644 |
} |
645 |
} |
|
646 |
||
29
dc3c102e1264
derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
647 |
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
|
648 |
// 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
|
649 |
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
|
650 |
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
|
651 |
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
|
652 |
// 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
|
653 |
// 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
|
654 |
// what-are-vertex-array-objects |
0 | 655 |
|
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
|
656 |
// 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
|
657 |
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
|
658 |
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
|
659 |
glBindBuffer(GL_ARRAY_BUFFER, vbo); |
0 | 660 |
|
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
|
661 |
{ |
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 |
// 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
|
663 |
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
|
664 |
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
|
665 |
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
|
666 |
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
|
667 |
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
|
668 |
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
|
669 |
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
|
670 |
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
|
671 |
} |
0 | 672 |
|
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
|
673 |
shaderProgram = std::make_shared<Program>(); |
0 | 674 |
|
28
4cbd9c0beb4c
pass texture xattr 'shader-shark.texture.scale' to the shaders as 'uTextureScale' + update variable locations on each shader reload
František Kučera <franta-hg@frantovo.cz>
parents:
27
diff
changeset
|
675 |
// 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
|
676 |
// glBindAttribLocation(program, LOC.input, "vertices"); |
0 | 677 |
|
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
|
678 |
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
|
679 |
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
|
680 |
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
|
681 |
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
|
682 |
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
|
683 |
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
|
684 |
|
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
|
685 |
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
|
686 |
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
|
687 |
type, file, fileName); |
0 | 688 |
|
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
|
689 |
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
|
690 |
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
|
691 |
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
|
692 |
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
|
693 |
// 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
|
694 |
// 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
|
695 |
} |
0 | 696 |
|
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
|
697 |
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
|
698 |
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
|
699 |
// 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
|
700 |
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
|
701 |
} |
0 | 702 |
|
29
dc3c102e1264
derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
703 |
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
|
704 |
// 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
|
705 |
// 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
|
706 |
// 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
|
707 |
// 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
|
708 |
// 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
|
709 |
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
|
710 |
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
|
711 |
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
|
712 |
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
|
713 |
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
|
714 |
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
|
715 |
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
|
716 |
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
|
717 |
shaderProgram->bindFragDataLocation("fColor", ProgAttr.fColor); |
0 | 718 |
} |
719 |
||
29
dc3c102e1264
derive OHP3D from ShaderShark
František Kučera <franta-hg@frantovo.cz>
parents:
28
diff
changeset
|
720 |
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
|
721 |
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
|
722 |
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
|
723 |
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
|
724 |
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
|
725 |
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
|
726 |
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
|
727 |
} |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
728 |
} |
80ad08521091
monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
729 |
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
|
730 |
} |