-2

I want to create a "L" shape so bascially 2 cuboids, I can do this the long way around but want to be able to create it with the vertices method which im struglging to achieve..

Can anyone help?

float vertices[8][3] = {

    { 0, 2, 0.5 }, 
    { 0.5, 2, 0.5 },
    { 0.5, 0, 0.5 }, 
    { 0, 0, 0.5 },
    { 0, 2, -0.5 }, 
    { 0.5, 2, -0.5 },
    { 0.5, 0, -0.5 }, 
    { 0, 0, -0.5 }

};

//set up the array of colours
float colours[6][3] = {

    { 1.0, 0.0, 1.0 }, 
    { 0.5, 0.0, 0.0 },
    { 0.0, 1.0, 0.0 }, 
    { 0.0, 0.5, 0.0 },
    { 0.0, 0.0, 1.0 }, 
    { 0.0, 0.0, 0.5 }

};

void polygon(int a, int b, int c, int d, int colour) {

    glColor3fv(colours[colour]);
    glBegin(GL_POLYGON);
    glVertex3fv(vertices[a]);
    glVertex3fv(vertices[b]);
    glVertex3fv(vertices[c]);
    glVertex3fv(vertices[d]);
    glEnd();
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_LINE_LOOP);
    glVertex3fv(vertices[a]);
    glVertex3fv(vertices[b]);
    glVertex3fv(vertices[c]);
    glVertex3fv(vertices[d]);
    glEnd();

}

void cube() {

    polygon(0, 3, 7, 4, 0); // left
    polygon(1, 5, 6, 2, 1);// right
    polygon(0, 4, 5, 1, 2);// bottom
    polygon(3, 2, 6, 7, 3);// top
    polygon(0, 1, 2, 3, 4); // near
    polygon(4, 7, 6, 5, 5);// far

}
genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

1

It is not so hard you must understand to calculate the number of face and vertex. If you want to make "L" shape then first calculate number of triangular faces. Each triangular face need three vertex so total number of vertex will be number of triangular faces * 3. For simplicity you can use two cube. Cube contains 12 triangular faces and all total 36 vertex. Data for cube is

float vertexData[12][3] = {
//  X     Y     Z      
// bottom
{-1.0f,-1.0f,-1.0f},
{ 1.0f,-1.0f,-1.0f},
{-1.0f,-1.0f, 1.0f},
{ 1.0f,-1.0f,-1.0f},
{ 1.0f,-1.0f, 1.0f},
{-1.0f,-1.0f, 1.0f},

// top
{-1.0f, 1.0f,-1.0f},
{-1.0f, 1.0f, 1.0f},
{ 1.0f, 1.0f,-1.0f},
{ 1.0f, 1.0f,-1.0f},
{-1.0f, 1.0f, 1.0f},
{ 1.0f, 1.0f, 1.0f},

// front
{-1.0f,-1.0f, 1.0f},
{ 1.0f,-1.0f, 1.0f},
{-1.0f, 1.0f, 1.0f},
{ 1.0f,-1.0f, 1.0f},
{ 1.0f, 1.0f, 1.0f},
{-1.0f, 1.0f, 1.0f},

// back
{-1.0f,-1.0f,-1.0f},
{-1.0f, 1.0f,-1.0f},
{ 1.0f,-1.0f,-1.0f},
{ 1.0f,-1.0f,-1.0f},
{-1.0f, 1.0f,-1.0f},
{ 1.0f, 1.0f,-1.0f},

// left
{-1.0f,-1.0f, 1.0f},
{-1.0f, 1.0f,-1.0f},
{-1.0f,-1.0f,-1.0f},
{-1.0f,-1.0f, 1.0f},
{-1.0f, 1.0f, 1.0f},
{-1.0f, 1.0f,-1.0f},

// right
 {1.0f,-1.0f, 1.0f},
 {1.0f,-1.0f,-1.0f},
 {1.0f, 1.0f,-1.0f},
 {1.0f,-1.0f, 1.0f},
 {1.0f, 1.0f,-1.0f},
 {1.0f, 1.0f, 1.0f},
 };

You this Data to draw cube.You can scale and rotate cube to make "L" shape.

Dinesh Subedi
  • 2,603
  • 1
  • 26
  • 36