Shark.cpp
branchv_0
changeset 1 fb65455622b9
parent 0 bb715a82a8f1
child 2 3faef2f5128e
equal deleted inserted replaced
0:bb715a82a8f1 1:fb65455622b9
    13  *
    13  *
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    16  */
    17 
    17 
       
    18 #include <memory>
       
    19 
    18 #include "Shark.h"
    20 #include "Shark.h"
    19 
    21 
    20 Shark::Shark(const Configuration& configuration) :
    22 Shark::Shark(const Configuration& configuration) :
    21 cfg(configuration) {
    23 cfg(configuration) {
    22 }
    24 }
    63 	GLXContext glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
    65 	GLXContext glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
    64 	glXMakeCurrent(dpy, win, glc);
    66 	glXMakeCurrent(dpy, win, glc);
    65 
    67 
    66 	// Load GLSL shaders:
    68 	// Load GLSL shaders:
    67 	GLuint shaderProgram = loadShaders();
    69 	GLuint shaderProgram = loadShaders();
       
    70 	loadTextures(shaderProgram);
    68 	loadVertices();
    71 	loadVertices();
    69 	loadTextures(shaderProgram);
       
    70 
    72 
    71 	auto toggleFullscreen = [&]() {
    73 	auto toggleFullscreen = [&]() {
    72 		full = setFullscreen(dpy, win, !full);
    74 		full = setFullscreen(dpy, win, !full);
    73 	};
    75 	};
    74 
    76 
   265 	fcntl(fd, F_SETFL, flags | O_NONBLOCK);
   267 	fcntl(fd, F_SETFL, flags | O_NONBLOCK);
   266 	return fd;
   268 	return fd;
   267 }
   269 }
   268 
   270 
   269 void Shark::loadVertices() {
   271 void Shark::loadVertices() {
   270 	const std::vector<GLfloat> vertices = {
   272 	for (int i = 0; i < textures.size(); i++) {
   271 		// Vertex XYZ                          Texture XY
   273 		const Texture& tex = textures[i];
   272 		-0.80f * TEX.ratio, +0.80f, +0.0, /**/ 0.0, 0.0,
   274 		GLfloat ratio = tex.getRatio();
   273 		+0.80f * TEX.ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
   275 		const std::vector<GLfloat> vertices = {
   274 		-0.80f * TEX.ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
   276 			// Vertex XYZ                      Texture XY
   275 
   277 			-0.80f * ratio, +0.80f, +0.0, /**/ 0.0, 0.0,
   276 		-0.80f * TEX.ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
   278 			+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
   277 		+0.80f * TEX.ratio, -0.80f, +0.0, /**/ 1.0, 1.0,
   279 			-0.80f * ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
   278 		+0.80f * TEX.ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
   280 
   279 
   281 			-0.80f * ratio, -0.80f, +0.0, /**/ 0.0, 1.0,
   280 		// viz glDrawArrays(), kde vybereme počátek a počet hodnot
   282 			+0.80f * ratio, -0.80f, +0.0, /**/ 1.0, 1.0,
   281 	};
   283 			+0.80f * ratio, +0.80f, +0.0, /**/ 1.0, 0.0,
   282 
   284 
   283 	// Vertex data:
   285 			// viz glDrawArrays(), kde vybereme počátek a počet hodnot
   284 	glVertexAttribPointer(ProgAttr.vertexXYZ, 3, // vertex items
   286 		};
   285 			GL_FLOAT, GL_FALSE, 5 * sizeof (float),
   287 
   286 			(void*) 0);
   288 		// Vertex data:
   287 	glEnableVertexAttribArray(ProgAttr.vertexXYZ);
   289 		glVertexAttribPointer(ProgAttr.vertexXYZ, 3, // vertex items
   288 
   290 				GL_FLOAT, GL_FALSE, 5 * sizeof (float),
   289 	// Texture positions:
   291 				(void*) 0);
   290 	glVertexAttribPointer(ProgAttr.textureXY, 2, // texture items
   292 		glEnableVertexAttribArray(ProgAttr.vertexXYZ);
   291 			GL_FLOAT, GL_FALSE, 5 * sizeof (float),
   293 
   292 			(void*) (3 * sizeof (float)));
   294 		// Texture positions:
   293 	glEnableVertexAttribArray(ProgAttr.textureXY);
   295 		glVertexAttribPointer(ProgAttr.textureXY, 2, // texture items
   294 
   296 				GL_FLOAT, GL_FALSE, 5 * sizeof (float),
   295 	glBufferData(GL_ARRAY_BUFFER,
   297 				(void*) (3 * sizeof (float)));
   296 			vertices.size() * sizeof (vertices[0]),
   298 		glEnableVertexAttribArray(ProgAttr.textureXY);
   297 			vertices.data(),
   299 
   298 			GL_STATIC_DRAW);
   300 		glBufferData(GL_ARRAY_BUFFER,
   299 	// GL_STATIC_DRAW:
   301 				vertices.size() * sizeof (vertices[0]),
   300 	//   The vertex data will be uploaded once
   302 				vertices.data(),
   301 	//   and drawn many times(e.g. the world).
   303 				GL_STATIC_DRAW);
   302 	// GL_DYNAMIC_DRAW:
   304 		// GL_STATIC_DRAW:
   303 	//   The vertex data will be created once, changed from
   305 		//   The vertex data will be uploaded once
   304 	// 	 time to time, but drawn many times more than that.
   306 		//   and drawn many times(e.g. the world).
   305 	// GL_STREAM_DRAW:
   307 		// GL_DYNAMIC_DRAW:
   306 	//   The vertex data will be uploaded once and drawn once.
   308 		//   The vertex data will be created once, changed from
   307 
   309 		// 	 time to time, but drawn many times more than that.
   308 	// see also glBindBuffer(GL_ARRAY_BUFFER, vbo); where we set current VBO
   310 		// GL_STREAM_DRAW:
   309 }
   311 		//   The vertex data will be uploaded once and drawn once.
   310 
   312 
   311 GLuint Shark::loadTexture(const std::string& fileName, int width, int height) {
   313 		// see also glBindBuffer(GL_ARRAY_BUFFER, vbo); where we set current VBO
   312 	MappedFile file(fileName);
   314 	}
   313 	if (file.getSize() == width * height * 4) {
   315 }
   314 		GLuint textureID;
   316 
   315 		glGenTextures(1, &textureID);
   317 Shark::Texture Shark::loadTexture(const std::string& fileName) {
   316 		glBindTexture(GL_TEXTURE_2D, textureID);
   318 	Texture tex;
   317 		auto GLT2D = GL_TEXTURE_2D;
   319 	tex.fileName = fileName;
   318 		glTexImage2D(GLT2D, 0, GL_RGBA,
   320 	MappedFile file(tex.fileName);
   319 				width, height,
   321 
   320 				0, GL_RGBA, GL_UNSIGNED_BYTE,
   322 	std::shared_ptr<ImageLoader::ImageBuffer> img(imageLoader.loadImage(file));
   321 				file.getData());
   323 
   322 		glTexParameteri(GLT2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   324 	tex.width = img->width;
   323 		glTexParameteri(GLT2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   325 	tex.height = img->height;
   324 		glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   326 
   325 		glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
   327 	glGenTextures(1, &tex.id);
   326 		glGenerateMipmap(GLT2D);
   328 	glBindTexture(GL_TEXTURE_2D, tex.id);
   327 		std::cerr << "loadTexture(\"" << fileName.c_str() << "\", "
   329 	auto GLT2D = GL_TEXTURE_2D;
   328 				<< width << ", " << height << ") = " << textureID << std::endl;
   330 	glTexImage2D(GLT2D, 0, GL_RGBA,
   329 		checkError(&std::cerr);
   331 			tex.width, tex.height,
   330 		return textureID;
   332 			0, GL_RGBA, GL_UNSIGNED_BYTE,
   331 	} else {
   333 			img->getData());
   332 		throw std::invalid_argument("wrong texture file size");
   334 	glTexParameteri(GLT2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   333 	}
   335 	glTexParameteri(GLT2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
       
   336 	glTexParameteri(GLT2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       
   337 	glTexParameteri(GLT2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
       
   338 	glGenerateMipmap(GLT2D);
       
   339 	std::cerr << "loadTexture(\"" << fileName.c_str() << "\", "
       
   340 			<< tex.width << ", " << tex.height << ") = " << tex.id << std::endl;
       
   341 	checkError(&std::cerr);
       
   342 	return tex;
   334 }
   343 }
   335 
   344 
   336 void Shark::loadTextures(GLuint shaderProgram) {
   345 void Shark::loadTextures(GLuint shaderProgram) {
   337 	for (const Configuration::Texture& tex : cfg.textures) {
   346 	for (const Configuration::Texture& tex : cfg.textures) {
   338 		GLuint jazz = loadTexture(tex.fileName, TEX.width, TEX.height);
   347 		textures.push_back(loadTexture(tex.fileName));
   339 		// FIXME: decode PNG, JPEG etc. formats
       
   340 		// FIXME: read width and height from the file
       
   341 		// TODO: review texture loading and binding
   348 		// TODO: review texture loading and binding
   342 		// works even without this - default texture
   349 		// works even without this - default texture
   343 		// glUniform1i(ProgAttr.jazz, jazz);
   350 		// glUniform1i(ProgAttr.jazz, jazz);
   344 		// checkError(&std::cerr);
   351 		// checkError(&std::cerr);
   345 	}
   352 	}