Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ 107e4b72

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

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

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

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

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

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

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

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

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

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

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

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

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

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

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

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

    
115
      final int NUMLINES = 10;
116

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

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

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

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

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

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

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

    
143
      mMesh = new MeshFlat(100,100*height/width);
144

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
    
152
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
153
      {
154
      VertexEffectDistort.enable();
155

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