texture.html
11 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<html>
<head>
<title>My first animated and textured WebGL content.</title>
<script src="./gl-matrix-min.js" type="text/javascript"></script>
<script type="text/javascript">
var projectionMatrix, modelViewMatrix;
var rotationAxis;
var shaderProgram, shaderVertexPositionAttribute
var shaderTexCoordAttribute, shaderSamplerUniform;
var shaderProjectionMatrixUniform, shaderModelViewMatrixUniform;
var duration = 5000; // ms
var currentTime = Date.now();
var okToRun = false;
var webGLTexture;
function handleTextureLoaded(gl, texture) {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
gl.RGBA,
gl.UNSIGNED_BYTE,
texture.image);
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_MAG_FILTER,
gl.NEAREST);
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_MIN_FILTER,
gl.NEAREST);
gl.bindTexture(gl.TEXTURE_2D, null);
okToRun = true;
}
function initTexture(gl) {
webGLTexture = gl.createTexture();
webGLTexture.image = new Image();
webGLTexture.image.onload = function () {
handleTextureLoaded(gl, webGLTexture)
}
//webGLTexture.image.src = "P3D/images/webgl-logo-256.jpg";
webGLTexture.image.src = "gravatar-256.jpg";
}
function initWebGL(canvas) {
var gl = null;
var msg = "Your browser does not support WebGL, " +
"or it is not enabled by default.";
try {
gl = canvas.getContext("experimental-webgl");
}
catch (e) {
msg = "Error creating WebGL Context!: " + e.toString();
}
if (!gl) {
alert(msg);
throw new Error(msg);
}
return gl;
}
function initViewport(gl, canvas) {
gl.viewport(0, 0, canvas.width, canvas.height);
}
// Create the vertex, color, and index data for a
// multicolored cube
function createCube(gl) {
// Vertex Data
var vertexBuffer;
vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
var verts = [
// Front face
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// Back face
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
// Top face
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
// Bottom face
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
// Right face
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
// Left face
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0
];
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array(verts),
gl.STATIC_DRAW);
var texCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
var textureCoords = [
// Front face
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// Back face
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
// Top face
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
// Bottom face
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
// Right face
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
// Left face
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
// Index data (defines the triangles to be drawn)
var cubeIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeIndexBuffer);
var cubeIndices = [
0, 1, 2, 0, 2, 3, // Front face
4, 5, 6, 4, 6, 7, // Back face
8, 9, 10, 8, 10, 11, // Top face
12, 13, 14, 12, 14, 15, // Bottom face
16, 17, 18, 16, 18, 19, // Right face
20, 21, 22, 20, 22, 23 // Left face
];
gl.bufferData(
gl.ELEMENT_ARRAY_BUFFER,
new Uint16Array(cubeIndices),
gl.STATIC_DRAW);
var cube = {
buffer:vertexBuffer,
texCoordBuffer:texCoordBuffer,
indices:cubeIndexBuffer,
vertSize:3,
nVerts:24,
texCoordSize:2,
nTexCoords: 24,
nIndices:36,
primtype:gl.TRIANGLES};
return cube;
}
function initMatrices(canvas) {
modelViewMatrix = mat4.create();
mat4.translate(
modelViewMatrix, modelViewMatrix, [0, 0, -4.6]);
// Create a project matrix with 45 degree field of view
projectionMatrix = mat4.create();
mat4.perspective(projectionMatrix, Math.PI / 4,
canvas.width / canvas.height, 1, 10000);
rotationAxis = vec3.create();
vec3.normalize(rotationAxis, [1, 1, 1]);
}
function createShader(gl, id, type) {
var shader;
var str = document.getElementById(id).text;
if (type == "fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (type == "vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
function initShader(gl, vertex, fragment) {
// load and compile the fragment and vertex shader
var fragmentShader = createShader(gl, fragment, "fragment");
var vertexShader = createShader(gl, vertex, "vertex");
// link them together into a new program
shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
// get pointers to the shader params
shaderVertexPositionAttribute = gl.getAttribLocation(
shaderProgram, "vertexPos");
gl.enableVertexAttribArray(shaderVertexPositionAttribute);
shaderTexCoordAttribute = gl.getAttribLocation(
shaderProgram, "texCoord");
gl.enableVertexAttribArray(shaderTexCoordAttribute);
shaderProjectionMatrixUniform = gl.getUniformLocation(
shaderProgram, "projectionMatrix");
shaderModelViewMatrixUniform = gl.getUniformLocation(
shaderProgram, "modelViewMatrix");
shaderSamplerUniform = gl.getUniformLocation(
shaderProgram, "uSampler");
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
}
function draw(gl, obj) {
// clear the background (transparent)
gl.clearColor(0.0, 0.0, 0.0, 0.0);
// // clear the background (black)
// gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// set the shader to use
gl.useProgram(shaderProgram);
// connect up the shader parameters: vertex position,
// color, and projection/model matrices
// set up the buffers
gl.bindBuffer(gl.ARRAY_BUFFER, obj.buffer);
gl.vertexAttribPointer(
shaderVertexPositionAttribute,
obj.vertSize,
gl.FLOAT,
false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, obj.texCoordBuffer);
gl.vertexAttribPointer(
shaderTexCoordAttribute,
obj.texCoordSize,
gl.FLOAT,
false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, obj.indices);
gl.uniformMatrix4fv(shaderProjectionMatrixUniform, false,
projectionMatrix);
gl.uniformMatrix4fv(shaderModelViewMatrixUniform, false,
modelViewMatrix);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, webGLTexture);
gl.uniform1i(shaderSamplerUniform, 0);
// draw the object
gl.drawElements(
obj.primtype, obj.nIndices, gl.UNSIGNED_SHORT, 0);
}
function animate() {
var now = Date.now();
var deltat = now - currentTime;
currentTime = now;
var fract = deltat / duration;
var angle = Math.PI * 2 * fract;
mat4.rotate(
modelViewMatrix,
modelViewMatrix,
angle,
rotationAxis);
}
function run(gl, cube) {
requestAnimationFrame(function() { run(gl, cube); });
if (okToRun) {
draw(gl, cube);
animate();
}
}
function startGl() {
// Get A WebGL context
var canvas = document.getElementById("cube");
var gl = initWebGL(canvas);
var obj = createCube(gl);
initViewport(gl, canvas);
initMatrices(canvas);
initShader(
gl,
"texture-vertex-shader",
"texture-fragment-shader");
initTexture(gl);
run(gl, obj);
}
</script>
<script id="texture-vertex-shader" type="x-shader/x-vertex">
attribute vec3 vertexPos;
attribute vec2 texCoord;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying vec2 vTexCoord;
void main(void) {
// Return the transformed and projected vertex value
gl_Position = projectionMatrix * modelViewMatrix * vec4(vertexPos, 1.0);
// Output the texture coordinate in vTexCoord
vTexCoord = texCoord;
}
</script>
<script id="texture-fragment-shader" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vTexCoord;
uniform sampler2D uSampler;
void main(void) {
// Return the pixel color: always output white
gl_FragColor = texture2D(uSampler, vec2(vTexCoord.s, vTexCoord.t));
}
</script>
<style>
* {
font-family: Verdana, sans-serif;
}
.gl {
position: absolute;
top: 130px;
left: 50%;
transform: translate(-50%, 0);
width: 200px;
height: 200px;
z-index: 2;
}
.content {
position: fixed;
top: 50%;
left: 50%;
/* bring your own prefixes */
transform: translate(-50%, -50%);
width: 800px;
background: white;
padding: 10px;
border-style: solid;
border-color: rgb(100, 190, 12);
border-radius: 30px;
border-width: 3px;
z-index: 1;
}
#background {
position: absolute;
left: 0px;
top: 0px;
width: 1920px;
height: 1200px;
background-image: url(./background1.jpg);
padding: 0px;
margin: 0px;
z-index: -1;
}
h1,h2,h3,h4,h5,h6 {
font-weight: normal;
}
h1,h4 {
text-decoration: underline;
}
h1 {
font-size: x-large;
}
</style>
</head>
<body onLoad="startGl()">
<div id="background"></div>
<div class="content">
<div class="gl">
<canvas id="cube" width="200", height="200"></canvas>
</div>
<h1>My first animated and textured WebGL content.</h1>
<p>
Finally my first small steps in the new world of WebGL. It is
pretty cool to have hardware accelerated, animated 3D objects
in your page.
</p>
<h2>Hidden on purpose.</h2>
<p>
This paragraph is hidden on purpose to see how the transparent
background of the GL canvas behaves if positioned above another
element. Now, we just fill in some stuff to get the paragraph
filled with some more stuff. Eventually we will have so much text
that it becomes big enought for the height of the GL canvas. Well,
as the canvas is transparant at least the maximum height of the
cube.
</p>
<h2>More to come...</h2>
</div>
</body>
</html>
<!-- vim: set ts=4 sw=4: -->