1

这可能是一个愚蠢的错误,但我看不到?!我有定义几何的类和渲染该几何的类。现在它是每个顶点的基本三角形和颜色。

这是定义所述几何对象数据的代码:

CGeometry* g = new CGeometry();
g->vertexes = new double[3*3];

g->vertexes[0] = 0;
g->vertexes[1] = 0;
g->vertexes[2] = 0;

g->vertexes[3] = 100;
g->vertexes[4] = 100;
g->vertexes[5] = 0;

g->vertexes[6] = 100;
g->vertexes[7] = 0;
g->vertexes[8] = 0;

g->colors = new double[12];

g->colors[0] = 1;
g->colors[1] = 1;
g->colors[2] = 0;
g->colors[3] = 1;

g->colors[4] = 1;
g->colors[5] = 0;
g->colors[6] = 1;
g->colors[7] = 0;

g->colors[8] = 0;
g->colors[9] = 1;
g->colors[10] = 1;
g->colors[11] = 0;

这是呈现所述数据的代码:

CGeometry* g = object->geometry;

int j = object->endIndex - object->startIndex;
double* vertexes = g->vertexes;
double* colors = g->colors;

glBegin(GL_TRIANGLES);
{
    for(int i = 0; i < j; i++){
        int coord = object->startIndex+i;
        int colorind = coord*4;

        double r,g,b,a;
        r = colors[colorind];
        g = colors[colorind+1];
        b = colors[colorind+2];
        a = colors[colorind+3];

        glColor4d(  r,g,b,a);
        glVertex3d( vertexes[coord*3],
                    vertexes[coord*3+1],
                    vertexes[coord*3+2]);
    }
}
glEnd();

然而,无论我放什么,我的三角形总是黄色的,或者颜色数组中第一种颜色的值。我已经进入调试器并检查了每个单独循环迭代的值,并且 rgb 和变量的值确实相应地发生了变化,并且并不总是黄色,但结果是一个黄色三角形。

但是,如果我从 neheGL 教程中获取以下内容:

glClearColor(0.1f,0.1f,0.1f,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();                                   // Reset The Current Modelview Matrix
//glTranslatef(1.5f,0.0f,0.0f);                     // Move Left 1.5 Units And Into The Screen 6.0
glBegin(GL_TRIANGLES);                              // Drawing Using Triangles
    glColor3f(1.0f,0.0f,0.0f);                      // Set The Color To Red
    glVertex3f( 0.0f, 1.0f, 0.0f);                  // Top
    glColor3f(0.0f,1.0f,0.0f);                      // Set The Color To Green
    glVertex3f(-1.0f,-1.0f, 0.0f);                  // Bottom Left
    glColor3f(0.0f,0.0f,1.0f);                      // Set The Color To Blue
    glVertex3f( 1.0f,-1.0f, 0.0f);                  // Bottom Right
glEnd();                                            // Finished Drawing The Triangle
glTranslatef(160.0f,0.0f,0.0f);                     // Move Right 3 Units
glColor3f(0.5f,0.5f,1.0f);                          // Set The Color To Blue One Time Only
glBegin(GL_QUADS);                                  // Draw A Quad
    glVertex3f(-1.0f, 1.0f, 0.0f);                  // Top Left
    glVertex3f( 1.0f, 1.0f, 0.0f);                  // Top Right
    glVertex3f( 1.0f,-1.0f, 0.0f);                  // Bottom Right
    glVertex3f(-1.0f,-1.0f, 0.0f);                  // Bottom Left
glEnd();

我得到一个很好的混合三角形,每个顶点有 3 种颜色

4

2 回答 2

5

实际上,我想我明白了:您只看到三角形左下角的一小部分。您需要离开它才能完全查看它:您的坐标太大。

于 2009-02-16T13:52:10.290 回答
2

第二种和第三种颜色的 alpha 值设置为零,因此它们是完全透明的。第一种颜色的 alpha=1 并且是在生成的三角形中唯一可以看到的颜色...

于 2009-02-16T11:33:50.780 回答