Shark.cpp
branchv_0
changeset 11 0aeedc35ebed
parent 10 8382173bfc35
child 12 076e3b2d97ac
equal deleted inserted replaced
10:8382173bfc35 11:0aeedc35ebed
   196 	bool reloadTexture(const std::string& fileName);
   196 	bool reloadTexture(const std::string& fileName);
   197 	void loadTextures();
   197 	void loadTextures();
   198 	std::shared_ptr<Program> loadShaders();
   198 	std::shared_ptr<Program> loadShaders();
   199 	bool reloadShader(const std::string& fileName);
   199 	bool reloadShader(const std::string& fileName);
   200 	void setTitle(const std::string& suffix = "");
   200 	void setTitle(const std::string& suffix = "");
       
   201 	static const std::string getDefaultFile(const std::string& relativePath);
   201 
   202 
   202 };
   203 };
   203 
   204 
   204 Shark::Shark(const Configuration& configuration) :
   205 Shark::Shark(const Configuration& configuration) :
   205 impl(new Impl(configuration)) {
   206 impl(new Impl(configuration)) {
   534 
   535 
   535 		// see also glBindBuffer(GL_ARRAY_BUFFER, vbo); where we set current VBO
   536 		// see also glBindBuffer(GL_ARRAY_BUFFER, vbo); where we set current VBO
   536 	}
   537 	}
   537 }
   538 }
   538 
   539 
       
   540 const std::string
       
   541 Shark::Shark::Impl::getDefaultFile(const std::string& relativePath) {
       
   542 	const char* envName = "SHADER_SHARK_DATA_DIR";
       
   543 	const char* envValue = ::getenv(envName);
       
   544 	if (envValue) {
       
   545 		return std::string(envValue) + "/" + relativePath;
       
   546 	} else {
       
   547 		throw std::invalid_argument(std::string("Configure $") + envName
       
   548 				+ " in order to use defaults"
       
   549 				" or specify textures and shaders as parameters");
       
   550 	}
       
   551 }
       
   552 
   539 Shark::Impl::Texture Shark::Impl::loadTexture(const std::string& fileName) {
   553 Shark::Impl::Texture Shark::Impl::loadTexture(const std::string& fileName) {
   540 	Texture tex;
   554 	Texture tex;
   541 	tex.fileName = fileName;
   555 	tex.fileName = fileName;
   542 	MappedFile file(tex.fileName);
   556 	MappedFile file(tex.fileName);
   543 
   557 
   563 	checkError(&std::cerr);
   577 	checkError(&std::cerr);
   564 	return tex;
   578 	return tex;
   565 }
   579 }
   566 
   580 
   567 void Shark::Impl::loadTextures() {
   581 void Shark::Impl::loadTextures() {
       
   582 	// Load default texture if there is no configured:
       
   583 	if (cfg.textures.empty())
       
   584 		cfg.textures.push_back({getDefaultFile("textures/default.png")});
       
   585 
   568 	for (const Configuration::Texture& tex : cfg.textures) {
   586 	for (const Configuration::Texture& tex : cfg.textures) {
   569 		textures.push_back(loadTexture(tex.fileName));
   587 		textures.push_back(loadTexture(tex.fileName));
   570 		watchedFiles.push_back(fileMonitor.watch(tex.fileName));
   588 		watchedFiles.push_back(fileMonitor.watch(tex.fileName));
   571 		// TODO: review texture loading and binding
   589 		// TODO: review texture loading and binding
   572 		// works even without this - default texture
   590 		// works even without this - default texture
   599 		// Vertex Buffer Object (VBO):
   617 		// Vertex Buffer Object (VBO):
   600 		GLuint vbo;
   618 		GLuint vbo;
   601 		glGenBuffers(1, &vbo);
   619 		glGenBuffers(1, &vbo);
   602 		glBindBuffer(GL_ARRAY_BUFFER, vbo);
   620 		glBindBuffer(GL_ARRAY_BUFFER, vbo);
   603 
   621 
   604 		if (cfg.shaders.empty()) {
   622 		{
   605 			// TODO: configurable absolute path or embedded defaults
   623 			// Load default shaders if there are no configured:
   606 			cfg.shaders.push_back({"shaders/first.vert", "vertex"});
   624 			int vc = 0;
   607 			cfg.shaders.push_back({"shaders/first.frag", "fragment"});
   625 			int fc = 0;
       
   626 			auto& ss = cfg.shaders;
       
   627 			for (const auto& s : ss) if (s.type == "vertex") vc++;
       
   628 			for (const auto& s : ss) if (s.type == "fragment") fc++;
       
   629 			auto& d = getDefaultFile;
       
   630 			if (vc == 0) ss.push_back({d("shaders/default.vert"), "vertex"});
       
   631 			if (fc == 0) ss.push_back({d("shaders/default.frag"), "fragment"});
   608 		}
   632 		}
   609 
   633 
   610 		std::shared_ptr<Program> program = std::make_shared<Program>();
   634 		std::shared_ptr<Program> program = std::make_shared<Program>();
   611 
   635 
   612 		// glBindFragDataLocation(program, 0, "outColor");
   636 		// glBindFragDataLocation(program, 0, "outColor");