Lagrange
Texture.h
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/fs/filesystem.h>
15#include <lagrange/ui/types/GLContext.h>
16#include <lagrange/ui/utils/math.h>
17#include <lagrange/ui/api.h>
18
19#include <memory>
20#include <optional>
21#include <string>
22#include <unordered_map>
23#include <vector>
24
25
26namespace lagrange {
27namespace ui {
28
29class LA_UI_API Texture
30{
31public:
32 struct Transform
33 {
34 Eigen::Vector2f scale;
35 Eigen::Vector2f offset;
36 float rotation;
38 Eigen::Vector2f scale_ = Eigen::Vector2f::Constant(1.0f),
39 Eigen::Vector2f offset_ = Eigen::Vector2f::Constant(0.0f),
40 float rotation_ = 0.0f);
41 Eigen::Matrix3f get_matrix() const;
42 };
43
44 struct Params
45 {
46 GLenum type;
47 // Set if overriding default 8 bit color
48 GLenum internal_format;
49 GLenum format; // Set if creating empty texture
50 GLenum mag_filter;
51 GLenum min_filter;
52 GLenum wrap_s;
53 GLenum wrap_t;
54 GLenum wrap_r;
55 float border_color[4]; // Only when clamp to border
56 bool generate_mipmap;
57 bool sRGB;
58 float gamma;
59 int multisample_samples; // Used for GL_TEXTURE_2D_MULTISAMPLE
60 Transform uv_transform;
61
62 // Default
63 Params()
64 : type(GL_TEXTURE_2D)
65 , internal_format(GL_NONE)
66 , format(GL_NONE)
67 , mag_filter(GL_LINEAR)
68 , min_filter(GL_LINEAR_MIPMAP_LINEAR)
69 , wrap_s(GL_REPEAT)
70 , wrap_t(GL_REPEAT)
71 , wrap_r(GL_REPEAT)
72 , border_color{0, 0, 0, 0}
73 , generate_mipmap(true)
74 , sRGB(false)
75 , gamma(1.0f)
76 , multisample_samples(4)
77 {}
78
79 // Shorthands
80 static Params multisampled_rgba16f(int num_samples = 4)
81 {
82 Params p;
83 p.type = GL_TEXTURE_2D_MULTISAMPLE;
84 p.internal_format = GL_RGBA16F;
85 p.generate_mipmap = false;
86 p.min_filter = GL_LINEAR;
87 p.multisample_samples = num_samples;
88 return p;
89 }
90
91
92 static Params multisampled_rgba16f_depth(int num_samples = 4)
93 {
94 Params p = multisampled_rgba16f(num_samples);
95 p.internal_format = GL_DEPTH_COMPONENT24;
96 return p;
97 }
98
99 static Params rgb16f()
100 {
102 p.type = GL_TEXTURE_2D;
103 p.format = GL_RGB;
104 p.internal_format = GL_RGB16F;
105 p.wrap_s = GL_CLAMP_TO_EDGE;
106 p.wrap_t = GL_CLAMP_TO_EDGE;
107 p.mag_filter = GL_LINEAR;
108 p.min_filter = GL_LINEAR;
109 p.generate_mipmap = false;
110 return p;
111 }
112
113
114 static Params rgba16f()
115 {
116 Params p = rgb16f();
117 p.format = GL_RGBA;
118 p.internal_format = GL_RGBA16F;
119 return p;
120 }
121
122 static Params red8()
123 {
125 p.type = GL_TEXTURE_2D;
126 p.format = GL_RED;
127 p.internal_format = GL_RED;
128 p.wrap_s = GL_CLAMP_TO_EDGE;
129 p.wrap_t = GL_CLAMP_TO_EDGE;
130 p.mag_filter = GL_LINEAR;
131 p.min_filter = GL_LINEAR;
132 p.generate_mipmap = false;
133 return p;
134 }
135
136 static Params rgb()
137 {
139 p.type = GL_TEXTURE_2D;
140 p.format = GL_RGB;
141 p.internal_format = GL_RGB;
142 p.wrap_s = GL_CLAMP_TO_EDGE;
143 p.wrap_t = GL_CLAMP_TO_EDGE;
144 p.mag_filter = GL_NEAREST;
145 p.min_filter = GL_NEAREST;
146 p.generate_mipmap = false;
147 return p;
148 }
149
150 static Params rgba()
151 {
152 Params p = rgb();
153 p.format = GL_RGBA;
154 p.internal_format = GL_RGBA;
155 return p;
156 }
157
158 static Params depth()
159 {
160 Params p = rgb();
161 p.format = GL_DEPTH_COMPONENT;
162 p.internal_format = GL_DEPTH_COMPONENT24;
163 return p;
164 }
165 };
166
167 Texture(const fs::path& file_path, const Texture::Params& params = Texture::Params()); // throws
168 Texture(
169 const void* image_data,
170 size_t size,
171 const Texture::Params& params = Texture::Params()); // throws
172
173 // Create empty texture
174 Texture(const Params& params, int width = 1, int height = 1, int depth = 0);
175
176 ~Texture();
177
178 void bind() const;
179
180 void bind_to(GLenum texture_unit) const;
181
182 GLuint get_id() const { return m_id; }
183
184 const Params& get_params() const { return m_params; }
185
186 // Resizes texture, destroys previous data
187 void resize(int width, int height = 0, int depth = 0, bool force = false);
188
189 int get_width() const { return m_width; }
190 int get_height() const { return m_height; }
191
192 void upload(float* data);
193 void upload(unsigned char* data);
194
195 void set_uv_transform(const Texture::Transform& uv_transform);
196
198 {
199 std::vector<unsigned char> data;
200 int w, h, components;
201 int row_stride;
202 };
203
204 std::optional<Texture::DownloadResult> download(
205 GLenum target = GL_TEXTURE_2D,
206 int mip_level = 0);
207
208 bool save_to(
209 const fs::path& file_path,
210 GLenum opengl_target = GL_TEXTURE_2D,
211 int quality = 90,
212 int mip_level = 0);
213
214
215 static bool is_internal_format_color_renderable(GLenum internal_format);
216
217 // Get element type used to allocate the texture memory (e.g. GL_UNSIGNED_BYTE, GL_FLOAT, ...)
218 GLenum get_gl_element_type() const { return m_gl_elem_type; }
219
220private:
221 void set_common_params();
222 void free();
223 void load_data_from_image(const fs::path& file_path);
224 void load_data_from_image(const void* image_data, size_t size);
225
226 int m_width;
227 int m_height;
228 int m_depth;
229 GLuint m_id;
230
231 Params m_params;
232 GLenum m_gl_elem_type;
233};
234
235} // namespace ui
236} // namespace lagrange
Definition: Texture.h:30
Lagrange UI Viewer and mini 3D engine.
Definition: AcceleratedPicking.h:22
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: Texture.h:198
Definition: Texture.h:45
Definition: Texture.h:33