OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // | 4 // |
5 // This sample is based upon the Ray Tracer sample by Jonas Sicking at: | 5 // This sample is based upon the Ray Tracer sample by Jonas Sicking at: |
6 // http://www.khronos.org/webgl/wiki/Demo_Repository | 6 // http://www.khronos.org/webgl/wiki/Demo_Repository |
7 | 7 |
8 /** | 8 /** |
9 * A sample GL application. | 9 * A sample GL application. |
10 */ | 10 */ |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 var shaderProgram; | 164 var shaderProgram; |
165 var aVertexPosition; | 165 var aVertexPosition; |
166 var aPlotPosition; | 166 var aPlotPosition; |
167 var cameraPos; | 167 var cameraPos; |
168 var sphere1Center; | 168 var sphere1Center; |
169 var sphere2Center; | 169 var sphere2Center; |
170 var sphere3Center; | 170 var sphere3Center; |
171 var ratio; | 171 var ratio; |
172 | 172 |
173 void initShaders() { | 173 void initShaders() { |
174 var vertexShader = loadShader(WebGLRenderingContext.VERTEX_SHADER, VERTEX_PROG RAM); | 174 var vertexShader = loadShader(WebGLRenderingContext.VERTEX_SHADER, |
175 var fragmentShader = loadShader(WebGLRenderingContext.FRAGMENT_SHADER, FRAGMEN T_PROGRAM); | 175 VERTEX_PROGRAM); |
176 var fragmentShader = loadShader(WebGLRenderingContext.FRAGMENT_SHADER, | |
177 FRAGMENT_PROGRAM); | |
176 | 178 |
177 shaderProgram = gl.createProgram(); | 179 shaderProgram = gl.createProgram(); |
178 if (shaderProgram == 0) { | 180 if (shaderProgram == 0) { |
179 throw new Exception("Could not create program."); | 181 throw new Exception("Could not create program."); |
180 } | 182 } |
181 | 183 |
182 gl.attachShader(shaderProgram, vertexShader); | 184 gl.attachShader(shaderProgram, vertexShader); |
183 gl.attachShader(shaderProgram, fragmentShader); | 185 gl.attachShader(shaderProgram, fragmentShader); |
184 gl.linkProgram(shaderProgram); | 186 gl.linkProgram(shaderProgram); |
185 | 187 |
186 if (gl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) ! = true) { | 188 if (gl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) ! = true) { |
187 final error = gl.getProgramInfoLog(shaderProgram); | 189 final error = gl.getProgramInfoLog(shaderProgram); |
188 throw new Exception("Program compilation error: $error"); | 190 throw new Exception("Program compilation error: $error"); |
189 } | 191 } |
190 | 192 |
191 gl.useProgram(shaderProgram); | 193 gl.useProgram(shaderProgram); |
192 | 194 |
193 aVertexPosition = gl.getAttribLocation(shaderProgram, "aVertexPosition"); | 195 aVertexPosition = gl.getAttribLocation(shaderProgram, "aVertexPosition"); |
194 gl.enableVertexAttribArray(aVertexPosition); | 196 gl.enableVertexAttribArray(aVertexPosition); |
195 | 197 |
196 aPlotPosition = gl.getAttribLocation(shaderProgram, "aPlotPosition"); | 198 aPlotPosition = gl.getAttribLocation(shaderProgram, "aPlotPosition"); |
197 gl.enableVertexAttribArray(aPlotPosition); | 199 gl.enableVertexAttribArray(aPlotPosition); |
198 | 200 |
199 cameraPos = gl.getUniformLocation(shaderProgram, "cameraPos"); | 201 cameraPos = gl.getUniformLocation(shaderProgram, "cameraPos"); |
200 sphere1Center = gl.getUniformLocation(shaderProgram, "sphere1Center"); | 202 sphere1Center = gl.getUniformLocation(shaderProgram, "sphere1Center"); |
201 sphere2Center = gl.getUniformLocation(shaderProgram, "sphere2Center"); | 203 sphere2Center = gl.getUniformLocation(shaderProgram, "sphere2Center"); |
202 sphere3Center = gl.getUniformLocation(shaderProgram, "sphere3Center"); | 204 sphere3Center = gl.getUniformLocation(shaderProgram, "sphere3Center"); |
203 } | 205 } |
204 | 206 |
207 // TODO(gram): This should go away at some point. For now it is a kludge to allo w | |
vsm
2013/01/16 05:16:02
line len
| |
208 // use to run same .dart file with WebGL and natively; the WebGL version will | |
209 // set this to (a) => new Float32Array.fromList(a). | |
210 var wrapVertexArray = false; | |
211 | |
212 wrapVertices(a) => wrapVertexArray ? (new Float32Array.fromList(a)) : a; | |
213 | |
205 void initBuffers() { | 214 void initBuffers() { |
206 var vertexPositionBuffer = gl.createBuffer(); | 215 var vertexPositionBuffer = gl.createBuffer(); |
207 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, vertexPositionBuffer); | 216 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, vertexPositionBuffer); |
208 var vertices = [ | 217 var vertices = [ |
209 1.0, 1.0, | 218 1.0, 1.0, |
210 -1.0, 1.0, | 219 -1.0, 1.0, |
211 1.0, -1.0, | 220 1.0, -1.0, |
212 -1.0, -1.0, | 221 -1.0, -1.0, |
213 ]; | 222 ]; |
214 | 223 |
215 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, new Float32Array.fromList(ve rtices), WebGLRenderingContext.STATIC_DRAW); | 224 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, wrapVertices(vertices), |
216 gl.vertexAttribPointer(aVertexPosition, 2, WebGLRenderingContext.FLOAT, false, 0, 0); | 225 WebGLRenderingContext.STATIC_DRAW); |
226 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, vertexPositionBuffer); | |
227 gl.vertexAttribPointer(aVertexPosition, 2, WebGLRenderingContext.FLOAT, | |
228 false,0, 0); | |
217 | 229 |
218 var plotPositionBuffer = gl.createBuffer(); | 230 var plotPositionBuffer = gl.createBuffer(); |
219 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, plotPositionBuffer); | 231 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, plotPositionBuffer); |
220 gl.vertexAttribPointer(aPlotPosition, 3, WebGLRenderingContext.FLOAT, false, 0 , 0); | 232 gl.vertexAttribPointer(aPlotPosition, 3, WebGLRenderingContext.FLOAT, |
233 false, 0, 0); | |
221 } | 234 } |
222 | 235 |
223 class Vector { | 236 class Vector { |
224 var x; | 237 var x; |
225 var y; | 238 var y; |
226 var z; | 239 var z; |
227 | |
228 Vector(this.x, this.y, this.z); | 240 Vector(this.x, this.y, this.z); |
229 } | 241 } |
230 | 242 |
243 // TODO(gram): This should be using vector_math. | |
231 crossProd(v1, v2) => | 244 crossProd(v1, v2) => |
232 new Vector(v1.y*v2.z - v2.y*v1.z, | 245 new Vector(v1.y*v2.z - v2.y*v1.z, |
233 v1.z*v2.x - v2.z*v1.x, | 246 v1.z*v2.x - v2.z*v1.x, |
234 v1.x*v2.y - v2.x*v1.y); | 247 v1.x*v2.y - v2.x*v1.y); |
235 | 248 |
236 Vector normalize(v) { | 249 Vector normalize(v) { |
237 var l = 1 / Math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z); | 250 var l = 1 / Math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z); |
238 return new Vector( v.x*l, v.y*l, v.z*l ); | 251 return new Vector( v.x*l, v.y*l, v.z*l ); |
239 } | 252 } |
240 | 253 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 // cameraCenter + cameraUp + cameraLeft * ratio | 291 // cameraCenter + cameraUp + cameraLeft * ratio |
279 var cameraTopLeft = vectAdd(vectAdd(cameraCenter, cameraUp), | 292 var cameraTopLeft = vectAdd(vectAdd(cameraCenter, cameraUp), |
280 vectMul(cameraLeft, ratio)); | 293 vectMul(cameraLeft, ratio)); |
281 var cameraBotLeft = vectAdd(vectSub(cameraCenter, cameraUp), | 294 var cameraBotLeft = vectAdd(vectSub(cameraCenter, cameraUp), |
282 vectMul(cameraLeft, ratio)); | 295 vectMul(cameraLeft, ratio)); |
283 var cameraTopRight = vectSub(vectAdd(cameraCenter, cameraUp), | 296 var cameraTopRight = vectSub(vectAdd(cameraCenter, cameraUp), |
284 vectMul(cameraLeft, ratio)); | 297 vectMul(cameraLeft, ratio)); |
285 var cameraBotRight = vectSub(vectSub(cameraCenter, cameraUp), | 298 var cameraBotRight = vectSub(vectSub(cameraCenter, cameraUp), |
286 vectMul(cameraLeft, ratio)); | 299 vectMul(cameraLeft, ratio)); |
287 | 300 |
288 // corners = [1.2, 1, -12, -1.2, 1, -12, 1.2, -1, -12, -1.2, -1, -12]; | |
289 var corners = []; | 301 var corners = []; |
290 pushVec(cameraTopRight, corners); | 302 pushVec(cameraTopRight, corners); |
291 pushVec(cameraTopLeft, corners); | 303 pushVec(cameraTopLeft, corners); |
292 pushVec(cameraBotRight, corners); | 304 pushVec(cameraBotRight, corners); |
293 pushVec(cameraBotLeft, corners); | 305 pushVec(cameraBotLeft, corners); |
294 var cornersArray = new Float32Array.fromList(corners); | 306 |
295 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, cornersArray, | 307 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, wrapVertices(corners), |
296 WebGLRenderingContext.STATIC_DRAW); | 308 WebGLRenderingContext.STATIC_DRAW); |
297 | 309 |
298 gl.uniform3f(cameraPos, cameraFrom.x, cameraFrom.y, cameraFrom.z); | 310 gl.uniform3f(cameraPos, cameraFrom.x, cameraFrom.y, cameraFrom.z); |
299 gl.uniform3f(sphere1Center, x1, y1, z1); | 311 gl.uniform3f(sphere1Center, x1, y1, z1); |
300 gl.uniform3f(sphere2Center, x2, y2, z2); | 312 gl.uniform3f(sphere2Center, x2, y2, z2); |
301 gl.uniform3f(sphere3Center, x3, y3, z3); | 313 gl.uniform3f(sphere3Center, x3, y3, z3); |
302 | 314 |
303 gl.drawArrays(WebGLRenderingContext.TRIANGLE_STRIP, 0, 4); | 315 gl.drawArrays(WebGLRenderingContext.TRIANGLE_STRIP, 0, 4); |
304 | 316 |
305 t += 0.03; | 317 t += 0.03; |
306 if (t > Math.PI * 200) { | 318 if (t > Math.PI * 200) { |
307 t -= Math.PI * 200; | 319 t -= Math.PI * 200; |
308 } | 320 } |
309 } | 321 } |
310 | 322 |
311 void setup() { | 323 void setup(int width, int height) { |
312 initShaders(); | 324 initShaders(); |
313 gl.clearColor(0.0, 0.0, 0.0, 1.0); | 325 gl.clearColor(0.0, 0.0, 0.0, 1.0); |
314 gl.clearDepth(1.0); | 326 gl.clearDepth(1.0); |
315 initBuffers(); | 327 initBuffers(); |
328 resize(width, height); | |
316 } | 329 } |
317 | 330 |
318 void resize(int width, int height) { | 331 void resize(int width, int height) { |
319 ratio = width / height; | 332 ratio = width / height; |
320 gl.viewport(0, 0, width, height); | 333 gl.viewport(0, 0, width, height); |
321 t -= 0.03; | 334 t -= 0.03; |
322 drawScene(); | 335 drawScene(); |
323 } | 336 } |
324 | 337 |
325 void draw() { | 338 void update() { |
326 drawScene(); | 339 drawScene(); |
327 } | 340 } |
341 | |
342 /* | |
343 onMotionDown(num when, num x, num y) {} | |
344 onMotionUp(num when, num x, num y) {} | |
345 onMotionMove(num when, num x, num y) {} | |
346 onMotionCancel(num when, num x, num y) {} | |
347 onMotionOutside(num when, num x, num y) {} | |
348 onMotionPointerDown(num when, num x, num y) {} | |
349 onMotionPointerUp(num when, num x, num y) {} | |
350 onKeyDown(num when, int flags, int keycode, int metastate, int repeat) {} | |
351 onKeyUp(num when, int flags, int keycode, int metastate, int repeat) {} | |
352 onKeyMultiple(num when, int flags, int keycode, int metastate, int repeat) { | |
353 } | |
354 */ | |
vsm
2013/01/16 05:16:02
delete?
| |
355 | |
OLD | NEW |