/**
* ShaderShark
* Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <memory>
#include "Shark.h"
Shark::Shark(const Configuration& configuration) :
cfg(configuration) {
}
Shark::~Shark() {
}
void Shark::run() {
Display* dpy = XOpenDisplay(NULL);
if (dpy == NULL) throw std::logic_error("Unable to connect to X server");
GLint att[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
XVisualInfo* vi = glXChooseVisual(dpy, 0, att);
Window root = DefaultRootWindow(dpy);
Window parent = cfg.rootWindow ? cfg.rootWindow : root;
XSetWindowAttributes swa;
swa.colormap = XCreateColormap(dpy, parent, vi->visual, AllocNone);
swa.event_mask = ExposureMask | KeyPressMask | PointerMotionMask
| ButtonPressMask
| StructureNotifyMask;
bool full = false;
unsigned int width = 1600;
unsigned int height = 1200;
if (parent != root) {
XWindowAttributes parentAttr;
XGetWindowAttributes(dpy, parent, &parentAttr);
width = parentAttr.width;
height = parentAttr.height;
}
Window win = XCreateWindow(
dpy, parent, 0, 0, width, height, 0,
vi->depth, InputOutput, vi->visual,
CWColormap | CWEventMask, &swa);
XMapWindow(dpy, win);
XStoreName(dpy, win, "ShaderShark");
setX11PID(dpy, win);
// XSetWindowBackground(dpy, win, 0) vs. glClearColor()
GLXContext glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
glXMakeCurrent(dpy, win, glc);
clear();
glXSwapBuffers(dpy, win);
// Load GLSL shaders:
shaderProgram = loadShaders();
loadTextures();
loadVertices();
auto toggleFullscreen = [&]() {
full = setFullscreen(dpy, win, !full);
};
auto resetView = [&]() {
ctx = initialCtx;
ctx.updateCameraFrontAndUp();
};
// root can reize our window
// or we can listen to root resize and then resize our window ourselves
bool listenToRootResizes = true;
if (listenToRootResizes) XSelectInput(dpy, parent, StructureNotifyMask);
bool keepRunningX11 = true;
int x11fd = XConnectionNumber(dpy);
EPoll epoll;
epoll.add(x11fd);
try {
epoll.add(setNonBlocking(STDIN_FILENO));
} catch (const EPoll::Exception& e) {
logOutput << "Will not monitor events on STDIN: " << e.what() << "\n";
}
// rended the 3D scene even before the first event:
runShaders();
glXSwapBuffers(dpy, win);
for (XEvent xev; keepRunningX11;) {
int epollEventCount = epoll.wait();
//std::cout << "trace: epoll.wait() = " << epollEventCount << std::endl;
for (int epollEvent = 0; epollEvent < epollEventCount; epollEvent++) {
if (epoll[epollEvent].data.fd == x11fd) {
if (!XPending(dpy)) {
// otherwise STDIN events are held until the first X11 event
logOutput << "trace: no pending X11 event" << std::endl;
break;
}
XWindowAttributes gwa;
XNextEvent(dpy, &xev);
bool redraw = false;
if (xev.type == Expose) {
std::cout << "XEvent: Expose" << std::endl;
XGetWindowAttributes(dpy, win, &gwa);
glViewport(0, 0, gwa.width, gwa.height);
redraw = true;
} else if (xev.type == KeyPress) {
DecodedKey key = decodeKeycode(dpy, xev.xkey.keycode);
std::cout << "XEvent: KeyPress:"
<< " keycode=" << key.code
<< " key=" << key.name
<< std::endl;
const float cSp = 0.05f; // camera speed
const float aSp = 5.f; // angle speed
if (key.matches(XK_q, XK_Escape)) keepRunningX11 = false;
else if (key.matches(XK_Left, XK_s)) ctx.turnLeft(aSp);
else if (key.matches(XK_Right, XK_f)) ctx.turnRight(aSp);
else if (key.matches(XK_Up, XK_e)) ctx.moveForward(cSp);
else if (key.matches(XK_Down, XK_d)) ctx.moveBackward(cSp);
else if (key.matches(XK_w)) ctx.rollLeft(aSp);
else if (key.matches(XK_r)) ctx.rollRight(aSp);
else if (key.matches(XK_t)) ctx.turnUp(aSp);
else if (key.matches(XK_g)) ctx.turnDown(aSp);
else if (key.matches(XK_m)) ctx.moveLeft(cSp);
else if (key.matches(XK_comma)) ctx.moveRight(cSp);
else if (key.matches(XK_l)) ctx.moveUp(cSp);
else if (key.matches(XK_period)) ctx.moveDown(cSp);
else if (key.matches(XK_j)) ctx.moveLeft(cSp * 5);
else if (key.matches(XK_k)) ctx.moveRight(cSp * 5);
else if (key.matches(XK_u)) ctx.moveLeft(cSp * 10);
else if (key.matches(XK_i)) ctx.moveRight(cSp * 10);
else if (key.matches(XK_x)) resetView();
else if (key.matches(XK_F11, XK_y)) toggleFullscreen();
redraw = true;
} else if (xev.type == ButtonPress) {
std::cout << "XEvent: ButtonPress:"
<< " button=" << xev.xbutton.button
<< std::endl;
if (xev.xbutton.button == 1);
else if (xev.xbutton.button == 4) ctx.adjustFov(+1.0);
else if (xev.xbutton.button == 5) ctx.adjustFov(-1.0);
else if (xev.xbutton.button == 8) resetView();
else if (xev.xbutton.button == 9) keepRunningX11 = false;
redraw = true;
} else if (xev.type == MotionNotify) {
// printCursorInfo(xev.xmotion);
} else if (xev.type == ConfigureNotify) {
std::cout << "XEvent: ConfigureNotify:"
<< " window=" << xev.xconfigure.window
<< " height=" << xev.xconfigure.height
<< " width=" << xev.xconfigure.width
<< std::endl;
if (listenToRootResizes
&& xev.xconfigure.window == parent) {
XResizeWindow(dpy, win,
xev.xconfigure.width, xev.xconfigure.height);
}
} else if (xev.type == UnmapNotify) {
std::cout << "XEvent: UnmapNotify" << std::endl;
} else if (xev.type == DestroyNotify) {
std::cout << "XEvent: DestroyNotify → finish" << std::endl;
break;
} else {
std::cout << "XEvent: type=" << xev.type << std::endl;
}
if (redraw) {
runShaders();
glXSwapBuffers(dpy, win);
}
} else if (epoll[epollEvent].data.fd == STDIN_FILENO) {
int epollFD = epoll[epollEvent].data.fd;
logOutput << "other event: fd=" << epollFD << " data=";
for (char ch; read(epollFD, &ch, 1) > 0;) {
std::stringstream msg;
msg
<< std::hex
<< std::setfill('0')
<< std::setw(2)
<< (int) ch;
logOutput << msg.str();
}
logOutput << std::endl;
} else {
logOutput
<< "error: event on an unexpected FD: "
<< epoll[epollEvent].data.fd
<< std::endl;
}
}
}
XFree(vi);
// for (auto page : pdfTextures) glDeleteTextures(1, &page.texture);
glXMakeCurrent(dpy, None, NULL);
glXDestroyContext(dpy, glc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
}
void Shark::clear() {
glClearColor(
(cfg.backgroundColor >> 16 & 0xFF) / 256.,
(cfg.backgroundColor >> 8 & 0xFF) / 256.,
(cfg.backgroundColor & 0xFF) / 256.,
1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Shark::runShaders() {
glUseProgram(shaderProgram);
checkError(&std::cerr);
clear();
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
GLfloat width = viewport[2];
GLfloat height = viewport[3];
glm::mat4 projection = glm::perspective(
glm::radians(ctx.fov),
width / height,
0.1f, 100.0f);
glUniformMatrix4fv(ProgAttr.uProjection, 1, GL_FALSE, &projection[0][0]);
glm::mat4 view = glm::lookAt(
ctx.cameraPos,
ctx.cameraPos + ctx.cameraFront,
ctx.cameraUp);
glUniformMatrix4fv(ProgAttr.uView, 1, GL_FALSE, &view[0][0]);
// glBindVertexArray(vao);
glm::mat4 model = glm::mat4(1.0f); // identity matrix
// model = glm::translate(model, glm::vec3(0., 0., 0.));
// float angle = 20.0f;
// glm::vec3 xxx = glm::vec3(1.0f, 0.3f, 0.5f);
// model = glm::rotate(model, glm::radians(angle), xxx);
glUniformMatrix4fv(ProgAttr.uModel, 1, GL_FALSE, &model[0][0]);
glDrawArrays(GL_TRIANGLES, 0, 2 * 3); // viz loadVertices() kde plníme data
std::cerr << "GLSL: glDrawArrays()" << std::endl;
}
void Shark::log(LogLevel level, std::string message) {
::log(logOutput, level, message);
}
int Shark::setNonBlocking(int fd) {
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
return fd;
}
void Shark::loadVertices() {
for (int i = 0; i < textures.size(); i++) {
const Texture& tex = textures[i];
GLfloat ratio = tex.getRatio();
const std::vector<GLfloat> vertices = {
// Vertex XYZ Texture XY
-0.80f * ratio, +0.80f, +0.0, /**/ 0.0, 0.0,
+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
-0.80f * ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
-0.80f * ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
+0.80f * ratio, -0.80f, +0.0, /**/ 1.0, 1.0,
+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
// viz glDrawArrays(), kde vybereme počátek a počet hodnot
};
// Vertex data:
glVertexAttribPointer(ProgAttr.aVertexXYZ, 3, // vertex items
GL_FLOAT, GL_FALSE, 5 * sizeof (float),
(void*) 0);
glEnableVertexAttribArray(ProgAttr.aVertexXYZ);
// Texture positions:
glVertexAttribPointer(ProgAttr.aTextureXY, 2, // texture items
GL_FLOAT, GL_FALSE, 5 * sizeof (float),
(void*) (3 * sizeof (float)));
glEnableVertexAttribArray(ProgAttr.aTextureXY);
glBufferData(GL_ARRAY_BUFFER,
vertices.size() * sizeof (vertices[0]),
vertices.data(),
GL_STATIC_DRAW);
// GL_STATIC_DRAW:
// The vertex data will be uploaded once
// and drawn many times(e.g. the world).
// GL_DYNAMIC_DRAW:
// The vertex data will be created once, changed from
// time to time, but drawn many times more than that.
// GL_STREAM_DRAW:
// The vertex data will be uploaded once and drawn once.
// see also glBindBuffer(GL_ARRAY_BUFFER, vbo); where we set current VBO
}
}
Shark::Texture Shark::loadTexture(const std::string& fileName) {
Texture tex;
tex.fileName = fileName;
MappedFile file(tex.fileName);
std::shared_ptr<ImageLoader::ImageBuffer> img(imageLoader.loadImage(file));
tex.width = img->width;
tex.height = img->height;
glGenTextures(1, &tex.id);
glBindTexture(GL_TEXTURE_2D, tex.id);
auto GLT2D = GL_TEXTURE_2D;
glTexImage2D(GLT2D, 0, GL_RGBA,
tex.width, tex.height,
0, GL_RGBA, GL_UNSIGNED_BYTE,
img->getData());
glTexParameteri(GLT2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GLT2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GLT2D);
std::cerr << "loadTexture(\"" << fileName.c_str() << "\", "
<< tex.width << ", " << tex.height << ") = " << tex.id << std::endl;
checkError(&std::cerr);
return tex;
}
void Shark::loadTextures() {
for (const Configuration::Texture& tex : cfg.textures) {
textures.push_back(loadTexture(tex.fileName));
// TODO: review texture loading and binding
// works even without this - default texture
// glUniform1i(ProgAttr.jazz, jazz);
// checkError(&std::cerr);
}
}
GLuint Shark::loadShaders() {
try {
// Vertex Array Object (VAO)
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// VAO - something like context for bound data/variables
// We can switch multiple VAOs. VAO can contain multiple VBOs.
// See also https://stackoverflow.com/questions/11821336/
// what-are-vertex-array-objects
// Vertex Buffer Object (VBO):
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
std::vector<std::string> fileNames = {
"shaders/first.vert",
"shaders/first.frag",
};
std::vector<GLuint> shaders;
GLuint program = glCreateProgram();
// glBindFragDataLocation(program, 0, "outColor");
// glBindAttribLocation(program, LOC.input, "vertices");
for (const std::string& fileName : fileNames) {
MappedFile file(fileName);
GLuint shader = glCreateShader(toShaderType(fileName));
auto fileData = file.getData();
GLint fileSize = file.getSize();
glShaderSource(shader, 1, &fileData, &fileSize);
glCompileShader(shader);
GLint compileStatus;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
std::cerr << "GLSL shader compile status: "
<< compileStatus
<< (compileStatus == GL_TRUE ? " = OK" : " = ERROR")
<< std::endl;
if (compileStatus != GL_TRUE) {
char error[512];
glGetShaderInfoLog(shader, sizeof (error), NULL, error);
std::cerr << "GLSL shader error: " << error;
throw std::logic_error("GLSL: shader failed to compile");
}
glAttachShader(program, shader);
shaders.push_back(shader);
std::cerr << "GLSL loaded: " << fileName.c_str() << std::endl;
}
// GLSL compiler does very efficient / aggressive optimization.
// Attributes and uniforms that are not used in the shader are deleted.
// And even if we e.g. read color from a texture and overwrite it,
// the variable is still deleted and considered „inactive“.
// Functions glGetAttribLocation() and glGetUniformLocation() return -1.
glLinkProgram(program);
ProgAttr.aVertexXYZ = glGetAttribLocation(program, "aVertexXYZ");
ProgAttr.aTextureXY = glGetAttribLocation(program, "aTextureXY");
ProgAttr.uModel = glGetUniformLocation(program, "uModel");
ProgAttr.uView = glGetUniformLocation(program, "uView");
ProgAttr.uProjection = glGetUniformLocation(program, "uProjection");
ProgAttr.uTexture = glGetUniformLocation(program, "uTexture");
ProgAttr.fColor = glGetFragDataLocation(program, "fColor");
glBindFragDataLocation(program, ProgAttr.fColor, "fColor");
// listVariables(program);
GLint linkStatus;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if (linkStatus != GL_TRUE) {
char error[512];
glGetProgramInfoLog(program, sizeof (error), NULL, error);
std::cerr << "GLSL program error: " << error;
throw std::logic_error("GLSL: program failed to link");
}
std::cerr << "GLSL shader count: " << shaders.size() << std::endl;
return program;
} catch (const std::exception& e) {
std::cerr << "Error while loading shaders: " << e.what() << std::endl;
} catch (...) {
std::cerr << "Error while loading shaders: unknown" << std::endl;
}
throw std::logic_error("GLSL: loadShaders() failed");
}