Shark.cpp
author František Kučera <franta-hg@frantovo.cz>
Sat, 02 Dec 2023 23:11:56 +0100
branchv_0
changeset 11 0aeedc35ebed
parent 10 8382173bfc35
child 12 076e3b2d97ac
permissions -rw-r--r--
use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * ShaderShark
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
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>
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    21
#include <memory>
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    22
#include <functional>
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    23
#include <sstream>
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    24
#include <vector>
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    25
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    26
#include "x11.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    27
#include "opengl.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    28
#include "EPoll.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    29
#include "Logger.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    30
#include "MappedFile.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    31
#include "ImageLoader.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    32
#include "Shader.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    33
#include "Program.h"
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    34
#include "FileMonitor.h"
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    35
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include "Shark.h"
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    38
class Shark::Impl {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    39
public:
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    40
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    41
	struct {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    42
		GLint aVertexXYZ = -2;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    43
		GLint aTextureXY = -2;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    44
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    45
		GLint fColor = -2;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    46
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    47
		GLint uModel = -2;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    48
		GLint uView = -2;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    49
		GLint uProjection = -2;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    50
		GLint uTexture = -2;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    51
	} ProgAttr;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    52
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    53
	struct {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    54
		float yaw = -90.f;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    55
		float pitch = 0.f;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    56
		float roll = 0.f;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    57
		float fov = 45.0f;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    58
		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
    59
		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
    60
		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
    61
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    62
		void adjustFov(float diff) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    63
			fov += diff;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    64
			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
    65
			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
    66
			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
    67
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    68
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    69
		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
    70
			cameraPos += cameraSpeed * cameraFront;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    71
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    72
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    73
		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
    74
			cameraPos -= cameraSpeed * cameraFront;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    75
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    76
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    77
		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
    78
			cameraPos -= glm::normalize(
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    79
					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
    80
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    81
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    82
		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
    83
			cameraPos += glm::normalize(
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    84
					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
    85
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    86
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    87
		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
    88
			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
    89
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    90
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    91
		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
    92
			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
    93
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    94
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    95
		void updateCameraFrontAndUp() {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    96
			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
    97
			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
    98
			dump("cameraPos", cameraPos);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    99
			dump("cameraFront", cameraFront);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   100
			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
   101
			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
   102
			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
   103
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   104
			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
   105
			cameraFront.y = sin(pitchR);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   106
			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
   107
			cameraFront = glm::normalize(cameraFront);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   108
			dump("cameraFront", cameraFront);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   109
			dump("cameraUp", cameraUp);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   110
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   111
			// 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
   112
			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
   113
					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
   114
			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
   115
			dump("cameraUp", cameraUp);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   116
			std::cerr << "-------------------------------------" << std::endl;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   117
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   118
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   119
		void limitPitch() {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   120
			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
   121
			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
   122
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   123
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   124
		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
   125
			yaw -= angleSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   126
			updateCameraFrontAndUp();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   127
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   128
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   129
		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
   130
			yaw += angleSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   131
			updateCameraFrontAndUp();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   132
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   133
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   134
		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
   135
			pitch += angleSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   136
			limitPitch();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   137
			updateCameraFrontAndUp();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   138
		}
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
		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
   141
			pitch -= angleSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   142
			limitPitch();
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 rollLeft(const float angleSpeed) {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   147
			roll += angleSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   148
			updateCameraFrontAndUp();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   149
		}
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
		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
   152
			roll -= angleSpeed;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   153
			updateCameraFrontAndUp();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   154
		}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   155
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   156
	} initialCtx, ctx;
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
	class Texture {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   159
	public:
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   160
		GLuint id;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   161
		std::string fileName;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   162
		int width;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   163
		int height;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   164
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   165
		GLfloat getRatio() const {
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   166
			return (GLfloat) width / (GLfloat) height;
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
	};
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
	ImageLoader imageLoader;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   178
	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
   179
	std::shared_ptr<Program> shaderProgram;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   180
	std::vector<Texture> textures;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   181
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   182
	Configuration cfg;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   183
	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
   184
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   185
	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
   186
	}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   187
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   188
	void run();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   189
	void clear();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   190
	void runShaders();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   191
	Window getRootWindow(Window defaultValue);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   192
	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
   193
	int setNonBlocking(int fd);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   194
	void loadVertices();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   195
	Texture loadTexture(const std::string& fileName);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   196
	bool reloadTexture(const std::string& fileName);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   197
	void loadTextures();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   198
	std::shared_ptr<Program> loadShaders();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   199
	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
   200
	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
   201
	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
   202
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   203
};
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   204
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   205
Shark::Shark(const Configuration& configuration) :
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   206
impl(new Impl(configuration)) {
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   207
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   208
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   209
Shark::~Shark() {
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   210
	// TODO: more SBRM
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   211
	impl->shaders.clear();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   212
	impl->shaderProgram = nullptr;
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   213
	XFree(impl->vi);
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   214
	// for (auto page : pdfTextures) glDeleteTextures(1, &page.texture);
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   215
	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
   216
	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
   217
	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
   218
	XCloseDisplay(impl->dpy);
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   219
	delete impl;
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   220
	// std::cerr << "~Shark()" << std::endl;
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   221
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   222
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   223
void Shark::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
   224
	std::stringstream title;
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   225
	title << "ShaderShark";
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   226
	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
   227
	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
   228
	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
   229
}
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   230
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   231
void Shark::run() {
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   232
	impl->run();
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   233
}
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   234
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   235
void Shark::Impl::run() {
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   236
	dpy = XOpenDisplay(NULL);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   237
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   238
	if (dpy == NULL) throw std::logic_error("Unable to connect to X server");
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   239
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   240
	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
   241
	vi = glXChooseVisual(dpy, 0, att);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   242
	Window root = DefaultRootWindow(dpy);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   243
	Window parent = cfg.rootWindow ? cfg.rootWindow : root;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   244
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   245
	XSetWindowAttributes swa;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   246
	swa.colormap = XCreateColormap(dpy, parent, vi->visual, AllocNone);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   247
	swa.event_mask = ExposureMask | KeyPressMask | PointerMotionMask
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   248
			| ButtonPressMask
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   249
			| StructureNotifyMask;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   250
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   251
	bool full = false;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   252
	unsigned int width = 1600;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   253
	unsigned int height = 1200;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   254
	if (parent != root) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   255
		XWindowAttributes parentAttr;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   256
		XGetWindowAttributes(dpy, parent, &parentAttr);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   257
		width = parentAttr.width;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   258
		height = parentAttr.height;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   259
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   260
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   261
	win = XCreateWindow(
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   262
			dpy, parent, 0, 0, width, height, 0,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   263
			vi->depth, InputOutput, vi->visual,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   264
			CWColormap | CWEventMask, &swa);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   265
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   266
	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
   267
	setTitle();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   268
	setX11PID(dpy, win);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   269
	// XSetWindowBackground(dpy, win, 0) vs. glClearColor()
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   270
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   271
	glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   272
	glXMakeCurrent(dpy, win, glc);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   273
3
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   274
	clear();
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   275
	glXSwapBuffers(dpy, win);
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   276
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   277
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   278
	// Load GLSL shaders:
3
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   279
	shaderProgram = loadShaders();
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   280
	loadTextures();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   281
	loadVertices();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   282
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   283
	auto toggleFullscreen = [&]() {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   284
		full = setFullscreen(dpy, win, !full);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   285
	};
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   286
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   287
	auto resetView = [&]() {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   288
		ctx = initialCtx;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   289
		ctx.updateCameraFrontAndUp();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   290
	};
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   291
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   292
	// root can reize our window
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   293
	// or we can listen to root resize and then resize our window ourselves
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   294
	bool listenToRootResizes = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   295
	if (listenToRootResizes) XSelectInput(dpy, parent, StructureNotifyMask);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   296
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   297
	bool keepRunningX11 = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   298
	int x11fd = XConnectionNumber(dpy);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   299
	EPoll epoll;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   300
	epoll.add(x11fd);
7
e6065118326f monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   301
	epoll.add(fileMonitor.getFD());
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   302
	try {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   303
		epoll.add(setNonBlocking(STDIN_FILENO));
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   304
	} catch (const EPoll::Exception& e) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   305
		logOutput << "Will not monitor events on STDIN: " << e.what() << "\n";
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   306
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   307
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   308
	// rended the 3D scene even before the first event:
3
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   309
	runShaders();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   310
	glXSwapBuffers(dpy, win);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   311
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   312
	for (XEvent xev; keepRunningX11;) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   313
		int epollEventCount = epoll.wait();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   314
		//std::cout << "trace: epoll.wait() = " << epollEventCount << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   315
		for (int epollEvent = 0; epollEvent < epollEventCount; epollEvent++) {
8
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   316
			bool redraw = false;
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   317
			if (epoll[epollEvent].data.fd == x11fd) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   318
				if (!XPending(dpy)) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   319
					// otherwise STDIN events are held until the first X11 event
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   320
					logOutput << "trace: no pending X11 event" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   321
					break;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   322
				}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   323
				XWindowAttributes gwa;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   324
				XNextEvent(dpy, &xev);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   325
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   326
				if (xev.type == Expose) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   327
					std::cout << "XEvent: Expose" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   328
					XGetWindowAttributes(dpy, win, &gwa);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   329
					glViewport(0, 0, gwa.width, gwa.height);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   330
					redraw = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   331
				} else if (xev.type == KeyPress) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   332
					DecodedKey key = decodeKeycode(dpy, xev.xkey.keycode);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   333
					std::cout << "XEvent: KeyPress:"
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   334
							<< " keycode=" << key.code
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   335
							<< " key=" << key.name
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   336
							<< std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   337
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   338
					const float cSp = 0.05f; // camera speed
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   339
					const float aSp = 5.f; // angle speed
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   340
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   341
					if (key.matches(XK_q, XK_Escape)) keepRunningX11 = false;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   342
					else if (key.matches(XK_Left, XK_s)) ctx.turnLeft(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   343
					else if (key.matches(XK_Right, XK_f)) ctx.turnRight(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   344
					else if (key.matches(XK_Up, XK_e)) ctx.moveForward(cSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   345
					else if (key.matches(XK_Down, XK_d)) ctx.moveBackward(cSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   346
					else if (key.matches(XK_w)) ctx.rollLeft(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   347
					else if (key.matches(XK_r)) ctx.rollRight(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   348
					else if (key.matches(XK_t)) ctx.turnUp(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   349
					else if (key.matches(XK_g)) ctx.turnDown(aSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   350
					else if (key.matches(XK_m)) ctx.moveLeft(cSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   351
					else if (key.matches(XK_comma)) ctx.moveRight(cSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   352
					else if (key.matches(XK_l)) ctx.moveUp(cSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   353
					else if (key.matches(XK_period)) ctx.moveDown(cSp);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   354
					else if (key.matches(XK_j)) ctx.moveLeft(cSp * 5);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   355
					else if (key.matches(XK_k)) ctx.moveRight(cSp * 5);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   356
					else if (key.matches(XK_u)) ctx.moveLeft(cSp * 10);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   357
					else if (key.matches(XK_i)) ctx.moveRight(cSp * 10);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   358
					else if (key.matches(XK_x)) resetView();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   359
					else if (key.matches(XK_F11, XK_y)) toggleFullscreen();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   360
					redraw = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   361
				} else if (xev.type == ButtonPress) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   362
					std::cout << "XEvent: ButtonPress:"
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   363
							<< " button=" << xev.xbutton.button
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   364
							<< std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   365
					if (xev.xbutton.button == 1);
9
53ba7e52c67c mouse wheel: move camera, not object
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   366
					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
   367
					else if (xev.xbutton.button == 5) ctx.adjustFov(+1.0);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   368
					else if (xev.xbutton.button == 8) resetView();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   369
					else if (xev.xbutton.button == 9) keepRunningX11 = false;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   370
					redraw = true;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   371
				} else if (xev.type == MotionNotify) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   372
					// printCursorInfo(xev.xmotion);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   373
				} else if (xev.type == ConfigureNotify) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   374
					std::cout << "XEvent: ConfigureNotify:"
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   375
							<< " window=" << xev.xconfigure.window
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   376
							<< " height=" << xev.xconfigure.height
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   377
							<< " width=" << xev.xconfigure.width
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   378
							<< std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   379
					if (listenToRootResizes
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   380
							&& xev.xconfigure.window == parent) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   381
						XResizeWindow(dpy, win,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   382
								xev.xconfigure.width, xev.xconfigure.height);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   383
					}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   384
				} else if (xev.type == UnmapNotify) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   385
					std::cout << "XEvent: UnmapNotify" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   386
				} else if (xev.type == DestroyNotify) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   387
					std::cout << "XEvent: DestroyNotify → finish" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   388
					break;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   389
				} else {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   390
					std::cout << "XEvent: type=" << xev.type << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   391
				}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   392
			} else if (epoll[epollEvent].data.fd == STDIN_FILENO) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   393
				int epollFD = epoll[epollEvent].data.fd;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   394
				logOutput << "other event: fd=" << epollFD << " data=";
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   395
				for (char ch; read(epollFD, &ch, 1) > 0;) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   396
					std::stringstream msg;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   397
					msg
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   398
							<< std::hex
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   399
							<< std::setfill('0')
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   400
							<< std::setw(2)
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   401
							<< (int) ch;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   402
					logOutput << msg.str();
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   403
				}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   404
				logOutput << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   405
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
   406
			} 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
   407
				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
   408
				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
   409
					logOutput << "   "
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   410
							<< " 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
   411
							<< " 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
   412
							<< 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
   413
					try {
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   414
						redraw |= reloadTexture(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
   415
						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
   416
						setTitle();
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   417
					} 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
   418
						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
   419
						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
   420
								<< 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
   421
								<< "': " << 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
   422
					}
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
   423
				}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   424
			} else {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   425
				logOutput
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   426
						<< "error: event on an unexpected FD: "
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   427
						<< epoll[epollEvent].data.fd
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   428
						<< std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   429
			}
8
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   430
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   431
			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
   432
				runShaders();
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   433
				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
   434
			}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   435
		}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   436
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   437
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   438
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   439
void Shark::Impl::clear() {
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   440
	glClearColor(
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   441
			(cfg.backgroundColor >> 16 & 0xFF) / 256.,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   442
			(cfg.backgroundColor >> 8 & 0xFF) / 256.,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   443
			(cfg.backgroundColor & 0xFF) / 256.,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   444
			1.0);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   445
	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
   446
}
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   447
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   448
void Shark::Impl::runShaders() {
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   449
	shaderProgram->use();
3
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   450
	checkError(&std::cerr);
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   451
48dc4ae894b0 clear screen / paint background before loading textures
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   452
	clear();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   453
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   454
	GLint viewport[4];
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   455
	glGetIntegerv(GL_VIEWPORT, viewport);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   456
	GLfloat width = viewport[2];
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   457
	GLfloat height = viewport[3];
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   458
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   459
	glm::mat4 projection = glm::perspective(
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   460
			glm::radians(ctx.fov),
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   461
			width / height,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   462
			0.1f, 100.0f);
2
3faef2f5128e better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   463
	glUniformMatrix4fv(ProgAttr.uProjection, 1, GL_FALSE, &projection[0][0]);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   464
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   465
	glm::mat4 view = glm::lookAt(
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   466
			ctx.cameraPos,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   467
			ctx.cameraPos + ctx.cameraFront,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   468
			ctx.cameraUp);
2
3faef2f5128e better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   469
	glUniformMatrix4fv(ProgAttr.uView, 1, GL_FALSE, &view[0][0]);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   470
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   471
	// glBindVertexArray(vao);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   472
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   473
	glm::mat4 model = glm::mat4(1.0f); // identity matrix
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   474
	// model = glm::translate(model, glm::vec3(0., 0., 0.));
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   475
	// float angle = 20.0f;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   476
	// glm::vec3 xxx = glm::vec3(1.0f, 0.3f, 0.5f);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   477
	// model = glm::rotate(model, glm::radians(angle), xxx);
2
3faef2f5128e better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   478
	glUniformMatrix4fv(ProgAttr.uModel, 1, GL_FALSE, &model[0][0]);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   479
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   480
	glDrawArrays(GL_TRIANGLES, 0, 2 * 3); // viz loadVertices() kde plníme data
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   481
	std::cerr << "GLSL: glDrawArrays()" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   482
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   483
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   484
void Shark::Impl::log(LogLevel level, std::string message) {
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   485
	::log(logOutput, level, message);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   486
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   487
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   488
int Shark::Impl::setNonBlocking(int fd) {
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   489
	int flags = fcntl(fd, F_GETFL, 0);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   490
	fcntl(fd, F_SETFL, flags | O_NONBLOCK);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   491
	return fd;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   492
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   493
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   494
void Shark::Impl::loadVertices() {
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   495
	for (int i = 0; i < textures.size(); i++) {
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   496
		const Texture& tex = textures[i];
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   497
		GLfloat ratio = tex.getRatio();
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   498
		const std::vector<GLfloat> vertices = {
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   499
			// Vertex XYZ                      Texture XY
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   500
			-0.80f * ratio, +0.80f, +0.0, /**/ 0.0, 0.0,
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   501
			+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   502
			-0.80f * ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   503
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   504
			-0.80f * ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   505
			+0.80f * ratio, -0.80f, +0.0, /**/ 1.0, 1.0,
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   506
			+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   507
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   508
			// viz glDrawArrays(), kde vybereme počátek a počet hodnot
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   509
		};
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   510
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   511
		// Vertex data:
2
3faef2f5128e better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   512
		glVertexAttribPointer(ProgAttr.aVertexXYZ, 3, // vertex items
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   513
				GL_FLOAT, GL_FALSE, 5 * sizeof (float),
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   514
				(void*) 0);
2
3faef2f5128e better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   515
		glEnableVertexAttribArray(ProgAttr.aVertexXYZ);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   516
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   517
		// Texture positions:
2
3faef2f5128e better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   518
		glVertexAttribPointer(ProgAttr.aTextureXY, 2, // texture items
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   519
				GL_FLOAT, GL_FALSE, 5 * sizeof (float),
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   520
				(void*) (3 * sizeof (float)));
2
3faef2f5128e better GLSL variable names
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   521
		glEnableVertexAttribArray(ProgAttr.aTextureXY);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   522
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   523
		glBufferData(GL_ARRAY_BUFFER,
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   524
				vertices.size() * sizeof (vertices[0]),
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   525
				vertices.data(),
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   526
				GL_STATIC_DRAW);
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   527
		// GL_STATIC_DRAW:
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   528
		//   The vertex data will be uploaded once
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   529
		//   and drawn many times(e.g. the world).
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   530
		// GL_DYNAMIC_DRAW:
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   531
		//   The vertex data will be created once, changed from
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   532
		// 	 time to time, but drawn many times more than that.
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   533
		// GL_STREAM_DRAW:
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   534
		//   The vertex data will be uploaded once and drawn once.
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   535
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   536
		// see also glBindBuffer(GL_ARRAY_BUFFER, vbo); where we set current VBO
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   537
	}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   538
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   539
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
   540
const std::string
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
   541
Shark::Shark::Impl::getDefaultFile(const std::string& 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
   542
	const char* envName = "SHADER_SHARK_DATA_DIR";
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
   543
	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
   544
	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
   545
		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
   546
	} 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
   547
		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
   548
				+ " 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
   549
				" 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
   550
	}
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
   551
}
0aeedc35ebed use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   552
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   553
Shark::Impl::Texture Shark::Impl::loadTexture(const std::string& fileName) {
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   554
	Texture tex;
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   555
	tex.fileName = fileName;
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   556
	MappedFile file(tex.fileName);
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   557
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   558
	std::shared_ptr<ImageLoader::ImageBuffer> img(imageLoader.loadImage(file));
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   559
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   560
	tex.width = img->width;
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   561
	tex.height = img->height;
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   562
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   563
	glGenTextures(1, &tex.id);
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   564
	glBindTexture(GL_TEXTURE_2D, tex.id);
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   565
	auto GLT2D = GL_TEXTURE_2D;
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   566
	glTexImage2D(GLT2D, 0, GL_RGBA,
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   567
			tex.width, tex.height,
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   568
			0, GL_RGBA, GL_UNSIGNED_BYTE,
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   569
			img->getData());
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   570
	glTexParameteri(GLT2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   571
	glTexParameteri(GLT2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   572
	glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   573
	glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   574
	glGenerateMipmap(GLT2D);
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   575
	std::cerr << "loadTexture(\"" << fileName.c_str() << "\", "
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   576
			<< tex.width << ", " << tex.height << ") = " << tex.id << std::endl;
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   577
	checkError(&std::cerr);
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   578
	return tex;
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   579
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   580
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   581
void Shark::Impl::loadTextures() {
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
	// Load default texture if there is no configured:
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 (cfg.textures.empty())
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
		cfg.textures.push_back({getDefaultFile("textures/default.png")});
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
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   586
	for (const Configuration::Texture& tex : cfg.textures) {
1
fb65455622b9 load textures from PNG, JPEG etc. files using ImageMagick
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   587
		textures.push_back(loadTexture(tex.fileName));
7
e6065118326f monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   588
		watchedFiles.push_back(fileMonitor.watch(tex.fileName));
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   589
		// TODO: review texture loading and binding
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   590
		// works even without this - default texture
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   591
		// glUniform1i(ProgAttr.jazz, jazz);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   592
		// checkError(&std::cerr);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   593
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   594
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   595
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   596
bool Shark::Impl::reloadTexture(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
   597
	for (const Configuration::Texture& tex : cfg.textures) {
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   598
		if (tex.fileName == fileName) {
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   599
			logOutput << "TODO: reload texture: " << fileName.c_str() << "\n";
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   600
			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
   601
		}
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   602
	}
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   603
	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
   604
}
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   605
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   606
std::shared_ptr<Program> Shark::Impl::loadShaders() {
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   607
	try {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   608
		// Vertex Array Object (VAO)
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   609
		GLuint vao;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   610
		glGenVertexArrays(1, &vao);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   611
		glBindVertexArray(vao);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   612
		// VAO - something like context for bound data/variables
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   613
		// We can switch multiple VAOs. VAO can contain multiple VBOs.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   614
		// See also https://stackoverflow.com/questions/11821336/
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   615
		// what-are-vertex-array-objects
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   616
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   617
		// Vertex Buffer Object (VBO):
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   618
		GLuint vbo;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   619
		glGenBuffers(1, &vbo);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   620
		glBindBuffer(GL_ARRAY_BUFFER, vbo);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   621
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
   622
		{
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
   623
			// Load default shaders if there are no configured:
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
   624
			int vc = 0;
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
   625
			int fc = 0;
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
   626
			auto& ss = cfg.shaders;
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
   627
			for (const auto& s : ss) if (s.type == "vertex") vc++;
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
   628
			for (const auto& s : ss) if (s.type == "fragment") fc++;
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
   629
			auto& d = getDefaultFile;
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
   630
			if (vc == 0) ss.push_back({d("shaders/default.vert"), "vertex"});
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
			if (fc == 0) ss.push_back({d("shaders/default.frag"), "fragment"});
6
fd93a46db15b support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   632
		}
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   633
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   634
		std::shared_ptr<Program> program = std::make_shared<Program>();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   635
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   636
		// glBindFragDataLocation(program, 0, "outColor");
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   637
		// glBindAttribLocation(program, LOC.input, "vertices");
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   638
6
fd93a46db15b support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   639
		for (const Configuration::Shader definition : cfg.shaders) {
fd93a46db15b support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   640
			Shader::Type type;
fd93a46db15b support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   641
			std::string fileName = definition.fileName;
fd93a46db15b support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   642
			if (definition.type == "fragment") type = Shader::Type::FRAGMENT;
fd93a46db15b support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   643
			else if (definition.type == "vertex") type = Shader::Type::VERTEX;
fd93a46db15b support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   644
			else throw std::invalid_argument("unsupported shader type");
fd93a46db15b support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   645
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   646
			MappedFile file(fileName);
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   647
			std::shared_ptr<Shader> shader = std::make_shared<Shader>(
6
fd93a46db15b support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   648
					type, file, fileName);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   649
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   650
			program->attachShader(*shader.get());
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   651
			shaders.push_back(shader);
7
e6065118326f monitor texture and shader file writes using inotify: print file events
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   652
			watchedFiles.push_back(fileMonitor.watch(fileName));
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   653
			std::cerr << "GLSL loaded: " << fileName.c_str() << 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
   654
			// We may detach and delete shaders,
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   655
			// but our shaders are small, so we keep them for later reloading.
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   656
		}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   657
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   658
		// GLSL compiler does very efficient / aggressive optimization.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   659
		// Attributes and uniforms that are not used in the shader are deleted.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   660
		// And even if we e.g. read color from a texture and overwrite it,
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   661
		// the variable is still deleted and considered „inactive“.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   662
		// Functions glGetAttribLocation() and glGetUniformLocation() return -1.
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   663
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   664
		program->link();
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   665
5
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   666
		ProgAttr.aVertexXYZ = program->getAttribLocation("aVertexXYZ");
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   667
		ProgAttr.aTextureXY = program->getAttribLocation("aTextureXY");
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   668
		ProgAttr.uModel = program->getUniformLocation("uModel");
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   669
		ProgAttr.uView = program->getUniformLocation("uView");
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   670
		ProgAttr.uProjection = program->getUniformLocation("uProjection");
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   671
		ProgAttr.uTexture = program->getUniformLocation("uTexture");
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   672
		ProgAttr.fColor = program->getFragDataLocation("fColor");
ee4ba9f5a053 OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   673
		program->bindFragDataLocation("fColor", ProgAttr.fColor);
0
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   674
		// listVariables(program);
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   675
		std::cerr << "GLSL shader count: " << shaders.size() << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   676
		return program;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   677
	} catch (const std::exception& e) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   678
		std::cerr << "Error while loading shaders: " << e.what() << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   679
	} catch (...) {
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   680
		std::cerr << "Error while loading shaders: unknown" << std::endl;
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   681
	}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   682
	throw std::logic_error("GLSL: loadShaders() failed");
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   683
}
bb715a82a8f1 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   684
10
8382173bfc35 private Impl class also for the Shark
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   685
bool Shark::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
   686
	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
   687
		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
   688
			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
   689
			shaderProgram->link();
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   690
			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
   691
		}
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   692
	}
80ad08521091 monitor texture and shader file writes using inotify: reload shaders
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   693
	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
   694
}