WebGL Beginner's Guide - Code / 1727_09 / ch9_Car_Showroom.html

  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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
<html>

<head>
<title>WebGL Beginner's Guide - Chapter 9 - Car Show Room</title>
<meta http-equiv='content-type' content='text/html; charset=ISO-8859-1'>
<link href='css/styles.css'   type='text/css' rel='stylesheet'>
<link href='css/cars.css' type='text/css' rel='stylesheet' />
<link href='css/smoothness/jquery-ui-1.8.13.custom.css' type='text/css' rel='stylesheet' />
<link href='css/colorpicker.css'  type='text/css' rel='stylesheet'/>
<!-- GUI Libraries //-->
<script type='text/javascript' src='js/gui/jquery-1.5.1.min.js'></script>
<script type='text/javascript' src='js/gui/jquery-ui-1.8.13.custom.min.js'></script> 
<script type='text/javascript' src='js/gui/colorpicker.js'></script>
<!-- MATH Libraries //-->
<script type='text/javascript' src='js/math/gl-matrix-min.js'></script>
<!-- WEBGL Libraries //-->
<script type='text/javascript' src='js/webgl/Globals.js'></script>
<script type='text/javascript' src='js/webgl/Utils.js'></script>
<script type='text/javascript' src='js/webgl/Program.js'></script>
<script type='text/javascript' src='js/webgl/Scene.js'></script>
<script type='text/javascript' src='js/webgl/Axis.js'></script>
<script type='text/javascript' src='js/webgl/Floor.js'></script>
<script type='text/javascript' src='js/webgl/Camera.js'></script>
<script type='text/javascript' src='js/webgl/CameraInteractor.js'></script>
<script type='text/javascript' src='js/webgl/SceneTransforms.js'></script>
<script type='text/javascript' src='js/webgl/Texture.js'></script>
<script type='text/javascript' src='js/webgl/Lights.js'></script>
<script type='text/javascript' src='js/webgl/WebGLApp.js'></script>
<script type='text/javascript' src='js/webgl/Picker.js'></script>

<script id="shader-vs" type="x-shader/x-vertex">

const int NUM_LIGHTS = 4;

attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec4 aVertexColor;

uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat4 uNMatrix;

uniform bool uTranslateLights;
uniform vec3 uLightPosition[NUM_LIGHTS];

varying vec3 vNormal;
varying vec3 vLightRay[NUM_LIGHTS];
varying vec3 vEye[NUM_LIGHTS];

void main(void) {

     vec4 c = aVertexColor;
     vec4 vertex = uMVMatrix * vec4(aVertexPosition, 1.0);
     vNormal = vec3(uNMatrix * vec4(aVertexNormal, 1.0));
     vec4 lightPosition = vec4(0.0);
     
     if (uTranslateLights){
         for(int i=0; i < NUM_LIGHTS; i++){
           lightPosition =   uMVMatrix * vec4(uLightPosition[i], 1.0);
           vLightRay[i] = vertex.xyz - lightPosition.xyz;
           vEye[i] = -vec3(vertex.xyz);
         }
     }    
     else {
        for(int i=0; i < NUM_LIGHTS; i++){
          lightPosition = vec4(uLightPosition[i], 1.0);
          vLightRay[i] = vertex.xyz - lightPosition.xyz;
          vEye[i] = -vec3(vertex.xyz);
        }
    }
    gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
}	
</script>

<script id="shader-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif


//Light uniforms
const int NUM_LIGHTS = 4;
uniform vec3  uLa[NUM_LIGHTS];   //ambient
uniform vec3  uLd[NUM_LIGHTS];   //diffuse
uniform vec3  uLs[NUM_LIGHTS];   //specular
uniform vec3 uLightPosition[NUM_LIGHTS];

//Material uniforms
uniform vec3  uKa;   //ambient
uniform vec3  uKd;   //diffuse
uniform vec3  uKs;   //specular
uniform float uNs;   //specular coefficient
uniform float d;     //Opacity
uniform int   illum; //Illumination mode


uniform bool  uWireframe;


varying vec3 vNormal;
varying vec3 vLightRay[NUM_LIGHTS];
varying vec3 vEye[NUM_LIGHTS];

float calculateAttenuation(in vec3 ray){
    float dist = length(ray);
    return (1.0 / (0.1 * dist));
}

void main(void) {
    if (uWireframe || illum == 0){
        gl_FragColor = vec4(uKd,d);
        return;
    }
    
   vec3 COLOR = vec3(0.0,0.0,0.0);
   vec3 N =  normalize(vNormal);
   vec3 L =  vec3(0.0,0.0,0.0);
   vec3 E =  vec3(0.0,0.0,0.0);
   vec3 R =  vec3(0.0,0.0,0.0);
   vec3 deltaRay = vec3(0.0);
   const int  lsize = 2;
   const float step = 0.25;
   const float inv_total = 1.0/((float(lsize*lsize) + 1.0)*(float(lsize*lsize) + 1.0));  //how many deltaRays
   
   float dx = 0.0;
   float dz = 0.0;
   float LT = 0.0;
   
   if (illum == 1){
        for(int i = 0; i < NUM_LIGHTS; i++){	
            L = normalize(vLightRay[i]);	
            N = normalize(vNormal);	
            COLOR += (uLa[i] * uKa) + (uLd[i] * uKd * clamp(dot(N, -L),0.0,1.0));
        }   
        gl_FragColor =  vec4(COLOR,d);
        return;
   }
   
   if (illum == 2){
        for(int i = 0; i < NUM_LIGHTS; i++){

            E = normalize(vEye[i]);
            L = normalize(vLightRay[i]);
            R = reflect(L, N);
            COLOR += (uLa[i] * uKa);
            COLOR += (uLd[i] * uKd * clamp(dot(N,-L),0.0,1.0));// * calculateAttenuation(vLightRay[i]));
            COLOR += (uLs[i] * uKs * pow( max(dot(R, E), 0.0), uNs) * 4.0);
        }
        gl_FragColor =  vec4(COLOR,d);
        return;
   }
}
</script>

<script type="text/javascript">

var camera = null,
light1 = null,
light2 = null,
light3 = null,
light4 = null,
transforms = null,

cameraHome = [0.0,0.4,7],
cameraAzimuth = 25,
cameraElevation = -11,
carSurface = undefined,
floorVisible = false,
translateLights = false;



function configure(){
    gl.clearColor(0.2,0.2,0.2, 1.0);
    gl.clearDepth(1.0);
    
    gl.enable(gl.DEPTH_TEST);
    gl.depthFunc(gl.LEQUAL);
    gl.enable(gl.BLEND);
    gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); 

    
  
    //Creates and sets up the camera location
    camera = new Camera(CAMERA_ORBITING_TYPE);
    camera.goHome(cameraHome);
    camera.setFocus([0.0,0.0,0.0]);
	camera.setAzimuth(cameraAzimuth);
	camera.setElevation(cameraElevation);
    camera.hookRenderer = render;
    
    //Creates and sets up the mouse and keyboard interactor
    var interactor = new CameraInteractor(camera, document.getElementById('the-canvas'));
    
    //Scene Transforms
    transforms = new SceneTransforms(camera);
   
    //init transforms
    transforms.init();

    light1 = new Light('far-left');
	light1.setPosition([-25,25,-25]);
	light1.setDiffuse([0.4,0.4,0.4]);
    light1.setAmbient([0.0,0.0,0.0]);
    light1.setSpecular([0.8,0.8,0.8]);
	
	light2 = new Light('far-right');
	light2.setPosition([25,25,-25]);
	light2.setDiffuse([0.4,0.4,0.4]);
    light2.setAmbient([0.0,0.0,0.0]);
    light2.setSpecular([0.8,0.8,0.8]);
	
	light3 = new Light('near-left');
	light3.setPosition([-25,25,25]);
	light3.setDiffuse([0.4,0.4,0.4]);
    light3.setAmbient([0.0,0.0,0.0]);
    light3.setSpecular([0.8,0.8,0.8]);
    
    light4 = new Light('near-right');
    light4.setPosition([25,25,25]);
    light4.setDiffuse([0.4,0.4,0.4]);
    light4.setAmbient([0.0,0.0,0.0]);
    light4.setSpecular([0.8,0.8,0.8]);
    
    
	Lights.add(light1);
	Lights.add(light2);
    Lights.add(light3);	
	Lights.add(light4);	
    
    //init Program
	var attributeList = ["aVertexPosition",
					"aVertexNormal",
					"aVertexColor"];

	var uniformList = [	"uPMatrix", 
					"uMVMatrix", 
					"uNMatrix",
                    "uLightPosition",
                    "uWireframe",
                    "uLa",
                    "uLd",
                    "uLs",
                    "uKa",
                    "uKd",
                    "uKs",
                    "uNs",
                    "d",
                    "illum",
                    "uTranslateLights"
					];
					
	Program.load(attributeList, uniformList);
	
	
    gl.uniform3fv(Program.uLightPosition, Lights.getArray('position'));
    gl.uniform3fv(Program.uLa ,     Lights.getArray('ambient'));
    gl.uniform3fv(Program.uLd,      Lights.getArray('diffuse'));
    gl.uniform3fv(Program.uLs,      Lights.getArray('specular'));
    
    gl.uniform3fv(Program.uKa ,     [1.0,1.0,1.0]);
    gl.uniform3fv(Program.uKd ,     [1.0,1.0,1.0]);
    gl.uniform3fv(Program.uKs ,     [1.0,1.0,1.0]);
    
    gl.uniform1f(Program.uNs, 1.0);
    gl.uniform1i(Program.uTranslateLights, translateLights);

}

function loadBMW(){
    Scene.objects = [];
    Scene.addObject(Floor);
    for(var i = 1; i <= 24; i+=1){
        Scene.loadObject('models/cars/bmw/part'+i+'.json');

    }
}

function loadSphere(){
    Scene.objects = [];
    Scene.addObject(Floor);
    Scene.loadObject('models/sphere.json','sphere');
}

function loadMustang(){
    Scene.objects = [];
    Scene.addObject(Floor);
    Scene.loadObject('models/cars/bmw/part5.json','world');
    for(var i = 1; i <= 103; i+=1){
        Scene.loadObject('models/cars/mustang/part'+i+'.json');
    }
   
}

function loadBMWV3(){
    Scene.objects = [];
    Scene.addObject(Floor);
    Scene.loadObject('models/cars/bmw/part5.json','world');
    for(var i = 1; i <= 62; i++){
        Scene.loadObject('models/cars/bmwv3/part'+i+'.json');
    }    
}

function loadAudi(){
    Scene.objects = [];
    Scene.addObject(Floor);
    Scene.loadObject('models/cars/bmw/part5.json','world');
    for(var i = 1; i <= 150; i+=1){
        Scene.loadObject('models/cars/audi/part'+i+'.json');
    }
}

function loadLamborghini(){
    Scene.objects = [];
    Scene.addObject(Floor);
    for(var i = 1; i <= 67; i+=1){
        Scene.loadObject('models/cars/lamborghini/part'+i+'.json');
    }
}





function loadNissan(){
    for(var i = 1; i <= 46; i+=1){
        Scene.loadObject('models/cars/nissan/part'+i+'.json');
    }
}

var selectedCar;
function load(){
    Floor.build(80,2);
    Floor.Ka = [1,1,1];
    Floor.Kd = [0.6,0.6,0.6]
    Floor.Ks = [1,1,1];
    Floor.Ni = 1;
    Floor.Ns = 1;
    Floor.d = 1.0;
    Floor.illum = 1;
   
    Floor.visible = floorVisible;
    
    Scene.addObject(Floor);
    loadBMW();
    selectedCar = 'bmwi8';
}

function render(){
    gl.viewport(0, 0, c_width, c_height);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    
   // orderObjectsInScene();
    transforms.updatePerspective();
    
    try{
        
        for (var i = 0; i < Scene.objects.length; i++){
            
            var object = Scene.objects[i];
			if (object.visible != undefined && !object.visible) continue;
			transforms.calculateModelView();           
            transforms.push();
            transforms.setMatrixUniforms();
            transforms.pop();
            
            
   
            gl.enableVertexAttribArray(Program.aVertexPosition);
            gl.disableVertexAttribArray(Program.aVertexNormal);
            gl.disableVertexAttribArray(Program.aVertexColor);
            
            gl.uniform1i(Program.uWireframe, false);
            gl.uniform3fv(Program.uKa, object.Ka);
            gl.uniform3fv(Program.uKd, object.Kd);
            gl.uniform3fv(Program.uKs, object.Ks);
            gl.uniform1f(Program.uNs, object.Ns);
            gl.uniform1f(Program.d, object.d);
            gl.uniform1i(Program.illum, object.illum);
            
           if(object.d < 1.0){  //tweaking parameters here
                switch(selectedCar){
                    case 'bmwv3': gl.uniform1f(Program.d, 0.14); break;
                    case 'audi' : gl.uniform1f(Program.d, 0.8); break;
                    case 'mustang': gl.uniform1f(Program.d, 0.3); break;
                    case 'bmwi8': break; //nothing to do here
                }
			}
           
            
            gl.bindBuffer(gl.ARRAY_BUFFER, object.vbo);
            gl.vertexAttribPointer(Program.aVertexPosition, 3, gl.FLOAT, false, 0, 0);
            gl.enableVertexAttribArray(Program.aVertexPosition);
            
			if(!object.wireframe){
				gl.bindBuffer(gl.ARRAY_BUFFER, object.nbo);
				gl.vertexAttribPointer(Program.aVertexNormal, 3, gl.FLOAT, false, 0, 0);
				gl.enableVertexAttribArray(Program.aVertexNormal);
            }
            else{
                gl.uniform1i(Program.uWireframe, true);
            }
			
            gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, object.ibo);
			
			if (object.wireframe){
                gl.drawElements(gl.LINES, object.indices.length, gl.UNSIGNED_SHORT,0);
            }
            else{
                gl.drawElements(gl.TRIANGLES, object.indices.length, gl.UNSIGNED_SHORT,0);
            }
			
            gl.bindBuffer(gl.ARRAY_BUFFER, null);
            gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);

        }
    }
    catch(err){
        alert(err);
        console.error(err.description);
    }
}

function resizeCanvas(){
    c_width = $('#content').width();
    c_height = $('#content').height();
    $('#the-canvas').attr('width',c_width);
    $('#the-canvas').attr('height',c_height);
}

$(window).resize(function(){resizeCanvas();});

var app;
function runShowRoom(){
    app = new WebGLApp("the-canvas");
    app.configureGLHook = configure;
    app.loadSceneHook   = load;
    app.drawSceneHook   = render;
    app.run(); 
}
</script>
</head>

<body onLoad='runShowRoom()'>
<div id="header">
    <h1>WebGL Beginner's Guide - Chapter 9 - Virtual Car Showroom</h1>
    <h2>Customize your Car</h2>
    <div id='logo-packt'><img src='packt.gif'/></div>
</div>

<div id="nav">
  
    <table width='100%' cellspacing ='0'>
    <tr><td colspan=3><h3>Car</h3></td></tr>
    <tr><td width='40%'> Select the car to load: </td><td colspan='2' width='60%'>
        <select id='select-car' >
                    <option value ='bmwi8'> BMW I8</option>
                    <option value ='mustang'> Ford Mustang </option>
                    <option value ='audi'> Audi R8 </option>
                    <option value ='bmwv3'> BMW v3 </option>
        </select>
        </td>
    </tr>
    <tr>
       <td>Car Color:</td>
       <td colspan=2><div id='carColor' class='colorSelector'><div style='background-color:rgb(255,255,255)'></div></div></td>
    </tr>
    <tr>
        <td>Car Shininness:</td>
        <td width='130px'><div id='slider-shininess'/></td>
        <td id='slider-shininess-value' width='60px'>  0</td>
   	</tr>
    </table>
    
    <hr/>
    
    <table width='100%'>
        <tr>
            <td><h3>Lights</h3></td>
        </tr>
		<tr>
            <td>
                <table style='text-align:center'>
                <tr>
                    <td></td><td>Ambient</td><td></td>
                    <td>Diffuse</td><td></td>
                    <td>Specular</td><td></td>
                </tr>    
                <tr>
                    <td style='text-align:right' width='160px'>far-left: </td>
                    <td width='150px'><div id='slider-la1'/></td><td id='slider-la1-value' width='20px'>0.0</td>
                    <td width='150px'><div id='slider-ld1'/></td><td id='slider-ld1-value' width='20px'>0.4</td>
                    <td width='150px'><div id='slider-ls1'/></td><td id='slider-ls1-value' width='20px'>0.8</td>
                </tr>
                <tr>
                    <td style='text-align:right' width='160px'>far-right: </td>
                    <td width='150px'><div id='slider-la2'/></td><td id='slider-la2-value' width='20px'>0.0</td>
                    <td width='150px'><div id='slider-ld2'/></td><td id='slider-ld2-value' width='20px'>0.4</td>
                    <td width='150px'><div id='slider-ls2'/></td><td id='slider-ls2-value' width='20px'>0.8</td>
                </tr>
                <tr>
                    <td style='text-align:right' width='160px'>near-left: </td>
                    <td width='150px'><div id='slider-la3'/></td><td id='slider-la3-value' width='20px'>0.0</td>
                    <td width='150px'><div id='slider-ld3'/></td><td id='slider-ld3-value' width='20px'>0.4</td>
                    <td width='150px'><div id='slider-ls3'/></td><td id='slider-ls3-value' width='20px'>0.8</td>
                </tr>
                <tr>
                    <td style='text-align:right' width='160px'>near-right: </td>
                    <td width='150px'><div id='slider-la4'/></td><td id='slider-la4-value' width='20px'>0.0</td>
                    <td width='150px'><div id='slider-ld4'/></td><td id='slider-ld4-value' width='20px'>0.4</td>
                    <td width='150px'><div id='slider-ls4'/></td><td id='slider-ls4-value' width='20px'>0.8</td>
                </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td align='right'><input type="checkbox" id="translate-lights"/> Translate Lights<br /></td>
        </tr>    
    </table>
    <hr/>
    <table width='100%'>
        <tr>
            <td><h3>Camera</h3></td>
        </tr>
        <tr>
        <td align='center'>
        <div id='opt-pose' >
            <input type='radio' id='opt-home'  name='posecam' /><label for='opt-home'>Home</label>
            <input type='radio' id='opt-above' name='posecam' /><label for='opt-above'>Above</label>
            <input type='radio' id='opt-front' name='posecam' /><label for='opt-front'>Front</label>
            <input type='radio' id='opt-back'  name='posecam' /><label for='opt-back'>Back</label>
            <input type='radio' id='opt-left'  name='posecam' /><label for='opt-left'>Left</label>
            <input type='radio' id='opt-right' name='posecam' /><label for='opt-right'>Right</label>
            
         </div>
         </td>
         </tr>
         <tr>
            <td align='center'>Zoom: Alt + Drag <input type="checkbox" id="show-floor"/>Floor Visible<br /></td>
         </tr>          
    </table>        

    
</div>

<div id="content">
    <canvas id='the-canvas'></canvas>
</div>

<script type='text/javascript'>resizeCanvas();</script>
<script type='text/javascript'>
    $('#opt-pose').buttonset();
    $('#slider-shininess').slider({value:0.0, min:0.0, max:1.0, step:0.1, slide:function(event, ui){updateShininessSelectedCar(ui)}, change:function(event,ui){updateShininessSelectedCar(ui)}});
    $('#slider-la1').slider({value:0.0, min:0.0, max:1.0, step:0.1, slide:function(){updateLightProperty(1, 'a')}, change:function(){updateLightProperty(1,'a')}});
    $('#slider-ld1').slider({value:0.4, min:0.0, max:1.0, step:0.1, slide:function(){updateLightProperty(1, 'd')}, change:function(){updateLightProperty(1,'d')}});
    $('#slider-ls1').slider({value:0.8, min:0.0, max:1.0, step:0.1, slide:function(){updateLightProperty(1, 's')}, change:function(){updateLightProperty(1,'s')}});
    $('#slider-la2').slider({value:0.0, min:0.0, max:1.0, step:0.1, slide:function(){updateLightProperty(2, 'a')}, change:function(){updateLightProperty(2,'a')}});
    $('#slider-ld2').slider({value:0.4, min:0.0, max:1.0, step:0.1, slide:function(){updateLightProperty(2, 'd')}, change:function(){updateLightProperty(2,'d')}});
    $('#slider-ls2').slider({value:0.8, min:0.0, max:1.0, step:0.1, slide:function(){updateLightProperty(2, 's')}, change:function(){updateLightProperty(2,'s')}});
    $('#slider-la3').slider({value:0.0, min:0.0, max:1.0, step:0.1, slide:function(){updateLightProperty(3, 'a')}, change:function(){updateLightProperty(3,'a')}});
    $('#slider-ld3').slider({value:0.4, min:0.0, max:1.0, step:0.1, slide:function(){updateLightProperty(3, 'd')}, change:function(){updateLightProperty(3,'d')}});
    $('#slider-ls3').slider({value:0.8, min:0.0, max:1.0, step:0.1, slide:function(){updateLightProperty(3, 's')}, change:function(){updateLightProperty(3,'s')}});
    $('#slider-la4').slider({value:0.0, min:0.0, max:1.0, step:0.1, slide:function(){updateLightProperty(4, 'a')}, change:function(){updateLightProperty(4,'a')}});
    $('#slider-ld4').slider({value:0.4, min:0.0, max:1.0, step:0.1, slide:function(){updateLightProperty(4, 'd')}, change:function(){updateLightProperty(4,'d')}});
    $('#slider-ls4').slider({value:0.8, min:0.0, max:1.0, step:0.1, slide:function(){updateLightProperty(4, 's')}, change:function(){updateLightProperty(4,'s')}});


$('#select-car').change(function(){
    selectedCar = $('#select-car').val();
    switch (selectedCar){
        case 'bmwi8'    :loadBMW();     break;
        case 'mustang'  :loadMustang(); break;
        case 'audi'     :loadAudi();    break;
        case 'bmwv3'    :loadBMWV3();   break;
    }
        
});  
  
$('#show-floor').change(function(){
	    floorVisible = !floorVisible;
        Floor.visible = floorVisible;
});

$('#translate-lights').change(function(){
    translateLights = !translateLights;
    gl.uniform1i(Program.uTranslateLights, translateLights);
});

function updateShininessSelectedCar(shine){

    $('#slider-shininess-value').html(shine.value);
    

    for(var i = 0, N = Scene.objects.length; i < N; i+=1){
        for(var j =0; j <3; j+=1){
        Scene.objects[i].Ks[j] = shine.value;
        }
    }
    app.refresh();
   
}

function updateLightProperty(index,property){
    var v = $('#slider-l'+property+''+index).slider("value");
    $('#slider-l'+property+''+index+'-value').html(v);
    var light = undefined;
    switch(index){
                case 1: light = light1; break;
                case 2: light = light2; break;
                case 3: light = light3; break;
                case 4: light = light4; break;
    }
    
    switch(property){
                case 'a':light.setAmbient([v,v,v]);
                         gl.uniform3fv(Program.uLa, Lights.getArray('ambient'));
                         break;
                case 'd':light.setDiffuse([v,v,v]);
                         gl.uniform3fv(Program.uLd, Lights.getArray('diffuse'));
                         break;
                case 's':light.setSpecular([v,v,v]);
                         gl.uniform3fv(Program.uLs, Lights.getArray('specular'));
                         break;
    }

    render();
}

function updateColorSelectedCar(color){
    var parts = [];
    switch(selectedCar){
        case 'bmwv3': parts.push(Scene.getObject('Shell_Mesh.023_Body'));
                  break;
        case 'mustang': for(var i=0, N = Scene.objects.length; i < N; i+=1){
                            if (Scene.objects[i].alias.indexOf('_pintura_carro') != -1){
                                parts.push(Scene.objects[i]);
                            }
                        }
                        break;
        case 'audi': for(var i=0, N = Scene.objects.length; i < N; i+=1){
                            if (Scene.objects[i].alias.indexOf('_Lack') != -1){
                                parts.push(Scene.objects[i]);
                            }
                        }
                        break;
        case 'bmwi8': for(var i=0, N = Scene.objects.length; i < N; i+=1){
                            if (Scene.objects[i].alias.indexOf('_BMW') != -1){
                                parts.push(Scene.objects[i]);
                            }
                        }
                        break;
    }
    var object;
    for(var i = 0, N = parts.length; i < N; i+=1){
        parts[i].Kd = color;
    }
    app.refresh();
};

var carColorHex = '#ffffff';
$('#carColor').ColorPicker({
    onSubmit: function(hsb, hex, rgb, el) {
            $(el).val(hex);
            $(el).ColorPickerHide();
            
    },
    color: '#00ff00',
    onShow: function (colpkr) {
        $(colpkr).fadeIn(500);
        return false;
    },
    onHide: function (colpkr) {
        $(colpkr).fadeOut(500);
        return false;
    },
    onChange: function (hsb, hex, rgb) {
        carColorHex = hex;
        $('#carColor div').css('backgroundColor', '#' + hex);
        updateColorSelectedCar([rgb.r/256,rgb.g/256,rgb.b/256]);
    },
    
    onBeforeShow: function (colpkr) {
            $(colpkr).ColorPickerSetColor(carColor);
        }
    });


var timer_anim_camera = undefined;
var goal_camera_azimuth = 0;
var goal_camera_elevation = 0;
var goal_camera_home = cameraHome;



$('#opt-home').click(function(){
    if(timer_anim_camera) { clearInterval(timer_anim_camera); }
    goal_camera_azimuth = cameraAzimuth;
    goal_camera_elevation = cameraElevation;
    timer_anim_camera = setInterval(animCamera, 5);
});

$('#opt-above').click(function(){
    if(timer_anim_camera) { clearInterval(timer_anim_camera); }
    goal_camera_azimuth = 0;
    goal_camera_elevation = -90;
    timer_anim_camera = setInterval(animCamera, 5);
});

$('#opt-front').click(function(){
    if(timer_anim_camera) { clearInterval(timer_anim_camera); }
    goal_camera_azimuth = 0;
    goal_camera_elevation = -10;
    timer_anim_camera = setInterval(animCamera, 5);
});

$('#opt-back').click(function(){
    if(timer_anim_camera) { clearInterval(timer_anim_camera); }
    goal_camera_azimuth = 180;
    goal_camera_elevation = -10;
    timer_anim_camera = setInterval(animCamera, 5);
});

$('#opt-left').click(function(){
    if(timer_anim_camera) { clearInterval(timer_anim_camera); }
    goal_camera_azimuth = -90;
    goal_camera_elevation = 0;
    timer_anim_camera = setInterval(animCamera, 5);
});

$('#opt-right').click(function(){
    if(timer_anim_camera) { clearInterval(timer_anim_camera); }
    goal_camera_azimuth = 90;
    goal_camera_elevation = 0;
    timer_anim_camera = setInterval(animCamera, 5);
});



function animCamera(){

    var ca = goal_camera_azimuth-camera.azimuth;
    var ce = goal_camera_elevation-camera.elevation;
    var deltaPos = vec3.create([goal_camera_home[0]-camera.position[0], 
                    goal_camera_home[1]-camera.position[1], 
                    goal_camera_home[2]-camera.position[2]]);
                    
    
    if (Math.abs(ca) < 0.5 && 
        Math.abs(ce) < 0.5 && 
        Math.abs(deltaPos[0]  < 0.1) &&
        Math.abs(deltaPos[1]  < 0.1) &&
        Math.abs(deltaPos[2]  < 0.1))
    {
        camera.setAzimuth(goal_camera_azimuth);
        camera.setElevation(goal_camera_elevation);
        camera.setPosition(goal_camera_home);
        clearInterval(timer_anim_camera);
    }
    else{
        camera.setAzimuth  (camera.azimuth   + ca/100);
        camera.setElevation(camera.elevation + ce/100);
        vec3.scale(deltaPos, 1/100);
        vec3.add(camera.position,deltaPos,deltaPos);
        camera.setPosition(deltaPos);
        render();
    }
}    


</script>
</body>
</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.