rohanparekh
rohanparekh

Reputation: 51

identify the surface of 3d cube

I am developing an iPad application which just drawing a 3d cube. User can rotate it with finger motion and can zoom in/out with pinching it.

Right now I am coloring whole cube (every surface) at same time. Now if I want user to color each surface of cube separately, means user will tap one surface and it will color that surface only but I dont know how to identify that surface what user have tapped..

I am drawing whole cube with simple openGL basics as below..

    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];

Thanks in advance....appreciated your support.

Upvotes: 0

Views: 273

Answers (2)

andrewmu
andrewmu

Reputation: 14534

This page has useful information about selecting surfaces: http://www.opengl.org/resources/faq/technical/selection.htm

Upvotes: 1

ChrisF
ChrisF

Reputation: 137148

You need to perform an intersection between the line defined by the point the user taps and the view direction and the cube.

You can discount all faces that are pointing in the same direction as the view direction (i.e. away from you) and only look at those pointing towards you. If you do the dot product of the face normal and the ray it will be negative when pointing towards you and positive when pointing away. If it's zero (within rounding error) then you are looking at the face edge on.

It can be called ray casting.

Upvotes: 0

Related Questions