.
Cmq app. gli scherzi sono arrivato alla conclusione delle funzioni di ricarico delle texture su android:
Codice:
#if defined(__ANDROID__) || defined(__ANDROID_TEST__)
TEXTURE_ID::TEXTURE_ID():node(0){};
struct TextureNode{
GLuint id;
BYTE* bytes;
int type, width, height;
bool linear,repeart;
TextureNode *next;
TextureNode *prev;
//
TextureNode():
id(0),
bytes(0), type(0), width(0), height(0),
next(0), prev(0),linear(false),repeart(false){}
//
void ClearTextureNode(){
if(bytes)
free(bytes);
};
};
struct TexturePool{
TextureNode* head;
TextureNode* end;
TexturePool():head(0),end(0){}
};
TextureNode* TexturePool_addNode(TexturePool *pool,
int id,BYTE* bytes,
int type,
int width,int height,
bool linear,bool repeart){
ERROR_LOG("TexturePool_addNode")
TextureNode *newnode=new TextureNode();
newnode->id=id;
int channel=0;
if(bytes){
if(type == TYPE_LUMINANCE_ALPHA) channel=2;
if(type == TYPE_RGB) channel=3;
if(type == TYPE_RGBA) channel=4;
newnode->bytes=(BYTE*)malloc(width*height*sizeof(BYTE)*channel);
memcpy(newnode->bytes,bytes,width*height*sizeof(BYTE)*channel);
}
newnode->type=type;
newnode->width=width;
newnode->height=height;
newnode->linear=linear;
newnode->repeart=height;
ERROR_LOG("newnode->bytes[0]="<<(int)(newnode->bytes[0]))
ERROR_LOG("newnode->bytes[1]="<<(int)(newnode->bytes[1]))
ERROR_LOG("newnode->bytes[2]="<<(int)(newnode->bytes[2]))
ERROR_LOG("I'm reload: newnode->width="<<newnode->width<< "\tnewnode->height=" << newnode->height);
if(!pool->head){
pool->end=pool->head=newnode;
return newnode;
}
newnode->next=pool->head;
pool->head->prev=newnode;
pool->head=newnode;
return newnode;
}
void TexturePool_deleteNode(TexturePool *pool,TextureNode *node){
ERROR_LOG("TexturePool_deleteNode\npool->head="<<pool->head<<"\npool->end="<<pool->end)
ERROR_LOG("node="<<node)
if(pool->head==node)
pool->head=node->next;
if(pool->end==node)
pool->end=node->prev;
if(node->next)
node->next->prev=node->prev;
if(node->prev)
node->prev->next=node->next;
node->ClearTextureNode();
delete node;
}
//
TexturePool _globalTexturePool;
#define TEXTUREPOOL (&_globalTexturePool)
//call from CFILE: window_os_android.c
extern "C" void _Android_TextureReload(){
///////////////////////////////////////////
glEnable(GL_TEXTURE_2D);
///////////////////////////////////////////
ERROR_LOG("_Android_TextureReload")
for(TextureNode* p=_globalTexturePool.end;p;p=p->prev){
//if(!glIsTexture(p->id)){
p->id=0;
glGenTextures(1,&(p->id));
glBindTexture(GL_TEXTURE_2D,p->id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D,0,
p->type,
p->width,
p->height,0,
p->type,
GL_UNSIGNED_BYTE,
p->bytes);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, p->linear ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, p->linear ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, p->repeart ? GL_REPEAT : GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, p->repeart ? GL_REPEAT : GL_CLAMP_TO_EDGE);
ERROR_LOG("p->bytes[0]="<<(int)(p->bytes[0]))
ERROR_LOG("p->bytes[1]="<<(int)(p->bytes[1]))
ERROR_LOG("p->bytes[2]="<<(int)(p->bytes[2]))
ERROR_LOG("I'm reload: p->width="<<p->width<< "\tp->height=" << p->height);
// }
}
}
extern "C" void _Android_TextureClear(){
///////////////////////////////////////////
glEnable(GL_TEXTURE_2D);
///////////////////////////////////////////
ERROR_LOG("_Android_TextureClear")
for(TextureNode* p=_globalTexturePool.end;p;p=p->prev){
glDeleteTextures(1,&(p->id));
}
}
#endif
TEXTURE_ID APIRender::MakeTexture2D (BYTE* bytes,
int type,
int width,int height,
bool linear,bool repeart){
TEXTURE_ID outTexture;
///////////////////////////////////////////
glEnable(GL_TEXTURE_2D);
///////////////////////////////////////////
#if defined(__ANDROID__) || defined(__ANDROID_TEST__)
GLuint id;
glGenTextures(1,&id);
glBindTexture(GL_TEXTURE_2D, id);
outTexture.node=(void*)TexturePool_addNode(TEXTUREPOOL,
id,
bytes,
type,
width,
height,
linear,
repeart);
#else
glGenTextures(1,&outTexture);
glBindTexture(GL_TEXTURE_2D, outTexture);
#endif
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D,0,
type,
width,
height,0,
type,
GL_UNSIGNED_BYTE,
bytes);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeart ? GL_REPEAT : GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeart ? GL_REPEAT : GL_CLAMP_TO_EDGE);
///////////////////////////////////////////
return outTexture;
}
void APIRender::SetTexture (TEXTURE_ID id,int i){
#if defined(__ANDROID__) || defined(__ANDROID_TEST__)
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0+i);
glBindTexture(GL_TEXTURE_2D,((TextureNode*)(id.node))->id);
#else
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0+i);
glBindTexture(GL_TEXTURE_2D,id);
#endif
}
void APIRender::DisableTexture (int n){
glActiveTexture(GL_TEXTURE0+n);
glDisable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
}
void APIRender::TextureRepeart(bool r){
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, r ? GL_REPEAT : GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, r ? GL_REPEAT : GL_CLAMP_TO_EDGE);
}
void APIRender::TextureLinear(bool l){
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, l ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, l ? GL_LINEAR : GL_NEAREST);
}
void APIRender::DeleteOneTexture2D(TEXTURE_ID id){
#if defined(__ANDROID__) || defined(__ANDROID_TEST__)
glDeleteTextures(1,&(((TextureNode*)(id.node))->id));
TexturePool_deleteNode(TEXTUREPOOL,(TextureNode*)(id.node));
#else
glDeleteTextures(1,&id);
#endif
}
void APIRender::DeleteTextures2D(TEXTURE_ID *id,int n){
#if defined(__ANDROID__) || defined(__ANDROID_TEST__)
for(int i=0;i<n;++i){
glDeleteTextures(1,&(((TextureNode*)(id[i].node))->id));
TexturePool_deleteNode(TEXTUREPOOL,(TextureNode*)(id[i].node));
}
#else
glDeleteTextures(n,id);
#endif
}
//TEXTURE////////////////////////////////////////////////////////////////////////////////////////