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 736457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:36:25+00:00 2026-05-14T07:36:25+00:00

Right now, I have more than 25 vertices that form a model. I want

  • 0

Right now, I have more than 25 vertices that form a model. I want to interpolate color linearly between the first and last vertex. The Problem is when I write the following code

glColor3f(1.0,0.0,0.0);
vertex3f(1.0,1.0,1.0);
vertex3f(0.9,1.0,1.0);
.
.`<more vertices>;
glColor3f(0.0,0.0,1.0);
vertex3f(0.0,0.0,0.0);

All the vertices except that last one are red. Now I am wondering if there is a way to interpolate color across these vertices without me having to manually interpolate color (instead natively, like how opengl does it automatically) at each vertex since, I will be having a lot more number of colors at various vertices. Any help would be extremely appreciated.

Thank you!

  • 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-14T07:36:25+00:00Added an answer on May 14, 2026 at 7:36 am

    OpenGL will interpolate colors of pixels between one vertex and the next, but I don’t know of any way to get it to automatically interpolate the values for intermediate vertexes. Normally, that’s not particularly difficult though — you don’t want to write code for each individual vertex anyway, so adding the computation is pretty trivial:

    class pointf { 
        GLfloat x, y, z;
    };
    
    std::vector<pointf> spots;
    
    // ...
    GLfloat start_blue = 1.0f;
    GLfloat end_blue = 0.0f;
    GLfloat start_green = 0.0f;
    GLfloat end_green = 0.0f;
    GLfloat start_red = 0.0f;
    GLfloat end_red = 1.0f;
    
    GLfloat size = spots.size();
    
    glBegin(GL_POLYGON);
    for (int i=0; i<spots.size(); i++) {
        GLfloat red = start_red + (end_red-start_red) * i/size;
        GLfloat green = start_green + (end_green-start_green) * i/size;
        GLfloat blue = start_blue + (end_blue-start_blue) * i/size;
    
        glColor3f(red, green, blue);
        glVertex3f(spots[i].x, spots[i].y, spots[i].z);
    }
    glEnd();
    

    One thing though: this does purely linear interpolation per vertex. It does not, for example, attempt to take into account the distance between one vertex and the next. I’d guess questions of how to do things like this are (at least part of) why OpenGL doesn’t attempt this on its own.

    Edit: for a gradient across a hemisphere, I’d try something like this:

    // Blue light on the left  
    GLfloat blue[] = {0.0f, 0.0f, 1.0f, 1.0f};
    GLfloat blue_pos[] = {-1.0f, 0.0f, -0.3f, 0.0f};
    
    // red light on the right
    GLfloat red[] = {1.0f, 0.0f, 0.0f, 1.0f};
    GLfloat red_pos[] = {1.0f, 0.0f, -0.3f, 0.0f};
    
    // turn on lighting:
    glEnable(GL_LIGHTING);
    glShadeModel(GL_SMOOTH);
    
    // Set up the two lights:
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, blue);
    glLightfv(GL_LIGHT0, GL_POSITION, blue_pos);
    
    glEnable(GL_LIGHT1);
    glLightfv(GL_LIGHT1, GL_DIFFUSE, red);
    glLightfv(GL_LIGHT1, GL_POSITION, red_pos);
    
    // set up the material for our sphere (light neutral gray):
    GLfloat sph_mat[] = {0.8f, 0.8f, 0.8f, 1.0f};
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, sph_mat);
    
    // Draw a sphere:
    GLUquadric *q = gluNewQuadric();
    gluQuadricOrientation(q, GLU_OUTSIDE);
    gluQuadricDrawStyle(q, GLU_FILL);
    gluQuadricNormals(q, GLU_SMOOTH);
    gluSphere(q, 1.0, 64, 64);
    

    For the moment, I’ve done the outside of a sphere, but doing a hemisphere isn’t drastically different.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I have a foreach loop that grabs the first link with an
Right now I have a script that will get the last five files in
Right now I have a function, in a class that is used to listen
Right now I have a link that causes a hidden div to appear when
Right now I have a PHP file that does a MYSQL query and then
Right now I have a vector called closest.labels that has the following data in
Okay, I have this program and I don't want more than one instance of
Good day! I right now have a function the drags an element from a
Right now I have an upload field while uploads files to the server. The
Right now I have 3 tables: User, Roles, and User_Roles for the many-to-many association.

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.