Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ 061449ed

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 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
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
import android.graphics.Bitmap;
37
import android.graphics.Canvas;
38
import android.graphics.Paint;
39
import android.graphics.Paint.Style;
40
import android.opengl.GLSurfaceView;
41
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44 061449ed Leszek Koltunski
class ProjectionRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
45 427ab7bf Leszek Koltunski
{
46
   private GLSurfaceView mView;
47 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
48 d218d64e leszek
   private DistortedScreen mScreen;
49 f3ca895f Leszek Koltunski
   private MeshRectangles mMesh;
50 b0ebdf5e Leszek Koltunski
   private DistortedTexture mTexture;
51 af662543 leszek
   private float mF, mNear;
52 18231014 Leszek Koltunski
   private Static3D mScale;
53 1f218177 Leszek Koltunski
54 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 10d53839 Leszek Koltunski
   ProjectionRenderer(GLSurfaceView view)
57 427ab7bf Leszek Koltunski
      { 
58 392e16fd Leszek Koltunski
      mView   = view;
59 698ad0a8 Leszek Koltunski
      mEffects= new DistortedEffects();
60 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
61 1f218177 Leszek Koltunski
62 18231014 Leszek Koltunski
      Static3D point1 = new Static3D(  0.25f,   0.25f, 0);
63
      Static3D point2 = new Static3D( -0.25f,   0.25f, 0);
64
      Static3D point3 = new Static3D(  0.25f,  -0.25f, 0);
65
      Static3D point4 = new Static3D( -0.25f,  -0.25f, 0);
66
      Static4D region = new Static4D( 0, 0, 0, 0.25f);
67
      Static3D vector = new Static3D( 0, 0, 0.25f);
68
69 513b2e9c Leszek Koltunski
      Static1D deformRadius = new Static1D(1);
70
71 18231014 Leszek Koltunski
      mScale = new Static3D(1,1,1);
72
73
      mEffects.apply( new MatrixEffectScale(mScale) );
74
75 513b2e9c Leszek Koltunski
      mEffects.apply( new VertexEffectDeform(vector, deformRadius, point1, region) );
76
      mEffects.apply( new VertexEffectDeform(vector, deformRadius, point2, region) );
77
      mEffects.apply( new VertexEffectDeform(vector, deformRadius, point3, region) );
78
      mEffects.apply( new VertexEffectDeform(vector, deformRadius, point4, region) );
79 18231014 Leszek Koltunski
80
      mTexture= new DistortedTexture();
81 427ab7bf Leszek Koltunski
      }
82
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
85 af662543 leszek
   float setFOV(int f)
86 427ab7bf Leszek Koltunski
      {
87 10d53839 Leszek Koltunski
      mF = f;
88 af662543 leszek
      mScreen.setProjection(mF,mNear);
89
      return mF;
90 427ab7bf Leszek Koltunski
      }
91 10d53839 Leszek Koltunski
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94 af662543 leszek
   float setNear(int near)
95 10d53839 Leszek Koltunski
      {
96 af662543 leszek
      mNear = near/100.0f;
97
      mScreen.setProjection(mF,mNear);
98
      return mNear;
99 10d53839 Leszek Koltunski
      }
100
101 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
102
   
103
   public void onDrawFrame(GL10 glUnused) 
104
      {
105 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
106 427ab7bf Leszek Koltunski
      }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
111 10d53839 Leszek Koltunski
      {
112 af662543 leszek
      mScreen.setProjection(mF,mNear);
113 95bc9f69 Leszek Koltunski
114 2f9752c5 Leszek Koltunski
      Paint paint = new Paint();
115 f6d884d5 Leszek Koltunski
      Bitmap bmp  = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
116 10d53839 Leszek Koltunski
      Canvas bmpCanvas = new Canvas(bmp);
117 427ab7bf Leszek Koltunski
118 2f9752c5 Leszek Koltunski
      paint.setColor(0xff008800);
119
      paint.setStyle(Style.FILL);
120 f6d884d5 Leszek Koltunski
      bmpCanvas.drawRect(0, 0, width, height, paint);
121 2f9752c5 Leszek Koltunski
      paint.setColor(0xffffffff);
122 10d53839 Leszek Koltunski
123
      final int NUMLINES = 10;
124
125 427ab7bf Leszek Koltunski
      for(int i=0; i<=NUMLINES ; i++ )
126
        {
127 a4d59c0b Leszek Koltunski
        bmpCanvas.drawRect(width*i/NUMLINES-1,                   0, width*i/NUMLINES+1, height             , paint);
128
        bmpCanvas.drawRect(                 0, height*i/NUMLINES-1, width             , height*i/NUMLINES+1, paint);
129 427ab7bf Leszek Koltunski
        }
130 f6d884d5 Leszek Koltunski
131 c7a31368 Leszek Koltunski
      mScale.set(width,height,width);
132 f6d884d5 Leszek Koltunski
133 b0ebdf5e Leszek Koltunski
      mTexture.setTexture(bmp);
134
135 698ad0a8 Leszek Koltunski
      if( mMesh!=null )  mMesh.markForDeletion();
136 f3ca895f Leszek Koltunski
      mMesh = new MeshRectangles(100,100*height/width);
137 5f2a53b6 leszek
138 fe59d375 Leszek Koltunski
      mScreen.detachAll();
139 5f2a53b6 leszek
      mScreen.attach(mTexture,mEffects,mMesh);
140 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
141 427ab7bf Leszek Koltunski
      }
142
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
    
145 f6d884d5 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
146
      {
147 01cef452 Leszek Koltunski
      VertexEffectDeform.enable();
148 6637d0f2 Leszek Koltunski
149 061449ed Leszek Koltunski
      DistortedLibrary.onCreate(mView.getContext(), this);
150
      }
151
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
154
    public void distortedException(Exception ex)
155
      {
156
      android.util.Log.e("Projection", ex.getMessage() );
157 f6d884d5 Leszek Koltunski
      }
158 427ab7bf Leszek Koltunski
}