我在Android上繪製,座標在-1.0和1.0之間,但渲染器將這些座標映射到錯誤的位置。在景觀中,寬度不會延伸到角落,而在縱向上,寬度略微超出屏幕邊框。我試圖讓這個填充屏幕分辨率。這裏是我的代碼:Android OpenGL2.0渲染器縮放到正確的分辨率
package com.mycompany.brickbreaker;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.util.Log;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MyGLRenderer implements GLSurfaceView.Renderer {
private BrickBreaker game;
public MyGLRenderer(BrickBreaker activity) {
super();
game = activity;
}
//mMVPMatrix stands for "Model View Projection Matrix"
private final float[] mMVPMatrix = new float[16];
private final float[] mProjectionMatrix = new float[16];
private final float[] mViewMatrix = new float[16];
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
//Set the background frame color
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
@Override
public void onDrawFrame(GL10 unused){
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
//Set the camera position (View Matrix)
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0f);
//Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
//Draw the rectangle!
game.draw(mMVPMatrix);
}
@Override
public void onSurfaceChanged(GL10 unused, int width, int height){
GLES20.glViewport(0, 0, width, height);
Log.d("Touch", "Surface changed at height/width " + height + "/" + width);
float ratio = (float) width/(float) height;
//This projection matrix is applied to object coordinates in
//the onDrawFrame() method
Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
public static int loadShader(int type, String shaderCode){
//create the shader type
int shader = GLES20.glCreateShader(type);
//add the code to the shader, and compile it
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
}
如果要使用-1.0和1.0之間的座標進行繪製,爲什麼要使用視圖和投影矩陣?這就是你默認的範圍,沒有任何**視圖或投影矩陣。 –