5
|
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>
|
|
30 <title>WebGL dandy</title>
|
|
31 <script src="resources/CanvasMatrix.js"> </script>
|
|
32 <script src="resources/utils3d.js"> </script>
|
|
33 <script src="resources/jkl-parsexml.js"> </script>
|
|
34 <script src="resources/makePanel.js"> </script>
|
|
35 <script src="resources/Character_state.js"> </script>
|
|
36 <script src="resources/Character.js"> </script>
|
|
37 <script src="resources/schedule.js"> </script>
|
|
38 <script src="resources/Player.js"> </script>
|
|
39 <script src="resources/enemy.js"> </script>
|
6
|
40 <script src="resources/bullet.js"> </script>
|
5
|
41
|
|
42 <script id="vshader" type="x-shader/x-vertex">
|
|
43 uniform mat4 u_modelViewProjMatrix;
|
|
44 uniform mat4 u_normalMatrix;
|
|
45 uniform vec3 lightDir;
|
|
46
|
|
47 attribute vec3 vNormal;
|
|
48 attribute vec4 vTexCoord;
|
|
49 attribute vec4 vPosition;
|
|
50
|
|
51 varying float v_Dot;
|
|
52 varying vec2 v_texCoord;
|
|
53
|
|
54 void main()
|
|
55 {
|
|
56 gl_Position = u_modelViewProjMatrix * vPosition;
|
|
57 v_texCoord = vTexCoord.st;
|
|
58 vec4 transNormal = u_normalMatrix * vec4(vNormal,1);
|
|
59 v_Dot = max(dot(transNormal.xyz, lightDir), 0.0);
|
|
60 // v_Dot = min(dot(transNormal.xyz, lightDir), 1.0);
|
|
61 }
|
|
62 </script>
|
|
63
|
|
64 <script id="fshader" type="x-shader/x-fragment">
|
|
65 uniform sampler2D sampler2d;
|
|
66
|
|
67 varying float v_Dot;
|
|
68 varying vec2 v_texCoord;
|
|
69
|
|
70 void main()
|
|
71 {
|
|
72 vec4 color = texture2D(sampler2d,v_texCoord);
|
|
73 color += vec4(0.1,0.1,0.1,1);
|
|
74 gl_FragColor = vec4(color.xyz * v_Dot, color.a);
|
|
75 }
|
|
76 </script>
|
|
77
|
|
78 <script>
|
6
|
79
|
5
|
80 pad = new Pad();
|
6
|
81 const LEFT_ARROW = 37;
|
|
82 const UP_ARROW = 38;
|
|
83 const RIGHT_ARROW = 39;
|
|
84 const DOWN_ARROW = 40;
|
|
85 const KEY_D = 68;
|
|
86 const KEY_A = 65;
|
5
|
87 const KEY_W = 87;
|
|
88 const KEY_S = 83;
|
|
89 const KEY_Z = 90;
|
6
|
90 const KEY_X = 88;
|
|
91 const KEY_Z = 90;
|
|
92 const KEY_R = 82;
|
|
93 const KEY_E = 69;
|
|
94 const KEY_W = 87;
|
|
95 const KEY_S = 83;
|
5
|
96 var audio = window.Audio && new Audio("sound/sample.wav");
|
|
97 var audioShoot = window.Audio && new Audio("sound/shotc.wav");
|
|
98
|
|
99 function init()
|
|
100 {
|
|
101 var gl = initWebGL("example", "vshader", "fshader",
|
|
102 [ "vNormal", "vTexCoord", "vPosition"],
|
|
103 [ 0, 0, 0, 1 ], 10000);
|
|
104
|
|
105 gl.uniform3f(gl.getUniformLocation(gl.program, "lightDir"), 0, 0, 1);
|
|
106 gl.uniform1i(gl.getUniformLocation(gl.program, "sampler2d"), 0);
|
|
107
|
|
108 gl.enable(gl.TEXTURE_2D);
|
|
109
|
|
110
|
6
|
111 loadEnemyXml( gl,"./xml/character.xml");
|
|
112 loadBulletXml( gl,"./xml/effect.xml");
|
|
113
|
|
114 player = new makePlayer(gl.chara);
|
|
115 enemy = new parseEnemy(gl);
|
|
116 bullets = new parseBullet(gl);
|
5
|
117
|
|
118 return gl;
|
|
119 }
|
|
120
|
|
121 width = -1;
|
|
122 height = -1;
|
|
123
|
|
124 function reshape(ctx)
|
|
125 {
|
|
126 var canvas = document.getElementById('example');
|
|
127 if (canvas.width == width && canvas.width == height)
|
|
128 return;
|
|
129
|
|
130 width = canvas.width;
|
|
131 height = canvas.height;
|
|
132
|
|
133 ctx.viewport(0, 0, width, height);
|
|
134
|
|
135 ctx.perspectiveMatrix = new CanvasMatrix4();
|
|
136 // ctx.perspectiveMatrix.lookat(0,0,6, 0, 0, 0, 0, 1, 0);
|
|
137 ctx.perspectiveMatrix.ortho(-100, 100, -70, 70, 0, 10000);
|
|
138
|
|
139 }
|
|
140
|
|
141
|
|
142
|
6
|
143 function loop(ctx,canvas)
|
5
|
144 {
|
|
145 reshape(ctx);
|
|
146 ctx.clear(ctx.COLOR_BUFFER_BIT | ctx.DEPTH_BUFFER_BIT);
|
|
147
|
6
|
148 // movePlayer( player, pad);
|
|
149 // drawPlayer(ctx, player, 1.2);
|
5
|
150
|
6
|
151 Player(ctx, player, pad);
|
5
|
152 obj_draw(ctx);
|
|
153
|
|
154 ctx.flush();
|
|
155
|
|
156 filpcount++;
|
|
157 schedule();
|
|
158 state_update();
|
|
159
|
|
160 framerate.snapshot();
|
|
161
|
|
162 }
|
|
163
|
|
164 function start()
|
|
165 {
|
|
166
|
|
167 audio && audio.play();//audio Test
|
|
168
|
|
169 var c = document.getElementById("example");
|
|
170 var w = Math.floor(window.innerWidth * 0.9);
|
|
171 var h = Math.floor(window.innerHeight * 0.9);
|
|
172
|
|
173 c.width = w;
|
|
174 c.height = h;
|
|
175
|
|
176 var ctx = init();
|
|
177
|
|
178 currentAngle = 0;
|
6
|
179 var f = function() { loop(ctx,c) };
|
5
|
180 setInterval(f, 10);
|
|
181 framerate = new Framerate("framerate");
|
|
182 }
|
|
183 function keybordDown()
|
|
184 {
|
|
185 var code = event.keyCode;
|
6
|
186 if(code == KEY_D) pad.right+=0.5;
|
|
187 if(code == KEY_A) pad.left+=0.5;
|
|
188 if(code == KEY_W) pad.up+=1;
|
|
189 if(code == KEY_S) pad.down+=1;
|
5
|
190 if(code == KEY_Z) audioShoot && audioShoot.play();
|
6
|
191 if(code == KEY_X) pad.k4++;
|
|
192 if(code == KEY_S) pad.k3++;
|
|
193 if(code == LEFT_ARROW) pad.left+=0.5;
|
|
194 if(code == RIGHT_ARROW) pad.right+=0.5;
|
|
195 if(code == UP_ARROW) pad.up+=1;
|
|
196 if(code == DOWN_ARROW) pad.down+=1;
|
5
|
197 }
|
|
198 function keybordPress()
|
|
199 {
|
|
200 pad.count++;
|
|
201 }
|
|
202 function keybordUp()
|
|
203 {
|
|
204 var code = event.keyCode;
|
|
205 if(code == KEY_D) pad.right = 0;
|
|
206 if(code == KEY_A) pad.left = 0;
|
|
207 if(code == KEY_W) pad.up = 0;
|
|
208 if(code == KEY_S) pad.down = 0;
|
6
|
209 if(code == KEY_X) pad.k4 = 0;
|
|
210 if(code == LEFT_ARROW) pad.left = 0;
|
|
211 if(code == RIGHT_ARROW) pad.right = 0;
|
|
212 if(code == UP_ARROW) pad.up = 0;
|
|
213 if(code == DOWN_ARROW) pad.down = 0;
|
|
214
|
5
|
215 pad.state=0;
|
|
216 pad.count=0;
|
|
217 }
|
|
218 </script>
|
|
219 <style type="text/css">
|
|
220 canvas {
|
|
221 border: 2px solid black;
|
|
222 }
|
|
223 </style>
|
|
224 </head>
|
|
225 <body onload="start()" onkeydown="keybordDown()" onkeypress="keybordPress()" onkeyup="keybordUp()">
|
|
226 <canvas id="example">
|
|
227 There is supposed to be an example drawing here, but it's not important.
|
|
228 </canvas>
|
|
229 <div id="framerate"></div>
|
|
230 <div id="console"></div>
|
|
231
|
|
232 <img id="test" style="border:1px solid red">
|
|
233
|
|
234 </body>
|
|
235 </html>
|