Procházet zdrojové kódy

Update Window_HUD;

Wasn't able to get it to work easily with desktop driver
bjorn před 2 roky
rodič
revize
bf4dc7467e

+ 37 - 43
examples/UI/Window_HUD/main.lua

@@ -1,5 +1,6 @@
 -- "Second screen experience" demo
 -- Click grid on desktop screen to build a scene simultaneously visible in VR space
+-- NOTE: does not currently work properly with the desktop driver, since lovr.mirror clears the window
 --
 -- Sample contributed by andi mcc
 
@@ -12,19 +13,16 @@ if type(jit) == 'table' and lovr.system.getOS() ~= "android" then
 end
 
 local mirror = lovr.mirror              -- Backup lovr.mirror before it is overwritten
-local font = lovr.graphics.newFont(36)  -- Font appropriate for screen-space usage
-font:setFlipEnabled(true)
+local font = lovr.graphics.newFont(24)  -- Font appropriate for screen-space usage
 font:setPixelDensity(1)
 
 -- Simple 2D triangle mesh
-local triangle = lovr.graphics.newMesh(
-	{{0,-1,0, 0,0,1}, {0.75,1,0, 0,0,1}, {-0.75,1,0, 0,0,1}},
-	'triangles', 'static'
-	)
+local triangle = lovr.graphics.newBuffer(
+	{{0,-1,0}, {0.75,1,0}, {-0.75,1,0}}, {'vec3:VertexPosition'})
 
 -- Constants
-local pixwidth = lovr.graphics.getWidth()   -- Window pixel width and height
-local pixheight = lovr.graphics.getHeight()
+local pixwidth = lovr.system.getWindowWidth()   -- Window pixel width and height
+local pixheight = lovr.system.getWindowHeight()
 local aspect = pixwidth/pixheight           -- Window aspect ratio
 local height = 2                            -- Window width and height in screen coordinates
 local width = aspect*2                      -- ( We will pick the coordinate system [[-1,1],[-aspect,aspect]] )
@@ -39,7 +37,7 @@ local gridspan = gridheight/2                       -- Half height of grid
 local cellheight = gridheight/cells                 -- Height of one grid cell
 local cellspan = cellheight/2                       -- Half height of one grid cell
 local bannedcell = math.ceil(cells/2)               -- Do not allow clicks at this x,y coordinate
-local fontscale = height/lovr.graphics.getHeight()  -- Scale argument to screen-space print() functions
+local fontscale = height/pixheight  -- Scale argument to screen-space print() functions
 
 -- Screen-space coordinate system
 local matrix = lovr.math.newMat4():orthographic(-aspect, aspect, -1, 1, -64, 64)
@@ -68,7 +66,7 @@ function lovr.load()
 	end
 end
 
-function drawGrid()
+function drawGrid(pass)
 	-- Draw cell backgrounds (where present)
 	for _x=1,cells do for _y=1,cells do
 		local gray = grid[_x][_y]
@@ -76,22 +74,22 @@ function drawGrid()
 			local x = -gridspan + _x * cellheight - cellspan -- Center of cell
 			local y = -gridspan + _y * cellheight - cellspan
 
-			lovr.graphics.setColor(gray,gray,gray,1)
-			lovr.graphics.plane('fill', x, y, 0, cellheight, cellheight)
+			pass:setColor(gray,gray,gray,1)
+			pass:plane(x, y, 0, cellheight, cellheight)
 		end
 	end end
 
 	-- Draw grid lines
-	lovr.graphics.setColor(1,1,1,1)
+	pass:setColor(1,1,1,1)
 	for c=0,cells do
 		local x = -gridspan + c * cellheight
 		local y = -gridspan + c * cellheight
-		lovr.graphics.line(-gridspan, y, 0, gridspan, y, 0)
-		lovr.graphics.line(x, -gridspan, 0, x, gridspan, 0)
+		pass:line(-gridspan, y, 0, gridspan, y, 0)
+		pass:line(x, -gridspan, 0, x, gridspan, 0)
 	end
 
 	-- Draw a red triangle indicating the position and orientation of the headset player
-	lovr.graphics.push()
+	pass:push()
 	local x, y, z, angle, ax, ay, az = lovr.headset.getPose()
 	-- Flatten the 3-space current rotation of the headset into just its xz axis
 	-- Equation from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToEuler/index.htm
@@ -99,51 +97,47 @@ function drawGrid()
 	local c = math.cos(angle)
 	local t = 1-c;
 	local xzangle = math.atan2(ay*s - ax*az*t , 1 - (ay*ay + az*az) * t);
-	lovr.graphics.setColor(1,0,0,1)
-	lovr.graphics.translate(x / towerscalexz, z / towerscalexz, 0)
-	lovr.graphics.scale(cellheight*0.5*0.75)
-	lovr.graphics.rotate(-xzangle, 0, 0, 1)
-	triangle:draw()
-	lovr.graphics.pop()
+  pass:setColor(1,0,0,1)
+	pass:translate(x / towerscalexz, z / towerscalexz, 0)
+	pass:scale(cellheight*0.5*0.75)
+	pass:rotate(-xzangle, 0, 0, 1)
+  pass:mesh(triangle)
+	pass:pop()
 end
 
 -- Draw HUD overlay
-function lovr.mirror()
-	mirror()
-	--lovr.graphics.clear() -- Uncomment to hide headset view in background of window
-	lovr.graphics.setShader(nil)
-	lovr.graphics.setDepthTest(nil)
-	lovr.graphics.origin()
-  lovr.graphics.setViewPose(1, mat4())
-	lovr.graphics.setProjection(1, matrix) -- Switch to screen space coordinates
-	drawGrid()
+function lovr.mirror(pass)
+	if mirror then mirror() end
+	pass:origin()
+  pass:setViewPose(1, mat4())
+	pass:setProjection(1, matrix) -- Switch to screen space coordinates
+	drawGrid(pass)
 
 	-- Draw instructions
-	lovr.graphics.setColor(1,1,1,1)
-	lovr.graphics.setFont(font)
-	lovr.graphics.print("Instructions: Click the grid to create or remove blocks.", 0, (gridheight+cellheight)/2, 0, fontscale)
+	pass:setColor(1,1,1,1)
+	pass:setFont(font)
+	pass:text("Instructions: Click the grid to create or remove blocks.", 0, (gridheight+cellheight)/2, 0, fontscale)
 end
 
 -- Draw one block
-function floorbox(_x,_y,gray)
+function floorbox(pass,_x,_y,gray)
 	local x = -gridspan + _x * cellheight - cellspan
 	local z = -gridspan + _y * cellheight - cellspan
 	local height = gray * towerscaley
-	lovr.graphics.box('fill', x*towerscalexz, height/2, z*towerscalexz, cellheight*towerscalexz, height, cellheight*towerscalexz)
+	pass:box(x*towerscalexz, height/2, z*towerscalexz, cellheight*towerscalexz, height, cellheight*towerscalexz)
 end
 
 -- Draw 3D scene
-function lovr.draw()
-	lovr.graphics.setDepthTest('lequal', true) -- mirror() will have disabled this
-	lovr.graphics.setShader(shader)
-	lovr.graphics.setColor(0,1,1)
+function lovr.draw(pass)
+	pass:setShader(shader)
+	pass:setColor(0,1,1)
 	for x=1,cells do for y=1,cells do
 		local gray = grid[x][y]
-		if gray then floorbox(x,y,gray) end
+		if gray then floorbox(pass,x,y,gray) end
 	end end
-	lovr.graphics.setShader()
+	pass:setShader()
 
 	if not lovr.mouse then -- If you can't click, you can't create any blocks
-		lovr.graphics.print('This example only works on a desktop computer.', 0, 1.7, -3, .2)
+		pass:text('This example only works on a desktop computer.', 0, 1.7, -3, .2)
 	end
 end

+ 4 - 4
examples/UI/Window_HUD/mouse.lua

@@ -17,7 +17,7 @@ ffi.cdef [[
   typedef void(*GLFWcursorposfun)(GLFWwindow*, double, double);
   typedef void(*GLFWscrollfun)(GLFWwindow*, double, double);
 
-  GLFWwindow* glfwGetCurrentContext(void);
+  GLFWwindow* os_get_glfw_window(void);
   void glfwGetInputMode(GLFWwindow* window, int mode);
   void glfwSetInputMode(GLFWwindow* window, int mode, int value);
   void glfwGetCursorPos(GLFWwindow* window, double* x, double* y);
@@ -29,7 +29,7 @@ ffi.cdef [[
   GLFWcursorposfun glfwSetScrollCallback(GLFWwindow* window, GLFWscrollfun callback);
 ]]
 
-local window = C.glfwGetCurrentContext()
+local window = ffi.C.os_get_glfw_window()
 
 local mouse = {}
 
@@ -38,7 +38,7 @@ local mouse = {}
 function mouse.getScale()
   local x, _ = ffi.new('int[1]'), ffi.new('int[1]')
   C.glfwGetWindowSize(window, x, _)
-  return lovr.graphics.getWidth()/x[0]
+  return lovr.system.getWindowWidth()/x[0]
 end
 
 function mouse.getX()
@@ -113,4 +113,4 @@ C.glfwSetScrollCallback(window, function(target, x, y)
   end
 end)
 
-return mouse
+return mouse

+ 11 - 21
examples/UI/Window_HUD/shader.lua

@@ -1,39 +1,29 @@
 return lovr.graphics.newShader([[
-out vec3 lightDirection;
-out vec3 normalDirection;
-
-vec4 position(mat4 projection, mat4 transform, vec4 vertex) {
-  vec3 lightPosition = vec3(0., 10., 3.);
-
-  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;
+vec4 lovrmain() {
+  return DefaultPosition;
 }
 ]], [[
-in vec3 lightDirection;
-in vec3 normalDirection;
+vec4 lovrmain() {
+  vec3 lightPosition = vec3(0., 10., 3.);
+  vec3 L = normalize(lightPosition - PositionWorld);
+  vec3 N = normalize(Normal);
 
-vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) {
   vec3 cAmbient = vec3(.25);
   vec3 cDiffuse = vec3(.75);
   vec3 cSpecular = vec3(.35);
 
-  float diffuse = max(dot(normalDirection, lightDirection), 0.);
+  float diffuse = max(dot(N, L), 0.);
   float specular = 0.;
 
   if (diffuse > 0.) {
-    vec3 r = reflect(lightDirection, normalDirection);
-    vec3 viewDirection = normalize(-vec3(gl_FragCoord));
+    vec3 R = reflect(L, N);
+    vec3 V = normalize(CameraPositionWorld - PositionWorld);
 
-    float specularAngle = max(dot(r, viewDirection), 0.);
+    float specularAngle = max(dot(R, V), 0.);
     specular = pow(specularAngle, 5.);
   }
 
   vec3 cFinal = pow(clamp(vec3(diffuse) * cDiffuse + vec3(specular) * cSpecular, cAmbient, vec3(1.)), vec3(.4545));
-  return vec4(cFinal, 1.) * graphicsColor * texture(image, uv);
+  return vec4(cFinal, 1.) * Color * getPixel(ColorTexture, UV);
 }
 ]])

+ 0 - 1
examples/init.lua

@@ -44,7 +44,6 @@ return {
   'Effects/Stereo_Image',
   'Effects/Cubemap_Texturing',
   'UI/Spectator_Camera',
-  'UI/Window_HUD',
   'Optimization/Instancing',
   'Optimization/Mask',
   'Debugging/FPS_Controls',