在Android上使用opengl es 2.0进行简单翻译
我到处寻找,了解如何用openGl es 2.0翻译形状,但我找不到正确的方法。旋转和缩放效果良好 我在android openGl es 2.0教程中尝试过,但形状更离散,而不是经过翻译 以下代码与android代码示例几乎相同,只是要翻译的代码行不同:在Android上使用opengl es 2.0进行简单翻译,android,opengl-es-2.0,Android,Opengl Es 2.0,我到处寻找,了解如何用openGl es 2.0翻译形状,但我找不到正确的方法。旋转和缩放效果良好 我在android openGl es 2.0教程中尝试过,但形状更离散,而不是经过翻译 以下代码与android代码示例几乎相同,只是要翻译的代码行不同: public class MyGLRenderer implements GLSurfaceView.Renderer { private static final String TAG = "MyGLRenderer";
public class MyGLRenderer implements GLSurfaceView.Renderer {
private static final String TAG = "MyGLRenderer";
private Triangle mTriangle;
private Square mSquare;
private final float[] mMVPMatrix = new float[16];
private final float[] mProjMatrix = new float[16];
private final float[] mVMatrix = new float[16];
private final float[] mRotationMatrix = new float[16];
// Declare as volatile because we are updating it from another thread
public volatile float mAngle;
@Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
// Set the background frame color
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
mTriangle = new Triangle();
mSquare = new Square();
}
@Override
public void onDrawFrame(GL10 unused) {
// Draw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
// Set the camera position (View matrix)
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
// Draw square
mSquare.draw(mMVPMatrix);
// Create a rotation for the triangle
// long time = SystemClock.uptimeMillis() % 4000L;
// float angle = 0.090f * ((int) time);
Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);
// Combine the rotation matrix with the projection and camera view
Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);
// Draw triangle
mTriangle.draw(mMVPMatrix);
}
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
// Adjust the viewport based on geometry changes,
// such as screen rotation
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / height;
// this projection matrix is applied to object coordinates
// in the onDrawFrame() method
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
public static 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;
}
/**
* Utility method for debugging OpenGL calls. Provide the name of the call
* just after making it:
*
* <pre>
* mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
* MyGLRenderer.checkGlError("glGetUniformLocation");</pre>
*
* If the operation is not successful, the check throws an error.
*
* @param glOperation - Name of the OpenGL call to check.
*/
public static void checkGlError(String glOperation) {
int error;
while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
Log.e(TAG, glOperation + ": glError " + error);
throw new RuntimeException(glOperation + ": glError " + error);
}
}
}
我认为这与顶点着色器代码有关,但我无法理解它?由于透视摄影机的缘故,您可能会看到失真。尝试减少平移对象的距离。由于透视相机的原因,您可能会看到扭曲。试着缩短你平移物体的距离。我在这篇文章中找到了答案: 只需将vertexShaderCode字符串中的uMVPMatrix和VPposition反转为:
// Set the camera position (View matrix)
Matrix.setLookAtM(mViewMatrixS, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
//translate the Matrix
Matrix.translateM(mViewMatrixS, 0, 2f, 1f, 0);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrixS, 0, mProjectionMatrix, 0, mViewMatrixS, 0);
// Draw square
mSquare.draw(mMVPMatrix);
我在这篇帖子上找到了答案: 只需将vertexShaderCode字符串中的uMVPMatrix和VPposition反转为:
// Set the camera position (View matrix)
Matrix.setLookAtM(mViewMatrixS, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
//translate the Matrix
Matrix.translateM(mViewMatrixS, 0, 2f, 1f, 0);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrixS, 0, mProjectionMatrix, 0, mViewMatrixS, 0);
// Draw square
mSquare.draw(mMVPMatrix);
下面的链接包含答案。花了一天的时间找到它。在这里发布帮助他人,因为我多次看到这篇文章
安卓exmaple项目是错误的,最终刚刚更新。仅供参考。以下链接包含答案。花了一天的时间找到它。在这里发布帮助他人,因为我多次看到这篇文章
安卓exmaple项目是错误的,最终刚刚更新。仅供参考。我正在渲染器的onDrawFrame方法中转换矩阵,因此我的代码如下所示:
这就是正确翻译我的形状的工作。我不知道这是否是问题所在。我正在渲染器的onDrawFrame方法中转换矩阵,因此我的代码如下所示:
这就是正确翻译我的形状的工作。我不知道这是否是问题所在。如果我将平移距离从5减小到0.1,如果只是减小失真如果我将平移距离从5减小到0.1,如果只是减小失真
// Set the camera position (View matrix)
Matrix.setLookAtM(mViewMatrixS, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
//translate the Matrix
Matrix.translateM(mViewMatrixS, 0, 2f, 1f, 0);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrixS, 0, mProjectionMatrix, 0, mViewMatrixS, 0);
// Draw square
mSquare.draw(mMVPMatrix);