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();
有帮助吗?

解决方案

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;
}

其他提示

I'd expect the colors to go from black to med red to med yellow to gray to lt blue grey with the way your if statement is setup (assuming you have at least 600 triangles.) How many tris are you rendering? Also, depending on how your other shaders are setup, textures could get in the way as could anything else if your vertex format is mismatched. Also not sure how glColor3ub() reacts when the blue field goes above 255, which it will in the above code rather quickly. If it truncates the bits, then you would see the colors repeat from yellow to blue grey every 256 triangles after the first 400 .

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top