Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ 01782e85

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 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
26
import org.distorted.library.main.DistortedEffects;
27
import org.distorted.library.main.DistortedScreen;
28 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
29 01782e85 Leszek Koltunski
import org.distorted.library.main.MeshFlat;
30
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 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 10d53839 Leszek Koltunski
   ProjectionRenderer(GLSurfaceView view)
54 427ab7bf Leszek Koltunski
      { 
55 392e16fd Leszek Koltunski
      mView   = view;
56 d04a4886 Leszek Koltunski
      mEffects= new DistortedEffects();
57 1f9a52f1 leszek
      mScreen = new DistortedScreen(mView);
58 427ab7bf Leszek Koltunski
      }
59
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62 af662543 leszek
   float setFOV(int f)
63 427ab7bf Leszek Koltunski
      {
64 10d53839 Leszek Koltunski
      mF = f;
65 af662543 leszek
      mScreen.setProjection(mF,mNear);
66
      return mF;
67 427ab7bf Leszek Koltunski
      }
68 10d53839 Leszek Koltunski
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71 af662543 leszek
   float setNear(int near)
72 10d53839 Leszek Koltunski
      {
73 af662543 leszek
      mNear = near/100.0f;
74
      mScreen.setProjection(mF,mNear);
75
      return mNear;
76 10d53839 Leszek Koltunski
      }
77
78 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
79
   
80
   public void onDrawFrame(GL10 glUnused) 
81
      {
82 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
83 427ab7bf Leszek Koltunski
      }
84
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
    
87
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
88 10d53839 Leszek Koltunski
      {
89 af662543 leszek
      mScreen.setProjection(mF,mNear);
90 392e16fd Leszek Koltunski
      mEffects.abortAllEffects();
91 95bc9f69 Leszek Koltunski
92 2f9752c5 Leszek Koltunski
      Paint paint = new Paint();
93 f6d884d5 Leszek Koltunski
      Bitmap bmp  = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
94 10d53839 Leszek Koltunski
      Canvas bmpCanvas = new Canvas(bmp);
95 427ab7bf Leszek Koltunski
96 2f9752c5 Leszek Koltunski
      paint.setColor(0xff008800);
97
      paint.setStyle(Style.FILL);
98 f6d884d5 Leszek Koltunski
      bmpCanvas.drawRect(0, 0, width, height, paint);
99 2f9752c5 Leszek Koltunski
      paint.setColor(0xffffffff);
100 10d53839 Leszek Koltunski
101
      final int NUMLINES = 10;
102
103 427ab7bf Leszek Koltunski
      for(int i=0; i<=NUMLINES ; i++ )
104
        {
105 f6d884d5 Leszek Koltunski
        bmpCanvas.drawRect(width*i/NUMLINES - 1,                    0,  width*i/NUMLINES + 1,  height               , paint);
106
        bmpCanvas.drawRect(                   0, height*i/NUMLINES -1,  width               ,  height*i/NUMLINES + 1, paint);
107 427ab7bf Leszek Koltunski
        }
108 f6d884d5 Leszek Koltunski
109
      int min = width<height ? width:height;
110
111 7bf107f7 Leszek Koltunski
      Static3D vector = new Static3D(0,0,min/5);
112
      Static4D region = new Static4D(0,0,min/5,min/5);
113
114 392e16fd Leszek Koltunski
      mEffects.distort(vector, new Static3D(  width/4,   height/4, 0), region);
115
      mEffects.distort(vector, new Static3D(3*width/4,   height/4, 0), region);
116
      mEffects.distort(vector, new Static3D(  width/4, 3*height/4, 0), region);
117
      mEffects.distort(vector, new Static3D(3*width/4, 3*height/4, 0), region);
118 f6d884d5 Leszek Koltunski
119 b0ebdf5e Leszek Koltunski
      // Avoid memory leaks: delete old texture if it exists (it might if we
120
      // got here after a brief amount of time spent in the background)
121
      if( mTexture!=null ) mTexture.markForDeletion();
122
123
      mTexture= new DistortedTexture(width,height);
124
      mTexture.setTexture(bmp);
125
126 5f2a53b6 leszek
      // likewise with the Mesh
127
      if( mMesh!=null ) mMesh.markForDeletion();
128
129
      mMesh = new MeshFlat(50,50*height/width);
130
131 fe59d375 Leszek Koltunski
      mScreen.detachAll();
132 5f2a53b6 leszek
      mScreen.attach(mTexture,mEffects,mMesh);
133 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
134 427ab7bf Leszek Koltunski
      }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
    
138 f6d884d5 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
139
      {
140 6637d0f2 Leszek Koltunski
      DistortedEffects.enableEffect(EffectNames.DISTORT);
141
142 f6d884d5 Leszek Koltunski
      try
143
        {
144 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
145 f6d884d5 Leszek Koltunski
        }
146
      catch(Exception ex)
147
        {
148
        android.util.Log.e("Projection", ex.getMessage() );
149
        }
150
      }
151 427ab7bf Leszek Koltunski
}