将“普通”矩形转换为一组GLes向量?

时间:2011-06-28 08:45:27

标签: c opengl-es geometry

如何将“普通”矩形转换为一组 OpenGL ES 顶点。我不擅长几何体,所以我不知道顶点是如何工作的,我希望能够操作矩形而不必通过反复试验来计算顶点的值。

我基本上需要转换这个结构:

typedef struct __nrect {
    float width;
    float height;
    float depth;

    /* center */
    float x;
    float y;
    float z;
} simple3dRect;

对于这样的事情:

    const GLfloat cubeVertices[6][12] = {
    { 1,-1, 1, -1,-1, 1,  1, 1, 1, -1, 1, 1 },
    { 1, 1, 1,  1,-1, 1,  1, 1,-1,  1,-1,-1 },
    {-1, 1,-1, -1,-1,-1, -1, 1, 1, -1,-1, 1 },
    { 1, 1, 1, -1, 1, 1,  1, 1,-1, -1, 1,-1 },
    { 1,-1,-1, -1,-1,-1,  1, 1,-1, -1, 1,-1 },
    { 1,-1, 1, -1,-1, 1,  1,-1,-1, -1,-1,-1 },
};

有一种简单的方法吗?

1 个答案:

答案 0 :(得分:1)

假设生成的立方体是轴对齐的,宽度对应于x轴,高度指向y轴,深度指向z轴:

const GLfloat cubeVertices[6][12] = {
    { x + width/2, y - height/2, z + depth/2,  x - width/2, y - height/2, z + depth/2,  x + width/2, y + height/2, z + depth/2,  x - width/2, y + height/2, z + depth/2 },
    { x + width/2, y + height/2, z + depth/2,  x + width/2, y - height/2, z + depth/2,  x + width/2, y + height/2, z - depth/2,  x + width/2, y - height/2, z - depth/2 },
    { x - width/2, y + height/2, z - depth/2,  x + width/2, y - height/2, z - depth/2,  x - width/2, y + height/2, z + depth/2,  x - width/2, y - height/2, z + depth/2 },
    { x + width/2, y + height/2, z + depth/2,  x - width/2, y + height/2, z + depth/2,  x + width/2, y + height/2, z - depth/2,  x - width/2, y + height/2, z - depth/2 },
    { x + width/2, y - height/2, z - depth/2,  x - width/2, y - height/2, z - depth/2,  x + width/2, y + height/2, z - depth/2,  x - width/2, y + height/2, z - depth/2 },
    { x + width/2, y - height/2, z + depth/2,  x - width/2, y - height/2, z + depth/2,  x + width/2, y - height/2, z - depth/2,  x - width/2, y - height/2, z - depth/2 },
};

显然,这可以通过预先计算宽度/ 2等值来简化/优化,为了清楚起见,这里包含了简写。