TGA 4 PyOpengl alpha channel problem
Stan
dragonscraft at yahoo.it
Sat Feb 22 12:21:28 EST 2003
Hi everyone!
I'm converting Nehe's tutorials in python with use of PyOpengl an
PyGame,it's easy loading images with pygame image.tostring metod, but
it doesen't support tga alpha channel. So using tutorial 24 i've made
a funciont to load TGA.
It loads images but agiain alpha channel doesn't work, I have to use
glBlendFunc(GL_SRC_ALPHA,GL_ONE) and glEnable(Blend) to make things
show in the right way, while in the C version they aren't needed.
I cant's figure out the problem....
The image is used to generate a font, the font code works i'm sure.
help needed plz.
This is my tga loader code:
class TextureImage(object):
def __init__(self):
self.imageData = [] # Image Data (Up To 32 Bits)
self.bpp = 0 # Image Color Depth In Bits Per Pixel.
self.width = 0 # Image Width
self.height = 0 # Image Height
self.texID = 0 # Texture ID Used To Select A Texture
def load_textures( texture, filename):
h = h1 = h2 = () # Tuples to hold header data
ImgCMD = ImgCMD = ()# Tuples to hold image color map data if
present
bytesPerPixel = 0 # Holds Number Of Bytes Per Pixel Used In The
TGA File
imageSize = 0 # Used To Store The Image Size When Setting
Aside Ram
Ttype=GL_RGBA # Set The Default GL Mode To RBGA (32 BPP)
tgafile = open(filename, 'rb')
############################ TGA FILE HEADER
########################
h = struct.unpack('bbb', tgafile.read(3))
# ID Length
# Color Map Type
# Image Type :
# 0 No Image Data Included
# 1 Uncompressed, Color-mapped Image
# 2 Uncompressed, True-color Image
# 3 Uncompressed, Black-and-white Image
# 9 Run-length encoded, Color-mapped Image
# 10 Run-length encoded, True-color Image
#11 Run-length encoded, Black-and-white Image
h1= struct.unpack('hhb', tgafile.read(5))
# Color Map Specification :
# First Entry Index
# Color map Length
# Color map Entry Size
h2 = struct.unpack("hhhhbb", tgafile.read(10))
# Image Specification :
# X-origin of Image
# Y-origin of Image
# Image Width
# Image Height
# Pixel Depth
# Image Descriptor
######################### IMAGE/COLOR MAP DATA
#######################
if (h[0] != 0): # Is there an Image ID?
ImgCMD= struct.unpack('hhb', tgafile.read(h[0]))
if (h[1] != 0): # Is there Color Map Data?
ImgCMD1= struct.unpack('hhb', tgafile.read(h[0]))
texture.width = h2[2] # Determine The TGA
Width (highbyte*256+lowbyte)
texture.height = h2[3] # Determine The TGA
Height (highbyte*256+lowbyte)
texture.bpp = h2[4] # Grab The TGA's Bits Per Pixel (24 or 32)
bytesPerPixel = texture.bpp/8 # Divide By 8 To Get The Bytes Per
Pixel
imageSize = texture.width*texture.height*bytesPerPixel
# Calculate The Memory Required For The TGA Data
if (h[2]==0): # It has no image so close the file
tgafile.close()
return
elif (h[2]>=9): # It'rle encoded not yet supported ;)
tgafile.close()
return
if (texture.bpp==16): # If Color's depth is 16 we need a tweak
for i in xrange(0,imageSize,1):
pixels = tgafile.read(2) # Read image data in byte format
b = (pixels & 0x1f) << 3
g = ((pixels >> 5) & 0x1f) << 3
r = ((pixels >> 10) & 0x1f) << 3
# This essentially assigns the color to our array and swaps
the
# B and R values at the same time.
texture.imageData[i * 3 + 0] = r
texture.imageData[i * 3 + 1] = g
texture.imageData[i * 3 + 2] = b
else: # Clor depth is 24 or 32
texture.imageData = tgafile.read(imageSize)
tgafile.close() # done
######################## OPENGL BINDING
##############################
if(texture.bpp==24): # Was The TGA 24 Bits
Ttype=GL_RGB # If So Set The 'type' To GL_RGB
texture.texID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture.texID)
gluBuild2DMipmaps(GL_TEXTURE_2D, bytesPerPixel , texture.width ,
texture.height, Ttype, GL_UNSIGNED_BYTE, texture.imageData )
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteriGL_TEXTURE_2D ,GL_TEXTURE_MIN_FILTER
,GL_LINEAR_MIPMAP_NEAREST)
More information about the Python-list
mailing list