PDA

View Full Version : TBGL Materials, new feature from BETA



Petr Schreiber
14-12-2009, 00:28
For all who keep an eye on ThinBASIC BETA,

here is demonstration working with latest version - basic materials.
Unlike classic OpenGL, you can define them as separate items, and bind them when necessary.
Still, this model will work on any OpenGL enabled card.

I leave you space for some experiments, you are free to define different ambient color from diffuse to achieve some interesting effects. Specularity is also thing which was previously possible only using gl functions, now you have it out of the box.



'
' Little example on materials
' Petr Schreiber, 2009
'

#MINVERSION 1.7.10.0

Uses "TBGL"

Begin Const
' -- Scene IDs
%sScene = 1

' -- Entity IDs
%eCamera = 1
%eLight
%eRed
%eGreen
%eBlue
End Const

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

' -- Create and show window
hWnd = TBGL_CreateWindowEx("3 different materials - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

' -- High quality primitives
TBGL_SetPrimitiveQuality 100

' -- 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, 5, 5, 5)
TBGL_EntitySetTargetPos(%sScene, %eCamera, 0, 0, 0)

' -- Create point light
TBGL_EntityCreateLight(%sScene, %eLight)
TBGL_EntitySetPos(%sScene, %eLight, 0, 10, 0)
TBGL_EntitySetSpecular(%sScene, %eLight, 255, 255, 255)

' -- Define some materials
Global mRed As Long = tbgl_NewMaterial
TBGL_SetMaterialDiffuse(mRed, 255, 0, 0)
TBGL_SetMaterialAmbient(mRed, 255, 0, 0)
TBGL_SetMaterialSpecular(mRed, 255, 255, 255)
TBGL_SetMaterialSpecularExponent(mRed, 8)

Global mGreen As Long = tbgl_NewMaterial
TBGL_SetMaterialDiffuse(mGreen, 0, 255, 0)
TBGL_SetMaterialAmbient(mGreen, 0, 255, 0)
TBGL_SetMaterialSpecular(mGreen, 255, 255, 255)
TBGL_SetMaterialSpecularExponent(mGreen, 16)

Global mBlue As Long = TBGL_NewMaterial
TBGL_SetMaterialDiffuse(mBlue, 0, 0, 255)
TBGL_SetMaterialAmbient(mBlue, 0, 0, 255)
TBGL_SetMaterialSpecular(mBlue, 255, 255, 255)
TBGL_SetMaterialSpecularExponent(mBlue, 128)

' -- Create something to look at
TBGL_EntityCreateFuncSlot(%sScene, %eRed, 0, "Sphere_Render")
TBGL_EntitySetPos(%sScene, %eRed,-2, 0, 0)

TBGL_EntitySetUserData(%sScene, %eRed, mRed)

TBGL_EntityCreateFuncSlot(%sScene, %eGreen, 0, "Sphere_Render")
TBGL_EntitySetPos(%sScene, %eGreen, 0, 0, 0)

TBGL_EntitySetUserData(%sScene, %eGreen, mGreen)

TBGL_EntityCreateFuncSlot(%sScene, %eBlue, 0, "Sphere_Render")
TBGL_EntitySetPos(%sScene, %eBlue, 2, 0, 0)

TBGL_EntitySetUserData(%sScene, %eBlue, mBlue)

' -- Resets status of all keys
TBGL_ResetKeyState()

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

TBGL_EntityTurn(%sScene, %eRed , 10/FrameRate, 0, 0)
TBGL_EntityTurn(%sScene, %eGreen, 10/FrameRate, 10/FrameRate, 10/FrameRate)
TBGL_EntityTurn(%sScene, %eBlue , 0, 0, 10/FrameRate)
TBGL_ClearFrame

TBGL_SceneRender(%sScene)

TBGL_DrawFrame

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

Wend
TBGL_DeleteMaterial(mGreen)
TBGL_DestroyWindow
End Function

Sub Sphere_Render()

Dim element As TBGL_tEntityIdentifier At TBGL_CallingEntity
Dim material As Long At TBGL_EntityGetUserDataPointer(element.scene, element.entity)

tbgl_PushMaterial(material)
TBGL_Torus 0.2, 0.8
TBGL_PopMaterial

End Sub

ErosOlmi
14-12-2009, 10:54
Incredible how few lines of code can create a quite complex real-time rendering.
Great.

Michael Hartlef
14-12-2009, 13:22
Hi Petr,

great addition to TBGL. Is it planed to include these material definitions into the M15 file format too?

Petr Schreiber
14-12-2009, 18:31
Thanks guys!

Mike, I will see, good idea.
I am thinking about extending the TBGL materials even more beyond OpenGL classic scheme, by adding texture, environment mapping mode and so on.

Current M15 handling functions provide great possibilities of custom mods using dedicated functions, which also makes the internal rendering little bit complicated.

I am thinking about "just load and use" format, with some strict rules (triangles only), no modifications on run time but wider feature possibilities, smarter internal format.

I think this format could finaly solve the problematic question of bones.

I am open to any discussion on this topic :)

kryton9
14-12-2009, 20:58
Wow this is a big step Petr. In fact, I controlled myself from requesting such a feature recently to not put any extra burdens, so this really surprised me!

In trying out all the major engines out there the last couple of years... I would say having a material file is really a great way to go.
I think the solution that many adapt is a good one for a reason:

Model Files
Material Files
and Scene Files

Keeping them separate allows for great flexibility.

I think leaving m15 as is also a good idea. So for someone just starting out, you have a nice model format that also has basic material abilities built in.
We have seen how well work from Blender with Mike's great exporter works so well.
This simple and almost seamless workflow is great for many projects.

But having the new material files and later scene files will be a great way to make the step into more adventurous areas.
Thanks for this new addition, as you can tell I think it is a very huge step foward as with many of the recent new additions.