Guest User

ShellRenderInterfaceOpenGL.cpp

a guest
May 25th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.80 KB | None | 0 0
  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.  
  28. #include <ShellRenderInterfaceOpenGL.h>
  29. #include <Rocket/Core.h>
  30.  
  31. #define GL_CLAMP_TO_EDGE 0x812F
  32.  
  33. ShellRenderInterfaceOpenGL::ShellRenderInterfaceOpenGL()
  34. {
  35. }
  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(num_vertices), int* indices, int num_indices, const Rocket::Core::TextureHandle texture, const Rocket::Core::Vector2f& translation)
  39. {
  40.     glPushMatrix();
  41.     glTranslatef(translation.x, translation.y, 0);
  42.  
  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.  
  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(Rocket::Core::Vertex), &vertices[0].tex_coord);
  58.     }
  59.  
  60.     //glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices);
  61.     glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, indices);
  62.  
  63.     glPopMatrix();
  64. }
  65.  
  66. // Called by Rocket when it wants to compile geometry it believes will be static for the forseeable future.    
  67. 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))
  68. {
  69.     return (Rocket::Core::CompiledGeometryHandle) NULL;
  70. }
  71.  
  72. // Called by Rocket when it wants to render application-compiled geometry.     
  73. void ShellRenderInterfaceOpenGL::RenderCompiledGeometry(Rocket::Core::CompiledGeometryHandle ROCKET_UNUSED(geometry), const Rocket::Core::Vector2f& ROCKET_UNUSED(translation))
  74. {
  75. }
  76.  
  77. // Called by Rocket when it wants to release application-compiled geometry.    
  78. void ShellRenderInterfaceOpenGL::ReleaseCompiledGeometry(Rocket::Core::CompiledGeometryHandle ROCKET_UNUSED(geometry))
  79. {
  80. }
  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.  
  91. // Called by Rocket when it wants to change the scissor region.    
  92. void ShellRenderInterfaceOpenGL::SetScissorRegion(int x, int y, int width, int height)
  93. {
  94.     glScissor(x, 768 - (y + height), width, height);
  95. }
  96.  
  97. // Set to byte packing, or the compiler will expand our struct, which means it won't read correctly from file
  98. #pragma pack(1)
  99. struct TGAHeader
  100. {
  101.     char  idLength;
  102.     char  colourMapType;
  103.     char  dataType;
  104.     short int colourMapOrigin;
  105.     short int colourMapLength;
  106.     char  colourMapDepth;
  107.     short int xOrigin;
  108.     short int yOrigin;
  109.     short int width;
  110.     short int height;
  111.     char  bitsPerPixel;
  112.     char  imageDescriptor;
  113. };
  114. // Restore packing
  115. #pragma pack()
  116.  
  117. // Called by Rocket when a texture is required by the library.     
  118. bool ShellRenderInterfaceOpenGL::LoadTexture(Rocket::Core::TextureHandle& texture_handle, Rocket::Core::Vector2i& texture_dimensions, const Rocket::Core::String& source)
  119. {
  120.     Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
  121.     Rocket::Core::FileHandle file_handle = file_interface->Open(source);
  122.     if (!file_handle)
  123.     {
  124.         return false;
  125.     }
  126.    
  127.     file_interface->Seek(file_handle, 0, SEEK_END);
  128.     size_t buffer_size = file_interface->Tell(file_handle);
  129.     file_interface->Seek(file_handle, 0, SEEK_SET);
  130.    
  131.     char* buffer = new char[buffer_size];
  132.     file_interface->Read(buffer, buffer_size, file_handle);
  133.     file_interface->Close(file_handle);
  134.  
  135.     TGAHeader header;
  136.     memcpy(&header, buffer, sizeof(TGAHeader));
  137.    
  138.     int color_mode = header.bitsPerPixel / 8;
  139.     int image_size = header.width * header.height * 4; // We always make 32bit textures
  140.    
  141.     if (header.dataType != 2)
  142.     {
  143.         Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Only 24/32bit uncompressed TGAs are supported.");
  144.         return false;
  145.     }
  146.    
  147.     // Ensure we have at least 3 colors
  148.     if (color_mode < 3)
  149.     {
  150.         Rocket::Core::Log::Message(Rocket::Core::Log::LT_ERROR, "Only 24 and 32bit textures are supported");
  151.         return false;
  152.     }
  153.    
  154.     const char* image_src = buffer + sizeof(TGAHeader);
  155.     unsigned char* image_dest = new unsigned char[image_size];
  156.    
  157.     // Targa is BGR, swap to RGB and flip Y axis
  158.     for (long y = 0; y < header.height; y++)
  159.     {
  160.         long read_index = y * header.width * color_mode;
  161.         long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * color_mode;
  162.         for (long x = 0; x < header.width; x++)
  163.         {
  164.             image_dest[write_index] = image_src[read_index+2];
  165.             image_dest[write_index+1] = image_src[read_index+1];
  166.             image_dest[write_index+2] = image_src[read_index];
  167.             if (color_mode == 4)
  168.                 image_dest[write_index+3] = image_src[read_index+3];
  169.             else
  170.                 image_dest[write_index+3] = 255;
  171.            
  172.             write_index += 4;
  173.             read_index += color_mode;
  174.         }
  175.     }
  176.  
  177.     texture_dimensions.x = header.width;
  178.     texture_dimensions.y = header.height;
  179.    
  180.     bool success = GenerateTexture(texture_handle, image_dest, texture_dimensions);
  181.    
  182.     delete [] image_dest;
  183.     delete [] buffer;
  184.    
  185.     return success;
  186. }
  187.  
  188. // Called by Rocket when a texture is required to be built from an internally-generated sequence of pixels.
  189. bool ShellRenderInterfaceOpenGL::GenerateTexture(Rocket::Core::TextureHandle& texture_handle, const Rocket::Core::byte* source, const Rocket::Core::Vector2i& source_dimensions)
  190. {
  191.     GLuint texture_id = 0;
  192.     glGenTextures(1, &texture_id);
  193.     if (texture_id == 0)
  194.     {
  195.         printf("Failed to generate textures\n");
  196.         return false;
  197.     }
  198.  
  199.     glBindTexture(GL_TEXTURE_2D, texture_id);
  200.  
  201.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, source_dimensions.x, source_dimensions.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, source);
  202.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  203.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  204.  
  205.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  206.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  207.  
  208.     texture_handle = (Rocket::Core::TextureHandle) texture_id;
  209.  
  210.     return true;
  211. }
  212.  
  213. // Called by Rocket when a loaded texture is no longer required.       
  214. void ShellRenderInterfaceOpenGL::ReleaseTexture(Rocket::Core::TextureHandle texture_handle)
  215. {
  216.     glDeleteTextures(1, (GLuint*) &texture_handle);
  217. }
Advertisement
Add Comment
Please, Sign In to add comment