Hi,
I have a little problem with rendring into texture. When I render text into backbuffer and copy backbuffer into texture by glCopyTexImage2D and then I mapped the texture on quad and render it, it looks like it has only half aplha.
If I render texture twice on same place it looks as the source backbuffer.
Backbuffer content
Quad with pre-rendred texture (Doesn't look like as original backbuffer and it's wrong!)
Function for render into texture
void BAMWE_text::textOutput(int size_x, int size_y, const char *text, BAMWE* engine, unsigned short style)
{
int new_x, new_y;
new_x = size_x <= 512 ? (size_x <= 256 ? (size_x <= 128 ? 128 : 256) : 512) : 1024;
new_y = size_y <= 512 ? (size_y <= 256 ? (size_y <= 128 ? 128 : 256) : 512) : 1024;
// set viewport
glViewport(0, 0, new_x, new_y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, new_x, new_y, 0.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// free old texture
if(TID > 0 && (texture_x != new_x || texture_y != new_y))
{
engine->freeTexture(TID);
TID = -1;
}
texture_x = new_x;
texture_y = new_y;
// map the quad
if(x != size_x || y != size_y)
{
sprite.getVertices()[0] = BAMWE_vertex(0, 0, 0);
sprite.getVertices()[1] = BAMWE_vertex((float)size_x, 0, 0);
sprite.getVertices()[2] = BAMWE_vertex((float)size_x, (float)size_y, 0);
sprite.getVertices()[3] = BAMWE_vertex(0, (float)size_y, 0);
sprite.getTextureCords()[0] = BAMWE_texture_cord(0,1);
sprite.getTextureCords()[1] = BAMWE_texture_cord((float)size_x/(float)new_x,1);
sprite.getTextureCords()[2] = BAMWE_texture_cord((float)size_x/(float)new_x,1-(float)size_y/(float)new_y);
sprite.getTextureCords()[3] = BAMWE_texture_cord(0,1-(float)size_y/(float)new_y);
sprite.createVBO();
}
// create free texture
if((TID = engine->createEmptyTexture(new_x, new_y)) == -1)
{
engine->log(__FILE__, __LINE__, "Can't create texture");
return;
}
// clear backbuffer
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// write text
glColor4f(1.0f,1.0f,1.0f,1.0f);
font_program->textOutputRECT(0, 0, (float)size_x, (float)size_y, text, engine, style);
// copy backbuffer
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, engine->getTexture(TID));
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, new_x, new_y, 0);
// clear backbuffer
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// return viewport
glViewport(0, 0, engine->getWidth(), engine->getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, engine->getWidth(), engine->getHeight(), 0.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Create empty texture
int BAMWE::createEmptyTexture(unsigned short x, unsigned int y)
{
int TID;
unsigned int* data;
// alloc data
data = (unsigned int*)new GLuint[((x * y)* 4 * sizeof(unsigned int))];
if(data == NULL)
return -1;
// get free ID for texure pointer
TID = free_TID.top();
free_TID.pop();
// create texture
glGenTextures(1, &textures[TID]);
glBindTexture(GL_TEXTURE_2D, textures[TID]);
glTexImage2D(GL_TEXTURE_2D, 0, 4, x, y, 0,
GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
delete [] data;
return TID;
}
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0);