Author Topic: Texture Mapped Tunnel  (Read 4408 times)

0 Members and 1 Guest are viewing this topic.

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Texture Mapped Tunnel
« on: September 03, 2006 »
Just as I have promised to Shockwave, here is a 'Texture Mapped Tunnel' in all it's glory  :P
It's not the same tunnel code that Clyde used in his demo but still the same approach.
You can easily find more info about it just googling for some detailed tutorial or article.


Code: [Select]
'---------------------------------------------------------------------
'
'Â  Â  Texture Mapped Tunnel
'
'Â  Â  Code: Rbraz - Sept 2006
'
'--------------------------------------------------------------------


Option Explicit

'Windowed
'#define PTC_WIN

'-------------------------------------
' Includes.
'-------------------------------------
#Include "windows.bi"
#Include "tinyptc.bi"

'Palette array
#Include "Data\brick03_pal.bas"
'Raw Image array
#Include "Data\brick03_raw.bas"

'Sub Routines
Declare Sub WritePixelFast( byval intX As Integer, byval intY As Integer, byval intC As Integer )
Declare Sub LoadDataTexture()
Declare Sub InitTunnel()

'Constants
Const XRES=640 'Screen Width
Const YRES=480 'Screen Height
Const ARES=XRES * YRESÂ  Â  Â  Â  Â  Â  Â  'Array Width

'TinyPTC Buffer
Dim Shared Buffer(ARES) as integer

'RGB color palette buffer
Dim Shared img_r(256), img_g(256), img_b(256) as short  

'texture constants/array
Dim Shared as integer textureWidth  = 256
Dim Shared as integer textureHeight = 256
Dim Shared texture(textureWidth * textureHeight)

'Tunnel
Dim Shared dist(ARES) as integer
Dim Shared angleData(ARES) as integer
dim ctime1 as double
dim as integer du,dv,x,y,texel

'Load Data Image
LoadDataTexture()

'Initialyze tunnel data
InitTunnel()

'Open TinyPTC window
If( ptc_open( "Texture Mapped Tunnel", XRES, YRES ) = 0 ) Then
  Â  End -1
End if

While Inkey$() <> Chr$(27)

  Â  Â  Â  ctime1 = GetTickCount() / 2000.0
  Â  Â  Â  
  Â  Â  Â  'add u,v displacement
  Â  Â  Â  du = (textureWidth * 2.0 * ctime1)
  Â  Â  Â  dv = (textureHeight * 0.5 * ctime1)  Â  Â
  Â  Â  Â  
  Â  Â  Â  for y = 0 to YRES-1
  Â  Â  Â  Â  Â  for x = 0 to XRES-1
  Â  Â  Â  Â  Â  Â  Â  'Get the texel from the texture
  Â  Â  Â  Â  Â  Â  Â  texel = texture( ((dist(x + (y*XRES)) + du) mod textureWidth) + ( ((angleData(x + (y*XRES)) + dv) mod textureHeight ) * textureWidth ) )
  Â  Â  Â  Â  Â  Â  Â  WritePixelFast(x,y,(img_r(texel) Shl 16) Or (img_g(texel) Shl 8 )  Or img_b(texel))
  Â  Â  Â  Â  Â  next
  Â  Â  Â  next
  Â  Â  Â  
  Â  Â  Â  Ptc_Update @Buffer(0)
  Â  
Wend
'Close TinyPTC window
Ptc_Close()

Sub WritePixelFast( byval intX As Integer, byval intY As Integer, byval intC As Integer )
  Â  If ( intX>0 And intX<XRES-1 ) And ( intY>0 And intY<YRES-1 ) Then
  Â  Â  Â  Buffer( intX + (intY * XRES) ) = intC
  Â  End If  Â
End Sub

Sub LoadDataTexture()
  Â  dim i as integer

  Â  'Load Colour palette
  Â  for i = 0 to 255
  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  'Palette array name
  Â  Â  Â  Â img_r( i ) = brick03_pal (i*3  )'Red color
  Â  Â  Â  Â img_g( i ) = brick03_pal (i*3+1)'Green color
  Â  Â  Â  Â img_b( i ) = brick03_pal (i*3+2)'Blue color
  Â  Next
  Â  Â  Â
  Â  for i = 0 to (textureWidth*textureHeight) - 1
  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  'Raw image array name
  Â  Â  Â  Â texture(i) = brick03_raw(i)
  Â  next  
  Â  Â  Â  
End Sub

Sub InitTunnel()
  Â  dim as integer x,y

  Â  'Initialyze tunnel data
  Â  for y = 0 to YRES-1
  Â  Â  Â  for x = 0 to XRES-1
  Â  Â  Â  Â  Â  dist(x + (y*XRES))  = ( 64 * textureHeight / sqr( ( (x - (XRES / 2.0)) * (x - (XRES / 2.0)) ) + ( (y - (YRES / 2.0)) * (y - (YRES / 2.0)) ) ) ) mod textureHeight
  Â  Â  Â  Â  Â  angleData(x + (y*XRES)) = (atan2( (YRES / 2.0) - y , (XRES / 2.0) - x )  * textureWidth / 3.1416)
  Â  Â  Â  next
  Â  next

End Sub
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Texture Mapped Tunnel
« Reply #1 on: September 03, 2006 »
:D I particularly like the way the screen keeps spinning even after you quit the demo!  Don't stare at it too long folks!
Jim
Challenge Trophies Won:

Offline MrP

  • Atari ST
  • ***
  • Posts: 176
  • Karma: 18
    • View Profile
Re: Texture Mapped Tunnel
« Reply #2 on: September 03, 2006 »
Very nice. I like that.....

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17412
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Texture Mapped Tunnel
« Reply #3 on: September 03, 2006 »
Thank you very, very much Rbraz. I'd wondered for ages how to make this kind of tunnel. I will be sure to look through this very carefully and see what other possibilities that the idea holds.
Maybe a new dbf intro on the horizon with a bump mapped tunnel? :) Hehe!
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Texture Mapped Tunnel
« Reply #4 on: September 03, 2006 »
No probs dude, sometime ago I read that you can apply your texture to any shape you like, as long as you can describe it with a mathematical formula, and that's a truth  ;D
Challenge Trophies Won:

Offline Tetra

  • DBF Aficionado
  • ******
  • Posts: 2532
  • Karma: 83
  • Pirate Monkey!
    • View Profile
Re: Texture Mapped Tunnel
« Reply #5 on: September 03, 2006 »
Nice one Rbraz :) and thanks for sharing
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Texture Mapped Tunnel
« Reply #6 on: September 04, 2006 »
What an amazing affect - thanks for the code man its wicked!

DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: Texture Mapped Tunnel
« Reply #7 on: September 05, 2006 »
Nice!

Freebasic/examples/gfx/re-tunnel or something. Uses the same approach.

I believe Optimus'  GP32 demo used the same approach.


Challenge Trophies Won: