├── Appendix
├── ColoredTriangle.frag
├── ColoredTriangle.js
├── ColoredTriangle.vert
├── CoordinateSystem.html
├── CoordinateSystem.js
├── CoordinateSystem_viewVolume.html
├── CoordinateSystem_viewVolume.js
├── LoadShaderFromFiles.html
└── LoadShaderFromFiles.js
├── ch02
├── ClickedPoints.html
├── ClickedPoints.js
├── ColoredPoints.html
├── ColoredPoints.js
├── DrawRectangle.html
├── DrawRectangle.js
├── HelloCanvas.html
├── HelloCanvas.js
├── HelloPoint1.html
├── HelloPoint1.js
├── HelloPoint2.html
└── HelloPoint2.js
├── ch03
├── HelloQuad.html
├── HelloQuad.js
├── HelloQuad_FAN.html
├── HelloQuad_FAN.js
├── HelloTriangle.html
├── HelloTriangle.js
├── HelloTriangle_LINES.html
├── HelloTriangle_LINES.js
├── HelloTriangle_LINE_LOOP.html
├── HelloTriangle_LINE_LOOP.js
├── HelloTriangle_LINE_STRIP.html
├── HelloTriangle_LINE_STRIP.js
├── MultiPoint.html
├── MultiPoint.js
├── RotatedTriangle.html
├── RotatedTriangle.js
├── RotatedTriangle_Matrix.html
├── RotatedTriangle_Matrix.js
├── ScaledTriangle_Matrix.html
├── ScaledTriangle_Matrix.js
├── TranslatedTriangle.html
└── TranslatedTriangle.js
├── ch04
├── RotatedTranslatedTriangle.html
├── RotatedTranslatedTriangle.js
├── RotatedTriangle_Matrix4.html
├── RotatedTriangle_Matrix4.js
├── RotatingTranslatedTriangle.html
├── RotatingTranslatedTriangle.js
├── RotatingTriangle.html
├── RotatingTriangle.js
├── RotatingTriangle_withButtons.html
├── RotatingTriangle_withButtons.js
├── TranslatedRotatedTriangle.html
└── TranslatedRotatedTriangle.js
├── ch05
├── ColoredTriangle.html
├── ColoredTriangle.js
├── HelloTriangle_FragCoord.html
├── HelloTriangle_FragCoord.js
├── MultiAttributeColor.html
├── MultiAttributeColor.js
├── MultiAttributeSize.html
├── MultiAttributeSize.js
├── MultiAttributeSize_Interleaved.html
├── MultiAttributeSize_Interleaved.js
├── MultiTexture.html
├── MultiTexture.js
├── TexturedQuad.html
├── TexturedQuad.js
├── TexturedQuad_Clamp_Mirror.html
├── TexturedQuad_Clamp_Mirror.js
├── TexturedQuad_Repeat.html
└── TexturedQuad_Repeat.js
├── ch07
├── ColoredCube.html
├── ColoredCube.js
├── ColoredCube_singleColor.html
├── ColoredCube_singleColor.js
├── DepthBuffer.html
├── DepthBuffer.js
├── HelloCube.html
├── HelloCube.js
├── HelloCube_singleColor.html
├── HelloCube_singleColor.js
├── LookAtRotatedTriangles.html
├── LookAtRotatedTriangles.js
├── LookAtRotatedTriangles_mvMatrix.html
├── LookAtRotatedTriangles_mvMatrix.js
├── LookAtTriangles.html
├── LookAtTriangles.js
├── LookAtTrianglesWithKeys.html
├── LookAtTrianglesWithKeys.js
├── LookAtTrianglesWithKeys_ViewVolume.html
├── LookAtTrianglesWithKeys_ViewVolume.js
├── OrthoView.html
├── OrthoView.js
├── OrthoView_halfSize.html
├── OrthoView_halfSize.js
├── OrthoView_halfWidth.html
├── OrthoView_halfWidth.js
├── PerspectiveView.html
├── PerspectiveView.js
├── PerspectiveView_mvp.html
├── PerspectiveView_mvp.js
├── PerspectiveView_mvpMatrix.html
├── PerspectiveView_mvpMatrix.js
├── Zfighting.html
└── Zfighting.js
├── ch08
├── LightedCube.html
├── LightedCube.js
├── LightedCube_ambient.html
├── LightedCube_ambient.js
├── LightedCube_animation.html
├── LightedCube_animation.js
├── LightedCube_perFragment.html
├── LightedCube_perFragment.js
├── LightedTranslatedRotatedCube.html
├── LightedTranslatedRotatedCube.js
├── PointLightedCube.html
├── PointLightedCube.js
├── PointLightedCube_animation.html
├── PointLightedCube_animation.js
├── PointLightedCube_perFragment.html
├── PointLightedCube_perFragment.js
├── PointLightedSphere.html
├── PointLightedSphere.js
├── PointLightedSphere_perFragment.html
└── PointLightedSphere_perFragment.js
├── ch09
├── JointModel.html
├── JointModel.js
├── MultiJointModel.html
├── MultiJointModel.js
├── MultiJointModel_segment.html
└── MultiJointModel_segment.js
├── ch10
├── 3DoverWeb.html
├── 3DoverWeb.js
├── BlendedCube.html
├── BlendedCube.js
├── Fog.html
├── Fog.js
├── Fog_w.html
├── Fog_w.js
├── FramebufferObject.html
├── FramebufferObject.js
├── HUD.html
├── HUD.js
├── LookAtBlendedTriangles.html
├── LookAtBlendedTriangles.js
├── MQOParser.js
├── OBJViewer.html
├── OBJViewer.js
├── PickFace.html
├── PickFace.js
├── PickObject.html
├── PickObject.js
├── Picking.html
├── Picking.js
├── ProgramObject.html
├── ProgramObject.js
├── RotateObject.html
├── RotateObject.js
├── RotatingTriangle_contextLost.html
├── RotatingTriangle_contextLost.js
├── RoundedPoints.html
├── RoundedPoints.js
├── Shadow.html
├── Shadow.js
├── Shadow_highp.html
├── Shadow_highp.js
├── Shadow_highp_sphere.html
├── Shadow_highp_sphere.js
├── cube.mtl
└── cube.obj
├── gifts
├── Particle.html
├── Particle.js
├── Printf.html
├── Printf.js
├── SpecularCube.html
├── SpecularCube.js
├── TextTexture.html
├── TextTexture.js
├── ThreeDUI.html
├── ThreeDUI.js
├── Wave.html
├── Wave.js
├── WorldCoordinate.html
└── WorldCoordinate.js
├── lib
├── cuon-matrix.js
├── cuon-utils.js
├── webgl-debug.js
└── webgl-utils.js
└── resources
├── 3objects.mqo
├── 7herbs.JPG
├── blueflower.JPG
├── blueflower2.JPG
├── circle.gif
├── cube.mqo
├── cube.mtl
├── cube.obj
├── kaeru.mqo
├── lightblueflower.JPG
├── numbers.png
├── orange.JPG
├── parasol.jpg
├── particle.png
├── pinkflower.JPG
├── redflower.jpg
├── sky.JPG
├── sky_cloud.jpg
├── sky_roof.JPG
├── sphere.mqo
├── vase.mqo
└── yellowflower.jpg
/Appendix/ColoredTriangle.frag:
--------------------------------------------------------------------------------
1 | #ifdef GL_ES
2 | precision mediump float;
3 | #endif GL_ES
4 | varying vec4 v_Color;
5 | void main() {
6 | gl_FragColor = v_Color;
7 | }
8 |
9 |
--------------------------------------------------------------------------------
/Appendix/ColoredTriangle.js:
--------------------------------------------------------------------------------
1 | // ColoredTriangle.js (c) 2012 matsuda
2 | // Vertex shader program
3 | var VSHADER_SOURCE =
4 | 'attribute vec4 a_Position;\n' +
5 | 'attribute vec4 a_Color;\n' +
6 | 'varying vec4 v_Color;\n' +
7 | 'void main() {\n' +
8 | ' gl_Position = a_Position;\n' +
9 | ' v_Color = a_Color;\n' +
10 | '}\n';
11 |
12 | // Fragment shader program
13 | var FSHADER_SOURCE =
14 | '#ifdef GL_ES\n' +
15 | 'precision mediump float;\n' +
16 | '#endif GL_ES\n' +
17 | 'varying vec4 v_Color;\n' +
18 | 'void main() {\n' +
19 | ' gl_FragColor = v_Color;\n' +
20 | '}\n';
21 |
22 | function main() {
23 | // Retrieve
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/ch10/Fog_w.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Fog(w)
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch10/FramebufferObject.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Frame Buffer Object
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch10/HUD.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | HUD
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/ch10/LookAtBlendedTriangles.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Alpha blending
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch10/OBJViewer.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | OBJ Viewer
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch10/PickFace.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Pick face
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch10/PickObject.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Pick Object
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch10/Picking.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Pick face
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | Please use a browser that supports "canvas"
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch10/ProgramObject.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Switch Shaders
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch10/RotateObject.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Rotate Object
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch10/RotatingTriangle_contextLost.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Continue to rotate the triangle (context lost event correspondence)
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch10/RotatingTriangle_contextLost.js:
--------------------------------------------------------------------------------
1 | // RotatingTriangle_contextLost.js (c) 2012 matsuda
2 | // Vertex shader program
3 | var VSHADER_SOURCE =
4 | 'attribute vec4 a_Position;\n' +
5 | 'uniform mat4 u_ModelMatrix;\n' +
6 | 'void main() {\n' +
7 | ' gl_Position = u_ModelMatrix * a_Position;\n' +
8 | '}\n';
9 |
10 | // Fragment shader program
11 | var FSHADER_SOURCE =
12 | 'void main() {\n' +
13 | ' gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +
14 | '}\n';
15 |
16 | function main() {
17 | // Retrieve element
18 | var canvas = document.getElementById('webgl');
19 |
20 | // Register event handler for context lost and context restored events
21 | canvas.addEventListener('webglcontextlost', contextLost, false);
22 | canvas.addEventListener('webglcontextrestored', function(ev) { start(canvas); }, false);
23 |
24 | start(canvas); // Perform WebGL related processes
25 | }
26 |
27 | // Current rotation angle
28 | var ANGLE_STEP = 45.0;
29 | // Current rotation angle
30 | var g_currentAngle = 0.0; // Changed from local variable to global variable
31 | var g_requestID; // The return value of requestAnimationFrame()
32 |
33 | function start(canvas) {
34 | // Get the rendering context for WebGL
35 | var gl = getWebGLContext(canvas);
36 | if (!gl) {
37 | console.log('Failed to get the rendering context for WebGL');
38 | return;
39 | }
40 |
41 | // Initialize shaders
42 | if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
43 | console.log('Failed to intialize shaders.');
44 | return;
45 | }
46 |
47 | var n = initVertexBuffers(gl); // Write the positions of vertices to a vertex shader
48 | if (n < 0) {
49 | console.log('Failed to set the positions of the vertices');
50 | return;
51 | }
52 |
53 | gl.clearColor(0.0, 0.0, 0.0, 1.0); // Specify the color for clearing
54 |
55 | // Get storage location of u_ModelMatrix
56 | var u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix');
57 | if (!u_ModelMatrix) {
58 | console.log('Failed to get the storage location of u_ModelMatrix');
59 | return;
60 | }
61 |
62 | var modelMatrix = new Matrix4(); // Create a model matrix
63 |
64 | var tick = function() { // Start drawing
65 | g_currentAngle = animate(g_currentAngle); // Update current rotation angle
66 | draw(gl, n, g_currentAngle, modelMatrix, u_ModelMatrix); // Draw the triangle
67 | g_requestID = requestAnimationFrame(tick, canvas); // Reregister this Function again
68 | };
69 | tick();
70 | }
71 |
72 | function contextLost(ev) { // Event Handler for context lost event
73 | cancelAnimationFrame(g_requestID); // Stop animation
74 | ev.preventDefault(); // Prevent the default behavior
75 | }
76 |
77 | function initVertexBuffers(gl) {
78 | var vertices = new Float32Array ([
79 | 0.0, 0.5, -0.5, -0.5, 0.5, -0.5
80 | ]);
81 | var n = 3; // The number of vertices
82 |
83 | // Create a buffer object
84 | var vertexBuffer = gl.createBuffer();
85 | if (!vertexBuffer) {
86 | console.log('Failed to create the buffer object');
87 | return -1;
88 | }
89 |
90 | // Bind the buffer object to target
91 | gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
92 | // Write date into the buffer object
93 | gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
94 |
95 | // Assign the buffer object to a_Position variable
96 | var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
97 | if(a_Position < 0) {
98 | console.log('Failed to get the storage location of a_Position');
99 | return -1;
100 | }
101 | gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
102 |
103 | // Enable the assignment to a_Position variable
104 | gl.enableVertexAttribArray(a_Position);
105 |
106 | // Unbind the buffer object
107 | gl.bindBuffer(gl.ARRAY_BUFFER, null);
108 |
109 | return n;
110 | }
111 |
112 | function draw(gl, n, currentAngle, modelMatrix, u_ModelMatrix) {
113 | // Set the rotation matrix
114 | modelMatrix.setRotate(currentAngle, 0, 0, 1);
115 |
116 | // Pass the rotation matrix to the vertex shader
117 | gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements);
118 |
119 | // Clear
120 | gl.clear(gl.COLOR_BUFFER_BIT);
121 |
122 | // Draw the rectangle
123 | gl.drawArrays(gl.TRIANGLES, 0, n);
124 | }
125 |
126 | // Last time that this function was called
127 | var g_last = Date.now();
128 | function animate(angle) {
129 | // Calculate the elapsed time
130 | var now = Date.now();
131 | var elapsed = now - g_last;
132 | g_last = now;
133 | // Update the current rotation angle (adjusted by the elapsed time)
134 | var newAngle = angle + (ANGLE_STEP * elapsed) / 1000.0;
135 | return newAngle %= 360;
136 | }
137 |
--------------------------------------------------------------------------------
/ch10/RoundedPoints.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Round Point
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/ch10/RoundedPoints.js:
--------------------------------------------------------------------------------
1 | // RoundedPoints.js (c) 2012 matsuda
2 | // Vertex shader program
3 | var VSHADER_SOURCE =
4 | 'attribute vec4 a_Position;\n' +
5 | 'void main() {\n' +
6 | ' gl_Position = a_Position;\n' +
7 | ' gl_PointSize = 10.0;\n' +
8 | '}\n';
9 |
10 | // Fragment shader program
11 | var FSHADER_SOURCE =
12 | '#ifdef GL_ES\n' +
13 | 'precision mediump float;\n' +
14 | '#endif GL_ES\n' +
15 | 'void main() {\n' + // Center coordinate is (0.5, 0.5)
16 | ' float d = distance(gl_PointCoord, vec2(0.5, 0.5));\n' +
17 | ' if(d < 0.5) {\n' + // Radius is 0.5
18 | ' gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +
19 | ' } else { discard; }\n' +
20 | '}\n';
21 |
22 | function main() {
23 | // Retrieve element
24 | var canvas = document.getElementById('webgl');
25 |
26 | // Get the rendering context for WebGL
27 | var gl = getWebGLContext(canvas);
28 | if (!gl) {
29 | console.log('Failed to get the rendering context for WebGL');
30 | return;
31 | }
32 |
33 | // Initialize shaders
34 | if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
35 | console.log('Failed to intialize shaders.');
36 | return;
37 | }
38 |
39 | // Set the vertex information
40 | var n = initVertexBuffers(gl);
41 | if (n < 0) {
42 | console.log('Failed to set the vertex information');
43 | return;
44 | }
45 |
46 | // Specify the color for clearing
47 | gl.clearColor(0, 0, 0, 1);
48 |
49 | // Clear
50 | gl.clear(gl.COLOR_BUFFER_BIT);
51 |
52 | // Draw three points
53 | gl.drawArrays(gl.POINTS, 0, n);
54 | }
55 |
56 | function initVertexBuffers(gl) {
57 | var vertices = new Float32Array([
58 | 0, 0.5, -0.5, -0.5, 0.5, -0.5
59 | ]);
60 | var n = 3; // The number of vertices
61 |
62 | // Create a buffer object
63 | var vertexBuffer = gl.createBuffer();
64 | if (!vertexBuffer) {
65 | console.log('Failed to create the buffer object');
66 | return -1;
67 | }
68 |
69 | // Bind the vertex buffer
70 | gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
71 | // Write date into the buffer object
72 | gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
73 |
74 | // Assign the buffer object to the attribute variable
75 | var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
76 | if (a_Position < 0) {
77 | console.log('Failed to get the storage location of a_Position');
78 | return -1;
79 | }
80 | gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
81 |
82 | // Unbind the buffer object
83 | gl.bindBuffer(gl.ARRAY_BUFFER, null);
84 |
85 | // Enable the assignment to a_Position variable
86 | gl.enableVertexAttribArray(a_Position);
87 |
88 | return n;
89 | }
90 |
--------------------------------------------------------------------------------
/ch10/Shadow.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Shadow
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch10/Shadow_highp.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Shadow (high-precision version)
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch10/Shadow_highp_sphere.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Shadow (high-precision version)
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/ch10/cube.mtl:
--------------------------------------------------------------------------------
1 | # Blender MTL File: ''
2 | # Material Count: 2
3 | newmtl Material
4 | Ka 0.000000 0.000000 0.000000
5 | Kd 1.000000 0.000000 0.000000
6 | Ks 0.000000 0.000000 0.000000
7 | Ns 96.078431
8 | Ni 1.000000
9 | d 1.000000
10 | illum 0
11 | newmtl Material.001
12 | Ka 0.000000 0.000000 0.000000
13 | Kd 1.000000 0.450000 0.000000
14 | Ks 0.000000 0.000000 0.000000
15 | Ns 96.078431
16 | Ni 1.000000
17 | d 1.000000
18 | illum 0
19 |
--------------------------------------------------------------------------------
/ch10/cube.obj:
--------------------------------------------------------------------------------
1 | # Blender v2.60 (sub 0) OBJ File: ''
2 | # www.blender.org
3 | mtllib cube.mtl
4 | o Cube
5 | v 1.000000 -1.000000 -1.000000
6 | v 1.000000 -1.000000 1.000000
7 | v -1.000000 -1.000000 1.000000
8 | v -1.000000 -1.000000 -1.000000
9 | v 1.000000 1.000000 -1.000000
10 | v 1.000000 1.000000 1.000001
11 | v -1.000000 1.000000 1.000000
12 | v -1.000000 1.000000 -1.000000
13 | usemtl Material
14 | f 1 2 3 4
15 | f 5 8 7 6
16 | f 2 6 7 3
17 | f 3 7 8 4
18 | f 5 1 4 8
19 | usemtl Material.001
20 | f 1 5 6 2
21 |
--------------------------------------------------------------------------------
/gifts/Particle.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Particle
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/gifts/Printf.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Display the value in fragment shader
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/gifts/SpecularCube.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Lighting demo (specular)
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/gifts/TextTexture.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Drawing text using "canvas"
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/gifts/ThreeDUI.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 3DUI
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/gifts/Wave.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Wave demo
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/gifts/WorldCoordinate.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | World Coordinate example
6 |
7 |
8 |
9 |
10 | Please use a browser that supports "canvas"
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/gifts/WorldCoordinate.js:
--------------------------------------------------------------------------------
1 | // WorldCoordinateSystem.js (c) matsuda
2 | // Vertex shader program
3 | var VSHADER_SOURCE =
4 | 'attribute vec4 a_Position;\n' +
5 | 'attribute vec4 a_Color;\n' +
6 | 'uniform mat4 u_mvpMatrix;\n' +
7 | 'varying vec4 v_Color;\n' +
8 | 'void main() {\n' +
9 | ' gl_Position = u_mvpMatrix * a_Position;\n' +
10 | ' v_Color = a_Color;\n' +
11 | '}\n';
12 |
13 | // Fragment shader program
14 | var FSHADER_SOURCE =
15 | '#ifdef GL_ES\n' +
16 | 'precision mediump float;\n' +
17 | '#endif\n' +
18 | 'varying vec4 v_Color;\n' +
19 | 'void main() {\n' +
20 | ' gl_FragColor = v_Color;\n' +
21 | '}\n';
22 |
23 | function main() {
24 | // Retrieve element
25 | var canvas = document.getElementById('webgl');
26 |
27 | // Get the rendering context for WebGL
28 | var gl = getWebGLContext(canvas);
29 | if (!gl) {
30 | console.log('Failed to get the rendering context for WebGL');
31 | return;
32 | }
33 |
34 | // Initialize shaders
35 | if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
36 | console.log('Failed to intialize shaders.');
37 | return;
38 | }
39 |
40 | // Set the vertex information
41 | var n = initVertexBuffers(gl);
42 | if (n < 0) {
43 | console.log('Failed to set the vertex information');
44 | return;
45 | }
46 |
47 | // Set the clear color and enable the depth test
48 | gl.clearColor(0, 0, 0, 1);
49 | gl.enable(gl.DEPTH_TEST);
50 |
51 | // Get the storage locations of uniform variables
52 | var u_mvpMatrix = gl.getUniformLocation(gl.program, 'u_mvpMatrix');
53 | if (!u_mvpMatrix) {
54 | console.log('Failed to get the storage location of uniform variable');
55 | return;
56 | }
57 |
58 | var modelMatrix = new Matrix4();
59 | var viewMatrix = new Matrix4();
60 | var projMatrix = new Matrix4();
61 | var mvpMatrix = new Matrix4();
62 |
63 | // Calculate the view projection matrix
64 | viewMatrix.setLookAt(0, 0, 5, 0, 0, -100, 0, 1, 0);
65 | projMatrix.setPerspective(30, canvas.width/canvas.height, 1, 100);
66 |
67 | // Clear
68 | gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
69 |
70 | for (var i = 0; i < 10; i++) {
71 | var x = Math.random() * Math.pow(-1, i);
72 | var z = Math.random() * i * 5;
73 | modelMatrix.setTranslate(x, 0, -z);
74 | // Calculate a model view projection matrix
75 | mvpMatrix.set(projMatrix).multiply(viewMatrix).multiply(modelMatrix);
76 | // Set the matrix to u_mvpMatrix
77 | gl.uniformMatrix4fv(u_mvpMatrix, false, mvpMatrix.elements);
78 |
79 | gl.drawArrays(gl.TRIANGLES, 0, n);
80 | }
81 | }
82 |
83 | function initVertexBuffers(gl) {
84 | var verticesColors = new Float32Array([
85 | // vertex color
86 | 0.0, 1.0, 0.0, 0.0, 0.0, 1.0,
87 | -0.5, -1.0, 0.0, 0.0, 0.0, 1.0,
88 | 0.5, -1.0, 0.0, 0.0, 1.0, 0.0,
89 | ]);
90 | var n = 3;
91 |
92 | // Create a buffer object
93 | var vertexColorbuffer = gl.createBuffer();
94 | if (!vertexColorbuffer) {
95 | console.log('Failed to create the buffer object');
96 | return -1;
97 | }
98 |
99 | gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorbuffer);
100 | gl.bufferData(gl.ARRAY_BUFFER, verticesColors, gl.STATIC_DRAW);
101 |
102 | var FSIZE = verticesColors.BYTES_PER_ELEMENT;
103 |
104 | var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
105 | if(a_Position < 0) {
106 | console.log('Failed to get the storage location of a_Position');
107 | return -1;
108 | }
109 | gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, FSIZE * 6, 0);
110 | gl.enableVertexAttribArray(a_Position);
111 |
112 | var a_Color = gl.getAttribLocation(gl.program, 'a_Color');
113 | if(a_Color < 0) {
114 | console.log('Failed to get the storage location of a_Color');
115 | return -1;
116 | }
117 | gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, FSIZE * 6, FSIZE * 3);
118 | gl.enableVertexAttribArray(a_Color);
119 |
120 | return n;
121 | }
122 |
--------------------------------------------------------------------------------
/lib/cuon-utils.js:
--------------------------------------------------------------------------------
1 | // cuon-utils.js (c) 2012 kanda and matsuda
2 | /**
3 | * Create a program object and make current
4 | * @param gl GL context
5 | * @param vshader a vertex shader program (string)
6 | * @param fshader a fragment shader program (string)
7 | * @return true, if the program object was created and successfully made current
8 | */
9 | function initShaders(gl, vshader, fshader) {
10 | var program = createProgram(gl, vshader, fshader);
11 | if (!program) {
12 | console.log('Failed to create program');
13 | return false;
14 | }
15 |
16 | gl.useProgram(program);
17 | gl.program = program;
18 |
19 | return true;
20 | }
21 |
22 | /**
23 | * Create the linked program object
24 | * @param gl GL context
25 | * @param vshader a vertex shader program (string)
26 | * @param fshader a fragment shader program (string)
27 | * @return created program object, or null if the creation has failed
28 | */
29 | function createProgram(gl, vshader, fshader) {
30 | // Create shader object
31 | var vertexShader = loadShader(gl, gl.VERTEX_SHADER, vshader);
32 | var fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fshader);
33 | if (!vertexShader || !fragmentShader) {
34 | return null;
35 | }
36 |
37 | // Create a program object
38 | var program = gl.createProgram();
39 | if (!program) {
40 | return null;
41 | }
42 |
43 | // Attach the shader objects
44 | gl.attachShader(program, vertexShader);
45 | gl.attachShader(program, fragmentShader);
46 |
47 | // Link the program object
48 | gl.linkProgram(program);
49 |
50 | // Check the result of linking
51 | var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
52 | if (!linked) {
53 | var error = gl.getProgramInfoLog(program);
54 | console.log('Failed to link program: ' + error);
55 | gl.deleteProgram(program);
56 | gl.deleteShader(fragmentShader);
57 | gl.deleteShader(vertexShader);
58 | return null;
59 | }
60 | return program;
61 | }
62 |
63 | /**
64 | * Create a shader object
65 | * @param gl GL context
66 | * @param type the type of the shader object to be created
67 | * @param source shader program (string)
68 | * @return created shader object, or null if the creation has failed.
69 | */
70 | function loadShader(gl, type, source) {
71 | // Create shader object
72 | var shader = gl.createShader(type);
73 | if (shader == null) {
74 | console.log('unable to create shader');
75 | return null;
76 | }
77 |
78 | // Set the shader program
79 | gl.shaderSource(shader, source);
80 |
81 | // Compile the shader
82 | gl.compileShader(shader);
83 |
84 | // Check the result of compilation
85 | var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
86 | if (!compiled) {
87 | var error = gl.getShaderInfoLog(shader);
88 | console.log('Failed to compile shader: ' + error);
89 | gl.deleteShader(shader);
90 | return null;
91 | }
92 |
93 | return shader;
94 | }
95 |
96 | /**
97 | * Initialize and get the rendering for WebGL
98 | * @param canvas element
99 | * @param opt_debug flag to initialize the context for debugging
100 | * @return the rendering context for WebGL
101 | */
102 | function getWebGLContext(canvas, opt_debug) {
103 | // Get the rendering context for WebGL
104 | var gl = WebGLUtils.setupWebGL(canvas);
105 | if (!gl) return null;
106 |
107 | // if opt_debug is explicitly false, create the context for debugging
108 | if (arguments.length < 2 || opt_debug) {
109 | gl = WebGLDebugUtils.makeDebugContext(gl);
110 | }
111 |
112 | return gl;
113 | }
114 |
--------------------------------------------------------------------------------
/resources/7herbs.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/7herbs.JPG
--------------------------------------------------------------------------------
/resources/blueflower.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/blueflower.JPG
--------------------------------------------------------------------------------
/resources/blueflower2.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/blueflower2.JPG
--------------------------------------------------------------------------------
/resources/circle.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/circle.gif
--------------------------------------------------------------------------------
/resources/cube.mqo:
--------------------------------------------------------------------------------
1 | Metasequoia Document
2 | Format Text Ver 1.0
3 |
4 | Scene {
5 | pos 0.0000 0.0000 1500.0000
6 | lookat 0.0000 0.0000 0.0000
7 | head 3.7864
8 | pich 0.2392
9 | ortho 0
10 | zoom2 5.0000
11 | amb 0.250 0.250 0.250
12 | }
13 | Material 2 {
14 | "mat1" col(1.000 0.000 0.000 1.000) dif(0.800) amb(0.600) emi(0.000) spc(0.000) power(5.00)
15 | "mat2" col(1.000 0.647 0.000 1.000) dif(0.800) amb(0.600) emi(0.000) spc(0.000) power(5.00)
16 | }
17 | Object "obj1" {
18 | visible 15
19 | locking 0
20 | shading 0
21 | facet 59.5
22 | color 0.898 0.498 0.698
23 | color_type 0
24 | vertex 8 {
25 | -100.0000 100.0000 100.0000
26 | -100.0000 -100.0000 100.0000
27 | 100.0000 100.0000 100.0000
28 | 100.0000 -100.0000 100.0000
29 | 100.0000 100.0000 -100.0000
30 | 100.0000 -100.0000 -100.0000
31 | -100.0000 100.0000 -100.0000
32 | -100.0000 -100.0000 -100.0000
33 | }
34 | face 6 {
35 | 4 V(0 2 3 1) M(0) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)
36 | 4 V(2 4 5 3) M(1) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)
37 | 4 V(4 6 7 5) M(0) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)
38 | 4 V(6 0 1 7) M(0) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)
39 | 4 V(6 4 2 0) M(0) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)
40 | 4 V(1 3 5 7) M(0) UV(0.00000 0.00000 1.00000 0.00000 1.00000 1.00000 0.00000 1.00000)
41 | }
42 | }
43 | Eof
44 |
--------------------------------------------------------------------------------
/resources/cube.mtl:
--------------------------------------------------------------------------------
1 | # Blender MTL File: ''
2 | # Material Count: 2
3 | newmtl Material
4 | Ka 0.000000 0.000000 0.000000
5 | Kd 1.000000 0.000000 0.000000
6 | Ks 0.000000 0.000000 0.000000
7 | Ns 96.078431
8 | Ni 1.000000
9 | d 1.000000
10 | illum 0
11 | newmtl Material.001
12 | Ka 0.000000 0.000000 0.000000
13 | Kd 1.000000 0.450000 0.000000
14 | Ks 0.000000 0.000000 0.000000
15 | Ns 96.078431
16 | Ni 1.000000
17 | d 1.000000
18 | illum 0
19 |
--------------------------------------------------------------------------------
/resources/cube.obj:
--------------------------------------------------------------------------------
1 | # Blender v2.60 (sub 0) OBJ File: ''
2 | # www.blender.org
3 | mtllib cube.mtl
4 | o Cube
5 | v 1.000000 -1.000000 -1.000000
6 | v 1.000000 -1.000000 1.000000
7 | v -1.000000 -1.000000 1.000000
8 | v -1.000000 -1.000000 -1.000000
9 | v 1.000000 1.000000 -1.000000
10 | v 1.000000 1.000000 1.000001
11 | v -1.000000 1.000000 1.000000
12 | v -1.000000 1.000000 -1.000000
13 | usemtl Material
14 | f 1 2 3 4
15 | f 5 8 7 6
16 | f 2 6 7 3
17 | f 3 7 8 4
18 | f 5 1 4 8
19 | usemtl Material.001
20 | f 1 5 6 2
21 |
--------------------------------------------------------------------------------
/resources/lightblueflower.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/lightblueflower.JPG
--------------------------------------------------------------------------------
/resources/numbers.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/numbers.png
--------------------------------------------------------------------------------
/resources/orange.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/orange.JPG
--------------------------------------------------------------------------------
/resources/parasol.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/parasol.jpg
--------------------------------------------------------------------------------
/resources/particle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/particle.png
--------------------------------------------------------------------------------
/resources/pinkflower.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/pinkflower.JPG
--------------------------------------------------------------------------------
/resources/redflower.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/redflower.jpg
--------------------------------------------------------------------------------
/resources/sky.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/sky.JPG
--------------------------------------------------------------------------------
/resources/sky_cloud.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/sky_cloud.jpg
--------------------------------------------------------------------------------
/resources/sky_roof.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/sky_roof.JPG
--------------------------------------------------------------------------------
/resources/yellowflower.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huningxin/webglbook_examples/e787cd3e0513005eeb78e88600e7d5418c6597e3/resources/yellowflower.jpg
--------------------------------------------------------------------------------