🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Transparency problem?

Started by
0 comments, last by Richard Head 24 years, 1 month ago
I create a 128 x 128 grass texture, I then tile sub images over it using glTexSubImage2d, these sub images are loaded using the glpng library and have valid alpha value! but when I run my app the transparent areas are black! instead of showing the grass in the main texture! here is some of my code can you help. oid COpenGLView::InitGL() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f) ; glShadeModel(GL_FLAT); //glEnable(GL_DEPTH_TEST); int x,y; for(x =0; x < 128; x++) { for(y = 0; y < 128; y++) { Tile[x][y][0] = 255; Tile[x][y][1] = 0; Tile[x][y][2] = 0; Tile[x][y][3] = 255; } } //SubTile[0] = auxDIBImageLoad("E:\\Grass.bmp"); //SubTile[1] = auxDIBImageLoad("E:\\Wheat.bmp"); pngLoadRaw("E:\\Grass.png",&info[0]); pngLoadRaw("E:\\Marsh.png",&info[1]); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) ; glEnable(GL_TEXTURE_2D); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE,Tile) ; glTexSubImage2D(GL_TEXTURE_2D,0,0,0,64,64,GL_RGB,GL_UNSIGNED_BYTE,info[0].Data); glTexSubImage2D(GL_TEXTURE_2D,0,0,64,64,64,GL_RGB,GL_UNSIGNED_BYTE,info[0].Data); glTexSubImage2D(GL_TEXTURE_2D,0,64,0,64,64,GL_RGB,GL_UNSIGNED_BYTE,info[0].Data); glTexSubImage2D(GL_TEXTURE_2D,0,64,64,64,64,GL_RGB,GL_UNSIGNED_BYTE,info[0].Data); } void COpenGLView::DrawGL() { glClear(GL_COLOR_BUFFER_BIT / GL_DEPTH_BUFFER_BIT); //glColor3f(1.0,0.0,0.0); //glPixelStorei(GL_UNPACK_ALIGNMENT, 1) ; glEnable(GL_TEXTURE_2D); //glBindTexture(GL_TEXTURE_2D,texture[0]); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.5); //glEnable(GL_BLEND); //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glTexSubImage2D(GL_TEXTURE_2D,0,0,0,64,64,GL_RGBA,GL_UNSIGNED_BYTE,info[1].Data); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex2i(10,138); glTexCoord2f(0,1); glVertex2i(10,10); glTexCoord2f(1,1); glVertex2i(138,10); glTexCoord2f(1,0); glVertex2i(138,138); glEnd(); glDisable(GL_BLEND); glDisable(GL_ALPHA_TEST); //glBindTexture(GL_TEXTURE_2D, texture[1]); //glTexSubImage2D(GL_TEXTURE_2D,0,GL_RGBA,info.Width,info.Height,info.Width, info.Height, //GL_UNSIGNED_BYTE,info.Data ); glFlush(); glDisable(GL_TEXTURE_2D); }
Advertisement
Looks like you aren''t using the alpha channel for the PNG files (info[0] and info[1]) when you call glTexSubImage2D. You specify the parameter GL_RGB when you call these procedures in InitGL, this completely ingnores the alpha channel.

You would be MUCH better of if you used the procedures included with glPNG, use the pngBind function and save yourself a whole lot of trouble. This way you don''t have to worry about the texture generation, glTexSubImage2d, etc...

All you really need to do is load each png with a call to pngBind, store the texture coordinate for each file. Then using in the Draw do this (pseudocode):

bind your background texture
draw background polygon (glBegin(GL_QUADS), etc...)

enable GL_BLEND
disable GL_DEPTH_TEST
set blending to GL_SRC_ALPHA, GL_ONE (glBlendFunc)

bind transparent texture (glBindTexture)
draw polygon slightly in front of background texture
disable GL_BLEND
enable GL_DEPTH_TEST

These are the exact steps i took and it worked perfectly for me. Feel free to email or ICQ me to discuss this further.

Jason A.

---
I write code.
---I write code.DelphiGL (http://delphigl.cfxweb.net)

This topic is closed to new replies.

Advertisement