地形陰影貼圖快速計算 |
|
axsoft
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:681 回覆:1056 積分:969 註冊:2002-03-13 發送簡訊給我 |
地形陰影貼圖快速計算
by Mircea Marghidanu
翻譯:Dreams woo 資料來源:http://www.gameres.com/Visual/3D/Roam/SceneShadow.htm Introduction
標題地形渲染是一個非常大的標題,本文將集中介紹對地形的光照和陰影的渲染。在下圖中你將看 到這種技術的效果。
![]() ![]() ![]() ![]() int intersect_map(const vector3& iv,const ray& r,Image* hm,float fHeightScale){ int w,hits; float d,h,D; vector3 v,dir; v = iv r.direction; w = hm->w; hits = 0; while (!(( v.x >= w-1 ) || ( v.x <= 0 ) || ( v.z >= w-1 ) || ( v.z <= 0 ))){ // length of lightdir's projection D = Magnitude(vector3(v.x,0,v.z)-vector3(r.origin.x,0,r.origin.z)); d = Magnitude(iv-v); // light direction h = iv.y (d*r.origin.y) / D; // X(P) point // check if height in point P is bigger than point X's height if (hm->data[ifloor(v.z)* w ifloor(v.x)] * fHeightScale > h){ hits ; // if so, mark as hit, and skip this work point. break; }; dir = r.direction; dir.y = 0; v = Normalize(dir); // fetch new working point }; return hits; }; Image* genLightmap(char* normal,Image* hm,vector3 fSunDir,int w,float fAmbient){ int i,j,hits; float f,dot; vector3 n,fVertex; Image* lmap; ray r; float fHeightScale = 10.0f / 255.0f; lmap = new Image(w,w,1); if (!lmap){printf("(!) Error: cannot alloc lightmap!\n");return 0;}; for (j=0; jdata[j*w i] * fHeightScale; fVertex.z = j; f = fAmbient ; r.origin = fVertex fSunDir * 2000.0f; r.direction = fSunDir; // checks current working point for intersection if (!intersect_map(fVertex,r,hm,fHeightScale)){ // compute the lighting equation n.x = (float)(normal[3*(j*w i) 0]); n.y = (float)(normal[3*(j*w i) 1]); n.z = (float)(normal[3*(j*w i) 2]); f = 0.5f*(1.0f DotProduct(Normalize(n),Normalize(fSunDir))); if (f>1.0f) f = 1.0f; }; dot = f * 255.0f; lmap->data[j*w i] = (unsigned char)dot; }; }; return lmap; };在你成功的建立你自己的光照圖後,你或許將要使用它。我將告訴你如何把他添加到你的引擎中, 這裡有兩個方法:一是把他當作光照圖使用,另外一個方法是設置地形坐標的每一個點的顏色為它相 應陰影圖中的顏色。 ![]() ![]() ![]() |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |