Skip to content

Commit cdc0255

Browse files
committed
add a Director response for model, view and projection matrix
1 parent c12e6bb commit cdc0255

File tree

5 files changed

+264
-120
lines changed

5 files changed

+264
-120
lines changed

app/src/main/java/com/asha/md360player4android/MD360DemoActivity.java

Lines changed: 85 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,91 @@
66
import android.content.pm.ConfigurationInfo;
77
import android.opengl.GLSurfaceView;
88
import android.os.Bundle;
9+
import android.view.View;
10+
import android.widget.SeekBar;
11+
import android.widget.Toast;
912

1013
/**
1114
* Created by hzqiujiadi on 16/1/22.
1215
* hzqiujiadi ashqalcn@gmail.com
1316
*/
1417
public class MD360DemoActivity extends Activity {
1518

16-
/** Hold a reference to our GLSurfaceView */
19+
private static final String TAG = "MD360DemoActivity";
20+
/** Hold a reference to our GLSurfaceView */
1721
private GLSurfaceView mGLSurfaceView;
22+
private MD360Director mDirector;
1823

1924
@Override
2025
public void onCreate(Bundle savedInstanceState){
2126
super.onCreate(savedInstanceState);
22-
23-
mGLSurfaceView = new GLSurfaceView(this);
24-
25-
// Check if the system supports OpenGL ES 2.0.
26-
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
27-
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
28-
final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
29-
30-
if (supportsEs2){
31-
// Request an OpenGL ES 2.0 compatible context.
32-
mGLSurfaceView.setEGLContextClientVersion(2);
33-
34-
// Set the renderer to our demo renderer, defined below.
35-
mGLSurfaceView.setRenderer(new MD360Renderer(this));
36-
} else {
37-
// This is where you could create an OpenGL ES 1.x compatible
38-
// renderer if you wanted to support both ES 1 and ES 2.
39-
return;
40-
}
41-
42-
setContentView(mGLSurfaceView);
27+
setContentView(R.layout.activity_main);
28+
29+
mDirector = new MD360Director();
30+
initOpenGL();
31+
initSeekBar();
4332
}
4433

34+
private void initOpenGL(){
35+
mGLSurfaceView = (GLSurfaceView) findViewById(R.id.surface_view);
36+
37+
// Check if the system supports OpenGL ES 2.0.
38+
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
39+
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
40+
final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
41+
42+
if (supportsEs2){
43+
// Request an OpenGL ES 2.0 compatible context.
44+
mGLSurfaceView.setEGLContextClientVersion(2);
45+
46+
// Set the renderer to our demo renderer, defined below.
47+
mGLSurfaceView.setRenderer(new MD360Renderer(this,mDirector));
48+
} else {
49+
mGLSurfaceView.setVisibility(View.GONE);
50+
Toast.makeText(MD360DemoActivity.this, "OpenGLES2 not supported.", Toast.LENGTH_SHORT).show();
51+
}
52+
}
53+
54+
private static float progressToValue(int progress, int min, int max){
55+
int range = max - min;
56+
float result = progress * 1.0f * range / 100.0f + min;
57+
return result;
58+
}
59+
60+
private void initSeekBar() {
61+
SeekBar viewSeekBar = (SeekBar) findViewById(R.id.seek_bar_view);
62+
SeekBar modelSeekBar = (SeekBar) findViewById(R.id.seek_bar_model);
63+
SeekBar projectionSeekBar = (SeekBar) findViewById(R.id.seek_bar_projection);
64+
65+
viewSeekBar.setOnSeekBarChangeListener(new SeekBarOnChangeListenerAdapter() {
66+
@Override
67+
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
68+
int min = 2;
69+
int max = 50;
70+
mDirector.updateCameraDistance(progressToValue(progress,min,max));
71+
}
72+
});
73+
74+
modelSeekBar.setOnSeekBarChangeListener(new SeekBarOnChangeListenerAdapter() {
75+
@Override
76+
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
77+
int min = 0;
78+
int max = 360;
79+
mDirector.updateModelRotate(progressToValue(progress,min,max));
80+
}
81+
});
82+
83+
projectionSeekBar.setOnSeekBarChangeListener(new SeekBarOnChangeListenerAdapter() {
84+
@Override
85+
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
86+
int min = 1;
87+
int max = 12;
88+
mDirector.updateProjectionNear(progressToValue(progress,min,max));
89+
}
90+
});
91+
92+
}
93+
4594
@Override
4695
protected void onResume(){
4796
// The activity must call the GL surface view's onResume() on activity onResume().
@@ -54,5 +103,18 @@ protected void onPause(){
54103
// The activity must call the GL surface view's onPause() on activity onPause().
55104
super.onPause();
56105
mGLSurfaceView.onPause();
57-
}
106+
}
107+
108+
public static abstract class SeekBarOnChangeListenerAdapter implements SeekBar.OnSeekBarChangeListener{
109+
@Override
110+
public void onStartTrackingTouch(SeekBar seekBar) {
111+
// nope
112+
}
113+
114+
@Override
115+
public void onStopTrackingTouch(SeekBar seekBar) {
116+
// nope
117+
}
118+
}
119+
58120
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.asha.md360player4android;
2+
3+
import android.opengl.GLES20;
4+
import android.opengl.Matrix;
5+
import android.util.Log;
6+
7+
/**
8+
* Created by hzqiujiadi on 16/1/22.
9+
* hzqiujiadi ashqalcn@gmail.com
10+
*
11+
* response for model * view * projection
12+
*/
13+
public class MD360Director {
14+
15+
private static final String TAG = "MD360Director";
16+
private float[] mModelMatrix = new float[16];
17+
private float[] mViewMatrix = new float[16];
18+
private float[] mProjectionMatrix = new float[16];
19+
20+
private float[] mMVMatrix = new float[16];
21+
private float[] mMVPMatrix = new float[16];
22+
23+
public MD360Director() {
24+
25+
}
26+
27+
public void prepare(){
28+
initCamera();
29+
initModel();
30+
}
31+
32+
private void initCamera() {
33+
// View Matrix
34+
updateCameraDistance(12);
35+
}
36+
37+
private void initModel(){
38+
// Model Matrix
39+
updateModelRotate(0);
40+
}
41+
42+
public void shot(MD360Program program) {
43+
// This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
44+
// (which currently contains model * view).
45+
Matrix.multiplyMM(mMVMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
46+
47+
// This multiplies the model view matrix by the projection matrix, and stores the result in the MVP matrix
48+
// (which now contains model * view * projection).
49+
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVMatrix, 0);
50+
51+
// Pass in the model view matrix
52+
GLES20.glUniformMatrix4fv(program.getMVMatrixHandle(), 1, false, mMVMatrix, 0);
53+
54+
// Pass in the combined matrix.
55+
GLES20.glUniformMatrix4fv(program.getMVPMatrixHandle(), 1, false, mMVPMatrix, 0);
56+
}
57+
58+
public void updateCameraDistance(float z) {
59+
Log.d(TAG,"updateCameraDistance:" + z);
60+
final float eyeX = 0.0f;
61+
final float eyeY = 0.0f;
62+
final float eyeZ = z;
63+
final float lookX = 0.0f;
64+
final float lookY = 0.0f;
65+
final float lookZ = 0.0f;
66+
final float upX = 0.0f;
67+
final float upY = 1.0f;
68+
final float upZ = 0.0f;
69+
Matrix.setIdentityM(mViewMatrix, 0);
70+
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
71+
}
72+
73+
public void updateModelRotate(float a) {
74+
Matrix.setIdentityM(mModelMatrix, 0);
75+
Matrix.setRotateM(mModelMatrix,0,a,0,1,0);
76+
}
77+
78+
private float mRatio = 1.5f;
79+
private float mNear = 1;
80+
public void updateProjection(int width, int height){
81+
// Projection Matrix
82+
mRatio = width * 1.0f / height;
83+
updateProjectionNear(mNear);
84+
}
85+
86+
public void updateProjectionNear(float near){
87+
mNear = near;
88+
final float left = -mRatio;
89+
final float right = mRatio;
90+
final float bottom = -0.5f;
91+
final float top = 0.5f;
92+
final float far = 500;
93+
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, mNear, far);
94+
}
95+
}

0 commit comments

Comments
 (0)