Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ 625c67de

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 GLSurfaceView mView;
53
   private final Resources mResources;
54
   private final DistortedEffects mEffects;
55
   private final DistortedScreen mScreen;
56
   private final DistortedTexture mTexture;
57
   private final Static3D mScale;
58

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
   ProjectionRenderer(GLSurfaceView view)
65
      { 
66
      mView = view;
67
      mResources = view.getResources();
68

    
69
      mEffects= new DistortedEffects();
70
      mScreen = new DistortedScreen();
71

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

    
79
      Static1D deformRadius = new Static1D(1);
80

    
81
      mScale = new Static3D(1,1,1);
82

    
83
      mEffects.apply( new MatrixEffectScale(mScale) );
84

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

    
90
      mTexture= new DistortedTexture();
91
      }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

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

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

    
124
      Paint paint = new Paint();
125
      Bitmap bmp  = Bitmap.createBitmap(width,height, 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, width, height, paint);
131
      paint.setColor(0xffffffff);
132

    
133
      final int NUMLINES = 10;
134

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

    
141
      mScale.set(width,height,width);
142

    
143
      mTexture.setTexture(bmp);
144

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

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

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

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

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