0001: #include "stdafx.h"
0002: #include "math.h"
0003: #include "prim.h"
0004: #include "render.h"
0005: 
0006: namespace Render {
0007: 
0008: D3DXVECTOR3 BG_COLOR = D3DXVECTOR3(0.25f, 0.4f, 0.5f);
0009: 
0010: class CCamera
0011: {
0012: private:
0013:     D3DXMATRIX  m_mView;
0014:     D3DXMATRIX  m_mProj;
0015:     D3DXMATRIX  m_mVP;
0016:     D3DXMATRIX  m_mVP_Inv;
0017:     D3DXMATRIX  m_mView_Inv;
0018: 
0019:     D3DXVECTOR3 m_vFrom;
0020:     D3DXVECTOR3 m_vLookat;
0021:     D3DXVECTOR3 m_vUp;
0022: 
0023:     float   m_fFovy;
0024:     float   m_fAspect;
0025:     float   m_fNear;
0026:     float   m_fFar;
0027: 
0028: public:
0029: 
0030:     void Update();
0031:     void SetFrom  (const D3DXVECTOR3 *p){m_vFrom  =*p;}
0032:     void SetLookAt(const D3DXVECTOR3 *p){m_vLookat=*p;}
0033:     void SetUp    (const D3DXVECTOR3 *p){m_vUp    =*p;}
0034:     void SetFovY  (float val){m_fFovy   = val;}
0035:     void SetAspect(float val){m_fAspect = val;}
0036:     void SetNear  (float val){m_fNear   = val;}
0037:     void SetFar   (float val){m_fFar    = val;}
0038: 
0039:     inline const D3DXMATRIX *GetViewInverse() const {return &m_mView_Inv;}
0040:     inline const D3DXMATRIX *GetViewProjInverse() const {return &m_mVP_Inv;}
0041:     inline void GetFrom (D3DXVECTOR3 *dest) const {*dest = m_vFrom;}
0042: };
0043: 
0044: 
0045: // ---------------------------------------------------------------------------
0046: // オブジェクト
0047: // ---------------------------------------------------------------------------
0048: char s_data[4*RENDER_WIDTH*RENDER_HEIGHT];// 仮想フレームバッファ
0049: CCamera camera;
0050: 
0051: CMesh *pRoom       = NULL;
0052: CMesh *pBlockSmall = NULL;
0053: CMesh *pBlockTall  = NULL;
0054: CMesh *pShpereS    = NULL;
0055: CMesh *pShpereT    = NULL;
0056: 
0057: static int s_x = 0;
0058: static int s_y = 0;
0059: static int s_state = 0;
0060: enum{
0061:     STATE_IDLE=0,
0062:     STATE_RENDER,
0063: };
0064: 
0065: // ---------------------------------------------------------------------------
0066: // 玉
0067: OBJ_DATA sphere_data[] = {
0068: {// 小さいのに乗ってるほう
0069:     OBJ_TYPE_SPHERE,
0070:     {
0071:         {0.01f, 0.05f, 0.02f },
0072:         {0.01f, 0.05f, 0.02f },
0073:         {0.10f, 0.10f, 0.10f },
0074:         16.0f,
0075:     },{{0,0,0},{0,0,0},{0,0,0}},{
0076:         {185.5, 165+100,169},
0077:         100.0f,
0078:     }
0079: },{// 大きいのに乗ってるほう
0080:     OBJ_TYPE_SPHERE,
0081:     {
0082:         {0.02f, 0.01f, 0.05f},
0083:         {0.02f, 0.01f, 0.05f},
0084:         {0.10f, 0.10f, 0.10f },
0085:         8.0f,
0086:     },{{0,0,0},{0,0,0},{0,0,0}},{
0087:         {368.5, 330+100,351},
0088:         100.0f,
0089:     }
0090: }};
0091: // ---------------------------------------------------------------------------
0092: // 部屋
0093: OBJ_DATA room_data[12] = {
0094: {// ライト1
0095:     OBJ_TYPE_TRIANGLE,
0096:     {
0097:         {100.0f, 100.0f, 100.0f},
0098:         {  0.0f,   0.0f,   0.0f},
0099:         {  0.0f,   0.0f,   0.0f},
0100:         0.0f,
0101:     },{
0102:         {213.0, 548.799f, 227.0},
0103:         {213.0, 548.799f, 332.0},
0104:         {343.0, 548.799f, 227.0},
0105:     }
0106: },{// ライト2
0107:     OBJ_TYPE_TRIANGLE,
0108:     {
0109:         {100.0f, 100.0f, 100.0f},
0110:         {  0.0f,   0.0f,   0.0f},
0111:         {  0.0f,   0.0f,   0.0f},
0112:         0.0f,
0113:     },{
0114:         {343.0, 548.799f, 227.0},
0115:         {213.0, 548.799f, 332.0},
0116:         {343.0, 548.799f, 332.0},
0117:     }
0118: },{// 左1
0119:     OBJ_TYPE_TRIANGLE,
0120:     {
0121:         {0.05f, 0.01f, 0.01f},
0122:         {0.05f, 0.01f, 0.01f},
0123:         {0.00f, 0.00f, 0.00f},
0124:         32.0f,
0125:     },{
0126:         {552.8f,   0.0f,   0.0f},
0127:         {556.0f, 548.8f,   0.0f},
0128:         {549.6f,   0.0f, 559.2f},
0129:     }
0130: 
0131: },{// 左2
0132:     OBJ_TYPE_TRIANGLE,
0133:     {
0134:         {0.05f, 0.01f, 0.01f},
0135:         {0.05f, 0.01f, 0.01f},
0136:         {0.00f, 0.00f, 0.00f},
0137:         32.0f,
0138:     },{
0139:         {549.6f,   0.0f, 559.2f},
0140:         {556.0f, 548.8f,   0.0f},
0141:         {556.0f, 548.8f, 559.2f},
0142:     }
0143: 
0144: },{// 右1
0145:     OBJ_TYPE_TRIANGLE,
0146:     {
0147:         {0.01f, 0.01f, 0.05f},
0148:         {0.01f, 0.01f, 0.05f},
0149:         {0.00f, 0.00f, 0.00f},
0150:         32.0f,
0151:     },{
0152:         {0.0f,   0.0f,   0.0f},
0153:         {0.0f,   0.0f, 559.2f},
0154:         {0.0f, 548.8f,   0.0f},
0155:     }
0156: 
0157: },{// 右2
0158:     OBJ_TYPE_TRIANGLE,
0159:     {
0160:         {0.01f, 0.01f, 0.05f},
0161:         {0.01f, 0.01f, 0.05f},
0162:         {0.00f, 0.00f, 0.00f},
0163:         32.0f,
0164:     },{
0165:         {0.0f, 548.8f,   0.0f},
0166:         {0.0f,   0.0f, 559.2f},
0167:         {0.0f, 548.8f, 559.2f},
0168:     }
0169: 
0170: },{// 奥1
0171:     OBJ_TYPE_TRIANGLE,
0172:     {
0173:         {0.05f, 0.05f, 0.05f},
0174:         {0.05f, 0.05f, 0.05f},
0175:         {0.00f, 0.00f, 0.00f},
0176:         32.0f,
0177:     },{
0178:         {  0.0f,   0.0f, 559.2f},
0179:         {549.6f,   0.0f, 559.2f},
0180:         {  0.0f, 548.8f, 559.2f},
0181:     }
0182: 
0183: },{// 奥2
0184:     OBJ_TYPE_TRIANGLE,
0185:     {
0186:         {0.05f, 0.05f, 0.05f},
0187:         {0.05f, 0.05f, 0.05f},
0188:         {0.00f, 0.00f, 0.00f},
0189:         32.0f,
0190:     },{
0191:         {  0.0f, 548.8f, 559.2f},
0192:         {549.6f,   0.0f, 559.2f},
0193:         {556.0f, 548.8f, 559.2f},
0194:     }
0195: 
0196: },{// 天井1
0197:     OBJ_TYPE_TRIANGLE,
0198:     {
0199:         {0.05f, 0.05f, 0.05f},
0200:         {0.05f, 0.05f, 0.05f},
0201:         {0.00f, 0.00f, 0.00f},
0202:         32.0f,
0203:     },{
0204:         {  0.0f, 548.8f,   0.0f},
0205:         {  0.0f, 548.8f, 559.2f},
0206:         {556.0f, 548.8f,   0.0f},
0207:     }
0208: 
0209: },{// 天井2
0210:     OBJ_TYPE_TRIANGLE,
0211:     {
0212:         {0.05f, 0.05f, 0.05f},
0213:         {0.05f, 0.05f, 0.05f},
0214:         {0.00f, 0.00f, 0.00f},
0215:         32.0f,
0216:     },{
0217:         {  0.0f, 548.8f, 559.2f},
0218:         {556.0f, 548.8f, 559.2f},
0219:         {556.0f, 548.8f,   0.0f},
0220:     }
0221: 
0222: },{// 床1
0223:     OBJ_TYPE_TRIANGLE,
0224:     {
0225:         {0.05f, 0.05f, 0.05f},
0226:         {0.05f, 0.05f, 0.05f},
0227:         {0.00f, 0.00f, 0.00f},
0228:         32.0f,
0229:     },{
0230:         {  0.0f, 0.0f,   0.0f},
0231:         {552.8f, 0.0f,   0.0f},
0232:         {  0.0f, 0.0f, 559.2f},
0233:     }
0234: },{// 床1
0235:     OBJ_TYPE_TRIANGLE,
0236:     {
0237:         {0.05f, 0.05f, 0.05f},
0238:         {0.05f, 0.05f, 0.05f},
0239:         {0.00f, 0.00f, 0.00f},
0240:         32.0f,
0241:     },{
0242:         {  0.0f, 0.0f, 559.2f},
0243:         {552.8f, 0.0f,   0.0f},
0244:         {549.6f, 0.0f, 559.2f},
0245:     }
0246: }};
0247: 
0248: // ---------------------------------------------------------------------------
0249: // 低い箱
0250: OBJ_DATA Short_block[] = {
0251: {
0252:     // 床
0253:     OBJ_TYPE_TRIANGLE,
0254:     {
0255:         {0.05f, 0.05f, 0.05f},
0256:         {0.05f, 0.05f, 0.05f},
0257:         {0.00f, 0.00f, 0.00f},
0258:         32.0f,
0259:     },{
0260:         { 82.0f, 165.0f, 225.0f},
0261:         {130.0f, 165.0f,  65.0f},
0262:         {240.0f, 165.0f, 272.0f},
0263:     }
0264: },{
0265:     OBJ_TYPE_TRIANGLE,
0266:     {
0267:         {0.05f, 0.05f, 0.05f},
0268:         {0.05f, 0.05f, 0.05f},
0269:         {0.00f, 0.00f, 0.00f},
0270:         32.0f,
0271:     },{
0272:         {290.0, 165.0, 114.0},
0273:         {240.0, 165.0, 272.0},
0274:         {130.0, 165.0,  65.0},
0275:     }
0276: },{//側面
0277:     OBJ_TYPE_TRIANGLE,
0278:     {
0279:         {0.05f, 0.05f, 0.05f},
0280:         {0.05f, 0.05f, 0.05f},
0281:         {0.00f, 0.00f, 0.00f},
0282:         32.0f,
0283:     },{
0284:         {290.0, 165.0, 114.0},
0285:         {290.0,   0.0, 114.0},
0286:         {240.0, 165.0, 272.0},
0287:     }
0288: },{
0289:     OBJ_TYPE_TRIANGLE,
0290:     {
0291:         {0.05f, 0.05f, 0.05f},
0292:         {0.05f, 0.05f, 0.05f},
0293:         {0.00f, 0.00f, 0.00f},
0294:         32.0f,
0295:     },{
0296:         {240.0, 165.0, 272.0},
0297:         {290.0,   0.0, 114.0},
0298:         {240.0,   0.0, 272.0},
0299:     }
0300: },{//前面
0301:     OBJ_TYPE_TRIANGLE,
0302:     {
0303:         {0.05f, 0.05f, 0.05f},
0304:         {0.05f, 0.05f, 0.05f},
0305:         {0.00f, 0.00f, 0.00f},
0306:         32.0f,
0307:     },{
0308:         {290.0,   0.0, 114.0},
0309:         {290.0, 165.0, 114.0},
0310:         {130.0,   0.0,  65.0},
0311:     }
0312: },{
0313:     OBJ_TYPE_TRIANGLE,
0314:     {
0315:         {0.05f, 0.05f, 0.05f},
0316:         {0.05f, 0.05f, 0.05f},
0317:         {0.00f, 0.00f, 0.00f},
0318:         32.0f,
0319:     },{
0320:         {130.0,   0.0,  65.0},
0321:         {290.0, 165.0, 114.0},
0322:         {130.0, 165.0,  65.0},
0323:     }
0324: },{//側面
0325:     OBJ_TYPE_TRIANGLE,
0326:     {
0327:         {0.05f, 0.05f, 0.05f},
0328:         {0.05f, 0.05f, 0.05f},
0329:         {0.00f, 0.00f, 0.00f},
0330:         32.0f,
0331:     },{
0332:         { 82.0,   0.0, 225.0},
0333:         {130.0, 165.0,  65.0},
0334:         { 82.0, 165.0, 225.0},
0335:     }
0336: },{
0337:     OBJ_TYPE_TRIANGLE,
0338:     {
0339:         {0.05f, 0.05f, 0.05f},
0340:         {0.05f, 0.05f, 0.05f},
0341:         {0.00f, 0.00f, 0.00f},
0342:         32.0f,
0343:     },{
0344:         { 82.0,   0.0, 225.0},
0345:         {130.0,   0.0,  65.0},
0346:         {130.0, 165.0,  65.0},
0347:     }
0348: },{//背面
0349:     OBJ_TYPE_TRIANGLE,
0350:     {
0351:         {0.05f, 0.05f, 0.05f},
0352:         {0.05f, 0.05f, 0.05f},
0353:         {0.00f, 0.00f, 0.00f},
0354:         32.0f,
0355:     },{
0356:         {240.0,   0.0, 272.0},
0357:         { 82.0, 165.0, 225.0},
0358:         {240.0, 165.0, 272.0},
0359:     }
0360: },{
0361:     OBJ_TYPE_TRIANGLE,
0362:     {
0363:         {0.05f, 0.05f, 0.05f},
0364:         {0.05f, 0.05f, 0.05f},
0365:         {0.00f, 0.00f, 0.00f},
0366:         32.0f,
0367:     },{
0368:         {240.0,   0.0, 272.0},
0369:         { 82.0,   0.0, 225.0},
0370:         { 82.0, 165.0, 225.0},
0371:     }
0372: }};
0373: 
0374: // ---------------------------------------------------------------------------
0375: // 高い箱
0376: OBJ_DATA Tall_block[] = {
0377: {
0378:     // 床
0379:     OBJ_TYPE_TRIANGLE,
0380:     {
0381:         {0.05f, 0.05f, 0.05f},
0382:         {0.05f, 0.05f, 0.05f},
0383:         {0.00f, 0.00f, 0.00f},
0384:         32.0f,
0385:     },{
0386:         {423.0f, 330.0f, 247.0f},
0387:         {314.0f, 330.0f, 456.0f},
0388:         {265.0f, 330.0f, 296.0f},
0389:     }
0390: },{
0391:     OBJ_TYPE_TRIANGLE,
0392:     {
0393:         {0.05f, 0.05f, 0.05f},
0394:         {0.05f, 0.05f, 0.05f},
0395:         {0.00f, 0.00f, 0.00f},
0396:         32.0f,
0397:     },{
0398:         {423.0f, 330.0f, 247.0f},
0399:         {472.0f, 330.0f, 406.0f},
0400:         {314.0f, 330.0f, 456.0f},
0401:     }
0402: },{//側面
0403:     OBJ_TYPE_TRIANGLE,
0404:     {
0405:         {0.05f, 0.05f, 0.05f},
0406:         {0.05f, 0.05f, 0.05f},
0407:         {0.00f, 0.00f, 0.00f},
0408:         32.0f,
0409:     },{
0410:         {423.0f,   0.0f, 247.0f},
0411:         {472.0f,   0.0f, 406.0f},
0412:         {423.0f, 330.0f, 247.0f},
0413:     }
0414: },{
0415:     OBJ_TYPE_TRIANGLE,
0416:     {
0417:         {0.05f, 0.05f, 0.05f},
0418:         {0.05f, 0.05f, 0.05f},
0419:         {0.00f, 0.00f, 0.00f},
0420:         32.0f,
0421:     },{
0422:         {423.0f, 330.0f, 247.0f},
0423:         {472.0f,   0.0f, 406.0f},
0424:         {472.0f, 330.0f, 406.0f},
0425:     }
0426: },{//背面
0427:     OBJ_TYPE_TRIANGLE,
0428:     {
0429:         {0.05f, 0.05f, 0.05f},
0430:         {0.05f, 0.05f, 0.05f},
0431:         {0.00f, 0.00f, 0.00f},
0432:         32.0f,
0433:     },{
0434:         {314.0f,   0.0f, 456.0f},
0435:         {314.0f, 330.0f, 456.0f},
0436:         {472.0f,   0.0f, 406.0f},
0437:     }
0438: },{
0439:     OBJ_TYPE_TRIANGLE,
0440:     {
0441:         {0.05f, 0.05f, 0.05f},
0442:         {0.05f, 0.05f, 0.05f},
0443:         {0.00f, 0.00f, 0.00f},
0444:         32.0f,
0445:     },{
0446:         {314.0f, 330.0f, 456.0f},
0447:         {472.0f, 330.0f, 406.0f},
0448:         {472.0f,   0.0f, 406.0f},
0449:     }
0450: },{//側面
0451:     OBJ_TYPE_TRIANGLE,
0452:     {
0453:         {0.05f, 0.05f, 0.05f},
0454:         {0.05f, 0.05f, 0.05f},
0455:         {0.00f, 0.00f, 0.00f},
0456:         32.0f,
0457:     },{
0458:         {314.0f,   0.0f, 456.0f},
0459:         {265.0f,   0.0f, 296.0f},
0460:         {265.0f, 330.0f, 296.0f},
0461:     }
0462: },{
0463:     OBJ_TYPE_TRIANGLE,
0464:     {
0465:         {0.05f, 0.05f, 0.05f},
0466:         {0.05f, 0.05f, 0.05f},
0467:         {0.00f, 0.00f, 0.00f},
0468:         32.0f,
0469:     },{
0470:         {314.0f,   0.0f, 456.0f},
0471:         {265.0f, 330.0f, 296.0f},
0472:         {314.0f, 330.0f, 456.0f},
0473:     }
0474: },{//前面
0475:     OBJ_TYPE_TRIANGLE,
0476:     {
0477:         {0.05f, 0.05f, 0.05f},
0478:         {0.05f, 0.05f, 0.05f},
0479:         {0.00f, 0.00f, 0.00f},
0480:         32.0f,
0481:     },{
0482:         {265.0f,   0.0f, 296.0f},
0483:         {423.0f,   0.0f, 247.0f},
0484:         {265.0f, 330.0f, 296.0f},
0485:     }
0486: },{
0487:     OBJ_TYPE_TRIANGLE,
0488:     {
0489:         {0.05f, 0.05f, 0.05f},
0490:         {0.05f, 0.05f, 0.05f},
0491:         {0.00f, 0.00f, 0.00f},
0492:         32.0f,
0493:     },{
0494:         {265.0f, 330.0f, 296.0f},
0495:         {423.0f,   0.0f, 247.0f},
0496:         {423.0f, 330.0f, 247.0f},
0497:     }
0498: }};
0499: 
0500: 
0501: // ---------------------------------------------------------------------------
0502: // 関数宣言
0503: // ---------------------------------------------------------------------------
0504: D3DXVECTOR3 *GetColor(D3DXVECTOR3 *dest, float x, float y);
0505: 
0506: 
0507: // ---------------------------------------------------------------------------
0508: // カメラクラス
0509: // ---------------------------------------------------------------------------
0510: void CCamera::Update()
0511: {
0512:     D3DXMatrixLookAtLH( &m_mView, &m_vFrom, &m_vLookat, &m_vUp );
0513:     D3DXMatrixPerspectiveFovLH( &m_mProj, m_fFovy, m_fAspect, m_fNear, m_fFar );
0514:     m_mVP = m_mView * m_mProj;
0515: 
0516:     D3DXMatrixInverse( &m_mView_Inv, NULL, &m_mView);
0517:     D3DXMatrixInverse( &m_mVP_Inv, NULL, &m_mVP);
0518: }
0519: // ---------------------------------------------------------------------------
0520: char *GetDataPointer()
0521: {
0522:     return s_data;
0523: }
0524: 
0525: // ---------------------------------------------------------------------------
0526: void Init()
0527: {
0528:     camera.SetFrom  (&D3DXVECTOR3(278,273,-800));
0529:     camera.SetLookAt(&D3DXVECTOR3(278,273,0));
0530:     camera.SetUp    (&D3DXVECTOR3(0,1,0));
0531:     camera.SetFovY  (D3DX_PI/4);
0532:     camera.SetAspect(1.0f);
0533:     camera.SetNear  (0.01f);
0534:     camera.SetFar   (100.0f);
0535:     camera.Update();
0536: 
0537:     pRoom = new CMesh();
0538:     pRoom->Init( room_data, 12 );
0539:     
0540:     pBlockSmall = new CMesh();
0541:     pBlockSmall->Init( Short_block, 10);
0542: 
0543:     pBlockTall = new CMesh();
0544:     pBlockTall->Init( Tall_block, 10);
0545:     
0546:     pShpereS = new CMesh();
0547:     pShpereS->Init( sphere_data, 1);
0548:     
0549:     pShpereT = new CMesh();
0550:     pShpereT->Init( sphere_data+1,1);
0551: 
0552:     Begin();// フレームバッファ等の初期化
0553: }
0554: // ---------------------------------------------------------------------------
0555: void Delete()
0556: {
0557:     if(pShpereT){delete pShpereT;pShpereT=NULL;}
0558:     if(pShpereS){delete pShpereS;pShpereS=NULL;}
0559:     if(pBlockTall){delete pBlockTall;pBlockTall=NULL;}
0560:     if(pBlockSmall){delete pBlockSmall;pBlockSmall=NULL;}
0561:     if(pRoom){delete pRoom; pRoom=NULL;}
0562: }
0563: // ---------------------------------------------------------------------------
0564: void Begin()
0565: {
0566:     s_state = STATE_RENDER;
0567:     
0568:     s_x = 0;
0569:     s_y = 0;
0570: 
0571:     // フレームバッファの初期化
0572:     for(int j=0; j<RENDER_HEIGHT; j++){
0573:     for(int i=0; i<RENDER_WIDTH ; i++){
0574:         s_data[4*(j*RENDER_WIDTH+i)+0]=(char)255;// R
0575:         s_data[4*(j*RENDER_WIDTH+i)+1]=(char)(i*256/RENDER_WIDTH );// G
0576:         s_data[4*(j*RENDER_WIDTH+i)+2]=(char)(j*256/RENDER_HEIGHT);// B
0577:     }
0578:     }
0579: }
0580: // ---------------------------------------------------------------------------
0581: int Render()
0582: {
0583:     if(STATE_IDLE == s_state) return 0;
0584:     
0585:     D3DXVECTOR3 col;
0586:     GetColor(&col, ((float)s_x+0.5f)/(float)RENDER_WIDTH
0587:                  , ((float)s_y+0.5f)/(float)RENDER_HEIGHT);
0588:     s_data[4*(s_y*RENDER_WIDTH+s_x)+0]=(char)(255.9*min(1,col.x));// R
0589:     s_data[4*(s_y*RENDER_WIDTH+s_x)+1]=(char)(255.9*min(1,col.y));// G
0590:     s_data[4*(s_y*RENDER_WIDTH+s_x)+2]=(char)(255.9*min(1,col.z));// B
0591:     
0592:     // 次のピクセルに移動
0593:     if(RENDER_WIDTH<=++s_x){
0594:         // 行を変える
0595:         s_x = 0;
0596:         if(RENDER_HEIGHT<=++s_y){
0597:             s_state = STATE_IDLE;// 終了
0598:             return 0;
0599:         }
0600:     }
0601: 
0602:     return -1;
0603: }
0604: 
0605: // ---------------------------------------------------------------------------
0606: D3DXVECTOR3 *GetColor(D3DXVECTOR3 *dest, D3DXVECTOR4 *x, D3DXVECTOR4 *v)
0607: {
0608:     D3DXVECTOR4 light_pos = D3DXVECTOR4(278.f, 548.8f, 279.5f,1);
0609:     
0610:     float t = CPrimitive::INFINTY_DIST;
0611:     CPrimitive *pObj = NULL;
0612:     D3DXVECTOR4 p, ptmp;
0613:     D3DXVECTOR4 n, ntmp;
0614:     
0615:     t = pShpereS   ->IsAcross(t, &n, &p, &pObj, x, v);
0616:     t = pShpereT   ->IsAcross(t, &n, &p, &pObj, x, v);
0617:     t = pRoom      ->IsAcross(t, &n, &p, &pObj, x, v);
0618:     t = pBlockSmall->IsAcross(t, &n, &p, &pObj, x, v);
0619:     t = pBlockTall ->IsAcross(t, &n, &p, &pObj, x, v);
0620: 
0621:     if( pObj ){
0622:         // 当たった!
0623: 
0624:         D3DXVECTOR4 l = light_pos-p;
0625:         float L2 = D3DXVec3Length((D3DXVECTOR3 *)&l);
0626:         D3DXVec3Normalize((D3DXVECTOR3 *)&l, (D3DXVECTOR3 *)&l);
0627: 
0628:         D3DXVECTOR3 dir, H;
0629:         // 視線の計算
0630:         camera.GetFrom(&dir);
0631:         dir = dir - *(D3DXVECTOR3 *)&p;
0632:         D3DXVec3Normalize(&dir, &dir);
0633:         // ハーフベクトル
0634:         H = dir+*(D3DXVECTOR3 *)&l;
0635:         D3DXVec3Normalize((D3DXVECTOR3 *)&H, (D3DXVECTOR3 *)&H);
0636: 
0637:         float LN = D3DXVec3Dot((D3DXVECTOR3 *)&l, (D3DXVECTOR3 *)&n);
0638:         float HN = D3DXVec3Dot((D3DXVECTOR3 *)&H, (D3DXVECTOR3 *)&n);
0639:         if(HN<0) HN=0;
0640:         if(0<LN){
0641:             // 当たったところから、光源が見えるかを判定
0642:             // 見えなければ、暗くする
0643:             bool bShadow = FALSE;
0644:             if(!bShadow) bShadow = pShpereS   ->IsAcross(L2, &p, &l);
0645:             if(!bShadow) bShadow = pShpereT   ->IsAcross(L2, &p, &l);
0646:             if(!bShadow) bShadow = pRoom      ->IsAcross(L2, &p, &l);
0647:             if(!bShadow) bShadow = pBlockSmall->IsAcross(L2, &p, &l);
0648:             if(!bShadow) bShadow = pBlockTall ->IsAcross(L2, &p, &l);
0649:             if(bShadow) LN*=0.0f;// difuseだけなくす
0650:         }else{
0651:             LN=0;
0652:         }
0653:         
0654:         pObj->GetColor(dest, LN, HN);
0655:         
0656:         // 光源の色の反映
0657:         D3DXVECTOR3 light_color = D3DXVECTOR3(10.f,9.0f,5.0f);
0658:         dest->x *= light_color.x;
0659:         dest->y *= light_color.y;
0660:         dest->z *= light_color.z;
0661:         
0662:         // 光の強さの適当な補正
0663:         *dest *= min(1.5f, 500000.0f/(10000.0f+L2)); // 距離による補正
0664:         *dest *= min(1, l.y+0.1f);                  // 光の向きをcosθの関数にする
0665:     }else{
0666:         // 外れ
0667:         *dest = BG_COLOR;
0668:     }
0669: 
0670: 
0671:     return dest;
0672: }
0673: // ---------------------------------------------------------------------------
0674: D3DXVECTOR3 *GetColor(D3DXVECTOR3 *dest, float x, float y)
0675: {
0676:     D3DXVECTOR4 ray_start_proj = D3DXVECTOR4(-2*x+1, -2*y+1, 0, 1);// 前方クリップ面
0677:     D3DXVECTOR4 ray_eye        = D3DXVECTOR4(0, 0, 0, 1);// ビュー空間でのカメラの位置
0678:     D3DXVECTOR4 ray_start;
0679:     D3DXVECTOR4 ray_to;
0680:     D3DXVECTOR4 ray_dir;
0681:     D3DXMATRIX mInv;
0682:     
0683:     // 視点を射影空間からワールド座標に変換する
0684:     D3DXVec4Transform( &ray_start, &ray_eye,        camera.GetViewInverse() );
0685:     D3DXVec4Transform( &ray_to,    &ray_start_proj, camera.GetViewProjInverse() );
0686:     D3DXVec4Scale( &ray_to,    &ray_to,    1.0f/ray_to.w   );// w=1 の射影空間に落とす
0687:     // 向きの計算
0688:     ray_dir = ray_to - ray_start;
0689:     D3DXVec4Normalize(&ray_dir, &ray_dir);
0690:     
0691:     GetColor(dest, &ray_start, &ray_dir);
0692: 
0693:     return dest;
0694: }
0695: 
0696: };// namespace Render
0697: