PDA

View Full Version : FreeImage + TBGL



Petr Schreiber
13-11-2009, 20:44
Hi,

as Mike started some work on FreeImage, I put here the example of usage of FreeImage as texture loader.
Please note the output will have Red and Blue components swapped (TBGL_MakeTexture does not allow BGR input).

I will work on enhancing the TBGL_MakeTexture to give us the wanted results.


Petr

Petr Schreiber
13-11-2009, 21:23
I think nobody likes waiting,

so here is the updated TBGL, which allows using %TBGL_DATA_BGR and %TBGL_DATA_BGRA.
You can see the image has correct colors now.

Lionheart008
13-11-2009, 23:03
great! thank you both for this new bgr feature.

this example shows my test (of the first bgr example by petr).
you can move the picture cubes :)


'
' Second TBGL FreeImage sample
'

Uses "TBGL"

Begin Const
' -- Scene IDs
%sScene = 1

' -- Entity IDs
%eCamera = 1
%eLight
%eBox
%eBox2
%eBox3
%eBox4
' -- Texture IDs
%tTexture1 = 1
%tTexture2 = 2
End Const

Function TBMAIN()
Local hWnd As DWord
Local FrameRate As Double

FreeImage_Init()

' -- Create and show window
hWnd = TBGL_CreateWindowEx("Nice! TBGL does now BGR too! - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX or %TBGL_WS_MinimizeBOX or %TBGL_WS_MaximizeBOX)
TBGL_ShowWindow

msgbox 0, "uses up, down, left, right for cubes", %MB_Iconinformation, "tbgl BGR testing"

' -- Create scene
TBGL_SceneCreate(%sScene)

' -- Create basic entities
' -- Create camera to look from 15, 15, 15 to 0, 0, 0
TBGL_EntityCreateCamera(%sScene, %eCamera)
TBGL_EntitySetPos(%sScene, %eCamera, 7, 7, 7)
TBGL_EntitySetTargetPos(%sScene, %eCamera, 0, 0, 0)

' -- Create point light
TBGL_EntityCreateLight(%sScene, %eLight)
TBGL_EntitySetPos(%sScene, %eLight, 15, 10, 5)

' -- Create something to look at
FreeImage_LoadTexture(APP_SourcePath+"Textures\the-incredibles2.jpg", %tTexture1, %TBGL_TEX_LINEAR)
TBGL_EntityCreateBox(%sScene, %eBox, 0, 2, 2, 2, 0, 255, 0, 255)
TBGL_EntitySetPos(%sScene, %eBox, 0, 2, 0)
TBGL_EntitySetTexture(%sScene, %eBox, %tTexture1)

FreeImage_LoadTexture(APP_SourcePath+"Textures\the-incredibles2.jpg", %tTexture1, %TBGL_TEX_LINEAR)
TBGL_EntityCreateBox(%sScene, %eBox2, 0, 2, 2, 2, 0, 255, 255, 0)
TBGL_EntitySetPos(%sScene, %eBox2, 3, 1, 2)
TBGL_EntitySetTexture(%sScene, %eBox2, %tTexture1)

FreeImage_LoadTexture(APP_SourcePath+"Textures\the-incredibles2.jpg", %tTexture1, %TBGL_TEX_LINEAR)
TBGL_EntityCreateBox(%sScene, %eBox3, 0, 2, 2, 2, 0, 0, 255, 255)
TBGL_EntitySetPos(%sScene, %eBox3, -5, 0, 1)
TBGL_EntitySetTexture(%sScene, %eBox3, %tTexture1)

FreeImage_LoadTexture(APP_SourcePath+"Textures\leoparden2.jpg", %tTexture2, %TBGL_TEX_LINEAR)
TBGL_EntityCreateBox(%sScene, %eBox4, 0, 10, 1, 10, 0, 255, 255, 155)
TBGL_EntitySetPos(%sScene, %eBox4, 0, -4, 0)
TBGL_EntitySetTexture(%sScene, %eBox4, %tTexture2)

' -- Resets status of all keys
TBGL_ResetKeyState()

' -- Main loop
While TBGL_IsWindow(hWnd)
FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame

TBGL_SceneRender(%sScene)

TBGL_DrawFrame

' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

If TBGL_GetWindowKeyState(hWnd, %VK_LEFT) Then
TBGL_EntityTurn(%sScene, %eBox, 0,-90/FrameRate, 0)
TBGL_EntityTurn(%sScene, %eBox, 0,0, -90/FrameRate)
TBGL_EntityTurn(%sScene, %eBox2, 0,0, -70/FrameRate)
TBGL_EntityTurn(%sScene, %eBox3, 0,0, -50/FrameRate)

ElseIf TBGL_GetWindowKeyState(hWnd, %VK_RIGHT) Then
TBGL_EntityTurn(%sScene, %eBox, 0, 90/FrameRate, 0)
TBGL_EntityTurn(%sScene, %eBox, 0, 0, 90/FrameRate)
TBGL_EntityTurn(%sScene, %eBox2, 0, 0, 70/FrameRate)
TBGL_EntityTurn(%sScene, %eBox3, 0, 0, 50/FrameRate)

ElseIf TBGL_GetWindowKeyState(hWnd, %VK_UP) Then
TBGL_EntityPush(%sScene, %eBox2, 0, 5/FrameRate, 0)
TBGL_EntityPush(%sScene, %eBox3, 0, 3/FrameRate, 0)
TBGL_EntityPush(%sScene, %eBox4, 0, 2/FrameRate, 0)

ElseIf TBGL_GetWindowKeyState(hWnd, %VK_DOWN) Then
TBGL_EntityPush(%sScene, %eBox2, 0, -5/FrameRate, 0)
TBGL_EntityPush(%sScene, %eBox3, 0, -3/FrameRate, 0)
TBGL_EntityPush(%sScene, %eBox4, 0, -2/FrameRate, 0)
End If

Wend

TBGL_DestroyWindow
End Function

Function FreeImage_Init()
Declare Function FreeImage_GetFileType Lib "FreeImage.dll" Alias "_FreeImage_GetFileType@8" (ByVal fnam As String, ByVal lSize As Long) As Long
Declare Function FreeImage_Load Lib "FreeImage.dll" Alias "_FreeImage_Load@12" (ByVal FREE_IMAGE_FORMAT As Long, ByVal fnam As ASCIIZ) As Long
Declare Function FreeImage_ConvertTo24Bits Lib "FreeImage.dll" Alias "_FreeImage_ConvertTo24Bits@4" (ByVal FIBITMAP As Long) As Long
Declare Function FreeImage_GetWidth Lib "FreeImage.dll" Alias "_FreeImage_GetWidth@4" (ByVal FIBITMAP As Long) As Long
Declare Function FreeImage_GetHeight Lib "FreeImage.dll" Alias "_FreeImage_GetHeight@4" (ByVal FIBITMAP As Long) As Long
Declare Function FreeImage_GetBits Lib "FreeImage.dll" Alias "_FreeImage_GetBits@4" (ByVal FIBITMAP As Long) As Long
Declare Sub FreeImage_Unload Lib "FreeImage.dll" Alias "_FreeImage_Unload@4" (ByVal FIBITMAP As Long)
End Function

Function FreeImage_LoadTexture(TextureFile As String, TextureIndex As Long, TextureFiltering As Long, Optional AnisotropicLevel As Long, TransparentColor As Long)

Local hBitmap As Long
Local iType As Long
Local width, height As Long

iType = FreeImage_GetFileType(TextureFile, 0)
hBitmap = FreeImage_Load(iType, TextureFile)
FreeImage_ConvertTo24Bits(hBitmap)

width = FreeImage_GetWidth(hBitmap)
height = FreeImage_GetHeight(hBitmap)

Dim sBuffer As String = PEEK$(FreeImage_GetBits(hBitmap), width*height*3)

TBGL_MakeTexture sBuffer, %TBGL_DATA_BGR, width, height, TextureIndex, TextureFiltering, AnisotropicLevel, TransparentColor

FreeImage_Unload(hBitmap)

End Function


by the way: how do you get these FreeImage Properties ?
for example:

Declare Function FreeImage_GetFileType Lib "FreeImage.dll" Alias "_FreeImage_GetFileType@8" (ByVal fnam As String, ByVal lSize As Long) As Long

best regards, thanks, looks very, very nice. frank

Petr Schreiber
13-11-2009, 23:13
Hi Frank,

the main thanks go to Michael Hartlef, I would not start this without his post :)

Very nice example, textures power-of-two ... nice!



by the way: how do you get these FreeImage Properties ?

You mean DECLAREs? Well, Mike did them (again :)). But basically it is adaptation of .h (C header) files, I guess.

Michael Hartlef
14-11-2009, 01:30
Thanks Petr for doing the job. :)
Yes that was my intention. I'm glad till TBGL will support it internally, we will have a little alternativ.