Real Software Forums
http://forums.realsoftware.com/

OpenGL and Texture Transparency
http://forums.realsoftware.com/viewtopic.php?f=4&t=32269
Page 1 of 2

Author:  ss2cire [ Sun Jan 31, 2010 6:53 am ]
Post subject:  OpenGL and Texture Transparency

Hi all,

I have the fllowing code:
Sub GCSprite.Constructor()
location = GCMakePoint(50, 50)
size = GCMakeSize(32,32)

texture = new OpenGLTexture()
//hard coded for testing purposes
dim image As picture = getFolderItem("Data").child("Sprites").child("Player").child("Falling.png").openAsPicture()

firstFrame = New Picture(32,32,32)

dim ffg as Graphics = firstFrame.graphics
dim ffmg as Graphics = firstFrame.mask.graphics

ffg.drawpicture image, 0, 0, 32, 32, 0, 0, size.width, size.height
ffmg.drawpicture image.mask, 0, 0, 32, 32, 0, 0, size.width, size.height

firstFrame.transparent = 1
call texture.LoadAsPicture(firstFrame)
End Sub


and im using the OpenGLTexture class from <insert name here>

and using the LoadAsPicture method as seen here:

Function LoadAsPicture(Image As Picture) As Boolean
' Disabling items to speed up loops
#IF ACCESS_DISABLE_OPTIONS
#Pragma BackgroundTasks False
#Pragma BoundsChecking False
#Pragma NilObjectChecking False
#Pragma StackOverflowChecking False
#ENDIF

' Local properties
Dim x, y, count, image_width, image_height As Integer
Dim surface, mask As RGBSurface
Dim pixel, mpixel As Color

' If we received a bad picture
If Image = Nil Or Image.RGBSurface = Nil then Return False

' Setting Dimentions
ImageWidth = Image.Width
ImageHeight = Image.Height

' Creating our Image Data memory block
ImageData = NewMemoryBlock( ( ImageWidth * ImageHeight * BytesPerPixel ) + BytesPerPixel )

' Storing local to speed things up
image_width = ImageWidth - 1
image_height = ImageHeight - 1

' Storing our RGB surface
surface = Image.RGBSurface()
mask = Image.Mask.RGBSurface()//i added this myself for debugging

' Setting our greyscale color(s?)
If BytesPerPixel = 1 Then
//code not needed as i never load a 1 pixel per byte image
//code is also removed for brevity.
' Setting our RGB colors
ElseIf BytesPerPixel = 3 Then
//im loading a picture with an alpha mask so this code is also irrelevant
' Setting our RGBA Colors
Elseif BytesPerPixel = 4 Then
Format = opengl.GL_RGBA
Dim value as Integer
' Setting our colors
For y = image_height DownTo 0
For x = 0 To image_width

' Storing our pixel
pixel = surface.Pixel( x, y )
mpixel = mask.Pixel( x, y )
' Storing our color
ImageData.Byte( count ) = pixel.Red
ImageData.Byte( count + 1 ) = pixel.Green
ImageData.Byte( count + 2 ) = pixel.Blue
'Value = mpixel.value * 255
'If Value <> 255 then break
ImageData.Byte( count + 3 ) = mpixel.value * 255

' Increasing our position in the memory block
count = count + 4
Next
Next
End If

' Everything went OK
Return True
End Function



with that code i get the following image (bottom left part of the image is the one im talking about):
Image

now I'm curious to know why that is? in the debugger the image and mask seems to get copied just
fine from the source image...

also if it's relevant, if I load the image with GL_RGB and ignore the mask, I get the image fine, but no
mask... which is not what I'm going for...

any insights would be greatly appreciated thanks in advance and regards.

P.S.
Some of the code is hard coded values (such as the image to load for testing purposes)

Author:  Agent [ Sun Jan 31, 2010 11:16 am ]
Post subject:  Re: OpenGL and Texture Transparency

Try inverting the mask.

Author:  Blake [ Sun Jan 31, 2010 1:42 pm ]
Post subject:  Re: OpenGL and Texture Transparency

Do you have GL_BLEND enabled?

Author:  ss2cire [ Sun Jan 31, 2010 4:50 pm ]
Post subject:  Re: OpenGL and Texture Transparency

Yeah, I've tried doing:

Sub Draw()
OpenGL.glPushMatrix()
OpenGL.glTranslatef(location.x, location.y, 0)

OpenGL.glEnable( OpenGL.GL_TEXTURE_2D )
OpenGL.glEnable(OpenGL.GL_ALPHA_TEST)
OpenGL.glEnable(OpenGL.GL_BLEND)
Texture.BindTexture()

OpenGL.glColor3f(1.0, 1.0, 1.0)
OpenGL.glBegin( OpenGL.GL_QUADS )
OpenGL.glTexCoord2f( 0.0, 0.0 )
OpenGL.glVertex2i( 0, 0 ) ' Bottom Left
OpenGL.glTexCoord2f( 0.0, 1.0 )
OpenGL.glVertex2i( 0, size.height ) ' Top left
OpenGL.glTexCoord2f( 1.0, 1.0 )
OpenGL.glVertex2i( size.width, size.height ) ' Top Right
OpenGL.glTexCoord2f( 1.0, 0.0 )
OpenGL.glVertex2i( size.width, 0 ) ' Bottom Right
OpenGL.glEnd()

OpenGL.glPopMatrix()
End Sub


but the result is exactly the same... Unless I'm missing something?

Author:  Blake [ Sun Jan 31, 2010 5:02 pm ]
Post subject:  Re: OpenGL and Texture Transparency

Try adding OpenGL.glBlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA)

Author:  ss2cire [ Sun Jan 31, 2010 5:09 pm ]
Post subject:  Re: OpenGL and Texture Transparency

Blake wrote:
Try adding OpenGL.glBlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA)

That would be to my Open even of the OpenGLSurface or to my sprite draw code?

Author:  Blake [ Sun Jan 31, 2010 5:54 pm ]
Post subject:  Re: OpenGL and Texture Transparency

I would put it in the open event.

Author:  ss2cire [ Sun Jan 31, 2010 6:16 pm ]
Post subject:  Re: OpenGL and Texture Transparency

Ok, I did that, and I have the same result :(

Author:  Blake [ Sun Jan 31, 2010 7:18 pm ]
Post subject:  Re: OpenGL and Texture Transparency

wait...with the OpenGLSurface you have to make sure you have 32 color/depth bits instead of the default 24.

Author:  ss2cire [ Sun Jan 31, 2010 8:43 pm ]
Post subject:  Re: OpenGL and Texture Transparency

Ok... I changed both ColorBits and DepthBits to 32 and I still get the same result :(

Author:  Blake [ Sun Jan 31, 2010 8:47 pm ]
Post subject:  Re: OpenGL and Texture Transparency

hmm... I've never used GL_ALPHA_TEST so maybe don't enable it and see what happens?

Author:  ss2cire [ Sun Jan 31, 2010 8:49 pm ]
Post subject:  Re: OpenGL and Texture Transparency

Ok this is weird... I set the values to 32 (Color and Depth) and I get the same results and then I even set them each to 0 and still get the same results.

I just commented out ALPHA_TEST and same results...

Author:  ss2cire [ Sun Jan 31, 2010 9:01 pm ]
Post subject:  Re: OpenGL and Texture Transparency

I could post the project for you to take a look at if you don't mind, would the work?

Author:  Blake [ Sun Jan 31, 2010 9:13 pm ]
Post subject:  Re: OpenGL and Texture Transparency

yea ill take a look

Author:  ss2cire [ Sun Jan 31, 2010 9:25 pm ]
Post subject:  Re: OpenGL and Texture Transparency

OK, here's the project and everything I have so far:
OpenGL Project (44.4k)

Page 1 of 2 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/