Android OpenGL ES 2.0以横向模式剪切屏幕
我开始学习OpenGL,正在使用以下网站: 但我似乎在这一部分遇到了问题(在肖像模式下效果很好):Android OpenGL ES 2.0以横向模式剪切屏幕,android,opengl-es-2.0,projection-matrix,Android,Opengl Es 2.0,Projection Matrix,我开始学习OpenGL,正在使用以下网站: 但我似乎在这一部分遇到了问题(在肖像模式下效果很好): private float[]mViewMatrix=new float[16]; /**存储投影矩阵。这用于将场景投影到二维视口上*/ 私有浮动[]mProjectionMatrix=新浮动[16]; /**为最终组合矩阵分配存储。这将被传递到着色器程序中*/ 私有浮点[]mMVPMatrix=新浮点[16]; /**这将用于传递变换矩阵*/ 私有int mMVPMatrixHandle; /*
private float[]mViewMatrix=new float[16];
/**存储投影矩阵。这用于将场景投影到二维视口上*/
私有浮动[]mProjectionMatrix=新浮动[16];
/**为最终组合矩阵分配存储。这将被传递到着色器程序中*/
私有浮点[]mMVPMatrix=新浮点[16];
/**这将用于传递变换矩阵*/
私有int mMVPMatrixHandle;
/**这将用于传递模型位置信息*/
私有int-mPositionHandle;
/**这将用于传递模型颜色信息*/
米科洛汉德尔私人酒店;
/**每个浮点数有多少字节*/
私有最终int mBytesPerFloat=4;
/**每个顶点有多少个元素*/
私有最终整数mStrideBytes=7*mBytesPerFloat;
/**元素中位置数据的大小*/
私有最终int mPositionDataSize=3;
/**颜色数据的偏移量*/
私人最终int MCOLOROFSET=3;
/**元素中颜色数据的大小*/
私有最终整数mColorDataSize=4;
表面上的公共空隙已更改(GL10 gl,整型宽度,整型高度)
{
GLES20.glViewport(0,0,宽度,高度);
//创建新的透视投影矩阵。高度将保持不变
//而宽度将根据纵横比而变化。
最终浮动比率=(浮动)宽度/高度;
最终浮动左=-比率;
最终浮动权=比率;
最终浮底=-1.0f;
最终浮顶=1.0f;
接近的最终浮动=1.0f;
最终浮动far=10.0f;
System.out.println(“高度:+高度”);
System.out.println(“宽度:”+宽度);
平截头体(mProjectionMatrix,0,左,右,下,上,近,远);
}
公共框架(GL10 gl)
{
最终浮动目镜=0.0f;
最终浮球Y=0.0f;
最终浮子eyeZ=1.5f;
final float lookY=0.0f;//用户看到的Y方向
final float lookZ=-5.0f;//Z用户看到的方向
//设置我们的向量。如果我们拿着相机,我们的头会指向这里。
最终浮子upX=0.0f;
最终浮子upY=1.0f;
最终浮子upZ=0.0f;
GLES20.glClearColor(红色、绿色、蓝色、clearcoloralpha);
GLES20.glClear(GLES20.GL_深度_缓冲_位| GLES20.GL_颜色_缓冲_位);
矩阵setLookAtM(mViewMatrix,0,eyeX,eyeY,eyeZ,xax,lookY,lookZ,upX,upY,upZ);
//画一个正对着的三角形。
对于(int i=0;i
但是,当我尝试在横向模式下移动三角形(向左或向右)时,当将三角形移动到某一侧时,三角形会被“截断”(不显示整个三角形)。似乎他们被当作是在屏幕之外,而实际上并非如此。如前所述,它似乎在纵向模式下工作良好
在横向模式下,高度为752,宽度为1280(Galaxy Tab 2)
这是否与此处设置的项目矩阵有关
谢谢你的帮助 你是对的,问题是你在移动你的相机:D xax应保持为0.0f
Matrix.setLookAtM(mViewMatrix,0,eyeX,eyeY,eyeZ,xax,lookY,lookZ,upX,upY,upZ)代码>752x1280是否适用于横向模式?我想应该是相反的。@harism你完全正确,谢谢你指出这一点。帖子已更新:)可能需要查看更多代码。这看起来没问题…@jtwigg谢谢你的回复。添加了更多的代码。onSurfaceCreated()与教程中的相同,占用了大量空间(这就是为什么我不将其粘贴在此处)。@jtwigg是的,这很好,因为这样相机将始终聚焦在同一个点上。我想让三角形向左/向右移动。但我当时的做法是移动摄像机的位置。这就是为什么它似乎切割了三角形:)所以我应该改变三角形本身的坐标,而不是相机的位置:)
private float[] mViewMatrix = new float[16];
/** Store the projection matrix. This is used to project the scene onto a 2D viewport. */
private float[] mProjectionMatrix = new float[16];
/** Allocate storage for the final combined matrix. This will be passed into the shader program. */
private float[] mMVPMatrix = new float[16];
/** This will be used to pass in the transformation matrix. */
private int mMVPMatrixHandle;
/** This will be used to pass in model position information. */
private int mPositionHandle;
/** This will be used to pass in model color information. */
private int mColorHandle;
/** How many bytes per float. */
private final int mBytesPerFloat = 4;
/** How many elements per vertex. */
private final int mStrideBytes = 7 * mBytesPerFloat;
/** Size of the position data in elements. */
private final int mPositionDataSize = 3;
/** Offset of the color data. */
private final int mColorOffset = 3;
/** Size of the color data in elements. */
private final int mColorDataSize = 4;
public void onSurfaceChanged(GL10 gl, int width, int height)
{
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 = 1.0f;
final float far = 10.0f;
System.out.println("Height: " + height);
System.out.println("Width: " + width);
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
}
public void onDrawFrame(GL10 gl)
{
final float eyeX = 0.0f;
final float eyeY = 0.0f;
final float eyeZ = 1.5f;
final float lookY = 0.0f; //Y direction of what user see
final float lookZ = -5.0f; //Z direction of what user see
// Set our up vector. This is where our head would be pointing were we holding the camera.
final float upX = 0.0f;
final float upY = 1.0f;
final float upZ = 0.0f;
GLES20.glClearColor(red, green, blue, clearcoloralpha);
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, xax, lookY, lookZ, upX, upY, upZ);
// Draw the triangle facing straight on.
for(int i = 0; i < Triangles.size(); i++)
{
Matrix.setIdentityM(Triangles.get(i).getModelMatrix(), 0);
if(Triangles.get(i).Rotate())
{
Triangles.get(i).rotation = (360.0f / 10000.0f) * ((int) Triangles.get(i).last);
Triangles.get(i).last+=20;
//Rotates the matrix by rotation degrees
Matrix.rotateM(Triangles.get(i).getModelMatrix(), 0, Triangles.get(i).rotation, 0.0f, 0.0f, 1.0f);
}
else
Matrix.rotateM(Triangles.get(i).getModelMatrix(), 0, Triangles.get(i).rotation, 0.0f, 0.0f, 1.0f);
drawTriangle(Triangles.get(i).getFloatBuffer(),Triangles.get(i));
}
}
private void drawTriangle(final FloatBuffer aTriangleBuffer, Triangle tri)
{
aTriangleBuffer.position(0);
GLES20.glVertexAttribPointer(mPositionHandle, tri.DataSize, GLES20.GL_FLOAT, false, mStrideBytes, aTriangleBuffer);
GLES20.glEnableVertexAttribArray(mPositionHandle);
aTriangleBuffer.position(3);
GLES20.glVertexAttribPointer(mColorHandle, tri.ColorDataSize, GLES20.GL_FLOAT, false, mStrideBytes, aTriangleBuffer);
GLES20.glEnableVertexAttribArray(mColorHandle);
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, tri.getModelMatrix(), 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}