Project

General

Profile

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

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

1
package org.distorted.library;
2

    
3
import android.opengl.Matrix;
4

    
5
///////////////////////////////////////////////////////////////////////////////////////////////////
6
/**
7
 * Only used within the Distorted Library itself.
8
 */
9
class DistortedProjection
10
  {
11
  int width,height,depth,distance;
12
  float[] projectionMatrix;
13
  
14
  private boolean invert;  // invert top with bottom? We don't do that for the projection to the screen,
15
                           // but we need that for the projection to FBOs. (that's because each time we
16
                           // render to FBO we invert the texture upside down because its vertex coords
17
                           // are purposefully set upside down in DistortedBackground; so each time we
18
                           // render through FBO we need to invert it once more to counter this effect)
19

    
20
///////////////////////////////////////////////////////////////////////////////////////////////////
21
   
22
  public DistortedProjection(boolean inv) 
23
   {
24
   invert = inv;  
25
   projectionMatrix = new float[16];   
26
   }
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

    
30
  void onSurfaceChanged(int surfaceWidth, int surfaceHeight)
31
    {
32
    width = surfaceWidth;
33
    height= surfaceHeight;  
34
    
35
    float ratio  = (float) surfaceWidth / surfaceHeight;
36
    float left   =-ratio;          //
37
    float right  = ratio;          // Create a new perspective projection matrix.
38
    float bottom = -1.0f;          //
39
    float top    =  1.0f;          // any change to those values will have serious consequences!
40
    float near, far;
41
   
42
    if( Distorted.mFOV>0.0f )  // perspective projection
43
      {  
44
      near= (float)(top / Math.tan(Distorted.mFOV*Math.PI/360)); 
45
      distance = (int)(height*near/(top-bottom));
46
      far = 2*distance-near;
47
     
48
      if( invert ) Matrix.frustumM(projectionMatrix, 0, left, right, top, bottom, near, far);
49
      else         Matrix.frustumM(projectionMatrix, 0, left, right, bottom, top, near, far);        
50
      }
51
    else                      // parallel projection
52
      {
53
      near= (float)(top / Math.tan(Math.PI/360)); 
54
      distance = (int)(height*near/(top-bottom));
55
      far = 2*distance-near;
56
    
57
      if( invert ) Matrix.orthoM(projectionMatrix, 0, -surfaceWidth/2, surfaceWidth/2, surfaceHeight/2,-surfaceHeight/2, near, far);
58
      else         Matrix.orthoM(projectionMatrix, 0, -surfaceWidth/2, surfaceWidth/2,-surfaceHeight/2, surfaceHeight/2, near, far);
59
      }
60
   
61
    depth = (int)((far-near)/2);
62
    }
63
  }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
//
(7-7/28)