2009-02-16 88 views
1

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

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

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

然而,不管是什么,我把我的三角形始终是黄色的,或颜色数组中第一种颜色的值。我已经进入调试器并检查每个单独循环迭代的值,并且r g b和一个变量的值实际上相应地改变,并且不总是黄色,但结果是黄色三角形。

然而,如果我把下面从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种颜色一个很好的混合三角形的每个顶点

回答

5

其实,我想我明白了:你只看到你的左下角的一小部分三角形。 您需要远离它以完全查看它:您的坐标太大。

2

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

+0

http://www.darkstars.co.uk/downloads/view.php?dl=2&file=spring/other/yellowTriangle2.jpg 仍为黄色 – 2009-02-16 11:49:52

+0

论,使用glColor3d的顶部不glColor4d不起作用所以阿尔法不是问题。 – 2009-02-16 12:00:16