C 将“转换为”;“正常”;矩形到一组向量?

C 将“转换为”;“正常”;矩形到一组向量?,c,opengl-es,geometry,C,Opengl Es,Geometry,如何将“普通”矩形转换为一组OpenGL ES顶点。我不擅长几何,所以我不知道顶点是如何工作的,我希望能够操纵矩形,而不必通过尝试和错误计算出顶点的值 我基本上需要转换这个结构: typedef struct __nrect { float width; float height; float depth; /* center */ float x; float y; float z; } simple3dRect; 对这样的事情:

如何将“普通”矩形转换为一组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 },
};

有没有简单的方法可以做到这一点?

假设生成的立方体是轴对齐的,并且宽度对应于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等值来简化/优化,为清晰起见,此处包含了直接计算值。

假设生成的立方体是轴对齐的,并且宽度对应于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等值来简化/优化,为了清晰起见,这里包括了直接计算。

normalRect结构没有足够的信息来实现这一点-它是否以(0,0,0)为中心?它的范围是(0,0,0)-(宽度、高度、深度)吗?我觉得你的normalRect看起来像一个体积,而不是一个矩形。想想一个立方体有多少个面,有多少个顶点定义了一个面,现在检查如何从normalRect中给出的信息到达每个面(假设这是你所有的,因为它不允许太多)。一个立方体有8个顶点,每个顶点有3个组件。我不知道为什么你的数组最终会有三倍于需要的条目(事实上,我知道,每个顶点用在三个面上。但它仍然不应该被组织为6x12)。如果制作一个3x6的顶点矩阵,则可以使用矩阵乘法来旋转、缩放和平移它。然后使用
gldrawerelements
进行渲染。我还建议您将其重命名为不使用“normal”这个词的名称,因为它不包含法向量,这很容易混淆。normalRect结构没有足够的信息来执行此操作-它是否以(0,0,0)为中心?它的范围是(0,0,0)-(宽度、高度、深度)吗?我觉得你的normalRect看起来像一个体积,而不是一个矩形。想想一个立方体有多少个面,有多少个顶点定义了一个面,现在检查如何从normalRect中给出的信息到达每个面(假设这是你所有的,因为它不允许太多)。一个立方体有8个顶点,每个顶点有3个组件。我不知道为什么你的数组最终会有三倍于需要的条目(事实上,我知道,每个顶点用在三个面上。但它仍然不应该被组织为6x12)。如果制作一个3x6的顶点矩阵,则可以使用矩阵乘法来旋转、缩放和平移它。然后使用
gldrawerelements
进行渲染。我还建议您将其重命名为不使用“normal”这个词的名称,因为它不包含法向量,这会让人感到困惑。这会不会产生一个宽度、高度和深度都是两倍的立方体?我认为应该是
width/2
等等。我认为现在任何编译器都会将冗余的{width,height,depth}/2优化为预计算。这太容易在AST上实现了。这难道不会产生一个宽度、高度和深度都是原来的两倍的立方体吗?我认为应该是
width/2
等等。我认为现在任何编译器都会将冗余的{width,height,depth}/2优化为预计算。这太容易在AST上实现了。