Android OpenGL ES 2.0:使用Matrix.traslateM无法获得我想要的结果

前端之家收集整理的这篇文章主要介绍了Android OpenGL ES 2.0:使用Matrix.traslateM无法获得我想要的结果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个使用OpenGL显示文本的应用程序.当我尝试使用Matrix.translateM将指定对象移动到特定位置时,我得到了意外的行为.所有其他转换矩阵都像我期望的那样工作.

这是我调用Matrix.translateM(model_matrix,-1.0f,0)时得到的结果:

这是没有翻译的图片

这是我的渲染器代码.

  1. @Override
  2. public void onSurfaceCreated(GL10 unused,EGLConfig config) {
  3. GLES20.glClearColor(0.5f,0.5f,1.0f);
  4. GLES20.glDisable(GLES20.GL_DEPTH_TEST);
  5. GLES20.glEnable(GLES20.GL_BLEND);
  6. GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA,GLES20.GL_ONE_MINUS_SRC_ALPHA);
  7.  
  8. // create programs
  9. simple_texture = new SimpleTexture();
  10.  
  11. // create shapes
  12. square = new Square();
  13.  
  14. // create debug text handlers
  15. text_fps = new Text(this.font,this.text_scale);
  16. text_fps.setSize(50);
  17. text_fps.setText("Test\nLonger line");
  18.  
  19. // set camera position
  20. final float eyeX = 0.0f;
  21. final float eyeY = 0.0f;
  22. final float eyeZ = -3.0f;
  23.  
  24. final float lookX = 0.0f;
  25. final float lookY = 0.0f;
  26. final float lookZ = 0.0f;
  27.  
  28. final float upX = 0.0f;
  29. final float upY = 1.0f;
  30. final float upZ = 0.0f;
  31.  
  32. Matrix.setLookAtM(view_matrix,eyeX,eyeY,eyeZ,lookX,lookY,lookZ,upX,upY,upZ);
  33. }
  34.  
  35. @Override
  36. public void onSurfaceChanged(GL10 unused,int width,int height) {
  37. GLES20.glViewport(0,width,height);
  38.  
  39. // set projection matrix
  40. float ratio = (float) width / height;
  41. Matrix.orthoM(projection_matrix,-ratio,ratio,-1,1,3,7);
  42.  
  43. setScreenRatio(ratio);
  44. setScreenHeight(height);
  45. }
  46.  
  47. @Override
  48. public void onDrawFrame(GL10 unused) {
  49. GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
  50.  
  51. // calculate projection and view transformation
  52. Matrix.multiplyMM(vp_matrix,projection_matrix,view_matrix,0);
  53.  
  54. // if we are connected to debugger draw statistics
  55. drawStatistics(vp_matrix);
  56. }

这是我的文字绘图代码.

  1. // use predefined program
  2. GLES20.glUseProgram(program);
  3.  
  4. // get attribute positions
  5. attr_matrix = GLES20.glGetUniformLocation(program,"u_Matrix");
  6. attr_position = GLES20.glGetAttribLocation(program,"a_Position");
  7. attr_texture_position = GLES20.glGetAttribLocation(program,"a_TexturePosition");
  8. attr_texture = GLES20.glGetUniformLocation(program,"u_Texture");
  9.  
  10. // set program parameters
  11. GLES20.glEnableVertexAttribArray(attr_position);
  12. GLES20.glVertexAttribPointer(attr_position,2,GLES20.GL_FLOAT,false,2 * 4,square_buffer);
  13.  
  14. // set texture parameters
  15. GLES20.glEnableVertexAttribArray(attr_texture_position);
  16. GLES20.glVertexAttribPointer(attr_texture_position,texture_buffer);
  17.  
  18. // set matrix
  19. float[] result = new float[16];
  20. float ratio = (float) texture_height / texture_width;
  21. float screen_scale = (float) texture_height / PageRenderer.getScreenHeight();
  22.  
  23. Matrix.setIdentityM(model_matrix,0);
  24. Matrix.translateM(model_matrix,0);
  25. Matrix.scaleM(model_matrix,screen_scale,screen_scale * ratio,screen_scale);
  26.  
  27. Matrix.setIdentityM(result,0);
  28. Matrix.multiplyMM(result,matrix,model_matrix,0);
  29. GLES20.glUniformMatrix4fv(attr_matrix,result,0);
  30.  
  31. // assign texture
  32. GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
  33. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,index);
  34. GLES20.glUniform1i(attr_texture,0);
  35.  
  36. // perform drawing
  37. GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP,4);
  38.  
  39. // disable vertex array
  40. GLES20.glDisableVertexAttribArray(attr_position);
  41. GLES20.glDisableVertexAttribArray(attr_texture_position);

着色器:

  1. int vertex_shader = PageRenderer.loadShader(
  2. GLES20.GL_VERTEX_SHADER,"uniform mat4 u_Matrix;" +
  3. "attribute vec4 a_Position;" +
  4. "attribute vec2 a_TexturePosition;" +
  5. "varying vec2 v_TexturePosition;" +
  6. "void main() {" +
  7. " gl_Position = a_Position * u_Matrix;" +
  8. " v_TexturePosition = a_TexturePosition;" +
  9. "}"
  10. );
  11. int fragment_shader = PageRenderer.loadShader(
  12. GLES20.GL_FRAGMENT_SHADER,"precision mediump float;" +
  13. "varying vec2 v_TexturePosition;" +
  14. "uniform sampler2D u_Texture;" +
  15. "void main() {" +
  16. " gl_FragColor = texture2D(u_Texture,v_TexturePosition);" +
  17. "}"
  18. );

按预期缩放和旋转工作,但我无法弄清楚为什么翻译不会.
请注意,我最近才使用OpenGL.

我期待translateM将文本移动到左侧,而不是“旋转”它.

解决方法

计算gl_Position时的变量顺序很重要!世界矩阵首先是顶点坐标,然后是顶点坐标.在提问时我确实知道这一点,但由于着色器代码来自Android OpenGL教程,所以没有注意.是有趣的人.教程不正确!

更正着色器代码

  1. int vertex_shader = PageRenderer.loadShader(
  2. GLES20.GL_VERTEX_SHADER,"uniform mat4 u_Matrix;" +
  3. "attribute vec4 a_Position;" +
  4. "attribute vec2 a_TexturePosition;" +
  5. "varying vec2 v_TexturePosition;" +
  6. "void main() {" +
  7. " gl_Position = u_Matrix * a_Position;" +
  8. " v_TexturePosition = a_TexturePosition;" +
  9. "}"
  10. );
  11. int fragment_shader = PageRenderer.loadShader(
  12. GLES20.GL_FRAGMENT_SHADER,v_TexturePosition);" +
  13. "}"
  14. );

猜你在找的Android相关文章