support custom shaders v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 01 Dec 2023 21:02:02 +0100
branchv_0
changeset 6 fd93a46db15b
parent 5 ee4ba9f5a053
child 7 e6065118326f
support custom shaders
CLIParser.h
Configuration.h
Shark.cpp
opengl.h
--- a/CLIParser.h	Fri Dec 01 20:25:49 2023 +0100
+++ b/CLIParser.h	Fri Dec 01 21:02:02 2023 +0100
@@ -74,9 +74,7 @@
 			} else if (option == OPTION_SHADER) {
 				const auto type = readNext(arguments, i);
 				const auto file = readNext(arguments, i);
-				if (type == "fragment") c.fragmentShaders.push_back({file});
-				else if (type == "vertex") c.vertexShaders .push_back({file});
-				else throw std::invalid_argument("unsupported shader type");
+				c.shaders.push_back({file, type});
 			} else if (option == OPTION_BACKGROUND_COLOR) {
 				c.backgroundColor = parseHexColor(readNext(arguments, i));
 			} else if (option == OPTION_ROOT_WINDOW) {
--- a/Configuration.h	Fri Dec 01 20:25:49 2023 +0100
+++ b/Configuration.h	Fri Dec 01 21:02:02 2023 +0100
@@ -33,18 +33,19 @@
 	};
 
 	class Shader : public File {
-	};
+	public:
+
+		std::string type;
 
-	class VertexShader : public Shader {
-	};
+		Shader(const std::string& fileName, const std::string& type) :
+				File(fileName), type(type) {
+		}
 
-	class FragmentShader : public Shader {
 	};
 
 	// TODO: support loading whole directory and monitoring using inotify
 	std::vector<Texture> textures;
-	std::vector<VertexShader> vertexShaders;
-	std::vector<FragmentShader> fragmentShaders;
+	std::vector<Shader> shaders;
 
 	unsigned long backgroundColor = (0x33 << 16 | 0x33 << 8 | 0x33);
 	Window rootWindow = 0;
--- a/Shark.cpp	Fri Dec 01 20:25:49 2023 +0100
+++ b/Shark.cpp	Fri Dec 01 21:02:02 2023 +0100
@@ -376,20 +376,27 @@
 		glGenBuffers(1, &vbo);
 		glBindBuffer(GL_ARRAY_BUFFER, vbo);
 
-		std::vector<std::string> fileNames = {
-			"shaders/first.vert",
-			"shaders/first.frag",
-		};
+		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"});
+		}
 
 		std::shared_ptr<Program> program = std::make_shared<Program>();
 
 		// glBindFragDataLocation(program, 0, "outColor");
 		// glBindAttribLocation(program, LOC.input, "vertices");
 
-		for (const std::string& fileName : fileNames) {
+		for (const Configuration::Shader definition : cfg.shaders) {
+			Shader::Type type;
+			std::string fileName = definition.fileName;
+			if (definition.type == "fragment") type = Shader::Type::FRAGMENT;
+			else if (definition.type == "vertex") type = Shader::Type::VERTEX;
+			else throw std::invalid_argument("unsupported shader type");
+
 			MappedFile file(fileName);
 			std::shared_ptr<Shader> shader = std::make_shared<Shader>(
-					toShaderType(fileName), file, fileName);
+					type, file, fileName);
 
 			program->attachShader(*shader.get());
 			shaders.push_back(shader);
--- a/opengl.h	Fri Dec 01 20:25:49 2023 +0100
+++ b/opengl.h	Fri Dec 01 21:02:02 2023 +0100
@@ -49,13 +49,6 @@
 }
 
 inline
-Shader::Type toShaderType(const std::string& fileName) {
-	if (fileName.ends_with(".vert")) return Shader::Type::VERTEX;
-	else if (fileName.ends_with(".frag")) return Shader::Type::FRAGMENT;
-	else throw std::invalid_argument("Expecting *.vert or *.frag file");
-}
-
-inline
 void dump(const char* name, const glm::vec3& value) {
 	std::cerr << "dump: " << name << " = ["
 			<< value.x << ", "