//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
RendererImplemented::RendererImplemented( int32_t squareMaxCount )
	: m_reference	( 1 )
	, m_device	( NULL )
	, m_context	( NULL )
	, m_vertexBuffer( NULL )
	, m_indexBuffer	( NULL )
	, m_squareMaxCount	( squareMaxCount )
	, m_coordinateSystem	( ::Effekseer::CoordinateSystem::RH )
	, m_renderState		( NULL )
	, m_restorationOfStates( true )

	, m_shader(nullptr)
	, m_shader_no_texture(nullptr)
	, m_shader_distortion(nullptr)
	, m_shader_no_texture_distortion(nullptr)
	, m_standardRenderer(nullptr)

	, m_background(nullptr)
	, m_distortingCallback(nullptr)
{
	::Effekseer::Vector3D direction( 1.0f, 1.0f, 1.0f );
	SetLightDirection( direction );
	::Effekseer::Color lightColor( 255, 255, 255, 255 );
	SetLightColor( lightColor );
	::Effekseer::Color lightAmbient( 0, 0, 0, 0 );
	SetLightAmbientColor( lightAmbient );

	m_state = new OriginalState();

#ifdef __EFFEKSEER_RENDERER_INTERNAL_LOADER__
	EffekseerRenderer::PngTextureLoader::Initialize();
#endif
}
示例#2
0
void Lighter::Light(float colorOut0[4], float colorOut1[4], const float colorIn[4], Vec3f pos, Vec3f norm)
{
    Color4 in(colorIn);

    const Color4 *ambient;
    if (materialUpdate_ & 1)
        ambient = ∈
    else
        ambient = &materialAmbient;

    const Color4 *diffuse;
    if (materialUpdate_ & 2)
        diffuse = ∈
    else
        diffuse = &materialDiffuse;

    const Color4 *specular;
    if (materialUpdate_ & 4)
        specular = ∈
    else
        specular = &materialSpecular;

    Color4 lightSum0 = globalAmbient * *ambient + materialEmissive;
    Color4 lightSum1(0, 0, 0, 0);

    for (int l = 0; l < 4; l++)
    {
        // can we skip this light?
        if (!gstate.isLightChanEnabled(l))
            continue;

        GELightType type = gstate.getLightType(l);

        Vec3f toLight(0,0,0);
        Vec3f lightDir(0,0,0);

        if (type == GE_LIGHTTYPE_DIRECTIONAL)
            toLight = Vec3f(gstate_c.lightpos[l]);  // lightdir is for spotlights
        else
            toLight = Vec3f(gstate_c.lightpos[l]) - pos;

        bool doSpecular = gstate.isUsingSpecularLight(l);
        bool poweredDiffuse = gstate.isUsingPoweredDiffuseLight(l);

        float distanceToLight = toLight.Length();
        float dot = 0.0f;
        float angle = 0.0f;
        float lightScale = 0.0f;

        if (distanceToLight > 0.0f) {
            toLight /= distanceToLight;
            dot = Dot(toLight, norm);
        }
        // Clamp dot to zero.
        if (dot < 0.0f) dot = 0.0f;

        if (poweredDiffuse)
            dot = powf(dot, specCoef_);

        // Attenuation
        switch (type) {
        case GE_LIGHTTYPE_DIRECTIONAL:
            lightScale = 1.0f;
            break;
        case GE_LIGHTTYPE_POINT:
            lightScale = clamp(1.0f / (gstate_c.lightatt[l][0] + gstate_c.lightatt[l][1]*distanceToLight + gstate_c.lightatt[l][2]*distanceToLight*distanceToLight), 0.0f, 1.0f);
            break;
        case GE_LIGHTTYPE_SPOT:
        case GE_LIGHTTYPE_UNKNOWN:
            lightDir = gstate_c.lightdir[l];
            angle = Dot(toLight.Normalized(), lightDir.Normalized());
            if (angle >= gstate_c.lightangle[l])
                lightScale = clamp(1.0f / (gstate_c.lightatt[l][0] + gstate_c.lightatt[l][1]*distanceToLight + gstate_c.lightatt[l][2]*distanceToLight*distanceToLight), 0.0f, 1.0f) * powf(angle, gstate_c.lightspotCoef[l]);
            break;
        default:
            // ILLEGAL
            break;
        }

        Color4 lightDiff(gstate_c.lightColor[1][l], 0.0f);
        Color4 diff = (lightDiff * *diffuse) * dot;

        // Real PSP specular
        Vec3f toViewer(0,0,1);
        // Better specular
        // Vec3f toViewer = (viewer - pos).Normalized();

        if (doSpecular)
        {
            Vec3f halfVec = (toLight + toViewer);
            halfVec.Normalize();

            dot = Dot(halfVec, norm);
            if (dot > 0.0f)
            {
                Color4 lightSpec(gstate_c.lightColor[2][l], 0.0f);
                lightSum1 += (lightSpec * *specular * (powf(dot, specCoef_) * lightScale));
            }
        }

        if (gstate.isLightChanEnabled(l))
        {
            Color4 lightAmbient(gstate_c.lightColor[0][l], 0.0f);
            lightSum0 += (lightAmbient * *ambient + diff) * lightScale;
        }
    }

    // 4?
    for (int i = 0; i < 4; i++) {
        colorOut0[i] = lightSum0[i] > 1.0f ? 1.0f : lightSum0[i];
        colorOut1[i] = lightSum1[i] > 1.0f ? 1.0f : lightSum1[i];
    }
}
示例#3
0
//=============================================================================
// 描画処理
//=============================================================================
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);
}