| 0.8.0 API documentation

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List

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