計算機(jī)圖形學(xué)作業(yè)3_第1頁
計算機(jī)圖形學(xué)作業(yè)3_第2頁
計算機(jī)圖形學(xué)作業(yè)3_第3頁
計算機(jī)圖形學(xué)作業(yè)3_第4頁
計算機(jī)圖形學(xué)作業(yè)3_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、 計算機(jī)圖形學(xué)教師:秦紅星學(xué)生;孔龍班級:0451202學(xué)號2012211766計算機(jī)圖形學(xué)作業(yè)3內(nèi)容:1.利用OpenGL實現(xiàn)光線跟蹤算法。2.實現(xiàn)真實感效果,包括遮擋、陰影、材質(zhì)鏡面反射和漫反射。3.實現(xiàn)交互操作:平移、縮放、旋轉(zhuǎn)。項目代碼:#include"vector.h"class DirLight private: vector Position; vector Direction; vector Ambient; vector Diffuse; vector Specular; public: DirLight(); DirLight(vector pos,v

2、ector dir,vector abt,vector dfs,vector spl); DirLight(); void setPosition(vector p); void setDirection(vector dir); void setAmbient(vector a); void setDiffuse(vector d); void setSpecular(vector s); vector getPosition(); vector getDirection(); vector getAmbient(); vector getDiffuse(); vector getSpecu

3、lar(); vector figAmbient(vector _material_Ka); vector figDiffuse(vector _N,vector _L,vector _material_Kd); vector figSpecular(vector _N,vector _L,vector _V,vector _material_Ks,double _shininess);#ifndef R#define R#include"vector.h"class Ray private:vector Origin;vector Direction; public:Ra

4、y();Ray(vector Or,vector Dir);Ray();void setOrigin(vector setOr);void setDirection(vector setDir);vector getOrigin();vector getDirection();vector getPoint(double t);void show(); #endif#include"Ray.h"#include"DirLight.h"#include"Sphere.h"extern Sphere Qiu;extern DirLight

5、 Light;vector Raytracer(Ray r);#ifndef S#define S#include"Ray.h"class Sphereprivate:vector Center;double Radius;public:Sphere();Sphere(vector c,double r);Sphere();void setCenter(vector c);void setradius(double r);vector getCenter();double getRadius();int isIntersec(Ray r,double &distan

6、ce);#endif#ifndef V#define Vclass vectorprivate:double x;double y;double z;public:vector();vector(double a,double b, double c);vector();void setLocation(double a,double b,double c);void getLocation(double &a,double &b,double &c);void normailize();void show();friend double VecpmulVec(vect

7、or &a,vector &b);friend vector operator + (vector &a,vector &b);friend vector operator - (vector &a,vector &b);friend vector operator * (vector &a,double t); friend vector operator * (vector &a,vector &b); ;#endif#include"DirLight.h" #include<math.h&g

8、t;double max(double a1,double a2) if(a1>a2) return a1; else return a2;DirLight:DirLight() Position=vector(0,0,260); Direction=vector(-1,-1,0); Direction.normailize(); Ambient=vector(0.2,0.2,0.2); Diffuse=vector(0.9,0.9,0.9); Specular=vector(1.0,1.0,1.0);DirLight:DirLight(vector pos,vector dir,vec

9、tor abt,vector dfs,vector spl) Position=pos; Direction=dir; Ambient=abt; Diffuse=dfs; Specular=spl;DirLight:DirLight()void DirLight:setPosition(vector p)Position=p;void DirLight:setDirection(vector dir)Direction=dir;void DirLight:setAmbient(vector a)Ambient=a;void DirLight:setDiffuse(vector d)Diffus

10、e=d;void DirLight:setSpecular(vector s)Specular=s;vector DirLight:getPosition()return Position;vector DirLight:getDirection()return Direction;vector DirLight:getAmbient()return Ambient;vector DirLight:getDiffuse()return Diffuse;vector DirLight:getSpecular()return Specular;vector DirLight:figAmbient(

11、vector _material_Ka) return Ambient*_material_Ka;vector DirLight:figDiffuse(vector _N,vector _L,vector _material_Kd) double dot=VecpmulVec(_N,_L)*0.2;vector diffuse=vector(1,1,0)*vector(1,1,1)*dot;return diffuse;vector DirLight:figSpecular(vector _N,vector _L,vector _V,vector _material_Ks,double _sh

12、ininess)vector _R=_L-_N*VecpmulVec(_N,_L)*2;double dot=VecpmulVec(_V,_R);double spec=pow(dot,20)*0.6;vector specular=vector(1,1,1)*spec;return specular;#include "vector.h"#include <math.h>#include<iostream>using namespace std;vector:vector()vector:vector(double a,double b,doubl

13、e c)x=a;y=b;z=c;vector:vector()void vector:setLocation(double a,double b,double c)x=a;y=b;z=c;void vector:getLocation(double &a,double &b,double &c) a=x; b=y; c=z;void vector:normailize() double f=x*x+y*y+z*z; f=sqrt(f); x=x/f; y=y/f; z=z/f;vector operator + (vector &a,vector &b)

14、return vector(a.x+b.x,a.y+b.y,a.z+b.z);vector operator - (vector &a,vector &b)return vector(a.x-b.x,a.y-b.y,a.z-b.z);vector operator * (vector &a,double t)return vector(a.x*t,a.y*t,a.z*t);vector operator * (vector &a,vector &b)return vector(a.x*b.x,a.y*b.y,a.z*b.z);void vector:sh

15、ow()cout<<"x:"<<x<<endl<<"y:"<<y<<endl<<"z:"<<z<<endl;double VecpmulVec(vector &a,vector &b)double result=a.x*b.x+a.y*b.y+a.z*b.z;return result;#include"Sphere.h"#include<math.h>Sphere:Sphere(

16、) Center=vector(0,0,0); Radius=200;Sphere:Sphere(vector c,double r)Center=c;Radius=r;Sphere:Sphere()void Sphere:setCenter(vector c)Center=c;void Sphere:setradius(double r)Radius=r;vector Sphere:getCenter()return Center;double Sphere:getRadius()return Radius;int Sphere:isIntersec(Ray r,double &di

17、stance)vector v=r.getOrigin()-Center;double b=VecpmulVec(v,r.getDirection()*(-1);double det=(b*b)-VecpmulVec(v,v)+Radius*Radius;int result=0;if(det>0)det=sqrt(det);double t1=b-det;double t2=b+det;if(t2>0)if(t1<0)if(t2<distance)distance=t2;result=-1; elseif(t1<distance)distance=t1;resu

18、lt=1; return result;#include"Raytracer.h"#include"Sphere.h"#include"vector.h"#include<math.h>vector Raytracer(Ray r) vector color(0,0,0); double distance=10000; if(Qiu.isIntersec(r,distance)!=0) vector P=r.getPoint(distance); double shade=1; vector L=Light.getPosi

19、tion()-P; double tdist=pow(VecpmulVec(L,L),0.5); L.normailize(); Ray ptl(P+L*0.00001,L); if(Qiu.isIntersec(ptl,tdist)!=0) shade=0; vector N=P-Qiu.getCenter(); N.normailize(); double dot = VecpmulVec( L, N );if (dot > 0)double diff = dot * 0.6 * shade;color =color+ vector(1,1,1) * vector(1,1,0)*di

20、ff;vector VV=r.getDirection();vector RR= L - N*VecpmulVec( L, N )*2;double dot1 = VecpmulVec( VV, RR );if (dot1 > 0)double spec = pow( dot1, 20 ) * 0.6 * shade;color= color+vector(0.4,0.4,0.4)*spec; return color;#include"Ray.h"#include<iostream>using namespace std;Ray:Ray()Ray:Ray

21、(vector Or,vector Dir)Origin=Or;Direction=Dir;Ray:Ray()void Ray:setOrigin(vector setDir) Origin=setDir;void Ray:setDirection(vector setDir)Direction=setDir;vector Ray:getOrigin() return Origin;vector Ray:getDirection()return Direction;vector Ray:getPoint(double t) vector point;point=Origin+Direction

22、*t;return point;void Ray:show()cout<<"Origin:"<<endl; /Origin.show();cout<<"Direction:"<<endl;Direction.show();#include"Raytracer.h"#include"Sphere.h"#include<gl/glut.h>#include<gl/glaux.h>#pragma comment(lib,"glaux.lib

23、") AUX_RGBImageRec *Image; GLubyte Buffer5005003;Sphere Qiu;DirLight Light;void Initial(void)void Render()vector color;double d_rgb3;int i_rgb3;int i;int j;double x;double y;vector eye(0,0,400);vector currentPixel;vector currentDir;Ray BeginR;BeginR.setOrigin(eye);for(i=0;i<500;i+) for(j=0;j

24、<500;j+) x=-250+j; y=-250+i; currentPixel.setLocation(x,y,300);currentDir=currentPixel-eye;currentDir.normailize();BeginR.setDirection(currentDir);color=Raytracer(BeginR);color=color*256;color.getLocation(d_rgb0,d_rgb1,d_rgb2); i_rgb0=d_rgb0;i_rgb1=d_rgb1;i_rgb2=d_rgb2;if(i_rgb0>255)i_rgb0=255;if(i_rgb1>255)i_rgb1=255;if(i_rgb2>255)i_rgb2=255;Bufferij0=(GLubyte)i_rgb0;Bufferij1=(GLubyte)i_rgb1;Bufferij2=(GLubyte

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論