模型矩阵转换OpenGL ES 2.0

时间:2017-12-20 22:13:27

标签: android opengl-es opengl-es-2.0

我在渲染器中的onDrawFrame中编写了这段代码:

[...]
setIdentityM(modelMatrix, 0);
translateM(modelMatrix, 0, 40f, 0f, 25f);
rotateM(modelMatrix, 0, 90f, 1f, 0f, 0f);
scaleM(modelMatrix, 0, 6f, 6f, 6f);

multiplyMM(MVPmatrix, 0, viewProjectionMatrix, 0, modelMatrix, 0);
myHouse1coords = myHouse1.getWorldCoords(modelMatrix); //myHouse1coords is an array
[...]

我使用此代码来追踪我在" house"上应用的信息:

[...]   
private final static float[] COORDINATES = { //x, y, z, r, g, b
        0f, 0f, 0f, 1f, 0f, 0f, //bottom
        2f, 0f, 0f, 0f, 1f, 0f, //dx
        0f, 1f, 0f, 0f, 0f, 1f, //sx
        2f, 1f, 0f, 1f, 1f, 1f //top
};
    public float[] getWorldCoords(float[] matrix)
{
    int i = 0;
    float[] coordinates1 = new float[4];
    float[] coordinates2 = new float[4];
    float[] coordinates3 = new float[4];
    float[] coordinates4 = new float[4];

    do {
        coordinates1[i] = COORDINATES[i];
        i++;
    } while (i < 3);
    i = 0;
    coordinates1[3] = 0f;

    do {
        coordinates2[i] = COORDINATES[i + 6];
        i++;
    } while (i < 3);
    i = 0;
    coordinates2[3] = 0f;

    do {
        coordinates3[i] = COORDINATES[i + 12];
        i++;
    } while (i < 3);
    i = 0;
    coordinates3[3] = 0f;

    do {
        coordinates4[i] = COORDINATES[i + 18];
        i++;
    } while (i < 3);
    coordinates4[3] = 0f;

    float[] finalCoordinates1 = new float[4];
    float[] finalCoordinates2 = new float[4];
    float[] finalCoordinates3 = new float[4];
    float[] finalCoordinates4 = new float[4];

    multiplyMV(finalCoordinates1, 0, matrix, 0, coordinates1, 0);
    multiplyMV(finalCoordinates2, 0, matrix, 0, coordinates2, 0);
    multiplyMV(finalCoordinates3, 0, matrix, 0, coordinates3, 0);
    multiplyMV(finalCoordinates4, 0, matrix, 0, coordinates4, 0);


    float[] coordinates = new float[16];
    i = 0;
    int j = 0;

    do {
        coordinates[i] = finalCoordinates1[j];
        i++;
        j++;
    } while (j < 4);
    j = 0;

    do {
        coordinates[i] = finalCoordinates2[j];
        i++;
        j++;
    } while (j < 4);
    j = 0;

    do {
        coordinates[i] = finalCoordinates3[j];
        i++;
        j++;
    } while (j < 4);
    j = 0;

    do {
        coordinates[i] = finalCoordinates4[j];
        i++;
        j++;
    } while (j < 4);

    Log.d("DRAGTESTRENDERER", "coords[4]" + coordinates[4]);
    return coordinates;
}

[...]

问题是,当我在logcat中运行此代码时,我得到12.0作为结果,好像我只在我的房子上应用了比例转换,我不明白为什么,因为我之前翻译并旋转它

1 个答案:

答案 0 :(得分:0)

问题在于我将坐标向量的第4个元素(w分量)设置为0f而不是1f。