chasep255
chasep255

Reputation: 12185

OpenGL, how to give sides of pyramid solid colors

Right now I am learning OpenGL and I want to draw a pyramid where each side has a different color. The issue I am having is that I only seem to be able to assign colors to the vertexes and not the sides. Therefore each side has a gradient effect rather than being a solid color. How can I give my sides solid color?

    void init_buffer()
    {
        glGenBuffers(1, &(b.trifan));
        glBindBuffer(GL_ARRAY_BUFFER, b.trifan);

        GLfloat trifan[6][3] =
        {
            {0.0, 1.0, 0.0},
            {1.0, 0.0, 1.0},
            {-1.0, 0.0, 1.0},
            {-1.0, 0.0, -1.0},
            {1.0, 0.0, -1.0},
            {1.0, 0.0, 1.0}
        };

        glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, trifan, GL_STATIC_COPY);


        //base of the pyramid as 2 triangles
        glGenBuffers(1, &(b.tribase));
        glBindBuffer(GL_ARRAY_BUFFER, b.tribase);
        GLfloat tribase[4][3] = 
        {
            {1.0, 0.0, -1.0},
            {-1.0, 0.0, -1.0},
            {1.0, 0.0, 1.0},
            {-1.0, 0.0, 1.0}
        };
        glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 4 * 3, tribase, GL_STATIC_COPY);

        //colors
        glGenBuffers(1, &b.colors);
        glBindBuffer(GL_ARRAY_BUFFER, b.colors);
        GLfloat colors[6][3] = 
        {
            {1.0, 0.0, 0.0},
            {1.0, 0.0, 0.0},
            {1.0, 0.0, 0.0},
            {1.0, 0.0, 1.0},
            {0.5, 0.0, 1.0},
            {1.0, 0.0, 0.5},
        };
        glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, colors, GL_STATIC_COPY);

        check_errors();
    }


...
//Inside my display function
glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    {
        glBindBuffer(GL_ARRAY_BUFFER, b.trifan);
        glVertexPointer(3, GL_FLOAT, 0, 0);

        glBindBuffer(GL_ARRAY_BUFFER, b.colors);
        glColorPointer(3, GL_FLOAT, 0, 0);

        glDrawArrays(GL_TRIANGLE_FAN, 0, 6);

        glBindBuffer(GL_ARRAY_BUFFER, b.tribase);
        glVertexPointer(3, GL_FLOAT, 0, 0);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    }
    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);

Upvotes: 0

Views: 470

Answers (1)

Prabindh
Prabindh

Reputation: 3504

You can duplicate the vertices for each face as already suggested in the comments. The same is also achievable with the use of different textures (bitmaps) for each face, without duplicating the vertices.

Refer to

http://oglsuperbible5.googlecode.com/svn/trunk/Src/Chapter05/Pyramid/Pyramid.cpp

Figure at https://www.scss.tcd.ie/Michael.Manzke/CS7055/Lab2/SuperBible.4th.Ed.Ch8-9.pdf

Note: The superbible reference only has one texture mapped on all faces, so additional code is to be written to map different textures to different faces.

Upvotes: 1

Related Questions