RmlUi_Renderer_BackwardCompatible_GL2.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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-2023 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 "RmlUi_Renderer_BackwardCompatible_GL2.h"
  29. #include <RmlUi/Core/Core.h>
  30. #include <RmlUi/Core/FileInterface.h>
  31. #include <RmlUi/Core/Log.h>
  32. #include <RmlUi/Core/Platform.h>
  33. #include <string.h>
  34. #if defined RMLUI_PLATFORM_WIN32
  35. #include "RmlUi_Include_Windows.h"
  36. #include <gl/Gl.h>
  37. #include <gl/Glu.h>
  38. #elif defined RMLUI_PLATFORM_MACOSX
  39. #include <AGL/agl.h>
  40. #include <OpenGL/gl.h>
  41. #include <OpenGL/glext.h>
  42. #include <OpenGL/glu.h>
  43. #elif defined RMLUI_PLATFORM_UNIX
  44. #include "RmlUi_Include_Xlib.h"
  45. #include <GL/gl.h>
  46. #include <GL/glext.h>
  47. #include <GL/glu.h>
  48. #include <GL/glx.h>
  49. #endif
  50. #define GL_CLAMP_TO_EDGE 0x812F
  51. RenderInterface_BackwardCompatible_GL2::RenderInterface_BackwardCompatible_GL2() {}
  52. void RenderInterface_BackwardCompatible_GL2::SetViewport(int in_viewport_width, int in_viewport_height)
  53. {
  54. viewport_width = in_viewport_width;
  55. viewport_height = in_viewport_height;
  56. }
  57. void RenderInterface_BackwardCompatible_GL2::BeginFrame()
  58. {
  59. RMLUI_ASSERT(viewport_width >= 0 && viewport_height >= 0);
  60. glViewport(0, 0, viewport_width, viewport_height);
  61. glEnableClientState(GL_VERTEX_ARRAY);
  62. glEnableClientState(GL_COLOR_ARRAY);
  63. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  64. glEnable(GL_BLEND);
  65. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  66. Rml::Matrix4f projection = Rml::Matrix4f::ProjectOrtho(0, (float)viewport_width, (float)viewport_height, 0, -10000, 10000);
  67. glMatrixMode(GL_PROJECTION);
  68. glLoadMatrixf(projection.data());
  69. glMatrixMode(GL_TEXTURE);
  70. glLoadIdentity();
  71. glMatrixMode(GL_MODELVIEW);
  72. glLoadIdentity();
  73. transform_enabled = false;
  74. }
  75. void RenderInterface_BackwardCompatible_GL2::EndFrame() {}
  76. void RenderInterface_BackwardCompatible_GL2::Clear()
  77. {
  78. glClearStencil(0);
  79. glClearColor(0, 0, 0, 1);
  80. glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  81. }
  82. void RenderInterface_BackwardCompatible_GL2::RenderGeometry(Rml::Vertex* vertices, int /*num_vertices*/, int* indices, int num_indices,
  83. const Rml::TextureHandle texture, const Rml::Vector2f& translation)
  84. {
  85. glPushMatrix();
  86. glTranslatef(translation.x, translation.y, 0);
  87. glVertexPointer(2, GL_FLOAT, sizeof(Rml::Vertex), &vertices[0].position);
  88. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Rml::Vertex), &vertices[0].colour);
  89. if (!texture)
  90. {
  91. glDisable(GL_TEXTURE_2D);
  92. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  93. }
  94. else
  95. {
  96. glEnable(GL_TEXTURE_2D);
  97. if (texture != TextureEnableWithoutBinding)
  98. glBindTexture(GL_TEXTURE_2D, (GLuint)texture);
  99. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  100. glTexCoordPointer(2, GL_FLOAT, sizeof(Rml::Vertex), &vertices[0].tex_coord);
  101. }
  102. glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices);
  103. glPopMatrix();
  104. }
  105. void RenderInterface_BackwardCompatible_GL2::EnableScissorRegion(bool enable)
  106. {
  107. if (enable)
  108. {
  109. if (!transform_enabled)
  110. {
  111. glEnable(GL_SCISSOR_TEST);
  112. glDisable(GL_STENCIL_TEST);
  113. }
  114. else
  115. {
  116. glDisable(GL_SCISSOR_TEST);
  117. glEnable(GL_STENCIL_TEST);
  118. }
  119. }
  120. else
  121. {
  122. glDisable(GL_SCISSOR_TEST);
  123. glDisable(GL_STENCIL_TEST);
  124. }
  125. }
  126. void RenderInterface_BackwardCompatible_GL2::SetScissorRegion(int x, int y, int width, int height)
  127. {
  128. if (!transform_enabled)
  129. {
  130. glScissor(x, viewport_height - (y + height), width, height);
  131. }
  132. else
  133. {
  134. // clear the stencil buffer
  135. glStencilMask(GLuint(-1));
  136. glClear(GL_STENCIL_BUFFER_BIT);
  137. // fill the stencil buffer
  138. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  139. glDepthMask(GL_FALSE);
  140. glStencilFunc(GL_NEVER, 1, GLuint(-1));
  141. glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP);
  142. float fx = (float)x;
  143. float fy = (float)y;
  144. float fwidth = (float)width;
  145. float fheight = (float)height;
  146. // draw transformed quad
  147. GLfloat vertices[] = {fx, fy, 0, fx, fy + fheight, 0, fx + fwidth, fy + fheight, 0, fx + fwidth, fy, 0};
  148. glDisableClientState(GL_COLOR_ARRAY);
  149. glVertexPointer(3, GL_FLOAT, 0, vertices);
  150. GLushort indices[] = {1, 2, 0, 3};
  151. glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, indices);
  152. glEnableClientState(GL_COLOR_ARRAY);
  153. // prepare for drawing the real thing
  154. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  155. glDepthMask(GL_TRUE);
  156. glStencilMask(0);
  157. glStencilFunc(GL_EQUAL, 1, GLuint(-1));
  158. }
  159. }
  160. // Set to byte packing, or the compiler will expand our struct, which means it won't read correctly from file
  161. #pragma pack(1)
  162. struct TGAHeader {
  163. char idLength;
  164. char colourMapType;
  165. char dataType;
  166. short int colourMapOrigin;
  167. short int colourMapLength;
  168. char colourMapDepth;
  169. short int xOrigin;
  170. short int yOrigin;
  171. short int width;
  172. short int height;
  173. char bitsPerPixel;
  174. char imageDescriptor;
  175. };
  176. // Restore packing
  177. #pragma pack()
  178. bool RenderInterface_BackwardCompatible_GL2::LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions,
  179. const Rml::String& source)
  180. {
  181. Rml::FileInterface* file_interface = Rml::GetFileInterface();
  182. Rml::FileHandle file_handle = file_interface->Open(source);
  183. if (!file_handle)
  184. {
  185. return false;
  186. }
  187. file_interface->Seek(file_handle, 0, SEEK_END);
  188. size_t buffer_size = file_interface->Tell(file_handle);
  189. file_interface->Seek(file_handle, 0, SEEK_SET);
  190. if (buffer_size <= sizeof(TGAHeader))
  191. {
  192. Rml::Log::Message(Rml::Log::LT_ERROR, "Texture file size is smaller than TGAHeader, file is not a valid TGA image.");
  193. file_interface->Close(file_handle);
  194. return false;
  195. }
  196. char* buffer = new char[buffer_size];
  197. file_interface->Read(buffer, buffer_size, file_handle);
  198. file_interface->Close(file_handle);
  199. TGAHeader header;
  200. memcpy(&header, buffer, sizeof(TGAHeader));
  201. int color_mode = header.bitsPerPixel / 8;
  202. int image_size = header.width * header.height * 4; // We always make 32bit textures
  203. if (header.dataType != 2)
  204. {
  205. Rml::Log::Message(Rml::Log::LT_ERROR, "Only 24/32bit uncompressed TGAs are supported.");
  206. delete[] buffer;
  207. return false;
  208. }
  209. // Ensure we have at least 3 colors
  210. if (color_mode < 3)
  211. {
  212. Rml::Log::Message(Rml::Log::LT_ERROR, "Only 24 and 32bit textures are supported.");
  213. delete[] buffer;
  214. return false;
  215. }
  216. const char* image_src = buffer + sizeof(TGAHeader);
  217. unsigned char* image_dest = new unsigned char[image_size];
  218. // Targa is BGR, swap to RGB and flip Y axis
  219. for (long y = 0; y < header.height; y++)
  220. {
  221. long read_index = y * header.width * color_mode;
  222. long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * color_mode;
  223. for (long x = 0; x < header.width; x++)
  224. {
  225. image_dest[write_index] = image_src[read_index + 2];
  226. image_dest[write_index + 1] = image_src[read_index + 1];
  227. image_dest[write_index + 2] = image_src[read_index];
  228. if (color_mode == 4)
  229. image_dest[write_index + 3] = image_src[read_index + 3];
  230. else
  231. image_dest[write_index + 3] = 255;
  232. write_index += 4;
  233. read_index += color_mode;
  234. }
  235. }
  236. texture_dimensions.x = header.width;
  237. texture_dimensions.y = header.height;
  238. bool success = GenerateTexture(texture_handle, image_dest, texture_dimensions);
  239. delete[] image_dest;
  240. delete[] buffer;
  241. return success;
  242. }
  243. bool RenderInterface_BackwardCompatible_GL2::GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* source,
  244. const Rml::Vector2i& source_dimensions)
  245. {
  246. GLuint texture_id = 0;
  247. glGenTextures(1, &texture_id);
  248. if (texture_id == 0)
  249. {
  250. Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to generate texture.");
  251. return false;
  252. }
  253. glBindTexture(GL_TEXTURE_2D, texture_id);
  254. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, source_dimensions.x, source_dimensions.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, source);
  255. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  256. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  257. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  258. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  259. texture_handle = (Rml::TextureHandle)texture_id;
  260. return true;
  261. }
  262. void RenderInterface_BackwardCompatible_GL2::ReleaseTexture(Rml::TextureHandle texture_handle)
  263. {
  264. glDeleteTextures(1, (GLuint*)&texture_handle);
  265. }
  266. void RenderInterface_BackwardCompatible_GL2::SetTransform(const Rml::Matrix4f* transform)
  267. {
  268. transform_enabled = (transform != nullptr);
  269. if (transform)
  270. {
  271. if (std::is_same<Rml::Matrix4f, Rml::ColumnMajorMatrix4f>::value)
  272. glLoadMatrixf(transform->data());
  273. else if (std::is_same<Rml::Matrix4f, Rml::RowMajorMatrix4f>::value)
  274. glLoadMatrixf(transform->Transpose().data());
  275. }
  276. else
  277. glLoadIdentity();
  278. }