summaryrefslogtreecommitdiff
path: root/examples/stencil_reflection/main.lua
blob: 9b57a472bb89abfa7722566f1b3c27133d8c12ce (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
-- Based on JeffM2501 stencil_reflection example for Raylib https://github.com/raylib-extras/examples-cpp/blob/main/stencil_reflection/main.cpp

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

Vec2 = require( "vector2" )
Cam3D = require( "camera3d" )

local monitor = 0
local camera = Cam3D:new()

function RL.init()
	RL.SetWindowTitle( "Stencil Reflection" )
	RL.SetWindowState( RL.FLAG_VSYNC_HINT )
	RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE )

	local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) )
	local mSize = Vec2:new( RL.GetMonitorSize( monitor ) )
	local winSize = Vec2:new( RL.GetScreenSize() )

	RL.SetWindowPosition( { mPos.x + mSize.x / 2 - winSize.x / 2, mPos.y + mSize.y / 2 - winSize.y / 2 } )

	camera:setPosition( { 0, 2, 6 } )
	camera:setTarget( { 0, 0, 0 } )
	camera:setUp( { 0, 1, 0 } )
	camera.mode = camera.MODES.ORBITAL
end

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

	if RL.IsKeyPressed( RL.KEY_SPACE ) then
		if camera.mode == camera.MODES.FREE then
			camera.mode = camera.MODES.FIRST_PERSON
		else
			camera.mode = camera.MODES.FREE
		end
	end
end

function RL.draw()
	RL.ClearBackground( RL.BLACK )

	camera:beginMode3D()
		RL.DrawCube( { 0, 0.5, 0 }, { 1, 1, 1 }, RL.DARKGREEN )
		RL.rlDrawRenderBatchActive()

		RL.glEnable( RL.GL_STENCIL_TEST )

		RL.glStencilFunc( RL.GL_ALWAYS, 1, 0xFF ) -- Set any stencil to 1
		RL.glStencilOp( RL.GL_KEEP, RL.GL_KEEP, RL.GL_REPLACE )
		RL.glStencilMask( 0xFF ) -- Write to stencil buffer.
		RL.rlDisableDepthMask()
		RL.glClear( RL.GL_STENCIL_BUFFER_BIT ) -- Clear stencil buffer (0 by default)

		RL.DrawPlane( { 0, 0, 0 }, { 3, 3 }, RL.DARKBLUE )
		RL.rlDrawRenderBatchActive()

		RL.glStencilFunc( RL.GL_EQUAL, 1, 0xFF ) -- Pass test if stencil value is 1.
		RL.glStencilMask( 0x00 ) -- Don't write anything to stencil buffer.
		RL.rlEnableDepthMask()

		RL.DrawCube( { 0, -0.5, 0 }, { 1, 1, 1 }, RL.ColorAlpha( RL.DARKGREEN, 0.5 ) )
		RL.rlDrawRenderBatchActive()

		RL.glDisable( RL.GL_STENCIL_TEST )
	camera:endMode3D()
end