计算机图形学实验三:实现四边形的三视图和透视投影图

// 计算机图形学实验三.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"stdio.h"
#include"vector"
#include"iostream"
#include<gl/glut.h>
using namespace std;
const int maxnum = 200;
vector<int>face[10];//最大数,面数vector
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;//dd视点,平移到适当距离nn,mm,ll
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 < pointnum; 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 < facenum; i++)
    {
        int size = face[i].size();
        for (int j = 0; j < size; 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_0(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();
    glColor3f(0.0f, 1.0f, 0.0f);
    glBegin(GL_LINES);
    glVertex2d(temppoint[0].x, temppoint[0].y);
    glVertex2d(0, 0);
    glVertex2d(temppoint[1].x, temppoint[1].y);
    glVertex2d(0, 0);
    glVertex2d(temppoint[2].x, temppoint[2].y);
    glVertex2d(0, 0);
    glVertex2d(temppoint[3].x, temppoint[3].y);
    glVertex2d(0, 0);
    glEnd();
}
//绘制坐标系
void oncoordinate()
{
    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);

    glVertex2d(winwidth/2+5, winheight-15);
    glVertex2d(winwidth/2+15, winheight-15);
    glVertex2d(winwidth/2+5, winheight-25);

    glVertex2d(winwidth/2+15, winheight-15);
    glVertex2d(winwidth/2+5, winheight-25);
    glVertex2d(winwidth/2+15, winheight-25);

    glVertex2d(winwidth/2-5, winheight-5);
    glVertex2d(winwidth/2,winheight);
    glVertex2d(winwidth/2+5, winheight-5);
    glVertex2d(winwidth/2, winheight);

    glVertex2d(winwidth/2+25,0+15);
    glVertex2d(winwidth/2+20,0+10);
    glVertex2d(winwidth/2+15,0+15);
    glVertex2d(winwidth/2+20,0+10);
    glVertex2d(winwidth/2+20,0+10);
    glVertex2d(winwidth/2+20,0+5);

    glVertex2d(winwidth/2-5,0+5);
    glVertex2d(winwidth/2,0);
    glVertex2d(winwidth/2 + 5,0+5);
    glVertex2d(winwidth/2, 0);

    glVertex2d(0+25,winheight/2+15);
    glVertex2d(0+20, winheight/2+10);
    glVertex2d(0+15, winheight/2+15);
    glVertex2d(0+20, winheight/2+10);
    glVertex2d(0+20, winheight/2+10);
    glVertex2d(0+20, winheight/2+5);

    glVertex2d(0+5, winheight/2+5);
    glVertex2d(0, winheight/2);
    glVertex2d(0+ 5, winheight/2-5);
    glVertex2d(0, winheight/2 );

    glVertex2d(winwidth-25, winheight/2 + 15);
    glVertex2d(winwidth-15, winheight/2 + 5);
    glVertex2d(winwidth-25, winheight/2 + 5);
    glVertex2d(winwidth-15, winheight/2 + 15);

    glVertex2d(winwidth-5, winheight/2 -5);
    glVertex2d(winwidth , winheight/2 );
    glVertex2d(winwidth - 5, winheight/2 + 5);
    glVertex2d(winwidth, winheight/2);

    glEnd();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    oncoordinate();
    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_0(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;
}
View Code

 

posted @ 2015-05-07 11:15  Run_For_Love  阅读(1195)  评论(0编辑  收藏  举报