26. onNewFrame() 구현하기
private int mGlProgram = GLES20.glCreateProgram();
private int mPositionParam;
private int mNormalParam;
private int mColorParam;
private int mModelViewProjectionParam;
private int mLightPosParam;
private int mModelViewParam;
private int mModelParam;
private int mIsFloorParam;
private float[] mHeadView;
...
/**
* Prepares OpenGL ES before we draw a frame.
* @param headTransform The head transformation in
the new frame.
*/
…
…
@Override
public void onNewFrame(HeadTransform headTransform) {
GLES20.glUseProgram(mGlProgram);
mModelViewProjectionParam = GLES20.
glGetUniformLocation(mGlProgram, "u_MVP");
mLightPosParam = GLES20.glGetUniformLocation
(mGlProgram, "u_LightPos");
mModelViewParam = GLES20.glGetUniformLocation
(mGlProgram, "u_MVMatrix");
mModelParam = GLES20.glGetUniformLocation
(mGlProgram, "u_Model");
mIsFloorParam = GLES20.glGetUniformLocation
(mGlProgram, "u_IsFloor");
// Build the Model part of the ModelView matrix.
Matrix.rotateM(mModelCube, 0, TIME_DELTA, 0.5f, 0.5
f, 1.0f);
// Build the camera matrix and apply it to the
ModelView.
Matrix.setLookAtM(mCamera, 0, 0.0f, 0.0f, CAMERA_Z,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
headTransform.getHeadView(mHeadView, 0);
checkGLError("onReadyToDraw");
}
27. onDrawEye() 구현하기
private int mPositionParam;
private int mNormalParam;
private int mColorParam;
// We keep the light always position
just above the user.
private final float[]
mLightPosInEyeSpace = new float[] {0.0f,
2.0f, 0.0f, 1.0f};
...
/**
* Draws a frame for an eye. The
transformation for that eye (from the
camera) is passed in as
* a parameter.
* @param transform The transformations
to apply to render this eye.
*/
…
@Override
public void onDrawEye(EyeTransform transform) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.
GL_DEPTH_BUFFER_BIT);
mPositionParam = GLES20.glGetAttribLocation(mGlProgram, "a_Position");
mNormalParam = GLES20.glGetAttribLocation(mGlProgram, "a_Normal");
mColorParam = GLES20.glGetAttribLocation(mGlProgram, "a_Color");
GLES20.glEnableVertexAttribArray(mPositionParam);
GLES20.glEnableVertexAttribArray(mNormalParam);
GLES20.glEnableVertexAttribArray(mColorParam);
checkGLError("mColorParam");
// Apply the eye transformation to the camera.
Matrix.multiplyMM(mView, 0, transform.getEyeView(), 0, mCamera, 0);
// Set the position of the light
Matrix.multiplyMV(mLightPosInEyeSpace, 0, mView, 0,
mLightPosInWorldSpace, 0);
GLES20.glUniform3f(mLightPosParam, mLightPosInEyeSpace[0],
mLightPosInEyeSpace[1],
mLightPosInEyeSpace[2]);
// Build the ModelView and ModelViewProjection matrices
// for calculating cube position and light.
Matrix.multiplyMM(mModelView, 0, mView, 0, mModelCube, 0);
Matrix.multiplyMM(mModelViewProjection, 0, transform.getPerspective(),
0, mModelView, 0);
drawCube();
// Set mModelView for the floor, so we draw floor in the correct
location
Matrix.multiplyMM(mModelView, 0, mView, 0, mModelFloor, 0);
Matrix.multiplyMM(mModelViewProjection, 0, transform.getPerspective(),
0,
mModelView, 0);
...
}
28. 자석 버튼 누름 감지하기
MagnetSensor.OnCardboardTriggerListener 사용
29. private Vibrator mVibrator;
private CardboardOverlayView mOverlayView;
...
/**
* Increment the score, hide the object,
and give feedback if the user pulls the
magnet while
* looking at the object. Otherwise,
remind the user what to do.
*/
@Override
public void onCardboardTrigger() {
if (isLookingAtObject()) {
mScore++;
mOverlayView.show3DToast("Found
it! Look around for another one.nScore =
" + mScore);
...
} else {
mOverlayView.show3DToast("Look
around to find the object!");
}
// Always give user feedback
mVibrator.vibrate(50);
}
/**
* Check if user is looking at object by
calculating where the object is in eye-
space.
* @return
*/
private boolean isLookingAtObject() {
float[] initVec = {0, 0, 0, 1.0f};
float[] objPositionVec = new float[4];
// Convert object space to camera
space. Use the headView from onNewFrame.
Matrix.multiplyMM(mModelView, 0,
mHeadView, 0, mModelCube, 0);
Matrix.multiplyMV(objPositionVec, 0,
mModelView, 0, initVec, 0);
float pitch = (float)Math.atan2
(objPositionVec[1], -objPositionVec[2]);
float yaw = (float)Math.atan2
(objPositionVec[0], -objPositionVec[2]);
...
return (Math.abs(pitch) < PITCH_LIMIT)
&& (Math.abs(yaw) < YAW_LIMIT);
}