PDA

View Full Version : image apply to box cube



Floyd
05-12-2011, 13:42
hello thinbasic! how I can load an image to an cube and apply to this cube? I am totally newbie on tbgl. A vertex box with six different images and colors at surface how to realise? last question: I want to push four cubes with different velocity for rotating with start and stop animation ;) regards, floyd

zak
05-12-2011, 14:56
for the first question look at this subject:
multiple textures on the cube faces :
http://www.thinbasic.com/community/showthread.php?10460-multiple-textures-on-the-cube-faces
download the two examples there, also don't forget to study the TBGL
Lessons by Petr here: http://psch.thinbasic.com/old/tbgl_tut_b.html

Petr Schreiber
05-12-2011, 17:24
Floyd, welcome here!

Zak gave you the essential advice, I would just add that for further study, there are examples in:
/thinBasic/SampleScripts/TBGL/

divided into multiple categories. I would recommend to study the Basic category first (basics of shape definition, texturing, ...), and then have a look at those in EntityBased category. They demonstrate the entity approach, very well suitable for easy manipulation with both camera, light and geometric entities.

TBGL module is documented via Help file (just press F1 on TBGL keyword to get there), Articles (http://www.thinbasic.com/community/content.php?16-tbgl) and dedicated website (http://psch.thinbasic.com/).


Petr

Floyd
09-12-2011, 11:38
thanks zak, petr.

I took some example and fiddled together for a first result with cubes and textures.


'
' The entity skeleton for TBGL
' Suitable for developing editor apps
' , started on 12-08-2011
'
' paul.w.floyd first attempt

Uses "UI", "TBGL"

' -- ID numbers of controls
Begin ControlID
%lCanvas
%bClose

%myTimer
End ControlID

' -- Global equates
Begin Const
%MAIN_WIDTH = 640
%MAIN_HEIGHT = 480

%timeOut = 20 ' -- Determines graphics refresh rate in milliseconds
End Const

' -- Scene-Entity related ids
Begin Const
' -- Scene IDs
%sScene = 1

' -- Entity IDs
%eCamera = 1
%eLight
%eBox
%bAnima
End Const


Function TBMain()

Local hDlg As DWord

Dialog New Pixels, 0, "Dialog with TBGL",-1,-1, %MAIN_WIDTH, %MAIN_HEIGHT, _
%WS_POPUP Or %WS_VISIBLE Or _
%WS_CLIPCHILDREN Or %WS_CAPTION Or _
%WS_SYSMENU Or %WS_MINIMIZEBOX Or %WS_MAXIMIZEBOX Or %WS_THICKFRAME, 0 To hDlg

' -- Place controls here
Control Add Label, hDlg, %lCanvas, "", 5, 5, %MAIN_WIDTH-10, %MAIN_HEIGHT-40
Control Set Color hDlg, %lCanvas, %BLACK, %BLACK
Control Set Resize hDlg, %lCanvas, 1, 1, 1, 1


Control Add Button, hDlg, %bClose, "Close", %MAIN_WIDTH-105, %MAIN_HEIGHT-30, 100, 25
Control Add Button, hDlg, %bAnima, "Anima", %MAIN_WIDTH-245, %MAIN_HEIGHT-30, 100, 25
Control Set Resize hDlg, %bAnima, 0, 1, 0, 1
Control Set Resize hDlg, %bClose, 0, 1, 0, 1

Dialog Set Minsize hDlg, 320, 230
Dialog Show Modal hDlg, Call dlgCallback

End Function

CallBack Function dlgCallback()
Static hCtrl As DWord

Select Case CBMSG

Case %WM_INITDIALOG
Dialog Set Timer CBHNDL, %myTimer, %timeOut, %NULL
Control Handle CBHNDL, %lCanvas To hCtrl

' -- Init OpenGL
TBGL_BindCanvas(hCtrl)

' -- Create scene
TBGL_SceneCreate(%sScene)

' -- Create basic entities
' -- Create camera to look from 5, 5, 5 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, 15, 10, 5)

' -- Create something to look at
TBGL_EntityCreateBox(%sScene, %eBox, 0, 1, 1, 1, 0, 255, 255, 255)
TBGL_EntitySetPos(%sScene, %eBox, 0, 0, 0)

Case %WM_SIZE, %WM_SIZING
TBGL_UpdateCanvasProportions(hCtrl)
RenderMyImage(hCtrl)

Case %WM_TIMER
RenderMyImage(hCtrl)

Case %WM_CLOSE
TBGL_ReleaseCanvas(hCtrl)
Dialog Kill Timer CBHNDL, %myTimer

Case %WM_COMMAND
Select Case CBCTL

Case %bClose
If CBCTLMSG = %BN_CLICKED Then Dialog End CBHNDL

End Select

End Select
End Function

Function RenderMyImage( hCtrl As DWord )
Static FrameRate As Double

' Load Texture, Apply it.
TBGL_LoadTexture APP_SourcePath + "asterix2.bmp" , 1, %TBGL_TEX_MIPMAP
TBGL_LoadTexture APP_SourcePath + "illus.bmp", 2, %TBGL_TEX_MIPMAP
TBGL_LoadTexture APP_SourcePath + "simpsons.bmp" , 3, %TBGL_TEX_MIPMAP
TBGL_LoadTexture APP_SourcePath + "robotguy.bmp" , 4, %TBGL_TEX_MIPMAP
TBGL_LoadTexture APP_SourcePath + "schultz.bmp" , 5, %TBGL_TEX_MIPMAP
TBGL_LoadTexture APP_SourcePath + "charly.bmp" , 6, %TBGL_TEX_MIPMAP

If TBGL_CanvasBound(hCtrl) Then

FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame

TBGL_SceneRender(%sScene)
CubesTextures()

TBGL_DrawFrame
TBGL_EntityTurn(%sScene, %eBOx, 0, 20/FrameRate, 0)

End If
End Function

Sub CubesTextures()
Dim xRot, yRot, zRot As Single
Dim frameRate As Double

FrameRate = TBGL_GetFrameRate

TBGL_Translate 0.0, 0.0, -5.0 ' Move Into Screen -5 Units
TBGL_Rotate Xrot, 1.0, 0.0, 0.0 ' Rotate on X-Axis
TBGL_Rotate Yrot, 0.0, 1.0, 0.0 ' Rotate on Y-Axis
TBGL_Rotate Zrot, 0.0, 0.0, 1.0 ' Rotate on Z-Axis

TBGL_UseTexturing %TRUE

TBGL_Color 200,155,0
TBGL_BindTexture 1
TBGL_BeginPoly %GL_QUADS
' Bottom Face
TBGL_TexCoord2D 1.0, 1.0 : TBGL_Vertex -1.5, -1.5, -1.5 ' Top Right of Texture and Quad
TBGL_TexCoord2D 0.0, 1.0 : TBGL_Vertex 1.5, -1.5, -1.5 ' Top Left
TBGL_TexCoord2D 0.0, 0.0 : TBGL_Vertex 1.5, -1.5, 1.5 ' Bottom Left
TBGL_TexCoord2D 1.0, 0.0 : TBGL_Vertex -1.5, -1.5, 1.5 ' Bottom Right
TBGL_EndPoly

TBGL_BindTexture 2
TBGL_BeginPoly %GL_QUADS

' Right Face
TBGL_TexCoord2D 1.0, 0.0 : TBGL_Vertex 1.5, -1.5, -1.5 ' Bottom Right of Texture and Quad
TBGL_TexCoord2D 1.0, 1.0 : TBGL_Vertex 1.5, 1.5, -1.5 ' Top Right
TBGL_TexCoord2D 0.0, 1.0 : TBGL_Vertex 1.5, 1.5, 1.5 ' Top Left
TBGL_TexCoord2D 0.0, 0.0 : TBGL_Vertex 1.5, -1.5, 1.5 ' Bottom Left
TBGL_EndPoly

TBGL_BindTexture 3
TBGL_BeginPoly %GL_QUADS ' Drawing using Quads
' Front Face
TBGL_TexCoord2D 0.0, 0.0 : TBGL_Vertex -1.5, -1.5, 1.5 ' Bottom Left of Texture and Quad
TBGL_TexCoord2D 1.0, 0.0 : TBGL_Vertex 1.5, -1.5, 1.5 ' Bottom Right
TBGL_TexCoord2D 1.0, 1.0 : TBGL_Vertex 1.5, 1.5, 1.5 ' Top Right
TBGL_TexCoord2D 0.0, 1.0 : TBGL_Vertex -1.5, 1.5, 1.5 ' Top Left
TBGL_EndPoly

TBGL_BindTexture 4
TBGL_BeginPoly %GL_QUADS ' Drawing using Quads

' Top Face
TBGL_TexCoord2D 0.0, 1.0 : TBGL_Vertex -1.5, 1.5, -1.5 ' Top Left of Texture and Quad
TBGL_TexCoord2D 0.0, 0.0 : TBGL_Vertex -1.5, 1.5, 1.5 ' Bottom Left
TBGL_TexCoord2D 1.0, 0.0 : TBGL_Vertex 1.5, 1.5, 1.5 ' Bottom Right
TBGL_TexCoord2D 1.0, 1.0 : TBGL_Vertex 1.5, 1.5, -1.5 ' Top Right
TBGL_EndPoly

TBGL_BindTexture 5
TBGL_BeginPoly %GL_QUADS
' Back Face
TBGL_TexCoord2D 1.0, 0.0 : TBGL_Vertex -1.5, -1.5, -1.5 ' Bottom Right of Texture and Quad
TBGL_TexCoord2D 1.0, 1.0 : TBGL_Vertex -1.5, 1.5, -1.5 ' Top Right
TBGL_TexCoord2D 0.0, 1.0 : TBGL_Vertex 1.5, 1.5, -1.5 ' Top Left
TBGL_TexCoord2D 0.0, 0.0 : TBGL_Vertex 1.5, -1.5, -1.5 ' Bottom Left
TBGL_EndPoly

TBGL_BindTexture 6
TBGL_BeginPoly %GL_QUADS
' Left Face
TBGL_TexCoord2D 0.0, 0.0 : TBGL_Vertex -1.5, -1.5, -1.5 ' Bottom Left of Texture and Quad
TBGL_TexCoord2D 1.0, 0.0 : TBGL_Vertex -1.5, -1.5, 1.5 ' Bottom Right
TBGL_TexCoord2D 1.0, 1.0 : TBGL_Vertex -1.5, 1.5, 1.5 ' Top Right
TBGL_TexCoord2D 0.0, 1.0 : TBGL_Vertex -1.5, 1.5, -1.5 ' Top Left
TBGL_EndPoly

Xrot += ( 54.0 / frameRate ) ' X-Axis Rotation
Yrot += ( 26.0 / frameRate ) ' Y-Axis Rotation
Zrot += ( 72.0 / frameRate ) ' Z-Axis Rotation

End Sub

my cube was displayed well, but too dark, different colors are missing. and I wanted to self rotate them, dont work with my cube sub, I suggest the entity had no id number for that. help to fix the problem I am glad to see. there was a lot to study. my mind is rotating in a tbgl mess. but the tbgl tool is very exciting, thank you guys!

floyd

how to get an image for my profile? (stratocaster)

ErosOlmi
09-12-2011, 12:13
how to get an image for my profile? (stratocaster)


Use sub-menu "Forum Actions/Edit profile"

Ciao
Eros

Petr Schreiber
09-12-2011, 12:48
Hi Floyd,

I think you did your first attempt great, just few suggestions for improvement:

loading the textures in RenderMyImage function will make the code load textures each frame. This is possible, but not necessary, it is enough to load the textures after binding the canvas in WM_InitDialog
the light was wrong, because your cube did not have the normals specified. Normals are vectors, which tell TBGL how the light will be reflected from the surface
mixing entity system and classic rendering the way you did is possible, but more systematic approach is to create custom entity based on actor template (TBGL/actor_template.taguit)
using textures 126x126 will work only on modern GPUs, I resized them to 128x128 (which is power of two dimension), which will work everywhere


I prepaired modified version for you with all the points corrected. The code for cube with multiple textures moved to file actor_MultiCube.tBasicU. This way you can manipulate your custom entity via TBGL_Entity* commands, which saves you some work. The modified rendering code (added normals) can be seen in actor_MultiCube.tBasicU file, function is named MultiCube_Render.

For better understanding, I would recommend to do some reading on custom entities here:
User defined entities (http://www.thinbasic.com/community/content.php?19-tbgl-user-defined-entities)


Petr

Floyd
13-01-2012, 18:38
I've overlooked this example, sorry, petr for late reply. thanks for expanded help and new entity examples I have to study yet for next time.

my new question here how I can stop a moving box (running from left side to the center of scene) by a command or with timer functions? I wanted to have 9 boxes side by side in one frontier line. entity push I know already but to stop a cube without having other stop or callback buttons that's existing a special command? any suggestion? I wanted program with stopping the cube for reaching a certain angle than box will stop. do you have better idea? thanks for help, floyd (paul)