-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (171 loc) · 5.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const wgl = require('./lib')
const glmatrix = require('gl-matrix')
const vec3 = glmatrix.vec3
const Broker = require('./broker')
const Triangle = require('./triangle')
const FirstPersonCamera = require('./first-person-camera')
const ColoredCube = require('./colored-cube')
const TexturedCube = require('./textured-cube')
const Terrain = require('./terrain')
const Skybox = require('./skybox')
const Water = require('./water')
const Square = require('./square')
window.onload = () => {
wgl('view', new Game())
}
function Heightmap(content,
assetName,
sizeFactor,
heightFactor,
waterHeight,
textures) {
this.getAssetName = () => assetName
this.getSizeFactor = () => sizeFactor
this.getHeightFactor = () => heightFactor
this.getTextures = () => textures
this.getPng = () => content.resources[this.getAssetName()].content
this.getWidth = () => this.getPng(content).getWidth()*this.getSizeFactor()
this.getHeight = () => this.getPng(content).getHeight()*this.getSizeFactor()
this.getWaterHeight = () => waterHeight*this.getHeightFactor()
}
function Environment(lightPosition,
ambientLight,
heightmap) {
this.getLightPosition = () => lightPosition
this.getAmbientLight = () => ambientLight
this.getHeightmap = () => heightmap
}
function Game() {
this.objects = []
this.config = {
shaders: {
'colored-cube': { vs: 'fx/colored-cube-vs.glsl', fs: 'fx/colored-cube-fs.glsl' },
'textured-cube': { vs: 'fx/textured-cube-vs.glsl', fs: 'fx/textured-cube-fs.glsl' },
'colored-triangle': { vs: 'fx/colored-triangle-vs.glsl', fs: 'fx/colored-triangle-fs.glsl' },
'terrain': { vs: 'fx/terrain-vs.glsl', fs: 'fx/terrain-fs.glsl' },
'skybox': { vs: 'fx/skybox-vs.glsl', fs: 'fx/skybox-fs.glsl' },
'water': { vs: 'fx/water-vs.glsl', fs: 'fx/water-fs.glsl' },
'square': { vs: 'fx/square-vs.glsl', fs: 'fx/square-fs.glsl' }
},
resources: {
'metal-box': {
src: 'img/metal_box.jpg',
type: 'img'
},
'water-bump-map': {
src: 'img/water_bump_map.png',
type: 'img'
},
'sand': {
src: 'img/sand.jpg',
type: 'img'
},
'grass': {
src: 'img/grass.jpg',
type: 'img'
},
'rock': {
src: 'img/rock.jpg',
type: 'img'
},
'snow': {
src: 'img/snow.jpg',
type: 'img'
},
'heightmap': {
src: 'img/heightmap_4.png',
type: 'heightmap'
},
'skybox_xn': {
src: 'img/skybox_2_xn.png',
type: 'img'
},
'skybox_xp': {
src: 'img/skybox_2_xp.png',
type: 'img'
},
'skybox_zn': {
src: 'img/skybox_2_zn.png',
type: 'img'
},
'skybox_zp': {
src: 'img/skybox_2_zp.png',
type: 'img'
},
'skybox_yn': {
src: 'img/skybox_2_yn.png',
type: 'img'
},
'skybox_yp': {
src: 'img/skybox_2_yp.png',
type: 'img'
}
}
}
const rgb = [1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0]
const broker = new Broker()
let camera = null
let environment = null
let terrain = null
let skybox = null
this.initialize = (context, content) => {
environment = new Environment(vec3.normalize(vec3.create(), vec3.fromValues(1.0, 0.3, -1.0)),
vec3.fromValues(1.0, 0.549, 0.0),
new Heightmap(content,
'heightmap',
1.0,
0.1,
-55, {
sand: 'sand',
grass: 'grass',
rock: 'rock',
snow: 'snow'
}))
camera = new FirstPersonCamera(vec3.fromValues(30.2, 0.0, -50.4),
vec3.fromValues(0.0, 0.0, -51.0),
environment,
1.8)
this.objects.push(camera)
skybox = new Skybox(camera, 1.0, environment)
this.objects.push(skybox)
this.objects.push(new ColoredCube(camera,
environment,
0.5,
vec3.fromValues(0.0, 0.0, -1.0)))
this.objects.push(new TexturedCube(camera,
environment,
1.3,
vec3.fromValues(0.55, -0.8, -2.0),
'metal-box'))
terrain = new Terrain(camera, environment, broker)
broker.addSubscription(camera, 'getHeightAtCameraPosition')
this.objects.push(new Water(camera,
environment,
terrain,
'water-bump-map'))
this.objects.push(terrain)
this.objects.push(new Square(camera, environment, 15))
}
this.update = (context, time) => {
}
this.draw = (context, time) => {
context.enable(context.CULL_FACE);
context.cullFace(context.BACK);
context.enable(context.DEPTH_TEST)
context.depthFunc(context.LEQUAL)
context.clearColor(0.0, 0.0, 0.0, 1.0)
context.viewport(0, 0, context.drawingBufferWidth, context.drawingBufferHeight)
terrain.usingRefractionBuffer(context, context => {
context.clear(context.COLOR_BUFFER_BIT | context.DEPTH_BUFFER_BIT)
terrain.drawRefraction(context, time)
})
terrain.usingReflectionBuffer(context, context => {
context.clear(context.COLOR_BUFFER_BIT | context.DEPTH_BUFFER_BIT)
skybox.draw(context, time)
terrain.drawReflection(context, time)
})
context.clear(context.COLOR_BUFFER_BIT | context.DEPTH_BUFFER_BIT)
}
}