I converted a skybox example written in Delphi to thinBasic. Of course, I got stuck and Petr saved the day. So big thanks as always to Petr!!
I cleaned up the code and present it here.
The Delphi source credit goes to Jan Horn. I made the textures in the older terragen and rendered to a small resolution, so the file wouldn't be too big.
I also came up with an easier naming for the textures to make it easier to orient yourself.
Arrow keys rotate your view, escape to exit.
Petr Schreiber
15-06-2007, 12:32
Hi,
next version of flares :)
With better textures it could look ... better, but now the different flares move "proportionally" as in classic camera.
Kent, you don't need to ( but it is not prohibited ) delete display lists on scripts end, TBGL does that automatically.
Bye,
Petr
Petr Schreiber
15-06-2007, 14:11
Hi,
use following code as patch to ZIP in previous post:
* smooth appearing of flares ( no brutal jump on/off )
* no UI module
* no thinbasic_glu
* removed unused variables
* sizing of flares using Z,X is back
* moves proportional to framerate
Bye,
Petr
'---version 3C
'---conversion and new textures by kryton9
'---cheap lens flare replaced with smooth less cheap ones ;)
uses "tbgl"
#include "%app_includepath%\thinbasic_gl.inc"
'---texture constants
global north as glUint = 1
global south as glUint = 2
global east as glUint = 3
global west as glUint = 4
global up as glUint = 5
global down as glUint = 6
global flare1 as glUint = 7
global flare2 as glUint = 8
global flare3 as glUint = 9
global flare4 as glUint = 10
'---global variables
global xangle, yangle,sx as glfloat = 0.47
global speed as glfloat = 50
global lensflaresize as double = 0
dim hwnd as dword
dim i as long
'i was using these for various tests and ended up using them for the flare
'i manually adjusted the values and then once i had a set of numbers that
'looked ok on screen, i hardcoded the values in here
sx = 1
msgbox (0, "skybox demo v3C"+$CRLF+$CRLF+"Skybox based on Delphi code by Jan Horn"+$CRLF+"thinBASIC port by kryton9"+$CRLF+"Lens flare by Psch", %MB_OK or %MB_ICONINFORMATION, "Demo")
'---create window and initialize opengl
hwnd = tbgl_createwindowex ("Use arrow keys to rotate, Z,X to zoom flares, ESC to exit",640,480,32,0)
tbgl_showwindow
tbgl_uselighting 0
tbgl_usetexture 1
'---load textures
tbgl_loadtexture app_sourcepath + "north.bmp", north , %tbgl_tex_mipmap
tbgl_loadtexture app_sourcepath + "south.bmp", south , %tbgl_tex_mipmap
tbgl_loadtexture app_sourcepath + "east.bmp", east , %tbgl_tex_mipmap
tbgl_loadtexture app_sourcepath + "west.bmp", west , %tbgl_tex_mipmap
tbgl_loadtexture app_sourcepath + "up.bmp", up , %tbgl_tex_mipmap
tbgl_loadtexture app_sourcepath + "down.bmp", down , %tbgl_tex_mipmap
'---clamp to the edges, this gets rid of seams
%gl_clamp_to_edge = &h0812f
for i = north to down
tbgl_bindtexture i
gltexparameteri(%gl_texture_2d, %gl_texture_wrap_s, %gl_clamp_to_edge)
gltexparameteri(%gl_texture_2d, %gl_texture_wrap_t, %gl_clamp_to_edge)
next
tbgl_loadtexture app_sourcepath + "flare1.bmp", flare1 , %tbgl_tex_mipmap
tbgl_loadtexture app_sourcepath + "flare2.bmp", flare2 , %tbgl_tex_mipmap
tbgl_loadtexture app_sourcepath + "flare3.bmp", flare3 , %tbgl_tex_mipmap
tbgl_loadtexture app_sourcepath + "flare4.bmp", flare4 , %tbgl_tex_mipmap
%skybox = 50
tbgl_newlist %skybox
tbgl_pushmatrix
tbgl_color 255,255,255
tbgl_scale 10,10,10
'---make our skybox cube
tbgl_bindtexture north
'---you only need to uncomment just this line if you want the same color tint for all sides
tbgl_beginpoly %gl_quads
tbgl_texcoord2d 0, 1 : tbgl_vertex 1, 1, 1
tbgl_texcoord2d 1, 1 : tbgl_vertex -1, 1, 1
tbgl_texcoord2d 1, 0 : tbgl_vertex -1, -1, 1
tbgl_texcoord2d 0, 0 : tbgl_vertex 1, -1, 1
tbgl_endpoly
tbgl_bindtexture south
tbgl_beginpoly %gl_quads
tbgl_texcoord2d 1, 1 : tbgl_vertex 1, 1, -1
tbgl_texcoord2d 0, 1 : tbgl_vertex -1, 1, -1
tbgl_texcoord2d 0, 0 : tbgl_vertex -1, -1, -1
tbgl_texcoord2d 1, 0 : tbgl_vertex 1, -1, -1
tbgl_endpoly
tbgl_bindtexture east
tbgl_beginpoly %gl_quads
tbgl_texcoord2d 0, 1 : tbgl_vertex -1, 1, 1
tbgl_texcoord2d 1, 1 : tbgl_vertex -1, 1, -1
tbgl_texcoord2d 1, 0 : tbgl_vertex -1, -1, -1
tbgl_texcoord2d 0, 0 : tbgl_vertex -1, -1, 1
tbgl_endpoly
tbgl_bindtexture west
tbgl_beginpoly %gl_quads
tbgl_texcoord2d 1, 1 : tbgl_vertex 1, 1, 1
tbgl_texcoord2d 0, 1 : tbgl_vertex 1, 1, -1
tbgl_texcoord2d 0, 0 : tbgl_vertex 1, -1, -1
tbgl_texcoord2d 1, 0 : tbgl_vertex 1, -1, 1
tbgl_endpoly
tbgl_bindtexture up
tbgl_beginpoly %gl_quads
tbgl_texcoord2d 0, 1 : tbgl_vertex 1, 1, -1
tbgl_texcoord2d 1, 1 : tbgl_vertex -1, 1, -1
tbgl_texcoord2d 1, 0 : tbgl_vertex -1, 1, 1
tbgl_texcoord2d 0, 0 : tbgl_vertex 1, 1, 1
tbgl_endpoly
tbgl_bindtexture down
tbgl_beginpoly %gl_quads
tbgl_texcoord2d 0, 0 : tbgl_vertex 1, -1, -1
tbgl_texcoord2d 1, 0 : tbgl_vertex -1, -1, -1
tbgl_texcoord2d 1, 1 : tbgl_vertex -1, -1, 1
tbgl_texcoord2d 0, 1 : tbgl_vertex 1, -1, 1
tbgl_endpoly
tbgl_popmatrix
tbgl_endlist
'---initializes keyboard input
tbgl_GetAsyncKeyState (-1)
dim FrameRate as number
'---the main loop
while tbgl_iswindow hwnd
FrameRate = tbgl_GetFrameRate
tbgl_RenderMatrix3d
tbgl_clearframe
'---rotate the scene based on keyboard input of arrow keys
tbgl_rotate yangle,1,0,0
tbgl_rotate xangle,0,1,0
tbgl_calllist %skybox
drawLensFlare
tbgl_drawframe
'---handle keyboard user input
if TBGL_GetWindowKeyState( hWnd, %vk_escape) then exit while
if TBGL_GetWindowKeyState( hWnd, %vk_right ) then xangle += speed/FrameRate
if TBGL_GetWindowKeyState( hWnd, %vk_left ) then xangle -= speed/FrameRate
if TBGL_GetWindowKeyState( hWnd, %vk_up ) then yangle += speed/FrameRate
if TBGL_GetWindowKeyState( hWnd, %vk_down ) then yangle -= speed/FrameRate
'these were used for testing and getting values
if TBGL_GetWindowKeyState( hWnd, %vk_x) then sx += (speed/100)/FrameRate
if TBGL_GetWindowKeyState( hWnd, %vk_z) then sx -= (speed/100)/FrameRate
wend
tbgl_destroywindow
Sub drawLensFlare()
local i, j as long
local sunX, sunY as single
local xres, yres as long
LOCAL R AS BYTE
' Get current screen size
tbgl_getwindowClient hwnd, xres, yres
' Point position to pixels
tbgl_Pos3dToPos2d ( 0.99, 0.55, 0.6, sunx, suny)
' Can we see the sun ? If not, no reason for flares !
if sunx < 0 or sunx > xres or suny < 0 or suny > yres then exit sub
' Lets go 2D
tbgl_RenderMatrix2d
' Post process stuff, no need for depth
tbgl_UseDepthMask 0
tbgl_useblend 1
' Lets cycle through all flare textures
for i = flare1 to flare1+3
tbgl_PushMatrix
'--lens flare
lensflaresize = 128/(i-flare1+1)
tbgl_Translate (sunx+(xres/2-sunx)*(i-flare1)/2, suny+(yres/2-suny)*(i-flare1)/2, 0)
tbgl_scale sx,sx,1
tbgl_bindtexture i
R = (sqr( (xres/2)^2 + (yres/2)^2)-sqr( (xres/2-sunx)^2 + (yres/2-suny)^2))/sqr( (xres/2)^2 + (yres/2)^2) *32
tbgl_color R,R,0
tbgl_beginpoly %gl_quads
tbgl_texcoord2d 0, 0 : tbgl_vertex lensflaresize, lensflaresize
tbgl_texcoord2d 1, 0 : tbgl_vertex -lensflaresize, lensflaresize
tbgl_texcoord2d 1, 1 : tbgl_vertex -lensflaresize, -lensflaresize
tbgl_texcoord2d 0, 1 : tbgl_vertex lensflaresize, -lensflaresize
tbgl_endpoly
tbgl_popMatrix
next
' Back to normal
tbgl_useblend 0
tbgl_UseDepthMask 1
end sub
Petr Schreiber
16-06-2007, 17:36
Hi Mike,
nice observation ! I missed that ...
To solve it please use this fixed routine:
Sub drawLensFlare()
local i, j as long
local sunX, sunY as single
local xres, yres as long
LOCAL R AS BYTE
' With sun in my back I don't want to have lens flares
if tbgl_IsPointBehindView(0.99, 0.55, 0.6) then exit sub
' Get current screen size
tbgl_getwindowClient hwnd, xres, yres
' Point position to pixels
tbgl_Pos3dToPos2d ( 0.99, 0.55, 0.6, sunx, suny)
' Can we see the sun ? If not, no reason for flares !
if sunx < 0 or sunx > xres or suny < 0 or suny > yres then exit sub
' Lets go 2D
tbgl_RenderMatrix2d
' Post process stuff, no need for depth
tbgl_UseDepthMask 0
tbgl_useblend 1
' Lets cycle through all flare textures
for i = flare1 to flare1+3
tbgl_PushMatrix
'--lens flare
lensflaresize = 128/(i-flare1+1)
tbgl_Translate (sunx+(xres/2-sunx)*(i-flare1)/2, suny+(yres/2-suny)*(i-flare1)/2, 0)
tbgl_scale sx,sx,1
tbgl_bindtexture i
R = (sqr( (xres/2)^2 + (yres/2)^2)-sqr( (xres/2-sunx)^2 + (yres/2-suny)^2))/sqr( (xres/2)^2 + (yres/2)^2) *32
tbgl_color R,R,0
tbgl_beginpoly %gl_quads
tbgl_texcoord2d 0, 0 : tbgl_vertex lensflaresize, lensflaresize
tbgl_texcoord2d 1, 0 : tbgl_vertex -lensflaresize, lensflaresize
tbgl_texcoord2d 1, 1 : tbgl_vertex -lensflaresize, -lensflaresize
tbgl_texcoord2d 0, 1 : tbgl_vertex lensflaresize, -lensflaresize
tbgl_endpoly
tbgl_popMatrix
next
' Back to normal
tbgl_useblend 0
tbgl_UseDepthMask 1
end sub
Bye,
Petr