Post by Tom Mulgrew on Jul 31, 2003 4:47:22 GMT -5
See if I can set the ball rolling :P
A coloured tube..
A coloured tube..
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Tube
' Settings
dim l, w, segl#, segw#, cMin, cMax, cRnd
l = 50 ' Tunnel length in segments
w = 20 ' Width in segments
segl# = 10 ' Segment size
segw# = 20
cMin = 0
cMax = 255
cRnd = 20
' Tube map
' 2D grid of colour components
dim r(w*l), g(w*l), b(w*l), ang#(w*l)
' Viewpoint & velocity
dim vz#, vang#, voffset
dim vzSpeed#, vangSpeed#
vzSpeed# = 0.2: vangSpeed# = 0.2
' Working
dim x, y, i1, i2, i3, i4, t, counter#, scale#
' Textures
dim tex
tex = LoadMipmapTexture ("textures\00004.jpg")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Subroutines
goto start
GenerateRow:
' Generate a row of colours based on the previous one
i1 = y * w
i2 = ((y + l - 1) % l) * w
for x = 0 to w - 1
' Average colours from previous row
r(i1 + x) = (r(i2 + (x + w - 1) % w) + r(i2 + x) + r(i2 + (x + 1) % w)) / 3
g(i1 + x) = (g(i2 + (x + w - 1) % w) + g(i2 + x) + g(i2 + (x + 1) % w)) / 3
b(i1 + x) = (b(i2 + (x + w - 1) % w) + b(i2 + x) + b(i2 + (x + 1) % w)) / 3
' Add random offset
r(i1 + x) = r(i1 + x) + rnd()%(cRnd*2+1)-cRnd
g(i1 + x) = g(i1 + x) + rnd()%(cRnd*2+1)-cRnd
b(i1 + x) = b(i1 + x) + rnd()%(cRnd*2+1)-cRnd
' Clamp colour components
if r(i1 + x) < cMin then r(i1 + x) = cMin endif
if g(i1 + x) < cMin then g(i1 + x) = cMin endif
if b(i1 + x) < cMin then b(i1 + x) = cMin endif
if r(i1 + x) > cMax then r(i1 + x) = cMax endif
if g(i1 + x) > cMax then g(i1 + x) = cMax endif
if b(i1 + x) > cMax then b(i1 + x) = cMax endif
' Setup angle
ang#(i1 + x) = 2 * m_pi / w * (x + (rnd()%100)/100.0)
next
return
start:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Setup original map
' Use random values for first row
y = 0: gosub generateRow
for x = 0 to w - 1
r(x) = rnd()%256
g(x) = rnd()%256
b(x) = rnd()%256
next
' Generate remaining rows
for y = 1 to l - 1
gosub GenerateRow
next
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Setup OpenGL
glDisable (GL_CULL_FACE)
glEnable (GL_TEXTURE_2D)
glBindTexture (GL_TEXTURE_2D, tex)
TextMode (TEXT_OVERLAID)
dim a$: a$ = "Tube"
locate (TextCols () - len(a$)) / 2, TextRows() / 2
print a$
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Main loop
' scale# = segw# / cMax * 0.8
while true
' Render tunnel
glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
glMatrixMode (GL_MODELVIEW)
glLoadIdentity ()
glTranslatef (0, 0, vz#)
glRotatef (vang#, 0, 0, 1)
for t = 0 to l - 2
y = (t + voffset) % l
for x = 0 to w - 1
' Find vertex indices
i1 = ((y + 1) % l) * w + x
i2 = ((y + 1) % l) * w + (x + 1) % w
i3 = y * w + x
i4 = y * w + (x + 1) % w
' Render slice of tube
glBegin (GL_QUADS)
glTexCoord2f (0, 0): glColor3ub (r(i1), g(i1), b(i1)): glVertex3f (sin(ang#(i1)) * (segw# - r(i1)*scale#), cos(ang#(i1)) * (segw# - g(i1)*scale#), -segl#)
glTexCoord2f (1, 0): glColor3ub (r(i2), g(i2), b(i2)): glVertex3f (sin(ang#(i2)) * (segw# - r(i2)*scale#), cos(ang#(i2)) * (segw# - g(i2)*scale#), -segl#)
glTexCoord2f (1, 1): glColor3ub (r(i4), g(i4), b(i4)): glVertex3f (sin(ang#(i4)) * (segw# - r(i4)*scale#), cos(ang#(i4)) * (segw# - g(i4)*scale#), 0)
glTexCoord2f (0, 1): glColor3ub (r(i3), g(i3), b(i3)): glVertex3f (sin(ang#(i3)) * (segw# - r(i3)*scale#), cos(ang#(i3)) * (segw# - g(i3)*scale#), 0)
glEnd ()
next
glTranslatef (0, 0, -segl#)
next
DrawText ()
SwapBuffers ()
' Update camera position
while SyncTimer (10)
' Respond to keyboard
if ScanKeyDown (VK_LEFT) then vangSpeed# = vangSpeed# - 0.01 endif
if ScanKeyDown (VK_RIGHT) then vangSpeed# = vangSpeed# + 0.01 endif
if ScanKeyDown (VK_UP) then vzSpeed# = vzSpeed# + 0.01 endif
if ScanKeyDown (VK_DOWN) then vzSpeed# = vzSpeed# - 0.01 endif
if vzSpeed# < 0 then vzSpeed# = 0 endif
' Move camera forwards
vz# = vz# + vzSpeed#
while vz# > segl#
vz# = vz# - segl#
y = voffset % l
gosub GenerateRow
voffset = voffset + 1
wend
' Rotate camera
vang# = vang# + vangSpeed#
wend
wend