Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/src/texture.cpp
00001 /*
00002  * texture.cpp - Kukatz 3D
00003  * Copyright (c) 2011 - TÖRÖK Attila (torokati44@gmail.com)
00004  *
00005  * This software is provided 'as-is', without any express or implied
00006  * warranty. In no event will the authors be held liable for any damages
00007  * arising from the use of this software.
00008  *
00009  * Permission is granted to anyone to use this software for any purpose,
00010  * including commercial applications, and to alter it and redistribute it
00011  * freely, subject to the following restrictions:
00012  *
00013  *       1. The origin of this software must not be misrepresented; you must not
00014  *                      claim that you wrote the original software. If you use this software
00015  *                      in a product, an acknowledgment in the product documentation would be
00016  *                      appreciated but is not required.
00017  *
00018  *       2. Altered source versions must be plainly marked as such, and must not be
00019  *                      misrepresented as being the original software.
00020  *
00021  *       3. This notice may not be removed or altered from any source
00022  *                      distribution.
00023  */
00024 
00025 #include "texture.hpp"
00026 
00027 #include <iostream>
00028 #include <vector>
00029 
00030 #include "opengl.hpp"
00031 
00032 // Kind of ugly and hacky, but I didn't want to change the original file.
00033 // Although that unused known_type variable could have been removed safely.
00034 #pragma GCC diagnostic push
00035 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
00036 #include "picopng.hpp"
00037 #pragma GCC diagnostic pop
00038 
00039 #include <resourcemanager.hpp>
00040 
00041 GLuint Texture::bound_texture = 0;
00042 
00043 Texture::Texture(std::string identifier, const unsigned char* data, sf::Uint32 data_size, bool rgba):
00044         Resource(identifier)
00045 {
00046         std::vector<unsigned char> pixels;
00047         
00048         if (decodePNG(pixels, width, height, data, data_size, rgba))
00049         {
00050                 std::cerr << "ERROR! Something went wrong with the decoding of texture \"" << id << "\".\n";
00051         }
00052         
00053         glGenTextures(1, &gl_id);
00054         glBindTexture(GL_TEXTURE_2D, gl_id);
00055         
00056         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
00057         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00058         
00059         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00060         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00061         
00062         gluBuild2DMipmaps(GL_TEXTURE_2D, (rgba?GL_RGBA8:GL_RGB8), width, height,
00063                 (rgba?GL_RGBA:GL_RGB), GL_UNSIGNED_BYTE, &pixels.front());
00064 }
00065 
00066 void Texture::bind()
00067 {
00068         if (bound_texture != gl_id)
00069         {
00070                 glBindTexture(GL_TEXTURE_2D, gl_id);
00071                 bound_texture = gl_id;
00072         }
00073 }
00074 
00075 void Texture::unbind()
00076 {
00077         glBindTexture(GL_TEXTURE_2D, 0);
00078         bound_texture = 0;
00079 }
00080 
00081 unsigned long Texture::get_width() const
00082 {
00083         return width;
00084 }
00085 
00086 unsigned long Texture::get_height() const
00087 {
00088         return height;
00089 }
00090 
00091 Texture::~Texture()
00092 {
00093         glDeleteTextures(1, &gl_id);
00094 }
 Összes Osztályok