PDA

View Full Version : multiple textures on the cube faces



zak
28-03-2010, 09:46
Hi
here is my version of casting multiple textures over the cube faces.
it is based on the nehe lesson 6 here:
http://community.thinbasic.com/index.php?topic=678.0
if there is another solution please tell me
i have displayed nehe pic twice, rabbit pic twice, tbgl pic twice, it is working but not sure if this is the only way
included the modified file with the pictures

P.S i found another way to do this in blitzbasic, i have to study it:
http://www.blitzbasic.com/Community/posts.php?topic=43543



' NeHe Lesson 06

uses "UI" ' MessageBox Functions
Uses "TBGL" ' ThinBASIC OpenGL Library

' Create MessageBox
dim fullScreen as long
fullScreen = msgbox(0, "Would you like to Run in Fullscreen Mode?", %MB_YESNO or %MB_ICONINFORMATION, "Start FullScreen?")

select case fullScreen

' Create FullScreen Display, Return Handle.
case %IDYES
dim hWnd as dword
hWnd = tbgl_createwindowex("", 1024, 768, 32, 1)

' Create Windowed Display, Return Handle.
case %IDNO
dim hWnd as dword
hWnd = tbgl_createwindowex("NeHe Lesson 06 - Press 'Esc' to Quit", 640, 480, 16, 0)

end select

' Declare Variables here

Dim rquad as Single ' Cube Rotation

dim xRot, yRot, zRot as single ' X, Y & Z-Axis Rotation.

dim frameRate as integer ' Timing Variable

TBGL_ShowWindow ' Show Display

TBGL_GetAsyncKeyState(-1) ' Reset all Keys

' Load Texture, Apply it.
TBGL_LoadTexture APP_SourcePath + "NeHe.bmp", 1, %TBGL_TEX_MIPMAP
TBGL_LoadTexture APP_SourcePath + "thinbasic.bmp", 2, %TBGL_TEX_MIPMAP
TBGL_LoadTexture APP_SourcePath + "rabbit.bmp", 3, %TBGL_TEX_MIPMAP
'TBGL_UseTexture 1 : TBGL_BindTexture 1

' Start Main Loop
While tbgl_isWindow(hWnd)

frameRate = tbgl_getframerate

TBGL_ClearFrame ' Clear Display
TBGL_Camera 0,0,1,0,0,0 ' Default Camera, View From 0,0,1 To 0,0,0.

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_UseTexture 2 : TBGL_BindTexture 2
TBGL_BeginPoly %GL_QUADS ' Drawing using Quads
' Front Face
tbgl_texcoord2d 0.0, 0.0 : TBGL_Vertex -1.0, -1.0, 1.0 ' Bottom Left of Texture and Quad
tbgl_TexCoord2d 1.0, 0.0 : tbgl_Vertex 1.0, -1.0, 1.0 ' Bottom Right
TBgl_TexCoord2d 1.0, 1.0 : tbgl_Vertex 1.0, 1.0, 1.0 ' Top Right
TBGL_TexCoord2D 0.0, 1.0 : TBGL_Vertex -1.0, 1.0, 1.0 ' Top Left
TBGL_EndPoly
TBGL_UseTexture 3 : TBGL_BindTexture 3
TBGL_BeginPoly %GL_QUADS
' Back Face
tbgl_TexCoord2d 1.0, 0.0 : tbgl_Vertex -1.0, -1.0, -1.0 ' Bottom Right of Texture and Quad
TBgl_TexCoord2d 1.0, 1.0 : tbgl_Vertex -1.0, 1.0, -1.0 ' Top Right
tbgl_TexCoord2d 0.0, 1.0 : tbgl_Vertex 1.0, 1.0, -1.0 ' Top Left
TBgl_TexCoord2D 0.0, 0.0 : tbgl_Vertex 1.0, -1.0, -1.0 ' Bottom Left
TBGL_EndPoly
TBGL_UseTexture 2 : TBGL_BindTexture 2
TBGL_BeginPoly %GL_QUADS
' Top Face
TBgl_TexCoord2d 0.0, 1.0 : tbgl_Vertex -1.0, 1.0, -1.0 ' Top Left of Texture and Quad
tbgl_TexCoord2d 0.0, 0.0 : tbgl_Vertex -1.0, 1.0, 1.0 ' Bottom Left
tbgl_TexCoord2d 1.0, 0.0 : tbgl_Vertex 1.0, 1.0, 1.0 ' Bottom Right
tbgl_TexCoord2d 1.0, 1.0 : tbgl_Vertex 1.0, 1.0, -1.0 ' Top Right
TBGL_EndPoly
TBGL_UseTexture 1 : TBGL_BindTexture 1
TBGL_BeginPoly %GL_QUADS
' Bottom Face
tbgl_texcoord2d 1.0, 1.0 : tbgl_vertex -1.0, -1.0, -1.0 ' Top Right of Texture and Quad
tbgl_texcoord2d 0.0, 1.0 : tbgl_vertex 1.0, -1.0, -1.0 ' Top Left
tbgl_texcoord2d 0.0, 0.0 : tbgl_vertex 1.0, -1.0, 1.0 ' Bottom Left
tbgl_texcoord2d 1.0, 0.0 : tbgl_vertex -1.0, -1.0, 1.0 ' Bottom Right
TBGL_EndPoly

TBGL_BeginPoly %GL_QUADS
' Right Face
tbgl_texcoord2d 1.0, 0.0 : tbgl_vertex 1.0, -1.0, -1.0 ' Bottom Right of Texture and Quad
tbgl_texcoord2d 1.0, 1.0 : tbgl_vertex 1.0, 1.0, -1.0 ' Top Right
tbgl_texcoord2d 0.0, 1.0 : tbgl_vertex 1.0, 1.0, 1.0 ' Top Left
tbgl_texcoord2d 0.0, 0.0 : tbgl_vertex 1.0, -1.0, 1.0 ' Bottom Left
TBGL_EndPoly
TBGL_UseTexture 3 : TBGL_BindTexture 3
TBGL_BeginPoly %GL_QUADS
' Left Face
tbgl_texcoord2d 0.0, 0.0 : tbgl_vertex -1.0, -1.0, -1.0 ' Bottom Left of Texture and Quad
tbgl_texcoord2d 1.0, 0.0 : tbgl_vertex -1.0, -1.0, 1.0 ' Bottom Right
tbgl_texcoord2d 1.0, 1.0 : tbgl_vertex -1.0, 1.0, 1.0 ' Top Right
tbgl_texcoord2d 0.0, 1.0 : tbgl_vertex -1.0, 1.0, -1.0 ' Top Left

TBGL_EndPoly

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

TBGL_DrawFrame ' Display anything

If tbgl_GetAsyncKeyState(%VK_ESCAPE) then Exit While

Wend

TBGL_DestroyWindow ' Closes Display

Petr Schreiber
28-03-2010, 19:44
Hi Zak,

thanks for sharing your code!

I did few modifications to it:


' NeHe Lesson 06

uses "UI" ' MessageBox Functions
Uses "TBGL" ' ThinBASIC OpenGL Library

' Create MessageBox
dim fullScreen as long
fullScreen = msgbox(0, "Would you like to Run in Fullscreen Mode?", %MB_YESNO or %MB_ICONINFORMATION, "Start FullScreen?")

Dim width, height, bpp As Long

TBGL_GetDesktopInfo(width, height, bpp)

select case fullScreen

' Create FullScreen Display, Return Handle.
case %IDYES
dim hWnd as dword
hWnd = TBGL_CreateWindowEx("", width, height, bpp, %TBGL_WS_FULLSCREEN)

' Create Windowed Display, Return Handle.
case %IDNO
dim hWnd as dword
hWnd = TBGL_CreateWindowEx("NeHe Lesson 06 - Press 'Esc' to Quit", 640, 480, bpp, %TBGL_WS_WINDOWED)

end select
TBGL_ShowWindow ' Show Display

' Declare Variables here

Dim rquad As Single ' Cube Rotation
Dim xRot, yRot, zRot As Single ' X, Y & Z-Axis Rotation.
Dim frameRate As Integer ' Timing Variable

TBGL_ResetKeyState() ' Reset all Keys

' Load Texture, Apply it.
TBGL_LoadTexture APP_SourcePath + "NeHe.bmp" , 1, %TBGL_TEX_MIPMAP
TBGL_LoadTexture APP_SourcePath + "thinbasic.bmp", 2, %TBGL_TEX_MIPMAP
TBGL_LoadTexture APP_SourcePath + "rabbit.bmp" , 3, %TBGL_TEX_MIPMAP


' Start Main Loop
While tbgl_isWindow(hWnd)

frameRate = tbgl_getframerate

TBGL_ClearFrame ' Clear Display
TBGL_Camera 0,0,1,0,0,0 ' Default Camera, View From 0,0,1 To 0,0,0.

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_BindTexture 1
TBGL_BeginPoly %GL_QUADS
' Bottom Face
TBGL_TexCoord2D 1.0, 1.0 : TBGL_Vertex -1.0, -1.0, -1.0 ' Top Right of Texture and Quad
TBGL_TexCoord2D 0.0, 1.0 : TBGL_Vertex 1.0, -1.0, -1.0 ' Top Left
TBGL_TexCoord2D 0.0, 0.0 : TBGL_Vertex 1.0, -1.0, 1.0 ' Bottom Left
TBGL_TexCoord2D 1.0, 0.0 : TBGL_Vertex -1.0, -1.0, 1.0 ' Bottom Right

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

TBGL_BindTexture 2
TBGL_BeginPoly %GL_QUADS ' Drawing using Quads
' Front Face
tbgl_texcoord2d 0.0, 0.0 : TBGL_Vertex -1.0, -1.0, 1.0 ' Bottom Left of Texture and Quad
tbgl_TexCoord2d 1.0, 0.0 : tbgl_Vertex 1.0, -1.0, 1.0 ' Bottom Right
TBgl_TexCoord2d 1.0, 1.0 : tbgl_Vertex 1.0, 1.0, 1.0 ' Top Right
TBGL_TexCoord2D 0.0, 1.0 : TBGL_Vertex -1.0, 1.0, 1.0 ' Top Left

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

TBGL_BindTexture 3
TBGL_BeginPoly %GL_QUADS
' Back Face
tbgl_TexCoord2d 1.0, 0.0 : tbgl_Vertex -1.0, -1.0, -1.0 ' Bottom Right of Texture and Quad
TBgl_TexCoord2d 1.0, 1.0 : tbgl_Vertex -1.0, 1.0, -1.0 ' Top Right
tbgl_TexCoord2d 0.0, 1.0 : tbgl_Vertex 1.0, 1.0, -1.0 ' Top Left
TBGL_TexCoord2D 0.0, 0.0 : TBGL_Vertex 1.0, -1.0, -1.0 ' Bottom Left

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


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

TBGL_DrawFrame ' Display anything

If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

Wend

TBGL_DestroyWindow ' Closes Display


TBGL_GetDesktopInfo tells you width, height and bit depth of desktop, useful settings for safe fullscreen
I know it is detail, but instead of using 1 and 0 in TBGL_CreateWindowEx, I recommend using %TBGL_WS_FULLSCREEN and %TBGL_WS_WINDOWED
You use 3 different textures, but you bind each twice, in the updated code I "sorted" the faces by texture, so only 3x begin/end is necessary

I did not forget your request, but I think this is such a specific case, that having control over it in the code would be better. For this, here I prepaired thinBASIC code for you, which will allow you rendering cube with individual colors and textures.

For this purpose I prepaired special include file, zaks_cube.inc, for you. You can include it in your applications, as shown in attached demo code. I also picked some different colors for each cube face.

I apologize I kept you waiting.


Petr

zak
29-03-2010, 06:45
thank you Petr, a wonderful code like the christmas gift, indeed there is a little talk about the muti textures over a cube.
i wish to you to be the owner and manager of a big and successful Programming, Graphics, Simulation, A.I company ( PGSAI Co. Ltd).
best wishes

Petr Schreiber
29-03-2010, 07:29
Thanks,

I am happy it works for you! :)


Petr