Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
OpenGLTexture.cpp
Go to the documentation of this file.
2 #ifdef USING_OPENGL
4 
5 #include <ThirdParty/glew-2.1.0/include/GL/glew.h>
6 
7 namespace Engine
8 {
9  OpenGLTexture::OpenGLTexture(const eastl::string& filename, int desiredChannels) : Texture(filename, desiredChannels)
10  {
11  eastl::string baseLocation = "Resources/Textures/";
12  baseLocation.append(filename);
13  stbi_uc* textureData = stbi_load(
14  Utility::FileExists(baseLocation) ?
15  baseLocation.c_str() :
16  "Resources/Textures/default.png", &width, &height, &channels, STBI_rgb_alpha);
17  OpenGLTexture::CreateTextureWithData(textureData, true);
18  stbi_image_free(textureData);
19  }
20 
21  OpenGLTexture::OpenGLTexture(int width, int height) : Texture(width, height)
22  {
23  }
24 
25  OpenGLTexture::~OpenGLTexture()
26  {
27  GLuint textureReference = GLuint(texture);
28 
29  if (texture)
30  glDeleteTextures(1, &textureReference);
31  }
32 
33  void OpenGLTexture::CreateTextureWithData(stbi_uc* data, bool genMipMaps, TextureDataSize bytes, bool storage)
34  {
35  this->dataSize = bytes;
36  GLuint textureReference = GLuint(texture);
37  if (textureReference)
38  glDeleteTextures(1, &textureReference);
39 
40  glGenTextures(1, &textureReference); // Gen
41  glGetError();
42 
43  glBindTexture(GL_TEXTURE_2D, textureReference); // Bind
44  glGetError();
45 
46  //
47  if (genMipMaps)
48  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Minmization
49  else
50  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Minmization
51  glGetError();
52 
53  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // Magnification
54  glGetError();
55 
56  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
57  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
58  glGetError();
59 
60  GLenum type;
61 
62  switch (bytes) {
64  type = GL_UNSIGNED_BYTE;
65  break;
67  type = GL_UNSIGNED_BYTE;
68  break;
70  type = GL_UNSIGNED_SHORT;
71  break;
73  type = GL_UNSIGNED_SHORT;
74  break;
76  type = GL_UNSIGNED_INT;
77  break;
79  type = GL_UNSIGNED_INT;
80  break;
81  default:
82  type = GL_UNSIGNED_BYTE;
83  break;
84  }
85 
86  glTexImage2D(
87  GL_TEXTURE_2D, // What (target)
88  0, // Mip-map level
89  GL_RGBA, // Internal format
90  width, // Width
91  height, // Height
92  0, // Border
93  GL_RGBA, // Format (how to use)
94  type, // Type (how to intepret)
95  data); // Data
96  glGetError();
97 
98  if (genMipMaps)
99  glGenerateMipmap(GL_TEXTURE_2D);
100 
101  glGetError();
102 
103  texture = uint64_t(textureReference);
104  }
105 } //namespace Engine
106 #endif
TextureDataSize
Definition: Texture.hpp:11
static bool FileExists(const eastl::string &fileName)
Definition: Utility.cpp:32