Advertisement
Guest User

Texture.cpp

a guest
Feb 10th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include "Texture.h"
  2.  
  3. #include <fstream>
  4. #include <algorithm>
  5.  
  6. #define FOURCC_DXT1 0x31545844 // Equivalent to "DXT1" in ASCII
  7. #define FOURCC_DXT3 0x33545844 // Equivalent to "DXT3" in ASCII
  8. #define FOURCC_DXT5 0x35545844 // Equivalent to "DXT5" in ASCII
  9.  
  10. Texture::Texture(char * filePath)
  11. {
  12.     unsigned char header[124];
  13.  
  14.     FILE *fp;
  15.  
  16.     fp = fopen(filePath, "rb");
  17.     if (fp == NULL)
  18.         return;
  19.    
  20.     // check file type
  21.     char filecode[4];
  22.     fread(filecode, 1, 4, fp);
  23.     if (strncmp(filecode, "DDS ", 4) != 0) {
  24.         fclose(fp);
  25.         return;
  26.     }
  27.  
  28.     //read header
  29.     fread(&header, 124, 1, fp);
  30.  
  31.     unsigned int height = *(unsigned int*)&(header[8]);
  32.     unsigned int width = *(unsigned int*)&(header[12]);
  33.     unsigned int linearSize = *(unsigned int*)&(header[16]);
  34.     unsigned int mipMapCount = *(unsigned int*)&(header[24]);
  35.     unsigned int fourCC = *(unsigned int*)&(header[80]);
  36.  
  37.     unsigned char * buffer;
  38.     unsigned int bufsize;
  39.    
  40.     bufsize = mipMapCount > 1 ? linearSize * 2 : linearSize;
  41.     buffer = new unsigned char[bufsize];
  42.     fread(buffer, 1, bufsize, fp);
  43.    
  44.     fclose(fp);
  45.  
  46.     unsigned int components = (fourCC == FOURCC_DXT1) ? 3 : 4;
  47.     unsigned int format;
  48.     switch (fourCC)
  49.     {
  50.     case FOURCC_DXT1:
  51.         format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  52.         break;
  53.     case FOURCC_DXT3:
  54.         format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  55.         break;
  56.     case FOURCC_DXT5:
  57.         format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  58.         break;
  59.     default:
  60.         delete buffer;
  61.         buffer = nullptr;
  62.  
  63.         return;
  64.     }
  65.  
  66.  
  67.     glGenTextures(1, &m_texture);
  68.  
  69.  
  70.     glBindTexture(GL_TEXTURE_2D, m_texture);
  71.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  72.  
  73.  
  74.     unsigned int blockSize = (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16;
  75.     unsigned int offset = 0;
  76.  
  77.  
  78.     for (unsigned int level = 0; level < mipMapCount && (width || height); ++level)
  79.     {
  80.         unsigned int size = ((width + 3) / 4)*((height + 3) / 4)*blockSize;
  81.         glCompressedTexImage2D(GL_TEXTURE_2D, level, format, width, height,
  82.             0, size, buffer + offset);
  83.  
  84.         offset += size;
  85.         width = std::max(width / 2, 1u);
  86.         height = std::max(height / 2, 1u);
  87.     }
  88.  
  89.  
  90.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  91.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  92.    
  93.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  94.    
  95.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  96.    
  97.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, (mipMapCount == 0) ? 0 : mipMapCount - 1);
  98.    
  99.     glGenerateMipmap(GL_TEXTURE_2D);
  100.  
  101.     delete buffer;
  102.     buffer = nullptr;
  103.  
  104.  
  105.     const GLfloat g_uv_buffer_data[] =
  106.     {
  107.         0.0f, 1.0f - 0.0f,
  108.         0.5f, 1.0f - 0.5f,
  109.         1.0f, 1.0f - 0.0f
  110.     };
  111.  
  112.     GLuint uvBuffer;
  113.     glGenBuffers(1, &uvBuffer);
  114.     glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
  115.     glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
  116.  
  117.    
  118.     glEnableVertexAttribArray(1);
  119.     glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
  120.     glVertexAttribPointer(
  121.         1,                                
  122.         2,                                
  123.         GL_FLOAT,                        
  124.         GL_FALSE,                        
  125.         0,                                
  126.         (void*)0                          
  127.     );
  128.  
  129.     glBindTexture(GL_TEXTURE_2D, 0);
  130. }
  131.  
  132.  
  133. Texture::~Texture()
  134. {
  135.     glDeleteTextures(1, &m_texture);
  136. }
  137.  
  138. void Texture::Bind()
  139. {
  140.     glBindTexture(GL_TEXTURE_2D, m_texture);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement