From 9bcab00b4d7765fc21055b1151091c929e36c939 Mon Sep 17 00:00:00 2001 From: wan-may Date: Sun, 9 Mar 2025 21:16:01 -0300 Subject: [PATCH] day one progress: render two models --- main.lua | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 main.lua diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..4b834fd --- /dev/null +++ b/main.lua @@ -0,0 +1,146 @@ +local love = assert( love ) +local testMesh = love.graphics.newMesh( + {--attributes + {"VertexPosition", "float", 4}, + {"VertexColor", "float", 3} + }, + {--vertices + { 0, 0, 0, 1, + 0, 0, 0 }, + { 1, 0, 0, 1, + 1, 0, 0 }, + { 0, 1, 0, 1, + 0, 1, 0 }, + { 0, 0, 1, 1, + 0, 0, 1 }, + }, + "strip", + "static" +) +testMesh:setVertexMap{ 1,2,3,4,1,2 } + +local testQuad = love.graphics.newMesh( + {--attributes + {"VertexPosition", "float", 4}, + {"VertexColor", "float", 3} + }, + {--vertices + { -1, -1,- 2, 1, + 1, 1, 1 }, + { 1, -1,- 2, 1, + 1, 0, 0 }, + { 1, 1,- 2, 1, + 0, 0, 1 }, + { -1, 1,- 2, 1, + 0, 1, 0 }, + }, + "fan", + "static" +) + +local testShader = [[ +#ifdef VERTEX + uniform mat4 mdl; + uniform mat4 view; + uniform mat4 proj; + vec4 position( mat4 _, vec4 pos ){ + return proj*view*mdl*pos; + } +#endif +#ifdef PIXEL + vec4 effect( vec4 color, Image tex, vec2 texuv, vec2 scruv) { + return color * vec4( 1., 1., 1., 0.5 ); + } +#endif +]] +testShader = love.graphics.newShader( testShader, testShader ) + +--fov in degrees, aspect w/h, near far +local function getProjectionMatrix(n, f) + local r = love.graphics.getWidth() / 2 + local t = love.graphics.getHeight() / 2 + return --[[{ + {n/r, 0, 0, 0}, + {0, n/t, 0, 0}, + {0, 0, -(f+n)/(f-n), -2 * f * n / (f-n) }, + {0, 0, -1, 0}, + }]] + { {1, 0, 0, 0}, + {0,-1, 0, 0}, + {0, 0, -1, -2*n}, + {0, 0, -1, 0}, + } +end + +local function getViewMatrix() + return { + {1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 1}, + } +end + +local testID = { + {1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 1}, + } + + +local testModelMatrix = { + {1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 1, -2}, + {0, 0, 0, 1}, + } +local testViewMatrix = getViewMatrix() +local testProjection = getProjectionMatrix( 0.2, 10 ) +local time = 0 + +local testDSCanvas = love.graphics.newCanvas( + love.graphics.getWidth(), + love.graphics.getHeight(), + { readable = true, + format = "depth24" }) + +local testCanvas = love.graphics.newCanvas() + +--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 } ) + love.graphics.clear() + love.graphics.setDepthMode( "less", true ) + love.graphics.setShader( testShader ) + testShader:send( "mdl", "row", testModelMatrix ) + testShader:send( "proj", "row", testProjection ) + testShader:send( "view", "row", testViewMatrix ) + love.graphics.draw( testMesh ) + + testShader:send( "mdl", "column", testID ) + love.graphics.draw( testQuad ) + + love.graphics.setShader() + love.graphics.draw( testQuad ) + love.graphics.setColor(1,1,1, 0.5 ) + -- love.graphics.rectangle( "fill", 0, 0, love.graphics.getDimensions() ) + love.graphics.setCanvas() + 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, (180 / math.pi) * time / 15 ) ) + --[[love.graphics.setShader() + love.graphics.setColor( 1,1,1,1 ) + love.graphics.circle( "fill", time, time/2, 5 )]] +end + +function love.update( dt ) + time = time + 10 * dt + testModelMatrix[3][4] = -2.0 - math.sin( time / 5 ) +end \ No newline at end of file