Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ 10b7e588

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.GridFlat;
27
import org.distorted.library.DistortedObject;
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 ProjectionRenderer implements GLSurfaceView.Renderer
41
{
42
   private GLSurfaceView mView;
43
   private DistortedObject mProjection;
44
   private GridFlat mGrid;
45

    
46
   private static float mF, mX, mY;
47
   private static int mWidth, mHeight;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
   ProjectionRenderer(GLSurfaceView view)
52
      { 
53
      mView = view;
54
      }
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

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

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

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

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

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

    
98
      Distorted.onSurfaceChanged(width, height);
99
      setupBitmap(width,height);
100
      }
101

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

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

    
120
    private void setupBitmap(int w, int h)
121
      {
122
      Paint paint = new Paint();
123
      mProjection = new DistortedObject(w,h,1);
124
      mGrid       = new GridFlat(50,50*h/w);
125
      Bitmap bmp = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
126
      Canvas bmpCanvas = new Canvas(bmp);
127

    
128
      paint.setColor(0xff008800);
129
      paint.setStyle(Style.FILL);
130
      bmpCanvas.drawRect(0, 0, w, h, paint);
131
      paint.setColor(0xffffffff);
132

    
133
      final int NUMLINES = 10;
134

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

    
148
      mProjection.distort(vector, new Static3D(  w/4,   h/4, 0), region);
149
      mProjection.distort(vector, new Static3D(3*w/4,   h/4, 0), region);
150
      mProjection.distort(vector, new Static3D(  w/4, 3*h/4, 0), region);
151
      mProjection.distort(vector, new Static3D(3*w/4, 3*h/4, 0), region);
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
    
156
}
(2-2/3)