ShellRenderInterfaceOpenGL.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <ShellRenderInterfaceOpenGL.h>
  28. #include <Rocket/Core.h>
  29. #define GL_CLAMP_TO_EDGE 0x812F
  30. ShellRenderInterfaceOpenGL::ShellRenderInterfaceOpenGL()
  31. {
  32. }
  33. // Called by Rocket when it wants to render geometry that it does not wish to optimise.
  34. void ShellRenderInterfaceOpenGL::RenderGeometry(Rocket::Core::Vertex* vertices, int ROCKET_UNUSED(num_vertices), int* indices, int num_indices, const Rocket::Core::TextureHandle texture, const Rocket::Core::Vector2f& translation)
  35. {
  36. glPushMatrix();
  37. glTranslatef(translation.x, translation.y, 0);
  38. glVertexPointer(2, GL_FLOAT, sizeof(Rocket::Core::Vertex), &vertices[0].position);
  39. glEnableClientState(GL_COLOR_ARRAY);
  40. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Rocket::Core::Vertex), &vertices[0].colour);
  41. if (!texture)
  42. {
  43. glDisable(GL_TEXTURE_2D);
  44. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  45. }
  46. else
  47. {
  48. glEnable(GL_TEXTURE_2D);
  49. glBindTexture(GL_TEXTURE_2D, (GLuint) texture);
  50. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  51. glTexCoordPointer(2, GL_FLOAT, sizeof(Rocket::Core::Vertex), &vertices[0].tex_coord);
  52. }
  53. glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices);
  54. glPopMatrix();
  55. }
  56. // Called by Rocket when it wants to compile geometry it believes will be static for the forseeable future.
  57. Rocket::Core::CompiledGeometryHandle ShellRenderInterfaceOpenGL::CompileGeometry(Rocket::Core::Vertex* ROCKET_UNUSED(vertices), int ROCKET_UNUSED(num_vertices), int* ROCKET_UNUSED(indices), int ROCKET_UNUSED(num_indices), const Rocket::Core::TextureHandle ROCKET_UNUSED(texture))
  58. {
  59. return (Rocket::Core::CompiledGeometryHandle) NULL;
  60. }
  61. // Called by Rocket when it wants to render application-compiled geometry.
  62. void ShellRenderInterfaceOpenGL::RenderCompiledGeometry(Rocket::Core::CompiledGeometryHandle ROCKET_UNUSED(geometry), const Rocket::Core::Vector2f& ROCKET_UNUSED(translation))
  63. {
  64. }
  65. // Called by Rocket when it wants to release application-compiled geometry.
  66. void ShellRenderInterfaceOpenGL::ReleaseCompiledGeometry(Rocket::Core::CompiledGeometryHandle ROCKET_UNUSED(geometry))
  67. {
  68. }
  69. // Called by Rocket when it wants to enable or disable scissoring to clip content.
  70. void ShellRenderInterfaceOpenGL::EnableScissorRegion(bool enable)
  71. {
  72. if (enable)
  73. glEnable(GL_SCISSOR_TEST);
  74. else
  75. glDisable(GL_SCISSOR_TEST);
  76. }
  77. // Called by Rocket when it wants to change the scissor region.
  78. void ShellRenderInterfaceOpenGL::SetScissorRegion(int x, int y, int width, int height)
  79. {
  80. glScissor(x, 768 - (y + height), width, height);
  81. }
  82. // Set to byte packing, or the compiler will expand our struct, which means it won't read correctly from file
  83. #pragma pack(1)
  84. struct TGAHeader
  85. {
  86. char idLength;
  87. char colourMapType;
  88. char dataType;
  89. short int colourMapOrigin;
  90. short int colourMapLength;
  91. char colourMapDepth;
  92. short int xOrigin;
  93. short int yOrigin;
  94. short int width;
  95. short int height;
  96. char bitsPerPixel;
  97. char imageDescriptor;
  98. };
  99. // Restore packing
  100. #pragma pack()
  101. // Called by Rocket when a texture is required by the library.
  102. bool ShellRenderInterfaceOpenGL::LoadTexture(Rocket::Core::TextureHandle& texture_handle, Rocket::Core::Vector2i& texture_dimensions, const Rocket::Core::String& source)
  103. {
  104. Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
  105. Rocket::Core::FileHandle file_handle = file_interface->Open(source);
  106. if (!file_handle)
  107. {
  108. return false;
  109. }
  110. file_interface->Seek(file_handle, 0, SEEK_END);
  111. size_t buffer_size = file_interface->Tell(file_handle);
  112. file_interface->Seek(file_handle, 0, SEEK_SET);
  113. char* buffer = new char[buffer_size];
  114. file_interface->Read(buffer, buffer_size, file_handle);
  115. file_interface->Close(file_handle);
  116. TGAHeader header;
  117. memcpy(&header, buffer, sizeof(TGAHeader));
  118. int color_mode = header.bitsPerPixel / 8;
  119. int image_size = header.width * header.height * 4; // We always make 32bit textures
  120. if (header.dataType != 2)
  121. {
  122. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Only 24/32bit uncompressed TGAs are supported.");
  123. return false;
  124. }
  125. // Ensure we have at least 3 colors
  126. if (color_mode < 3)
  127. {
  128. Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Only 24 and 32bit textures are supported");
  129. return false;
  130. }
  131. const char* image_src = buffer + sizeof(TGAHeader);
  132. unsigned char* image_dest = new unsigned char[image_size];
  133. // Targa is BGR, swap to RGB and flip Y axis
  134. for (long y = 0; y < header.height; y++)
  135. {
  136. long read_index = y * header.width * color_mode;
  137. long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * color_mode;
  138. for (long x = 0; x < header.width; x++)
  139. {
  140. image_dest[write_index] = image_src[read_index+2];
  141. image_dest[write_index+1] = image_src[read_index+1];
  142. image_dest[write_index+2] = image_src[read_index];
  143. if (color_mode == 4)
  144. image_dest[write_index+3] = image_src[read_index+3];
  145. else
  146. image_dest[write_index+3] = 255;
  147. write_index += 4;
  148. read_index += color_mode;
  149. }
  150. }
  151. texture_dimensions.x = header.width;
  152. texture_dimensions.y = header.height;
  153. bool success = GenerateTexture(texture_handle, image_dest, texture_dimensions);
  154. delete [] image_dest;
  155. delete [] buffer;
  156. return success;
  157. }
  158. // Called by Rocket when a texture is required to be built from an internally-generated sequence of pixels.
  159. bool ShellRenderInterfaceOpenGL::GenerateTexture(Rocket::Core::TextureHandle& texture_handle, const Rocket::Core::byte* source, const Rocket::Core::Vector2i& source_dimensions)
  160. {
  161. GLuint texture_id = 0;
  162. glGenTextures(1, &texture_id);
  163. if (texture_id == 0)
  164. {
  165. printf("Failed to generate textures\n");
  166. return false;
  167. }
  168. glBindTexture(GL_TEXTURE_2D, texture_id);
  169. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, source_dimensions.x, source_dimensions.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, source);
  170. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  171. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  172. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  173. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  174. texture_handle = (Rocket::Core::TextureHandle) texture_id;
  175. return true;
  176. }
  177. // Called by Rocket when a loaded texture is no longer required.
  178. void ShellRenderInterfaceOpenGL::ReleaseTexture(Rocket::Core::TextureHandle texture_handle)
  179. {
  180. glDeleteTextures(1, (GLuint*) &texture_handle);
  181. }