Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Texture.h"
- #include <fstream>
- #include <algorithm>
- #define FOURCC_DXT1 0x31545844 // Equivalent to "DXT1" in ASCII
- #define FOURCC_DXT3 0x33545844 // Equivalent to "DXT3" in ASCII
- #define FOURCC_DXT5 0x35545844 // Equivalent to "DXT5" in ASCII
- Texture::Texture(char * filePath)
- {
- unsigned char header[124];
- FILE *fp;
- fp = fopen(filePath, "rb");
- if (fp == NULL)
- return;
- // check file type
- char filecode[4];
- fread(filecode, 1, 4, fp);
- if (strncmp(filecode, "DDS ", 4) != 0) {
- fclose(fp);
- return;
- }
- //read header
- fread(&header, 124, 1, fp);
- unsigned int height = *(unsigned int*)&(header[8]);
- unsigned int width = *(unsigned int*)&(header[12]);
- unsigned int linearSize = *(unsigned int*)&(header[16]);
- unsigned int mipMapCount = *(unsigned int*)&(header[24]);
- unsigned int fourCC = *(unsigned int*)&(header[80]);
- unsigned char * buffer;
- unsigned int bufsize;
- bufsize = mipMapCount > 1 ? linearSize * 2 : linearSize;
- buffer = new unsigned char[bufsize];
- fread(buffer, 1, bufsize, fp);
- fclose(fp);
- unsigned int components = (fourCC == FOURCC_DXT1) ? 3 : 4;
- unsigned int format;
- switch (fourCC)
- {
- case FOURCC_DXT1:
- format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
- break;
- case FOURCC_DXT3:
- format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
- break;
- case FOURCC_DXT5:
- format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
- break;
- default:
- delete buffer;
- buffer = nullptr;
- return;
- }
- glGenTextures(1, &m_texture);
- glBindTexture(GL_TEXTURE_2D, m_texture);
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- unsigned int blockSize = (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16;
- unsigned int offset = 0;
- for (unsigned int level = 0; level < mipMapCount && (width || height); ++level)
- {
- unsigned int size = ((width + 3) / 4)*((height + 3) / 4)*blockSize;
- glCompressedTexImage2D(GL_TEXTURE_2D, level, format, width, height,
- 0, size, buffer + offset);
- offset += size;
- width = std::max(width / 2, 1u);
- height = std::max(height / 2, 1u);
- }
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, (mipMapCount == 0) ? 0 : mipMapCount - 1);
- glGenerateMipmap(GL_TEXTURE_2D);
- delete buffer;
- buffer = nullptr;
- const GLfloat g_uv_buffer_data[] =
- {
- 0.0f, 1.0f - 0.0f,
- 0.5f, 1.0f - 0.5f,
- 1.0f, 1.0f - 0.0f
- };
- GLuint uvBuffer;
- glGenBuffers(1, &uvBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
- glEnableVertexAttribArray(1);
- glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
- glVertexAttribPointer(
- 1,
- 2,
- GL_FLOAT,
- GL_FALSE,
- 0,
- (void*)0
- );
- glBindTexture(GL_TEXTURE_2D, 0);
- }
- Texture::~Texture()
- {
- glDeleteTextures(1, &m_texture);
- }
- void Texture::Bind()
- {
- glBindTexture(GL_TEXTURE_2D, m_texture);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement