这可能是一个愚蠢的错误,但我看不到?!我有定义几何的类和渲染该几何的类。现在它是每个顶点的基本三角形和颜色。
这是定义所述几何对象数据的代码:
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 种颜色