我開始使用Android中的OpenGL ES,並且遇到了一個我不太明白的情況。 OpenGL的原點應該是屏幕的左下角,對嗎?因爲對我來說,它似乎在屏幕中間。到目前爲止,我只是通過翻譯和旋轉到我想要的座標系來解決這個問題,但我想首先理解爲什麼會發生這種情況。下面,這是一個最小的例子,它將精靈放置在屏幕的死點。我沒有在此代碼中執行任何翻譯或旋轉。OpenGL中原點的位置
package com.lyrismobilestudio.opengltest;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.ByteOrder;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.egl.EGLConfig;
public class MainActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GLSurfaceView view = new GLSurfaceView(this);
Renderer renderer = new MyRenderer(this);
view.setRenderer(renderer);
setContentView(view);
}
}
class MyRenderer implements Renderer {
public Sprite strike;
public Context context;
public MyRenderer(Context context) {
this.context = context;
strike = new Sprite();
}
@Override public void onDrawFrame(GL10 gl) {
gl.glClearColor(.5f,.5f,1f,0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
strike.draw(gl);
try { Thread.sleep(100); } catch (Exception e) {}
}
@Override public void onSurfaceChanged(GL10 gl, int width, int height){
}
@Override public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
strike.loadTexture(gl, R.drawable.strike, context);
}
}
class Sprite {
FloatBuffer vertexBuffer;
FloatBuffer textureBuffer;
ByteBuffer indexBuffer;
private int[] textures = new int[1];
public Sprite() {
float[] vertices = {
-.1f, .1f,
.1f, .1f,
.1f, -.1f,
-.1f, -.1f,
};
float[] textureVertices = new float[]{
0f, 0f,
1f, 0f,
1f, 1f,
0f, 1f
};
byte indices[] = {
0, 1, 2,
0, 2, 3
};
ByteBuffer temp = ByteBuffer.allocateDirect(vertices.length*4);
temp.order(ByteOrder.nativeOrder());
vertexBuffer = temp.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
temp = ByteBuffer.allocateDirect(textureVertices.length*4);
temp.order(ByteOrder.nativeOrder());
textureBuffer = temp.asFloatBuffer();
textureBuffer.put(textureVertices);
textureBuffer.position(0);
indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);
}
public void draw(GL10 gl){
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_BYTE, indexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
public void loadTexture(GL10 gl, int texture, Context context){
InputStream imageStream = context.getResources().openRawResource(texture);
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(imageStream);
} catch (Exception e) {
} finally {
try {
imageStream.close();
imageStream=null;
} catch (Exception e) {
}
}
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D,textures[0]);
gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
}
視口座標來自x和y的[-1,1] –