Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ 1f218177

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(mView);
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
      mEffects.abortAllEffects();
107

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

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

    
117
      final int NUMLINES = 10;
118

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

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

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

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

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

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

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

    
145
      mMesh = new MeshFlat(50,50*height/width);
146

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

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
    
154
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
155
      {
156
      DistortedEffects.enableEffect(EffectName.DISTORT);
157

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