1 #include "stdafx.h"
2 #include"stdio.h"
3 #include"vector"
4 #include"iostream"
5 #include
6 using namespacestd;7 const int maxnum = 200;8 vectorface[10];9 int winwidth = 1000, winheight = 600;10 int pointnum = 4, facenum = 4;11 double matrix[4][4]=
12 {13 { 1, 0, 0, 0},14 { 0, 1, 0, 0},15 { 0, 0, 1, 0},16 { 500, 300, 300, 1}17 };18 double xoz[4][4]=
19 {20 {1,0,0,0},21 {0,0,0,0},22 {0,0,1,0},23 {0,0,0,1}24 };25 double xoy[4][4]26 {27 {1,0,0,0},28 {0,0,-1,0},29 {0,0,0,0},30 {0,0,-50,1}31 };32 double yoz[4][4]33 {34 {0,0,0,0},35 {-1,0,0,0},36 {0,0,1,0},37 {-150,0,0,1}38 };39 double dd = -400, nn = -200, mm = -360, ll = -500;40 double yoy[4][4]=
41 {42 {1,0,0,0},43 {0,1,0,0},44 {0,0,0,1/dd},45 {ll,mm,0,1+nn/dd}46 };47 structdefpoint48 {49 doublex, y, z, tag;50 }point[maxnum], tpoint[maxnum], xozpoint[maxnum],51 xoypoint[maxnum], yozpoint[maxnum], yoypoint[maxnum];52
53 voidthpmidinit()54 {55 pointnum = 4;56 point[0].x = 400, point[0].y = 0, point[0].z = 0, point[0].tag = 1;57 point[1].x = 400, point[1].y = 200, point[1].z = 0, point[1].tag = 1;58 point[2].x = 0, point[2].y = 200, point[2].z = 0, point[2].tag = 1;59 point[3].x =200, point[3].y = 200, point[3].z = 200, point[3].tag = 1;60
61 facenum = 4;62 face[0].push_back(0); face[0].push_back(1); face[0].push_back(2);63 face[1].push_back(0); face[1].push_back(1); face[1].push_back(3);64 face[2].push_back(0); face[2].push_back(2); face[2].push_back(3);65 face[3].push_back(1); face[3].push_back(2); face[3].push_back(3);66 }67
68 void transform(defpoint newpoint[], defpoint oldpoint[], double tran[4][4])69 {70 for (int i = 0; i < pointnum; i++)71 {72 double tx = oldpoint[i].x, ty = oldpoint[i].y, tz = oldpoint[i].z, ttag =oldpoint[i].tag;73 newpoint[i].x = tx*tran[0][0] + ty*tran[1][0] + tz*tran[2][0] + ttag*tran[3][0];74 newpoint[i].y = tx*tran[0][1] + ty*tran[1][1] + tz*tran[2][1] + ttag*tran[3][1];75 newpoint[i].z = tx*tran[0][2] + ty*tran[1][2] + tz*tran[2][2] + ttag*tran[3][2];76 newpoint[i].tag = tx*tran[0][3] + ty*tran[1][3] + tz*tran[2][3] + ttag*tran[3][3];77 if (newpoint[i].tag != 0 && newpoint[i].tag != 1)78 {79 newpoint[i].x /=newpoint[i].tag;80 newpoint[i].y /=newpoint[i].tag;81 newpoint[i].z /=newpoint[i].tag;82 newpoint[i].tag = 1;83 }84 }85 }86
87 void reshape(int w, inth)88 {89 winwidth = w; winheight =h;90 glViewport(0, 0, w, h);91 glMatrixMode(GL_PROJECTION);92 glLoadIdentity();93 gluOrtho2D(0.0, winwidth, 0.0, winheight);94 }95 voidondraw(defpoint temppoint[])96 {97 glBegin(GL_LINES);98 for (int i = 0; i < facenum; i++)99 {100 int size =face[i].size();101 for (int j = 0; j < size; j++)102 {103 glVertex2d(temppoint[face[i][j]].x, temppoint[face[i][j]].z);104 glVertex2d(temppoint[face[i][(j + 1) % size]].x, temppoint[face[i][(j + 1) %size]].z);105 }106 }107 glEnd();108 }109
110 voidondraw_0(defpoint temppoint[])111 {112 glColor3f(1.0f, 0.0f, 0.0f);113 glBegin(GL_LINES);114 glVertex2d(temppoint[0].x, temppoint[0].y);115 glVertex2d(temppoint[1].x, temppoint[1].y);116 glVertex2d(temppoint[0].x, temppoint[0].y);117 glVertex2d(temppoint[2].x, temppoint[2].y);118 glVertex2d(temppoint[0].x, temppoint[0].y);119 glVertex2d(temppoint[3].x, temppoint[3].y);120 glVertex2d(temppoint[1].x, temppoint[1].y);121 glVertex2d(temppoint[2].x, temppoint[2].y);122 glVertex2d(temppoint[1].x, temppoint[1].y);123 glVertex2d(temppoint[3].x, temppoint[3].y);124 glVertex2d(temppoint[2].x, temppoint[2].y);125 glVertex2d(temppoint[3].x, temppoint[3].y);126
127 glEnd();128 glColor3f(0.0f, 1.0f, 0.0f);129 glBegin(GL_LINES);130 glVertex2d(temppoint[0].x, temppoint[0].y);131 glVertex2d(0, 0);132 glVertex2d(temppoint[1].x, temppoint[1].y);133 glVertex2d(0, 0);134 glVertex2d(temppoint[2].x, temppoint[2].y);135 glVertex2d(0, 0);136 glVertex2d(temppoint[3].x, temppoint[3].y);137 glVertex2d(0, 0);138 glEnd();139 }140
141 voidoncoordinate()142 {143 glColor3f(0.0f, 0.0f, 0.0f);144 glBegin(GL_LINES);145
146 glVertex2d(winwidth / 2, 0);147 glVertex2d(winwidth / 2, winheight);148 glVertex2d(0, winheight/2);149 glVertex2d(winwidth, winheight/2);150
151 glVertex2d(winwidth/2+5, winheight-15);152 glVertex2d(winwidth/2+15, winheight-15);153 glVertex2d(winwidth/2+5, winheight-25);154
155 glVertex2d(winwidth/2+15, winheight-15);156 glVertex2d(winwidth/2+5, winheight-25);157 glVertex2d(winwidth/2+15, winheight-25);158
159 glVertex2d(winwidth/2-5, winheight-5);160 glVertex2d(winwidth/2,winheight);161 glVertex2d(winwidth/2+5, winheight-5);162 glVertex2d(winwidth/2, winheight);163
164 glVertex2d(winwidth/2+25,0+15);165 glVertex2d(winwidth/2+20,0+10);166 glVertex2d(winwidth/2+15,0+15);167 glVertex2d(winwidth/2+20,0+10);168 glVertex2d(winwidth/2+20,0+10);169 glVertex2d(winwidth/2+20,0+5);170
171 glVertex2d(winwidth/2-5,0+5);172 glVertex2d(winwidth/2,0);173 glVertex2d(winwidth/2 + 5,0+5);174 glVertex2d(winwidth/2, 0);175
176 glVertex2d(0+25,winheight/2+15);177 glVertex2d(0+20, winheight/2+10);178 glVertex2d(0+15, winheight/2+15);179 glVertex2d(0+20, winheight/2+10);180 glVertex2d(0+20, winheight/2+10);181 glVertex2d(0+20, winheight/2+5);182
183 glVertex2d(0+5, winheight/2+5);184 glVertex2d(0, winheight/2);185 glVertex2d(0+ 5, winheight/2-5);186 glVertex2d(0, winheight/2);187
188 glVertex2d(winwidth-25, winheight/2 + 15);189 glVertex2d(winwidth-15, winheight/2 + 5);190 glVertex2d(winwidth-25, winheight/2 + 5);191 glVertex2d(winwidth-15, winheight/2 + 15);192
193 glVertex2d(winwidth-5, winheight/2 -5);194 glVertex2d(winwidth , winheight/2);195 glVertex2d(winwidth - 5, winheight/2 + 5);196 glVertex2d(winwidth, winheight/2);197
198 glEnd();199 }200
201 voiddisplay()202 {203 glClear(GL_COLOR_BUFFER_BIT);204 oncoordinate();205 glColor3f(0.0f, 0.0f, 0.0f);206 glBegin(GL_LINES);207 glVertex2d(winwidth/2,0);208 glVertex2d(winwidth/2, winheight);209 glVertex2d(0, winheight/2);210 glVertex2d(winwidth, winheight / 2);211 glEnd();212
213 glColor3f(1.0f, 0.0f, 0.0f);214 ondraw(xozpoint);215 glColor3f(0.0f, 1.0f, 0.0f);216 ondraw(xoypoint);217 glColor3f(0.0f, 0.0f, 1.0f);218 ondraw(yozpoint);219 glColor3f(1.0f, 0.0f, 0.0f);220 ondraw_0(yoypoint);221 glutSwapBuffers();222 }223
224 voidgetthpmidview()225 {226 transform(xozpoint, point, xoz);227 transform(xoypoint, point, xoy);228 transform(yozpoint, point, yoz);229 transform(yoypoint, point, yoy);230 transform(xozpoint, xozpoint, matrix);231 transform(xoypoint, xoypoint, matrix);232 transform(yozpoint, yozpoint, matrix);233 transform(yoypoint, yoypoint, matrix);234 }235 voidinitial()236 {237 for (int i = 0; i < 10; i++)238 face[i].clear();239 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);240 thpmidinit();241 getthpmidview();242 }243 int main(int argc, char *argv[])244 {245 glutInit(&argc, argv);246 glutInitDisplayMode(GLUT_DOUBLE |GLUT_RGB);247 glutInitWindowSize(1000, 600);248 glutInitWindowPosition(150, 100);249 glutCreateWindow("三维图形 透视投影图&三视图 演示程序");250 glutDisplayFunc(display);251 glutReshapeFunc(reshape);252 initial();253 glutMainLoop();254 return 0;255 }