start ffi matrices; resources

This commit is contained in:
wan-may 2025-03-11 23:14:39 -03:00
parent 25410647cc
commit 569bb389a9
6 changed files with 149 additions and 22 deletions

BIN
fonts/bjcrus.ttf Normal file

Binary file not shown.

11
i18n/en.lua Normal file
View File

@ -0,0 +1,11 @@
return {
intro = {
"ᐊᑯᓕᕕᒃ",
"1968",
"BROTHER JARED has led his flock beyond darkest Thule",
"to conjure ZIMINIAR, KING OF THE NORTH.",
"You are the latest to attempt the ritual.",
"Succeed, and be crowned Brother Jared's foremost wife.",
"You are at the mercy of ZIMINIAR, and the elements he commands."
}
}

View File

@ -1,38 +1,53 @@
local love = assert( love )
local mat4 = require( "mat4" )
local alph = 1/math.sqrt(2)
local testMesh = love.graphics.newMesh(
{--attributes
{"VertexPosition", "float", 4},
{"VertexColor", "float", 3}
{"VertexColor", "float", 3},
{"VertexTexCoord", "float", 2},
},
{--vertices
{ 0, 0, 0, 1,
1, 1, 1 },
{ 1, 0, 0, 1,
1, 0, 0 },
{ 0, 1, 0, 1,
0, 1, 0 },
{ 0, 0, 1, 1,
0, 0, 1 },
{ 1, 0, -alph, 1,
1, 1, 1,
1, 0 },
{ -1, 0, -alph, 1,
1, 0, 0,
0, 0 },
{ 0, 1, alph, 1,
0, 1, 0,
1, 1, },
{ 0, -1, alph, 1,
0, 0, 1,
0, 1, },
},
"strip",
"static"
)
testMesh:setVertexMap{ 1,2,3,4,1,2 }
local rockTexture = love.graphics.newImage( "tex/rock.png" )
rockTexture:setWrap( "repeat", "repeat" )
testMesh:setTexture( rockTexture )
local testQuad = love.graphics.newMesh(
{--attributes
{"VertexPosition", "float", 4},
{"VertexColor", "float", 3}
{"VertexColor", "float", 3},
{"VertexTexCoord", "float", 2},
},
{--vertices
{ -1, -1,- 2, 1,
1, 1, 1 },
1, 1, 1,
0, 0, },
{ 1, -1,- 2, 1,
1, 0, 0 },
1, 0, 0,
1, 0, },
{ 1, 1,- 2, 1,
0, 0, 1 },
0, 0, 1,
1, 1, },
{ -1, 1,- 2, 1,
0, 1, 0 },
0, 1, 0,
0, 1, },
},
"fan",
"static"
@ -52,13 +67,13 @@ local testShader = [[
#endif
#ifdef PIXEL
vec4 effect( vec4 color, Image tex, vec2 texuv, vec2 scruv) {
return color * vec4( 1.0, 1.0, 1.0, 1.0 ) ;
return color * Texel(tex, texuv) * vec4( 1.0, 1.0, 1.0, 1.0 ) ;
}
#endif
]]
testShader = love.graphics.newShader( testShader, testShader )
--TODO: column major
--column major
local function prod( A, B )
local C = {}
for j = 1,4 do C[j] = {0, 0, 0, 0} end
@ -162,12 +177,14 @@ local testDSCanvas = love.graphics.newCanvas(
format = "depth24" })
local testCanvas = love.graphics.newCanvas()
local creeFont = love.graphics.newFont( "fonts/bjcrus.ttf", 48 )
--row major
--4th column [:][4] translates
--diagonal [i][i], i < 4 scales
--rotations in [1:3][1:3]
function love.draw()
love.graphics.setColor( 1, 1 ,1, 1 )
love.graphics.push( "all" )
love.graphics.setCanvas( { testCanvas, depthstencil = testDSCanvas } )
@ -177,10 +194,19 @@ function love.draw()
testShader:send( "mdl", "column", testModelMatrix )
testShader:send( "proj", "column", testProjection )
testShader:send( "view", "column", testViewMatrix )
love.graphics.draw( testMesh )
--love.graphics.draw( testMesh )
math.randomseed( 4 )
for i = 1, 15 do
local x, y, z = 3 * ( math.random() - 0.5 ), 3 * ( math.random() - 0.5 )
testShader:send( "mdl", "column",
prod( trans( x, y, -8 ),
rotate( 0, time, math.random() )))
love.graphics.draw( testMesh )
end
testShader:send( "mdl", "column", id() )
--love.graphics.draw( testQuad )
-- love.graphics.draw( testQuad )
love.graphics.setShader()
love.graphics.setColor(1,1,1, 0.5 )
@ -189,7 +215,12 @@ function love.draw()
love.graphics.setDepthMode( "always", true )
love.graphics.pop()
love.graphics.draw( testCanvas )
love.graphics.print( string.format( "%05.2f\t:TIME\n%05.2f\t:ANGLE", time, time ))
local str = require( "i18n.en" )
love.graphics.print( string.format( "%05.2f\t:TIME\n%05.2f\t:ANGLE\n", time, time))
love.graphics.print( { {1, 0.2, 0.2}, str.intro1},
creeFont,
0.5 * ( love.graphics.getWidth() - creeFont:getWidth(str.intro[1]) ),
0.5 * ( love.graphics.getHeight() - creeFont:getHeight() ))
--[[love.graphics.setShader()
love.graphics.setColor( 1,1,1,1 )
love.graphics.circle( "fill", time, time/2, 5 )]]
@ -204,6 +235,5 @@ function love.update( dt )
testModelMatrix = prod(
testModelMatrix,
rotate( time, time, time )
)
end
end

86
mat4.lua Normal file
View File

@ -0,0 +1,86 @@
--column major!
local love = assert( love )
local ffi = require('ffi')
local cmat4 = ffi.typeof( [[struct{ float x[4][4]; }]] )
--this is actually the argument we need to send to shader:send
local data = love.data.newByteData( ffi.sizeof( cmat4 ) * 1024 )
print( "size", data:getSize() )
local t = {}
t.__index = t
ffi.metatype( cmat4, t )
function t.__tostring( m )
return ([[
|%05.2f %05.2f %05.2f %05.2f|
|%05.2f %05.2f %05.2f %05.2f|
|%05.2f %05.2f %05.2f %05.2f|
|%05.2f %05.2f %05.2f %05.2f|]]):format(
m.x[0][0], m.x[1][0], m.x[2][0], m.x[3][0],
m.x[0][1], m.x[1][1], m.x[2][1], m.x[3][1],
m.x[0][2], m.x[1][2], m.x[2][2], m.x[3][2],
m.x[0][3], m.x[1][3], m.x[2][3], m.x[3][3])
end
function t.__mul( A, B )
local C = ffi.gc(cmat4(),nil)
for j = 1, 4 do
for i = 1, 4 do
C.x[j][i] =
A.x[1][i] * B.x[j][1] +
A.x[2][i] * B.x[j][2] +
A.x[3][i] * B.x[j][3] +
A.x[4][i] * B.x[j][4]
end
end
return C
end
function t.__add( A, B )
local C = cmat4()
for i = 1, 4 do
for j = 1, 4 do
C[i][j] = A.x[i][j] + B.x[i][j]
end
end
return C
end
function t:rotate( a, b, g )
local c, s = math.cos, math.sin
local ca, sa, cb, sb, cg, sg = c(a), s(a), c(b), s(b), c(g), s(g)
return cmat4(
ca*cb, sa*cb, -sb, 0,
ca*sb*sg - sa*cg, sa*sb*sg + ca*cg, cb*sg, 0,
ca*sb*cg + sa*sg, sa*sb*cg - ca*sg, cb*cg, 0,
0, 0, 0, 1
) * self
end
function t:scale( x, y, z )
return cmat4(
x, 0, 0, 0,
0, y, 0, 0,
0, 0, z, 0,
0, 0, 0, 1
) * self
end
function t:translate( x, y, z )
local a = cmat4()
a.x[0][0], a.x[1][1], a.x[2][2], a.x[3][3] = 1, 1, 1, 1
a.x[3][0], a.x[3][1], a.x[3][2] = x, y, z
return a * self
end
do --tests
print( cmat4() ); io.flush()
local a = ffi.gc(cmat4(), nil)
a.x[0][0], a.x[1][1], a.x[2][2],a.x[3][3] = 1,1,1,1
print( a ); io.flush()
print( a:translate( 1, 2, 3 ) );
io.flush()
end
return cmat4

BIN
tex/rock.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 371 KiB

0
view.lua Normal file
View File