Browse Source

Simple instanced mesh

mcc 7 years ago
parent
commit
4c8b2a3ab5
2 changed files with 50 additions and 22 deletions
  1. 48 5
      examples/Mesh/main.lua
  2. 2 17
      examples/Mesh/shader.lua

+ 48 - 5
examples/Mesh/main.lua

@@ -1,12 +1,43 @@
-local shader = require("shader")
+local fragmentShader = require("shader")
 
-local mesh1, mesh2, mesh3, mesh4
+local mesh1, mesh2
+local mesh4instance
+local mesh1program, mesh3program, mesh4program
+local gridSize = 8
+
+-- Call this function with a string containing a glsl function preTransform()
+-- Which maps world space coordinates to world space coordinates.
+local function makeShader(prefix)
+  return lovr.graphics.newShader(prefix .. [[
+out vec3 lightDirection;
+out vec3 normalDirection;
+
+vec3 lightPosition = vec3(0, 10, 3);
+
+vec4 position(mat4 projection, mat4 transform, vec4 _vertex) {
+  vec4 vertex = preTransform(_vertex);
+
+  vec4 vVertex = transform * vertex;
+  vec4 vLight = lovrView * vec4(lightPosition, 1.);
+
+  lightDirection = normalize(vec3(vLight - vVertex));
+  normalDirection = normalize(lovrNormalMatrix * lovrNormal);
+
+  return projection * transform * vertex;
+}
+]], fragmentShader)
+end
+
+-- This "standard" program is the same as the standard light shader from the other examples-- it does nothing.
+local mesh1program = makeShader("vec4 preTransform(vec4 v) { return v; }")
 
 local rotate = 0
 
 function lovr.load()
   lovr.graphics.setCullingEnabled(true)
 
+--  mesh1program()
+
   mesh1 = lovr.graphics.newMesh({{ 'lovrPosition', 'float', 3 }, { 'lovrNormal', 'float', 3 }}, 3)
   mesh1:setVertices({{0,0,0, 0,0,1}, {1,0,0, 0,0,1}, {0,1,0, 0,0,1}}) -- A triangle
 
@@ -52,6 +83,16 @@ function lovr.load()
     21, 22, 23, 21, 23, 24, -- Face bottom
   })
 
+  mesh3program = makeShader([[
+  uniform int gridSize;
+  vec4 preTransform(vec4 v) {
+    int x = gl_InstanceID % gridSize;
+    int y = (gl_InstanceID / gridSize) % gridSize;
+    int z = (gl_InstanceID / gridSize) / gridSize;
+    return v/vec4(2,2,2,1) + vec4(x,y,z,1);
+  }
+  ]])
+  mesh3program:send("gridSize", gridSize)
   --local vertexData = lovr.data.newVertexData(3, {{ 'position', 'float', 3 }, { 'normal', 'float', 3 }})
   --vertexData:setVertices({{0,0,0}, {0,1,0}, {1,0,0}})
 --  mesh:attachAttribute(instancingMesh, 'instancedPosition') -- NYI, attaches an attribute from a different mesh onto this mesh
@@ -63,7 +104,7 @@ function lovr.update(dt)
 end
 
 function lovr.draw(eye)
-  lovr.graphics.setShader(shader)
+  lovr.graphics.setShader(mesh1program)
   lovr.graphics.setColor(1,1,1,1)
   lovr.graphics.rotate(rotate, 0, 1, 0)
 
@@ -78,13 +119,15 @@ function lovr.draw(eye)
   mesh2:draw(0,0,0)
   lovr.graphics.pop()
 
+  lovr.graphics.setShader(mesh3program)
   lovr.graphics.push()
   lovr.graphics.rotate(2 * math.pi/2, 0, 1, 0)
   lovr.graphics.translate(0, 0, -2)
-  lovr.graphics.cube('fill', 0,0,0)
-  --mesh1:draw(0,0,0)
+  lovr.graphics.scale(2/gridSize)
+  mesh2:drawInstanced(gridSize*gridSize*gridSize, 0,0,0)
   lovr.graphics.pop()
 
+  lovr.graphics.setShader(mesh1program)
   lovr.graphics.push()
   lovr.graphics.rotate(3 * math.pi/2, 0, 1, 0)
   lovr.graphics.translate(0, 0, -2)

+ 2 - 17
examples/Mesh/shader.lua

@@ -1,19 +1,4 @@
-return lovr.graphics.newShader([[
-out vec3 lightDirection;
-out vec3 normalDirection;
-
-vec3 lightPosition = vec3(0, 10, 3);
-
-vec4 position(mat4 projection, mat4 transform, vec4 vertex) {
-  vec4 vVertex = transform * vec4(lovrPosition, 1.);
-  vec4 vLight = lovrView * vec4(lightPosition, 1.);
-
-  lightDirection = normalize(vec3(vLight - vVertex));
-  normalDirection = normalize(lovrNormalMatrix * lovrNormal);
-
-  return projection * transform * vertex;
-}
-]], [[
+return [[
 in vec3 lightDirection;
 in vec3 normalDirection;
 
@@ -36,4 +21,4 @@ vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
   vec3 cFinal = pow(clamp(vec3(diffuse) * cDiffuse + vec3(specular) * cSpecular, cAmbient, vec3(1.)), vec3(.4545));
   return vec4(cFinal, 1.) * graphicsColor * texture(image, uv);
 }
-]])
+]]