openMSX
GLImage.cc
Go to the documentation of this file.
1 #include "GLImage.hh"
2 #include "GLContext.hh"
3 #include "SDLSurfacePtr.hh"
4 #include "MSXException.hh"
5 #include "Math.hh"
6 #include "PNG.hh"
7 #include "build-info.hh"
8 #include <cstdlib>
9 #include <SDL.h>
10 
11 using std::string;
12 using namespace gl;
13 
14 namespace openmsx {
15 
16 static gl::Texture loadTexture(
17  SDLSurfacePtr surface, ivec2& size)
18 {
19  size = ivec2(surface->w, surface->h);
20  // Make a copy to convert to the correct pixel format.
21  // TODO instead directly load the image in the correct format.
22  SDLSurfacePtr image2(size[0], size[1], 32,
23  OPENMSX_BIGENDIAN ? 0xFF000000 : 0x000000FF,
24  OPENMSX_BIGENDIAN ? 0x00FF0000 : 0x0000FF00,
25  OPENMSX_BIGENDIAN ? 0x0000FF00 : 0x00FF0000,
26  OPENMSX_BIGENDIAN ? 0x000000FF : 0xFF000000);
27 
28  SDL_Rect area;
29  area.x = 0;
30  area.y = 0;
31  area.w = size[0];
32  area.h = size[1];
33  SDL_SetSurfaceBlendMode(surface.get(), SDL_BLENDMODE_NONE);
34  SDL_BlitSurface(surface.get(), &area, image2.get(), &area);
35 
36  gl::Texture texture(true); // enable interpolation
37  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, size[0], size[1], 0,
38  GL_RGBA, GL_UNSIGNED_BYTE, image2->pixels);
39  return texture;
40 }
41 
42 static gl::Texture loadTexture(
43  const string& filename, ivec2& size)
44 {
45  SDLSurfacePtr surface(PNG::load(filename, false));
46  try {
47  return loadTexture(std::move(surface), size);
48  } catch (MSXException& e) {
49  throw MSXException("Error loading image ", filename, ": ",
50  e.getMessage());
51  }
52 }
53 
54 
55 GLImage::GLImage(OutputSurface& /*output*/, const string& filename)
56  : texture(loadTexture(filename, size))
57 {
58 }
59 
60 GLImage::GLImage(OutputSurface& /*output*/, const string& filename, float scalefactor)
61  : texture(loadTexture(filename, size))
62 {
63  size = trunc(vec2(size) * scalefactor);
64  checkSize(size);
65 }
66 
67 GLImage::GLImage(OutputSurface& /*output*/, const string& filename, ivec2 size_)
68  : texture(loadTexture(filename, size))
69 {
70  checkSize(size_);
71  size = size_;
72 }
73 
74 GLImage::GLImage(OutputSurface& /*output*/, ivec2 size_, unsigned rgba)
75  : texture(gl::Null())
76 {
77  checkSize(size_);
78  size = size_;
79  borderSize = 0;
80  borderR = borderG = borderB = borderA = 0; // not used, but avoid (harmless) UMR
81  for (int i = 0; i < 4; ++i) {
82  bgR[i] = (rgba >> 24) & 0xff;
83  bgG[i] = (rgba >> 16) & 0xff;
84  bgB[i] = (rgba >> 8) & 0xff;
85  unsigned alpha = (rgba >> 0) & 0xff;
86  bgA[i] = (alpha == 255) ? 256 : alpha;
87  }
88 }
89 
90 GLImage::GLImage(OutputSurface& /*output*/, ivec2 size_, const unsigned* rgba,
91  int borderSize_, unsigned borderRGBA)
92  : texture(gl::Null())
93 {
94  checkSize(size_);
95  size = size_;
96  borderSize = borderSize_;
97  for (int i = 0; i < 4; ++i) {
98  bgR[i] = (rgba[i] >> 24) & 0xff;
99  bgG[i] = (rgba[i] >> 16) & 0xff;
100  bgB[i] = (rgba[i] >> 8) & 0xff;
101  unsigned alpha = (rgba[i] >> 0) & 0xff;
102  bgA[i] = (alpha == 255) ? 256 : alpha;
103  }
104 
105  borderR = (borderRGBA >> 24) & 0xff;
106  borderG = (borderRGBA >> 16) & 0xff;
107  borderB = (borderRGBA >> 8) & 0xff;
108  unsigned alpha = (borderRGBA >> 0) & 0xff;
109  borderA = (alpha == 255) ? 256 : alpha;
110 }
111 
113  : texture(loadTexture(std::move(image), size))
114 {
115 }
116 
117 void GLImage::draw(OutputSurface& /*output*/, ivec2 pos, uint8_t r, uint8_t g, uint8_t b, uint8_t alpha)
118 {
119  // 4-----------------7
120  // | |
121  // | 0---------3 |
122  // | | | |
123  // | | | |
124  // | 1-------- 2 |
125  // | |
126  // 5-----------------6
127  int bx = (size[0] > 0) ? borderSize : -borderSize;
128  int by = (size[1] > 0) ? borderSize : -borderSize;
129  ivec2 positions[8] = {
130  pos + ivec2( + bx, + by), // 0
131  pos + ivec2( + bx, size[1] - by), // 1
132  pos + ivec2(size[0] - bx, size[1] - by), // 2
133  pos + ivec2(size[0] - bx, + by), // 3
134  pos + ivec2(0 , 0 ), // 4
135  pos + ivec2(0 , size[1] ), // 5
136  pos + ivec2(size[0] , size[1] ), // 6
137  pos + ivec2(size[0] , 0 ), // 7
138  };
139 
140  glEnable(GL_BLEND);
141  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
142  glEnableVertexAttribArray(0);
143 
144  if (texture.get()) {
145  vec2 tex[4] = {
146  vec2(0.0f, 0.0f),
147  vec2(0.0f, 1.0f),
148  vec2(1.0f, 1.0f),
149  vec2(1.0f, 0.0f),
150  };
151 
152  gl::context->progTex.activate();
153  glUniform4f(gl::context->unifTexColor,
154  r / 255.0f, g / 255.0f, b / 255.0f, alpha / 255.0f);
155  glUniformMatrix4fv(gl::context->unifTexMvp, 1, GL_FALSE,
156  &gl::context->pixelMvp[0][0]);
157  glVertexAttribPointer(0, 2, GL_INT, GL_FALSE, 0, positions + 4);
158  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, tex);
159  glEnableVertexAttribArray(1);
160  texture.bind();
161  glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
162  glDisableVertexAttribArray(1);
163  } else {
164  assert(r == 255);
165  assert(g == 255);
166  assert(b == 255);
167  gl::context->progFill.activate();
168  glUniformMatrix4fv(gl::context->unifFillMvp, 1, GL_FALSE,
169  &gl::context->pixelMvp[0][0]);
170  glVertexAttribPointer(0, 2, GL_INT, GL_FALSE, 0, positions);
171  glVertexAttrib4f(1, borderR / 255.0f, borderG / 255.0f, borderB / 255.0f,
172  (borderA * alpha) / (255.0f * 255.0f));
173 
174  if ((2 * borderSize >= abs(size[0])) ||
175  (2 * borderSize >= abs(size[1]))) {
176  // only border
177  glDisableVertexAttribArray(1);
178  glDrawArrays(GL_TRIANGLE_FAN, 4, 4);
179  } else {
180  // border
181  if (borderSize > 0) {
182  uint8_t indices[10] = { 4,0,5,1,6,2,7,3,4,0 };
183  glDisableVertexAttribArray(1);
184  glDrawElements(GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_BYTE, indices);
185  }
186 
187  // interior
188  uint8_t col[4][4] = {
189  { bgR[0], bgG[0], bgB[0], uint8_t((bgA[0] * alpha) / 256) },
190  { bgR[2], bgG[2], bgB[2], uint8_t((bgA[2] * alpha) / 256) },
191  { bgR[3], bgG[3], bgB[3], uint8_t((bgA[3] * alpha) / 256) },
192  { bgR[1], bgG[1], bgB[1], uint8_t((bgA[1] * alpha) / 256) },
193  };
194  glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, col);
195  glEnableVertexAttribArray(1);
196  glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
197  glDisableVertexAttribArray(1);
198  }
199  }
200  glDisableVertexAttribArray(0);
201  glDisable(GL_BLEND);
202 }
203 
204 } // namespace openmsx
SDL_Surface * get()
Most basic/generic texture: only contains a texture ID.
Definition: GLUtil.hh:35
vecN< N, int > trunc(const vecN< N, T > &x)
Definition: gl_vec.hh:383
STL namespace.
A frame buffer where pixels can be written to.
size_t size(std::string_view utf8)
Wrapper around a SDL_Surface.
void bind()
Makes this texture the active GL texture.
Definition: GLUtil.hh:77
static void checkSize(gl::ivec2 size)
Performs a sanity check on image size.
Definition: BaseImage.cc:8
GLuint get() const
Returns the underlying openGL handler id.
Definition: GLUtil.hh:70
std::unique_ptr< Context > context
Definition: GLContext.cc:9
constexpr const char *const filename
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
XMLElement load(string_view filename, string_view systemID)
Definition: XMLLoader.cc:33
vecN< 2, int > ivec2
Definition: gl_vec.hh:147
int g
GLImage(OutputSurface &output, const std::string &filename)
Definition: GLImage.cc:55
void draw(OutputSurface &output, gl::ivec2 pos, uint8_t r, uint8_t g, uint8_t b, uint8_t alpha) override
Definition: GLImage.cc:117
vecN< 2, float > vec2
Definition: gl_vec.hh:144
Definition: gl_mat.hh:24