Advertisement
DIXERION

Rotating Cube

Feb 19th, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.75 KB | Source Code | 0 0
  1. local Mesh = love.graphics.newMesh (
  2.     {
  3.         {"VertexPosition", "float", 3},
  4.         {"VertexColor", "byte", 4}
  5.     }, {
  6.         {0, 0, 0, love.math.gammaToLinear (0.6, 0.6, 0.6, 1.0)},
  7.         {1, 0, 0, love.math.gammaToLinear (1.0, 0.6, 0.6, 1.0)},
  8.         {0, 1, 0, love.math.gammaToLinear (0.6, 1.0, 0.6, 1.0)},
  9.         {1, 1, 0, love.math.gammaToLinear (1.0, 1.0, 0.6, 1.0)},
  10.         {0, 0, 1, love.math.gammaToLinear (0.6, 0.6, 1.0, 1.0)},
  11.         {1, 0, 1, love.math.gammaToLinear (1.0, 0.6, 1.0, 1.0)},
  12.         {0, 1, 1, love.math.gammaToLinear (0.6, 1.0, 1.0, 1.0)},
  13.         {1, 1, 1, love.math.gammaToLinear (1.0, 1.0, 1.0, 1.0)}
  14.     }, "triangles"
  15. )
  16.  
  17. local Shader = love.graphics.newShader ([[
  18.     #pragma language glsl3
  19.     //
  20.     uniform vec3 ObjectTranslation;
  21.     uniform mat3 ObjectTransform;
  22.     //
  23.     uniform mat4 CameraProjection;
  24.     //
  25.     varying vec3 FragmentPosition;
  26.     //
  27.     vec4 position (mat4, vec4 P) {
  28.         vec3 Q = (vec3 (TransformMatrix*P) - 0.5)*ObjectTransform + ObjectTranslation;
  29.         vec4 R = vec4 (Q, 1.0)*CameraProjection;
  30.         //
  31.         FragmentPosition = Q;
  32.         //
  33.         return vec4 (R.x, -R.y, R.z*2.0 - R.w, R.w);
  34.     }
  35. ]], [[
  36.     #pragma language glsl3
  37.     //
  38.     const vec3 LightPosition = vec3 (2.0, -1.0, 1.0);
  39.     //
  40.     varying vec3 FragmentPosition;
  41.     //
  42.     uniform Image MainTex;
  43.     //
  44.     void effect () {
  45.         float D = distance (FragmentPosition, LightPosition);
  46.         float L = 1.2/(D*D);
  47.         //
  48.         vec4 C = vec4 (1.0);
  49.         //
  50.         C *= vec4 (vec3 (L), 1.0);
  51.         C *= VaryingColor;
  52.         C *= gammaToLinear (Texel (MainTex, vec2 (VaryingTexCoord)));
  53.         //
  54.         love_PixelColor = linearToGamma (C);
  55.     }
  56. ]])
  57.  
  58. --
  59.  
  60. Mesh:setVertexMap (
  61.     2, 4, 8, 2, 8, 6,
  62.     1, 5, 7, 1, 7, 3,
  63.     3, 7, 8, 3, 8, 4,
  64.     1, 2, 6, 1, 6, 5,
  65.     5, 6, 8, 5, 8, 7,
  66.     1, 3, 4, 1, 4, 2
  67. )
  68.  
  69. --
  70.  
  71. local function viewScale (VF, VR)
  72.     local VM = math.tan (VF/2)/math.sqrt (VR*VR + 1)
  73.     --
  74.     return {VR*VM*2, VM*2}
  75. end
  76.  
  77. local function projectionMatrix (VS, VO, DR)
  78.     local VD = DR [2]/(DR [2] - DR [1])
  79.     --
  80.     return {VO [1], VO [2], VD, 1, -2/VS [1], 0, 0, 0, 0, -2/VS [2], 0, 0, 0, 0, -DR [1]*VD, 0}
  81. end
  82.  
  83. local function rotate (R)
  84.     local D = R [1]*R [1] + R [2]*R [2] + R [3]*R [3]
  85.     --
  86.     if D < 1e-9 then return {1, 0, 0, 0, 1, 0, 0, 0, 1} end
  87.     --
  88.     local A = math.sqrt (D)
  89.     local N = {R [1]/A, R [2]/A, R [3]/A}
  90.     --
  91.     local C = math.cos (A)
  92.     local S = math.sin (A)
  93.     --
  94.     local O = 1 - C
  95.     --
  96.     return {
  97.         N [1]*N [1]*O + C, N [1]*N [2]*O + N [3]*S, N [1]*N [3]*O - N [2]*S,
  98.         N [2]*N [1]*O - N [3]*S, N [2]*N [2]*O + C, N [2]*N [3]*O + N [1]*S,
  99.         N [3]*N [1]*O + N [2]*S, N [3]*N [2]*O - N [1]*S, N [3]*N [3]*O + C
  100.     }
  101. end
  102.  
  103. local function multiply (A, B)
  104.     return {
  105.         A [1]*B [1] + A [2]*B [4] + A [3]*B [7],
  106.         A [1]*B [2] + A [2]*B [5] + A [3]*B [8],
  107.         A [1]*B [3] + A [2]*B [6] + A [3]*B [9],
  108.         --
  109.         A [4]*B [1] + A [5]*B [4] + A [6]*B [7],
  110.         A [4]*B [2] + A [5]*B [5] + A [6]*B [8],
  111.         A [4]*B [3] + A [5]*B [6] + A [6]*B [9],
  112.         --
  113.         A [7]*B [1] + A [8]*B [4] + A [9]*B [7],
  114.         A [7]*B [2] + A [8]*B [5] + A [9]*B [8],
  115.         A [7]*B [3] + A [8]*B [6] + A [9]*B [9]
  116.     }
  117. end
  118.  
  119. local function smooth (A, B, F, DT)
  120.     local R = F*DT*math.pi*2
  121.     --
  122.     return A + (B - A)/(R + 1)*R
  123. end
  124.  
  125. --
  126.  
  127. local Position = {{3, 0, 0}, {3, 0, 0}}
  128.  
  129. local Torque = {0, 0, 0}
  130.  
  131. local Rotation = {1, 0, 0, 0, 1, 0, 0, 0, 1}
  132.  
  133. --
  134.  
  135. function love.load ()
  136.     love.graphics.setBackgroundColor (0.1, 0.1, 0.1)
  137. end
  138.  
  139. function love.keypressed (Button)
  140.     if Button == "escape" then love.event.quit () end
  141. end
  142.  
  143. function love.mousemoved (MX, MY, DX, DY)
  144.     if not love.mouse.isDown (1) then return end
  145.     --
  146.     Torque [2] = Torque [2] - DY*0.13
  147.     Torque [3] = Torque [3] + DX*0.13
  148. end
  149.  
  150. function love.wheelmoved (DX, DY)
  151.     Position [1] [1] = math.min (math.max (Position [1] [1] - DY*0.25, 2), 8)
  152. end
  153.  
  154. function love.update (DT)
  155.     Position [2] [1] = smooth (Position [2] [1], Position [1] [1], 2, DT)
  156.     Position [2] [2] = smooth (Position [2] [2], Position [1] [2], 2, DT)
  157.     Position [2] [3] = smooth (Position [2] [3], Position [1] [3], 2, DT)
  158.     --
  159.     local DC = love.mouse.isDown (1) and 2 or 0.1
  160.     --
  161.     Torque [1] = smooth (Torque [1], 0, DC, DT)
  162.     Torque [2] = smooth (Torque [2], 0, DC, DT)
  163.     Torque [3] = smooth (Torque [3], 0, DC, DT)
  164.     --
  165.     Rotation = multiply (Rotation, rotate ({Torque [1]*DT, Torque [2]*DT, Torque [3]*DT}))
  166. end
  167.  
  168. function love.draw ()
  169.     local TE = love.timer.getTime ()
  170.     local VS = viewScale (math.rad (100), love.graphics.getWidth ()/love.graphics.getHeight ())
  171.     --
  172.     Shader:send ("ObjectTranslation", Position [2])
  173.     Shader:send ("ObjectTransform", Rotation)
  174.     Shader:send ("CameraProjection", projectionMatrix (VS, {0, 0}, {0.1, 100}))
  175.     --
  176.     love.graphics.push ("all")
  177.     love.graphics.setShader (Shader)
  178.     love.graphics.setBlendMode ("alpha", "alphamultiply")
  179.     love.graphics.setDepthMode ("lequal", true)
  180.     love.graphics.setMeshCullMode ("back")
  181.     love.graphics.draw (Mesh)
  182.     love.graphics.pop ()
  183. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement