Xcelled194's picture

Problem rendering transparency with VB.NET/ OpenTK/TexLib?

Hi all, My friend and I need to create a game for Visual Basic class. We thought we'd reinvent the old "Break my computer" game for Windows - you know, where you choose a hammer and click the screen and a virtual hammer puts cracks in the screen?

We're using VB.net (Visual Studio 2008) and OpenTK with TexLib. It's all going fine, except, several of our images (such as a bullet hole) will not render properly. We just used GIMP to whip up a very quick PNG, its a green circle surrounded by transparency, but when it displays on -screen, it is a solid square. We have confirmed that VB is loading the image properly, but for some reason OpenTK renders it a solid green square (See attached images).

Heres the crucial parts of our code:

    Protected Overrides Sub OnRenderFrame(ByVal e As FrameEventArgs)
        GL.Clear(ClearBufferMask.ColorBufferBit)
        GL.MatrixMode(MatrixMode.Modelview)
        GL.LoadIdentity()
        GL.Ortho(0, Me.Width, Me.Height, 0, -1, 1)
        If (Not texturingFails) Then 'draw the screen texture on a rectangle, using hardware acceleration
            GL.BindTexture(TextureTarget.Texture2D, screenTexture)
            GL.Begin(BeginMode.Quads)
            GL.TexCoord2(0, 0) : GL.Vertex2(0, 0) 'upper left
            GL.TexCoord2(1, 0) : GL.Vertex2(Me.Width, 0) 'upper right
            GL.TexCoord2(1, 1) : GL.Vertex2(Me.Width, Me.Height) 'bottom right
            GL.TexCoord2(0, 1) : GL.Vertex2(0, Me.Height) 'bottom left
            GL.End()
            Dim leftX As Double
            Dim rightX As Double
            Dim topY As Double
            Dim bottomY As Double
 
            For Each bullet As System.Drawing.Point In bulletHoles            
                GL.BindTexture(TextureTarget.Texture2D, bulletTexture)
                GL.Begin(BeginMode.Quads)
                'The range of coordinates is from 0.0 to 1.0
                'The locations can be calculated using (X position / Width) or (Y position / Height)
                leftX = (bullet.X - (bulletHole.Width / 2))
                rightX = (bullet.X + (bulletHole.Width / 2))
                topY = (bullet.Y - (bulletHole.Height / 2))
                bottomY = (bullet.Y + (bulletHole.Height / 2))      
                GL.TexCoord2((leftX / Me.Width), (topY / Me.Height)) : GL.Vertex2(leftX, topY) 'upper left
                GL.TexCoord2((rightX / Me.Width), (topY / Me.Height)) : GL.Vertex2(rightX, topY) 'upper right
                GL.TexCoord2((rightX / Me.Width), (bottomY / Me.Height)) : GL.Vertex2(rightX, bottomY) 'bottom right
                GL.TexCoord2((leftX / Me.Width), (bottomY / Me.Height)) : GL.Vertex2(leftX, bottomY) 'bottom left
                GL.End()
            Next bullet
            Dim glerror1 As Integer = GL.GetError()
            If (glerror1 <> 0) Then 'Handles errors with OpenGL
            MsgBox("OpenGL Error Occured: " & glerror1)
            End If
        Else
            MsgBox("An error occured when texturing the objects in the game. The game will now close.")
            Me.Close()
        End If
        Me.SwapBuffers() 'DO NOT EVER COMMENT THIS OUT. EVER. DON'T DO IT. I WARNED YOU.
End Sub
    Private Function CreateFilterlessTextureFromBitmap(ByVal bitmap As Bitmap, ByVal alphaTransparency As Boolean) As Integer
            Dim data As Imaging.BitmapData = bitmap.LockBits(New Rectangle(0, 0, bitmap.Width, bitmap.Height), _
            Imaging.ImageLockMode.[ReadOnly], Imaging.PixelFormat.Format32bppArgb)
        Dim tex As Integer = TexUtil.GiveMeATexture()
        'The following line forces OpenGL to use the BEST compression algorithm available, regardless of the speed trade-off
        GL.Hint(HintTarget.TextureCompressionHint, HintMode.Nicest)
        GL.BindTexture(TextureTarget.Texture2D, tex)
        'GL_COMPRESSED_RGB_S3TC_DXT1 is the best choice when the alpha channel is not necessary
        If (Not alphaTransparency) Then
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.CompressedRgbS3tcDxt1Ext, data.Width, data.Height, 0, _
         PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0)
        Else
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, _
         PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0)
        End If
                            bitmap.UnlockBits(data)
        'Instead of using TexLib's setParameters, we want to override those with our own parameters.
        'By default, TexLib uses "Linear" filtering. We want to use "Nearest" filtering instead.
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, CInt(TextureMinFilter.Nearest))
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, CInt(TextureMagFilter.Nearest))
        Return tex
    End Function

We've spent days searching around the net, nothing has worked.... Does anyone see anything wrong?

Inline Images

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Xcelled194's picture

Thanks, but I'm not sure I quite understand. We've tried using

GL.Enable(EnableCap.Blend)

before, but sadly it had no effect on the final. Is there something else I'm missing?

c2woody's picture

Clear the depth buffer, when drawing the quad disable depth testing, enable texturing, enable blending, set a blending function, disable lighting, set white as drawing color. So yes, you may have missed something, but that's hard to tell because the snippet you posted is incomplete.

Xcelled194's picture

I tried everything you said, the problem has not resolved. So lets approach this a different way: Can someone write sample VB or C# code that will load a semi transparent image to a texture and then draw it on screen? You can use my image in the first post. Cause I could then compare/contrast the code, and hopefully see whats wrong....

c2woody's picture

Post your full code that reproduces the problem.

Xcelled194's picture

Well, we figured it out. It was a stupid mistake.

                   GL.TexCoord2((leftX / Me.Width), (topY / Me.Height)) : GL.Vertex2(leftX, topY) 'upper left
                   GL.TexCoord2((rightX / Me.Width), (topY / Me.Height)) : GL.Vertex2(rightX, topY) 'upper right
                   GL.TexCoord2((rightX / Me.Width), (bottomY / Me.Height)) : GL.Vertex2(rightX, bottomY) 'bottom right
                   GL.TexCoord2((leftX / Me.Width), (bottomY / Me.Height)) : GL.Vertex2(leftX, bottomY) 'bottom left
Shoulda been:
                    GL.TexCoord2(0, 0) : GL.Vertex2(leftX, topY) 'upper left
                    GL.TexCoord2(1, 0) : GL.Vertex2(rightX, topY) 'upper right
                    GL.TexCoord2(1, 1) : GL.Vertex2(rightX, bottomY) 'bottom right
                    GL.TexCoord2(0, 1) : GL.Vertex2(leftX, bottomY) 'bottom left