Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedProjection.java @ 6a06a912

1
package org.distorted.library;
2

    
3
import android.opengl.Matrix;
4

    
5
///////////////////////////////////////////////////////////////////////////////////////////////////
6

    
7
public class DistortedProjection 
8
  {
9
  int width,height,depth,distance;
10
  float[] projectionMatrix;
11
  
12
  private boolean invert;  // invert top with bottom? We don't do that for the projection to the screen,
13
                           // but we need that for the projection to FBOs. (that's because each time we
14
                           // render to FBO we invert the texture upside down because its vertex coords
15
                           // are purposefully set upside down in DistortedBackground; so each time we
16
                           // render through FBO we need to invert it once more to counter this effect)
17
  
18
  
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20
   
21
  public DistortedProjection(boolean inv) 
22
   {
23
   invert = inv;  
24
   projectionMatrix = new float[16];   
25
   }
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
//
(7-7/28)