Project

General

Profile

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

examples / src / main / java / org / distorted / examples / projection / ProjectionRenderer.java @ 95bc9f69

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 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
26 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
27 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
28
import org.distorted.library.DistortedEffectQueues;
29 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
30
import org.distorted.library.type.Static4D;
31 427ab7bf Leszek Koltunski
32
import android.graphics.Bitmap;
33
import android.graphics.Canvas;
34
import android.graphics.Paint;
35
import android.graphics.Paint.Style;
36
import android.opengl.GLES20;
37
import android.opengl.GLSurfaceView;
38
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41 10d53839 Leszek Koltunski
class ProjectionRenderer implements GLSurfaceView.Renderer
42 427ab7bf Leszek Koltunski
{
43
   private GLSurfaceView mView;
44 f6d884d5 Leszek Koltunski
   private DistortedTexture mTexture;
45
   private DistortedEffectQueues mQueues;
46 10b7e588 Leszek Koltunski
   private GridFlat mGrid;
47 10d53839 Leszek Koltunski
48
   private static float mF, mX, mY;
49
   private static int mWidth, mHeight;
50 2f9752c5 Leszek Koltunski
51 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 10d53839 Leszek Koltunski
   ProjectionRenderer(GLSurfaceView view)
54 427ab7bf Leszek Koltunski
      { 
55
      mView = view;
56 f6d884d5 Leszek Koltunski
      mQueues = new DistortedEffectQueues();
57 427ab7bf Leszek Koltunski
      }
58
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 10d53839 Leszek Koltunski
   static int setFOV(int f)
62 427ab7bf Leszek Koltunski
      {
63 10d53839 Leszek Koltunski
      mF = f;
64
      Distorted.setProjection(mF,mX,mY);
65
      return (int)mF;
66 427ab7bf Leszek Koltunski
      }
67 10d53839 Leszek Koltunski
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70
   static int setX(int x)
71
      {
72 8cb9208c Leszek Koltunski
      mX = (x-50)*0.02f*mWidth;
73 10d53839 Leszek Koltunski
      Distorted.setProjection(mF,mX,mY);
74
      return (int)mX;
75
      }
76
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
79
   static int setY(int y)
80
      {
81 8cb9208c Leszek Koltunski
      mY = (y-50)*0.02f*mHeight;
82 10d53839 Leszek Koltunski
      Distorted.setProjection(mF,mX,mY);
83
      return (int)mY;
84
      }
85
86 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
87
   
88
   public void onDrawFrame(GL10 glUnused) 
89
      {
90
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
91 f6d884d5 Leszek Koltunski
      mQueues.draw(System.currentTimeMillis(), mTexture,mGrid);
92 427ab7bf Leszek Koltunski
      }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
    
96
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
97 10d53839 Leszek Koltunski
      {
98
      mWidth = width;
99
      mHeight= height;
100
101 95bc9f69 Leszek Koltunski
      Distorted.setProjection(mF,mX,mY);
102
      mQueues.abortAllEffects();
103
104 2f9752c5 Leszek Koltunski
      Paint paint = new Paint();
105 7451c98a Leszek Koltunski
      mTexture    = new DistortedTexture(width,height);
106 f6d884d5 Leszek Koltunski
      mGrid       = new GridFlat(50,50*height/width);
107
      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
      mTexture.setTexture(bmp);
124
125
      int min = width<height ? width:height;
126
127 7bf107f7 Leszek Koltunski
      Static3D vector = new Static3D(0,0,min/5);
128
      Static4D region = new Static4D(0,0,min/5,min/5);
129
130 f6d884d5 Leszek Koltunski
      mQueues.distort(vector, new Static3D(  width/4,   height/4, 0), region);
131
      mQueues.distort(vector, new Static3D(3*width/4,   height/4, 0), region);
132
      mQueues.distort(vector, new Static3D(  width/4, 3*height/4, 0), region);
133
      mQueues.distort(vector, new Static3D(3*width/4, 3*height/4, 0), region);
134
135
      Distorted.onSurfaceChanged(width, height);
136 427ab7bf Leszek Koltunski
      }
137
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140 f6d884d5 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
141
      {
142
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
143
144
      try
145
        {
146
        Distorted.onSurfaceCreated(mView.getContext());
147
        }
148
      catch(Exception ex)
149
        {
150
        android.util.Log.e("Projection", ex.getMessage() );
151
        }
152
      }
153 427ab7bf Leszek Koltunski
}