-
Notifications
You must be signed in to change notification settings - Fork 4
/
hello-webgl-texture.html
184 lines (149 loc) · 5.54 KB
/
hello-webgl-texture.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
<html>
<head>
<title>Hello, WebGL (adopted from Learning WebGL lesson 2)</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D tex;
varying vec2 texCoord;
void main(void) {
gl_FragColor = texture2D(tex, texCoord);
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 positionAttr;
attribute vec2 texCoordAttr;
varying vec2 texCoord;
void main(void) {
gl_Position = vec4(positionAttr, 1.0);
texCoord = texCoordAttr;
}
</script>
<script type="text/javascript">
var gl = null;
var viewportWidth = 0;
var viewportHeight = 0;
function initGL(canvas) {
try {
gl = canvas.getContext("webgl");
if (!gl)
gl = canvas.getContext("experimental-webgl");
if (gl) {
viewportWidth = canvas.width;
viewportHeight = canvas.height;
}
} catch (e) {
}
// Not the best error detection logic.
// Redirect to http://get.webgl.org in failure case.
if (!gl) {
alert("Could not initialise WebGL, sorry :-(");
}
}
function getShader(gl, id) {
var script = document.getElementById(id);
if (!script) {
return null;
}
var shader;
if (script.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (script.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, script.text);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var program;
function initShaders() {
var vertexShader = getShader(gl, "shader-vs");
var fragmentShader = getShader(gl, "shader-fs");
program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
gl.useProgram(program);
program.positionAttr = gl.getAttribLocation(program, "positionAttr");
gl.enableVertexAttribArray(program.positionAttr);
program.texCoordAttr = gl.getAttribLocation(program, "texCoordAttr");
gl.enableVertexAttribArray(program.texCoordAttr);
program.textureLoc = gl.getUniformLocation(program, "tex");
}
var buffer;
function initGeometry() {
buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
// Interleave vertex positions and colors
var vertexData = [
// X Y Z U V
0.0, 0.8, 0.0, 0.5, 1.0,
// X Y Z U V
-0.8, -0.8, 0.0, 0.0, 0.0,
// X Y Z U V
0.8, -0.8, 0.0, 1.0, 0.0,
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexData), gl.STATIC_DRAW);
}
var texture;
function loadTexture(src) {
// Create and initialize the WebGLTexture object.
texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
// Create a DOM image object.
var image = new Image();
// Set up the onload handler for the image, which will be called by
// the browser at some point in the future once the image has
// finished downloading.
image.onload = function() {
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, image);
drawScene();
};
// Start downloading the image by setting its source.
image.src = src;
}
function drawScene() {
gl.viewport(0, 0, viewportWidth, viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
// There are 5 floating-point values per vertex
var stride = 5 * Float32Array.BYTES_PER_ELEMENT;
// Set up position stream
gl.vertexAttribPointer(program.positionAttr, 3, gl.FLOAT, false, stride, 0);
// Set up texCoord stream
gl.vertexAttribPointer(program.texCoordAttr, 2, gl.FLOAT, false, stride, 3 * Float32Array.BYTES_PER_ELEMENT);
// Bind the texture to texture unit 0
gl.bindTexture(gl.TEXTURE_2D, texture);
// Point the uniform sampler to texture unit 0
gl.uniform1i(program.textureLoc, 0);
gl.drawArrays(gl.TRIANGLES, 0, 3);
}
function webGLStart() {
var canvas = document.getElementById("lesson02-canvas");
initGL(canvas);
initShaders()
initGeometry();
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.disable(gl.DEPTH_TEST);
loadTexture("images/webgl-logo-pot.png");
}
</script>
</head>
<body onload="webGLStart();">
<canvas id="lesson02-canvas" style="border: none;" width="400" height="400"></canvas>
</body>
</html>