0.8.0 API documentation
Code sample: Creating an OpenGL texture object from file
1 {c++}
2 #include <gli/gli.hpp>
3 
4 /// Filename can be KTX or DDS files
5 GLuint create_texture(char const* Filename)
6 {
7  gli::texture Texture = gli::load(Filename);
8  if(Texture.empty())
9  return 0;
10 
11  gli::gl GL(gli::gl::PROFILE_GL33);
12  gli::gl::format const Format = GL.translate(Texture.format(), Texture.swizzles());
13  GLenum Target = GL.translate(Texture.target());
14 
15  GLuint TextureName = 0;
16  glGenTextures(1, &TextureName);
17  glBindTexture(Target, TextureName);
18  glTexParameteri(Target, GL_TEXTURE_BASE_LEVEL, 0);
19  glTexParameteri(Target, GL_TEXTURE_MAX_LEVEL, static_cast<GLint>(Texture.levels() - 1));
20  glTexParameteri(Target, GL_TEXTURE_SWIZZLE_R, Format.Swizzles[0]);
21  glTexParameteri(Target, GL_TEXTURE_SWIZZLE_G, Format.Swizzles[1]);
22  glTexParameteri(Target, GL_TEXTURE_SWIZZLE_B, Format.Swizzles[2]);
23  glTexParameteri(Target, GL_TEXTURE_SWIZZLE_A, Format.Swizzles[3]);
24 
25  glm::tvec3<GLsizei> const Extent(Texture.extent());
26  GLsizei const FaceTotal = static_cast<GLsizei>(Texture.layers() * Texture.faces());
27 
28  switch(Texture.target())
29  {
30  case gli::TARGET_1D:
31  glTexStorage1D(
32  Target, static_cast<GLint>(Texture.levels()), Format.Internal, Extent.x);
33  break;
34  case gli::TARGET_1D_ARRAY:
35  case gli::TARGET_2D:
36  case gli::TARGET_CUBE:
37  glTexStorage2D(
38  Target, static_cast<GLint>(Texture.levels()), Format.Internal,
39  Extent.x, Texture.target() == gli::TARGET_2D ? Extent.y : FaceTotal);
40  break;
41  case gli::TARGET_2D_ARRAY:
42  case gli::TARGET_3D:
43  case gli::TARGET_CUBE_ARRAY:
44  glTexStorage3D(
45  Target, static_cast<GLint>(Texture.levels()), Format.Internal,
46  Extent.x, Extent.y,
47  Texture.target() == gli::TARGET_3D ? Extent.z : FaceTotal);
48  break;
49  default:
50  assert(0);
51  break;
52  }
53 
54  for(std::size_t Layer = 0; Layer < Texture.layers(); ++Layer)
55  for(std::size_t Face = 0; Face < Texture.faces(); ++Face)
56  for(std::size_t Level = 0; Level < Texture.levels(); ++Level)
57  {
58  GLsizei const LayerGL = static_cast<GLsizei>(Layer);
59  glm::tvec3<GLsizei> Extent(Texture.extent(Level));
60  Target = gli::is_target_cube(Texture.target())
61  ? static_cast<GLenum>(GL_TEXTURE_CUBE_MAP_POSITIVE_X + Face)
62  : Target;
63 
64  switch(Texture.target())
65  {
66  case gli::TARGET_1D:
67  if(gli::is_compressed(Texture.format()))
68  glCompressedTexSubImage1D(
69  Target, static_cast<GLint>(Level), 0, Extent.x,
70  Format.Internal, static_cast<GLsizei>(Texture.size(Level)),
71  Texture.data(Layer, Face, Level));
72  else
73  glTexSubImage1D(
74  Target, static_cast<GLint>(Level), 0, Extent.x,
75  Format.External, Format.Type,
76  Texture.data(Layer, Face, Level));
77  break;
78  case gli::TARGET_1D_ARRAY:
79  case gli::TARGET_2D:
80  case gli::TARGET_CUBE:
81  if(gli::is_compressed(Texture.format()))
82  glCompressedTexSubImage2D(
83  Target, static_cast<GLint>(Level),
84  0, 0,
85  Extent.x,
86  Texture.target() == gli::TARGET_1D_ARRAY ? LayerGL : Extent.y,
87  Format.Internal, static_cast<GLsizei>(Texture.size(Level)),
88  Texture.data(Layer, Face, Level));
89  else
90  glTexSubImage2D(
91  Target, static_cast<GLint>(Level),
92  0, 0,
93  Extent.x,
94  Texture.target() == gli::TARGET_1D_ARRAY ? LayerGL : Extent.y,
95  Format.External, Format.Type,
96  Texture.data(Layer, Face, Level));
97  break;
98  case gli::TARGET_2D_ARRAY:
99  case gli::TARGET_3D:
100  case gli::TARGET_CUBE_ARRAY:
101  if(gli::is_compressed(Texture.format()))
102  glCompressedTexSubImage3D(
103  Target, static_cast<GLint>(Level),
104  0, 0, 0,
105  Extent.x, Extent.y,
106  Texture.target() == gli::TARGET_3D ? Extent.z : LayerGL,
107  Format.Internal, static_cast<GLsizei>(Texture.size(Level)),
108  Texture.data(Layer, Face, Level));
109  else
110  glTexSubImage3D(
111  Target, static_cast<GLint>(Level),
112  0, 0, 0,
113  Extent.x, Extent.y,
114  Texture.target() == gli::TARGET_3D ? Extent.z : LayerGL,
115  Format.External, Format.Type,
116  Texture.data(Layer, Face, Level));
117  break;
118  default: assert(0); break;
119  }
120  }
121  return TextureName;
122 }