OpenGL颜色一半的立方体

时间:2018-05-20 17:42:10

标签: c++ visual-studio opengl glut

我试图将一半立方体的颜色看起来像充满了水。在下面的代码中,我绘制了具有白色边缘的立方体:

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

}

但我不知道如何在不擦除白边的情况下对其中的一半进行着色。除此之外,我必须制作动画,以便水可以上下起伏。

1 个答案:

答案 0 :(得分:1)

请注意,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
};

定义" water"的指数。四边形:

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

预览:

water