我正在尝试将一个立方体的一半上色,使其看起来像充满水。在下面的代码中,我用白色边缘绘制了立方体:

void initGL() {
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glShadeModel(GL_SMOOTH);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glLoadIdentity();

    glTranslatef(-3.0f, 0.0f, -9.0f);

    glBegin(GL_QUADS);
    glBegin(GL_LINES);

    glColor3f(1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);

    glColor3f(1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);

    glColor3f(1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);

    glColor3f(1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);

    glColor3f(1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);

    glColor3f(1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);
    glEnd();
    glutSwapBuffers();

}

但是我不知道如何在不擦除白色边缘的情况下为其中的一半上色。除此之外,我还必须对其进行动画处理,以便水可以上下流动。

最佳答案

请注意,不赞成使用glBeginglEnd(甚至是glVertexPointerglEnableClientState)。
阅读有关Fixed Function PipelineVertex Specification的信息。

但是根据您的代码,我建议定义一个顶点数据数组( glVertexPointer )
以及客户端功能( glEnableClientState )来解决您的问题。
进一步定义索引数组,并使用 glDrawElements
从数组数据渲染(索引)图元。

定义顶点坐标:

GLfloat water_height = -0.5f;
GLfloat vertices[] =
{
    -1.0f, -1.0f,         -1.0f,  // 0
     1.0f, -1.0f,         -1.0f,  // 1
     1.0f, -1.0f,          1.0f,  // 2
    -1.0f, -1.0f,          1.0f,  // 3

    -1.0f,  1.0f,         -1.0f,  // 5
     1.0f,  1.0f,         -1.0f,  // 6
     1.0f,  1.0f,          1.0f,  // 7
    -1.0f,  1.0f,          1.0f,  // 8

    -1.0f,  water_height, -1.0f,  // 9
     1.0f,  water_height, -1.0f,  // 10
     1.0f,  water_height,  1.0f,  // 11
    -1.0f,  water_height,  1.0f   // 12
};

定义“水”四边形的索引:
// 6 quad indices for the 6 sides of the water cube
GLuint water_indices[] =
{
    0, 1, 2, 3,
    0, 1, 9, 8,
    1, 2, 10, 9,
    2, 3, 11, 10,
    3, 0, 8, 11,
    8, 9, 10, 11
};

定义边缘的索引:
// 8 edges for the part of the cube which is not filled with water
GLuint edge_indices[] =
{
    4, 5,
    5, 6,
    6, 7,
    7, 4,
    4, 8,
    5, 9,
    6, 10,
    7, 11
};

绘制四边形和线条:
glVertexPointer( 3, GL_FLOAT, 0, vertices );
glEnableClientState( GL_VERTEX_ARRAY );

glColor4f( 0.3f, 0.5f, 1.0f, 1.0f );
glDrawElements( GL_QUADS, 6 * 4, GL_UNSIGNED_INT, water_indices );

glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
glDrawElements( GL_LINES, 8 * 2, GL_UNSIGNED_INT, edge_indices );

glDisableClientState( GL_VERTEX_ARRAY );

预习:

c++ - OpenGL彩色立方体的一半-LMLPHP

07-26 09:25