Fix rotation and other minor problems

This commit is contained in:
Simon Leistikow 2017-09-14 01:20:51 +02:00
parent e222a1812b
commit 395c82f341
2 changed files with 15 additions and 31 deletions

View File

@ -103,6 +103,7 @@ public class Renderer implements GLSurfaceView.Renderer {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GLUtils.texImage2D(GL_TEXTURE_2D, 0, bitmap, 0);
glBindTexture(GL_TEXTURE_2D, 0);
// Release bitmap for garbage collection.
bitmap = null;
@ -124,16 +125,14 @@ public class Renderer implements GLSurfaceView.Renderer {
glUniformMatrix4fv(mvpLocation, 1, false, mvpMatrix, 0);
glUniform1i(texLocation, 0);
glBindTexture(GL_TEXTURE_2D, textureID[0]);
glDrawElements(GL_TRIANGLES, sphere.getIndexBuffer().capacity(), GL_UNSIGNED_SHORT, sphere.getIndexBuffer());
glBindTexture(GL_TEXTURE_2D, 0);
glDisableVertexAttribArray(textureCoordinatesLocation);
glDisableVertexAttribArray(positionLocation);
glUseProgram(0);
int error = glGetError();
if(error != GL_NO_ERROR)
Log.e("Renderer", "Error: " + getEGLErrorString(error));
}
/**
@ -156,28 +155,15 @@ public class Renderer implements GLSurfaceView.Renderer {
* @param config surface configuration
*/
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
initialize();
//TODO: (re)move tmp code
Matrix.setIdentityM(modlMatrix, 0);
//Matrix.translateM(modlMatrix, 0, 0, 0, 4.0f);
Matrix.setLookAtM(viewMatrix, 0, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
}
/**
* Initialize OpenGL state and data.
*/
public void initialize() {
// Initialize sphere.
sphere = new Sphere(10.0f, 32, 32); // TODO: choose useful parameters.
// Set OpenGL state.
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glEnable(GL_TEXTURE_2D);
glFrontFace(GL_CW);
glActiveTexture(GL_TEXTURE0);
// Build shader program.
@ -185,15 +171,10 @@ public class Renderer implements GLSurfaceView.Renderer {
// Generate texture.
glGenTextures(1, textureID, 0);
}
/**
* Reset OpenGL state and delete data.
*/
public void deinitialize() {
sphere = null;
glDeleteTextures(1, textureID, 0);
glDeleteProgram(programID);
// Initialize matrices.
Matrix.setIdentityM(modlMatrix, 0);
Matrix.setLookAtM(viewMatrix, 0, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
}
/**

View File

@ -23,14 +23,14 @@ public class SphereSurfaceView extends GLSurfaceView implements SensorEventListe
// The actual rotation matrix determined by user input.
private final float rotationMatrix [] = new float[16];
// The following fields are used as cache.
private final float tmpMatrix[] = new float[9];
private final float tmpVector[] = new float[3];
// This matrix is used to compensate sensor coordinate system rotation.
private final float offsetMatrix [] = new float[16];
public SphereSurfaceView(Context context) {
super(context);
Matrix.setIdentityM(rotationMatrix, 0);
Matrix.setRotateM(offsetMatrix, 0, 90.0f, 1.0f, 0.0f, 0.0f);
SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ROTATION_VECTOR).get(0);
@ -81,7 +81,10 @@ public class SphereSurfaceView extends GLSurfaceView implements SensorEventListe
if(USE_TOUCH)
return;
SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
synchronized (rotationMatrix) {
SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
Matrix.multiplyMM(rotationMatrix, 0, rotationMatrix, 0, offsetMatrix, 0);
}
}
/**