forked from imindseye/WebGL-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 2
/
textureTeapot.html
191 lines (135 loc) · 4.92 KB
/
textureTeapot.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html>
<head>
<title>hwshen WebGL — Texture Mapping </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;
precision mediump int;
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;
uniform int use_texture;
uniform sampler2D myTexture;
varying vec4 eye_pos;
varying vec3 v_normal;
varying highp vec2 FtexCoord;
varying vec4 vColor;
void main(void) {
vec4 texcolor;
if ( use_texture == 1 ) {
texcolor = texture2D(myTexture, FtexCoord);
gl_FragColor = texcolor;
// gl_FragColor = vColor*texcolor;
}
else gl_FragColor = vColor;
// gl_FragColor = vColor;
}
</script>
<!-- ************** Vertex Shader ************* -->
<script id="shader-vs" type="x-shader/x-vertex">
precision mediump float;
precision mediump int;
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aVertexTexCoords;
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;
uniform int use_texture;
uniform sampler2D myTexture;
varying vec4 eye_pos; //vertex position in eye space
varying vec3 v_normal; // vertex normal
varying highp vec2 FtexCoord;
varying vec4 vColor;
void main(void) {
// transform light pos from local to eye space
// vec4 light_pos_in_eye = uVMatrix * uMMatrix * 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));
vec4 ambient = ambient_coef * light_ambient;
float ndotl = max(dot(v_normal, light_vector), 0.0);
vec4 diffuse = diffuse_coef * light_diffuse* ndotl;
// 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;
if (ndotl>0.0)
specular = specular_coef* light_specular*pow(rdotv, mat_shininess);
else
specular = vec4(0,0,0,1);
vColor = ambient+diffuse+specular;
FtexCoord = aVertexTexCoords;
gl_Position = uPMatrix*uVMatrix*uMMatrix*vec4(aVertexPosition, 1.0);
// vColor = vec4(use_texture, use_texture, 0, 1.0);
// vColor = vec4(aVertexTexCoords, 1.0, 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="textureTeapot.js"></script>
</head>
<body onload="webGLStart();">
<canvas
id="code13-canvas"style="border:none;"width="700"height="700"></canvas>
<h4> Texture: </h4>
<button onclick="texture(0)"> NO </button>
<button onclick="texture(1)"> YES </button>
<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"> textureTeapot.html </div>
</h3>
<h5>
A texture mapped Utah teapot
</h5>
</div>
</body>
</html>