Code:
import android.opengl.Matrix;
public class Camera {
vec3 pos;
vec3 up;
vec3 right;
vec3 dir;
int width, height;
float near;
float far;
float[] viewMatrix;
float[] projMatrix;
float[] combMatrix;
public Camera(int width, int height){
float ratio = (float)width/height;
float left = -ratio;
float right = ratio;
float bottom = -1.0f;
float top = 1.0f;
near = 1.0f;
far = 100.0f;
Matrix.frustumM(projMatrix, 0, left, right, bottom, top, bottom, top);
float eyeX = 0.0f;
float eyeY = 0.0f;
float eyeZ = 0.0f;
float centerX = 0.0f;
float centerY = 0.0f;
float centerZ = -1.0f;
float upX = 0.0f;
float upY = 1.0f;
float upZ = 0.0f;
Matrix.setLookAtM(viewMatrix, 0, eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
}
public void rotate(float angle, vec3 axis){
Matrix.rotateM(viewMatrix, 0, angle, axis.x, axis.y, axis.z);
}
public void rotate(float angle, float x, float y, float z){
Matrix.rotateM(viewMatrix, 0, angle, x, y, z);
}
public void translate(float dist, vec3 axis){
Matrix.translateM(viewMatrix, 0, axis.x, axis.y, axis.z);
}
public void translate(float dist, float x, float y, float z){
Matrix.translateM(viewMatrix, 0, x, y, z);
}
}
any advice on my class so far, how would i use this class as a part of my rendering loop?
Code:
public class MyRenderer implements GLSurfaceView.Renderer {
Context context;
TextureLoader tLoader;
int buf;
int number;
public MyRenderer(Context c){
this.context = c;
tLoader = new TextureLoader(this.context);
vertsBuffer = ByteBuffer.allocateDirect(verts.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
vertsBuffer.put(verts).position(0);
textBuffer = ByteBuffer.allocateDirect(text.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
textBuffer.put(text).position(0);
indexBuffer = ByteBuffer.allocateDirect(index.length*2).order(ByteOrder.nativeOrder()).asShortBuffer();
indexBuffer.put(index).position(0);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
vShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
GLES20.glShaderSource(vShader, vCode);
GLES20.glCompileShader(vShader);
fShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
GLES20.glShaderSource(fShader, fCode);
GLES20.glCompileShader(fShader);
sProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(sProgram, vShader);
GLES20.glAttachShader(sProgram, fShader);
GLES20.glLinkProgram(sProgram);
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
buf = tLoader.loadTexture(R.raw.bkg);
float eyeX = 0.0f;
float eyeY = 0.0f;
float eyeZ = 0.0f;
float centerX = 0.0f;
float centerY = 0.0f;
float centerZ = -1.0f;
float upX = 0.0f;
float upY = 1.0f;
float upZ = 0.0f;
Matrix.setLookAtM(viewMatrix, 0, eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
mWidth = width;
mHeight = height;
GLES20.glViewport(0, 0, mWidth, mHeight);
float ratio = (float)mWidth/mHeight;
float left = -ratio;
float right = ratio;
float bottom = -1.0f;
float top = 1.0f;
float near = 1.0f;
float far = 100.0f;
Matrix.orthoM(projMatrix, 0, left, right, bottom, top, near, far);
}
public void onDrawFrame(GL10 gl) {
GLES20.glViewport(0, 0, mWidth, mHeight);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glUseProgram(sProgram);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, buf);
GLES20.glUniform1i(GLES20.glGetUniformLocation(sProgram, "texture"), 0);
Matrix.setIdentityM(modelMatrix, 0);
Matrix.translateM(modelMatrix, 0, 0.0f, 0.0f, -7.0f);
GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(sProgram, "aPos"), 3, GLES20.GL_FLOAT, false, 0, vertsBuffer);
GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(sProgram, "aPos"));
GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(sProgram, "aTexPos"), 2, GLES20.GL_FLOAT, false, 0, textBuffer);
GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(sProgram, "aTexPos"));
Matrix.multiplyMM(modViewProjMatrix, 0, viewMatrix, 0, modelMatrix, 0);
Matrix.multiplyMM(modViewProjMatrix, 0, projMatrix, 0, modViewProjMatrix, 0);
GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(sProgram, "MVPMat"), 1, false, modViewProjMatrix, 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, indexBuffer);
}
private float[] text = {0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f };
private float[] verts = {-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f};
private final short[] index = {0, 1, 2, 2, 3, 0};
private int vShader, fShader, sProgram, mWidth, mHeight;
private FloatBuffer vertsBuffer, textBuffer;;
private ShortBuffer indexBuffer;
private float[] modelMatrix = new float[16];
private float[] viewMatrix = new float[16];
private float[] projMatrix = new float[16];
private float[] modViewProjMatrix = new float[16];
private final String vCode =
"uniform mat4 MVPMat; \n"
+"attribute vec2 aTexPos; \n"
+"attribute vec3 aPos; \n"
+"varying vec2 vTexPos; \n"
+"void main(){ \n"
+" vTexPos = aTexPos; \n"
+" gl_Position = MVPMat * vec4(aPos, 1.0); \n"
+"} \n";
private final String fCode =
"precision mediump float; \n"
+"uniform sampler2D texture; \n"
+"varying vec2 vTexPos; \n"
+"void main(){ \n" \n"
+"gl_FragColor = texture2D(texture, vTexPos); \n"
+"} \n";
}