c - 使用不同颜色的三角形镶嵌球体

标签 c opengl geometry tesselation

我正在编写一个函数来生成一个使用三角形 segmentation 的球体,但我想要的是三角形都具有不同的颜色。但是,当我运行代码时,它创建了一个球体,但颜色范围从浅蓝色到黑色,根本没有绿色或红色,并且颜色重复,这不是我想要的。

这是一段代码。整个代码可以生成一个球体,但我真正坚持的是着色。

trianglesvector<vector<Vertex3>>其中包含构成该球体的所有三角形顶点的集合。

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();

最佳答案

代替 glColor3ub(red, green, blue),尝试使用 glColor3ub( rand()%255, rand()%255, rand()%255 ) .

用法:

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();

编辑:生成、存储和渲染:

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

关于c - 使用不同颜色的三角形镶嵌球体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8481194/

相关文章:

c - 屏蔽信号的原因?

Objective-C sqlite 在 LIKE 子句中添加查询参数

opengl - GLSL:启用/禁用纹理+着色器

algorithm - 往返经纬度和3D点的可逆算法

c++ - 如何检查 3D 点是在 vector 的左侧还是右侧

algorithm - 从矩形绘制椭圆

while(1)之前的代码不运行

c - 在 C 中使用 read 打印 "hello world!"

c - 使用 openGL 和/或 X11 的截图

java - 在 OpenGL 中使用 GL_QUADS 渲染时,形状的一部分丢失