2009-02-16 29 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