forked from imindseye/WebGL-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 2
/
shading.html
168 lines (118 loc) · 4.5 KB
/
shading.html
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
<!DOCTYPE html>
<html>
<head>
<title>hwshen WebGL — Gouraud Shading </title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="glMatrix-0.9.5.min.js"></script>
<!-- ************** Fragment Shader ************* -->
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
uniform mat4 uMMatrix;
uniform mat4 uVMatrix;
uniform mat4 uPMatrix;
uniform mat4 uNMatrix;
uniform vec4 light_pos;
uniform vec4 ambient_coef;
uniform vec4 diffuse_coef;
uniform vec4 specular_coef;
uniform float mat_shininess;
uniform vec4 light_ambient;
uniform vec4 light_diffuse;
uniform vec4 light_specular;
varying vec4 eye_pos;
varying vec3 v_normal;
varying vec4 vColor;
void main(void) {
gl_FragColor =vColor;
}
</script>
<!-- ************** Vertex Shader ************* -->
<script id="shader-vs" type="x-shader/x-vertex">
precision mediump float;
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec3 aVertexColor;
uniform mat4 uMMatrix;
uniform mat4 uVMatrix;
uniform mat4 uPMatrix;
uniform mat4 uNMatrix;
uniform vec4 light_pos;
uniform vec4 ambient_coef;
uniform vec4 diffuse_coef;
uniform vec4 specular_coef;
uniform float mat_shininess;
uniform vec4 light_ambient;
uniform vec4 light_diffuse;
uniform vec4 light_specular;
varying vec4 eye_pos; //vertex position in eye space
varying vec3 v_normal; // vertex normal
varying vec4 vColor;
void main(void) {
// transform light pos from world to eye space
// vec4 light_pos_in_eye = uVMatrix * light_pos;
vec4 light_pos_in_eye = light_pos;
// transform normal from local to eye space: normal matrix is the inverse transpose of the modelview matrix
v_normal =normalize(vec3(uNMatrix*vec4(aVertexNormal,0.0)));
// transform the vertex position to eye space
eye_pos = uVMatrix*uMMatrix*vec4(aVertexPosition, 1.0);
// light vector L = l-p
vec3 light_vector = normalize(vec3(light_pos_in_eye - eye_pos));
// eye vector V = e-p, where e is (0,0,0)
vec3 eye_vector = normalize(-vec3(eye_pos));
// halfway vector (L+V)
vec3 halfv = normalize(light_vector+eye_vector);
vec4 ambient = ambient_coef * light_ambient;
float ndotl = max(dot(v_normal, light_vector), 0.0);
vec4 diffuse = diffuse_coef * light_diffuse* vec4(ndotl, ndotl, ndotl, 1.0);
// both lines below are okay. One is to use the reflect function the other is to compute by yourself
// vec3 R= normalize(vec3(reflect(-light_vector, v_normal)));
vec3 R = normalize(2.0 * ndotl *v_normal-light_vector);
float rdotv = max(dot(R, eye_vector), 0.0);
vec4 specular;
float rdotv_val = pow(rdotv, mat_shininess);
if (ndotl>0.0)
specular = specular_coef* light_specular* vec4(rdotv_val, rdotv_val, rdotv_val, 1.0);
else
specular = vec4(0,0,0,1);
gl_Position = uPMatrix*uVMatrix*uMMatrix*vec4(aVertexPosition, 1.0);
vColor = ambient+diffuse+specular;
// vColor = diffuse;
// vColor = specular;
// vColor = vec4(eye_vector, 1.0);
// vColor = vec4(aVertexColor, 1.0);
// vColor = vec4(aVertexNormal, 1.0);
// vColor = vec4(v_normal, 1.0);
}
</script>
<script type="text/javascript" src="shaders_setup.js"></script>
<script type="text/javascript" src="12-shading.js"></script>
</head>
<body onload="webGLStart();">
<canvas id="code03-canvas"style="border:none;"width="700"height="700"></canvas>
<h4> Geometry: </h4>
<button onclick="geometry(0)"> POINTS</button>
<button onclick="geometry(1)"> LINEs</button>
<button onclick="geometry(2)"> FACES</button>
<h4> Background: </h4>
<button onclick="BG(1,0,0)"> Red</button>
<button onclick="BG(0,1,0)"> Green</button>
<button onclick="BG(0,0,1)"> Blue</button>
<button onclick="BG(0.5,0.5,0.5)"> Grey</button>
<button onclick="BG(0,0,0)"> Black</button>
<button onclick="BG(1,1,1)"> White</button>
<div>
<h4>
Click on the left mouse button and move the coursor to rotate
</h4>
<button onclick="redraw()"> Go Back! </button>
</div>
<div>
<h3>
<div id="title"> code09.html COMMENTS</div>
</h3>
<h5>
A simple 3D rendering of cube using gl elemenntary array
</h5>
</div>
</body>
</html>