Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ e4330c89

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.EffectName;
26
import org.distorted.library.effect.VertexEffectDistort;
27
import org.distorted.library.main.Distorted;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedScreen;
30
import org.distorted.library.main.MeshFlat;
31
import org.distorted.library.main.DistortedTexture;
32
import org.distorted.library.type.Static3D;
33
import org.distorted.library.type.Static4D;
34

    
35
import android.graphics.Bitmap;
36
import android.graphics.Canvas;
37
import android.graphics.Paint;
38
import android.graphics.Paint.Style;
39
import android.opengl.GLSurfaceView;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
class ProjectionRenderer implements GLSurfaceView.Renderer
44
{
45
   private GLSurfaceView mView;
46
   private DistortedEffects mEffects;
47
   private DistortedScreen mScreen;
48
   private MeshFlat mMesh;
49
   private DistortedTexture mTexture;
50
   private float mF, mNear;
51

    
52
   private Static3D mVector, mPoint1, mPoint2, mPoint3, mPoint4;
53
   private Static4D mRegion;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
63
      mVector = new Static3D(0,0,0);
64
      mPoint1 = new Static3D(0,0,0);
65
      mPoint2 = new Static3D(0,0,0);
66
      mPoint3 = new Static3D(0,0,0);
67
      mPoint4 = new Static3D(0,0,0);
68
      mRegion = new Static4D(0,0,0,0);
69

    
70
      mEffects.apply( new VertexEffectDistort(mVector, mPoint1, mRegion) );
71
      mEffects.apply( new VertexEffectDistort(mVector, mPoint2, mRegion) );
72
      mEffects.apply( new VertexEffectDistort(mVector, mPoint3, mRegion) );
73
      mEffects.apply( new VertexEffectDistort(mVector, mPoint4, mRegion) );
74
      }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
   float setFOV(int f)
79
      {
80
      mF = f;
81
      mScreen.setProjection(mF,mNear);
82
      return mF;
83
      }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
   float setNear(int near)
88
      {
89
      mNear = near/100.0f;
90
      mScreen.setProjection(mF,mNear);
91
      return mNear;
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
   
96
   public void onDrawFrame(GL10 glUnused) 
97
      {
98
      mScreen.render( System.currentTimeMillis() );
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
    
103
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
104
      {
105
      mScreen.setProjection(mF,mNear);
106

    
107
      Paint paint = new Paint();
108
      Bitmap bmp  = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
109
      Canvas bmpCanvas = new Canvas(bmp);
110

    
111
      paint.setColor(0xff008800);
112
      paint.setStyle(Style.FILL);
113
      bmpCanvas.drawRect(0, 0, width, height, paint);
114
      paint.setColor(0xffffffff);
115

    
116
      final int NUMLINES = 10;
117

    
118
      for(int i=0; i<=NUMLINES ; i++ )
119
        {
120
        bmpCanvas.drawRect(width*i/NUMLINES - 1,                    0,  width*i/NUMLINES + 1,  height               , paint);
121
        bmpCanvas.drawRect(                   0, height*i/NUMLINES -1,  width               ,  height*i/NUMLINES + 1, paint);
122
        }
123

    
124
      int min = width<height ? width:height;
125

    
126
      mVector.set(0,0,min/5);
127
      mRegion.set(0,0,min/5,min/5);
128

    
129
      mPoint1.set(  width/4,   height/4, 0);
130
      mPoint2.set(3*width/4,   height/4, 0);
131
      mPoint3.set(  width/4, 3*height/4, 0);
132
      mPoint4.set(3*width/4, 3*height/4, 0);
133

    
134
      // Avoid memory leaks: delete old texture if it exists (it might if we
135
      // got here after a brief amount of time spent in the background)
136
      if( mTexture!=null ) mTexture.markForDeletion();
137

    
138
      mTexture= new DistortedTexture(width,height);
139
      mTexture.setTexture(bmp);
140

    
141
      // likewise with the Mesh
142
      if( mMesh!=null ) mMesh.markForDeletion();
143

    
144
      mMesh = new MeshFlat(50,50*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
      DistortedEffects.enableEffect(EffectName.DISTORT);
156

    
157
      try
158
        {
159
        Distorted.onCreate(mView.getContext());
160
        }
161
      catch(Exception ex)
162
        {
163
        android.util.Log.e("Projection", ex.getMessage() );
164
        }
165
      }
166
}
(2-2/3)