以前に作成していたアプリをUnityに移植する事になりました。
元々はOpenGL ES1で作成していたのですが、UnityがES1のサポートが終了していたので、
ES2に移行させる必要が発生しました。
ES1の時は、8bitインデックを読み込む場合、
GL_PALETTE8_RGB8_OES
GL_PALETTE8_RGBA8_OES
GL_PALETTE8_R5_G6_B5_OES
GL_PALETTE8_RGBA4_OES
GL_PALETTE8_RGB5_A1_OES
GL_PALETTE8_RGB8_OES 等
を使用していたのですが、ES2ではこれらのサポートが終了しているようで
ES2での読み込み方法が解らなかった為、質問致しました。
###OpenGE ES1のソース
C++
1// test.cpp 2 3#include "test.h" 4 5TEXTURE::TEXTURE() : Width(0), Height(0), TexID(0), TexParam(0) 6{ 7} 8 9TEXTURE::~TEXTURE() 10{ 11 release(); 12} 13 14bool TEXTURE::loadBmpData(unsigned char *pLoadBuffer, int KeyIndex, unsigned char *pPalette) 15{ 16 int i; 17 18 BITMAPFILEHEADER *pBF = NULL; 19 BITMAPINFOHEADER *pBI = NULL; 20 21 unsigned int BmpPitch; 22 int PalCount; 23 unsigned char *pBmpPal; 24 unsigned char *pBmpBits; 25 26 unsigned int Pitch; 27 unsigned int Size; 28 unsigned char *pData; 29 unsigned short *pPal; 30 unsigned char *pBits; 31 32 int Point = 0; 33 int Point2 = 0; 34 unsigned int Format = 0; 35 36 if( pLoadBuffer == NULL ) return false; 37 if( 0 != memcpy( pLoadBuffer, "BM", 2 ) ) return false; 38 39 // ヘッダ部抽出。 40 pBF = (BITMAPFILEHEADER*)( pLoadBuffer ); 41 pBI = (BITMAPINFOHEADER*)( pLoadBuffer + sizeof(BITMAPFILEHEADER) ); 42 43 BmpPitch = ( ( ((pBI->biWidth * pBI->biBitCount) >> 2) + 7 ) & ~7 ) >> 1; 44 pBmpPal = &pLoadBuffer[ sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ) ]; 45 pBmpBits = &pLoadBuffer[ pBF->bfOffBits ]; 46 47 // パレット変更があるならポインタ差し替え 48 if( pPalette != NULL ) pBmpPal = pPalette; 49 50 // パレット数 51 PalCount = (1 << pBI->biBitCount); 52 53 // サイズを2の乗数になるように調整 54 Width = Height = 1; 55 while( Width < pBI->biWidth ) Width <<= 1; 56 while( Height < pBI->biHeight ) Height <<= 1; 57 58 Pitch = ((Width * pBI->biBitCount) >> 3); 59 Size = PalCount * sizeof(unsigned short) + Pitch * Height; 60 pData = (unsigned char*)malloc( Size ); 61 if( pData == NULL ) return false; 62 pPal = (unsigned short*)&pData[ 0 ]; 63 pBits = (unsigned char*)&pData[ PalCount * sizeof(unsigned short) ]; 64 65 // 8888を5551に変換 不透明部分に0xFF 透過部分に0x00を入れる 66 for( i = 0 ; i < PalCount ; i++ ) 67 { 68 if( KeyIndex == i ) pPal[ i ] = RGBA_TO_R5G5B5A1( pBmpPal[ i * 4 + 2 ], pBmpPal[ i * 4 + 1 ], pBmpPal[ i * 4 + 0 ], 0x00 ); 69 else pPal[ i ] = RGBA_TO_R5G5B5A1( pBmpPal[ i * 4 + 2 ], pBmpPal[ i * 4 + 1 ], pBmpPal[ i * 4 + 0 ], 0xFF ); 70 } 71 72 // データ部分を上下反転 73 for( i = pBI->biHeight - 1 ; i >= 0 ; i-- ) 74 { 75 Point = BmpPitch * i; 76 77 memcpy( &pBits[ Point2 ], &pBmpBits[ Point ], BmpPitch ); 78 79 Point2 += Pitch; 80 } 81 82 if( pBI->biBitCount == 4 ) Format = GL_PALETTE4_RGB5_A1_OES; 83 else Format = GL_PALETTE8_RGB5_A1_OES; 84 85 // OpenGLバッファ作成 86 glEnable( GL_TEXTURE_2D ); 87 glGenTextures( 1, &TexID ); 88 glBindTexture( GL_TEXTURE_2D, TexID ); 89 90 // テクスチャ作成 91 glCompressedTexImage2D( GL_TEXTURE_2D, 0, Format, Width, Height, 0, Size, pData ); 92 93 // パラメータ設定 94 glBindTexture( GL_TEXTURE_2D, TexID ); 95 96 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 97 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 98 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); 99 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); 100 101 if(pData) { free((void *)pData); pData = 0; } 102 103 return true; 104} 105 106void TEXTURE::release() 107{ 108 if( TexID == 0 ) return; 109 110 glBindTexture( GL_TEXTURE_2D, 0 ); 111 glDeleteTextures( 1, &TexID ); 112 113 Width = 0; 114 Height = 0; 115 TexID = 0; 116 TexParam = 0; 117} 118
C++
1// test.h 2 3#ifndef __TEXTURE_HEADER__ 4#define __TEXTURE_HEADER__ 5 6#ifdef __ANDROID__ 7#include <GLES/gl.h> 8#include <GLES/glxt.h> 9#else 10#include <OpenGLES/ES1/gl.h> 11#include <OpenGLES/ES1/glext.h> 12#endif 13 14#include <string.h> 15#include <stdlib.h> 16 17class TEXTURE 18{ 19public: 20 int Width; // 画像の幅 21 int Height; // 画像の高さ 22 GLuint TexID; // 識別番号 23 int TexParam; 24 25private: 26 27 void loadIndexObmData(unsigned char *pLoadBuffer, unsigned char *pPalette ); 28 29public: 30 TEXTURE(); 31 32 ~TEXTURE(); 33 34 bool loadBmpData(unsigned char *pLoadBuffer, int KeyIndex, unsigned char *pPalette); 35 36 void release(); 37}; 38 39struct BITMAPFILEHEADER 40{ 41 unsigned short bfType; 42 unsigned int bfSize; 43 unsigned short bfReserved1; 44 unsigned short bfReserved2; 45 unsigned int bfOffBits; 46}; 47 48 49struct BITMAPINFOHEADER 50{ 51 unsigned int biSize; 52 int biWidth; 53 int biHeight; 54 unsigned short biPlanes; 55 unsigned short biBitCount; 56 unsigned int biCompression; 57 unsigned int biSizeImage; 58 int biXPelsPerMeter; 59 int biYPelsPerMeter; 60 unsigned int biClrUsed; 61 unsigned int biClrImportant; 62}; 63 64#define RGBA_TO_R5G5B5A1( r, g, b, a ) ( (((r) >> 3) << 11) | (((g) >> 3) << 6) | (((b) >> 3) << 1) | ((a) >> 7) ) 65 66// Unityからの呼び出し用に追加 67extern "C" 68{ 69 namespace 70 { 71 TEXTURE* g_pTexture; // テクスチャー本体 72 int g_itextureCount = 0; 73 } 74 75 // TEXTUREを生成してポインターを返す 76 void* getTexture() 77 { 78 g_pTexture = new TEXTURE(); 79 return g_pTexture; 80 } 81 82 // TEXTUREを解放して削除する 83 void releaseTexture() 84 { 85 if(g_pTexture != nullptr) 86 delete g_pTexture; 87 } 88 89 // Bmpデータを読み込む 90 bool loadBmpData(void* ptr, unsigned char *pLoadBuffer, int KeyIndex, unsigned char *pPalette) 91 { 92 TEXTURE* pTexture = reinterpret_cast<TEXTURE*>(ptr); 93 return pTexture->loadBmpData( pLoadBuffer, KeyIndex, pPalette ); 94 } 95 96 // Unityで作成したテクスチャハンドルに描画するよう設定する 97 void setTextureID(void* ptr, void* texture_id) 98 { 99 TEXTURE* pTexture = reinterpret_cast<TEXTURE*>(ptr); 100 pTexture->TexID = (GLuint)(size_t)texture_id; 101 } 102} 103 104#endif // __TEXTURE_HEADER__ 105 106
上記をUnityのプラグインに移植して、Unity側から呼び出して使用しようと思っています。
具体的には
http://tips.hecomi.com/entry/2016/01/04/183121
のサイトで行われているような感じでテクスチャをプラグイン側で作成して
Unity側で読み込んでAndroid/iOSで表示させようと思っています。
画像を16bitにして、インデックスを使用しないパターンやpng化も考慮しましたが
アプリ容量の問題や画像数がかなり多いので、従来のデータ(8bitインデックス)を
読み込ませたいと考えています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。