当前位置:  开发笔记 > 编程语言 > 正文

黄色三角应该是3种颜色的OpenGL

如何解决《黄色三角应该是3种颜色的OpenGL》经验,为你挑选了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种颜色



1> Renaud Bompu..:

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

推荐阅读
Life一切安好
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有