1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
|
20
|
package org.distorted.examples.bean;
|
21
|
|
22
|
import java.io.IOException;
|
23
|
import java.io.InputStream;
|
24
|
|
25
|
import javax.microedition.khronos.egl.EGLConfig;
|
26
|
import javax.microedition.khronos.opengles.GL10;
|
27
|
|
28
|
import org.distorted.examples.R;
|
29
|
|
30
|
import org.distorted.library.EffectTypes;
|
31
|
import org.distorted.library.Distorted;
|
32
|
import org.distorted.library.DistortedObject;
|
33
|
import org.distorted.library.GridFlat;
|
34
|
import org.distorted.library.type.Dynamic3D;
|
35
|
import org.distorted.library.type.Static3D;
|
36
|
import org.distorted.library.type.Static4D;
|
37
|
|
38
|
import android.graphics.Bitmap;
|
39
|
import android.graphics.BitmapFactory;
|
40
|
import android.opengl.GLES20;
|
41
|
import android.opengl.GLSurfaceView;
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
|
45
|
class BeanRenderer implements GLSurfaceView.Renderer
|
46
|
{
|
47
|
private GLSurfaceView mView;
|
48
|
private DistortedObject mBean;
|
49
|
private GridFlat mGrid;
|
50
|
private Static3D pLeft, pRight;
|
51
|
private Dynamic3D dLeft, dRight;
|
52
|
private Static4D rLeft, rRight;
|
53
|
private int bmpHeight, bmpWidth;
|
54
|
|
55
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
56
|
|
57
|
BeanRenderer(GLSurfaceView v)
|
58
|
{
|
59
|
mView = v;
|
60
|
|
61
|
pLeft = new Static3D( 98, 183, 0);
|
62
|
pRight= new Static3D(233, 140, 0);
|
63
|
|
64
|
rLeft = new Static4D( -3,-33,47,47);
|
65
|
rRight= new Static4D(-14,-33,47,47);
|
66
|
|
67
|
dLeft = new Dynamic3D(2000,0.0f);
|
68
|
dRight= new Dynamic3D(2000,0.0f);
|
69
|
|
70
|
Static3D p1 = new Static3D( 0, 0, 0);
|
71
|
Static3D p2 = new Static3D(-15,-30, 0);
|
72
|
|
73
|
dLeft.add(p1);
|
74
|
dLeft.add(p1);
|
75
|
dLeft.add(p1);
|
76
|
dLeft.add(p1);
|
77
|
dLeft.add(p2);
|
78
|
dLeft.add(p2);
|
79
|
|
80
|
dRight.add(p1);
|
81
|
dRight.add(p2);
|
82
|
dRight.add(p2);
|
83
|
dRight.add(p1);
|
84
|
dRight.add(p1);
|
85
|
dRight.add(p1);
|
86
|
}
|
87
|
|
88
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89
|
|
90
|
public void onDrawFrame(GL10 glUnused)
|
91
|
{
|
92
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
93
|
mBean.draw(System.currentTimeMillis(), mGrid);
|
94
|
}
|
95
|
|
96
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
97
|
|
98
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
99
|
{
|
100
|
mBean.abortEffects(EffectTypes.MATRIX);
|
101
|
|
102
|
if( (float)bmpHeight/bmpWidth > (float)height/width )
|
103
|
{
|
104
|
int w = (height*bmpWidth)/bmpHeight;
|
105
|
float factor = (float)height/bmpHeight;
|
106
|
|
107
|
mBean.move( new Static3D((width-w)/2,0,0) );
|
108
|
mBean.scale(factor);
|
109
|
}
|
110
|
else
|
111
|
{
|
112
|
int h = (width*bmpHeight)/bmpWidth;
|
113
|
float factor = (float)width/bmpWidth;
|
114
|
|
115
|
mBean.move( new Static3D(0,(height-h)/2,0) );
|
116
|
mBean.scale(factor);
|
117
|
}
|
118
|
|
119
|
Distorted.onSurfaceChanged(width, height);
|
120
|
}
|
121
|
|
122
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
123
|
|
124
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
125
|
{
|
126
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
127
|
|
128
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
|
129
|
Bitmap bitmap;
|
130
|
|
131
|
try
|
132
|
{
|
133
|
bitmap = BitmapFactory.decodeStream(is);
|
134
|
}
|
135
|
finally
|
136
|
{
|
137
|
try
|
138
|
{
|
139
|
is.close();
|
140
|
}
|
141
|
catch(IOException e) { }
|
142
|
}
|
143
|
|
144
|
bmpHeight = bitmap.getHeight();
|
145
|
bmpWidth = bitmap.getWidth();
|
146
|
|
147
|
mBean = new DistortedObject(bmpWidth, bmpHeight, 1);
|
148
|
mBean.setTexture(bitmap);
|
149
|
mBean.distort(dLeft , pLeft , rLeft );
|
150
|
mBean.distort(dRight, pRight, rRight);
|
151
|
|
152
|
mGrid = new GridFlat(25,25*bmpHeight/bmpWidth);
|
153
|
|
154
|
try
|
155
|
{
|
156
|
Distorted.onSurfaceCreated(mView.getContext());
|
157
|
}
|
158
|
catch(Exception ex)
|
159
|
{
|
160
|
android.util.Log.e("Bean", ex.getMessage() );
|
161
|
}
|
162
|
}
|
163
|
}
|