Project

General

Profile

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

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

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