// ■バンプマッピング #include "myView.h" // ラベル #define LB_1 1001 // 方位角 #define LB_2 1002 // 仰角 #define LB_3 1003 // 距離 #define LB_4 1004 // 反射率 //#define LB_5 1005 // 屈折率 //#define LB_6 1006 // 物体色 // エディットボックス #define ED_1 2001 // 方位角 #define ED_2 2002 // 仰角 #define ED_3 2003 // 距離 //#define ED_4 2004 // 反射率 //#define ED_5 2005 // 屈折率 // ボタン #define BT_1 3001 // ボタン1 //#define BT_2 3002 // ボタン2 //#define BT_3 3003 // ボタン3 static HWND bt1;//, bt2, bt3; // 実行指定用ボタン static HWND editA, editB, editC;//, editD, editE;//変換用係数を指定するeditBox static myBitmap txt; //--------------------------------------------------------------------- //■バンプマッピング // 以下の係数を用いる //  diffuse:拡散反射係数, specular: 鏡面反射係数, ambient: 環境光, // (reflect: 反射率, refract: 屈折率, matColor: 物体色)は使わない // Point mapping(Point V, int dir) { //(注意)言語によってasin, acosで求まる値の範囲が異なるので // 適宜 a, e の値を調整すること double a=0, e = (asin((double)dir*(double)V.Z)+PI/2)/PI; double XX=V.X, YY=V.Y; double aa=acos((double)dir*XX/sqrt(XX*XX+YY*YY))/PI/2; double wi=txt.b.bmWidth, hi=txt.b.bmHeight; a=aa; if(V.Y>0) a= aa; //C#のときは不要, VC++, VB用 else if(V.Y<0) a=-aa; //C#のときは不要, VC++, VB用 return setPoint(a*wi+ txt.b.bmWidth/2, e*hi);//VC++用 // return setPoint(a*wi, e*hi); //C#, VBのときはこちら } Point mapping(Point V){return mapping(V, 1);} double mixPixel(HDC hdc, int X1, int Y1){ return 0.5*((double)RED (GetPixel(hdc,X1,Y1))+ (double)GREEN(GetPixel(hdc,X1,Y1))+ (double)BLUE (GetPixel(hdc,X1,Y1)))/3; } void drawBall(){ //球体 HCURSOR hc=LoadCursor(NULL,IDC_WAIT); SetCursor(hc); HDC hdctx =startBitmap(txt); Ball Ba=setBall(setPoint(0,0,0),120); init2D(500,400); origin(250,200); Point L0 = setPoint(200,-200,500); //照明光 Point V0 = setPoint(500,200,200); //視点 setview(V0); for(int x=0;x=txt.b.bmWidth -1) X2=txt.b.bmWidth -1; if(Y2>=txt.b.bmHeight-1) Y2=txt.b.bmHeight-1; double I00=mixPixel(hdctx,X1, Y1); double I10=mixPixel(hdctx,X2, Y1); double I01=mixPixel(hdctx,X1, Y2); Point DX=setPoint(1,0,I10-I00); Point DY=setPoint(0,1,I01-I00); Point F=mlt(rot_z(90),unit(extmlt(DX, DY))); N=mlt(rotate(setPoint(0,0,1),F),N); int c=shading(V,L,N,0xFF77FF); drawPoint(x,y,c); } } DeleteDC(hdctx); hc=LoadCursor(NULL,IDC_ARROW); SetCursor(hc); } //----------------------------------------------------------------------------------------- void initBitMap(HWND hw){// ビットマップ作成とメモリデバイスコンテキスト作成 createBitmap(hw); clearBitmap(); windowSize(hw,700,450); } /*myBitmap createMyBitmap(HWND hw, TCHAR str[]){ myBitmap txt; txt.h=LoadBitmap(hInstance,str);HDC hdc=GetDC(hw); GetObject(txt.h, sizeof(BITMAP), &txt.b); return txt; } HDC startBitmap(){ HDC hdctx =CreateCompatibleDC(hbuf); SelectObject(hdctx,txt.h); SetStretchBltMode(hdctx,COLORONCOLOR); return hdctx; } void drawBitmap(HDC hdctx, myBitmap txt,int x, int y, double SX, double SY){ StretchBlt(hbuf,x,y,(int)(txt.b.bmWidth*SX), (int)(txt.b.bmHeight*SY), hdctx, x,y,txt.b.bmWidth, txt.b.bmHeight, SRCCOPY); } */ void procCreate(HWND hw, WPARAM wp, LPARAM lp){ initBitMap(hw); SetWindowText(hw,TEXT("バンプマッピング(Bump Mapping)")); createLabel(hw,lp,(HMENU) LB_1,510, 45,TEXT("拡散反射係数"));//ラベル生成 createLabel(hw,lp,(HMENU) LB_2,510, 85,TEXT("鏡面反射係数")); createLabel(hw,lp,(HMENU) LB_3,510,120,TEXT("環境光係数")); //createLabel(hw,lp,(HMENU) LB_4,510,160,TEXT("反射率")); //createLabel(hw,lp,(HMENU) LB_5,510,200,TEXT("屈折率")); editA=createEdit(hw,lp,(HMENU) ED_1,620,45,TEXT("0.7"));//エディットボックス生成 editB=createEdit(hw,lp,(HMENU) ED_2,620,85,TEXT("0.2")); editC=createEdit(hw,lp,(HMENU) ED_3,620,120,TEXT("0.3")); //editD=createEdit(hw,lp,(HMENU) ED_3,620,160,TEXT("0.2")); //editE=createEdit(hw,lp,(HMENU) ED_3,620,200,TEXT("1.03")); bt1 =createButton(hw,lp,(HMENU) BT_1,520, 5,TEXT("描画"));//ボタン生成 matColor =0xFF; //最初の画面にテクスチャ画像を表示する。 txt=createMyBitmap(hw, TEXT("bump")); HDC hdctx =startBitmap(txt); drawBitmap(hdctx, txt, 0, 0, 1, 1); DeleteDC(hdctx); } void procPaint(HWND hw, WPARAM wp, LPARAM lp){//ビットマップを画面に表示 PAINTSTRUCT ps; HDC hdc=BeginPaint(hw, &ps); BitBlt(hdc,10,10,490,390,hbuf,0,0,SRCCOPY); EndPaint(hw,&ps); } // diffuse: 拡散反射係数, specular: 鏡面反射係数, ambient: 環境光 // reflect: 反射率, refract: 屈折率, matColor: 物体色 void getValue(){ diffuse= getValue(editA); specular = getValue(editB); ambient = getValue(editC); //reflect= getValue(editD); refract = getValue(editE); } void procBall(HWND hw){//透明描画 clearBitmap(); getValue(); drawBall(); InvalidateRect(hw, NULL, TRUE); } void procCommand(HWND hw, WPARAM wp, LPARAM lp){ switch(LOWORD(wp)){ case BT_1: procBall(hw) ; return; } } LRESULT CALLBACK WndProc(HWND hw, UINT msg, WPARAM wp, LPARAM lp){ switch(msg){ case WM_DESTROY : PostQuitMessage(0) ; return 0; case WM_CREATE : procCreate (hw, wp, lp); return 0; case WM_PAINT : procPaint (hw, wp, lp); return 0; case WM_COMMAND : procCommand(hw, wp, lp); } return DefWindowProc(hw, msg, wp, lp); }