set scaling filters according to the texture file extended attributes (xattr: shader-shark.texture.mag-filter = linear | nearest) v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 23 Dec 2023 23:10:41 +0100
branchv_0
changeset 25 717653cedc4a
parent 24 98d033d3ef7c
child 26 e7ceb915177e
set scaling filters according to the texture file extended attributes (xattr: shader-shark.texture.mag-filter = linear | nearest)
Shark.cpp
--- a/Shark.cpp	Sun Dec 10 22:23:32 2023 +0100
+++ b/Shark.cpp	Sat Dec 23 23:10:41 2023 +0100
@@ -182,6 +182,7 @@
 	void log(LogLevel level, std::string message);
 	int setNonBlocking(int fd);
 	void loadVertices();
+	void parametrizeTexture(std::shared_ptr<Texture> tex);
 	bool reloadTexture(const std::string& fileName);
 	void loadTextures();
 	std::shared_ptr<Program> loadShaders();
@@ -488,6 +489,7 @@
 void Shark::Impl::loadVertices() {
 	for (int i = 0; i < textures.size(); i++) {
 		std::shared_ptr<Texture> tex = textures[i];
+		// TODO: draw a rectangle for each texture
 		GLfloat ratio = tex->getRatio();
 		const std::vector<GLfloat> vertices = {
 			// Vertex XYZ                      Texture XY
@@ -499,7 +501,7 @@
 			+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
+			// see glDrawArrays(), where we set start offset and count
 		};
 
 		// Vertex data:
@@ -544,6 +546,24 @@
 	}
 }
 
+void Shark::Impl::parametrizeTexture(std::shared_ptr<Texture> tex) {
+	XAttrs xa(tex->getFileName());
+	std::string magf = xa["shader-shark.texture.mag-filter"];
+	std::string minf = xa["shader-shark.texture.min-filter"];
+	// TODO: std::string scale = xa["shader-shark.texture.scale"];
+	// TODO: keep MappedFile locked until we read the attributes
+
+	auto GLT2D = GL_TEXTURE_2D;
+	auto MAG = GL_TEXTURE_MAG_FILTER;
+	auto MIN = GL_TEXTURE_MIN_FILTER;
+
+	if (magf == "linear") glTexParameteri(GLT2D, MAG, GL_LINEAR);
+	else if (magf == "nearest") glTexParameteri(GLT2D, MAG, GL_NEAREST);
+
+	if (minf == "linear") glTexParameteri(GLT2D, MIN, GL_LINEAR);
+	else if (minf == "nearest") glTexParameteri(GLT2D, MIN, GL_NEAREST);
+}
+
 void Shark::Impl::loadTextures() {
 	// Load default texture if there is no configured:
 	if (cfg.textures.empty())
@@ -554,6 +574,7 @@
 				img(imageLoader.loadImage(MappedFile(tex.fileName)));
 		textures.push_back(std::make_shared<Texture>(
 				img->width, img->height, *img, tex.fileName));
+		parametrizeTexture(textures.back());
 		// static const uint32_t watchMask = IN_CLOSE_WRITE | IN_ATTRIB;
 		// watchedFiles.push_back(fileMonitor.watch(tex.fileName, watchMask));
 		watchedFiles.push_back(fileMonitor.watch(tex.fileName));
@@ -570,6 +591,7 @@
 			std::shared_ptr<ImageLoader::ImageBuffer>
 					img(imageLoader.loadImage(MappedFile(fileName)));
 			tex->update(img->width, img->height, *img);
+			parametrizeTexture(tex);
 			loadVertices();
 			return true;
 		}