OgreGenUtilites.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "OgreGenUtilites.h"
  2. #include "Logger.h"
  3. void
  4. CreateTextureFromVram( const Ogre::PixelBox& pb, Vram* vram, const int start_x, const int start_y, const int clut_x, const int clut_y, const int texture_x, const int texture_y, const BPP bpp, const bool transparency )
  5. {
  6. for( u32 y = 0; y < 256; ++y )
  7. {
  8. u32* data = static_cast< u32* >( pb.data ) + ( y + start_y ) * pb.rowPitch;
  9. for( u32 x = 0; x < 256; ++x )
  10. {
  11. if( bpp == BPP_4 )
  12. {
  13. u8 pixel = vram->GetU8( texture_x * 2 + x / 2, texture_y + y );
  14. u16 clut1 = vram->GetU16( clut_x * 2 + ( pixel & 0xf ) * 2, clut_y );
  15. u16 clut2 = vram->GetU16( clut_x * 2 + ( pixel >> 4 ) * 2, clut_y );
  16. u32 ogre_pixel1 = ( ( ( clut1 & 0x1f ) * 255 / 31 ) << 0x18 ) | ( ( ( ( clut1 >> 5 ) & 0x1f ) * 255 / 31 ) << 0x10 ) | ( ( ( ( clut1 >> 10 ) & 0x1f ) * 255 / 31 ) << 0x8 ) | 0;
  17. u8 stp = ( clut1 & 0x8000 ) >> 15;
  18. AddTransparency( ogre_pixel1, transparency, stp );
  19. u32 ogre_pixel2 = ( ( ( clut2 & 0x1f ) * 255 / 31 ) << 0x18 ) | ( ( ( ( clut2 >> 5 ) & 0x1f ) * 255 / 31 ) << 0x10 ) | ( ( ( ( clut2 >> 10 ) & 0x1f ) * 255 / 31 ) << 0x8 ) | 0;
  20. stp = ( clut2 & 0x8000 ) >> 15;
  21. AddTransparency( ogre_pixel2, transparency, stp );
  22. data[ start_x + x ] = ogre_pixel1;
  23. ++x;
  24. data[ start_x + x ] = ogre_pixel2;
  25. }
  26. else if( bpp == BPP_8 )
  27. {
  28. u8 pixel = vram->GetU8( texture_x * 2 + x, texture_y + y );
  29. u16 clut = vram->GetU16( clut_x * 2 + pixel * 2, clut_y );
  30. u32 ogre_pixel = ( ( ( clut & 0x1f ) * 255 / 31 ) << 0x18 ) | ( ( ( ( clut >> 5 ) & 0x1f ) * 255 / 31 ) << 0x10 ) | ( ( ( ( clut >> 10 ) & 0x1f ) * 255 / 31 ) << 0x8 ) | 0;
  31. u8 stp = ( clut & 0x8000 ) >> 15;
  32. AddTransparency( ogre_pixel, transparency, stp );
  33. data[ start_x + x ] = ogre_pixel;
  34. }
  35. else if( bpp == BPP_BLACK )
  36. {
  37. data[ start_x + x ] = 0xffffffff;
  38. }
  39. }
  40. }
  41. }
  42. void
  43. CreateTexture( Vram* vram, const MeshData& mesh_data, const Ogre::String& texture_file_name, const VectorTexForGen& textures )
  44. {
  45. Ogre::TexturePtr ptex;
  46. Ogre::HardwarePixelBufferSharedPtr buffer;
  47. ptex = Ogre::TextureManager::getSingleton().createManual( "DynaTex", "General", Ogre::TEX_TYPE_2D, mesh_data.tex_width, mesh_data.tex_height, 0, Ogre::PF_R8G8B8A8, Ogre::TU_STATIC );
  48. buffer = ptex->getBuffer( 0, 0 );
  49. buffer->lock( Ogre::HardwareBuffer::HBL_DISCARD );
  50. const Ogre::PixelBox& pb = buffer->getCurrentLock();
  51. for( unsigned int i = 0; i < textures.size(); ++i )
  52. {
  53. LOGGER->Log( "CreateTexture palette_x=\"" + Ogre::StringConverter::toString( textures[ i ].palette_x ) + "\" palette_y=\"" + Ogre::StringConverter::toString( textures[ i ].palette_y ) + "\" bpp=\"" + Ogre::StringConverter::toString( textures[ i ].bpp ) + "\"." );
  54. CreateTextureFromVram( pb, vram, textures[ i ].start_x, textures[ i ].start_y, textures[ i ].palette_x, textures[ i ].palette_y, textures[ i ].texture_x, textures[ i ].texture_y, textures[ i ].bpp, true );
  55. }
  56. Ogre::Image image;
  57. image.loadDynamicImage( ( Ogre::uchar* )pb.data, mesh_data.tex_width, mesh_data.tex_height, Ogre::PF_R8G8B8A8 );
  58. image.save( texture_file_name );
  59. buffer->unlock();
  60. return;
  61. }
  62. void
  63. CreateMaterial( const Ogre::String& material_name, const Ogre::String& material_file_name, const Ogre::String& texture_name, const Ogre::String& vertex_program, const Ogre::String& fragment_program )
  64. {
  65. Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create(material_name, "General");
  66. Ogre::Technique* tech = material->getTechnique(0);
  67. Ogre::Pass* pass1 = tech->getPass(0);
  68. pass1->setVertexColourTracking(Ogre::TVC_AMBIENT);
  69. //pass1->setCullingMode(Ogre::CULL_NONE);
  70. if( texture_name != "" )
  71. {
  72. pass1->setAlphaRejectFunction(Ogre::CMPF_GREATER);
  73. pass1->setAlphaRejectValue(0);
  74. Ogre::TextureUnitState* tex = pass1->createTextureUnitState();
  75. tex->setTextureName(texture_name);
  76. tex->setNumMipmaps(-1);
  77. tex->setTextureFiltering(Ogre::TFO_NONE);
  78. }
  79. if( vertex_program != "" )
  80. {
  81. pass1->setVertexProgram( vertex_program, true );
  82. }
  83. if( fragment_program != "" )
  84. {
  85. pass1->setFragmentProgram( fragment_program, true );
  86. }
  87. Ogre::MaterialSerializer mat;
  88. mat.exportMaterial(material, material_file_name);
  89. }
  90. void
  91. AddTexture( TexForGen& texture, const MeshData& data, VectorTexForGen& textures, Logger* logger )
  92. {
  93. for( unsigned int i = 0; i < textures.size(); ++i )
  94. {
  95. if( texture == textures[ i ] )
  96. {
  97. texture.start_x = textures[ i ].start_x;
  98. texture.start_y = textures[ i ].start_y;
  99. return;
  100. }
  101. }
  102. if( logger != NULL )
  103. {
  104. //logger->Log("New Texture: X:" + ToHexString(texture.texture_x, 4, '0') + ", Y:" + ToHexString(texture.texture_y, 4, '0') + ", CLUTX:" + ToHexString(texture.palette_x, 4, '0') + ", CLUTY:" + ToHexString(texture.palette_y, 4, '0') + ", bpp:" + ToHexString(texture.bpp, 4, '0') + "\n");
  105. }
  106. float start_x = 0;
  107. float start_y = 0;
  108. if( textures.size() > 0 )
  109. {
  110. start_x = float(textures[ textures.size() - 1 ].start_x);
  111. start_y = float(textures[ textures.size() - 1 ].start_y);
  112. if( start_x + 256 >= data.tex_width )
  113. {
  114. if( start_y + 256 >= data.tex_height )
  115. {
  116. if( logger != NULL )
  117. {
  118. logger->Log( "[ERROR] Can't add anymore textures. Increase tex size.\n");
  119. }
  120. return;
  121. }
  122. start_x = 0;
  123. start_y += 256;
  124. }
  125. else
  126. {
  127. start_x += 256;
  128. }
  129. }
  130. texture.start_x = start_x;
  131. texture.start_y = start_y;
  132. textures.push_back( texture );
  133. if( logger != NULL )
  134. {
  135. //logger->Log("Startx:" + ToHexString(texture.start_x, 4, '0') + ", Starty:" + ToHexString(texture.start_y, 4, '0') + "\n");
  136. }
  137. }
  138. void
  139. AddTransparency( u32& colour, const bool transparency, const bool stp )
  140. {
  141. if( transparency == false )
  142. {
  143. if( stp == false && ( colour & 0xffffff00 ) == 0 )
  144. {
  145. colour |= 0;
  146. }
  147. else
  148. {
  149. colour |= 255;
  150. }
  151. }
  152. else
  153. {
  154. if( stp == false && ( colour & 0xffffff00 ) == 0 )
  155. {
  156. colour |= 0;
  157. }
  158. else if( stp == false && ( colour & 0xffffff00 ) != 0 )
  159. {
  160. colour |= 255;
  161. }
  162. else if( stp == true && ( colour & 0xffffff00 ) == 0)
  163. {
  164. colour |= 255;
  165. }
  166. else if( stp == true && ( colour & 0xffffff00 ) != 0)
  167. {
  168. colour |= 255 / 4;
  169. }
  170. }
  171. }