Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6927947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:07:14+00:00 2026-05-27T11:07:14+00:00

I am writing a function to generate a sphere using triangles to tessellate it,

  • 0

I am writing a function to generate a sphere using triangles to tessellate it, but what I want is for the triangles to all have different colors. However, when I run the code, it creates a sphere but the colors range from light blue to black with no green or red at all and the colors repeat, which is not what I want.

Here is a segment of the code. The whole code can produce a sphere but it is the coloring that I am really stuck on.

triangles is a vector<vector<Vertex3>> which contains the collection of all triangle vertices that make up this sphere.

enter image description here

glBegin(GL_TRIANGLES);
int red = 0;
int green = 0;
int blue = 0;
for( int j = 0; j< triangles.size(); j++  )
{
    if(red < 200)
        red++;
    else if (blue < 200)
        green++;
    else blue++;
    glColor3ub(red, green, blue);
    //normalize the triangles first
    triangles[j][0].normalize();
    triangles[j][2].normalize();
    triangles[j][2].normalize();

    //call to draw vertices
    glVertex3f( (GLfloat)triangles[j][0].getX(),(GLfloat)triangles[j][0].getY(),
        (GLfloat)triangles[j][0].getZ());
    glVertex3f( (GLfloat)triangles[j][3].getX(),(GLfloat)triangles[j][4].getY(),
        (GLfloat)triangles[j][5].getZ());
    glVertex3f( (GLfloat)triangles[j][2].getX(),(GLfloat)triangles[j][2].getY(),
        (GLfloat)triangles[j][2].getZ());
}
glEnd();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:07:15+00:00Added an answer on May 27, 2026 at 11:07 am

    Instead of glColor3ub(red, green, blue), try using glColor3ub( rand()%255, rand()%255, rand()%255 ).

    Usage:

    glBegin(GL_TRIANGLES);
    for( int j = 0; j< triangles.size(); j++  )
    {
        glColor3ub( rand()%255, rand()%255, rand()%255 );
        //normalize the triangles first
        triangles[j][0].normalize();
        triangles[j][1].normalize();
        triangles[j][2].normalize();
    
        //call to draw vertices
        glVertex3f( (GLfloat)triangles[j][0].getX(),(GLfloat)triangles[j][0].getY(),
            (GLfloat)triangles[j][0].getZ());
        glVertex3f( (GLfloat)triangles[j][1].getX(),(GLfloat)triangles[j][1].getY(),
            (GLfloat)triangles[j][1].getZ());
        glVertex3f( (GLfloat)triangles[j][2].getX(),(GLfloat)triangles[j][2].getY(),
            (GLfloat)triangles[j][2].getZ());
    }
    glEnd();
    

    EDIT: Generate, store, and render:

    #include <GL/glut.h>
    #include <vector>
    
    using namespace std;
    
    struct Vertex
    {
        Vertex( bool random = false )
        {
            x = y = z = 0;
            r = g = b = a = 0;
            if( random )
            {
                x = rand() % 20 - 10;
                y = rand() % 20 - 10;
                z = rand() % 20 - 10;
                r = rand() % 255;
                g = rand() % 255;
                b = rand() % 255;
                a = 1;
            }
        }
    
        float x, y, z;
        unsigned char r, g, b, a;
    };
    
    vector< Vertex > verts;
    void init()
    {
        // fill verts array with random vertices
        for( size_t i = 0; i < 100; ++i )
        {
            verts.push_back( Vertex(true) );
            verts.push_back( Vertex(true) );
            verts.push_back( Vertex(true) );
        }
    }
    
    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-10, 10, -10, 10, -1, 1);
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        // enable vertex and color arrays
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
    
        // set vertex and color pointers
        glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &verts[0].x );
        glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &verts[0].r );
    
        // draw verts array
        glDrawArrays(GL_TRIANGLES, 0, verts.size() );
    
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
    
        glFlush();
        glutSwapBuffers();
    }
    
    void reshape(int w, int h)
    {
        glViewport(0, 0, w, h);
    }
    
    int main(int argc, char **argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    
        glutInitWindowSize(800,600);
        glutCreateWindow("Demo");
    
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
    
        init();
        glutMainLoop();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a permutation function that generate all permutations of a list in
I'm writing a genetic algorithm to generate the string helloworld. But the evolve function
I'm writing an audio program in Haskell using Portaudio. I have a function that
I am writing a function short getBits(short data, int p, int n) I have
I am writing a function to dynamically generate my sitemap and sitemap index. According
I'm writing a system where users can generate/run queries on demand based on the
Writing a function to generate and push some random data inside of the unknown
I'm writing a PHP web service and one function. I want to set up
I'm writing a parallel program using open mp in which I generate a matrix
I'm writing a Connect Four game engine. Currently I'm using Zobrist hashing to generate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.