计算机图形学 java_计算机图形学

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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Eclipse平台JAVA实现 1. 实验内容 用基本增量算法和Bresenham算法画直线 2.实验目的 1)理解在显示器上画图与在纸上画图的本质区别; 2)掌握直线的光栅扫描转换过程; 3)掌握不同算法绘制直线的思路和优缺点。 3. 实验要求 1)将像素网格表现出来,建立网格坐标系; 2)用橡皮筋的形式输入参数; 3)鼠标移动时,显示鼠标当前位置; 4)显示判别式的计算过程和下一点的选择策略; 5)记录生成点的坐标,建议用表的形式; 6)图形生成过程可以重复进行。 1. 实验内容 用正负法和Bresenham算法画圆弧 2.实验目的 1)掌握圆及圆弧的光栅扫描转换过程; 2)掌握不同算法绘制圆弧的技巧和优缺点。 3. 实验要求 1)将像素网格表现出来,建立网格坐标系; 2)用橡皮筋的形式输入参数; 3)鼠标移动时,显示鼠标当前位置; 4)显示判别式的计算过程和下一点的选择策略; 5)记录生成点的坐标,建议用表的形式; 6)图形生成过程可以重复进行。 1. 实验内容 用Cohen-SutherLand算法和liang _barsky算法进行线段裁剪 2.实验目的 1)理解裁剪的相关概念 2)掌握直线段的一般裁剪过程; 3)理解并掌握Cohen-SutherLand 算法的编码思想; 4)理解并掌握Liang_Barsky算法的参数化裁剪思想; 3. 实验要求 1)将像素网格表现出来,建立网格坐标系; 2)用橡皮筋的形式输入剪裁线段和裁剪窗口; 3)鼠标移动时,显示鼠标当前位置; 4)对于线段裁剪,线段被窗口的四条边裁剪的过程要显示出来; 6)裁剪过程可以重复进行。 1. 实验内容 用Sutherland-Hodgman算法进行多边形裁剪 2.实验目的 1)理解多边形裁剪与直线段裁剪的区别; 2)掌握多边形的裁剪过程; 3)理解并掌握Sutherland-Hodgman算法的裁剪思想。 3. 实验要求 1)将像素网格表现出来,建立网格坐标系; 2)用橡皮筋的形式输入剪裁多边形和裁剪窗口; 3)鼠标移动时,显示鼠标当前位置; 4)多边形被窗口的四条边裁剪的过程以及多边形顶点增删的过程要显示出来; 5)裁剪过程可以重复进行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值