Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedProjection.java @ 2fce34f4

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
  private boolean invert;  // invert top with bottom? We don't do that for the projection to the screen,
34
                           // but we need that for the projection to FBOs. (that's because each time we
35
                           // render to FBO we invert the texture upside down because its vertex coords
36
                           // are purposefully set upside down in DistortedBackground; so each time we
37
                           // render through FBO we need to invert it once more to counter this effect)
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
   
41
  public DistortedProjection(boolean inv) 
42
   {
43
   invert = inv;  
44
   projectionMatrix = new float[16];   
45
   }
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
//
(10-10/17)