ShellRenderInterfaceOpenGL.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include <ShellRenderInterfaceExtensions.h>
  28. #include <ShellRenderInterfaceOpenGL.h>
  29. #include <Rocket/Core.h>
  30. #define GL_CLAMP_TO_EDGE 0x812F
  31. ShellRenderInterfaceOpenGL::ShellRenderInterfaceOpenGL()
  32. {
  33. m_rocket_context = NULL;
  34. m_width = 0;
  35. m_height = 0;
  36. }
  37. // Called by Rocket when it wants to render geometry that it does not wish to optimise.
  38. void ShellRenderInterfaceOpenGL::RenderGeometry(Rocket::Core::Vertex* vertices, int ROCKET_UNUSED_PARAMETER(num_vertices), int* indices, int num_indices, const Rocket::Core::TextureHandle texture, const Rocket::Core::Vector2f& translation)
  39. {
  40. ROCKET_UNUSED(num_vertices);
  41. glPushMatrix();
  42. glTranslatef(translation.x, translation.y, 0);
  43. glVertexPointer(2, GL_FLOAT, sizeof(Rocket::Core::Vertex), &vertices[0].position);
  44. glEnableClientState(GL_COLOR_ARRAY);
  45. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Rocket::Core::Vertex), &vertices[0].colour);
  46. if (!texture)
  47. {
  48. glDisable(GL_TEXTURE_2D);
  49. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  50. }
  51. else
  52. {
  53. glEnable(GL_TEXTURE_2D);
  54. glBindTexture(GL_TEXTURE_2D, (GLuint) texture);
  55. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  56. glTexCoordPointer(2, GL_FLOAT, sizeof(Rocket::Core::Vertex), &vertices[0].tex_coord);
  57. }
  58. glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices);
  59. glPopMatrix();
  60. }
  61. // Called by Rocket when it wants to compile geometry it believes will be static for the forseeable future.
  62. Rocket::Core::CompiledGeometryHandle ShellRenderInterfaceOpenGL::CompileGeometry(Rocket::Core::Vertex* ROCKET_UNUSED_PARAMETER(vertices), int ROCKET_UNUSED_PARAMETER(num_vertices), int* ROCKET_UNUSED_PARAMETER(indices), int ROCKET_UNUSED_PARAMETER(num_indices), const Rocket::Core::TextureHandle ROCKET_UNUSED_PARAMETER(texture))
  63. {
  64. ROCKET_UNUSED(vertices);
  65. ROCKET_UNUSED(num_vertices);
  66. ROCKET_UNUSED(indices);
  67. ROCKET_UNUSED(num_indices);
  68. ROCKET_UNUSED(texture);
  69. return (Rocket::Core::CompiledGeometryHandle) NULL;
  70. }
  71. // Called by Rocket when it wants to render application-compiled geometry.
  72. void ShellRenderInterfaceOpenGL::RenderCompiledGeometry(Rocket::Core::CompiledGeometryHandle ROCKET_UNUSED_PARAMETER(geometry), const Rocket::Core::Vector2f& ROCKET_UNUSED_PARAMETER(translation))
  73. {
  74. ROCKET_UNUSED(geometry);
  75. ROCKET_UNUSED(translation);
  76. }
  77. // Called by Rocket when it wants to release application-compiled geometry.
  78. void ShellRenderInterfaceOpenGL::ReleaseCompiledGeometry(Rocket::Core::CompiledGeometryHandle ROCKET_UNUSED_PARAMETER(geometry))
  79. {
  80. ROCKET_UNUSED(geometry);
  81. }
  82. // Called by Rocket when it wants to enable or disable scissoring to clip content.
  83. void ShellRenderInterfaceOpenGL::EnableScissorRegion(bool enable)
  84. {
  85. if (enable)
  86. glEnable(GL_SCISSOR_TEST);
  87. else
  88. glDisable(GL_SCISSOR_TEST);
  89. }
  90. // Called by Rocket when it wants to change the scissor region.
  91. void ShellRenderInterfaceOpenGL::SetScissorRegion(int x, int y, int width, int height)
  92. {
  93. glScissor(x, m_height - (y + height), width, height);
  94. }
  95. // Set to byte packing, or the compiler will expand our struct, which means it won't read correctly from file
  96. #pragma pack(1)
  97. struct TGAHeader
  98. {
  99. char idLength;
  100. char colourMapType;
  101. char dataType;
  102. short int colourMapOrigin;
  103. short int colourMapLength;
  104. char colourMapDepth;
  105. short int xOrigin;
  106. short int yOrigin;
  107. short int width;
  108. short int height;
  109. char bitsPerPixel;
  110. char imageDescriptor;
  111. };
  112. // Restore packing
  113. #pragma pack()
  114. // Called by Rocket when a texture is required by the library.
  115. bool ShellRenderInterfaceOpenGL::LoadTexture(Rocket::Core::TextureHandle& texture_handle, Rocket::Core::Vector2i& texture_dimensions, const Rocket::Core::String& source)
  116. {
  117. Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
  118. Rocket::Core::FileHandle file_handle = file_interface->Open(source);
  119. if (!file_handle)
  120. {
  121. return false;
  122. }
  123. file_interface->Seek(file_handle, 0, SEEK_END);
  124. size_t buffer_size = file_interface->Tell(file_handle);
  125. file_interface->Seek(file_handle, 0, SEEK_SET);
  126. char* buffer = new char[buffer_size];
  127. file_interface->Read(buffer, buffer_size, file_handle);
  128. file_interface->Close(file_handle);
  129. TGAHeader header;
  130. memcpy(&header, buffer, sizeof(TGAHeader));
  131. int color_mode = header.bitsPerPixel / 8;
  132. int image_size = header.width * header.height * 4; // We always make 32bit textures
  133. if (header.dataType != 2)
  134. {
  135. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Only 24/32bit uncompressed TGAs are supported.");
  136. return false;
  137. }
  138. // Ensure we have at least 3 colors
  139. if (color_mode < 3)
  140. {
  141. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Only 24 and 32bit textures are supported");
  142. return false;
  143. }
  144. const char* image_src = buffer + sizeof(TGAHeader);
  145. unsigned char* image_dest = new unsigned char[image_size];
  146. // Targa is BGR, swap to RGB and flip Y axis
  147. for (long y = 0; y < header.height; y++)
  148. {
  149. long read_index = y * header.width * color_mode;
  150. long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * color_mode;
  151. for (long x = 0; x < header.width; x++)
  152. {
  153. image_dest[write_index] = image_src[read_index+2];
  154. image_dest[write_index+1] = image_src[read_index+1];
  155. image_dest[write_index+2] = image_src[read_index];
  156. if (color_mode == 4)
  157. image_dest[write_index+3] = image_src[read_index+3];
  158. else
  159. image_dest[write_index+3] = 255;
  160. write_index += 4;
  161. read_index += color_mode;
  162. }
  163. }
  164. texture_dimensions.x = header.width;
  165. texture_dimensions.y = header.height;
  166. bool success = GenerateTexture(texture_handle, image_dest, texture_dimensions);
  167. delete [] image_dest;
  168. delete [] buffer;
  169. return success;
  170. }
  171. // Called by Rocket when a texture is required to be built from an internally-generated sequence of pixels.
  172. bool ShellRenderInterfaceOpenGL::GenerateTexture(Rocket::Core::TextureHandle& texture_handle, const Rocket::Core::byte* source, const Rocket::Core::Vector2i& source_dimensions)
  173. {
  174. GLuint texture_id = 0;
  175. glGenTextures(1, &texture_id);
  176. if (texture_id == 0)
  177. {
  178. printf("Failed to generate textures\n");
  179. return false;
  180. }
  181. glBindTexture(GL_TEXTURE_2D, texture_id);
  182. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, source_dimensions.x, source_dimensions.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, source);
  183. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  184. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  185. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  186. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  187. texture_handle = (Rocket::Core::TextureHandle) texture_id;
  188. return true;
  189. }
  190. // Called by Rocket when a loaded texture is no longer required.
  191. void ShellRenderInterfaceOpenGL::ReleaseTexture(Rocket::Core::TextureHandle texture_handle)
  192. {
  193. glDeleteTextures(1, (GLuint*) &texture_handle);
  194. }