计算机图形学实验报告-实验5Phong光照模型
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
计算机图形学实验报告
班级计算机工硕班
学号 **********
姓名王泽晶
实验五: Phong光照模型
实验目的
通过本次试验,学生可以掌握简单光照明模型的计算,以及真实感绘制中三维场景搭建的初步知识。
实验内容:
对给定的光源、相机状态,对球进行Phong光照明模型绘制。
搭建三维场景:
a)在三维空间中摆放1个球,半径为R,默认为50 ,摆放位置为(0,0,0)
b)球的材质默认值为Ka = (0.1,0.1,0.1), Kd = (0,0,0.8), Ks = 0.2, n = 10
c)视点方向初始为(0,0,1),光源方向初始为(1,1,1)
d)视口设置为x0 = -100, y0 = -75, w = 200, h = 150
使用phong模型绘制场景
试验步骤:
添加成员函数,编写成员数代码为
override public function computeIntersection( viewStart:Vec3, viewDir:Vec3):Boolean {
// See /geometry/sphereline/
var viewEnd:Vec3 = viewStart.add(viewDir);
var A:Number = Math.pow(viewEnd.getVec(0) - viewStart.getVec(0), 2)
+Math.pow(viewEnd.getVec(1) - viewStart.getVec(1), 2) +
Math.pow(viewEnd.getVec(2) - viewStart.getVec(2), 2);
var B:Number =((viewEnd.getVec(0) - viewStart.getVec(0)) * (viewStart.getVec(0) - _position.getVec(0)) +
(viewEnd.getVec(1) - viewStart.getVec(1)) * (viewStart.getVec(1) -
_position.getVec(1)) +
(viewEnd.getVec(2) - viewStart.getVec(2)) * (viewStart.getVec(2) -
_position.getVec(2))) * 2.0;
var C:Number = Math.pow(_position.getVec(0) - viewStart.getVec(0), 2) + Math.pow(_position.getVec(1) - viewStart.getVec(1), 2) +
Math.pow(_position.getVec(2) - viewStart.getVec(2), 2) - _radius*_radius;
// Solve C + Bt + At^2 = 0
var delta:Number = B*B - 4*A*C;
if ( delta<0.0 || A==0.0 ) return false;
// We don't consider whether 0 var t2:Number = (-B - Math.sqrt(delta)) / (2*A); if ( t1 point = viewStart.multiplyk(1.0 - t1).add(viewEnd.multiplyk(t1)); else point = viewStart.multiplyk(1.0 - t2).add(viewEnd.multiplyk(t2)); normal = Vec3.normalize(point.minus(_position)); return true; } public var _width :Number =0.0; public var _height:Number = 0.0; public var data:Array = new Array(); protected function group1_creationCompleteHandler(event:FlexEvent):void { draw(); } public function draw():void{ graphics.clear(); if(txtViewDir.text == "") return; var ary:Array = txtViewDir.text.split(","); var flag:Boolean = false; for(var i:int= 0;i { if(ary[i] == "" || isNaN(ary[i])) { flag = true; break; } } if(flag) txtViewDir.setStyle("color",0xff0000); return; } txtViewDir.setStyle("color",0x000000); var viewDir:Vec3 = new Vec3(Number(ary[0]), Number(ary[1]), Number(ary[2])); ary = txtLight.text.split(","); flag = false; for(i= 0;i { if(ary[i] == "" || isNaN(ary[i])) { flag = true; break; } } if(flag) { txtLight.setStyle("color",0xff0000); return; } txtLight.setStyle("color",0x000000); var light:Light = new Light(); light.direction = new Vec3(Number(ary[0]), Number(ary[1]), Number(ary[2])).negative(); light.ambient = new Vec3(Number(ary[0]), Number(ary[1]), Number(ary[2])); light.intensity = new Vec3(Number(ary[0]), Number(ary[1]), Number(ary[2])); var material:Material = new Material(); material.diffuse =new Vec3(0.0, 0.0, 0.8); material.specular =new Vec3(0.2, 0.2, 0.2); material.ambient =new Vec3(0.1, 0.1, 0.1); data = createSceneImage( 200, 150, viewDir, light, material ); drawImg(); } public function drawImg():void{ for(var y:int = 0 ;y<150;y++)