summaryrefslogtreecommitdiff
path: root/examples/basic_lighting/main.lua
blob: 8c5aed0400a352eac610db5f42264b0692772807 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
-- Based on raylib [shaders] example - basic lighting by Chris Camacho (@codifies)
-- https://github.com/raysan5/raylib/blob/master/examples/shaders/shaders_basic_lighting.c

package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"

Util = require( "utillib" )
Vec2 = require( "vector2" )
Vec3 = require( "vector3" )
Cam3D = require( "camera3d" )

local monitor = 0
local camera = {}

local plane = -1
local cube = -1
local material = -1
local shader = -1
local lights = {}

function RL.init()
	local mPos = Vec2:newT( RL.GetMonitorPosition( monitor ) )
	local mSize = Vec2:newT( RL.GetMonitorSize( monitor ) )
	local winSize = Vec2:new( 1028, 720 )

	RL.SetGCUnload( false )
	RL.SetWindowTitle( "Simple Lighting" )
	RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE )
	RL.SetWindowState( RL.FLAG_VSYNC_HINT )
	RL.SetWindowSize( winSize )
	RL.SetWindowPosition( { mPos.x + mSize.x / 2 - winSize.x / 2, mPos.y + mSize.y / 2 - winSize.y / 2 } )

	-- Define the camera to look into our 3d world.
	camera = Cam3D:new()

	camera:setPosition( { 0, 4, 12 } )
	camera:setTarget( { 0, 0, 0 } )
	camera:setUp( { 0, 1, 0 } )
	-- camera.mode = camera.MODES.ORBITAL
	camera.mode = camera.MODES.FIRST_PERSON

	RL.HideCursor()

	-- Generate meshes.
	plane = RL.GenMeshPlane( 10, 10, 3, 3 )
	cube = RL.GenMeshCube( { 2, 4, 2 } )

	-- Load basic lighting shader.
	-- NOTE: Shaders used in this example are #version 330 (OpenGL 3.3).

	-- Shader folders.
	local glslDir = {
		-- [ RL.RL_OPENGL_11 ] = "glsl100",
		-- [ RL.RL_OPENGL_21 ] = "glsl120",
		[ RL.RL_OPENGL_33 ] = "glsl330",
		[ RL.RL_OPENGL_43 ] = "glsl330",
		-- [ RL.RL_OPENGL_ES_20 ] = "glsl120",
	}
	local prefix = RL.GetBasePath().."../resources/shaders/"..glslDir[ RL.rlGetVersion() ].."/"

	shader = RL.LoadShader( prefix.."lighting.vs", prefix.."lighting.fs" )

	-- Get some required shader locations.
	local viewPosLoc = RL.GetShaderLocation( shader, "viewPos" )
	RL.SetShaderLocationIndex( shader, RL.SHADER_LOC_VECTOR_VIEW, viewPosLoc )
	--[[
		NOTE: "matModel" location name is automatically assigned on shader loading, 
		no need to get the location again if using that uniform name.
	]]--

	-- Ambient light level (some basic lighting).
	local ambientLoc = RL.GetShaderLocation( shader, "ambient" )
	RL.SetShaderValue( shader, ambientLoc, { 0.1, 0.1, 0.1, 1.0 }, RL.SHADER_UNIFORM_VEC4 )

	-- Create material.
	local materialData = {
		shader = shader,
		maps = {
			{
				RL.MATERIAL_MAP_ALBEDO,
				{
					color = RL.WHITE,
				},
			},
		},
	}
	material = RL.CreateMaterial( materialData )

	-- Create lights.
	table.insert( lights, RL.CreateLight( RL.LIGHT_POINT, { -2, 1, -2 }, RL.Vector3Zero(), RL.YELLOW, shader ) )
	table.insert( lights, RL.CreateLight( RL.LIGHT_POINT, { 2, 1, 2 }, RL.Vector3Zero(), RL.RED, shader ) )
	table.insert( lights, RL.CreateLight( RL.LIGHT_POINT, { -2, 1, 2 }, RL.Vector3Zero(), RL.GREEN, shader ) )
	table.insert( lights, RL.CreateLight( RL.LIGHT_POINT, { 2, 1, -2 }, RL.Vector3Zero(), RL.BLUE, shader ) )
end

function RL.update( delta )
	camera:update( delta )

	-- Check key inputs to enable/disable lights.
	if RL.IsKeyPressed( RL.KEY_Y ) then
		RL.SetLightEnabled( lights[1], not RL.IsLightEnabled( lights[1] ) )
	end
	if RL.IsKeyPressed( RL.KEY_R ) then
		RL.SetLightEnabled( lights[2], not RL.IsLightEnabled( lights[2] ) )
	end
	if RL.IsKeyPressed( RL.KEY_G ) then
		RL.SetLightEnabled( lights[3], not RL.IsLightEnabled( lights[3] ) )
	end
	if RL.IsKeyPressed( RL.KEY_B ) then
		RL.SetLightEnabled( lights[4], not RL.IsLightEnabled( lights[4] ) )
	end

	-- Update light values (actually, only enable/disable them).
	for _, light in ipairs( lights ) do
		RL.UpdateLightValues( shader, light )
	end
end

function RL.draw()
	RL.ClearBackground( { 100, 150, 100 } )

	-- Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f }).
	local loc = RL.GetShaderLocationIndex( shader, RL.SHADER_LOC_VECTOR_VIEW )
	RL.SetShaderValue( shader, loc, camera:getPosition():arr(), RL.SHADER_UNIFORM_VEC3 )

	camera:beginMode3D()
		RL.DrawMesh( plane, material, RL.MatrixIdentity() )
		RL.DrawMesh( cube, material, RL.MatrixIdentity() )

		-- Draw spheres to show where the lights are.
		for _, light in ipairs( lights ) do
			if RL.IsLightEnabled( light ) then
				RL.DrawSphereEx( RL.GetLightPosition( light ), 0.2, 8, 8, RL.GetLightColor( light ) )
			else
				RL.DrawSphereWires( RL.GetLightPosition( light ), 0.2, 8, 8, RL.ColorAlpha( RL.GetLightColor( light ), 0.3 ) )
			end
		end

	camera:endMode3D()

	RL.DrawText( "Use keys [Y][R][G][B] to toggle lights", { 10, 10 }, 20, RL.DARKGRAY )
end

function RL.exit()
	RL.UnloadMaterial( material, true )
	RL.UnloadMesh( plane )
	RL.UnloadMesh( cube )
end