Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ 30f337e5

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.effect.MatrixEffectScale;
26
import org.distorted.library.effect.VertexEffectDeform;
27
import org.distorted.library.main.DistortedLibrary;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedScreen;
30
import org.distorted.library.mesh.MeshSquare;
31
import org.distorted.library.main.DistortedTexture;
32
import org.distorted.library.type.Static1D;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35

    
36
import android.app.ActivityManager;
37
import android.content.Context;
38
import android.content.pm.ConfigurationInfo;
39
import android.content.res.Resources;
40
import android.graphics.Bitmap;
41
import android.graphics.Canvas;
42
import android.graphics.Paint;
43
import android.graphics.Paint.Style;
44
import android.opengl.GLSurfaceView;
45

    
46
import java.io.InputStream;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class ProjectionRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
51
{
52
   private final Resources mResources;
53
   private final DistortedEffects mEffects;
54
   private final DistortedScreen mScreen;
55
   private final DistortedTexture mTexture;
56
   private final Static3D mScale;
57

    
58
   private MeshSquare mMesh;
59
   private float mF, mNear;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
   ProjectionRenderer(GLSurfaceView view)
64
      {
65
      mResources = view.getResources();
66

    
67
      mEffects= new DistortedEffects();
68
      mScreen = new DistortedScreen();
69

    
70
      Static3D point1 = new Static3D(  0.25f,   0.25f, 0);
71
      Static3D point2 = new Static3D( -0.25f,   0.25f, 0);
72
      Static3D point3 = new Static3D(  0.25f,  -0.25f, 0);
73
      Static3D point4 = new Static3D( -0.25f,  -0.25f, 0);
74
      Static4D region = new Static4D( 0, 0, 0, 0.25f);
75
      Static3D vector = new Static3D( 0, 0, 0.25f);
76

    
77
      Static1D deformRadius = new Static1D(1);
78

    
79
      mScale = new Static3D(1,1,1);
80

    
81
      mEffects.apply( new MatrixEffectScale(mScale) );
82

    
83
      mEffects.apply( new VertexEffectDeform(vector, deformRadius, point1, region) );
84
      mEffects.apply( new VertexEffectDeform(vector, deformRadius, point2, region) );
85
      mEffects.apply( new VertexEffectDeform(vector, deformRadius, point3, region) );
86
      mEffects.apply( new VertexEffectDeform(vector, deformRadius, point4, region) );
87

    
88
      mTexture= new DistortedTexture();
89
      }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
   float setFOV(int f)
94
      {
95
      mF = f;
96
      mScreen.setProjection(mF,mNear);
97
      return mF;
98
      }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
   float setNear(int near)
103
      {
104
      mNear = near/100.0f;
105
      mScreen.setProjection(mF,mNear);
106
      return mNear;
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
   
111
   public void onDrawFrame(GL10 glUnused) 
112
      {
113
      mScreen.render( System.currentTimeMillis() );
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
    
118
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
119
      {
120
      mScreen.setProjection(mF,mNear);
121

    
122
      Paint paint = new Paint();
123
      Bitmap bmp  = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
124
      Canvas bmpCanvas = new Canvas(bmp);
125

    
126
      paint.setColor(0xff008800);
127
      paint.setStyle(Style.FILL);
128
      bmpCanvas.drawRect(0, 0, width, height, paint);
129
      paint.setColor(0xffffffff);
130

    
131
      final int NUMLINES = 10;
132

    
133
      for(int i=0; i<=NUMLINES ; i++ )
134
        {
135
        bmpCanvas.drawRect(width*i/NUMLINES-1,                   0, width*i/NUMLINES+1, height             , paint);
136
        bmpCanvas.drawRect(                 0, height*i/NUMLINES-1, width             , height*i/NUMLINES+1, paint);
137
        }
138

    
139
      mScale.set(width,height,width);
140

    
141
      mTexture.setTexture(bmp);
142

    
143
      if( mMesh!=null )  mMesh.markForDeletion();
144
      mMesh = new MeshSquare(100,100*height/width);
145

    
146
      mScreen.detachAll();
147
      mScreen.attach(mTexture,mEffects,mMesh);
148
      mScreen.resize(width, height);
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
    
153
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
154
      {
155
      VertexEffectDeform.enable();
156
      DistortedLibrary.onSurfaceCreated(this);
157
      }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
    public void distortedException(Exception ex)
162
      {
163
      android.util.Log.e("Projection", ex.getMessage() );
164
      }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
    public InputStream localFile(int fileID)
169
      {
170
      return mResources.openRawResource(fileID);
171
      }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
    public void logMessage(String message)
176
      {
177
      android.util.Log.e("Projection", message );
178
      }
179
}
(2-2/3)