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.meshfile;
|
21
|
|
22
|
import android.content.Context;
|
23
|
import android.content.res.Resources;
|
24
|
import android.graphics.Bitmap;
|
25
|
import android.graphics.Canvas;
|
26
|
import android.graphics.Paint;
|
27
|
import android.opengl.GLSurfaceView;
|
28
|
|
29
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
30
|
import org.distorted.library.effect.MatrixEffectScale;
|
31
|
import org.distorted.library.main.DistortedEffects;
|
32
|
import org.distorted.library.main.DistortedLibrary;
|
33
|
import org.distorted.library.main.DistortedScreen;
|
34
|
import org.distorted.library.main.DistortedTexture;
|
35
|
import org.distorted.library.mesh.MeshBase;
|
36
|
import org.distorted.library.mesh.MeshFile;
|
37
|
import org.distorted.library.type.DynamicQuat;
|
38
|
import org.distorted.library.type.Static3D;
|
39
|
import org.distorted.library.type.Static4D;
|
40
|
|
41
|
import java.io.DataInputStream;
|
42
|
import java.io.IOException;
|
43
|
import java.io.InputStream;
|
44
|
|
45
|
import javax.microedition.khronos.egl.EGLConfig;
|
46
|
import javax.microedition.khronos.opengles.GL10;
|
47
|
|
48
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49
|
|
50
|
class MeshFileRenderer implements GLSurfaceView.Renderer
|
51
|
{
|
52
|
private GLSurfaceView mView;
|
53
|
private DistortedTexture mTexture;
|
54
|
private DistortedScreen mScreen;
|
55
|
private DistortedEffects mEffects;
|
56
|
private Static3D mScale;
|
57
|
private MeshBase mMesh;
|
58
|
|
59
|
Static4D mQuat1, mQuat2;
|
60
|
int mScreenMin;
|
61
|
|
62
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63
|
|
64
|
MeshFileRenderer(GLSurfaceView v)
|
65
|
{
|
66
|
mView = v;
|
67
|
mScreen = new DistortedScreen();
|
68
|
mScale= new Static3D(1,1,1);
|
69
|
Static3D center=new Static3D(0,0,0);
|
70
|
|
71
|
mQuat1 = new Static4D(0,0,0,1);
|
72
|
mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
|
73
|
|
74
|
DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
|
75
|
DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
|
76
|
|
77
|
quatInt1.add(mQuat1);
|
78
|
quatInt2.add(mQuat2);
|
79
|
|
80
|
mEffects = new DistortedEffects();
|
81
|
mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
|
82
|
mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
|
83
|
mEffects.apply( new MatrixEffectScale(mScale));
|
84
|
|
85
|
mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
86
|
}
|
87
|
|
88
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89
|
|
90
|
public void onDrawFrame(GL10 glUnused)
|
91
|
{
|
92
|
mScreen.render( System.currentTimeMillis() );
|
93
|
}
|
94
|
|
95
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
96
|
|
97
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
98
|
{
|
99
|
final float SCALE = 0.3f;
|
100
|
mScreenMin = Math.min(width, height);
|
101
|
float factor = SCALE*mScreenMin;
|
102
|
mScale.set(factor,factor,factor);
|
103
|
mScreen.resize(width, height);
|
104
|
}
|
105
|
|
106
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107
|
|
108
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
109
|
{
|
110
|
if( mTexture==null ) mTexture = new DistortedTexture();
|
111
|
mTexture.setTexture( createTexture(0) );
|
112
|
|
113
|
try
|
114
|
{
|
115
|
DistortedLibrary.onCreate(mView.getContext());
|
116
|
}
|
117
|
catch(Exception ex)
|
118
|
{
|
119
|
android.util.Log.e("MeshFile", ex.getMessage() );
|
120
|
}
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
void open(int resourceID)
|
126
|
{
|
127
|
long t1 = System.currentTimeMillis();
|
128
|
mMesh = createMesh(resourceID);
|
129
|
long t2 = System.currentTimeMillis();
|
130
|
|
131
|
android.util.Log.e("file", "time: "+(t2-t1));
|
132
|
|
133
|
mScreen.detachAll();
|
134
|
mScreen.attach(mTexture,mEffects,mMesh);
|
135
|
}
|
136
|
|
137
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138
|
|
139
|
private Bitmap createTexture(int resourceID)
|
140
|
{
|
141
|
final int[] FACE_COLORS = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
|
142
|
final int FACES=FACE_COLORS.length;
|
143
|
int SIZE = 200;
|
144
|
float STROKE = 0.05f*SIZE;
|
145
|
float OFF = STROKE/2 -1;
|
146
|
float OFF2 = 0.5f*SIZE + OFF;
|
147
|
float HEIGHT = SIZE - OFF;
|
148
|
float RADIUS = SIZE/12;
|
149
|
float ARC1_H = 0.2f*SIZE;
|
150
|
float ARC1_W = SIZE*0.5f;
|
151
|
float ARC2_W = 0.153f*SIZE;
|
152
|
float ARC2_H = 0.905f*SIZE;
|
153
|
float ARC3_W = SIZE-ARC2_W;
|
154
|
|
155
|
Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
|
156
|
Canvas canvas = new Canvas(result);
|
157
|
Paint paint = new Paint();
|
158
|
paint.setAntiAlias(true);
|
159
|
paint.setStrokeWidth(STROKE);
|
160
|
|
161
|
for(int i=0; i<FACES; i++)
|
162
|
{
|
163
|
paint.setColor(FACE_COLORS[i]);
|
164
|
paint.setStyle(Paint.Style.FILL);
|
165
|
|
166
|
canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
|
167
|
|
168
|
paint.setColor(0xff000000);
|
169
|
paint.setStyle(Paint.Style.STROKE);
|
170
|
|
171
|
canvas.drawLine( i*SIZE, HEIGHT, SIZE +i*SIZE, HEIGHT, paint);
|
172
|
canvas.drawLine( OFF +i*SIZE, SIZE, OFF2 +i*SIZE, 0, paint);
|
173
|
canvas.drawLine((SIZE-OFF)+i*SIZE, SIZE, (SIZE-OFF2) +i*SIZE, 0, paint);
|
174
|
|
175
|
canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
|
176
|
canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
|
177
|
canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
|
178
|
}
|
179
|
|
180
|
return result;
|
181
|
}
|
182
|
|
183
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
184
|
|
185
|
private MeshBase createMesh(int resourceID)
|
186
|
{
|
187
|
Context con = mView.getContext();
|
188
|
Resources res = con.getResources();
|
189
|
InputStream is = res.openRawResource(resourceID);
|
190
|
DataInputStream dos = new DataInputStream(is);
|
191
|
MeshBase mesh = new MeshFile(dos);
|
192
|
|
193
|
try
|
194
|
{
|
195
|
is.close();
|
196
|
}
|
197
|
catch(IOException e)
|
198
|
{
|
199
|
android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
|
200
|
}
|
201
|
|
202
|
return mesh;
|
203
|
}
|
204
|
}
|