Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ 77e66c58

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19 427ab7bf Leszek Koltunski
20 10d53839 Leszek Koltunski
package org.distorted.examples.projection;
21 427ab7bf Leszek Koltunski
22
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24
25 18231014 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
26 01cef452 Leszek Koltunski
import org.distorted.library.effect.VertexEffectDeform;
27 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
28 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedScreen;
30 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
31 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
32 513b2e9c Leszek Koltunski
import org.distorted.library.type.Static1D;
33 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35 427ab7bf Leszek Koltunski
36 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
37
import android.content.Context;
38
import android.content.pm.ConfigurationInfo;
39
import android.content.res.Resources;
40 427ab7bf Leszek Koltunski
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 dc10a48d Leszek Koltunski
import java.io.InputStream;
47
48 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 dc10a48d Leszek Koltunski
class ProjectionRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
51 427ab7bf Leszek Koltunski
{
52 dc10a48d Leszek Koltunski
   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 f2085b96 Leszek Koltunski
   private MeshSquare mMesh;
60 af662543 leszek
   private float mF, mNear;
61 1f218177 Leszek Koltunski
62 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64 10d53839 Leszek Koltunski
   ProjectionRenderer(GLSurfaceView view)
65 427ab7bf Leszek Koltunski
      { 
66 dc10a48d Leszek Koltunski
      mView = view;
67
      mResources = view.getResources();
68
69 698ad0a8 Leszek Koltunski
      mEffects= new DistortedEffects();
70 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
71 1f218177 Leszek Koltunski
72 18231014 Leszek Koltunski
      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 513b2e9c Leszek Koltunski
      Static1D deformRadius = new Static1D(1);
80
81 18231014 Leszek Koltunski
      mScale = new Static3D(1,1,1);
82
83
      mEffects.apply( new MatrixEffectScale(mScale) );
84
85 513b2e9c Leszek Koltunski
      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 18231014 Leszek Koltunski
90
      mTexture= new DistortedTexture();
91 427ab7bf Leszek Koltunski
      }
92
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
95 af662543 leszek
   float setFOV(int f)
96 427ab7bf Leszek Koltunski
      {
97 10d53839 Leszek Koltunski
      mF = f;
98 af662543 leszek
      mScreen.setProjection(mF,mNear);
99
      return mF;
100 427ab7bf Leszek Koltunski
      }
101 10d53839 Leszek Koltunski
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104 af662543 leszek
   float setNear(int near)
105 10d53839 Leszek Koltunski
      {
106 af662543 leszek
      mNear = near/100.0f;
107
      mScreen.setProjection(mF,mNear);
108
      return mNear;
109 10d53839 Leszek Koltunski
      }
110
111 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
112
   
113
   public void onDrawFrame(GL10 glUnused) 
114
      {
115 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
116 427ab7bf Leszek Koltunski
      }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
121 10d53839 Leszek Koltunski
      {
122 af662543 leszek
      mScreen.setProjection(mF,mNear);
123 95bc9f69 Leszek Koltunski
124 2f9752c5 Leszek Koltunski
      Paint paint = new Paint();
125 f6d884d5 Leszek Koltunski
      Bitmap bmp  = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
126 10d53839 Leszek Koltunski
      Canvas bmpCanvas = new Canvas(bmp);
127 427ab7bf Leszek Koltunski
128 2f9752c5 Leszek Koltunski
      paint.setColor(0xff008800);
129
      paint.setStyle(Style.FILL);
130 f6d884d5 Leszek Koltunski
      bmpCanvas.drawRect(0, 0, width, height, paint);
131 2f9752c5 Leszek Koltunski
      paint.setColor(0xffffffff);
132 10d53839 Leszek Koltunski
133
      final int NUMLINES = 10;
134
135 427ab7bf Leszek Koltunski
      for(int i=0; i<=NUMLINES ; i++ )
136
        {
137 a4d59c0b Leszek Koltunski
        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 427ab7bf Leszek Koltunski
        }
140 f6d884d5 Leszek Koltunski
141 c7a31368 Leszek Koltunski
      mScale.set(width,height,width);
142 f6d884d5 Leszek Koltunski
143 b0ebdf5e Leszek Koltunski
      mTexture.setTexture(bmp);
144
145 698ad0a8 Leszek Koltunski
      if( mMesh!=null )  mMesh.markForDeletion();
146 f2085b96 Leszek Koltunski
      mMesh = new MeshSquare(100,100*height/width);
147 5f2a53b6 leszek
148 fe59d375 Leszek Koltunski
      mScreen.detachAll();
149 5f2a53b6 leszek
      mScreen.attach(mTexture,mEffects,mMesh);
150 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
151 427ab7bf Leszek Koltunski
      }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
    
155 f6d884d5 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
156
      {
157 01cef452 Leszek Koltunski
      VertexEffectDeform.enable();
158 dc10a48d Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this);
159 061449ed Leszek Koltunski
      }
160
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
163
    public void distortedException(Exception ex)
164
      {
165
      android.util.Log.e("Projection", ex.getMessage() );
166 f6d884d5 Leszek Koltunski
      }
167 dc10a48d Leszek Koltunski
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 427ab7bf Leszek Koltunski
}