gdritter repos animaltransiro / d558512
Starting to work on normal map stuff Getty Ritter 6 years ago
10 changed file(s) with 95 addition(s) and 24 deletion(s). Collapse all Expand all
11 local consts = require 'constants'
22
3 local normalMapper = love.graphics.newShader
4 [[
5 extern Image tex_nrm;
6 extern float lightX;
7 extern float lightY;
8
9 vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
10 vec4 light_dir = normalize(vec4(lightX, lightY, 1.0, 1.0));
11 // vec4 light_dir = normalize(light_pos - vec4(texture_coords, 0.0, 0.0));
12
13 vec2 r_coords = floor((texture_coords * 2048) + 0.5) / 2048;
14 vec4 pixel = Texel(texture, r_coords);
15 vec4 normal = Texel(tex_nrm, r_coords);
16
17 float l = max(dot(light_dir, normalize(normal)), 0.0);
18 vec3 col = vec3(pixel) * pow(l, 4.0);
19 return vec4(col, 1.0);
20 }
21 ]]
22
323 function drawAll(state)
24
425 -- create a canvas to blit to
526 love.graphics.setCanvas(state.canvas)
627
829 for x = 0, consts.boardWidth - 1 do
930 for y = 0, consts.boardHeight - 1 do
1031 if state.board:lookup(x, y) then
11 state.board:lookup(x, y):draw(x, y, state.t)
32 love.graphics.draw(state.spritesheet,
33 state.board:lookup(x, y):draw(x, y, state.t))
1234 end
1335 end
1436 end
1941 -- if an entity is in this space, draw it
2042 local e = state.board:lookupEntity(x, y)
2143 if e and e[1] and e[1].behind then
22 e[1]:draw(x, y, state.t)
44 love.graphics.draw(state.spritesheet,
45 e[1]:draw(x, y, state.t))
2346 end
2447 end
2548 end
3154 -- XXX: extend this to mobile entities in general
3255 local sX, sY = state.char:gameTopCoords()
3356 if sX == x and sY == y then
34 state.char:draw(state.t)
57 love.graphics.draw(state.spritesheet,
58 state.char:draw(state.t))
3559 end
3660
3761 -- if an entity is in this space, draw it
3862 local es = state.board:lookupEntity(x, y) or {}
3963 for i, e in pairs(es) do
4064 if e and not e.behind then
41 e:draw(x, y, state.t)
65 love.graphics.draw(state.spritesheet,
66 e:draw(x, y, state.t))
4267 end
4368 end
4469
5883 love.graphics.print(state.message, 12, h + 12)
5984 end
6085
86 -- now the same, but with normals
87 love.graphics.setCanvas(state.normals)
88
89 -- draw the board background
90 for x = 0, consts.boardWidth - 1 do
91 for y = 0, consts.boardHeight - 1 do
92 if state.board:lookup(x, y) then
93 love.graphics.draw(state.spriteNormals,
94 state.board:lookup(x, y):draw(x, y, state.t))
95 end
96 end
97 end
98
99 -- draw the entities
100 for x = 0, consts.boardWidth - 1 do
101 for y = 0, consts.boardHeight - 1 do
102 -- if an entity is in this space, draw it
103 local e = state.board:lookupEntity(x, y)
104 if e and e[1] and e[1].behind then
105 love.graphics.draw(state.spriteNormals,
106 e[1]:draw(x, y, state.t))
107 end
108 end
109 end
110
111 -- draw the entities
112 for x = 0, consts.boardWidth - 1 do
113 for y = 0, consts.boardHeight - 1 do
114 -- if the character is in this space, draw it first
115 -- XXX: extend this to mobile entities in general
116 local sX, sY = state.char:gameTopCoords()
117 if sX == x and sY == y then
118 love.graphics.draw(state.spriteNormals,
119 state.char:draw(state.t))
120 end
121
122 -- if an entity is in this space, draw it
123 local es = state.board:lookupEntity(x, y) or {}
124 for i, e in pairs(es) do
125 if e and not e.behind then
126 love.graphics.draw(state.spriteNormals,
127 e:draw(x, y, state.t))
128 end
129 end
130
131 end
132 end
133
61134 -- print the debug message
62135 -- love.graphics.setColor(255, 255, 255)
63136 -- love.graphics.print(debug, 8, 8)
71144
72145 love.graphics.setColor(255, 255, 255)
73146
147 love.graphics.setShader(normalMapper)
148 normalMapper:send('tex_nrm', state.normals)
149 normalMapper:send('lightX', (state.t % 500) / 500.0)
150 normalMapper:send('lightY', 1.0)
74151 -- blit the smaller canvas back onto the larger screen
75152 love.graphics.draw(state.canvas, 0, 0, 0, 2, 2)
153 love.graphics.setShader()
76154
77155 end
78156
156156
157157 function Entity:draw(t)
158158 if self.dx == 0 and self.dy == 0 then
159 self.sprites[self.facing]:drawPx(self.x, self.y, t)
159 return self.sprites[self.facing]:drawPx(self.x, self.y, t)
160160 else
161 self.sprites[self.facing .. 'Moving']:drawPx(self.x, self.y, t)
161 return self.sprites[self.facing .. 'Moving']:drawPx(self.x, self.y, t)
162162 end
163163 end
164164
1111 return
1212 end
1313
14 local state = {
14 state = {
1515 loadMap = function(self, name)
1616 self.board = board.loadBoardFromTiled('areas/' .. name .. '.lua')
1717 end,
3636 state.canvas = love.graphics.newCanvas(
3737 consts.boardWidth * consts.tileSize,
3838 consts.boardHeight * consts.tileSize)
39 state.normals = love.graphics.newCanvas(
40 consts.boardWidth * consts.tileSize,
41 consts.boardHeight * consts.tileSize)
3942 state.canvas:setFilter('nearest', 'nearest')
43 state.normals:setFilter('nearest', 'nearest')
44 state.spritesheet = love.graphics.newImage('tiles/spritesheet.png')
45 state.spriteNormals = love.graphics.newImage('tiles/spritesheet-normal.png')
4046
4147 love.window.setMode(consts.tileSize * consts.boardWidth * 2,
4248 consts.tileSize * consts.boardHeight * 2)
8282 function tileAnimation(self, x, y, t)
8383 local tM = math.floor(t / 40) % self.totalFrames
8484
85 love.graphics.draw(
86 spritesheet,
87 self.quads[tM + 1],
88 x * consts.tileSize,
89 y * consts.tileSize)
85 return self.quads[tM + 1], x * consts.tileSize, y * consts.tileSize
9086 end
9187
9288 function tileAnimationPx(self, x, y, t)
9389 local tM = math.floor(t / 8) % self.totalFrames
9490
95 love.graphics.draw(
96 spritesheet,
97 self.quads[tM + 1],
98 x, y)
91 return self.quads[tM + 1], x, y
9992 end
10093
10194 function Tile:draw(x, y)
102 love.graphics.draw(spritesheet,
103 self.quad,
104 x * consts.tileSize,
105 y * consts.tileSize)
95 return self.quad, x * consts.tileSize, y * consts.tileSize
10696 end
10797
10898 function Tile:drawPx(x, y)
109 love.graphics.draw(spritesheet,
110 self.quad,
111 x,
112 y)
99 return self.quad, x, y
113100 end
114101
115102 local tileCache = {}
Binary diff not shown
Binary diff not shown
Binary diff not shown
Binary diff not shown