识别3d立方体的表面

时间:2010-07-21 09:32:18

标签: iphone opengl-es colors drawing ipad

我正在开发一个只绘制三维立方体的iPad应用程序。用户可以通过手指动作旋转它,并可以通过捏合来放大/缩小。

现在我同时为整个立方体(每个表面)着色。现在,如果我希望用户分别为立方体的每个表面着色,意味着用户将点击一个表面,它将仅为该表面着色,但我不知道如何识别用户已点击的那个表面..

我正在使用简单的openGL基础绘制整个多维数据集,如下所示。

    static const GLfloat cubeVertices[] = {

        -1.0, -1.0, 1.0,
        1.0, -1.0, 1.0,
        1.0, 1.0, 1.0,
        -1.0, 1.0, 1.0,

        -1.0, -1.0, -1.0,
        1.0, -1.0, -1.0,
        1.0, 1.0, -1.0,
        -1.0, 1.0, -1.0,
    };


    static const GLushort cubeIndicesFaceFront[] = {
        0, 1, 2, 3, 0
    };

    static const GLushort cubeIndicesFaceBack[] = {
        4, 5, 6, 7, 4
    };

    static const GLushort cubeIndicesFaceLeft[] = {
        0, 4, 7, 3, 0
    };

    static const GLushort cubeIndicesFaceRight[] = {
        1, 5, 6, 2, 1
    };

    static const GLushort cubeIndicesFaceTop[] = {
        3, 2, 6, 7, 3
    };

    static const GLushort cubeIndicesFaceBottom[] = {
        0, 1, 5, 4, 0
    };

static const GLubyte cubeColors[] = {
    0, 255, 255, 255,
    0, 255, 255, 255,
    0, 255, 255, 255,
    0, 255, 255, 255,
    0, 255, 255, 255,
};      


    glVertexPointer(3, GL_FLOAT, 0, cubeVertices);
    glEnableClientState(GL_VERTEX_ARRAY);


    glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeColors);
    glEnableClientState(GL_COLOR_ARRAY);
    glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceFront);
    glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceBack);
    glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceLeft);
    glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceRight);
    glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceTop);
    glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceBottom);


    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];

提前致谢....感谢您的支持。

2 个答案:

答案 0 :(得分:1)

此页面包含有关选择曲面的有用信息:   http://www.opengl.org/resources/faq/technical/selection.htm

答案 1 :(得分:0)

您需要在用户点击的点所定义的线与视图方向和立方体之间执行交集。

您可以折扣指向与视图方向相同方向的所有面部(即远离您)并仅查看指向您的面部。如果你做面部法线和光线的点积,当指向你时它将是负的而当指向远处时它是正的。如果它为零(在舍入误差范围内),则表示您正在查看面边缘。

它可以称为光线投射。

相关问题