Shark.cpp
branchv_0
changeset 25 717653cedc4a
parent 24 98d033d3ef7c
child 27 2a156cb51479
equal deleted inserted replaced
24:98d033d3ef7c 25:717653cedc4a
   180 	void runShaders();
   180 	void runShaders();
   181 	Window getRootWindow(Window defaultValue);
   181 	Window getRootWindow(Window defaultValue);
   182 	void log(LogLevel level, std::string message);
   182 	void log(LogLevel level, std::string message);
   183 	int setNonBlocking(int fd);
   183 	int setNonBlocking(int fd);
   184 	void loadVertices();
   184 	void loadVertices();
       
   185 	void parametrizeTexture(std::shared_ptr<Texture> tex);
   185 	bool reloadTexture(const std::string& fileName);
   186 	bool reloadTexture(const std::string& fileName);
   186 	void loadTextures();
   187 	void loadTextures();
   187 	std::shared_ptr<Program> loadShaders();
   188 	std::shared_ptr<Program> loadShaders();
   188 	bool reloadShader(const std::string& fileName);
   189 	bool reloadShader(const std::string& fileName);
   189 	void setTitle(const std::string& suffix = "");
   190 	void setTitle(const std::string& suffix = "");
   486 }
   487 }
   487 
   488 
   488 void Shark::Impl::loadVertices() {
   489 void Shark::Impl::loadVertices() {
   489 	for (int i = 0; i < textures.size(); i++) {
   490 	for (int i = 0; i < textures.size(); i++) {
   490 		std::shared_ptr<Texture> tex = textures[i];
   491 		std::shared_ptr<Texture> tex = textures[i];
       
   492 		// TODO: draw a rectangle for each texture
   491 		GLfloat ratio = tex->getRatio();
   493 		GLfloat ratio = tex->getRatio();
   492 		const std::vector<GLfloat> vertices = {
   494 		const std::vector<GLfloat> vertices = {
   493 			// Vertex XYZ                      Texture XY
   495 			// Vertex XYZ                      Texture XY
   494 			-0.80f * ratio, +0.80f, +0.0, /**/ 0.0, 0.0,
   496 			-0.80f * ratio, +0.80f, +0.0, /**/ 0.0, 0.0,
   495 			+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
   497 			+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
   497 
   499 
   498 			-0.80f * ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
   500 			-0.80f * ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
   499 			+0.80f * ratio, -0.80f, +0.0, /**/ 1.0, 1.0,
   501 			+0.80f * ratio, -0.80f, +0.0, /**/ 1.0, 1.0,
   500 			+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
   502 			+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
   501 
   503 
   502 			// viz glDrawArrays(), kde vybereme počátek a počet hodnot
   504 			// see glDrawArrays(), where we set start offset and count
   503 		};
   505 		};
   504 
   506 
   505 		// Vertex data:
   507 		// Vertex data:
   506 		glVertexAttribPointer(ProgAttr.aVertexXYZ, 3, // vertex items
   508 		glVertexAttribPointer(ProgAttr.aVertexXYZ, 3, // vertex items
   507 				GL_FLOAT, GL_FALSE, 5 * sizeof (float),
   509 				GL_FLOAT, GL_FALSE, 5 * sizeof (float),
   542 				+ " in order to use defaults"
   544 				+ " in order to use defaults"
   543 				" or specify textures and shaders as parameters");
   545 				" or specify textures and shaders as parameters");
   544 	}
   546 	}
   545 }
   547 }
   546 
   548 
       
   549 void Shark::Impl::parametrizeTexture(std::shared_ptr<Texture> tex) {
       
   550 	XAttrs xa(tex->getFileName());
       
   551 	std::string magf = xa["shader-shark.texture.mag-filter"];
       
   552 	std::string minf = xa["shader-shark.texture.min-filter"];
       
   553 	// TODO: std::string scale = xa["shader-shark.texture.scale"];
       
   554 	// TODO: keep MappedFile locked until we read the attributes
       
   555 
       
   556 	auto GLT2D = GL_TEXTURE_2D;
       
   557 	auto MAG = GL_TEXTURE_MAG_FILTER;
       
   558 	auto MIN = GL_TEXTURE_MIN_FILTER;
       
   559 
       
   560 	if (magf == "linear") glTexParameteri(GLT2D, MAG, GL_LINEAR);
       
   561 	else if (magf == "nearest") glTexParameteri(GLT2D, MAG, GL_NEAREST);
       
   562 
       
   563 	if (minf == "linear") glTexParameteri(GLT2D, MIN, GL_LINEAR);
       
   564 	else if (minf == "nearest") glTexParameteri(GLT2D, MIN, GL_NEAREST);
       
   565 }
       
   566 
   547 void Shark::Impl::loadTextures() {
   567 void Shark::Impl::loadTextures() {
   548 	// Load default texture if there is no configured:
   568 	// Load default texture if there is no configured:
   549 	if (cfg.textures.empty())
   569 	if (cfg.textures.empty())
   550 		cfg.textures.push_back({getDefaultFile("textures/default.png")});
   570 		cfg.textures.push_back({getDefaultFile("textures/default.png")});
   551 
   571 
   552 	for (const Configuration::Texture& tex : cfg.textures) {
   572 	for (const Configuration::Texture& tex : cfg.textures) {
   553 		std::shared_ptr<ImageLoader::ImageBuffer>
   573 		std::shared_ptr<ImageLoader::ImageBuffer>
   554 				img(imageLoader.loadImage(MappedFile(tex.fileName)));
   574 				img(imageLoader.loadImage(MappedFile(tex.fileName)));
   555 		textures.push_back(std::make_shared<Texture>(
   575 		textures.push_back(std::make_shared<Texture>(
   556 				img->width, img->height, *img, tex.fileName));
   576 				img->width, img->height, *img, tex.fileName));
       
   577 		parametrizeTexture(textures.back());
   557 		// static const uint32_t watchMask = IN_CLOSE_WRITE | IN_ATTRIB;
   578 		// static const uint32_t watchMask = IN_CLOSE_WRITE | IN_ATTRIB;
   558 		// watchedFiles.push_back(fileMonitor.watch(tex.fileName, watchMask));
   579 		// watchedFiles.push_back(fileMonitor.watch(tex.fileName, watchMask));
   559 		watchedFiles.push_back(fileMonitor.watch(tex.fileName));
   580 		watchedFiles.push_back(fileMonitor.watch(tex.fileName));
   560 		// TODO: review texture loading and binding
   581 		// TODO: review texture loading and binding
   561 		// works even without this - default texture
   582 		// works even without this - default texture
   568 	for (std::shared_ptr<Texture> tex : textures) {
   589 	for (std::shared_ptr<Texture> tex : textures) {
   569 		if (tex->getFileName() == fileName) {
   590 		if (tex->getFileName() == fileName) {
   570 			std::shared_ptr<ImageLoader::ImageBuffer>
   591 			std::shared_ptr<ImageLoader::ImageBuffer>
   571 					img(imageLoader.loadImage(MappedFile(fileName)));
   592 					img(imageLoader.loadImage(MappedFile(fileName)));
   572 			tex->update(img->width, img->height, *img);
   593 			tex->update(img->width, img->height, *img);
       
   594 			parametrizeTexture(tex);
   573 			loadVertices();
   595 			loadVertices();
   574 			return true;
   596 			return true;
   575 		}
   597 		}
   576 	}
   598 	}
   577 	return false;
   599 	return false;