Project

General

Profile

Download (3.45 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / DistortedProjection.java @ f6fb3c6d

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library;
21

    
22
import android.opengl.Matrix;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/**
26
 * Only used within the Distorted Library itself.
27
 */
28
class DistortedProjection
29
  {
30
  int width,height,depth,distance;
31
  float[] projectionMatrix;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
   
35
  DistortedProjection()
36
   {
37
   projectionMatrix = new float[16];   
38
   }
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
  void onSurfaceChanged(int surfaceWidth, int surfaceHeight)
43
    {
44
    width = surfaceWidth;
45
    height= surfaceHeight;  
46
    
47
    float ratio  = (float) surfaceWidth / surfaceHeight;
48
    float left   =-ratio;          //
49
    float right  = ratio;          // Create a new perspective projection matrix.
50
    float bottom = -1.0f;          //
51
    float top    =  1.0f;          // any change to those values will have serious consequences!
52
    float near, far;
53
   
54
    if( Distorted.mFOV>0.0f )  // perspective projection
55
      {  
56
      near= (float)(top / Math.tan(Distorted.mFOV*Math.PI/360)); 
57
      distance = (int)(height*near/(top-bottom));
58
      far = 2*distance-near;
59
     
60
      Matrix.frustumM(projectionMatrix, 0, left, right, bottom, top, near, far);
61
      }
62
    else                      // parallel projection
63
      {
64
      near= (float)(top / Math.tan(Math.PI/360)); 
65
      distance = (int)(height*near/(top-bottom));
66
      far = 2*distance-near;
67
    
68
      Matrix.orthoM(projectionMatrix, 0, -surfaceWidth/2, surfaceWidth/2,-surfaceHeight/2, surfaceHeight/2, near, far);
69
      }
70
   
71
    depth = (int)((far-near)/2);
72
    }
73
  }
(11-11/19)