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 |
d04a4886
|
Leszek Koltunski
|
import org.distorted.library.DistortedEffects;
|
27 |
392e16fd
|
Leszek Koltunski
|
import org.distorted.library.DistortedFramebuffer;
|
28 |
10b7e588
|
Leszek Koltunski
|
import org.distorted.library.GridFlat;
|
29 |
f6d884d5
|
Leszek Koltunski
|
import org.distorted.library.DistortedTexture;
|
30 |
7589635e
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
31 |
|
|
import org.distorted.library.type.Static4D;
|
32 |
427ab7bf
|
Leszek Koltunski
|
|
33 |
|
|
import android.graphics.Bitmap;
|
34 |
|
|
import android.graphics.Canvas;
|
35 |
|
|
import android.graphics.Paint;
|
36 |
|
|
import android.graphics.Paint.Style;
|
37 |
|
|
import android.opengl.GLES20;
|
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 |
f6d884d5
|
Leszek Koltunski
|
private DistortedTexture mTexture;
|
46 |
d04a4886
|
Leszek Koltunski
|
private DistortedEffects mEffects;
|
47 |
392e16fd
|
Leszek Koltunski
|
private DistortedFramebuffer mScreen;
|
48 |
10b7e588
|
Leszek Koltunski
|
private GridFlat mGrid;
|
49 |
10d53839
|
Leszek Koltunski
|
|
50 |
392e16fd
|
Leszek Koltunski
|
private float mF, mX, mY;
|
51 |
|
|
private int mWidth, mHeight;
|
52 |
2f9752c5
|
Leszek Koltunski
|
|
53 |
427ab7bf
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54 |
|
|
|
55 |
10d53839
|
Leszek Koltunski
|
ProjectionRenderer(GLSurfaceView view)
|
56 |
427ab7bf
|
Leszek Koltunski
|
{
|
57 |
392e16fd
|
Leszek Koltunski
|
mView = view;
|
58 |
d04a4886
|
Leszek Koltunski
|
mEffects= new DistortedEffects();
|
59 |
392e16fd
|
Leszek Koltunski
|
mScreen = new DistortedFramebuffer(0);
|
60 |
427ab7bf
|
Leszek Koltunski
|
}
|
61 |
|
|
|
62 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63 |
|
|
|
64 |
392e16fd
|
Leszek Koltunski
|
int setFOV(int f)
|
65 |
427ab7bf
|
Leszek Koltunski
|
{
|
66 |
10d53839
|
Leszek Koltunski
|
mF = f;
|
67 |
392e16fd
|
Leszek Koltunski
|
mScreen.setProjection(mF,mX,mY);
|
68 |
10d53839
|
Leszek Koltunski
|
return (int)mF;
|
69 |
427ab7bf
|
Leszek Koltunski
|
}
|
70 |
10d53839
|
Leszek Koltunski
|
|
71 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
72 |
|
|
|
73 |
392e16fd
|
Leszek Koltunski
|
int setX(int x)
|
74 |
10d53839
|
Leszek Koltunski
|
{
|
75 |
8cb9208c
|
Leszek Koltunski
|
mX = (x-50)*0.02f*mWidth;
|
76 |
392e16fd
|
Leszek Koltunski
|
mScreen.setProjection(mF,mX,mY);
|
77 |
10d53839
|
Leszek Koltunski
|
return (int)mX;
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
81 |
|
|
|
82 |
392e16fd
|
Leszek Koltunski
|
int setY(int y)
|
83 |
10d53839
|
Leszek Koltunski
|
{
|
84 |
8cb9208c
|
Leszek Koltunski
|
mY = (y-50)*0.02f*mHeight;
|
85 |
392e16fd
|
Leszek Koltunski
|
mScreen.setProjection(mF,mX,mY);
|
86 |
10d53839
|
Leszek Koltunski
|
return (int)mY;
|
87 |
|
|
}
|
88 |
|
|
|
89 |
427ab7bf
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
90 |
|
|
|
91 |
|
|
public void onDrawFrame(GL10 glUnused)
|
92 |
|
|
{
|
93 |
|
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
94 |
bc29e409
|
Leszek Koltunski
|
mScreen.renderTo(mTexture, mGrid, mEffects, System.currentTimeMillis() );
|
95 |
427ab7bf
|
Leszek Koltunski
|
}
|
96 |
|
|
|
97 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98 |
|
|
|
99 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
100 |
10d53839
|
Leszek Koltunski
|
{
|
101 |
|
|
mWidth = width;
|
102 |
|
|
mHeight= height;
|
103 |
|
|
|
104 |
392e16fd
|
Leszek Koltunski
|
mScreen.setProjection(mF,mX,mY);
|
105 |
|
|
mEffects.abortAllEffects();
|
106 |
95bc9f69
|
Leszek Koltunski
|
|
107 |
2f9752c5
|
Leszek Koltunski
|
Paint paint = new Paint();
|
108 |
7451c98a
|
Leszek Koltunski
|
mTexture = new DistortedTexture(width,height);
|
109 |
f6d884d5
|
Leszek Koltunski
|
mGrid = new GridFlat(50,50*height/width);
|
110 |
|
|
Bitmap bmp = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
|
111 |
10d53839
|
Leszek Koltunski
|
Canvas bmpCanvas = new Canvas(bmp);
|
112 |
427ab7bf
|
Leszek Koltunski
|
|
113 |
2f9752c5
|
Leszek Koltunski
|
paint.setColor(0xff008800);
|
114 |
|
|
paint.setStyle(Style.FILL);
|
115 |
f6d884d5
|
Leszek Koltunski
|
bmpCanvas.drawRect(0, 0, width, height, paint);
|
116 |
2f9752c5
|
Leszek Koltunski
|
paint.setColor(0xffffffff);
|
117 |
10d53839
|
Leszek Koltunski
|
|
118 |
|
|
final int NUMLINES = 10;
|
119 |
|
|
|
120 |
427ab7bf
|
Leszek Koltunski
|
for(int i=0; i<=NUMLINES ; i++ )
|
121 |
|
|
{
|
122 |
f6d884d5
|
Leszek Koltunski
|
bmpCanvas.drawRect(width*i/NUMLINES - 1, 0, width*i/NUMLINES + 1, height , paint);
|
123 |
|
|
bmpCanvas.drawRect( 0, height*i/NUMLINES -1, width , height*i/NUMLINES + 1, paint);
|
124 |
427ab7bf
|
Leszek Koltunski
|
}
|
125 |
f6d884d5
|
Leszek Koltunski
|
|
126 |
|
|
mTexture.setTexture(bmp);
|
127 |
|
|
|
128 |
|
|
int min = width<height ? width:height;
|
129 |
|
|
|
130 |
7bf107f7
|
Leszek Koltunski
|
Static3D vector = new Static3D(0,0,min/5);
|
131 |
|
|
Static4D region = new Static4D(0,0,min/5,min/5);
|
132 |
|
|
|
133 |
392e16fd
|
Leszek Koltunski
|
mEffects.distort(vector, new Static3D( width/4, height/4, 0), region);
|
134 |
|
|
mEffects.distort(vector, new Static3D(3*width/4, height/4, 0), region);
|
135 |
|
|
mEffects.distort(vector, new Static3D( width/4, 3*height/4, 0), region);
|
136 |
|
|
mEffects.distort(vector, new Static3D(3*width/4, 3*height/4, 0), region);
|
137 |
f6d884d5
|
Leszek Koltunski
|
|
138 |
392e16fd
|
Leszek Koltunski
|
mScreen.resize(width, height);
|
139 |
427ab7bf
|
Leszek Koltunski
|
}
|
140 |
|
|
|
141 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142 |
|
|
|
143 |
f6d884d5
|
Leszek Koltunski
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
144 |
|
|
{
|
145 |
|
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
146 |
|
|
|
147 |
|
|
try
|
148 |
|
|
{
|
149 |
|
|
Distorted.onSurfaceCreated(mView.getContext());
|
150 |
|
|
}
|
151 |
|
|
catch(Exception ex)
|
152 |
|
|
{
|
153 |
|
|
android.util.Log.e("Projection", ex.getMessage() );
|
154 |
|
|
}
|
155 |
|
|
}
|
156 |
427ab7bf
|
Leszek Koltunski
|
}
|