Revision 508ee98f
Added by Leszek Koltunski almost 5 years ago
src/main/AndroidManifest.xml | ||
---|---|---|
58 | 58 |
<activity android:name=".movingglow.MovingGlowActivity"/> |
59 | 59 |
<activity android:name=".earth.EarthActivity"/> |
60 | 60 |
<activity android:name=".rubik.RubikActivity"/> |
61 |
<activity android:name=".meshjoin.MeshJoinActivity"/> |
|
61 | 62 |
</application> |
62 | 63 |
</manifest> |
src/main/java/org/distorted/examples/TableOfContents.java | ||
---|---|---|
70 | 70 |
import org.distorted.examples.movingglow.MovingGlowActivity; |
71 | 71 |
import org.distorted.examples.earth.EarthActivity; |
72 | 72 |
import org.distorted.examples.rubik.RubikActivity; |
73 |
import org.distorted.examples.meshjoin.MeshJoinActivity; |
|
73 | 74 |
|
74 | 75 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
75 | 76 |
|
... | ... | |
116 | 117 |
GLOW (R.drawable.icon_example_glow , R.string.example_glow , R.string.example_glow_subtitle , GlowActivity.class), |
117 | 118 |
MOVINGGLOW (R.drawable.icon_example_movingglow , R.string.example_moving_glow , R.string.example_moving_glow_subtitle , MovingGlowActivity.class), |
118 | 119 |
EARTH (R.drawable.icon_example_earth , R.string.example_earth , R.string.example_earth_subtitle , EarthActivity.class), |
119 |
RUBIK (R.drawable.icon_example_rubik , R.string.example_rubik , R.string.example_rubik_subtitle , RubikActivity.class) |
|
120 |
RUBIK (R.drawable.icon_example_rubik , R.string.example_rubik , R.string.example_rubik_subtitle , RubikActivity.class), |
|
121 |
MESHJOIN (R.drawable.icon_example_wip , R.string.example_meshjoin , R.string.example_meshjoin_subtitle , MeshJoinActivity.class), |
|
120 | 122 |
; |
121 | 123 |
|
122 | 124 |
final int icon, title, subtitle; |
src/main/java/org/distorted/examples/meshjoin/MeshJoinActivity.java | ||
---|---|---|
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.meshjoin; |
|
21 |
|
|
22 |
import android.app.Activity; |
|
23 |
import android.os.Bundle; |
|
24 |
|
|
25 |
import org.distorted.library.main.DistortedLibrary; |
|
26 |
|
|
27 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
28 |
|
|
29 |
public class MeshJoinActivity extends Activity |
|
30 |
{ |
|
31 |
private MeshJoinSurfaceView mView; |
|
32 |
|
|
33 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
34 |
|
|
35 |
@Override |
|
36 |
protected void onCreate(Bundle icicle) |
|
37 |
{ |
|
38 |
super.onCreate(icicle); |
|
39 |
mView = new MeshJoinSurfaceView(this); |
|
40 |
setContentView(mView); |
|
41 |
} |
|
42 |
|
|
43 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
44 |
|
|
45 |
@Override |
|
46 |
protected void onPause() |
|
47 |
{ |
|
48 |
mView.onPause(); |
|
49 |
DistortedLibrary.onPause(); |
|
50 |
super.onPause(); |
|
51 |
} |
|
52 |
|
|
53 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
54 |
|
|
55 |
@Override |
|
56 |
protected void onResume() |
|
57 |
{ |
|
58 |
super.onResume(); |
|
59 |
mView.onResume(); |
|
60 |
} |
|
61 |
|
|
62 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
63 |
|
|
64 |
@Override |
|
65 |
protected void onDestroy() |
|
66 |
{ |
|
67 |
DistortedLibrary.onDestroy(); |
|
68 |
super.onDestroy(); |
|
69 |
} |
|
70 |
|
|
71 |
} |
src/main/java/org/distorted/examples/meshjoin/MeshJoinRenderer.java | ||
---|---|---|
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.meshjoin; |
|
21 |
|
|
22 |
import android.graphics.Bitmap; |
|
23 |
import android.graphics.BitmapFactory; |
|
24 |
import android.opengl.GLSurfaceView; |
|
25 |
|
|
26 |
import org.distorted.examples.R; |
|
27 |
import org.distorted.library.effect.MatrixEffectMove; |
|
28 |
import org.distorted.library.effect.MatrixEffectQuaternion; |
|
29 |
import org.distorted.library.effect.MatrixEffectScale; |
|
30 |
import org.distorted.library.main.DistortedEffects; |
|
31 |
import org.distorted.library.main.DistortedLibrary; |
|
32 |
import org.distorted.library.main.DistortedScreen; |
|
33 |
import org.distorted.library.main.DistortedTexture; |
|
34 |
import org.distorted.library.mesh.MeshBase; |
|
35 |
import org.distorted.library.mesh.MeshQuad; |
|
36 |
import org.distorted.library.type.DynamicQuat; |
|
37 |
import org.distorted.library.type.Static3D; |
|
38 |
import org.distorted.library.type.Static4D; |
|
39 |
|
|
40 |
import java.io.IOException; |
|
41 |
import java.io.InputStream; |
|
42 |
|
|
43 |
import javax.microedition.khronos.egl.EGLConfig; |
|
44 |
import javax.microedition.khronos.opengles.GL10; |
|
45 |
|
|
46 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
47 |
|
|
48 |
class MeshJoinRenderer implements GLSurfaceView.Renderer |
|
49 |
{ |
|
50 |
private GLSurfaceView mView; |
|
51 |
private DistortedTexture mTexture; |
|
52 |
private DistortedScreen mScreen; |
|
53 |
private DistortedEffects mEffects; |
|
54 |
private Static3D mMove, mScale, mCenter; |
|
55 |
private MeshBase mMesh; |
|
56 |
private float mObjHeight, mObjWidth, mObjDepth; |
|
57 |
|
|
58 |
Static4D mQuat1, mQuat2; |
|
59 |
int mScreenMin; |
|
60 |
|
|
61 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
62 |
|
|
63 |
MeshJoinRenderer(GLSurfaceView v) |
|
64 |
{ |
|
65 |
mView = v; |
|
66 |
mScreen = new DistortedScreen(); |
|
67 |
mMove = new Static3D(0,0,0); |
|
68 |
mScale= new Static3D(1,1,1); |
|
69 |
mCenter=new Static3D(0,0,0); |
|
70 |
|
|
71 |
mQuat1 = new Static4D(0,0,0,1); // unity |
|
72 |
mQuat2 = new Static4D(0,0,0,1); // quaternions |
|
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, mCenter) ); |
|
82 |
mEffects.apply( new MatrixEffectQuaternion(quatInt1, mCenter) ); |
|
83 |
mEffects.apply( new MatrixEffectScale(mScale)); |
|
84 |
mEffects.apply( new MatrixEffectMove(mMove) ); |
|
85 |
} |
|
86 |
|
|
87 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
88 |
|
|
89 |
public void onDrawFrame(GL10 glUnused) |
|
90 |
{ |
|
91 |
mScreen.render( System.currentTimeMillis() ); |
|
92 |
} |
|
93 |
|
|
94 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
95 |
|
|
96 |
public void onSurfaceChanged(GL10 glUnused, int width, int height) |
|
97 |
{ |
|
98 |
final float SCALE = 0.75f; |
|
99 |
|
|
100 |
mScreenMin = width<height ? width:height; |
|
101 |
float factor = ( width*mObjHeight > height*mObjWidth ) ? (SCALE*height)/mObjHeight : (SCALE*width)/mObjWidth; |
|
102 |
mMove.set( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , -factor*mObjDepth/2 ); |
|
103 |
mScale.set(factor,factor,factor); |
|
104 |
mCenter.set( mObjWidth/2, mObjHeight/2, mObjDepth/2 ); |
|
105 |
mScreen.resize(width, height); |
|
106 |
} |
|
107 |
|
|
108 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
109 |
|
|
110 |
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) |
|
111 |
{ |
|
112 |
InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl); |
|
113 |
Bitmap bitmap; |
|
114 |
|
|
115 |
try |
|
116 |
{ |
|
117 |
bitmap = BitmapFactory.decodeStream(is); |
|
118 |
} |
|
119 |
finally |
|
120 |
{ |
|
121 |
try |
|
122 |
{ |
|
123 |
is.close(); |
|
124 |
} |
|
125 |
catch(IOException e) { } |
|
126 |
} |
|
127 |
|
|
128 |
if( mTexture==null ) mTexture = new DistortedTexture(); |
|
129 |
mTexture.setTexture(bitmap); |
|
130 |
|
|
131 |
if( mMesh==null ) mMesh = createJoinedMesh(); |
|
132 |
|
|
133 |
mObjWidth = mMesh.getStretchX(); |
|
134 |
mObjHeight= mMesh.getStretchY(); |
|
135 |
mObjDepth = mMesh.getStretchZ(); |
|
136 |
|
|
137 |
mScreen.detachAll(); |
|
138 |
mScreen.attach(mTexture,mEffects,mMesh); |
|
139 |
|
|
140 |
try |
|
141 |
{ |
|
142 |
DistortedLibrary.onCreate(mView.getContext()); |
|
143 |
} |
|
144 |
catch(Exception ex) |
|
145 |
{ |
|
146 |
android.util.Log.e("MeshJoin", ex.getMessage() ); |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
151 |
|
|
152 |
private MeshBase createJoinedMesh() |
|
153 |
{ |
|
154 |
return new MeshQuad(); |
|
155 |
} |
|
156 |
} |
src/main/java/org/distorted/examples/meshjoin/MeshJoinSurfaceView.java | ||
---|---|---|
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.meshjoin; |
|
21 |
|
|
22 |
import android.app.ActivityManager; |
|
23 |
import android.content.Context; |
|
24 |
import android.content.pm.ConfigurationInfo; |
|
25 |
import android.opengl.GLSurfaceView; |
|
26 |
import android.view.MotionEvent; |
|
27 |
|
|
28 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
29 |
|
|
30 |
class MeshJoinSurfaceView extends GLSurfaceView |
|
31 |
{ |
|
32 |
private int mX, mY; |
|
33 |
private MeshJoinRenderer mRenderer; |
|
34 |
|
|
35 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
36 |
|
|
37 |
public MeshJoinSurfaceView(Context context) |
|
38 |
{ |
|
39 |
super(context); |
|
40 |
|
|
41 |
mX = -1; |
|
42 |
mY = -1; |
|
43 |
|
|
44 |
mRenderer = new MeshJoinRenderer(this); |
|
45 |
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
|
46 |
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo(); |
|
47 |
setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 ); |
|
48 |
setRenderer(mRenderer); |
|
49 |
} |
|
50 |
|
|
51 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
52 |
|
|
53 |
public MeshJoinRenderer getRenderer() |
|
54 |
{ |
|
55 |
return mRenderer; |
|
56 |
} |
|
57 |
|
|
58 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
59 |
|
|
60 |
@Override public boolean onTouchEvent(MotionEvent event) |
|
61 |
{ |
|
62 |
int action = event.getAction(); |
|
63 |
int x = (int)event.getX(); |
|
64 |
int y = (int)event.getY(); |
|
65 |
|
|
66 |
switch(action) |
|
67 |
{ |
|
68 |
case MotionEvent.ACTION_DOWN: mX = x; |
|
69 |
mY = y; |
|
70 |
break; |
|
71 |
|
|
72 |
case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 ) |
|
73 |
{ |
|
74 |
float px = mY-y; |
|
75 |
float py = mX-x; |
|
76 |
float pz = 0; |
|
77 |
float plen = (float)Math.sqrt(px*px + py*py + pz*pz); |
|
78 |
|
|
79 |
if( plen>0 ) |
|
80 |
{ |
|
81 |
px /= plen; |
|
82 |
py /= plen; |
|
83 |
pz /= plen; |
|
84 |
|
|
85 |
float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin); |
|
86 |
float sinA = (float)Math.sqrt(1-cosA*cosA); |
|
87 |
|
|
88 |
mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA); |
|
89 |
} |
|
90 |
} |
|
91 |
break; |
|
92 |
|
|
93 |
case MotionEvent.ACTION_UP : mX = -1; |
|
94 |
mY = -1; |
|
95 |
|
|
96 |
float qx = mRenderer.mQuat1.get0(); |
|
97 |
float qy = mRenderer.mQuat1.get1(); |
|
98 |
float qz = mRenderer.mQuat1.get2(); |
|
99 |
float qw = mRenderer.mQuat1.get3(); |
|
100 |
|
|
101 |
float rx = mRenderer.mQuat2.get0(); |
|
102 |
float ry = mRenderer.mQuat2.get1(); |
|
103 |
float rz = mRenderer.mQuat2.get2(); |
|
104 |
float rw = mRenderer.mQuat2.get3(); |
|
105 |
|
|
106 |
// This is quaternion multiplication. (tx,ty,tz,tw) |
|
107 |
// is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw) |
|
108 |
float tx = rw*qx - rz*qy + ry*qz + rx*qw; |
|
109 |
float ty = rw*qy + rz*qx + ry*qw - rx*qz; |
|
110 |
float tz = rw*qz + rz*qw - ry*qx + rx*qy; |
|
111 |
float tw = rw*qw - rz*qz - ry*qy - rx*qx; |
|
112 |
|
|
113 |
// The point of this is so that there are always |
|
114 |
// exactly 2 quaternions: Quat1 representing the rotation |
|
115 |
// accumulating only since the last screen touch, and Quat2 |
|
116 |
// which remembers the combined effect of all previous |
|
117 |
// swipes. |
|
118 |
// We cannot be accumulating an ever-growing list of quaternions |
|
119 |
// and add a new one every time user swipes the screen - there |
|
120 |
// is a limited number of slots in the EffectQueueMatrix! |
|
121 |
mRenderer.mQuat1.set(0f, 0f, 0f, 1f); |
|
122 |
mRenderer.mQuat2.set(tx, ty, tz, tw); |
|
123 |
|
|
124 |
break; |
|
125 |
} |
|
126 |
|
|
127 |
return true; |
|
128 |
} |
|
129 |
|
|
130 |
} |
|
131 |
|
src/main/res/values/strings.xml | ||
---|---|---|
193 | 193 |
<string name="example_earth_subtitle">Test the sphere Mesh by showing the Earth in cosmos.</string> |
194 | 194 |
<string name="example_rubik">Memory Test</string> |
195 | 195 |
<string name="example_rubik_subtitle">Keep an eye on memory consumption while allocating and deallocating Nodes.</string> |
196 |
<string name="example_meshjoin">Joining Meshes</string> |
|
197 |
<string name="example_meshjoin_subtitle">See how one can join several simple meshes to form a single, more advanced one.</string> |
|
196 | 198 |
|
197 | 199 |
<string name="example_movingeffects_toast">Click on \'RESET\' and define your path by touching the screen. Then click on one of the effects and see it move along your path.</string> |
198 | 200 |
<string name="example_rotate_toast">Rotate the scene by swiping the screen</string> |
Also available in: Unified diff
New API: MeshBase.join() and a skeleton of an App to test it.