--- a/Shark.cpp Wed Nov 29 01:27:05 2023 +0100
+++ b/Shark.cpp Fri Dec 01 20:25:49 2023 +0100
@@ -24,15 +24,25 @@
}
Shark::~Shark() {
+ // TODO: more SBRM
+ shaders.clear();
+ shaderProgram = nullptr;
+ XFree(vi);
+ // for (auto page : pdfTextures) glDeleteTextures(1, &page.texture);
+ glXMakeCurrent(dpy, None, NULL);
+ glXDestroyContext(dpy, glc);
+ XDestroyWindow(dpy, win);
+ XCloseDisplay(dpy);
+ // std::cerr << "~Shark()" << std::endl;
}
void Shark::run() {
- Display* dpy = XOpenDisplay(NULL);
+ 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);
+ vi = glXChooseVisual(dpy, 0, att);
Window root = DefaultRootWindow(dpy);
Window parent = cfg.rootWindow ? cfg.rootWindow : root;
@@ -52,7 +62,7 @@
height = parentAttr.height;
}
- Window win = XCreateWindow(
+ win = XCreateWindow(
dpy, parent, 0, 0, width, height, 0,
vi->depth, InputOutput, vi->visual,
CWColormap | CWEventMask, &swa);
@@ -62,7 +72,7 @@
setX11PID(dpy, win);
// XSetWindowBackground(dpy, win, 0) vs. glClearColor()
- GLXContext glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
+ glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
glXMakeCurrent(dpy, win, glc);
clear();
@@ -209,14 +219,6 @@
}
}
}
-
- 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() {
@@ -229,7 +231,7 @@
}
void Shark::runShaders() {
- glUseProgram(shaderProgram);
+ shaderProgram->use();
checkError(&std::cerr);
clear();
@@ -358,7 +360,7 @@
}
}
-GLuint Shark::loadShaders() {
+std::shared_ptr<Program> Shark::loadShaders() {
try {
// Vertex Array Object (VAO)
GLuint vao;
@@ -379,36 +381,17 @@
"shaders/first.frag",
};
- std::vector<GLuint> shaders;
-
- GLuint program = glCreateProgram();
+ std::shared_ptr<Program> program = std::make_shared<Program>();
// 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);
+ std::shared_ptr<Shader> shader = std::make_shared<Shader>(
+ toShaderType(fileName), file, fileName);
- 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);
+ program->attachShader(*shader.get());
shaders.push_back(shader);
std::cerr << "GLSL loaded: " << fileName.c_str() << std::endl;
}
@@ -419,27 +402,18 @@
// the variable is still deleted and considered „inactive“.
// Functions glGetAttribLocation() and glGetUniformLocation() return -1.
- glLinkProgram(program);
+ program->link();
- 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");
+ ProgAttr.aVertexXYZ = program->getAttribLocation("aVertexXYZ");
+ ProgAttr.aTextureXY = program->getAttribLocation("aTextureXY");
+ ProgAttr.uModel = program->getUniformLocation("uModel");
+ ProgAttr.uView = program->getUniformLocation("uView");
+ ProgAttr.uProjection = program->getUniformLocation("uProjection");
+ ProgAttr.uTexture = program->getUniformLocation("uTexture");
+ ProgAttr.fColor = program->getFragDataLocation("fColor");
+ program->bindFragDataLocation("fColor", ProgAttr.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;