Project

General

Profile

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

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

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.MeshRectangles;
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.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
class ProjectionRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
45
{
46
   private GLSurfaceView mView;
47
   private DistortedEffects mEffects;
48
   private DistortedScreen mScreen;
49
   private MeshRectangles mMesh;
50
   private DistortedTexture mTexture;
51
   private float mF, mNear;
52
   private Static3D mScale;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
   ProjectionRenderer(GLSurfaceView view)
57
      { 
58
      mView   = view;
59
      mEffects= new DistortedEffects();
60
      mScreen = new DistortedScreen();
61

    
62
      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
      Static1D deformRadius = new Static1D(1);
70

    
71
      mScale = new Static3D(1,1,1);
72

    
73
      mEffects.apply( new MatrixEffectScale(mScale) );
74

    
75
      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

    
80
      mTexture= new DistortedTexture();
81
      }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
   float setFOV(int f)
86
      {
87
      mF = f;
88
      mScreen.setProjection(mF,mNear);
89
      return mF;
90
      }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
   float setNear(int near)
95
      {
96
      mNear = near/100.0f;
97
      mScreen.setProjection(mF,mNear);
98
      return mNear;
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
   
103
   public void onDrawFrame(GL10 glUnused) 
104
      {
105
      mScreen.render( System.currentTimeMillis() );
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
111
      {
112
      mScreen.setProjection(mF,mNear);
113

    
114
      Paint paint = new Paint();
115
      Bitmap bmp  = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
116
      Canvas bmpCanvas = new Canvas(bmp);
117

    
118
      paint.setColor(0xff008800);
119
      paint.setStyle(Style.FILL);
120
      bmpCanvas.drawRect(0, 0, width, height, paint);
121
      paint.setColor(0xffffffff);
122

    
123
      final int NUMLINES = 10;
124

    
125
      for(int i=0; i<=NUMLINES ; i++ )
126
        {
127
        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
        }
130

    
131
      mScale.set(width,height,width);
132

    
133
      mTexture.setTexture(bmp);
134

    
135
      if( mMesh!=null )  mMesh.markForDeletion();
136
      mMesh = new MeshRectangles(100,100*height/width);
137

    
138
      mScreen.detachAll();
139
      mScreen.attach(mTexture,mEffects,mMesh);
140
      mScreen.resize(width, height);
141
      }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
    
145
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
146
      {
147
      VertexEffectDeform.enable();
148

    
149
      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
      }
158
}
(2-2/3)