@Janbo. Nice point lights. Your demo of drawing on a mesh is also very fascinating.
Since you asked, this is the code for baking a light onto a plane in my original post. I felt reluctant to share because I never finished it or cleaned it up. It only works if the light is above the plane. It does not work if you rotate the plane or move it elsewhere in the scene.
It works by measuring the plane's width and height, splitting it apart into a grid of user-specified size, and then getting the raycast distance from the light's position to each of the four corners of every quadrant on that grid. It then takes the average of those four points to set a value to paint to an image for that quadrant. Excuse the long input functions which I share between my projects.
// Created: 2020-07-10
// show all errors
SetErrorMode(2)
#include "functions.agc"
Bio_DefineGlobal()
Bio_Initialize()
Bio_PrepCamera()
Bio_Prep3DSettings()
Bio_PrepScene()
LoadImage(10,"olm_50.png")
SetObjectLightmap(50,10)
do
Bio_GetCurrentInput()
Bio_MoveCamera()
if GetRawKeyPressed(27) then end
if key_1_p
if GetPointLightExists(1)
DeletePointLight(1)
PointLightactive=0
else
CreatePointLight(1,plight_x#,plight_y#,plight_z#,plight_d#,plight_r#,plight_g#,plight_b#)
SetPointLightMode(1,1)
PointLightactive=1
endif
endif
if key_3_p
Bio_Lightmapper(50,200,2,1,6)
endif
//HUD
if key_2_P
if show_hud=1 then show_hud=0 else show_hud=1
endif
if show_hud=1
Print("FPS: "+str(Round(ScreenFPS())))
Print("GPU: "+str(ROUND(GetImageMemoryUsage()))+" MB")
Print("2: Toggle HUD")
Print("1: Toggle Point Light")
Print("3: Run Lightmapper")
Print(str(GetCameraX(1)))
Print(str(GetCameraY(1)))
Print(str(GetCameraZ(1)))
endif
Sync()
loop
function Bio_Lightmapper(obj,rcast_size,lmap_scale,lmap_blendmode,lmap_imagenum)
//obj = object number
//rcast_size = How many "quadrants" to split width and height for raycasting (i.e. 200 will split obj into 200x200 boxes, casting rays at corner of each box)
//lmap_scale = Must be integer and 2 or larger. Scale of lightmap compared to raycast size (must be larger to correctly render corners of box rays to sepearate pixels) (i.e. a scale of 4 renders a 4x4 pixel area in the final lightmap for each raycasted quadrant)
//lmap_blendmode = If 1, creates a single blended color for a raycasted quadrant that blends light levels of each corner. If 0, color is gradient and changes towards each corner of each quadrant
//lmap_imagenum = image number to render lightmap to
`rcast_size=200
`lmap_scale=10
CreateRenderImage(lmap_imagenum,rcast_size*lmap_scale,rcast_size*lmap_scale,0,0)
object_w#=GetObjectSizeMaxX(obj)-GetObjectSizeMinX(obj)
object_h#=GetObjectSizeMaxY(obj)-GetObjectSizeMinY(obj)
startingposition_x#=GetObjectX(obj)-(object_w#/2)
startingposition_y#=GetObjectZ(obj)+(object_h#/2)
SetRenderToImage(lmap_imagenum,0)
SetVirtualResolution(rcast_size*lmap_scale,rcast_size*lmap_scale)
for lm_x#=0 to rcast_size-1
for lm_y#=0 to rcast_size-1
raycheck_tl=ObjectRayCast( 0, plight_x#, plight_y#, plight_z#, startingposition_x#+(object_w#*(lm_x#/rcast_size)), -0.0001, startingposition_y#-(object_h#*(lm_y#/rcast_size)))
if raycheck_tl=obj and plight_d#-GetObjectRayCastDistance(0)>0
lightstrength_tl#=((plight_d#-GetObjectRayCastDistance(0))/plight_d#)
else
lightstrength_tl#=0
endif
raycheck_tr=ObjectRayCast( 0, plight_x#, plight_y#, plight_z#, startingposition_x#+(object_w#*((lm_x#/rcast_size)+(1.0/rcast_size))), -0.0001, startingposition_y#-(object_h#*(lm_y#/rcast_size)))
if raycheck_tr=obj and plight_d#-GetObjectRayCastDistance(0)>0
lightstrength_tr#=((plight_d#-GetObjectRayCastDistance(0))/plight_d#)
else
lightstrength_tr#=0
endif
raycheck_bl=ObjectRayCast( 0, plight_x#, plight_y#, plight_z#, startingposition_x#+((object_w#*(lm_x#/rcast_size))), -0.0001, startingposition_y#-(object_h#*((lm_y#/rcast_size)+(1.0/rcast_size))))
if raycheck_bl=obj and plight_d#-GetObjectRayCastDistance(0)>0
lightstrength_bl#=((plight_d#-GetObjectRayCastDistance(0))/plight_d#)
else
lightstrength_bl#=0
endif
raycheck_br=ObjectRayCast( 0, plight_x#, plight_y#, plight_z#, startingposition_x#+(object_w#*((lm_x#/rcast_size)+(1.0/rcast_size))), -0.0001,startingposition_y#-(object_h#*((lm_y#/rcast_size)+(1.0/rcast_size))))
if raycheck_br=obj and plight_d#-GetObjectRayCastDistance(0)>0
lightstrength_br#=((plight_d#-GetObjectRayCastDistance(0))/plight_d#)
else
lightstrength_br#=0
endif
if lmap_blendmode = 1
blend#=((lightstrength_tl#+lightstrength_tr#+lightstrength_bl#+lightstrength_br#)/4.0)*255.0
color_blend=MakeColor( blend#,blend#,blend#)
DrawBox(lm_x#*lmap_scale,lm_y#*lmap_scale,(lm_x#*lmap_scale)+lmap_scale,(lm_y#*lmap_scale)+lmap_scale-1,color_blend,color_blend,color_blend,color_blend,1)
endif
if lmap_blendmode = 0
color_tl=MakeColor(lightstrength_tl#*255,lightstrength_tl#*255,lightstrength_tl#*255)
color_tr=MakeColor(lightstrength_tr#*255,lightstrength_tr#*255,lightstrength_tr#*255)
color_bl=MakeColor(lightstrength_bl#*255,lightstrength_bl#*255,lightstrength_bl#*255)
color_br=MakeColor(lightstrength_br#*255,lightstrength_br#*255,lightstrength_br#*255)
DrawBox(lm_x#*lmap_scale,lm_y#*lmap_scale,(lm_x#*lmap_scale)+lmap_scale,(lm_y#*lmap_scale)+lmap_scale-1,color_tl,color_tr,color_bl,color_br,1) `Per pixel blend
endif
next
next
SetRenderToScreen()
SetVirtualResolution(screen_width,screen_height)
SaveImage(lmap_imagenum,"olm_"+str(obj)+".png")
DeleteImage(lmap_imagenum)
endfunction
And the companion AGC file with related functions
function Bio_Initialize()
// set window and display properties
SetWindowTitle("Lightmap-testing")
SetWindowSize(screen_width,screen_height,0)
SetVirtualResolution(screen_width,screen_height) //divides the screen into units. NOT setting display
SetWindowAllowResize(1) // allow the user to resize the window
SetScissor(0,0,0,0) // use the maximum available screen space, no black borders
SetVSync(1)
//Print text settings
SetPrintSize(24)
UseNewDefaultFonts(1)
//Set Write Path to Read Path
SetRawWritePath(GetReadPath())
endfunction
function Bio_Prep3DSettings()
//Turn off sun and set ambience to black
SetSunActive( 0 )
setambientcolor(30,30,30)
//Antialias and texture filter OFF
SetAntialiasMode(0) //Anti-Alias off
SetDefaultMagFilter(0) //linear filtering off
SetDefaultMinFilter(0) //linear filtering off
SetGenerateMipmaps(0) //Generate mipmaps to reduce texture aliasing
//Turn on texture wrapping
SetDefaultWrapU(1)
SetDefaultWrapV(1)
//Camera FOV
SetCameraFOV(1,90)
endfunction
function Bio_PrepScene()
//Plane
CreateObjectPlane(50,100,100)
SetObjectPosition(50,0,0,0)
SetObjectRotation(50,90,0,0)
//Light map values for plane
global plane_topleft# as float [3]=[-50,0,50]
global plane_topright# as float [3]=[50,0,50]
global plane_bottomleft# as float [3]=[-50,0,-50]
global plane_bottomright# as float [3]=[-50,0,-50]
//Boxes
CreateObjectBox(2,10,10,10)
SetObjectPosition(2,0,10,0)
CreateObjectSphere(3,10,10,10)
SetObjectPosition(3,15,5,-20)
//Texturing
LoadImage(1,"Grass_001_COLOR.jpg")
LoadImage(2,"Rubble_001_COLOR.jpg")
SetObjectImage(50,1,0)
SetObjectImage(2,2,0)
SetObjectImage(3,2,0)
//Point light
global plight_x#=0.0
global plight_y#=50.0
global plight_z#=0.0
global plight_d#=160.0
global plight_r#=255.0
global plight_g#=255.0
global plight_b#=255.0
CreatePointLight(1,plight_x#,plight_y#,plight_z#,plight_d#,plight_r#,plight_g#,plight_b#)
SetPointLightMode(1,1)
PointLightactive=1
endfunction
function Bio_GetCurrentCamera() //Updates current camera angles and positions
cam_ax#=GetCameraAngleX(1)
cam_ay#=GetCameraAngleY(1)
cam_az#=GetCameraAngleZ(1)
cam_px#=GetCameraX(1)
cam_py#=GetCameraY(1)
cam_pz#=GetCameraZ(1)
endfunction
function Bio_PrepCamera()
setcameraposition(1,0,20,-30) //Setting y to 120
SetCameraRotation(1,30,0,0) //Rotate camera based upon right stick
SetCameraFOV(1,90)
endfunction
function Bio_MoveCamera()
Bio_GetCurrentCamera()
MoveCameraLocalZ(1,2*mouse_wheeldelta#)
MoveCameraLocalZ(1,0.5*key_w_s)
MoveCameraLocalZ(1,-0.5*key_s_s)
MoveCameraLocalX(1,-0.5*key_a_s)
MoveCameraLocalX(1,0.5*key_d_s)
//rotating camera
if mouse_rclick_s=1 or mouse_mclick_s=1
diffx#=(mouse_x#-mouse_x_lastf#)*0.2
diffy#=(mouse_y#-mouse_y_lastf#)*0.2
newcamangx#=cam_ax#+diffy#
if newcamangx#>89.5 then newcamangx#=89.5
if newcamangx#<-89.5 then newcamangx#=-89.5
SetCameraRotation(1,newcamangx#,cam_ay#+diffx#,cam_az#)
endif
mouse_x_lastf#=mouse_x#
mouse_y_lastf#=mouse_y#
endfunction
function Bio_DefineGlobal() //Define Global variables
//keyboard
global key_left_p
global key_right_p
global key_up_p
global key_down_p
global key_left_s
global key_up_s
global key_right_s
global key_down_s
global key_shift_s
global key_enter_p
global key_delete_p
global mouse_rclick_s
global mouse_mclick_s
global mouse_wheeldelta#
global mouse_lclick_p
global mouse_lclick_s
global key_w_s
global key_a_s
global key_s_s
global key_d_s
global mouse_x#
global mouse_x_lastf# //for last frame
global mouse_y#
global mouse_y_lastf# //for last frame
global key_home_p
global key_F1_p
global key_1_p
global key_2_p
global key_3_p
global key_4_p
global key_5_p
global key_6_p
global key_7_p
global key_8_p
global key_9_p
global key_0_p
global key_end_p
global key_F2_p
global key_numpad8_p
global key_numpad4_p
global key_numpad6_p
global key_numpad2_p
global key_numpad5_p
global key_numpad7_p
global key_t_p
global key_ctrl_s
global key_m_s
global key_F5_p
global key_F6_p
global key_F7_p
global key_F8_p
global key_F9_p
global key_p_p
global key_o_p
global key_n_s
global key_b_s
global key_v_s
global key_c_s
global key_0_s
global key_F3_p
global key_i_p
global key_bslash_p
global key_rbrack_p
global key_x_s
global key_z_s
global key_q_p
global key_l_p
global key_r_p
global key_u_s
global key_i_s
global key_o_s
global key_p_s
global key_y_s
global key_c_p
global key_x_p
global key_e_p
global key_z_p
global key_lbrack_p
//camera angles and positions
global cam_ax#
global cam_ay#
global cam_az#
global cam_px#
global cam_py#
global cam_pz#
//Screen size
global screen_width=1024
global screen_height=768
//Other
global show_hud=1
global dim rcast_map_array_x#[1000]
global dim rcast_map_array_y#[1000]
endfunction
function Bio_GetCurrentInput()
//get current input
key_left_p=GetRawKeyPressed(37)
key_up_p=GetRawKeyPressed(38)
key_right_p=GetRawKeyPressed(39)
key_down_p=GetRawKeyPressed(40)
key_left_s=GetRawKeyState(37)
key_up_s=GetRawKeyState(38)
key_right_s=GetRawKeyState(39)
key_down_s=GetRawKeyState(40)
key_shift_s=GetRawKeyState(257) + GetRawKeyState(258)
key_enter_p=GetRawKeyPressed(13)
key_delete_p=GetRawKeyPressed(46)
key_w_s=GetRawKeyState(87)
key_a_s=GetRawKeyState(65)
key_s_s=GetRawKeyState(83)
key_d_s=GetRawKeyState(68)
mouse_lclick_p=GetRawMouseLeftPressed()
mouse_lclick_s=GetRawMouseLeftState()
mouse_rclick_s=GetRawMouseRightState()
mouse_mclick_s=GetRawMouseMiddleState()
mouse_wheeldelta#=GetRawMouseWheelDelta()
mouse_x#=GetRawMouseX()
mouse_y#=GetRawMouseY()
key_home_p=GetRawKeyPressed(36)
key_F1_p=GetRawKeyPressed(112)
key_1_p=GetRawKeyPressed(49)
key_2_p=GetRawKeyPressed(50)
key_3_p=GetRawKeyPressed(51)
key_4_p=GetRawKeyPressed(52)
key_5_p=GetRawKeyPressed(53)
key_6_p=GetRawKeyPressed(54)
key_7_p=GetRawKeyPressed(55)
key_8_p=GetRawKeyPressed(56)
key_9_p=GetRawKeyPressed(57)
key_0_p=GetRawKeyPressed(48)
key_0_s=GetRawKeyState(48)
key_end_p = GetRawKeyPressed(35)
key_numpad8_p=GetRawKeyPressed(104)
key_numpad4_p=GetRawKeyPressed(100)
key_numpad6_p=GetRawKeyPressed(102)
key_numpad2_p=GetRawKeyPressed(98)
key_numpad5_p=GetRawKeyPressed(101)
key_numpad7_p=GetRawKeyPressed(103)
key_t_p=GetRawKeyPressed(84)
key_ctrl_s=GetRawKeyState(17)
key_m_s=GetRawKeyState(77)
key_n_s=GetRawKeyState(78)
key_b_s=GetRawKeyState(66)
key_v_s=GetRawKeyState(86)
key_c_s=GetRawKeyState(67)
key_c_p=GetRawKeyPressed(67)
key_e_p=GetRawKeyPressed(69)
key_x_s=GetRawKeyState(88)
key_x_p=GetRawKeyPressed(88)
key_z_s=GetRawKeyState(90)
key_z_p=GetRawKeyPressed(90)
key_q_p=GetRawKeyPressed(81)
key_y_s=GetRawKeyState(89)
key_l_p=GetRawKeyPressed(76)
key_F2_p=GetRawKeyPressed(113)
key_F5_p=GetRawKeyPressed(116)
key_F6_p=GetRawKeyPressed(117)
key_F7_p=GetRawKeyPressed(118)
key_F8_p=GetRawKeyPressed(119)
key_F9_p=GetRawKeyPressed(120)
key_p_p=GetRawKeyPressed(80)
key_o_p=GetRawKeyPressed(79)
key_F3_p=GetRawKeyPressed(114)
key_i_p=GetRawKeyPressed(73)
key_r_p=GetRawKeyPressed(82)
key_u_s=GetRawKeyState(85)
key_i_s=GetRawKeyState(73)
key_o_s=GetRawKeyState(79)
key_p_s=GetRawKeyState(80)
key_bslash_p=GetRawKeyPressed(220)
key_rbrack_p=GetRawKeyPressed(221)
key_lbrack_p=GetRawKeyPressed(219)
Endfunction