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
|
-- Based on raylib example - Draw Textured Polygon by Chris Camacho (@codifies)
package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
Vec2 = require( "vector2" )
local monitor = 0
local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) )
local mSize = Vec2:new( RL.GetMonitorSize( monitor ) )
local winSize = Vec2:new( RL.GetScreenSize() )
local polygon = {
texture = -1,
texcoords = {
Vec2:new( 0.75, 0.0 ),
Vec2:new( 0.25, 0.0 ),
Vec2:new( 0.0, 0.5 ),
Vec2:new( 0.0, 0.75 ),
Vec2:new( 0.25, 1.0 ),
Vec2:new( 0.375, 0.875 ),
Vec2:new( 0.625, 0.875 ),
Vec2:new( 0.75, 1.0 ),
Vec2:new( 1.0, 0.75 ),
Vec2:new( 1.0, 0.5 ),
Vec2:new( 0.75, 0.0 ),
},
points = {},
positions = {},
angle = 0.0,
}
function RL.init()
RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE )
RL.SetWindowState( RL.FLAG_VSYNC_HINT )
RL.SetWindowPosition( { mPos.x + mSize.x / 2 - winSize.x / 2, mPos.y + mSize.y / 2 - winSize.y / 2 } )
RL.SetWindowTitle( "Textured Polygon" )
polygon.texture = RL.LoadTexture( RL.GetBasePath().."../resources/images/cat.png" )
-- Define the base poly vertices from the UV's
-- NOTE: They can be specified in any other way
for _, texcoord in ipairs( polygon.texcoords ) do
table.insert( polygon.points, Vec2:new( ( texcoord.x - 0.5 ) * 256, ( texcoord.y - 0.5 ) * 256 ) )
end
-- Define the vertices drawing position
-- NOTE: Initially same as points but updated every frame
for _, point in ipairs( polygon.points ) do
table.insert( polygon.positions, point:clone() )
end
end
function RL.process( delta )
polygon.angle = polygon.angle + delta
-- Update points rotation with an angle transform
-- NOTE: Base points position are not modified
for i = 1, #polygon.points do
polygon.positions[i] = Vec2:new( RL.Vector2Rotate( polygon.points[i], polygon.angle ) )
end
end
-- Draw textured polygon, defined by vertex and texture coordinates
-- NOTE: Polygon center must have straight line path to all points
-- without crossing perimeter, points must be in anticlockwise order
local function drawTexturePoly( texture, center, points, texcoords, tint )
RL.rlSetTexture( RL.GetTextureId( texture ) )
-- Texturing is only supported on RL_QUADS
RL.rlBegin( RL.RL_QUADS )
RL.rlColor4ub( tint )
for i = 1, #points - 1 do
RL.rlTexCoord2f( { 0.5, 0.5 } )
RL.rlVertex2f( center )
RL.rlTexCoord2f( texcoords[i] )
RL.rlVertex2f( center + points[i] )
RL.rlTexCoord2f( texcoords[i + 1] )
RL.rlVertex2f( center + points[i + 1] )
-- Dublicate of last vertex to complete quad.
RL.rlTexCoord2f( texcoords[i + 1] )
RL.rlVertex2f( center + points[i + 1] )
end
RL.rlEnd()
end
function RL.draw()
RL.ClearBackground( RL.RAYWHITE )
drawTexturePoly( polygon.texture, winSize:scale( 0.5 ), polygon.positions, polygon.texcoords, RL.WHITE )
end
|