OpenGL未将坐标正确映射到android屏幕
Posted
技术标签:
【中文标题】OpenGL未将坐标正确映射到android屏幕【英文标题】:OpenGL not mapping coordinates correctly to android screen 【发布时间】:2012-04-25 23:17:50 【问题描述】:我在将 OpenGL 坐标正确映射到我的 android 屏幕时遇到问题。由于某种原因,x 坐标不包含在屏幕中。当我尝试在屏幕中间绘制一个半径为 x 屏幕一半的圆时,这是可见的。圆圈应该足够大以适合屏幕的 x 边框,但它却超出了屏幕边框,实际上是在触摸屏幕的 y 边框。另外,如果我告诉圆在 0,0(左上角)处绘制,则在将其转换为 OpenGL 坐标后,中心实际上将绘制在 x 边界之外。
希望这是有道理的。
这是我的 DrawScreen 类:
public class DrawScreen implements GLSurfaceView.Renderer
Ball ball;
public float mAngle;
private int mProgram;
private int maPositionHandle;
private int muMVPMatrixHandle;
private float[] mMVPMatrix = new float[16];
private float[] mVMatrix = new float[16];
private float[] mProjMatrix = new float[16];
private final String vertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
"uniform mat4 uMVPMatrix; \n" +
"attribute vec4 vPosition; \n" +
"void main() \n" +
// the matrix must be included as a modifier of gl_Position
" gl_Position = uMVPMatrix * vPosition; \n" +
" \n";
private final String fragmentShaderCode =
"precision mediump float; \n" +
"void main() \n" +
" gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n" +
" \n";
public void onSurfaceCreated(GL10 unused, EGLConfig config)
ball = new Ball();
// Set the background frame color
GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
ball.initShapes(240, 360, 240);
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
GLES20.glLinkProgram(mProgram); // creates OpenGL program executables
// get handle to the vertex shader's vPosition member
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
public void onDrawFrame(GL10 unused)
// Redraw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
// Add program to OpenGL environment
GLES20.glUseProgram(mProgram);
// Prepare the circle data
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 12, ball.ballVB);
GLES20.glEnableVertexAttribArray(maPositionHandle);
// Apply a ModelView Projection transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
// Draw the circle
GLES20.glDrawArrays(GLES20.GL_LINE_LOOP, 0, (int) ball.getNumSeg());
public void onSurfaceChanged(GL10 unused, int width, int height)
// Set the OpenGL viewport to the same size as the surface.
GLES20.glViewport(0, 0, width, height);
// Create a new perspective projection matrix. The height will stay the same
// while the width will vary as per aspect ratio.
final float ratio = (float) width / height;
final float left = ratio;
final float right = -ratio;
final float bottom = 1.0f;
final float top = -1.0f;
final float near = 3.0f;
final float far = 7.0f;
//Matrix.frustumM(mProjMatrix, 0, left, right, bottom, top, near, far);
Matrix.orthoM(mProjMatrix, 0, left, right, bottom, top, near, far);
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
private int loadShader(int type, String shaderCode)
// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
int shader = GLES20.glCreateShader(type);
// add the source code to the shader and compile it
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
这里是我将屏幕坐标转换为 OpenGL 坐标的地方:
float numSegments = 360;
public void initShapes(float tx, float ty, float tr)
cx = (tx / (480 / 2 )) - 1.f;
cy = (ty / (720 / 2 )) - 1.f;
r = (tr / (480 / 2 )) ;
System.out.println(r);
System.out.println(cx);
System.out.println(cy);
float ballCoords[] = new float[(int) (numSegments * 3)];
double theta = (2 * 3.1415926 / numSegments);
float c = (float) Math.cos(theta);//precalculate the sine and cosine
float s = (float) Math.sin(theta);
float t;
float x = r;//we start at angle = 0
float y = 0;
for(int i = 0; i < (numSegments * 3); i = i + 3 )
ballCoords[i] = (x + cx);
ballCoords[i + 1] = (y + cy);
ballCoords[i + 2] = (0);
//apply the rotation matrix
t = x;
x = c * x - s * y;
y = s * t + c * y;
// initialize vertex Buffer for triangle
ByteBuffer vbb = ByteBuffer.allocateDirect(
// (# of coordinate values * 4 bytes per float)
ballCoords.length * 4);
vbb.order(ByteOrder.nativeOrder());// use the device hardware's native byte order
ballVB = vbb.asFloatBuffer(); // create a floating point buffer from the ByteBuffer
ballVB.put(ballCoords); // add the coordinates to the FloatBuffer
ballVB.position(0); // set the buffer to read the first coordinate
【问题讨论】:
【参考方案1】:好的,所以我解决了它...虽然它看起来不漂亮。
我基本上把 onSurfaceChanged 中的所有数字都翻到了他们的头上,它看起来就是我现在想要的样子。
这是我的新代码:
public void onSurfaceChanged(GL10 unused, int width, int height)
// Set the OpenGL viewport to the same size as the surface.
GLES20.glViewport(0, 0, width, height);
// Create a new perspective projection matrix. The height will stay the same
// while the width will vary as per aspect ratio.
final float ratio = (float) height / width;
final float left = 1;
final float right = -1;
final float bottom = ratio;
final float top = -ratio;
final float near = 3.0f;
final float far = 7.0f;
//Matrix.frustumM(mProjMatrix, 0, left, right, bottom, top, near, far);
Matrix.orthoM(mProjMatrix, 0, left, right, bottom, top, near, far);
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
【讨论】:
哇,说得太早了~现在我有同样的错误,但相反。 X 工作正常,但如果我将 y 设置为 0,则不会一直到顶部。以上是关于OpenGL未将坐标正确映射到android屏幕的主要内容,如果未能解决你的问题,请参考以下文章