use default texture and shaders from the $SHADER_SHARK_DATA_DIR directory, if not configured as command line arguments
--- a/Makefile Sat Dec 02 21:25:02 2023 +0100
+++ b/Makefile Sat Dec 02 23:11:56 2023 +0100
@@ -20,8 +20,8 @@
clean:
rm -rf build
-run: build/shader-shark build/jazz.png
- $(<) --texture build/jazz.png
+run: build/shader-shark
+ SHADER_SHARK_DATA_DIR=. $(<)
build:
mkdir -p $(@)
@@ -37,11 +37,3 @@
build/shader-shark: $(SRC) build *.h
$(CXX) -std=c++20 -g -o $(@) $(SRC) $$(pkg-config --cflags --libs \
epoxy x11 glu glm Magick++)
-
-build/jazz.png: textures/jazz.png
- mkdir -p build
- cp --reflink=auto $(<) $(@)
-
-build/jazz.rgba: textures/jazz.png
- mkdir -p build
- convert -format rgba $(<) $(@)
--- a/Shark.cpp Sat Dec 02 21:25:02 2023 +0100
+++ b/Shark.cpp Sat Dec 02 23:11:56 2023 +0100
@@ -198,6 +198,7 @@
std::shared_ptr<Program> loadShaders();
bool reloadShader(const std::string& fileName);
void setTitle(const std::string& suffix = "");
+ static const std::string getDefaultFile(const std::string& relativePath);
};
@@ -536,6 +537,19 @@
}
}
+const std::string
+Shark::Shark::Impl::getDefaultFile(const std::string& relativePath) {
+ const char* envName = "SHADER_SHARK_DATA_DIR";
+ const char* envValue = ::getenv(envName);
+ if (envValue) {
+ return std::string(envValue) + "/" + relativePath;
+ } else {
+ throw std::invalid_argument(std::string("Configure $") + envName
+ + " in order to use defaults"
+ " or specify textures and shaders as parameters");
+ }
+}
+
Shark::Impl::Texture Shark::Impl::loadTexture(const std::string& fileName) {
Texture tex;
tex.fileName = fileName;
@@ -565,6 +579,10 @@
}
void Shark::Impl::loadTextures() {
+ // Load default texture if there is no configured:
+ if (cfg.textures.empty())
+ cfg.textures.push_back({getDefaultFile("textures/default.png")});
+
for (const Configuration::Texture& tex : cfg.textures) {
textures.push_back(loadTexture(tex.fileName));
watchedFiles.push_back(fileMonitor.watch(tex.fileName));
@@ -601,10 +619,16 @@
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
- if (cfg.shaders.empty()) {
- // TODO: configurable absolute path or embedded defaults
- cfg.shaders.push_back({"shaders/first.vert", "vertex"});
- cfg.shaders.push_back({"shaders/first.frag", "fragment"});
+ {
+ // Load default shaders if there are no configured:
+ int vc = 0;
+ int fc = 0;
+ auto& ss = cfg.shaders;
+ for (const auto& s : ss) if (s.type == "vertex") vc++;
+ for (const auto& s : ss) if (s.type == "fragment") fc++;
+ auto& d = getDefaultFile;
+ if (vc == 0) ss.push_back({d("shaders/default.vert"), "vertex"});
+ if (fc == 0) ss.push_back({d("shaders/default.frag"), "fragment"});
}
std::shared_ptr<Program> program = std::make_shared<Program>();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/shaders/default.frag Sat Dec 02 23:11:56 2023 +0100
@@ -0,0 +1,17 @@
+#version 330 core
+
+uniform sampler2D uTexture;
+in vec2 vTextureXY;
+out vec3 fColor;
+
+vec3 grayscale(vec3 original) {
+ const vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);
+ float luminance = dot(original, luminanceWeighting);
+ return vec3(luminance);
+}
+
+void main(){
+ fColor = texture(uTexture, vTextureXY).rgb;
+ // fColor = grayscale(fColor);
+ // fColor *= vec3(0.8, 1., 0.2);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/shaders/default.vert Sat Dec 02 23:11:56 2023 +0100
@@ -0,0 +1,13 @@
+#version 330 core
+
+uniform mat4 uModel;
+uniform mat4 uView;
+uniform mat4 uProjection;
+in vec3 aVertexXYZ;
+in vec2 aTextureXY;
+out vec2 vTextureXY;
+
+void main() {
+ gl_Position = uProjection * uView * uModel * vec4(aVertexXYZ, 1.0f);
+ vTextureXY = aTextureXY;
+}
--- a/shaders/first.frag Sat Dec 02 21:25:02 2023 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-#version 330 core
-
-uniform sampler2D uTexture;
-in vec2 vTextureXY;
-out vec3 fColor;
-
-vec3 grayscale(vec3 original) {
- const vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);
- float luminance = dot(original, luminanceWeighting);
- return vec3(luminance);
-}
-
-void main(){
- fColor = texture(uTexture, vTextureXY).rgb;
- // fColor = grayscale(fColor);
- // fColor *= vec3(0.8, 1., 0.2);
-}
--- a/shaders/first.vert Sat Dec 02 21:25:02 2023 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,13 +0,0 @@
-#version 330 core
-
-uniform mat4 uModel;
-uniform mat4 uView;
-uniform mat4 uProjection;
-in vec3 aVertexXYZ;
-in vec2 aTextureXY;
-out vec2 vTextureXY;
-
-void main() {
- gl_Position = uProjection * uView * uModel * vec4(aVertexXYZ, 1.0f);
- vTextureXY = aTextureXY;
-}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/textures/default.png Sat Dec 02 23:11:56 2023 +0100
@@ -0,0 +1,1 @@
+jazz.png
\ No newline at end of file