| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #include <GL/glew.h>
- #include "Texture.h"
- #include "Renderer.h"
- #include "Image.h"
- //======================================================================================================================
- // Statics =
- //======================================================================================================================
- int Texture::textureUnitsNum = -1;
- bool Texture::mipmappingEnabled = true;
- bool Texture::compressionEnabled = false;
- int Texture::anisotropyLevel = 8;
- //======================================================================================================================
- // Constructor =
- //======================================================================================================================
- Texture::Texture():
- glId(numeric_limits<uint>::max()),
- target(GL_TEXTURE_2D)
- {
- }
- //======================================================================================================================
- // load =
- //======================================================================================================================
- bool Texture::load(const char* filename)
- {
- target = GL_TEXTURE_2D;
- if(glId != numeric_limits<uint>::max())
- {
- ERROR("Texture already loaded");
- return false;
- }
- Image img;
- if(!img.load(filename)) return false;
- // bind the texture
- glGenTextures(1, &glId);
- bind(0);
- if(mipmappingEnabled)
- {
- setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- }
- else
- {
- setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- }
- setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- texParameter(GL_TEXTURE_MAX_ANISOTROPY_EXT, float(anisotropyLevel));
- // leave to GL_REPEAT. There is not real performance impact
- setRepeat(true);
- int format = (img.bpp==32) ? GL_RGBA : GL_RGB;
- int intFormat; // the internal format of the image
- if(compressionEnabled)
- {
- //int_format = (img.bpp==32) ? GL_COMPRESSED_RGBA_S3TC_DXT1_EXT : GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
- intFormat = (img.bpp==32) ? GL_COMPRESSED_RGBA : GL_COMPRESSED_RGB;
- }
- else
- {
- intFormat = (img.bpp==32) ? GL_RGBA : GL_RGB;
- }
- glTexImage2D(target, 0, intFormat, img.width, img.height, 0, format, GL_UNSIGNED_BYTE, img.data);
- if(mipmappingEnabled)
- {
- glGenerateMipmap(target);
- }
- img.unload();
- return true;
- }
- //======================================================================================================================
- // createEmpty2D =
- //======================================================================================================================
- bool Texture::createEmpty2D(float width_, float height_, int internalFormat, int format_, GLenum type_,
- bool mimapping)
- {
- DEBUG_ERR(glGetError() != GL_NO_ERROR); // dont enter the func with prev error
- target = GL_TEXTURE_2D;
- DEBUG_ERR(internalFormat>0 && internalFormat<=4); // deprecated internal format
- DEBUG_ERR(glId != numeric_limits<uint>::max()); // Texture already loaded
- // GL stuff
- glGenTextures(1, &glId);
- bind();
- if(mimapping)
- setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- else
- setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- setRepeat(true);
- // allocate to vram
- glTexImage2D(target, 0, internalFormat, width_, height_, 0, format_, type_, NULL);
- if(mimapping)
- glGenerateMipmap(target);
- GLenum errid = glGetError();
- if(errid != GL_NO_ERROR)
- {
- ERROR("OpenGL Error: " << gluErrorString(errid));
- return false;
- }
- return true;
- }
- //======================================================================================================================
- // createEmpty2DMSAA =
- //======================================================================================================================
- bool Texture::createEmpty2DMSAA(float width, float height, int samples_num, int internal_format)
- {
- /*type = GL_TEXTURE_2D_MULTISAMPLE;
- DEBUG_ERR(internal_format>0 && internal_format<=4); // deprecated internal format
- DEBUG_ERR(glId != numeric_limits<uint>::max()) // Texture already loaded
- glGenTextures(1, &glId);
- bind();
-
- // allocate
- glTexImage2DMultisample(type, samples_num, internal_format, width, height, false);*/
- return true;
- }
- //======================================================================================================================
- // unload =
- //======================================================================================================================
- void Texture::unload()
- {
- glDeleteTextures(1, &glId);
- }
- //======================================================================================================================
- // bind =
- //======================================================================================================================
- void Texture::bind(uint unit) const
- {
- if(unit >= static_cast<uint>(textureUnitsNum))
- WARNING("Max tex units passed");
- glActiveTexture(GL_TEXTURE0+unit);
- glBindTexture(target, glId);
- }
- //======================================================================================================================
- // getWidth =
- //======================================================================================================================
- int Texture::getWidth() const
- {
- bind();
- int i;
- glGetTexLevelParameteriv(target, 0, GL_TEXTURE_WIDTH, &i);
- return i;
- }
- //======================================================================================================================
- // getHeight =
- //======================================================================================================================
- int Texture::getHeight() const
- {
- bind();
- int i;
- glGetTexLevelParameteriv(target, 0, GL_TEXTURE_HEIGHT, &i);
- return i;
- }
- //======================================================================================================================
- // setTexParameter [int] =
- //======================================================================================================================
- void Texture::setTexParameter(GLenum paramName, GLint value) const
- {
- bind();
- glTexParameteri(target, paramName, value);
- }
- //======================================================================================================================
- // setTexParameter [float] =
- //======================================================================================================================
- void Texture::texParameter(GLenum paramName, GLfloat value) const
- {
- bind();
- glTexParameterf(target, paramName, value);
- }
|