我们再来利用三角形、四边形、多边形制作一个简单的小屋,看起来更加逼真一些。小屋由5个四边形和两个五边形组成。墙面的颜色我们可以在画笔中找到后,得到其RGB值后再使用glColor3ub来设定。
void DrawHouse()
{
glBegin(GL_QUADS);
//房屋的前面
glColor3ub(140, 140, 140);
glVertex3f(-2.0f, 0.0f, 1.0f);
glVertex3f(2.0f, 0.0f, 1.0f);
glVertex3f(2.0f, 2.0f, 1.0f);
glVertex3f(-2.0f, 2.0f, 1.0f);
//房屋的后面
glColor3ub(140, 140, 140);
glVertex3f(2.0f, 0.0f, -1.0f);
glVertex3f(-2.0f, 0.0f, -1.0f);
glVertex3f(-2.0f, 2.0f, -1.0f);
glVertex3f(2.0f, 2.0f, -1.0f);
//房屋的地面
glColor3ub(155, 65, 14);
glVertex3f(-2.0f, 0.0f, 1.0f);
glVertex3f(-2.0f, 0.0f, -1.0f);
glVertex3f(2.0f, 0.0f, -1.0f);
glVertex3f(2.0f, 0.0f, 1.0f);
//前屋顶
glColor3ub(238, 156, 0);
glVertex3f(-2.2f, 1.95f, 1.2f);
glVertex3f(2.2f, 1.95f, 1.2f);
glVertex3f(2.2f, 2.5f, 0.0f);
glVertex3f(-2.2f, 2.5f, 0.0f);
//后屋顶
glColor3ub(238, 156, 0); //
glVertex3f(2.2f, 1.95f, -1.2f);
glVertex3f(-2.2f, 1.95f, -1.2f);
glVertex3f(-2.2f, 2.5f, 0.0f);
glVertex3f(2.2f, 2.5f, 0.0f);
glEnd();
glColor3ub(175, 93, 156);
glBegin(GL_POLYGON);
//房屋的左侧面
glVertex3f(-2.0f, 0.0f, 1.0f);
glVertex3f(-2.0f, 2.0f, 1.0f);
glVertex3f(-2.0f, 2.5f, 0.0f);
glVertex3f(-2.0f, 2.0f, -1.0f);
glVertex3f(-2.0f, 0.0f, -1.0f);
glEnd();
glBegin(GL_POLYGON);
//房屋的右侧面
glVertex3f(2.0f, 0.0f, -1.0f);
glVertex3f(2.0f, 2.0f, -1.0f);
glVertex3f(2.0f, 2.5f, 0.0f);
glVertex3f(2.0f, 2.0f, 1.0f);
glVertex3f(2.0f, 0.0f, 1.0f);
glEnd();
}
图4-2 复杂三维模型
上面在绘制立方体时要一个面一个面地绘制,显得很麻烦。实际上OpenGL的图形函数也只有点、线、三角形、四边形、多边形等简单图形,如果要绘制一个圆球,还不是很容易的事。为了方便程序员,OpenGL的辅助库提供了一些绘制基本三维图形的函数,例如立方体只需要调用辅助函数auxWireCude或auxSolidCube即可。
虽然使用辅助函数绘图方便一些,但是却失去了灵活性。使用auxSolidCube也很难实现六个面都是不同的混合色彩的效果。所以是否使用辅助函数,应该视程序的需要而定。
辅助库提供的三维图形有两种形式,一种是网格状,也成为线框模型,另一种是实体。函数名称分别是auxWire*和auxSolid*,具体如表4-1所示:
表4-1 OpenGL辅助库模型函数
三维图形 |
函数 |
球体 |
void auxWireSphere(GLdouble radius) |
void auxSolidSphere(GLdouble radius) |
|
立方体 |
void auxWireCube(GLdouble size) |
void auxSolidCube(GLdouble size) |
|
长方体 |
void auxWireBox(GLdouble width,GLdouble height,GLdouble depth) |
void auxSolidBox(GLdouble width,GLdouble height,GLdouble depth) |
|
圆环 |
void auxWireTorus(GLdouble innerRadius,GLdouble outerRadius) |
void auxSolidTorus(GLdouble innerRadius,GLdouble outerRadius) |
|
圆柱 |
void auxWireCylinder(GLdouble radius,GLdouble height) |
void auxSolidCylinder(GLdouble radius,GLdouble height) |
|
圆锥 |
void auxWireCone(GLdouble radius,GLdouble height) |
void auxSolidCone(GLdouble radius,GLdouble height) |
|
四面体 |
void auxWireTetrahedron(GLdouble radius) |
void auxSolidTetrahedron(GLdouble radius) |
|
八面体 |
void auxWireOctahedron(GLdouble radius) |
void auxSolidOctahedron(GLdouble radius) |
|
十二面体 |
void auxWireDodecahedron(GLdouble radius) |
void auxSolidDodecahedron(GLdouble radius) |
|
二十面体 |
void auxWireIcosahedron(GLdouble radius) |
void auxSolidIcosahedron(GLdouble radius) |
|
茶壶 |
void auxWireTeapot(GLdouble size) |
void aucSolidTeapot(GLdouble size) |
在下面的程序中,利用辅助函数绘制了立方体、圆球、十二面体、圆环、茶壶。
void glMain()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f); //所有的图形全部用红色
glLoadIdentity(); //加载单位矩阵
glTranslatef(0.0f, 0.0f, -8.0f);
auxWireSphere(1.0); //网格球体
glTranslatef(2.5f, 0.0f, 0.0f); //相对变换
auxWireCube(1.0); //网格立方体
glTranslatef(-5.0f, 0.0f, 0.0f);
auxWireTeapot(1.0f); //网格茶壶
glTranslatef(0.0f, -2.0f, 0.0f);
auxSolidTeapot(1.0); //实心茶壶
glTranslatef(2.5f, 0.0f, 0.0f);
auxWireDodecahedron(1.0); //实心十二面体
glTranslatef(2.5f, 0.0f, 0.0f);
auxWireTorus(0.2, 1.0); //网格圆环
SwapBuffers(hDC);
}
程序运行结果如图4-3所示。