声明:
由于本人是一名学生,现阶段还要完成学业,所以我们每周假期再回!谢谢大家理解和支持!
上篇上手实践
运行结果
实现代码
#include <windows.h>
#define GLUT_DISABLE_ATEXIT_HACK //处理不同系统的配置问题的宏
#include <GL/glut.h>
#include <stdio.h>
//定义二维数组,其第二维含有两个元素
//调用glVertex2i(houseline[i][0][Winheight-houseline[i][1])
int house_outline[][2]={
{290,1040},{290,1000},{380,870},{470,870},{470,730},{405,730},{405,680},
{455,680},{455,530},{365,525},{365,500},{500,375},{530,375},{700,240},
{730,240},{850,275},{850,190},{880,180},{900,190},{935,175},{980,190},
{980,335},{1080,440},{1080,560},{1200,640},{1200,665},{1150,665},{1150,1040},
{925,1040},{680,1040},{450,1040},{450,1000},{530,890},{620,890},{620,520},
{640,520},{640,480},{685,485},{685,525},{640,520},{640,480},{500,375},{600,380},
{700,490},{685,485},{640,480},{640,520},{620,520},{500,455},{405,525},{455,530},{530,465}
};
int house_chimney[][2]={
{670,520},{670,560},{680,560},{680,1040},{680,560},{730,560},{730,240},{730,560},{765,560},
{765,495},{765,520},{925,630},{925,1040},{925,630},{935,640},{1150,660},{1150,1040},{1150,660},
{1200,665},{1200,640},{935,615},{935,640},{935,615},{765,495},{880,510},{880,180},{880,350},
{850,350},{850,275},{850,350},{730,320},{880,350},{880,510},{900,513},{900,190},{935,175},{935,520},
{900,513},{935,520},{980,535},{980,335},{980,535},{1080,560}
};
int house_door[][2]={
{405,680},{585,680},{585,730},{470,730},{585,730},{585,890},{500,890},{500,730},{550,730},
{550,800},{540,800},{540,780},{540,840},{540,820},{550,820},{550,800},{550,890},{560,890},
{560,730}
};
int house_stairs1[][2]={
{550,890},{470,890},{470,870},{410,870},{320,1000},{425,1000},{510,890}
};
int house_stairs2[][2]={
{290,1040},{600,1040},{500,905},{420,905},{420,905},{420,930},{480,930},{400,930},{400,930},
{400,955},{460,955},{380,955},{380,955},{380,980},{440,980},{360,980},{360,980},{360,1000}
};
int house_windows1[][2]={
{660,310},{660,380},{700,375},{700,305},{660,310},{680,307},{680,377}
};
int house_windows2[][2]={
{470,530},{470,680},{585,680},{585,530},{470,530},{527,530},{527,680}
};
int house_windows3[][2]={
{880,735},{880,910},{730,910},{730,735},{880,735},{880,823},{730,823},
{805,823},{805,735},{805,910}
};
int house_windows4[][2]={
{830,570},{770,570},{770,670},{830,670},{830,570},{800,570},{800,610},
{810,610},{810,630},{810,610},{800,610},{800,670}
};
int house_windows5[][2]={
{950,735},{1120,735},{1150,850},{1120,850},{1120,910},{950,910},{950,735},
{1120,735},{1150,850},{1080,850},{1080,910},{1080,850},{1010,850},{1010,910},
{1010,850},{970,850},{950,735}
};
//自定义划线函数,方便后续重复调用,减少工作量
void DrawLine(float x=1.0,float y=1.0,float z=1.0){
//程序健壮性
if((x||y||z)>1.0 && (x||y||z)<0.0){
printf("ERROR:函数实参范围为[0.0<=(x||y||z)<=1.0]\n");
x=0.0;
y=1.0;
z=1.0;
}
//房子外部轮廓
glBegin(GL_LINE_STRIP);
glColor3f(x, y, z); // 设置线条颜色
for(int i=0;i<52;i++){
glVertex2i(house_outline[i][0],1271-house_outline[i][1]);}
glEnd();
//右侧墙壁和烟囱
glBegin(GL_LINE_STRIP );
glColor3f(x, y, z); // 设置线条颜色
for(int i=0;i<43;i++){
glVertex2i(house_chimney[i][0],1271-house_chimney[i][1]);}
glEnd();
//房子门
glBegin(GL_LINE_STRIP);
glColor3f(x, y, z); // 设置线条颜色
for(int i=0;i<19;i++){
glVertex2i(house_door[i][0],1271-house_door[i][1]);}
glEnd();
//楼梯
glBegin(GL_LINE_STRIP);
glColor3f(x, y, z); // 设置线条颜色
for(int i=0;i<7;i++){
glVertex2i(house_stairs1[i][0],1271-house_stairs1[i][1]);}
glEnd();
glBegin(GL_LINES);
glColor3f(x, y, z); // 设置线条颜色
for(int i=0;i<18;i++){
glVertex2i(house_stairs2[i][0],1271-house_stairs2[i][1]);}
glEnd();
//窗户
glColor3f(x, y, z);// 设置线条颜色
//最上面小窗户
glBegin(GL_LINE_STRIP);
for(int i=0;i<7;i++){
glVertex2i(house_windows1[i][0],1271-house_windows1[i][1]);}
glEnd();
//阳台窗户
glBegin(GL_LINE_STRIP);
for(int i=0;i<7;i++){
glVertex2i(house_windows2[i][0],1271-house_windows2[i][1]);}
glEnd();
//中间大窗户
glBegin(GL_LINE_STRIP);
for(int i=0;i<10;i++){
glVertex2i(house_windows3[i][0],1271-house_windows3[i][1]);}
glEnd();
//中间小窗户
glBegin(GL_LINE_STRIP);
for(int i=0;i<12;i++){
glVertex2i(house_windows4[i][0],1271-house_windows4[i][1]);}
glEnd();
//最右侧窗户
glBegin(GL_LINE_STRIP);
for(int i=0;i<17;i++){
glVertex2i(house_windows5[i][0],1271-house_windows5[i][1]);}
glEnd();
}
void drawHouse(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除屏幕和缓存区
glLineWidth(5); //画笔宽度
glMatrixMode(GL_MODELVIEW); //几何变换函数
//几何变换
glTranslatef(1300,200,0); //平移
DrawLine(0.0,1.0,1.0);
glTranslatef(250,70,0);
glScalef(0.75,0.75,1); //收缩
DrawLine(1.0,0.0,0.0);
glLoadIdentity();
glTranslatef(950,800,0);
glScalef(0.5,0.5,1); //收缩
glRotatef(-20,0.0,0.0,1.0); //旋转
DrawLine(1.0,0.0,1.0);
glLoadIdentity(); //还原源图片
//原图 (白色)
DrawLine();
glFlush();
}
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0); // 设置背景色为黑色
glMatrixMode(GL_PROJECTION); // glPolygonMode(GL_FRONT_AND_BACK ,GL_POINT);
gluOrtho2D(0.0, 2700, 0.0, 1500.0); // 设置二维正交投影坐标轴
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0.0, 50); //绘图窗口与电脑桌面左上角的位置关系
glutInitWindowSize(2700, 1500); // 使用更合适的窗口大小
glutCreateWindow("House-Transformation");
init();
glutDisplayFunc(drawHouse);
glutMainLoop();
return 0;
}
功能简单介绍
这段代码使用OpenGL和GLUT库在Windows平台上绘制了一个房屋的轮廓,并对其进行了几何变换。
主要功能包括: 定义了房屋轮廓、烟囱、门、楼梯和窗户的坐标数组。 实现了DrawLine函数,用于绘制房屋的各个部分,可通过参数设置线条颜色。 drawHouse函数中,使用glClear清除屏幕和缓存区,设置画笔宽度,并进行了平移、缩放和旋转等几何变换,绘制了不同颜色和位置的房屋。 init函数设置了背景色为黑色,并定义了二维正交投影坐标轴。 main函数初始化GLUT,创建窗口,设置显示函数为drawHouse,并进入主循环。
代码展示了OpenGL的基本绘图和变换功能,适用于学习计算机图形学的基础知识。通过修改几何变换参数,可以轻松实现不同的视觉效果。
与上篇实验项目代码比对
新增代码段
glMatrixMode(GL_MODELVIEW); //几何变换函数
//几何变换
glTranslatef(1300,200,0); //平移
DrawLine(0.0,1.0,1.0);
glTranslatef(250,70,0);
glScalef(0.75,0.75,1); //收缩
DrawLine(1.0,0.0,0.0);
glLoadIdentity();
glTranslatef(950,800,0);
glScalef(0.5,0.5,1); //收缩
glRotatef(-20,0.0,0.0,1.0); //旋转
DrawLine(1.0,0.0,1.0);
glLoadIdentity(); //还原源图片
标签:850,openGL,house,640,图形学,730,几何变换,1.0,680
From: https://blog.csdn.net/2303_77716250/article/details/143496827