三视图代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include "stdafx.h"
#include "stdlib.h"
#include
#include
#include
using namespace std;

const int MaxNum = 200;vectorFace[10];
int winWidth = 1000, winHeight = 600;
int PointNum = 4, FaceNum = 4;
double Matrix[4][4] = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 },
{ 0, 0, 1, 0 }, { 500, 300, 300, 1 } };
double XOZ[4][4] = { { 1, 0, 0, 0 }, { 0, 0, 0, 0 },
{ 0, 0, 1, 0 }, { 0, 0, 0, 1 } };
double XOY[4][4] = { { 1, 0, 0, 0 }, { 0, 0, -1, 0 },
{ 0, 0, 0, 0 }, { 0, 0, -50, 1 } };
double YOZ[4][4] = { { 0, 0, 0, 0 }, { -1, 0, 0, 0 },
{ 0, 0, 1, 0 }, { -150, 0, 0, 1 } };
double DD= -400, NN = -200, MM = -360, LL = -500;
double YOY[4][4] = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 },
{ 0, 0, 0, 1 / DD }, { LL, MM, 0, 1 + NN / DD } };
struct DefPoint
{
double x, y, z, tag;
}Point[MaxNum], TPoint[MaxNum], XOZPoint[MaxNum], XOYPoint[MaxNum],
YOZPoint[MaxNum],YOYPoint[MaxNum];
void ThPmidInit()
{
PointNum = 4;
Point[0].x = 400, Point[0].y = 0, Point[0].z = 0, Point[0].tag = 1;
Point[1].x = 400, Point[1].y = 200, Point[1].z = 0, Point[1].tag = 1;
Point[2].x = 0, Point[2].y = 200, Point[2].z = 0, Point[2].tag = 1;
Point[3].x = 200, Point[3].y = 200, Point[3].z = 200, Point[3].tag = 1;
FaceNum = 4;
Face[0].push_back(0); Face[0].push_back(1); Face[0].push_back(2);
Face[1].push_back(0); Face[1].push_back(1); Face[1].push_back(3);
Face[2].push_back(0); Face[2].push_back(2); Face[2].push_back(3);
Face[3].push_back(1); Face[3].push_back(2); Face[3].push_back(3);
}
void TransForm(DefPoint NewPoint[], DefPoint OldPoint[], double Tran[4][4])
{
for (int i = 0; i{
double tx = OldPoint[i].x, ty = OldPoint[i].y,
tz = OldPoint[i].z, ttag = OldPoint[i].tag;
NewPoint[i].x = tx*Tran[0][0] + ty*Tran[1][0] +
tz*Tran[2][0] + ttag*Tran[3][0];
NewPoint[i].y = tx*Tran[0][1] + ty*Tran[1][1] +
tz*Tran[2][1] + ttag*Tran[3][1];
NewPoint[i].z = tx*Tran[0][2] + ty*Tran[1][2] +
tz*Tran[2][2] + ttag*Tran[3][2];
NewPoint[i].tag = tx*Tran[0][3] + ty*Tran[1][3] +
tz*Tran[2][3] + ttag*Tran[3][3];
if (NewPoint[i].tag != 0 && NewPoint[i].tag != 1)
{
NewPoint[i].x /= NewPoint[i].tag,
NewPoint[i].y /= NewPoint[i].tag,
NewPoint[i].z /= NewPoint[i].tag,
NewPoint[i].tag = 1;
}
}
}
void ReShape(int w, int h)
{
winWidth = w; winHeight = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, winWidth, 0.0, winHeight);
}
void OnDraw(DefPoint TempPoint[])
{
glBegin(GL_LINES);
for (int i = 0; i{
int size = Face[i].size();
for (int j = 0; j{
glVertex2d(TempPoint[Face[i][j]].x,
TempPoint[Face[i][j]].z);
glVertex2d(TempPoint[Face[i][(j + 1) % size]].x,
TempPoint[Face[i][(j + 1) % size]].z);
}
}
glEnd();
}
void OnDraw_O(DefPoint TempPoint[])
{
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex2d(TempPoint[0].x, TempPoint

[0].y);
glVertex2d(TempPoint[1].x, TempPoint[1].y);
glVertex2d(TempPoint[0].x, TempPoint[0].y);
glVertex2d(TempPoint[2].x, TempPoint[2].y);
glVertex2d(TempPoint[0].x, TempPoint[0].y);
glVertex2d(TempPoint[3].x, TempPoint[3].y);
glVertex2d(TempPoint[1].x, TempPoint[1].y);
glVertex2d(TempPoint[2].x, TempPoint[2].y);
glVertex2d(TempPoint[1].x, TempPoint[1].y);
glVertex2d(TempPoint[3].x, TempPoint[3].y);
glVertex2d(TempPoint[2].x, TempPoint[2].y);
glVertex2d(TempPoint[3].x, TempPoint[3].y);
glEnd();
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex2d(winWidth / 2, 0);
glVertex2d(winWidth / 2, winHeight);
glVertex2d(0, winHeight / 2);
glVertex2d(winWidth, winHeight / 2);
glEnd();

glColor3f(1.0f, 0.0f, 0.0f);
OnDraw(XOZPoint);
glColor3f(0.0f, 1.0f, 0.0f);
OnDraw(XOYPoint);
glColor3f(0.0f, 0.0f, 1.0f);
OnDraw(YOZPoint);
glColor3f(1.0f, 0.0f, 0.0f);
OnDraw_O(YOYPoint);
glutSwapBuffers();
}
void GetThPmidView()
{
TransForm(XOZPoint, Point, XOZ);
TransForm(XOYPoint, Point, XOY);
TransForm(YOZPoint, Point, YOZ);
TransForm(YOYPoint, Point, YOY);
TransForm(XOZPoint, XOZPoint, Matrix);
TransForm(XOYPoint, XOYPoint, Matrix);
TransForm(YOZPoint, YOZPoint, Matrix);
TransForm(YOYPoint, YOYPoint, Matrix);
}
void Initial()
{
for (int i = 0; i<10; i++)Face[i].clear();
glClearColor(1.0f,1.0f, 1.0f, 1.0f);
ThPmidInit();
GetThPmidView();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1000, 600);
glutInitWindowPosition(150, 100);
glutCreateWindow("三视图");
glutDisplayFunc(Display);
glutReshapeFunc(ReShape);
Initial();
glutMainLoop();
return 0;
}

相关文档
最新文档