OgreGenUtilites.cpp 7.0 KB

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