Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fov / FOVRenderer.java @ 2f9752c5

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.examples.fov;
21

    
22
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24

    
25
import org.distorted.library.Distorted;
26
import org.distorted.library.DistortedBitmap;
27
import org.distorted.library.type.Static2D;
28
import org.distorted.library.type.Static3D;
29
import org.distorted.library.type.Static4D;
30

    
31
import android.graphics.Bitmap;
32
import android.graphics.Canvas;
33
import android.graphics.Paint;
34
import android.graphics.Paint.Style;
35
import android.opengl.GLES20;
36
import android.opengl.GLSurfaceView;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
class FOVRenderer implements GLSurfaceView.Renderer 
41
{
42
   private static final int NUMLINES = 10;
43
   
44
   private GLSurfaceView mView;
45
   private DistortedBitmap fov;
46

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

    
49
   FOVRenderer(GLSurfaceView view)
50
      { 
51
      mView = view;
52
      }
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
   static void setFOV(int fov)
57
      {
58
      Distorted.setFov(fov);
59
      }
60
   
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
   
63
   public void onDrawFrame(GL10 glUnused) 
64
      {
65
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
66
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
67
     
68
      fov.draw(System.currentTimeMillis());
69
      }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
    
73
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
74
      { 
75
      Distorted.onSurfaceChanged(width, height);
76
      setupBitmap(width,height);
77
      }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
    
81
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
82
      {  
83
      try
84
        {
85
        Distorted.onSurfaceCreated(mView.getContext());
86
        }
87
      catch(Exception ex)
88
        {
89
        android.util.Log.e("FOV", ex.getMessage() );
90
        }
91
      }
92
    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
    private void setupBitmap(int w, int h)
96
      {
97
      Paint paint = new Paint();
98
      fov = new DistortedBitmap(w,h, 50);
99
      Bitmap fovBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
100
      Canvas fovCanvas = new Canvas(fovBitmap);    
101

    
102
      paint.setColor(0xff008800);
103
      paint.setStyle(Style.FILL);
104
      fovCanvas.drawRect(0, 0, w, h, paint);
105
      paint.setColor(0xffffffff);
106
      
107
      for(int i=0; i<=NUMLINES ; i++ )
108
        {
109
        fovCanvas.drawRect(w*i/NUMLINES - 1,                0,  w*i/NUMLINES + 1,  h               , paint);
110
        fovCanvas.drawRect(               0, h *i/NUMLINES -1,  w               ,  h*i/NUMLINES + 1, paint);
111
        }
112
        
113
      fov.setBitmap(fovBitmap);
114
        
115
      int min = w<h ? w:h;
116
        
117
      Static3D vector = new Static3D(0,0,min/5);
118
      Static4D region = new Static4D(0,0,min/5,min/5);
119

    
120
      fov.distort(vector, new Static2D(  w/4,   h/4), region);
121
      fov.distort(vector, new Static2D(3*w/4,   h/4), region);
122
      fov.distort(vector, new Static2D(  w/4, 3*h/4), region);
123
      fov.distort(vector, new Static2D(3*w/4, 3*h/4), region);
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
}
(2-2/3)