Hi everyone.
I use glBlendFunc() to apply some transparency to the background texture of my start menu.
I have to call a glutPostRedisplay() and I’ve noticed that at each redisplay() call the background lose some transparency (more redisplay less transparency till I lose all transparency effect).
Here is my code:
bool Background::initialize()
{
m_vertices.push_back(Vertex(-1.0f, -1.0f, -1.0f));
m_vertices.push_back(Vertex(1.0f, -1.0f, -1.0f));
m_vertices.push_back(Vertex(1.0f, 1.0f, -1.0f));
m_vertices.push_back(Vertex(-1.0f, 1.0f, -1.0f));
m_indices.push_back(0);
m_indices.push_back(1);
m_indices.push_back(2);
m_indices.push_back(3);
Color white = Color(1.0f,1.0f,1.0f,0.3f);
Color black = Color(0.0f,0.0f,0.0f,0.0f);
m_colors.push_back(white);
m_colors.push_back(white);
m_colors.push_back(white);
m_colors.push_back(white);
glGenBuffers(1, &m_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * m_vertices.size(), &m_vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &m_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * m_indices.size(), &m_indices[0], GL_STATIC_DRAW);
glGenBuffers(1, &m_colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4 * m_colors.size(), &m_colors[0], GL_STATIC_DRAW);
m_texCoord.push_back(texCoords(0.0,0.0));
m_texCoord.push_back(texCoords(1.0, 0.0));
m_texCoord.push_back(texCoords(1.0, 1.0));
m_texCoord.push_back(texCoords(0.0, 1.0));
// Texture
glGenBuffers(1, &m_texture);
glBindBuffer(GL_ARRAY_BUFFER, m_texture); //Bind the vertex buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * m_texCoord.size() * 2, &m_texCoord[0], GL_STATIC_DRAW);
if (!m_backTexture.load("textures/tron2.tga"))
{
std::cerr << "Could not load the grass texture" << std::endl;
return false;
}
glGenTextures(1, &m_backTexID);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_backTexID);
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, m_backTexture.getWidth(),
m_backTexture.getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE,
m_backTexture.getImageData());
return true;
}
void Background::render()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable( GL_TEXTURE_2D );
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
glColorPointer(4, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, m_texture);
glTexCoordPointer(2, GL_FLOAT, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer);
glMatrixMode (GL_MODELVIEW);
glPushMatrix ();
glLoadIdentity ();
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
glDrawElements(GL_QUADS, (GLsizei) m_indices.size(), GL_UNSIGNED_INT, 0);
glPopMatrix ();
glMatrixMode (GL_MODELVIEW);
glPopMatrix ();
glDepthMask(GL_TRUE);
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glDisable(GL_VERTEX_ARRAY);
glDisable(GL_COLOR_ARRAY);
glDisable(GL_TEXTURE_COORD_ARRAY);
}
the glutRedisplay() call:
void StartMenu::mouse(int btn, int state, int x, int y)
{
int posx, posy;
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
posx = x;
posy = wheight-y-1;
if( posx >= b1_pos_x && posx <= b1_posx_width && posy >= b1_pos_y && posy <= b1_posy_height ) {
std::cout << "pressed " << posx << " " << posy << std::endl;
pressed1 = true;
glutPostRedisplay();
}
else if( posx >= b2_pos_x && posx <= b2_posx_width && posy >= b2_pos_y && posy <= b2_posy_height ) {
pressed2 = true;
glutPostRedisplay();
}
else if( posx >= b3_pos_x && posx <= b3_posx_width && posy >= b3_pos_y && posy <= b3_posy_height ) {
pressed3 = true;
glutPostRedisplay();
}
//g_game = &scene;
//scene->render();
} else {
pressed1 = false;
pressed2 = false;
pressed3 = false;
glutPostRedisplay();
}
}
How can I solfe my problem? Thank you