简体   繁体   English

如何在OpenGLES 2.0中转换和缩放对象

[英]How to translate and scale objects in OpenGLES 2.0

I can use the below code to scale and translate a square using OpenGLES. 我可以使用以下代码使用OpenGLES缩放和平移正方形。 But I'm not sure how to calculate the translation and scale factors. 但是我不确定如何计算平移和比例因子。 For example using the below picture of the OpenGL coordinate system and Matrix.translateM(mViewMatrix, 0, .5f, 0, 0); 例如,使用下面的OpenGL坐标系图片和Matrix.translateM(mViewMatrix, 0, .5f, 0, 0); I would expect the square to be drawn halfway to the right of the screen, but instead it's drawn halfway to the left from the center. 我希望正方形会在屏幕的右侧绘制一半,但它会在屏幕的左侧绘制一半。 However Matrix.translateM(mViewMatrix, 0, 0, .5f, 0); 但是Matrix.translateM(mViewMatrix, 0, 0, .5f, 0); does translate the square halfway up the screen from the center. 确实将正方形从屏幕中心移到屏幕的一半。

How would I translate and scale in order to programmatically draw N squares side by side horizontally filling the top of the screen? 如何以编程方式并排绘制N正方形并水平填充屏幕顶部,我将如何进行转换和缩放?

OpenGLES coordinate system

@Override
public void onDrawFrame(GL10 unused) {

    // Draw background color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    // Set the camera position (View matrix)
    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    // Translate by some amount
    // Matrix.translateM(mViewMatrix, 0, ?, ?, 0);

    // Scale by some amount
    // Matrix.scaleM(mViewMatrix, 0, ?, ?, 1);

    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);

    // Draw square
    mSquare.draw(mMVPMatrix);
}

I am not sure why you would need translate and scale to fill up a row of squares. 我不确定为什么您需要平移和缩放以填充一行正方形。 To get rows of squares programmatically in openGL ES I would just make a bunch of squares initialized where you want them. 为了在openGL ES中以编程方式获取正方形行,我只需要在需要的位置初始化一堆正方形即可。 An edited snippet from one of my projects went something like this: 我的一个项目中的一段经过编辑的片段如下所示:

public void onSurfaceCreated(GL10 unused, EGLConfig config){
   GLES20.glClearColor(bgr, bgg, bgb, bga);
   float z=0.0f;
   float y=0.0f;
   for(float i=(-worldwidth);i<worldwidth;i+=(cellwidth)){
      square=new Square(i,y,z);     
      cellvec.addElement(square);
   }
}
public void onDrawFrame(GL10 unused) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
    for(int i=0;i<cellvec.size();i++){
        cellvec.elementAt(i).draw(mMVPMatrix);
    }
}

I am not entirely sure if something like this is what your looking for but it it seems to get the result of a row of squares like you wanted. 我不能完全确定您是否正在寻找这样的东西,但是它似乎得到了您想要的一排正方形的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM