ShellRenderInterfaceOpenGL.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include <ShellRenderInterfaceExtensions.h>
  29. #include <ShellRenderInterfaceOpenGL.h>
  30. #include <RmlUi/Core/Core.h>
  31. #include <RmlUi/Core/FileInterface.h>
  32. #include <type_traits>
  33. #include <string.h>
  34. #define GL_CLAMP_TO_EDGE 0x812F
  35. ShellRenderInterfaceOpenGL::ShellRenderInterfaceOpenGL() : m_width(0), m_height(0), m_transform_enabled(false), m_rmlui_context(nullptr)
  36. {
  37. }
  38. // Called by RmlUi when it wants to render geometry that it does not wish to optimise.
  39. void ShellRenderInterfaceOpenGL::RenderGeometry(Rml::Vertex* vertices, int RMLUI_UNUSED_PARAMETER(num_vertices), int* indices, int num_indices, const Rml::TextureHandle texture, const Rml::Vector2f& translation)
  40. {
  41. RMLUI_UNUSED(num_vertices);
  42. glPushMatrix();
  43. glTranslatef(translation.x, translation.y, 0);
  44. glVertexPointer(2, GL_FLOAT, sizeof(Rml::Vertex), &vertices[0].position);
  45. glEnableClientState(GL_COLOR_ARRAY);
  46. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Rml::Vertex), &vertices[0].colour);
  47. if (!texture)
  48. {
  49. glDisable(GL_TEXTURE_2D);
  50. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  51. }
  52. else
  53. {
  54. glEnable(GL_TEXTURE_2D);
  55. glBindTexture(GL_TEXTURE_2D, (GLuint) texture);
  56. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  57. glTexCoordPointer(2, GL_FLOAT, sizeof(Rml::Vertex), &vertices[0].tex_coord);
  58. }
  59. glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices);
  60. glPopMatrix();
  61. }
  62. // Called by RmlUi when it wants to compile geometry it believes will be static for the forseeable future.
  63. Rml::CompiledGeometryHandle ShellRenderInterfaceOpenGL::CompileGeometry(Rml::Vertex* RMLUI_UNUSED_PARAMETER(vertices), int RMLUI_UNUSED_PARAMETER(num_vertices), int* RMLUI_UNUSED_PARAMETER(indices), int RMLUI_UNUSED_PARAMETER(num_indices), const Rml::TextureHandle RMLUI_UNUSED_PARAMETER(texture))
  64. {
  65. RMLUI_UNUSED(vertices);
  66. RMLUI_UNUSED(num_vertices);
  67. RMLUI_UNUSED(indices);
  68. RMLUI_UNUSED(num_indices);
  69. RMLUI_UNUSED(texture);
  70. return (Rml::CompiledGeometryHandle) nullptr;
  71. }
  72. // Called by RmlUi when it wants to render application-compiled geometry.
  73. void ShellRenderInterfaceOpenGL::RenderCompiledGeometry(Rml::CompiledGeometryHandle RMLUI_UNUSED_PARAMETER(geometry), const Rml::Vector2f& RMLUI_UNUSED_PARAMETER(translation))
  74. {
  75. RMLUI_UNUSED(geometry);
  76. RMLUI_UNUSED(translation);
  77. }
  78. // Called by RmlUi when it wants to release application-compiled geometry.
  79. void ShellRenderInterfaceOpenGL::ReleaseCompiledGeometry(Rml::CompiledGeometryHandle RMLUI_UNUSED_PARAMETER(geometry))
  80. {
  81. RMLUI_UNUSED(geometry);
  82. }
  83. // Called by RmlUi when it wants to enable or disable scissoring to clip content.
  84. void ShellRenderInterfaceOpenGL::EnableScissorRegion(bool enable)
  85. {
  86. if (enable) {
  87. if (!m_transform_enabled) {
  88. glEnable(GL_SCISSOR_TEST);
  89. glDisable(GL_STENCIL_TEST);
  90. } else {
  91. glDisable(GL_SCISSOR_TEST);
  92. glEnable(GL_STENCIL_TEST);
  93. }
  94. } else {
  95. glDisable(GL_SCISSOR_TEST);
  96. glDisable(GL_STENCIL_TEST);
  97. }
  98. }
  99. // Called by RmlUi when it wants to change the scissor region.
  100. void ShellRenderInterfaceOpenGL::SetScissorRegion(int x, int y, int width, int height)
  101. {
  102. if (!m_transform_enabled) {
  103. glScissor(x, m_height - (y + height), width, height);
  104. } else {
  105. // clear the stencil buffer
  106. glStencilMask(GLuint(-1));
  107. glClear(GL_STENCIL_BUFFER_BIT);
  108. // fill the stencil buffer
  109. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  110. glDepthMask(GL_FALSE);
  111. glStencilFunc(GL_NEVER, 1, GLuint(-1));
  112. glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP);
  113. float fx = (float)x;
  114. float fy = (float)y;
  115. float fwidth = (float)width;
  116. float fheight = (float)height;
  117. // draw transformed quad
  118. GLfloat vertices[] = {
  119. fx, fy, 0,
  120. fx, fy + fheight, 0,
  121. fx + fwidth, fy + fheight, 0,
  122. fx + fwidth, fy, 0
  123. };
  124. glDisableClientState(GL_COLOR_ARRAY);
  125. glVertexPointer(3, GL_FLOAT, 0, vertices);
  126. GLushort indices[] = { 1, 2, 0, 3 };
  127. glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, indices);
  128. glEnableClientState(GL_COLOR_ARRAY);
  129. // prepare for drawing the real thing
  130. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  131. glDepthMask(GL_TRUE);
  132. glStencilMask(0);
  133. glStencilFunc(GL_EQUAL, 1, GLuint(-1));
  134. }
  135. }
  136. // Set to byte packing, or the compiler will expand our struct, which means it won't read correctly from file
  137. #pragma pack(1)
  138. struct TGAHeader
  139. {
  140. char idLength;
  141. char colourMapType;
  142. char dataType;
  143. short int colourMapOrigin;
  144. short int colourMapLength;
  145. char colourMapDepth;
  146. short int xOrigin;
  147. short int yOrigin;
  148. short int width;
  149. short int height;
  150. char bitsPerPixel;
  151. char imageDescriptor;
  152. };
  153. // Restore packing
  154. #pragma pack()
  155. // Called by RmlUi when a texture is required by the library.
  156. bool ShellRenderInterfaceOpenGL::LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& source)
  157. {
  158. Rml::FileInterface* file_interface = Rml::GetFileInterface();
  159. Rml::FileHandle file_handle = file_interface->Open(source);
  160. if (!file_handle)
  161. {
  162. return false;
  163. }
  164. file_interface->Seek(file_handle, 0, SEEK_END);
  165. size_t buffer_size = file_interface->Tell(file_handle);
  166. file_interface->Seek(file_handle, 0, SEEK_SET);
  167. RMLUI_ASSERTMSG(buffer_size > sizeof(TGAHeader), "Texture file size is smaller than TGAHeader, file must be corrupt or otherwise invalid");
  168. if(buffer_size <= sizeof(TGAHeader))
  169. {
  170. file_interface->Close(file_handle);
  171. return false;
  172. }
  173. char* buffer = new char[buffer_size];
  174. file_interface->Read(buffer, buffer_size, file_handle);
  175. file_interface->Close(file_handle);
  176. TGAHeader header;
  177. memcpy(&header, buffer, sizeof(TGAHeader));
  178. int color_mode = header.bitsPerPixel / 8;
  179. int image_size = header.width * header.height * 4; // We always make 32bit textures
  180. if (header.dataType != 2)
  181. {
  182. Rml::Log::Message(Rml::Log::LT_ERROR, "Only 24/32bit uncompressed TGAs are supported.");
  183. return false;
  184. }
  185. // Ensure we have at least 3 colors
  186. if (color_mode < 3)
  187. {
  188. Rml::Log::Message(Rml::Log::LT_ERROR, "Only 24 and 32bit textures are supported");
  189. return false;
  190. }
  191. const char* image_src = buffer + sizeof(TGAHeader);
  192. unsigned char* image_dest = new unsigned char[image_size];
  193. // Targa is BGR, swap to RGB and flip Y axis
  194. for (long y = 0; y < header.height; y++)
  195. {
  196. long read_index = y * header.width * color_mode;
  197. long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * color_mode;
  198. for (long x = 0; x < header.width; x++)
  199. {
  200. image_dest[write_index] = image_src[read_index+2];
  201. image_dest[write_index+1] = image_src[read_index+1];
  202. image_dest[write_index+2] = image_src[read_index];
  203. if (color_mode == 4)
  204. image_dest[write_index+3] = image_src[read_index+3];
  205. else
  206. image_dest[write_index+3] = 255;
  207. write_index += 4;
  208. read_index += color_mode;
  209. }
  210. }
  211. texture_dimensions.x = header.width;
  212. texture_dimensions.y = header.height;
  213. bool success = GenerateTexture(texture_handle, image_dest, texture_dimensions);
  214. delete [] image_dest;
  215. delete [] buffer;
  216. return success;
  217. }
  218. // Called by RmlUi when a texture is required to be built from an internally-generated sequence of pixels.
  219. bool ShellRenderInterfaceOpenGL::GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* source, const Rml::Vector2i& source_dimensions)
  220. {
  221. GLuint texture_id = 0;
  222. glGenTextures(1, &texture_id);
  223. if (texture_id == 0)
  224. {
  225. printf("Failed to generate textures\n");
  226. return false;
  227. }
  228. glBindTexture(GL_TEXTURE_2D, texture_id);
  229. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, source_dimensions.x, source_dimensions.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, source);
  230. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  231. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  232. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  233. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  234. texture_handle = (Rml::TextureHandle) texture_id;
  235. return true;
  236. }
  237. // Called by RmlUi when a loaded texture is no longer required.
  238. void ShellRenderInterfaceOpenGL::ReleaseTexture(Rml::TextureHandle texture_handle)
  239. {
  240. glDeleteTextures(1, (GLuint*) &texture_handle);
  241. }
  242. // Called by RmlUi when it wants to set the current transform matrix to a new matrix.
  243. void ShellRenderInterfaceOpenGL::SetTransform(const Rml::Matrix4f* transform)
  244. {
  245. m_transform_enabled = (bool)transform;
  246. if (transform)
  247. {
  248. if (std::is_same<Rml::Matrix4f, Rml::ColumnMajorMatrix4f>::value)
  249. glLoadMatrixf(transform->data());
  250. else if (std::is_same<Rml::Matrix4f, Rml::RowMajorMatrix4f>::value)
  251. glLoadMatrixf(transform->Transpose().data());
  252. }
  253. else
  254. glLoadIdentity();
  255. }
  256. ShellRenderInterfaceOpenGL::Image ShellRenderInterfaceOpenGL::CaptureScreen()
  257. {
  258. Image image;
  259. image.num_components = 3;
  260. image.width = m_width;
  261. image.height = m_height;
  262. const int byte_size = image.width * image.height * image.num_components;
  263. image.data = Rml::UniquePtr<Rml::byte[]>(new Rml::byte[byte_size]);
  264. glReadPixels(0, 0, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, image.data.get());
  265. bool result = true;
  266. GLenum err;
  267. while ((err = glGetError()) != GL_NO_ERROR)
  268. {
  269. result = false;
  270. Rml::Log::Message(Rml::Log::LT_ERROR, "Could not capture screenshot, got GL error: 0x%x", err);
  271. }
  272. if (!result)
  273. return Image();
  274. return image;
  275. }