예제 #1
0
파일: Cube.cpp 프로젝트: bakge/bakge
Cube* Cube::Create()
{
    static const Scalar Normals[] = {
        0, 0, +1.0f, // A+Z
        0, -1.0f, 0, // A-Y
        -1.0f, 0, 0, // A-X
        0, 0, +1.0f, // B+Z
        0, -1.0f, 0, // B-Y
        +1.0f, 0, 0, // B+X
        0, 0, +1.0f, // C+Z
        0, +1.0f, 0, // C+Y
        +1.0f, 0, 0, // C+X
        0, 0, +1.0f, // D+Z
        0, +1.0f, 0, // D+Y
        -1.0f, 0, 0, // D-X
        0, 0, -1.0f, // E-Z
        0, -1.0f, 0, // E-Y
        -1.0f, 0, 0, // E-X
        0, 0, -1.0f, // F-Z
        0, -1.0f, 0, // F-Y
        +1.0f, 0, 0, // F+X
        0, 0, -1.0f, // G-Z
        0, +1.0f, 0, // G+Y
        +1.0f, 0, 0, // G+X
        0, 0, -1.0f, // H-Z
        0, +1.0f, 0, // H+Y
        -1.0f, 0, 0  // H-X
    };

    static const int Indices[] = {
        0, 9, 6, // +Z
        0, 3, 6,
        15, 18, 21, // -Z
        15, 12, 21,
        14, 23, 11, // +Y
        14, 2, 11,
        5, 8, 20, // -Y
        5, 17, 20,
        10, 22, 19, // +X
        10, 7, 19,
        1, 13, 16, // -X
        1, 4, 16
    };

    static const Scalar TexCoords[] = {
        0.995f, 0.005f, // A+Z
        0.005f, 0.995f, // A-Y
        0.995f, 0.005f, // A-X
        0.005f, 0.005f, // B+Z
        0.995f, 0.995f, // B-Y
        0.005f, 0.005f, // B+X
        0.005f, 0.995f, // C+Z
        0.995f, 0.005f, // C+Y
        0.005f, 0.995f, // C+X
        0.995f, 0.995f, // D+Z
        0.005f, 0.005f, // D+Y
        0.995f, 0.995f, // D-X
        0.005f, 0.005f, // E-Z
        0.005f, 0.005f, // E-Y
        0.005f, 0.005f, // E-X
        0.995f, 0.005f, // F-Z
        0.995f, 0.005f, // F-Y
        0.995f, 0.005f, // F+X
        0.995f, 0.995f, // G-Z
        0.995f, 0.995f, // G+Y
        0.995f, 0.995f, // G+X
        0.005f, 0.995f, // H-Z
        0.005f, 0.995f, // H+Y
        0.005f, 0.995f  // H-X
    };

    static const Scalar Positions[] = {
        -0.5f, -0.5f, +0.5f,
        -0.5f, -0.5f, +0.5f,
        -0.5f, -0.5f, +0.5f,
        +0.5f, -0.5f, +0.5f,
        +0.5f, -0.5f, +0.5f,
        +0.5f, -0.5f, +0.5f,
        +0.5f, +0.5f, +0.5f,
        +0.5f, +0.5f, +0.5f,
        +0.5f, +0.5f, +0.5f,
        -0.5f, +0.5f, +0.5f,
        -0.5f, +0.5f, +0.5f,
        -0.5f, +0.5f, +0.5f,
        -0.5f, -0.5f, -0.5f,
        -0.5f, -0.5f, -0.5f,
        -0.5f, -0.5f, -0.5f,
        +0.5f, -0.5f, -0.5f,
        +0.5f, -0.5f, -0.5f,
        +0.5f, -0.5f, -0.5f,
        +0.5f, +0.5f, -0.5f,
        +0.5f, +0.5f, -0.5f,
        +0.5f, +0.5f, -0.5f,
        -0.5f, +0.5f, -0.5f,
        -0.5f, +0.5f, -0.5f,
        -0.5f, +0.5f, -0.5f
    };

    Cube* C = new Cube;

    if(C->CreateBuffers() != BGE_SUCCESS) {
        delete C;
        return NULL;
    }

    C->SetPositionData(24, Positions);
    C->SetNormalData(24, Normals);
    C->SetTexCoordData(24, TexCoords);
    C->SetIndexData(36, Indices);

    C->Unbind();

    return C;
}
예제 #2
0
파일: Cube.cpp 프로젝트: jgcoded/bakge
Cube* Cube::Create(Scalar Length, Scalar Width, Scalar Height)
{
    Cube* C = new Cube;

    if(C->CreateBuffers() != BGE_SUCCESS) {
        delete C;
        return NULL;
    }

    /* So following changes affect our cube's VAO */
    C->BindVAO();

    /* *
     * A cube has 6 faces, each composed of two triangles.
     * We only need 8 vertices (3 components for each vertex, 24 buffer size)
     * The indices buffer will create our triangles
     * */
    C->NumFaces = 6;
    C->NumVertices = 4 * 6; /* 4 vertices per face */
    C->NumIndices = 36; /* 2 triangles per face, 3 vertices per triangle */
    Scalar* Vertices = new Scalar[72];
    Scalar* Normals = new Scalar[72];
    Scalar* TexCoords = new Scalar[48];
    unsigned int* Indices = new unsigned int[36];

    /* *
     *       H__________G
     *      /|         /|
     *    D/_|_______C/ |         +Y HEIGHT
     *    |  |       |  |         |            MODEL COORDINATE
     *    |  |       |  |         |
     *    |  |E______|__|F        |_____ +X WIDTH
     *    | /        | /         /
     *    |/_________|/         /
     *    A          B         +Z LENGTH
     *
     * Each of A through H corresponds to 3 separate vertices.
     * They are separate so normals can be constant across each
     * of the cube's faces.
     *
     * e.g. A corresponds to vertices A+Z, A-Y and A-X
     *
     * Texcoords
     * Z and X faces: When the face normal is pointing towards you and
     * the Y axis pointing up, texcoords are as follows
     *    BL 0, 0    BR 1, 0
     *    TL 0, 1    TR 1, 1
     *
     *  e.g. +Z face:
     *        A 0, 0
     *        B 1, 0
     *        D 0, 1
     *        C 1, 1
     *
     * Y faces:
     *   +Y:
     *     D 0, 0
     *     C 1, 0
     *     H 0, 1
     *     G 1, 1
     *
     *   -Y:
     *     E 0, 0
     *     F 1, 0
     *     A 0, 1
     *     B 1, 1
     * */

    /* *
     * Convenience. Dimensions are full edge measurements and the cube is
     * formed around the origin (0, 0, 0) anyways
     * */
    Width /= 2;
    Length /= 2;
    Height /= 2;

    memset((void*)Normals, 0, sizeof(Normals[0]) * 72);
    memset((void*)TexCoords, 0, sizeof(TexCoords[0]) * 48);

    /* +Z */
    Indices[0] = 0;
    Indices[1] = 9;
    Indices[2] = 6;
    Indices[3] = 0;
    Indices[4] = 3;
    Indices[5] = 6;

    /* -Z */
    Indices[6] = 15;
    Indices[7] = 18;
    Indices[8] = 21;
    Indices[9] = 15;
    Indices[10] = 12;
    Indices[11] = 21;

    /* -X */
    Indices[12] = 14;
    Indices[13] = 23;
    Indices[14] = 11;
    Indices[15] = 14;
    Indices[16] = 2;
    Indices[17] = 11;

    /* +X */
    Indices[18] = 5;
    Indices[19] = 8;
    Indices[20] = 20;
    Indices[21] = 5;
    Indices[22] = 17;
    Indices[23] = 20;

    /* +Y */
    Indices[24] = 10;
    Indices[25] = 22;
    Indices[26] = 19;
    Indices[27] = 10;
    Indices[28] = 7;
    Indices[29] = 19;

    /* -Y */
    Indices[30] = 1;
    Indices[31] = 13;
    Indices[32] = 16;
    Indices[33] = 1;
    Indices[34] = 4;
    Indices[35] = 16;

    /* A+Z */
    Vertices[0] = -Width;
    Vertices[1] = -Height;
    Vertices[2] = +Length;
    Normals[2] = +1.0f;
    /* A-Y */
    Vertices[3] = -Width;
    Vertices[4] = -Height;
    Vertices[5] = +Length;
    Normals[4] = -1.0f;
    TexCoords[3] = 1;
    /* A-X */
    Vertices[6] = -Width;
    Vertices[7] = -Height;
    Vertices[8] = +Length;
    Normals[6] = -1.0f;
    TexCoords[4] = 1;

    /* B+Z */
    Vertices[9] = +Width;
    Vertices[10] = -Height;
    Vertices[11] = +Length;
    Normals[11] = +1.0f;
    TexCoords[6] = 1;
    /* B-Y */
    Vertices[12] = +Width;
    Vertices[13] = -Height;
    Vertices[14] = +Length;
    Normals[13] = -1.0f;
    TexCoords[8] = 1;
    TexCoords[9] = 1;
    /* B+X */
    Vertices[15] = +Width;
    Vertices[16] = -Height;
    Vertices[17] = +Length;
    Normals[15] = + 1.0f;

    /* C+Z */
    Vertices[18] = +Width;
    Vertices[19] = +Height;
    Vertices[20] = +Length;
    Normals[20] = +1.0f;
    TexCoords[12] = 1;
    TexCoords[13] = 1;
    /* C+Y */
    Vertices[21] = +Width;
    Vertices[22] = +Height;
    Vertices[23] = +Length;
    Normals[22] = + 1.0f;
    TexCoords[14] = 1;
    /* C+X */
    Vertices[24] = +Width;
    Vertices[25] = +Height;
    Vertices[26] = +Length;
    Normals[24] = +1.0f;
    TexCoords[17] = 1;

    /* D+Z */
    Vertices[27] = -Width;
    Vertices[28] = +Height;
    Vertices[29] = +Length;
    Normals[29] = +1.0f;
    TexCoords[19] = 1;
    /* D+Y */
    Vertices[30] = -Width;
    Vertices[31] = +Height;
    Vertices[32] = +Length;
    Normals[31] = +1.0f;
    /* D-X */
    Vertices[33] = -Width;
    Vertices[34] = +Height;
    Vertices[35] = +Length;
    Normals[33] = -1.0f;
    TexCoords[22] = 1;
    TexCoords[23] = 1;

    /* E-Z */
    Vertices[36] = -Width;
    Vertices[37] = -Height;
    Vertices[38] = -Length;
    Normals[38] = -1.0f;
    TexCoords[24] = 1;
    /* E-Y */
    Vertices[39] = -Width;
    Vertices[40] = -Height;
    Vertices[41] = -Length;
    Normals[40] = -1.0f;
    /* E-X */
    Vertices[42] = -Width;
    Vertices[43] = -Height;
    Vertices[44] = -Length;
    Normals[42] = -1.0f;

    /* F-Z */
    Vertices[45] = +Width;
    Vertices[46] = -Height;
    Vertices[47] = -Length;
    Normals[47] = -1.0f;
    /* F-Y */
    Vertices[48] = +Width;
    Vertices[49] = -Height;
    Vertices[50] = -Length;
    Normals[49] = -1.0f;
    TexCoords[32] = 1;
    /* F+X */
    Vertices[51] = +Width;
    Vertices[52] = -Height;
    Vertices[53] = -Length;
    Normals[51] = +1.0f;
    TexCoords[34] = 1;

    /* G-Z */
    Vertices[54] = +Width;
    Vertices[55] = +Height;
    Vertices[56] = -Length;
    Normals[56] = -1.0f;
    TexCoords[37] = 1;
    /* G+Y */
    Vertices[57] = +Width;
    Vertices[58] = +Height;
    Vertices[59] = -Length;
    Normals[58] = +1.0f;
    TexCoords[38] = 1;
    TexCoords[39] = 1;
    /* G+X */
    Vertices[60] = +Width;
    Vertices[61] = +Height;
    Vertices[62] = -Length;
    Normals[60] = +1.0f;
    TexCoords[40] = 1;
    TexCoords[41] = 1;

    /* H-Z */
    Vertices[63] = -Width;
    Vertices[64] = +Height;
    Vertices[65] = -Length;
    Normals[65] = -1.0f;
    TexCoords[42] = 1;
    TexCoords[43] = 1;
    /* H+Y */
    Vertices[66] = -Width;
    Vertices[67] = +Height;
    Vertices[68] = -Length;
    Normals[67] = +1.0f;
    TexCoords[45] = 1;
    /* H-X */
    Vertices[69] = -Width;
    Vertices[70] = +Height;
    Vertices[71] = -Length;
    Normals[69] = -1.0f;
    TexCoords[47] = 1;

    glBindBuffer(GL_ARRAY_BUFFER, C->MeshBuffers[MESH_BUFFER_POSITIONS]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices[0]) * 72, Vertices,
                                                        GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, C->MeshBuffers[MESH_BUFFER_NORMALS]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Normals[0]) * 72, Normals,
                                                        GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, C->MeshBuffers[MESH_BUFFER_TEXCOORDS]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(TexCoords[0]) * 48, TexCoords,
                                                        GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, C->MeshBuffers[MESH_BUFFER_INDICES]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices[0]) * 36,
                                            Indices, GL_STATIC_DRAW);

    C->Unbind();

    delete[] Vertices;
    delete[] Normals;
    delete[] Indices;
    delete[] TexCoords;

    return C;
}