Shark.cpp
branchv_0
changeset 1 fb65455622b9
parent 0 bb715a82a8f1
child 2 3faef2f5128e
--- a/Shark.cpp	Sun Nov 26 16:27:50 2023 +0100
+++ b/Shark.cpp	Tue Nov 28 22:45:33 2023 +0100
@@ -15,6 +15,8 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <memory>
+
 #include "Shark.h"
 
 Shark::Shark(const Configuration& configuration) :
@@ -65,8 +67,8 @@
 
 	// Load GLSL shaders:
 	GLuint shaderProgram = loadShaders();
+	loadTextures(shaderProgram);
 	loadVertices();
-	loadTextures(shaderProgram);
 
 	auto toggleFullscreen = [&]() {
 		full = setFullscreen(dpy, win, !full);
@@ -267,77 +269,82 @@
 }
 
 void Shark::loadVertices() {
-	const std::vector<GLfloat> vertices = {
-		// Vertex XYZ                          Texture XY
-		-0.80f * TEX.ratio, +0.80f, +0.0, /**/ 0.0, 0.0,
-		+0.80f * TEX.ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
-		-0.80f * TEX.ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
+	for (int i = 0; i < textures.size(); i++) {
+		const Texture& tex = textures[i];
+		GLfloat ratio = tex.getRatio();
+		const std::vector<GLfloat> vertices = {
+			// Vertex XYZ                      Texture XY
+			-0.80f * ratio, +0.80f, +0.0, /**/ 0.0, 0.0,
+			+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
+			-0.80f * ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
 
-		-0.80f * TEX.ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
-		+0.80f * TEX.ratio, -0.80f, +0.0, /**/ 1.0, 1.0,
-		+0.80f * TEX.ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
+			-0.80f * ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
+			+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
-	};
+			// viz glDrawArrays(), kde vybereme počátek a počet hodnot
+		};
 
-	// Vertex data:
-	glVertexAttribPointer(ProgAttr.vertexXYZ, 3, // vertex items
-			GL_FLOAT, GL_FALSE, 5 * sizeof (float),
-			(void*) 0);
-	glEnableVertexAttribArray(ProgAttr.vertexXYZ);
+		// Vertex data:
+		glVertexAttribPointer(ProgAttr.vertexXYZ, 3, // vertex items
+				GL_FLOAT, GL_FALSE, 5 * sizeof (float),
+				(void*) 0);
+		glEnableVertexAttribArray(ProgAttr.vertexXYZ);
 
-	// Texture positions:
-	glVertexAttribPointer(ProgAttr.textureXY, 2, // texture items
-			GL_FLOAT, GL_FALSE, 5 * sizeof (float),
-			(void*) (3 * sizeof (float)));
-	glEnableVertexAttribArray(ProgAttr.textureXY);
+		// Texture positions:
+		glVertexAttribPointer(ProgAttr.textureXY, 2, // texture items
+				GL_FLOAT, GL_FALSE, 5 * sizeof (float),
+				(void*) (3 * sizeof (float)));
+		glEnableVertexAttribArray(ProgAttr.textureXY);
 
-	glBufferData(GL_ARRAY_BUFFER,
-			vertices.size() * sizeof (vertices[0]),
-			vertices.data(),
-			GL_STATIC_DRAW);
-	// GL_STATIC_DRAW:
-	//   The vertex data will be uploaded once
-	//   and drawn many times(e.g. the world).
-	// GL_DYNAMIC_DRAW:
-	//   The vertex data will be created once, changed from
-	// 	 time to time, but drawn many times more than that.
-	// GL_STREAM_DRAW:
-	//   The vertex data will be uploaded once and drawn once.
+		glBufferData(GL_ARRAY_BUFFER,
+				vertices.size() * sizeof (vertices[0]),
+				vertices.data(),
+				GL_STATIC_DRAW);
+		// GL_STATIC_DRAW:
+		//   The vertex data will be uploaded once
+		//   and drawn many times(e.g. the world).
+		// GL_DYNAMIC_DRAW:
+		//   The vertex data will be created once, changed from
+		// 	 time to time, but drawn many times more than that.
+		// GL_STREAM_DRAW:
+		//   The vertex data will be uploaded once and drawn once.
 
-	// see also glBindBuffer(GL_ARRAY_BUFFER, vbo); where we set current VBO
+		// see also glBindBuffer(GL_ARRAY_BUFFER, vbo); where we set current VBO
+	}
 }
 
-GLuint Shark::loadTexture(const std::string& fileName, int width, int height) {
-	MappedFile file(fileName);
-	if (file.getSize() == width * height * 4) {
-		GLuint textureID;
-		glGenTextures(1, &textureID);
-		glBindTexture(GL_TEXTURE_2D, textureID);
-		auto GLT2D = GL_TEXTURE_2D;
-		glTexImage2D(GLT2D, 0, GL_RGBA,
-				width, height,
-				0, GL_RGBA, GL_UNSIGNED_BYTE,
-				file.getData());
-		glTexParameteri(GLT2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
-		glTexParameteri(GLT2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
-		glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-		glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
-		glGenerateMipmap(GLT2D);
-		std::cerr << "loadTexture(\"" << fileName.c_str() << "\", "
-				<< width << ", " << height << ") = " << textureID << std::endl;
-		checkError(&std::cerr);
-		return textureID;
-	} else {
-		throw std::invalid_argument("wrong texture file size");
-	}
+Shark::Texture Shark::loadTexture(const std::string& fileName) {
+	Texture tex;
+	tex.fileName = fileName;
+	MappedFile file(tex.fileName);
+
+	std::shared_ptr<ImageLoader::ImageBuffer> img(imageLoader.loadImage(file));
+
+	tex.width = img->width;
+	tex.height = img->height;
+
+	glGenTextures(1, &tex.id);
+	glBindTexture(GL_TEXTURE_2D, tex.id);
+	auto GLT2D = GL_TEXTURE_2D;
+	glTexImage2D(GLT2D, 0, GL_RGBA,
+			tex.width, tex.height,
+			0, GL_RGBA, GL_UNSIGNED_BYTE,
+			img->getData());
+	glTexParameteri(GLT2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+	glTexParameteri(GLT2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+	glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+	glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+	glGenerateMipmap(GLT2D);
+	std::cerr << "loadTexture(\"" << fileName.c_str() << "\", "
+			<< tex.width << ", " << tex.height << ") = " << tex.id << std::endl;
+	checkError(&std::cerr);
+	return tex;
 }
 
 void Shark::loadTextures(GLuint shaderProgram) {
 	for (const Configuration::Texture& tex : cfg.textures) {
-		GLuint jazz = loadTexture(tex.fileName, TEX.width, TEX.height);
-		// FIXME: decode PNG, JPEG etc. formats
-		// FIXME: read width and height from the file
+		textures.push_back(loadTexture(tex.fileName));
 		// TODO: review texture loading and binding
 		// works even without this - default texture
 		// glUniform1i(ProgAttr.jazz, jazz);