Starting to work on normal map stuff
Getty Ritter
7 years ago
1 | 1 | local consts = require 'constants' |
2 | 2 | |
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 | ||
3 | 23 | function drawAll(state) |
24 | ||
4 | 25 | -- create a canvas to blit to |
5 | 26 | love.graphics.setCanvas(state.canvas) |
6 | 27 | |
8 | 29 | for x = 0, consts.boardWidth - 1 do |
9 | 30 | for y = 0, consts.boardHeight - 1 do |
10 | 31 | if state.board:lookup(x, y) then |
11 |
|
|
32 | love.graphics.draw(state.spritesheet, | |
33 | state.board:lookup(x, y):draw(x, y, state.t)) | |
12 | 34 | end |
13 | 35 | end |
14 | 36 | end |
19 | 41 | -- if an entity is in this space, draw it |
20 | 42 | local e = state.board:lookupEntity(x, y) |
21 | 43 | if e and e[1] and e[1].behind then |
22 |
|
|
44 | love.graphics.draw(state.spritesheet, | |
45 | e[1]:draw(x, y, state.t)) | |
23 | 46 | end |
24 | 47 | end |
25 | 48 | end |
31 | 54 | -- XXX: extend this to mobile entities in general |
32 | 55 | local sX, sY = state.char:gameTopCoords() |
33 | 56 | if sX == x and sY == y then |
34 |
|
|
57 | love.graphics.draw(state.spritesheet, | |
58 | state.char:draw(state.t)) | |
35 | 59 | end |
36 | 60 | |
37 | 61 | -- if an entity is in this space, draw it |
38 | 62 | local es = state.board:lookupEntity(x, y) or {} |
39 | 63 | for i, e in pairs(es) do |
40 | 64 | if e and not e.behind then |
41 |
|
|
65 | love.graphics.draw(state.spritesheet, | |
66 | e:draw(x, y, state.t)) | |
42 | 67 | end |
43 | 68 | end |
44 | 69 | |
58 | 83 | love.graphics.print(state.message, 12, h + 12) |
59 | 84 | end |
60 | 85 | |
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 | ||
61 | 134 | -- print the debug message |
62 | 135 | -- love.graphics.setColor(255, 255, 255) |
63 | 136 | -- love.graphics.print(debug, 8, 8) |
71 | 144 | |
72 | 145 | love.graphics.setColor(255, 255, 255) |
73 | 146 | |
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) | |
74 | 151 | -- blit the smaller canvas back onto the larger screen |
75 | 152 | love.graphics.draw(state.canvas, 0, 0, 0, 2, 2) |
153 | love.graphics.setShader() | |
76 | 154 | |
77 | 155 | end |
78 | 156 |
156 | 156 | |
157 | 157 | function Entity:draw(t) |
158 | 158 | if self.dx == 0 and self.dy == 0 then |
159 |
|
|
159 | return self.sprites[self.facing]:drawPx(self.x, self.y, t) | |
160 | 160 | else |
161 |
|
|
161 | return self.sprites[self.facing .. 'Moving']:drawPx(self.x, self.y, t) | |
162 | 162 | end |
163 | 163 | end |
164 | 164 |
11 | 11 | return |
12 | 12 | end |
13 | 13 | |
14 |
|
|
14 | state = { | |
15 | 15 | loadMap = function(self, name) |
16 | 16 | self.board = board.loadBoardFromTiled('areas/' .. name .. '.lua') |
17 | 17 | end, |
36 | 36 | state.canvas = love.graphics.newCanvas( |
37 | 37 | consts.boardWidth * consts.tileSize, |
38 | 38 | consts.boardHeight * consts.tileSize) |
39 | state.normals = love.graphics.newCanvas( | |
40 | consts.boardWidth * consts.tileSize, | |
41 | consts.boardHeight * consts.tileSize) | |
39 | 42 | 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') | |
40 | 46 | |
41 | 47 | love.window.setMode(consts.tileSize * consts.boardWidth * 2, |
42 | 48 | consts.tileSize * consts.boardHeight * 2) |
82 | 82 | function tileAnimation(self, x, y, t) |
83 | 83 | local tM = math.floor(t / 40) % self.totalFrames |
84 | 84 | |
85 | love.graphics.draw( | |
86 | spritesheet, | |
87 | self.quads[tM + 1], | |
88 | x * consts.tileSize, | |
89 |
|
|
85 | return self.quads[tM + 1], x * consts.tileSize, y * consts.tileSize | |
90 | 86 | end |
91 | 87 | |
92 | 88 | function tileAnimationPx(self, x, y, t) |
93 | 89 | local tM = math.floor(t / 8) % self.totalFrames |
94 | 90 | |
95 | love.graphics.draw( | |
96 | spritesheet, | |
97 | self.quads[tM + 1], | |
98 | x, y) | |
91 | return self.quads[tM + 1], x, y | |
99 | 92 | end |
100 | 93 | |
101 | 94 | 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 | |
106 | 96 | end |
107 | 97 | |
108 | 98 | function Tile:drawPx(x, y) |
109 | love.graphics.draw(spritesheet, | |
110 | self.quad, | |
111 | x, | |
112 | y) | |
99 | return self.quad, x, y | |
113 | 100 | end |
114 | 101 | |
115 | 102 | local tileCache = {} |
Binary diff not shown
Binary diff not shown
Binary diff not shown
Binary diff not shown
Binary diff not shown
Binary diff not shown