forked from steven-chien/PolyDraw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyShow.html
More file actions
205 lines (169 loc) · 5.9 KB
/
polyShow.html
File metadata and controls
205 lines (169 loc) · 5.9 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
<html>
<head>
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec2 aTexturePosition;
uniform vec2 uResolution;
varying vec2 vTexturePosition;
void main() {
gl_Position = vec4(aVertexPosition, 1.0);
vTexturePosition = aTexturePosition;
gl_PointSize = 10.0;
}
</script>
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D uImage;
varying vec2 vTexturePosition;
void main() {
gl_FragColor = texture2D(uImage, vTexturePosition);
}
</script>
<script type="text/javascript">
var canvas = null, gl = null;
var vertexShader = null, fragmentShader = null;
var glProgram = null, texturePositionBuffer = null;
var texCoordLocation = null, positionLocation = null;
var texture = null;
var texturePositionBuffer = null, polygonVertexPositionBuffer = null;
var image = null;
var texCoord = [], vertices = [];
function initWebGL() {
image = new Image();
image.src = "data/texture.png";
image.onload = function() {
initShaders();
initBuffers();
initTexture();
drawScene();
}
}
function initShaders() {
/* create vertex and fragment shaders */
vertexShader = gl.createShader(gl.VERTEX_SHADER);
fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
/* bind shader to glsl */
gl.shaderSource(vertexShader, document.getElementById("shader-vs").innerHTML);
gl.shaderSource(fragmentShader, document.getElementById("shader-fs").innerHTML);
/* compile and check vertex shader */
gl.compileShader(vertexShader);
if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) {
alert("Cannot compile vertex shader!");
}
/* compile and check fragment shader */
gl.compileShader(fragmentShader);
if(!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) {
alert("Cannot compile fragment shader!");
}
/* create GL glProgram and link shaders */
glProgram = gl.createProgram();
gl.attachShader(glProgram, vertexShader);
gl.attachShader(glProgram, fragmentShader);
gl.linkProgram(glProgram);
if (!gl.getProgramParameter(glProgram, gl.LINK_STATUS)) {
alert("Unable to initialize the shader glProgram.");
}
//use glProgram
gl.useProgram(glProgram);
}
function initBuffers() {
/* lookup shader attributes */
positionLocation = gl.getAttribLocation(glProgram, "aVertexPosition");
texCoordLocation = gl.getAttribLocation(glProgram, "aTexturePosition");
/* create texture position buffer and populate it */
texturePositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texturePositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(texCoord), gl.STATIC_DRAW);
gl.enableVertexAttribArray(texCoordLocation);
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0);
/* create vertex position buffer and populate it */
polygonVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, polygonVertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0);
}
function initTexture() {
/* create a new 2D texture */
texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
/* image parameter setting */
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
/* upload image to GPU */
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
/* setup resolution */
var resolutionLocation = gl.getUniformLocation(glProgram, "uResolution");
gl.uniform2f(resolutionLocation, canvas.width, canvas.height);
}
function drawScene() {
/* clear screen */
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
/* draw from buffer */
gl.drawArrays(gl.TRIANGLE_FAN, 0, vertices.length/3);
}
function fileUploadHandler(event) {
var file = event.target.files[0];
if (!file) {
return;
}
vertices = [];
texCoord = [];
/* create a new file reader */
var reader = new FileReader();
reader.onload = function(e) {
var j=0;
/* split content file */
var content = e.target.result.split(",");
/* read strings and convert them to float */
for(var i=0; i<content.length; i++) {
vertices.push(parseFloat(content[i]));
}
/* look for the corresponding coordinate on the texture */
for(var i=0; i<vertices.length; i++) {
if(j==2) {
j=0;
continue;
}
texCoord.push((vertices[i]+1)/2);
j++;
}
/* start the program */
initWebGL();
};
reader.readAsText(file);
}
function init() {
// Get A WebGL context
canvas = document.getElementById("test-canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight * 0.95;
/* get WebGL context */
gl = canvas.getContext("experimental-webgl");
if (!gl) {
return;
}
/* clear screen */
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
/* install event listener to open file */
document.getElementById("file-input").addEventListener("change", fileUploadHandler, false);
}
</script>
</head>
<body onload="init()">
<canvas id="test-canvas"></canvas>
<input type="file" id="file-input">Click "Choose File" and pick the filed saved by polyDraw.html</input>
</body>
</html>