Euphoria
texture.cc
Go to the documentation of this file.
1 
2 #include <iostream>
3 
4 #include "core/image.h"
5 #include "assert/assert.h"
6 #include "log/log.h"
7 #include "io/vfs_path.h"
8 
9 #include "render/gl.h"
10 #include "render/texture.h"
11 
12 #include "undef_windows/undef_windows.h"
13 
14 
15 namespace
16 {
19  {
20  switch(v)
21  {
22  case eu::render::TextureWrap::repeat: return GL_REPEAT;
23  case eu::render::TextureWrap::mirrored_repeat: return GL_MIRRORED_REPEAT;
24  case eu::render::TextureWrap::clamp_to_edge: return GL_CLAMP_TO_EDGE;
25  }
26 
27  DIE("Unhandled texture wrap value");
28  return GL_REPEAT;
29  }
30 
31 
34  {
35  switch(v)
36  {
37  case eu::render::FilterMagnification::nearest: return GL_NEAREST;
38  case eu::render::FilterMagnification::linear: return GL_LINEAR;
39  }
40 
41 
42  DIE("Unhandled filter magnification value");
43  return GL_LINEAR;
44  }
45 
46 
49  {
50  switch(v)
51  {
52  case eu::render::FilterMinification::nearest: return GL_NEAREST;
53  case eu::render::FilterMinification::linear: return GL_LINEAR;
54  }
55 
56  DIE("Unhandled filter minification value");
57  return GL_LINEAR;
58  }
59 
60 
61  const eu::render::TextureId*&
62  get_bound_texture()
63  {
64  static const eu::render::TextureId* bound_texture = nullptr;
65  return bound_texture;
66  }
67 }
68 
69 
70 
71 namespace eu::render
72 {
74  : wrap(TextureWrap::repeat)
75  , minification(FilterMinification::linear)
76  , magnification(FilterMagnification::linear)
77  {
78  }
79 
80 
83  {
84  wrap = v;
85  return *this;
86  }
87 
88 
91  {
92  magnification = v;
93  return *this;
94  }
95 
96 
99  {
100  minification = v;
101  return *this;
102  }
103 
104 
106  {
107  glGenTextures(1, &id);
108  }
109 
110 
112  {
113  glDeleteTextures(1, &id);
114  }
115 
116 
117  GLuint
119  {
120  return id;
121  }
122 
123 
124  bool
126  {
127  return this == get_bound_texture();
128  }
129 
130 
131  void
132  use(const TextureId* texture)
133  {
134  if(texture != nullptr)
135  {
136  glBindTexture(GL_TEXTURE_2D, texture->get_id());
137  }
138  get_bound_texture() = texture;
139  }
140 
141 
143  : width(0)
144  , height(0)
145  {
146  }
147 
148 
149  void
151  (
152  int new_width,
153  int new_height,
154  const unsigned char* pixel_data,
155  GLint internal_format,
156  GLuint image_format,
157  const Texture2dLoadData& data
158  )
159  {
160  use(this);
161 
162  width = new_width;
163  height = new_height;
164 
165  glTexImage2D
166  (
167  GL_TEXTURE_2D,
168  0,
169  internal_format,
170  width,
171  height,
172  0,
173  image_format,
174  GL_UNSIGNED_BYTE,
175  pixel_data
176  );
177 
178  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, con(data.wrap));
179  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, con(data.wrap));
180  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, con(data.minification));
181  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, con(data.magnification));
182 
183  glBindTexture(GL_TEXTURE_2D, 0);
184  }
185 
186 
187  void
189  (
190  io::FileSystem* fs,
191  const io::FilePath& path,
192  core::AlphaLoad alpha,
193  const Texture2dLoadData& data
194  )
195  {
196  const auto loaded = core::load_image(fs, path, alpha);
197  if(!loaded.image.is_valid())
198  {
199  LOG_ERROR("Failed to load image {0}: {1}", path, loaded.error);
200  return;
201  }
202  load_from_image(loaded.image, alpha, data);
203  }
204 
205 
206  void
208  (
209  const core::Image& image,
210  core::AlphaLoad alpha,
211  const Texture2dLoadData& data
212  )
213  {
214  GLint internal_format = GL_RGB;
215  GLuint image_format = GL_RGB;
216  if(image.has_alpha && alpha == core::AlphaLoad::keep)
217  {
218  internal_format = GL_RGBA;
219  image_format = GL_RGBA;
220  }
221 
223  (
224  image.width,
225  image.height,
226  image.get_pixel_data(),
227  internal_format,
228  image_format,
229  data
230  );
231  }
232 }
#define DIE(message)
Definition: assert.h:67
#define LOG_ERROR(...)
Definition: log.h:9
constexpr Rgbi con(unsigned char r, unsigned char g, unsigned char b)
AlphaLoad
Definition: image.h:139
ImageLoadResult load_image(io::FileSystem *fs, const io::FilePath &path, AlphaLoad alpha)
Definition: image.cc:295
void use(const ShaderProgram *shader)
Definition: shader.cc:114
FilterMagnification
Definition: texture.h:23
FilterMinification
Definition: texture.h:29
T wrap(const Range< T > &range, T value)
Definition: range.h:130
void load_from_file(io::FileSystem *fs, const io::FilePath &path, core::AlphaLoad alpha, const Texture2dLoadData &data)
Definition: texture.cc:189
void load_from_image(const core::Image &image, core::AlphaLoad alpha, const Texture2dLoadData &data)
Definition: texture.cc:208
void load_from_pixels(int width, int height, const unsigned char *pixel_data, gl::Int internal_format, gl::Uint image_format, const Texture2dLoadData &data)
Definition: texture.cc:151
Texture2dLoadData & set_wrap(TextureWrap v)
Definition: texture.cc:82
Texture2dLoadData & set_filter_magnification(FilterMagnification v)
Definition: texture.cc:90
FilterMagnification magnification
Definition: texture.h:50
Texture2dLoadData & set_filter_minification(FilterMinification v)
Definition: texture.cc:98
FilterMinification minification
Definition: texture.h:49
gl::Uint get_id() const
Definition: texture.cc:118
bool is_currently_bound() const
Definition: texture.cc:125