OpenGL作为一种图形与硬件的接口,与其他图形程序开发工具相比较,它提供了众多图形函数,直观的编程环境简化了三维图形的绘制过程,使用OpenGL搭建一个三维场景,能够通过输入设备与场景内物体交互。豪华单间
配置环境:vs22+freeglut库
1.绘制墙体使其成为封闭空间,在封闭空间内完成场景的布置
//空间基本架构
void drawWall()
{
glColor3f(0.47, 0.54, 0.60);//rgb颜色可调
GLfloat matWall[] = { 0.37, 0.83, 0.98};
GLfloat matBlack[] = { 0, 0, 0, 1 };
GLfloat matWhite[] = { 1, 1, 1, 1 };
GLfloat matShininess[] = { 40 };
glMaterialfv(GL_FRONT, GL_AMBIENT, matWall);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matWall);
glMaterialfv(GL_FRONT, GL_SPECULAR, matWhite);
glMaterialfv(GL_FRONT, GL_SHININESS, matShininess);
glMaterialfv(GL_FRONT, GL_EMISSION, matBlack);
// 左边
drawQuads(-10, 10, -20,
-10, 10, 0,
-10, -10, 0,
-10, -10, -20);
// 右边
drawQuads(10, -10, -20,
10, -10, 0,
10, 10, 0,
10, 10, -20);
// back
drawQuads(10, 10, -20,
-10, 10, -20,
-10, -10, -20,
10, -10, -20);
// 天花板
drawQuads(10, 10, -20,
10, 10, 0,
-10, 10, 0,
-10, 10, -20);
// 地板
glColor3f(0.75, 0.75, 0.75);
drawQuads(-10, -10, -20,
-10, -10, 0,
10, -10, 0,
10, -10, -20);
}
glMaterialfv ()函数指定照明模型的材料参数。
2.绘制一张床将其放在角落(左)
void drawBed()
{
glColor3f(1, 0.8, 0.5);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrown);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrown);
glMaterialfv(GL_FRONT, GL_SPECULAR, matWhite);
glMaterialfv(GL_FRONT, GL_SHININESS, matShininess);
glMaterialfv(GL_FRONT, GL_EMISSION, matBlack);
// 床头
glPushMatrix();
glTranslatef(-9.5, -6, -16);
glScalef(1, 8, 8);
glutSolidCube(1);
glPopMatrix();
// 床尾
glPushMatrix();
glTranslatef(4.5, -8.5, -16);
glScalef(1, 3, 8);
glutSolidCube(1);
glPopMatrix();
//床板
glPushMatrix();
glTranslatef(-2, -7, -16);
glScalef(14, 1, 8);
glutSolidCube(1);
glPopMatrix();
//床垫
glColor3f(0.6, 0.97, 0.6);
glMaterialfv(GL_FRONT, GL_AMBIENT, matGreen);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matGreen);
glMaterialfv(GL_FRONT, GL_SPECULAR, matWhite);
glMaterialfv(GL_FRONT, GL_SHININESS, matShininess);
glMaterialfv(GL_FRONT, GL_EMISSION, matBlack);
glPushMatrix();
glTranslatef(-2, -6, -16);
glScalef(14, 1, 8);
glutSolidCube(1);
glPopMatrix();
}
效果图如下:
3.房间的一些基本配置
void drawDesk()
{
glColor3f(1, 0.8, 0.5);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrown);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrown);
glMaterialfv(GL_FRONT, GL_SPECULAR, matWhite);
glMaterialfv(GL_FRONT, GL_SHININESS, matShininess);
glMaterialfv(GL_FRONT, GL_EMISSION, matBlack);
//书桌
glPushMatrix();
glTranslatef(9, -4, -10);
glScalef(4, 0.5, 20);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(9.75, -7, -10);
glScalef(0.5, 6, 20);
glutSolidCube(1);
glPopMatrix();
//床头柜
glPushMatrix();
glTranslatef(-8, -8, -10);
glScalef(4, 4, 4);
glutSolidCube(1);
glPopMatrix();
}
这桌子建议改成实体的bushi再添置一个茶壶 bushi文章来源:https://www.toymoban.com/news/detail-510348.html
void drawTeapot()
{
glColor3f(0.80,0.71,0.34);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrown);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrown);
glMaterialfv(GL_FRONT, GL_SPECULAR, matWhite);
glMaterialfv(GL_FRONT, GL_SHININESS, matShininess);
glMaterialfv(GL_FRONT, GL_EMISSION, matBlack);
glPushMatrix();
glTranslatef(-8, -5.5, -10);
glRotatef(-100, 0, 1, 0);
glutSolidTeapot(0.8);
glPopMatrix();
}
场景先写到这儿吧,其他物品随意可加的,下一篇写画安卓精灵求个关注
完整源码评论区留邮箱文章来源地址https://www.toymoban.com/news/detail-510348.html
到了这里,关于计算机图形学:绘制一个3d交互场景(1)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!