Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 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 1f218177 Leszek Koltunski
import org.distorted.library.effect.VertexEffectDistort;
26 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
27
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedScreen;
29 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshFlat;
30 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
31 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33 427ab7bf Leszek Koltunski
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 10d53839 Leszek Koltunski
class ProjectionRenderer implements GLSurfaceView.Renderer
43 427ab7bf Leszek Koltunski
{
44
   private GLSurfaceView mView;
45 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
46 d218d64e leszek
   private DistortedScreen mScreen;
47 5f2a53b6 leszek
   private MeshFlat mMesh;
48 b0ebdf5e Leszek Koltunski
   private DistortedTexture mTexture;
49 af662543 leszek
   private float mF, mNear;
50 2f9752c5 Leszek Koltunski
51 1f218177 Leszek Koltunski
   private Static3D mVector, mPoint1, mPoint2, mPoint3, mPoint4;
52
   private Static4D mRegion;
53
54 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 10d53839 Leszek Koltunski
   ProjectionRenderer(GLSurfaceView view)
57 427ab7bf Leszek Koltunski
      { 
58 392e16fd Leszek Koltunski
      mView   = view;
59 d04a4886 Leszek Koltunski
      mEffects= new DistortedEffects();
60 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
61 1f218177 Leszek Koltunski
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 427ab7bf Leszek Koltunski
      }
74
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
77 af662543 leszek
   float setFOV(int f)
78 427ab7bf Leszek Koltunski
      {
79 10d53839 Leszek Koltunski
      mF = f;
80 af662543 leszek
      mScreen.setProjection(mF,mNear);
81
      return mF;
82 427ab7bf Leszek Koltunski
      }
83 10d53839 Leszek Koltunski
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
86 af662543 leszek
   float setNear(int near)
87 10d53839 Leszek Koltunski
      {
88 af662543 leszek
      mNear = near/100.0f;
89
      mScreen.setProjection(mF,mNear);
90
      return mNear;
91 10d53839 Leszek Koltunski
      }
92
93 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
94
   
95
   public void onDrawFrame(GL10 glUnused) 
96
      {
97 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
98 427ab7bf Leszek Koltunski
      }
99
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
    
102
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
103 10d53839 Leszek Koltunski
      {
104 af662543 leszek
      mScreen.setProjection(mF,mNear);
105 95bc9f69 Leszek Koltunski
106 2f9752c5 Leszek Koltunski
      Paint paint = new Paint();
107 f6d884d5 Leszek Koltunski
      Bitmap bmp  = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
108 10d53839 Leszek Koltunski
      Canvas bmpCanvas = new Canvas(bmp);
109 427ab7bf Leszek Koltunski
110 2f9752c5 Leszek Koltunski
      paint.setColor(0xff008800);
111
      paint.setStyle(Style.FILL);
112 f6d884d5 Leszek Koltunski
      bmpCanvas.drawRect(0, 0, width, height, paint);
113 2f9752c5 Leszek Koltunski
      paint.setColor(0xffffffff);
114 10d53839 Leszek Koltunski
115
      final int NUMLINES = 10;
116
117 427ab7bf Leszek Koltunski
      for(int i=0; i<=NUMLINES ; i++ )
118
        {
119 f6d884d5 Leszek Koltunski
        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 427ab7bf Leszek Koltunski
        }
122 f6d884d5 Leszek Koltunski
123
      int min = width<height ? width:height;
124
125 1f218177 Leszek Koltunski
      mVector.set(0,0,min/5);
126
      mRegion.set(0,0,min/5,min/5);
127 7bf107f7 Leszek Koltunski
128 1f218177 Leszek Koltunski
      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 f6d884d5 Leszek Koltunski
133 b0ebdf5e Leszek Koltunski
      // 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 5f2a53b6 leszek
      // likewise with the Mesh
141
      if( mMesh!=null ) mMesh.markForDeletion();
142
143 48017ca2 Leszek Koltunski
      mMesh = new MeshFlat(100,100*height/width);
144 5f2a53b6 leszek
145 fe59d375 Leszek Koltunski
      mScreen.detachAll();
146 5f2a53b6 leszek
      mScreen.attach(mTexture,mEffects,mMesh);
147 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
148 427ab7bf Leszek Koltunski
      }
149
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
    
152 f6d884d5 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
153
      {
154 885b9cca leszek
      VertexEffectDistort.enable();
155 6637d0f2 Leszek Koltunski
156 f6d884d5 Leszek Koltunski
      try
157
        {
158 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
159 f6d884d5 Leszek Koltunski
        }
160
      catch(Exception ex)
161
        {
162
        android.util.Log.e("Projection", ex.getMessage() );
163
        }
164
      }
165 427ab7bf Leszek Koltunski
}