Esempio n. 1
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);
}
Esempio n. 2
0
void Earth::draw() const {
    Camera* cam = static_cast<Camera*>(getGame()->getObjectByName("cam"));

//    mat4f t = glm::scale(fullTransform, vec3f(radius));
//    mat4f modelViewProjectionMatrix = cam->projection * cam->view * t;
//    mat4f modelViewMatrix =  t;
//    mat4f normalMatrix( glm::transpose(glm::inverse(modelViewMatrix)));

//    vec3f lightPos = vec3f(0,0,0); // parentObject->pos;

//    Texture* earth_day = TextureManager::get("earth_daytime");
//    earth_day->bind();
//    sphere.program->uniform("sampler")->set(2);

//    Texture* earth_night = TextureManager::get("earth_nighttime");
//    earth_night->bind();
//    sphere.program->uniform("EarthNight")->set(3);

//    Texture* earth_cloud = TextureManager::get("earth_cloud");
//    earth_cloud->bind();
//    sphere.program->uniform("EarthCloudGloss")->set(4);

//     Texture* earth_specular_map = TextureManager::get("earth_specular");
//     earth_specular_map->bind();
//     sphere.program->uniform("EarthSpecularMap")->set(5);

//    sphere.program->uniform("LightPosition")->set(lightPos);
//    sphere.program->uniform("MVPMatrix")->set(modelViewProjectionMatrix);
//    sphere.program->uniform("MVMatrix")->set(modelViewMatrix);
//    sphere.program->uniform("NormalMatrix")->set(normalMatrix);

    mat4f projection = cam->projection;
    mat4f view = cam->view;
    mat4f model = glm::scale(fullTransform, getScale());
    mat4f t = projection*view*model;
    mat4f normalMatrix( glm::transpose(glm::inverse(model)));

    vec3f lightPos = vec3f(0.0f);// - position;
    float shininess = 10.0f;

    vec3f emission = vec3f(0.0f);
    //vec3f specular = vec3f(0.5f);
    vec3f specular = vec3f(1.0f, 0.9255f, 0.698f)*0.3f;
    vec3f lightAmbient = vec3f(0.0f);
    vec3f lightDiffuse(1.0f);
    vec3f lightSpecular(1.0f);

    mat4f viewModel = glm::inverse(view*model);
    vec4f camPos = viewModel[3];
    mat4f iModel = ( (glm::inverse(model)));
    vec3f cameraPos = vec3f(camPos); //(vec3f) (iModel*(vec4f(cam->getPosition(), 0)));//****  //vec3f(model*vec4f(cam->getPosition(),1.0));// 
    float Kr = 0.0025f;
    float Km = 0.0010f;
    float ESun = 2.f;
    float fScale = 1.f/(outerRadius-innerRadius);
    float fScaleDepth = 0.25f; //Must be 25%
    float fCameraHeight = glm::length(cameraPos);
    float g = -0.750f; // Mie aerosol scattering constant
    float g2 = g*g;
    vec3f wavelength = vec3f(0.650f, 0.570f, 0.475f);
    vec3f v3InvWavelength = vec3f(1.0f / powf(wavelength.x, 4.0f), 1.0f / powf(wavelength.y, 4.0f), 1.0f / powf(wavelength.z, 4.0f));
    vec3f lightPos_v2 = glm::normalize(mul4(iModel, -getPosition()));
    // vec3f lightPos = vec3f(0.f);
    sphere.program->uniform("v3CameraPos")->set(cameraPos);       // The camera's current position
    sphere.program->uniform("v3LightPos")->set(lightPos_v2);        // The direction vector to the light source
    sphere.program->uniform("v3InvWavelength")->set(v3InvWavelength);   // 1 / pow(wavelength, 4) for the red, green, and blue channels
    // sphere.program->uniform("fCameraHeight")->set(fCameraHeight); // The camera's current height
    sphere.program->uniform("fCameraHeight2")->set(fCameraHeight*fCameraHeight); // fCameraHeight^2
    sphere.program->uniform("fOuterRadius")->set(outerRadius);                   // The outer (spheresphere) radius
    sphere.program->uniform("fOuterRadius2")->set(outerRadius*outerRadius);      // fOuterRadius^2
    sphere.program->uniform("fInnerRadius")->set(innerRadius);                   // The inner (planetary) radius
    // sphere.program->uniform("fInnerRadius2")->set(innerRavec3f(0.0f);//*******  //dius*innerRadius);      // fInnerRadius^2
    sphere.program->uniform("fKrESun")->set(Kr*ESun);                            // Kr * ESun
    sphere.program->uniform("fKmESun")->set(Km*ESun);                            // Kr * ESun
    sphere.program->uniform("fKr4PI")->set(Kr*4.f*PI);                           // Kr * 4 * PI
    sphere.program->uniform("fKm4PI")->set(Km*4.f*PI);                           // Km * 4 * PI
    sphere.program->uniform("fScale")->set(fScale);                              // 1 / (fOuterRadius - fInnerRadius)
    sphere.program->uniform("fScaleDepth")->set(fScaleDepth);                    // The scale depth (i.e. the altitude at which the spheresphere's average density is found)
    sphere.program->uniform("fScaleOverScaleDepth")->set(fScale / (fScaleDepth) );  // fScale / fScaleDepth


    sphere.program->uniform("lightPos")->set(lightPos);
    sphere.program->uniform("shininess")->set(shininess);
    sphere.program->uniform("emission")->set(emission);
    sphere.program->uniform("specular")->set(specular);
    sphere.program->uniform("lightAmbient")->set(lightAmbient);
    sphere.program->uniform("lightDiffuse")->set(lightDiffuse);
    //sphere.program->uniform("lightSpecular")->set(lightSpecular);

    Texture* tex;
    tex = Textures.get("earth");
    tex->bind();
    sphere.program->uniform("sampler")->set((int)tex->getSlot());

    tex = Textures.get("earthNight");
    tex->bind();
    sphere.program->uniform("samplerNight")->set((int)tex->getSlot());

    tex = Textures.get("earthWater");
    tex->bind();
    sphere.program->uniform("samplerWater")->set((int)tex->getSlot());

    tex = Textures.get("earthWaterTex");
    tex->bind();
    sphere.program->uniform("samplerWaterTex")->set((int)tex->getSlot());


    tex = Textures.get("earthNormal");
    tex->bind();
    sphere.program->uniform("samplerNormal")->set((int)tex->getSlot());
    
    tex = Textures.get("earthClouds");
    tex->bind();
    sphere.program->uniform("samplerCloud")->set((int)tex->getSlot());
    
    sphere.program->uniform("modelViewProjectionMatrix")->set(t);
    sphere.program->uniform("modelMatrix")->set(model);
    sphere.program->uniform("viewMatrix")->set(view);
    sphere.program->uniform("normalMatrix")->set(normalMatrix);
    sphere.program->uniform("globaltime")->set(time);


    //glDisable(GL_CULL_FACE);
    //glDepthMask(GL_FALSE);
    sphere.draw();
    //glDepthMask(GL_TRUE);
    //glEnable(GL_CULL_FACE);


}