Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: samples/android/assets/dart/simplegl.dart

Issue 11362103: Rotating spheres sample (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: More cleanup Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/android/assets/dart/gl.dart ('k') | samples/android/proguard-project.txt » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 #library("simplegl");
6
7 #import("gl.dart");
8
9 #import("dart:math", prefix: 'Math');
10
11 const FRAGMENT_PROGRAM = """
12 precision mediump float;
13
14 const vec3 lightDir = vec3(0.577350269, 0.577350269, -0.577350269);
15 varying vec3 vPosition;
16 uniform vec3 cameraPos;
17 uniform vec3 sphere1Center;
18 uniform vec3 sphere2Center;
19 uniform vec3 sphere3Center;
20
21 bool intersectSphere(vec3 center, vec3 lStart, vec3 lDir,
22 out float dist) {
23 vec3 c = center - lStart;
24 float b = dot(lDir, c);
25 float d = b*b - dot(c, c) + 1.0;
26 if (d < 0.0) {
27 dist = 10000.0;
28 return false;
29 }
30
31 dist = b - sqrt(d);
32 if (dist < 0.0) {
33 dist = 10000.0;
34 return false;
35 }
36
37 return true;
38 }
39
40 vec3 lightAt(vec3 N, vec3 V, vec3 color) {
41 vec3 L = lightDir;
42 vec3 R = reflect(-L, N);
43
44 float c = 0.3 + 0.4 * pow(max(dot(R, V), 0.0), 30.0) + 0.7 * dot(L, N);
45
46 if (c > 1.0) {
47 return mix(color, vec3(1.6, 1.6, 1.6), c - 1.0);
48 }
49
50 return c * color;
51 }
52
53 bool intersectWorld(vec3 lStart, vec3 lDir, out vec3 pos,
54 out vec3 normal, out vec3 color) {
55 float d1, d2, d3;
56 bool h1, h2, h3;
57
58 h1 = intersectSphere(sphere1Center, lStart, lDir, d1);
59 h2 = intersectSphere(sphere2Center, lStart, lDir, d2);
60 h3 = intersectSphere(sphere3Center, lStart, lDir, d3);
61
62 if (h1 && d1 < d2 && d1 < d3) {
63 pos = lStart + d1 * lDir;
64 normal = pos - sphere1Center;
65 color = vec3(0.0, 0.0, 0.9);
66 if (fract(pos.x / 1.5) > 0.5 ^^
67 fract(pos.y / 1.5) > 0.5 ^^
68 fract(pos.z / 1.5) > 0.5) {
69 color = vec3(1.0, 0.0, 1.0);
70 }
71 else {
72 color = vec3(1.0, 1.0, 0.0);
73 }
74 }
75 else if (h2 && d2 < d3) {
76 pos = lStart + d2 * lDir;
77 normal = pos - sphere2Center;
78 color = vec3(0.9, mod(normal.y * 2.5, 1.0), 0.0);
79 }
80 else if (h3) {
81 pos = lStart + d3 * lDir;
82 normal = pos - sphere3Center;
83 color = vec3(0.0, clamp(sphere3Center.y/1.5, 0.0, 0.9),
84 clamp(0.9 - sphere3Center.y/1.5, 0.0, 0.9));
85 }
86 else if (lDir.y < -0.01) {
87 pos = lStart + ((lStart.y + 2.7) / -lDir.y) * lDir;
88 if (pos.x*pos.x + pos.z*pos.z > 30.0) {
89 return false;
90 }
91 normal = vec3(0.0, 1.0, 0.0);
92 if (fract(pos.x / 5.0) > 0.5 == fract(pos.z / 5.0) > 0.5) {
93 color = vec3(1.0);
94 }
95 else {
96 color = vec3(0.0);
97 }
98 }
99 else {
100 return false;
101 }
102
103 return true;
104 }
105
106 void main(void)
107 {
108 vec3 cameraDir = normalize(vPosition - cameraPos);
109
110 vec3 p1, norm, p2;
111 vec3 col, colT, colM, col3;
112 if (intersectWorld(cameraPos, cameraDir, p1,
113 norm, colT)) {
114 col = lightAt(norm, -cameraDir, colT);
115 colM = (colT + vec3(0.2)) / 1.2;
116 cameraDir = reflect(cameraDir, norm);
117 if (intersectWorld(p1, cameraDir, p2, norm, colT)) {
118 col += lightAt(norm, -cameraDir, colT) * colM;
119 colM *= (colT + vec3(0.2)) / 1.2;
120 cameraDir = reflect(cameraDir, norm);
121 if (intersectWorld(p2, cameraDir, p1, norm, colT)) {
122 col += lightAt(norm, -cameraDir, colT) * colM;
123 }
124 }
125
126 gl_FragColor = vec4(col, 1.0);
127 }
128 else {
129 gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
130 }
131 }
132 """;
133
134 const VERTEX_PROGRAM = """
135 attribute vec2 aVertexPosition;
136 attribute vec3 aPlotPosition;
137
138 varying vec3 vPosition;
139
140 void main(void)
141 {
142 gl_Position = vec4(aVertexPosition, 1.0, 1.0);
143 vPosition = aPlotPosition;
144 }
145 """;
146
147 loadShader(final type, final program) {
148 final shader = gl.createShader(type);
149 gl.shaderSource(shader, program);
150 gl.compileShader(shader);
151 if (gl.getShaderParameter(shader, WebGLRenderingContext.COMPILE_STATUS) != Web GLRenderingContext.TRUE) {
152 throw new Exception("Could not compile shader");
153 }
154
155 return shader;
156 }
157
158 var shaderProgram;
159 var aVertexPosition;
160 var aPlotPosition;
161 var cameraPos;
162 var sphere1Center;
163 var sphere2Center;
164 var sphere3Center;
165 var ratio;
166
167 initShaders() {
168 var vertexShader = loadShader(WebGLRenderingContext.VERTEX_SHADER, VERTEX_PROG RAM);
169 var fragmentShader = loadShader(WebGLRenderingContext.FRAGMENT_SHADER, FRAGMEN T_PROGRAM);
170
171 shaderProgram = gl.createProgram();
172 if (shaderProgram == 0) {
173 throw new Exception("Could not create program.");
174 }
175
176 gl.attachShader(shaderProgram, vertexShader);
177 gl.attachShader(shaderProgram, fragmentShader);
178 gl.linkProgram(shaderProgram);
179
180 if (gl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) ! = WebGLRenderingContext.TRUE) {
181 throw new Exception("Could not initialize shaders");
182 }
183
184 gl.useProgram(shaderProgram);
185
186 aVertexPosition = gl.getAttribLocation(shaderProgram, "aVertexPosition");
187 gl.enableVertexAttribArray(aVertexPosition);
188
189 aPlotPosition = gl.getAttribLocation(shaderProgram, "aPlotPosition");
190 gl.enableVertexAttribArray(aPlotPosition);
191
192 cameraPos = gl.getUniformLocation(shaderProgram, "cameraPos");
193 sphere1Center = gl.getUniformLocation(shaderProgram, "sphere1Center");
194 sphere2Center = gl.getUniformLocation(shaderProgram, "sphere2Center");
195 sphere3Center = gl.getUniformLocation(shaderProgram, "sphere3Center");
196 }
197
198 initBuffers() {
199 var vertexPositionBuffer = gl.createBuffer();
200 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, vertexPositionBuffer);
201 var vertices = [
202 1.0, 1.0,
203 -1.0, 1.0,
204 1.0, -1.0,
205 -1.0, -1.0,
206 ];
207
208 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, vertices, WebGLRenderingCont ext.STATIC_DRAW);
209 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, vertexPositionBuffer);
210 gl.vertexAttribPointer(aVertexPosition, 2, WebGLRenderingContext.FLOAT, false, 0, 0);
211
212 var plotPositionBuffer = gl.createBuffer();
213 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, plotPositionBuffer);
214 gl.vertexAttribPointer(aPlotPosition, 3, WebGLRenderingContext.FLOAT, false, 0 , 0);
215 }
216
217 class Vector {
218 var x;
219 var y;
220 var z;
221 Vector(this.x, this.y, this.z);
222 }
223
224 crossProd(v1, v2) {
225 return
226 new Vector(
227 v1.y*v2.z - v2.y*v1.z,
228 v1.z*v2.x - v2.z*v1.x,
229 v1.x*v2.y - v2.x*v1.y);
230 }
231
232 normalize(v) {
233 var l = Math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
234 return new Vector( v.x/l, v.y/l, v.z/l );
235 }
236
237 vectAdd(v1, v2) {
238 return new Vector( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z );
239 }
240
241 vectSub(v1, v2) {
242 return new Vector( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z );
243 }
244
245 vectMul(v, l) {
246 return new Vector( v.x*l, v.y*l, v.z*l );
247 }
248
249 pushVec(v, arr) {
250 arr.addAll([v.x, v.y, v.z]);
251 }
252
253 var t = 0;
254 drawScene() {
255 var x1 = Math.sin(t * 1.1) * 1.5;
256 var y1 = Math.cos(t * 1.3) * 1.5;
257 var z1 = Math.sin(t + Math.PI/3) * 1.5;
258 var x2 = Math.cos(t * 1.2) * 1.5;
259 var y2 = Math.sin(t * 1.4) * 1.5;
260 var z2 = Math.sin(t*1.25 - Math.PI/3) * 1.5;
261 var x3 = Math.cos(t * 1.15) * 1.5;
262 var y3 = Math.sin(t * 1.37) * 1.5;
263 var z3 = Math.sin(t*1.27) * 1.5;
264
265 var cameraFrom = new Vector(
266 Math.sin(t * 0.4) * 18,
267 Math.sin(t * 0.13) * 5 + 5,
268 Math.cos(t * 0.4) * 18 );
269 var cameraTo = new Vector(0.0, 0.0, 0.0);
270 var cameraPersp = 6.0;
271 var up = new Vector(0.0, 1.0, 0.0);
272 var cameraDir = normalize(vectSub(cameraTo, cameraFrom));
273
274 var cameraLeft = normalize(crossProd(cameraDir, up));
275 var cameraUp = normalize(crossProd(cameraLeft, cameraDir));
276 // cameraFrom + cameraDir * cameraPersp
277 var cameraCenter = vectAdd(cameraFrom, vectMul(cameraDir, cameraPersp));
278
279 // cameraCenter + cameraUp + cameraLeft * ratio
280 var cameraTopLeft = vectAdd(vectAdd(cameraCenter, cameraUp),
281 vectMul(cameraLeft, ratio));
282 var cameraBotLeft = vectAdd(vectSub(cameraCenter, cameraUp),
283 vectMul(cameraLeft, ratio));
284 var cameraTopRight = vectSub(vectAdd(cameraCenter, cameraUp),
285 vectMul(cameraLeft, ratio));
286 var cameraBotRight = vectSub(vectSub(cameraCenter, cameraUp),
287 vectMul(cameraLeft, ratio));
288
289 //corners = [1.2, 1, -12, -1.2, 1, -12, 1.2, -1, -12, -1.2, -1, -12];
290 var corners = [];
291 pushVec(cameraTopRight, corners);
292 pushVec(cameraTopLeft, corners);
293 pushVec(cameraBotRight, corners);
294 pushVec(cameraBotLeft, corners);
295
296 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, corners, WebGLRenderingConte xt.STATIC_DRAW);
297
298 gl.uniform3f(cameraPos, cameraFrom.x, cameraFrom.y, cameraFrom.z);
299 gl.uniform3f(sphere1Center, x1, y1, z1);
300 gl.uniform3f(sphere2Center, x2, y2, z2);
301 gl.uniform3f(sphere3Center, x3, y3, z3);
302
303 gl.drawArrays(WebGLRenderingContext.TRIANGLE_STRIP, 0, 4);
304
305 t += 0.03;
306 if (t > Math.PI * 200) {
307 t -= Math.PI * 200;
308 }
309 }
310
311 setup() {
312 initShaders();
313 gl.clearColor(0.0, 0.0, 0.0, 1.0);
314 gl.clearDepth(1.0);
315 initBuffers();
316 }
317
318 resize(int width, int height) {
319 ratio = width / height;
320 gl.viewport(0, 0, width, height);
321 t -= 0.03;
322 drawScene();
323 return "setup $width $height";
324 }
325
326 draw() {
327 drawScene();
328 return "draw";
329 }
OLDNEW
« no previous file with comments | « samples/android/assets/dart/gl.dart ('k') | samples/android/proguard-project.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698