Light* Transformation::lightToObject(Light* light) { Light* returnLight; Eigen::Matrix<float,4,4> matrix = inverseMatrix->getMatrix(); if(light->isPointLight()) { Coordinate* lightPos = light->getPosition(); Eigen::Vector4f lightPt(lightPos->getX(), lightPos->getY(), lightPos->getZ(), 1.0f); lightPt = matrix * lightPt; returnLight = new PointLight(new Coordinate(lightPt(0),lightPt(1), lightPt(2)), light->getColor()); } else { Vector* lightDir = light->getDirection()->getOpposite(); Eigen::Vector4f lightVec(lightDir->getX(), lightDir->getY(), lightDir->getZ(), 0.0f); lightVec = matrix * lightVec; Vector* returnDir = new Vector(lightVec(0),lightVec(1), lightVec(2)); returnDir->normalize(); returnLight = new DirectionLight(returnDir, light->getColor()); } return returnLight; }
Vector SpotLight::calcIntensity( Raytracer* rt, HitProperties const& hp ) const { // calc light vec Vector lightVec( getLightVector( hp ) ); // intensity is normal dot light vector double intensity = hp.normal.dot( lightVec ); if( intensity < 0.0 ) return Vector( 0.0, 0.0, 0.0 ); // get light direction Vector lightDir( getMatrix().getForward() ); // calc angle between lightVec and lightDir // multiplied by 2 because we only calculate half angle but need the total angle double angle = abs( acos( lightVec.dot( lightDir.negated() ) ) * RAD2DEG * 2.0 ); // inside spot area? if( angle < outerAngle ) { // inside fall off area? if( angle > innerAngle ) { // calc linear fall off angle = ( angle - innerAngle ) / ( outerAngle - innerAngle ); intensity *= ( 1.0 - angle ); } } else return Vector( 0.0, 0.0, 0.0 ); // construct ray double dist; Ray r( hp.point, getMatrix().getTranslation(), &dist ); if( getShadowing() && rt->getScene()->intersect( r, dist ) ) return Vector( 0.0, 0.0, 0.0 ); // depends on light dir return color * intensity; }
//============================================================================= // 描画処理 //============================================================================= void CMeshField::Draw(int pTexture) { D3DXMATRIX mtxWorld; D3DXMATRIX mtxScl,mtxRot,mtxTranslate; SetMtxView(CManager::GetCamera()->GetMtxView()); // 視錐台 作成 FRUSTUM sFrustum; // セットアップする関数 SetupFOVClipPlanes(VIEW_ANGLE,VIEW_ASPECT,VIEW_NEAR_Z,VIEW_FAR_Z,sFrustum); //頂点バッファの中身を埋める VERTEX_3D *pVtx; // 頂点データの範囲をロックし、頂点バッファへのポインタを取得 m_pD3DVtxBuff->Lock(0, 0, (void**)&pVtx, 0); for(int nCntVtxZ = 0; nCntVtxZ < (m_nNumBlockZ + 1); nCntVtxZ++) { for(int nCntVtxX = 0; nCntVtxX < (m_nNumBlockX + 1); nCntVtxX++) { VECTOR3 pos; // 頂点座標の設定 pos.x=pVtx[nCntVtxZ * (m_nNumBlockX + 1) + nCntVtxX].vtx.x; pos.z=pVtx[nCntVtxZ * (m_nNumBlockX + 1) + nCntVtxX].vtx.z; pos.y=pVtx[nCntVtxZ * (m_nNumBlockX + 1) + nCntVtxX].vtx.y; MATRIX4x4 matrix; matrix._11 = m_MtxView._11; matrix._12 = m_MtxView._12; matrix._13 = m_MtxView._13; matrix._14 = m_MtxView._14; matrix._21 = m_MtxView._21; matrix._22 = m_MtxView._22; matrix._23 = m_MtxView._23; matrix._24 = m_MtxView._24; matrix._31 = m_MtxView._31; matrix._32 = m_MtxView._32; matrix._33 = m_MtxView._33; matrix._34 = m_MtxView._34; matrix._41 = m_MtxView._41; matrix._42 = m_MtxView._42; matrix._43 = m_MtxView._43; matrix._44 = m_MtxView._44; // ??? if(!MeshFOVCheck(&pos,8,sFrustum,matrix)) //if( 判定用関数 ) // 視錐台から、外れていたら { continue; // 描画しないで、次のモデルへ } } } // 頂点データをアンロックする m_pD3DVtxBuff->Unlock(); LPDIRECT3DDEVICE9 pDevice = CManager::GetDevice(); // ワールドマトリックスの初期化 D3DXMatrixIdentity(&m_mtxWorld); // 回転を反映 D3DXMatrixRotationYawPitchRoll(&mtxRot, m_Rot.y, m_Rot.x, m_Rot.z); D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &mtxRot); // 移動を反映 D3DXMatrixTranslation(&mtxTranslate, m_Pos.x, m_Pos.y, m_Pos.z); D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &mtxTranslate); if (m_bTransParent) { pDevice->SetRenderState(D3DRS_ZENABLE, FALSE); // Zバッファを使用しない } else { pDevice->SetRenderState(D3DRS_ZENABLE, TRUE); // Zバッファを使用する } //ワールド行列変数 D3DXMATRIX world, view, proj, rot, pos, mtxParent; D3DXMATRIX invWorld; //ビュー行列 D3DXVECTOR3 eye(0.0f, 50.0f, -100.0f); D3DXVECTOR3 at(0.0f, 20.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXVECTOR3 lightVec(0.20f, 0.0f, 0.80f); D3DXCOLOR lightDiffuse(0.8f, 0.8f, 0.8f, 1.0f); D3DXCOLOR lightDiffuse2(0.8f, 0.4f, 0.4f, 1.0f); D3DXCOLOR lightAmbient(0.5f, 0.5f, 0.5f, 1.0f); CCamera *pCamera; // カメラを取得 pCamera = CManager::GetCamera(); eye = pCamera->GetPosP(); at = pCamera->GetPosR(); up = pCamera->GetVecUp(); pDevice->GetTransform(D3DTS_WORLD, &mtxParent); //ワールド行列 D3DXMatrixIdentity(&m_mtxWorld); D3DXMatrixRotationYawPitchRoll(&rot, m_Rot.y, m_Rot.x, m_Rot.z); D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &rot); D3DXMatrixTranslation(&pos, m_Pos.x, m_Pos.y, m_Pos.z); D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &pos); D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &mtxParent); D3DXMatrixInverse(&invWorld, NULL, &m_mtxWorld); D3DXVec3TransformCoord(&lightVec, &lightVec, &invWorld); D3DXMatrixLookAtLH(&view, &eye, &at, &up); D3DXMatrixPerspectiveFovLH(&proj, D3DXToRadian(45), 960.0f / 540.0f, 10.0f, 100000.0f); D3DXMATRIX wvp = m_mtxWorld*view*proj; D3DXVECTOR3 cameraVec = at - eye; D3DXVec3Normalize(&cameraVec, &cameraVec); D3DXVec3Normalize(&lightVec, &lightVec); D3DXVECTOR3 playerPos = CGame::GetPlayer(0)->GetPos(); // ワールドマトリックスの設定 pDevice->SetTransform(D3DTS_WORLD, &m_mtxWorld); // 頂点バッファをレンダリングパイプラインに設定 pDevice->SetStreamSource(0, m_pD3DVtxBuff, 0, sizeof(VERTEX_3D)); // インデックスバッファをレンダリングパイプラインに設定 pDevice->SetIndices(m_pD3DIndexBuff); // 頂点フォーマットの設定 pDevice->SetFVF(FVF_VERTEX_3D); if (pTexture == NULL) { pDevice->SetRenderState(D3DRS_FOGENABLE, TRUE); pDevice->SetRenderState(D3DRS_FOGCOLOR, D3DCOLOR_XRGB(255, 255, 255)); _vsc[1]->SetMatrix(pDevice, "world", &m_mtxWorld); _vsc[1]->SetMatrix(pDevice, "gWvp", &wvp); _vsc[1]->SetFloatArray(pDevice, "LightDir", (float*)&lightVec, 3); _vsc[1]->SetVector(pDevice, "LightDiffuse", (D3DXVECTOR4*)&lightDiffuse); _vsc[1]->SetVector(pDevice, "LightDiffuse2", (D3DXVECTOR4*)&lightDiffuse2); _vsc[1]->SetVector(pDevice, "LightAmbient", (D3DXVECTOR4*)&lightAmbient); _vsc[1]->SetFloatArray(pDevice, "CameraVec", (float*)&cameraVec, 3); _vsc[1]->SetFloatArray(pDevice, "PlayerPos", (float*)&playerPos, 3); _vsc[1]->SetFloatArray(pDevice, "Pos", (float*)&eye, 3); // テクスチャの設定 pDevice->SetTexture(0, CTexture::GetTex(m_texid)); unsigned int s0 = _psc->GetSamplerIndex("texSampler"); pDevice->SetTexture(s0, CTexture::GetTex(m_texid)); pDevice->SetVertexShader(_vs[1]); } else { _vsc[0]->SetMatrix(pDevice, "world", &m_mtxWorld); _vsc[0]->SetMatrix(pDevice, "gWvp", &wvp); _vsc[0]->SetFloatArray(pDevice, "LightDir", (float*)&lightVec, 3); _vsc[0]->SetVector(pDevice, "LightDiffuse", (D3DXVECTOR4*)&lightDiffuse); _vsc[0]->SetVector(pDevice, "LightDiffuse2", (D3DXVECTOR4*)&lightDiffuse2); _vsc[0]->SetVector(pDevice, "LightAmbient", (D3DXVECTOR4*)&lightAmbient); _vsc[0]->SetFloatArray(pDevice, "CameraVec", (float*)&cameraVec, 3); _vsc[0]->SetFloatArray(pDevice, "PlayerPos", (float*)&playerPos, 3); _vsc[0]->SetFloatArray(pDevice, "Pos", (float*)&m_Pos, 3); // テクスチャの設定 pDevice->SetTexture(0, CTexture::GetTex(pTexture)); unsigned int s0 = _psc->GetSamplerIndex("shadowSampler"); pDevice->SetTexture(s0, CTexture::GetTex(pTexture)); pDevice->SetVertexShader(_vs[0]); } pDevice->SetPixelShader(_ps); // マテリアルの設定 pDevice->SetMaterial(&m_material); // ポリゴンの描画 pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0, 0, m_nNumVertex, 0, m_nNumPolygon); pDevice->SetRenderState(D3DRS_FOGENABLE, FALSE); pDevice->SetRenderState(D3DRS_ZENABLE, TRUE); // Zバッファを使用する pDevice->SetVertexShader(nullptr); pDevice->SetPixelShader(nullptr); }