Normand Briere
2019-09-06 9cc83b97378c48bae3792064f2d01b2f954c0e01
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
// Decompiled by Jad v1.5.7b. Copyright 1997-99 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Camera.java
 
class Camera extends Object3D
{
    static final long serialVersionUID = 4754289789178680517L;
    
   /*static*/ float DECAL = 12; // 3;
   /*static*/ float SCALE = 1; // 0.5f;
   
   float shaper_fovy  = 25.0f;
   float shaper_zNear = 0.01f;
   float shaper_zFar  = 1E5f; // 500.0f;
        
    Camera()
    {
        super("Camera");
        //<-2.2,0.7,-2> and pointed it at <-0.61,0.3,-0.6>
    //    location = LA.newVector(2,0.7,-2);
    //    lookAt = new cVector(0.6,0.3,-0.6);
        location = LA.newVector(6,2,4);
        lookAt = new cVector(0.0,0.5,0);
        direction = new cVector();
        toParent = LA.newMatrix();
        fromParent = LA.newMatrix();
        toScreen = //toParent; // may 2013
                    LA.newMatrix();
        fromScreen = //fromParent; // may 2013
                    LA.newMatrix();
        initCamera();
        perspective = true;
    }
 
    Camera(int viewCode)
    {
        super("Camera" + CameraPane.Name(viewCode));
        this.viewCode = viewCode;
        lookAt = new cVector();
        direction = new cVector();
        toParent = LA.newMatrix();
        fromParent = LA.newMatrix();
        toScreen = //toParent; // may 2013
                    LA.newMatrix();
        fromScreen = //fromParent; // may 2013
                    LA.newMatrix();
        switch (viewCode)
        {
        case 0:
            location = LA.newVector(0, 0, 20);
            perspective = true;
            break;
        case 1: // '\001'
            location = LA.newVector(0, 0, 4); // Needed for translation direction
            perspective = false;
            break;
        case 2: // '\002'
            location = LA.newVector(0, 4, 0);
            //UP = LA.newVector(1, 0, 0);
            perspective = false;
            //LA.matXRotate(toScreen, LA.toRadians(-90));
            //LA.matXRotate(fromScreen, LA.toRadians(90));
            break;
        case 3: // '\003'
            location = LA.newVector(4, 0, 0); // Needed for translation direction
            //LA.matZRotate(toScreen, LA.toRadians(-90));
            //LA.matXRotate(toScreen, LA.toRadians(-90));
            //LA.matXRotate(fromScreen, LA.toRadians(90));
            //LA.matZRotate(fromScreen, LA.toRadians(90));
            perspective = false;
            break;
        case 4: // Default light
            location = LA.newVector(-5, 15, 10);
            perspective = false;
            break;
        }
        
   computeTransform();
        initCamera();
    }
 
    Camera(cVector from)
    {
        super("Camera" + 0);
        viewCode = 0;
        location = new cVector();
        lookAt = new cVector();
        direction = new cVector();
        toParent = LA.newMatrix();
        fromParent = LA.newMatrix();
        toScreen = //toParent; // may 2013
                    LA.newMatrix();
        fromScreen = //fromParent; // may 2013
                    LA.newMatrix();
        setLocation(from);
   computeTransform();
        initCamera();
       perspective = true;
    }
 
    private void initCamera()
    {
        focalLength = 1;
        setAspect(4, 3);
        background = LA.newVector(0.8, 0.8, 0.8);
    }
 
    void CreateMaterial(boolean multiply)
    {
        super.CreateMaterial(multiply);
        
        material.shift = 90;
        material.cameralight = 0.2f;
        material.shadowbias = 10;
    }
 
    void setAspect(int width, int height)
    {
        //hAspect = width;
        //vAspect = height;
        aspect = (double)width / (double)height;
    }
 
    void setLocation(cVector from)
    {
        //setAim(from, lookAt);
        LA.vecCopy(from, location);
   //computeTransform();
    }
 
    void setFocus(cVector focus)
    {
        //setAim(location, focus);
        LA.vecCopy(focus, lookAt);
   //computeTransform();
    }
 
    cVector UP = LA.newVector(0, 1, 0);
    cVector UP2 = LA.newVector(1, 0, 0);
    
    // actual dynamic up vector
    cVector up = new cVector();
    
    cVector away = new cVector();
    cVector right = new cVector();
    
    double temp[][] = LA.newMatrix();
    
    void setAim(cVector from, cVector towards)
    {
        LA.vecCopy(from, location);
        LA.vecCopy(towards, lookAt);
        
        // refresh toScreen matrix
   computeTransform();
    }
    
   void RotatePosition(float dx, float dy)
   {
        if(hAspect != 0) return;
        
        if (CameraPane.IMAGEFLIP)
        {
            dx = -dx;
        }
        
        if (CameraPane.ROTATECAMERA)
        {
            float t = dx;
            dx = dy;
            dy = -t;
        }
        
            cStatic.point1.set(location);
            cStatic.point2.set(lookAt);
       LA.vecSub(location, lookAt, location);
       
                // avril 2014
   //     TransformToLocal(location);
                
       double angle = -dx / 200.0;
       quat.set(UP, angle);
       quat.rotate(location);
       
       angle = -dy / 200.0;
       quat.set(right, angle);
       quat.rotate(location);
       
                // avril 2014
   //     TransformToWorld(location);
        
       LA.vecAdd(location, lookAt, location);
 
       tempvec.set(right);
       
       computeTransform();
 
       if (tempvec.dot(right) < 0)
       {
           UP.mul(-1);
           computeTransform();
       }
 
            if(hAspect != 0)
            {
                location.set(cStatic.point1);
                lookAt.set(cStatic.point2);
            }
        }
   
   cVector tempvec = new cVector();
   cQuat quat = new cQuat();
   
   public double Distance()
   {
       LA.vecSub(location, lookAt, tempvec);
       return tempvec.length();
   }
   
   void Translate(float dx, float dy, float scale)
   {
        if(hAspect != 0) return;
        
        if (CameraPane.IMAGEFLIP)
        {
            dx = -dx;
        }
        
        if (CameraPane.ROTATECAMERA)
        {
            float t = dx;
            dx = dy;
            dy = -t;
        }
        
            cStatic.point1.set(location);
            cStatic.point2.set(lookAt);
            
       double dist = Distance();
       double factor = 1;
       
                scale /= shaper_fovy/60;
                
       //if (perspective)
           factor = dist;
       
       LA.vecCopy(up, tempvec);
       tempvec.x *= factor*dy/scale;
       tempvec.y *= factor*dy/scale;
       tempvec.z *= factor*dy/scale;
       
       LA.vecAdd(location, tempvec, location);
       LA.vecAdd(lookAt, tempvec, lookAt);
       
       dx *= -1;
       
       LA.vecCopy(right, tempvec);
       tempvec.x *= factor*dx/scale;
       tempvec.y *= factor*dx/scale;
       tempvec.z *= factor*dx/scale;
       
       LA.vecAdd(location, tempvec, location);
       LA.vecAdd(lookAt, tempvec, lookAt);
       
       computeTransform();
            if(hAspect != 0)
            {
                location.set(cStatic.point1);
                lookAt.set(cStatic.point2);
            }
   }
   
   void BackForth(float dx, float dy, float scale)
   {
        if(hAspect != 0) return;
            cStatic.point1.set(location);
            cStatic.point2.set(lookAt);
       if (!perspective)
       {
           //zoom *= Math.exp(dy);
           
           //return;
       }
       
                if (scale == 0)
                {
                    // Zoom
                    LA.vecSub(location, lookAt, location);
 
                    cVector p = location;
 
                    double factor = Math.exp(-dy/300.0); // (1 + dy/100);
 
                    p.x *= factor;
                    p.y *= factor;
                    p.z *= factor;
 
                    LA.vecAdd(location, lookAt, location);
                }
                else
                {
                    LA.vecSub(location, lookAt, direction);
 
                    //scale /= Distance();
                    scale /= shaper_fovy/20;
 
                    location.x -= dy * direction.x / scale;
                    //location.y -= dy * direction.y / scale;
                    location.z -= dy * direction.z / scale;
                    lookAt.x -= dy * direction.x / scale;
                    //lookAt.y -= dy * direction.y / scale;
                    lookAt.z -= dy * direction.z / scale;
                }
 
       computeTransform();
        if(hAspect != 0)
        {
            // Doesn't work
            location.set(cStatic.point1);
            lookAt.set(cStatic.point2);
        }
   }
   
   void RotateInterest(float dx, float dy)
   {
            cStatic.point1.set(location);
            location.set(lookAt);
            lookAt.set(cStatic.point1);
            right.mul(-1);
         //   UP.mul(-1);
            
            RotatePosition(dx,-dy);
            
         //   UP.mul(-1);
            right.mul(-1);
            cStatic.point1.set(location);
            location.set(lookAt);
            lookAt.set(cStatic.point1);
            
            computeTransform();
   }
   
    //synchronized // june 2014
            void computeTransform()
    {
        //new Exception().printStackTrace();
        if (toScreen == toParent) // dec 2013
        {
            toScreen = LA.newMatrix();
            fromScreen = LA.newMatrix();
        }
        
        cVector up2 = up;
        cVector right2 = right;
        
   up2.x = UP.x;
   up2.y = UP.y;
   up2.z = UP.z;
        LA.vecSub(lookAt, location, away);
        LA.vecNormalize(away);
        LA.vecCross(away, up2, right2);
       if (right2.length2() < 0.0001)
       {
           // UP failed
           up2.x = UP2.x;
           up2.y = UP2.y;
           up2.z = UP2.z;
           LA.vecSub(lookAt, location, away);
           LA.vecNormalize(away);
           LA.vecCross(away, up2, right2);
           if (!(right2.length2() > 0))
                        {
                            // old problem...
                            assert (right2.length2() <= 0 || right2.length2() > 0); // weirdest thing ever
                            up2.x = UP.x = 0;
                            up2.y = UP.y = 1;
                            up2.z = UP.z = 0;
                            LA.vecSub(lookAt, location, away);
                            LA.vecNormalize(away);
                            LA.vecCross(away, up2, right2);
                        }
       }
        LA.vecNormalize(right2);
        LA.vecCross(right2, away, up2);
       if (LA.vecLen2(up2) != 0)
       {
           LA.vecNormalize(up2);
           LA.matIdentity(toScreen);
           LA.matIdentity(fromScreen);
           for (int i=0; i < 3; i++)
           {
               toScreen[i][0] = right2.get(i);
               toScreen[i][1] = up2.get(i);
               toScreen[i][2] = -away.get(i);
               fromScreen[0][i] = right2.get(i);
               fromScreen[1][i] = up2.get(i);
               fromScreen[2][i] = -away.get(i);
           }
       }
 
        for (int i=0; i < 3; i++)
            fromScreen[3][i] = location.get(i);
 
        for (int i=0; i < 3; i++)
            temp[3][i] = -location.get(i);
 
        LA.matConcat(temp, toScreen, toScreen);
 
        if (CameraPane.ROTATECAMERA)
        {
            double[][] rotate = { { 0,1,0,0 }, { -1,0,0,0}, { 0,0,1,0}, { 0,0,0,1 } };
            //double[][] rotate_1 = { { 0,-1,0,0 }, { 1,0,0,0}, { 0,0,1,0}, { 0,0,0,1 } };
        
            LA.matConcat(fromScreen, rotate, fromScreen);
            //LA.matConcat(toScreen, rotate_1, toScreen);
        }
        
        if (CameraPane.IMAGEFLIP)
        {
            double[][] flip = { { -1,0,0,0 }, { 0,1,0,0}, { 0,0,1,0}, { 0,0,0,1 } };
        
            LA.matConcat(fromScreen, flip, fromScreen);
        }
        
       //if (this == CameraPane.lightCamera)
           //CameraPane.lighttouched = true;
        LA.matInvert(fromScreen, toScreen);
    }
 
//    void Draw(CameraPane display, Object3D /*Composite*/ root, boolean selected)
//    {
//        // don't draw anything aout 2013
//    }
    
    public void generatePOV(StringBuffer buf, int width, int height)
    {
        double imageAspect = (double)width / (double)height;
        if (imageAspect > aspect)
            width = (int)((double)height * aspect);
        else
            height = (int)((double)width / aspect);
        double right = width;
        double up = height;
        if (perspective)
        {
            right = aspect;
            up = 1;
        }
        buf.append("// ");
        buf.append(perspective ? "perspective" : "orthographic");
        buf.append(": aspect ");
        buf.append(hAspect);
        buf.append(" wide by ");
        buf.append(vAspect);
        buf.append(" high\n");
        buf.append("camera {\n");
        if (!perspective)
            buf.append("  orthographic\n");
        buf.append("  location ");
        LA.toPOVRay(location, buf);
        buf.append("\n  sky <0,0,1>\n");
        if (perspective)
        {
            buf.append("  right <");
            buf.append(hAspect);
            buf.append("/");
            buf.append(vAspect);
            buf.append(",0,0>\n  up <0,0,1>\n");
        } else
        {
            buf.append("  right <");
            buf.append(width);
            buf.append(",0,0>\n  up <0,0,");
            buf.append(height);
            buf.append(">\n");
        }
        buf.append("  direction <0,");
        buf.append(focalLength);
        buf.append(",0>\n  look_at ");
        LA.toPOVRay(lookAt, buf);
        buf.append("\n}\n");
        buf.append("light_source { ");
        LA.toPOVRay(location, buf);
        buf.append(" color rgb <1,1,1> }\n");
        buf.append("light_source { <0,0,500> color rgb <1,1,1> }\n\n");
    }
 
    static final int viewTop = 1;
    static final int viewFront = 2;
    static final int viewSide = 3;
    int viewCode;
    cVector location;
    cVector lookAt;
    cVector direction;
    double[][] toScreen;
    double[][] fromScreen;
    boolean perspective;
    int hAspect = 0; // Free camera
    int vAspect;
    double aspect;
    double focalLength;
    cVector background;
}