3
|
1 <!DOCTYPE html>
|
|
2 <!--
|
|
3 /*
|
|
4 * Copyright (C) 2009 Apple Inc. All Rights Reserved.
|
|
5 *
|
|
6 * Redistribution and use in source and binary forms, with or without
|
|
7 * modification, are permitted provided that the following conditions
|
|
8 * are met:
|
|
9 * 1. Redistributions of source code must retain the above copyright
|
|
10 * notice, this list of conditions and the following disclaimer.
|
|
11 * 2. Redistributions in binary form must reproduce the above copyright
|
|
12 * notice, this list of conditions and the following disclaimer in the
|
|
13 * documentation and/or other materials provided with the distribution.
|
|
14 *
|
|
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
|
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
|
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
26 */
|
|
27 -->
|
|
28 <html>
|
|
29 <head>
|
4
|
30 <title>WebGL dandy</title>
|
3
|
31 <script src="resources/CanvasMatrix.js"> </script>
|
|
32 <script src="resources/utils3d.js"> </script>
|
|
33 <script src="resources/jkl-parsexml.js"> </script>
|
4
|
34 <!-- <script src="resources/makeXmlObj.js"> </script> -->
|
|
35 <script src="resources/Character.js"> </script>
|
|
36 <script src="resources/makePanel.js"> </script>
|
|
37 <script src="resources/schedule.js"> </script>
|
|
38 <script src="resources/Player.js"> </script>
|
3
|
39
|
|
40 <script id="vshader" type="x-shader/x-vertex">
|
|
41 uniform mat4 u_modelViewProjMatrix;
|
|
42 uniform mat4 u_normalMatrix;
|
|
43 uniform vec3 lightDir;
|
|
44
|
|
45 attribute vec3 vNormal;
|
|
46 attribute vec4 vTexCoord;
|
|
47 attribute vec4 vPosition;
|
|
48
|
|
49 varying float v_Dot;
|
|
50 varying vec2 v_texCoord;
|
|
51
|
|
52 void main()
|
|
53 {
|
|
54 gl_Position = u_modelViewProjMatrix * vPosition;
|
|
55 v_texCoord = vTexCoord.st;
|
|
56 vec4 transNormal = u_normalMatrix * vec4(vNormal,1);
|
|
57 v_Dot = max(dot(transNormal.xyz, lightDir), 0.0);
|
|
58 // v_Dot = min(dot(transNormal.xyz, lightDir), 1.0);
|
|
59 }
|
|
60 </script>
|
|
61
|
|
62 <script id="fshader" type="x-shader/x-fragment">
|
|
63 uniform sampler2D sampler2d;
|
|
64
|
|
65 varying float v_Dot;
|
|
66 varying vec2 v_texCoord;
|
|
67
|
|
68 void main()
|
|
69 {
|
|
70 vec4 color = texture2D(sampler2d,v_texCoord);
|
|
71 color += vec4(0.1,0.1,0.1,1);
|
|
72 gl_FragColor = vec4(color.xyz * v_Dot, color.a);
|
|
73 }
|
|
74 </script>
|
|
75
|
|
76 <script>
|
4
|
77 pad = new Pad();
|
|
78 const KEY_D = 65;
|
|
79 const KEY_A = 68;
|
|
80 const KEY_W = 87;
|
|
81 const KEY_S = 83;
|
|
82 const KEY_Z = 90;
|
|
83 var audio = window.Audio && new Audio("sound/sample.wav");
|
|
84 var audioShoot = window.Audio && new Audio("sound/shotc.wav");
|
|
85
|
3
|
86 function init()
|
|
87 {
|
|
88 var gl = initWebGL("example", "vshader", "fshader",
|
|
89 [ "vNormal", "vTexCoord", "vPosition"],
|
|
90 [ 0, 0, 0, 1 ], 10000);
|
|
91
|
|
92 gl.uniform3f(gl.getUniformLocation(gl.program, "lightDir"), 0, 0, 1);
|
|
93 gl.uniform1i(gl.getUniformLocation(gl.program, "sampler2d"), 0);
|
|
94
|
|
95 gl.enable(gl.TEXTURE_2D);
|
|
96 gl.obj = new Object();
|
4
|
97 gl.fontObj = new Object();
|
3
|
98 for (var name in xmlObj) {
|
|
99 gl.obj[name] = makeXmlObj(gl, xmlObj[name]);
|
|
100 gl.obj[name].texture = loadImageTexture(gl, xmlObj[name].image);
|
|
101 }
|
4
|
102 for (var name in xmlFont) {
|
|
103 gl.fontObj[name] = makeXmlObj(gl, xmlFont[name]);
|
|
104 gl.fontObj[name].texture = loadImageTexture(gl, xmlFont[name].image);
|
|
105 }
|
|
106
|
|
107 initCharacter();
|
|
108 player = new Player(gl.obj);
|
3
|
109
|
|
110 return gl;
|
|
111 }
|
|
112
|
|
113 width = -1;
|
|
114 height = -1;
|
|
115
|
|
116 function reshape(ctx)
|
|
117 {
|
|
118 var canvas = document.getElementById('example');
|
|
119 if (canvas.width == width && canvas.width == height)
|
|
120 return;
|
|
121
|
|
122 width = canvas.width;
|
|
123 height = canvas.height;
|
|
124
|
|
125 ctx.viewport(0, 0, width, height);
|
|
126
|
|
127 ctx.perspectiveMatrix = new CanvasMatrix4();
|
4
|
128 // ctx.perspectiveMatrix.lookat(0,0,6, 0, 0, 0, 0, 1, 0);
|
|
129 // ctx.perspectiveMatrix.perspective(30, width/height, 1, 10000);
|
|
130 ctx.perspectiveMatrix.ortho(-100, 100, -70, 70, 0, 10000);
|
3
|
131
|
|
132 }
|
|
133
|
|
134 function drawOne(ctx, glObj, angle, x, y, z, scale)
|
|
135 {
|
|
136 // setup VBOs
|
|
137 ctx.enableVertexAttribArray(0);
|
|
138 ctx.enableVertexAttribArray(1);
|
|
139 ctx.enableVertexAttribArray(2);
|
|
140
|
|
141 ctx.bindBuffer(ctx.ARRAY_BUFFER, glObj.vertexObject);
|
|
142 ctx.vertexAttribPointer(2, 3, ctx.FLOAT, false, 0, 0);
|
|
143
|
|
144 ctx.bindBuffer(ctx.ARRAY_BUFFER, glObj.normalObject);
|
|
145 ctx.vertexAttribPointer(0, 3, ctx.FLOAT, false, 0, 0);
|
|
146
|
|
147 ctx.bindBuffer(ctx.ARRAY_BUFFER, glObj.texCoordObject);
|
|
148 ctx.vertexAttribPointer(1, 2, ctx.FLOAT, false, 0, 0);
|
|
149
|
|
150 ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, glObj.indexObject);
|
|
151
|
|
152 // generate the model-view matrix
|
|
153 var mvMatrix = new CanvasMatrix4();
|
|
154 mvMatrix.scale(scale, scale, scale);
|
4
|
155 mvMatrix.translate(0,0,0);
|
3
|
156 // mvMatrix.rotate(30, 1,0,0);
|
4
|
157 mvMatrix.rotate(180, 0,0,1);
|
3
|
158 mvMatrix.translate(x,y,z);
|
|
159 /*
|
|
160 var mvNormalMatrix = new CanvasMatrix4();
|
|
161 mvMatrix.rotate(angle, 0,1,0);
|
|
162 mvMatrix.rotate(30, 1,0,0);
|
|
163 mvMatrix.translate(x,y,z);
|
|
164 */
|
|
165 // construct the normal matrix from the model-view matrix
|
|
166 var normalMatrix = new CanvasMatrix4(mvMatrix);
|
|
167 normalMatrix.invert();
|
|
168 normalMatrix.transpose();
|
|
169 ctx.uniformMatrix4fv(ctx.getUniformLocation(ctx.program, "u_normalMatrix"), false, normalMatrix.getAsWebGLFloatArray());
|
|
170
|
|
171 // construct the model-view * projection matrix
|
|
172 var mvpMatrix = new CanvasMatrix4(mvMatrix);
|
|
173 mvpMatrix.multRight(ctx.perspectiveMatrix);
|
|
174 ctx.uniformMatrix4fv(ctx.getUniformLocation(ctx.program, "u_modelViewProjMatrix"), false, mvpMatrix.getAsWebGLFloatArray());
|
|
175
|
|
176 // ctx.bindTexture(ctx.TEXTURE_2D, texture);
|
|
177 ctx.bindTexture(ctx.TEXTURE_2D, glObj.texture);
|
|
178 ctx.drawElements(ctx.TRIANGLES, glObj.numIndices, ctx.UNSIGNED_SHORT, 0);
|
|
179 }
|
|
180
|
4
|
181 function loop(ctx)
|
3
|
182 {
|
|
183 reshape(ctx);
|
|
184 ctx.clear(ctx.COLOR_BUFFER_BIT | ctx.DEPTH_BUFFER_BIT);
|
|
185
|
4
|
186 /*
|
3
|
187 for (var name in ctx.obj) {
|
4
|
188 drawOne(ctx, ctx.obj[name], currentAngle, 0, 0, 0, 0.05);
|
|
189 drawOne(ctx, ctx.obj[name], currentAngle, 0, 0, 300, 0.8);
|
3
|
190 }
|
4
|
191 */
|
|
192 movePlayer(player, pad);
|
|
193
|
|
194 // drawOne(ctx, ctx.obj["player_idle"], currentAngle, X, 0, 10, 1.2);
|
|
195 // drawOne(ctx, player.image, currentAngle, X, 0, 10, 1.2);
|
|
196 drawPlayer(ctx, player, 1.2);
|
|
197
|
3
|
198 ctx.flush();
|
4
|
199
|
|
200 filpcount++;
|
|
201
|
3
|
202 framerate.snapshot();
|
4
|
203
|
3
|
204 }
|
|
205
|
|
206 function start()
|
|
207 {
|
|
208 //xmlファイル読み込み
|
4
|
209 xmlObj = parsePanel("./xml/character.xml");
|
|
210 xmlFont = parsePanel("./xml/font.xml");
|
|
211
|
|
212 audio && audio.play();//audio Test
|
|
213
|
3
|
214
|
|
215
|
|
216 for (var name in xmlObj ){
|
|
217 document.getElementById("test").src=xmlObj[name].image;
|
|
218 }
|
|
219 var c = document.getElementById("example");
|
|
220 var w = Math.floor(window.innerWidth * 0.9);
|
|
221 var h = Math.floor(window.innerHeight * 0.9);
|
|
222
|
|
223 c.width = w;
|
|
224 c.height = h;
|
|
225
|
|
226 var ctx = init();
|
|
227 currentAngle = 0;
|
4
|
228 var f = function() { loop(ctx) };
|
3
|
229 setInterval(f, 10);
|
|
230 framerate = new Framerate("framerate");
|
|
231 }
|
4
|
232 function keybordDown()
|
|
233 {
|
|
234 var code = event.keyCode;
|
|
235 if(code == KEY_D) pad.right+=1.5;
|
|
236 if(code == KEY_A) pad.left+=1.5;
|
|
237 if(code == KEY_W) pad.up+=2;
|
|
238 if(code == KEY_S) pad.down+=2;
|
|
239 if(code == KEY_Z) audioShoot && audioShoot.play();
|
|
240 }
|
|
241 function keybordPress()
|
|
242 {
|
|
243 pad.count++;
|
|
244 }
|
|
245 function keybordUp()
|
|
246 {
|
|
247 var code = event.keyCode;
|
|
248 if(code == KEY_D) pad.right = 0;
|
|
249 if(code == KEY_A) pad.left = 0;
|
|
250 if(code == KEY_W) pad.up = 0;
|
|
251 if(code == KEY_S) pad.down = 0;
|
|
252
|
|
253 pad.state=0;
|
|
254 pad.count=0;
|
|
255 }
|
3
|
256 </script>
|
|
257 <style type="text/css">
|
|
258 canvas {
|
|
259 border: 2px solid black;
|
|
260 }
|
|
261 </style>
|
|
262 </head>
|
4
|
263 <body onload="start()" onkeydown="keybordDown()" onkeypress="keybordPress()" onkeyup="keybordUp()">
|
3
|
264 <canvas id="example">
|
|
265 There is supposed to be an example drawing here, but it's not important.
|
|
266 </canvas>
|
|
267 <div id="framerate"></div>
|
|
268 <div id="console"></div>
|
|
269
|
|
270 <img id="test" style="border:1px solid red">
|
|
271
|
|
272 </body>
|
|
273 </html>
|