RmlUi_Renderer_GL2.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 "RmlUi_Renderer_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_GL2::RenderInterface_GL2() {}
  52. void RenderInterface_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_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. }
  74. void RenderInterface_GL2::EndFrame() {}
  75. void RenderInterface_GL2::Clear()
  76. {
  77. glClearStencil(0);
  78. glClearColor(0, 0, 0, 1);
  79. glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  80. }
  81. void RenderInterface_GL2::RenderGeometry(Rml::Vertex* vertices, int /*num_vertices*/, int* indices, int num_indices, const Rml::TextureHandle texture,
  82. const Rml::Vector2f& translation)
  83. {
  84. glPushMatrix();
  85. glTranslatef(translation.x, translation.y, 0);
  86. glVertexPointer(2, GL_FLOAT, sizeof(Rml::Vertex), &vertices[0].position);
  87. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Rml::Vertex), &vertices[0].colour);
  88. if (!texture)
  89. {
  90. glDisable(GL_TEXTURE_2D);
  91. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  92. }
  93. else
  94. {
  95. glEnable(GL_TEXTURE_2D);
  96. if (texture != TextureEnableWithoutBinding)
  97. glBindTexture(GL_TEXTURE_2D, (GLuint)texture);
  98. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  99. glTexCoordPointer(2, GL_FLOAT, sizeof(Rml::Vertex), &vertices[0].tex_coord);
  100. }
  101. glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices);
  102. glPopMatrix();
  103. }
  104. void RenderInterface_GL2::EnableScissorRegion(bool enable)
  105. {
  106. if (enable)
  107. glEnable(GL_SCISSOR_TEST);
  108. else
  109. glDisable(GL_SCISSOR_TEST);
  110. }
  111. void RenderInterface_GL2::SetScissorRegion(int x, int y, int width, int height)
  112. {
  113. glScissor(x, viewport_height - (y + height), width, height);
  114. }
  115. // Set to byte packing, or the compiler will expand our struct, which means it won't read correctly from file
  116. #pragma pack(1)
  117. struct TGAHeader {
  118. char idLength;
  119. char colourMapType;
  120. char dataType;
  121. short int colourMapOrigin;
  122. short int colourMapLength;
  123. char colourMapDepth;
  124. short int xOrigin;
  125. short int yOrigin;
  126. short int width;
  127. short int height;
  128. char bitsPerPixel;
  129. char imageDescriptor;
  130. };
  131. // Restore packing
  132. #pragma pack()
  133. bool RenderInterface_GL2::LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& source)
  134. {
  135. Rml::FileInterface* file_interface = Rml::GetFileInterface();
  136. Rml::FileHandle file_handle = file_interface->Open(source);
  137. if (!file_handle)
  138. {
  139. return false;
  140. }
  141. file_interface->Seek(file_handle, 0, SEEK_END);
  142. size_t buffer_size = file_interface->Tell(file_handle);
  143. file_interface->Seek(file_handle, 0, SEEK_SET);
  144. if (buffer_size <= sizeof(TGAHeader))
  145. {
  146. Rml::Log::Message(Rml::Log::LT_ERROR, "Texture file size is smaller than TGAHeader, file is not a valid TGA image.");
  147. file_interface->Close(file_handle);
  148. return false;
  149. }
  150. char* buffer = new char[buffer_size];
  151. file_interface->Read(buffer, buffer_size, file_handle);
  152. file_interface->Close(file_handle);
  153. TGAHeader header;
  154. memcpy(&header, buffer, sizeof(TGAHeader));
  155. int color_mode = header.bitsPerPixel / 8;
  156. int image_size = header.width * header.height * 4; // We always make 32bit textures
  157. if (header.dataType != 2)
  158. {
  159. Rml::Log::Message(Rml::Log::LT_ERROR, "Only 24/32bit uncompressed TGAs are supported.");
  160. delete[] buffer;
  161. return false;
  162. }
  163. // Ensure we have at least 3 colors
  164. if (color_mode < 3)
  165. {
  166. Rml::Log::Message(Rml::Log::LT_ERROR, "Only 24 and 32bit textures are supported.");
  167. delete[] buffer;
  168. return false;
  169. }
  170. const char* image_src = buffer + sizeof(TGAHeader);
  171. unsigned char* image_dest = new unsigned char[image_size];
  172. // Targa is BGR, swap to RGB and flip Y axis
  173. for (long y = 0; y < header.height; y++)
  174. {
  175. long read_index = y * header.width * color_mode;
  176. long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * color_mode;
  177. for (long x = 0; x < header.width; x++)
  178. {
  179. image_dest[write_index] = image_src[read_index + 2];
  180. image_dest[write_index + 1] = image_src[read_index + 1];
  181. image_dest[write_index + 2] = image_src[read_index];
  182. if (color_mode == 4)
  183. image_dest[write_index + 3] = image_src[read_index + 3];
  184. else
  185. image_dest[write_index + 3] = 255;
  186. write_index += 4;
  187. read_index += color_mode;
  188. }
  189. }
  190. texture_dimensions.x = header.width;
  191. texture_dimensions.y = header.height;
  192. bool success = GenerateTexture(texture_handle, image_dest, texture_dimensions);
  193. delete[] image_dest;
  194. delete[] buffer;
  195. return success;
  196. }
  197. bool RenderInterface_GL2::GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* source, const Rml::Vector2i& source_dimensions)
  198. {
  199. GLuint texture_id = 0;
  200. glGenTextures(1, &texture_id);
  201. if (texture_id == 0)
  202. {
  203. Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to generate texture.");
  204. return false;
  205. }
  206. glBindTexture(GL_TEXTURE_2D, texture_id);
  207. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, source_dimensions.x, source_dimensions.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, source);
  208. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  209. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  210. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  211. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  212. texture_handle = (Rml::TextureHandle)texture_id;
  213. return true;
  214. }
  215. void RenderInterface_GL2::ReleaseTexture(Rml::TextureHandle texture_handle)
  216. {
  217. glDeleteTextures(1, (GLuint*)&texture_handle);
  218. }
  219. void RenderInterface_GL2::SetTransform(const Rml::Matrix4f* transform)
  220. {
  221. if (transform)
  222. {
  223. if (std::is_same<Rml::Matrix4f, Rml::ColumnMajorMatrix4f>::value)
  224. glLoadMatrixf(transform->data());
  225. else if (std::is_same<Rml::Matrix4f, Rml::RowMajorMatrix4f>::value)
  226. glLoadMatrixf(transform->Transpose().data());
  227. }
  228. else
  229. glLoadIdentity();
  230. }