#include <windows.h>
#include <gl/glut.h>
#include <math.h>
#include<iostream>
using namespace std;
const GLint xPosition = 100;
const GLint yPosition = 100;
const GLint screenWidth = 640;
const GLint screenHeight = 480;
struct GLPoint{
GLdouble x, y;
};
GLPoint pt= {0,0};
GLPoint V[25] = {/*大房子外框架*/{7,23},{14,18},{14,0},{0,0},{0,18},/*烟囱*/{9,21.5},{9,23},{11,23},{11,20},/*大门*/{3,0},{3,10},{6,10},{6,0},
/*大房子窗户*/{12,14},{10,14},{10,16},{12,16},/*小房子*/{14,14},{20,10},{20,0},{14,10},/*小房子窗户*/{19,7},{16,7},{16,9},{19,9}};
GLPoint AfterV[25];
void myDisplay();
void myInit();
void setWindow(GLdouble left, GLdouble right , GLdouble botton, GLdouble top);
void setViewport(GLdouble left, GLdouble right , GLdouble botton, GLdouble top);
void drawBigHouse(GLPoint ptV[]);
void drawSmallHouse(GLPoint ptV[]);
void moveTo(GLPoint);
void LineTo(GLPoint);
void transform2D(GLPoint ptV);
//在程序中使用仿射变换
//house()
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(xPosition,yPosition);
glutInitWindowSize(screenWidth,screenHeight);
glutCreateWindow("House");
setWindow(0,64,0,48);
setViewport(0,screenWidth,0,screenHeight);
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
void setWindow(GLdouble left, GLdouble right , GLdouble botton, GLdouble top){
glMatrixMode(GL_PROJECTION);
//glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(left,right,botton,top);
}
void setViewport(GLdouble left, GLdouble right , GLdouble botton, GLdouble top){
glViewport(left,right,right-left,top-botton);
}
void moveTo(GLPoint p){
pt.x = p.x;
pt.y = p.y;
}
void LineTo(GLPoint p){
glBegin(GL_LINES);
glVertex2d(pt.x,pt.y);
glVertex2d(p.x, p.y);
glEnd();
glFlush();
pt.x= p.x;
pt.y =p.y;
}
void drawSmallHouse(GLPoint ptV[]){
//小房子框架
moveTo(ptV[17]);
LineTo(ptV[18]);
LineTo(ptV[19]);
LineTo(ptV[2]);
LineTo(ptV[20]);
LineTo(ptV[18]);
//小房子窗户
moveTo(ptV[21]);
LineTo(ptV[22]);
LineTo(ptV[23]);
LineTo(ptV[24]);
LineTo(ptV[21]);
}
/************************************************************************/
/* 大房子外框架
{7,23},{14,18},{14,0},{0,0},{0,18},
烟囱
{9,21.5},{9,23},{11,23},{11,20},
大门
{3,0},{3,10},{6,10},{6,0},
大房子窗户
{12,14},{10,14},{10,16},{12,16} */
/************************************************************************/
void drawBigHouse(GLPoint ptV[]){
//外框架
moveTo(ptV[0]);
LineTo(ptV[1]);
LineTo(ptV[2]);
LineTo(ptV[3]);
LineTo(ptV[4]);
LineTo(ptV[0]);
//横梁
moveTo(ptV[1]);
LineTo(ptV[4]);
//烟囱
moveTo(ptV[5]);
LineTo(ptV[6]);
LineTo(ptV[7]);
LineTo(ptV[8]);
//门
moveTo(ptV[9]);
LineTo(ptV[10]);
LineTo(ptV[11]);
LineTo(ptV[12]);
//窗户
moveTo(ptV[13]);
LineTo(ptV[14]);
LineTo(ptV[15]);
LineTo(ptV[16]);
LineTo(ptV[13]);
}
void myDisplay(){
glClear(GL_COLOR_BUFFER_BIT);
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity(); //设置当前变换矩阵为单位矩阵
drawBigHouse(V);
drawSmallHouse(V);
//glMatrixMode(GL_MODELVIEW);
//glScaled(2.0,2.0,1.0); //对CT后乘一个缩放矩阵,x方向上,y方向上缩放2倍。
glMatrixMode(GL_MODELVIEW);
glTranslated(32.0,25.0,1.0); //对CT后乘一个平移矩阵,x方向平移32,y方向平移25
glMatrixMode(GL_MODELVIEW);
glRotated(-30,0.0,0.0,1.0);
drawBigHouse(V);
drawSmallHouse(V);
}
void myInit(){
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glLineWidth(2.0);
}