Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ 8cb9208c

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.projection;
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.Static3D;
28
import org.distorted.library.type.Static4D;
29

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

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
class ProjectionRenderer implements GLSurfaceView.Renderer
40
{
41
   private GLSurfaceView mView;
42
   private DistortedBitmap mProjection;
43

    
44
   private static float mF, mX, mY;
45
   private static int mWidth, mHeight;
46

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

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

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

    
56
   static int setFOV(int f)
57
      {
58
      mF = f;
59
      Distorted.setProjection(mF,mX,mY);
60
      return (int)mF;
61
      }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
   static int setX(int x)
66
      {
67
      mX = (x-50)*0.02f*mWidth;
68
      Distorted.setProjection(mF,mX,mY);
69
      return (int)mX;
70
      }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
   static int setY(int y)
75
      {
76
      mY = (y-50)*0.02f*mHeight;
77
      Distorted.setProjection(mF,mX,mY);
78
      return (int)mY;
79
      }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
   
83
   public void onDrawFrame(GL10 glUnused) 
84
      {
85
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
86
      mProjection.draw(System.currentTimeMillis());
87
      }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
    
91
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
92
      {
93
      mWidth = width;
94
      mHeight= height;
95

    
96
      Distorted.onSurfaceChanged(width, height);
97
      setupBitmap(width,height);
98
      }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
    
102
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
103
      {
104
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
105

    
106
      try
107
        {
108
        Distorted.onSurfaceCreated(mView.getContext());
109
        }
110
      catch(Exception ex)
111
        {
112
        android.util.Log.e("Projection", ex.getMessage() );
113
        }
114
      }
115
    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
    private void setupBitmap(int w, int h)
119
      {
120
      Paint paint = new Paint();
121
      mProjection = new DistortedBitmap(w,h, 50);
122
      Bitmap bmp = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
123
      Canvas bmpCanvas = new Canvas(bmp);
124

    
125
      paint.setColor(0xff008800);
126
      paint.setStyle(Style.FILL);
127
      bmpCanvas.drawRect(0, 0, w, h, paint);
128
      paint.setColor(0xffffffff);
129

    
130
      final int NUMLINES = 10;
131

    
132
      for(int i=0; i<=NUMLINES ; i++ )
133
        {
134
        bmpCanvas.drawRect(w*i/NUMLINES - 1,                0,  w*i/NUMLINES + 1,  h               , paint);
135
        bmpCanvas.drawRect(               0, h *i/NUMLINES -1,  w               ,  h*i/NUMLINES + 1, paint);
136
        }
137
        
138
      mProjection.setBitmap(bmp);
139
        
140
      int min = w<h ? w:h;
141
        
142
      Static3D vector = new Static3D(0,0,min/5);
143
      Static4D region = new Static4D(0,0,min/5,min/5);
144

    
145
      mProjection.distort(vector, new Static3D(  w/4,   h/4, 0), region);
146
      mProjection.distort(vector, new Static3D(3*w/4,   h/4, 0), region);
147
      mProjection.distort(vector, new Static3D(  w/4, 3*h/4, 0), region);
148
      mProjection.distort(vector, new Static3D(3*w/4, 3*h/4, 0), region);
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
    
153
}
(2-2/3)