forked from stackgl/glsl-lighting-walkthrough
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-torus.js
64 lines (53 loc) · 1.58 KB
/
create-torus.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
/*
Creates a new 3D torus with its own shader and vertex buffers.
*/
var createGeometry = require('gl-geometry')
var createShader = require('gl-shader')
var createTorus = require('torus-mesh')
var mat4 = require('gl-mat4')
//our phong shader for the brick torus
var glslify = require('glslify')
var vert = glslify('./shaders/phong.vert')
var frag = glslify('./shaders/phong.frag')
module.exports = function(gl) {
var complex = createTorus({
majorSegments: 64,
minorSegments: 64
})
//enable derivatives for face normals
var ext = gl.getExtension('OES_standard_derivatives')
if (!ext)
throw new Error('derivatives not supported')
//create our shader
var shader = createShader(gl, vert, frag)
//create a geometry with some vertex attributes
var geom = createGeometry(gl)
.attr('position', complex.positions)
.attr('normal', complex.normals)
.attr('uv', complex.uvs, { size: 2 })
.faces(complex.cells)
//our model-space transformations
var model = mat4.create()
var mesh = {
draw: draw,
light: null,
flatShading: false,
}
return mesh
function draw(camera) {
//set our uniforms for the shader
shader.bind()
shader.uniforms.projection = camera.projection
shader.uniforms.view = camera.view
shader.uniforms.model = model
shader.uniforms.flatShading = mesh.flatShading ? 1 : 0
shader.uniforms.light = mesh.light
shader.uniforms.texDiffuse = 0
shader.uniforms.texNormal = 1
shader.uniforms.texSpecular = 2
//draw the mesh
geom.bind(shader)
geom.draw(gl.TRIANGLES)
geom.unbind()
}
}