Esempio n. 1
0
bool InitResourceDX9(void)
{
	g_orient_matrix.Identity();

	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	if ( !ReInitResourceDX9() )
		return false;

	g_pPostEffect = GutLoadFXShaderDX9("../../shaders/Posteffect.fx");
	if ( NULL==g_pPostEffect )
		return false;

	g_pWaterEffect = GutLoadFXShaderDX9("../../shaders/Watereffect.fx");
	if ( NULL==g_pWaterEffect )
		return false;

	g_pWaterTexture = GutLoadTexture_DX9("../../textures/lena.tga");
	if ( NULL==g_pWaterTexture )
		return false;

	g_Model_DX9.ConvertToDX9Model(&g_Model);

	return true;
}
Esempio n. 2
0
bool InitResourceDX9(void)
{
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_DirectX(90.0f, 1.0f, 0.1f, 100.0f);
	// 設定視角轉換矩陣
	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &projection_matrix);
	// 關閉打光
	device->SetRenderState(D3DRS_LIGHTING, FALSE);
	// 改變三角形正面的面向
	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);

	int num_vertices = sizeof(g_pyramid_vertices)/sizeof(Vertex_VC);

	// 把RGBA的顏色資料轉換成DX9 BGRA格式
	for ( int i=0; i<num_vertices; i++ )
	{
		unsigned char temp = g_pyramid_vertices[i].m_RGBA[0];
		g_pyramid_vertices[i].m_RGBA[0] = g_pyramid_vertices[i].m_RGBA[2];
		g_pyramid_vertices[i].m_RGBA[2] = temp;
	}

	num_vertices = sizeof(g_road_vertices)/sizeof(Vertex_VC);
	for ( int i=0; i<num_vertices; i++ )
	{
		unsigned char temp = g_road_vertices[i].m_RGBA[0];
		g_road_vertices[i].m_RGBA[0] = g_road_vertices[i].m_RGBA[2];
		g_road_vertices[i].m_RGBA[2] = temp;
	}

	return true;
}
Esempio n. 3
0
// `使用Direct3D9來繪圖`
void RenderFrameDX9(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// `消除畫面`
	device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
	// `開始下繪圖指令`
	device->BeginScene(); 
	// `計算出一個可以轉換到鏡頭座標系的矩陣`
	Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
	device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix);

	// `設定資料格式`
	// D3DFVF_XYZ = `使用3個浮點數來記錄位置`
	// D3DFVF_DIFFUSE = `使用32bits整數型態來記錄BGRA顏色`
	device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE); 

	// `太陽`
	device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &g_sun_matrix);
	device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, 
		g_pSphereIndices, D3DFMT_INDEX16, g_pSunVertices, sizeof(Vertex_VC) );
	// `地球`
	device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &g_earth_matrix);
	device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, 
		g_pSphereIndices, D3DFMT_INDEX16, g_pEarthVertices, sizeof(Vertex_VC) );
	// `月亮`
	device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &g_moon_matrix);
	device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, g_iNumSphereVertices, g_iNumSphereTriangles, 
		g_pSphereIndices, D3DFMT_INDEX16, g_pMoonVertices, sizeof(Vertex_VC) );

	// `宣告所有的繪圖指令都下完了`
	device->EndScene(); 

	// `把背景backbuffer的畫面呈現出來`
	device->Present( NULL, NULL, NULL, NULL );
}
Esempio n. 4
0
bool InitResourceDX9(void)
{
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// 設定視角轉換矩陣
	int w, h;
	GutGetWindowSize(w, h);
	float aspect = (float) h / (float) w;
	Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_DirectX(g_fFovW, aspect, 0.1f, 100.0f);
	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &projection_matrix);
	// 關閉打光
	device->SetRenderState(D3DRS_LIGHTING, FALSE);
	// 畫出正向跟反向的三角形
	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	// 載入貼圖
	g_pTexture = GutLoadTexture_DX9("../../textures/lena_rgb.tga");
	if ( NULL==g_pTexture )
		return false;
	// trilinear
	device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

	return true;
}
Esempio n. 5
0
// 使用Direct3D9來繪圖
void RenderFrameDX9(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	device->SetRenderState( D3DRS_LIGHTING, FALSE );
	device->Clear(
		0, NULL, // 清除整個畫面 
		D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, // 清除顏色跟Z Buffer
		D3DCOLOR_ARGB(0, 0, 0, 0), // 設定要把顏色清成黑色
		1.0f, // 設定要把Z值清為1, 也就是離鏡頭最遠.
		0 // 設定要把Stencil buffer清為0, 在這沒差.
		);

	// 計算出一個可以轉換到鏡頭座標系的矩陣
	Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
	// 計算出一個使用非平行投影的矩陣
	Matrix4x4 perspective_matrix = GutMatrixPerspectiveRH_DirectX(90.0f, 1.0f, 1.0f, 100.0f);
	// 把這兩個矩陣相乘
	Matrix4x4 view_perspective_matrix = view_matrix * perspective_matrix;
	// 把空間中的座標點轉換到螢幕座標系上
	Vector4 vertices[16];
	for ( int i=0; i<16; i++ )
	{
		vertices[i] = g_vertices[i] * view_perspective_matrix;
		vertices[i] /= vertices[i].GetW();
	}

	device->BeginScene(); // 開始下繪圖指令
	device->SetFVF(D3DFVF_XYZ); // 設定資料格式
	// 畫出金字塔的8條邊線
	device->DrawPrimitiveUP(D3DPT_LINELIST, 8, vertices, sizeof(Vector4)); 
	device->EndScene(); // 宣告所有的繪圖指令都下完了

	// 把背景backbuffer的畫面呈現出來
	device->Present( NULL, NULL, NULL, NULL );
}
Esempio n. 6
0
void ResizeWindowDX9(int width, int height)
{
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// Reset Device
	GutResetGraphicsDeviceDX9();
	// 設定視角轉換矩陣
	int w, h;
	GutGetWindowSize(w, h);
	float aspect = (float) h / (float) w;
	Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_DirectX(g_fFovW, aspect, 0.1f, 100.0f);
	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &projection_matrix);
	// 關閉打光
	device->SetRenderState(D3DRS_LIGHTING, FALSE);
	// 畫出正向跟反向的三角形
	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	// trilinear
	device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

	// trilinear
	device->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
}
Esempio n. 7
0
// 使用DirectX 9來繪圖
void RenderFrameDX9(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	if ( g_bWireframe )
	{
		device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
	}
	else
	{
		device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
	}

	switch(g_iMode)
	{
	default:
	case 1:
		Antialiasing_None();
		break;
	case 2:
		Antialiasing_MultiSampling();
		break;
	case 3:
		Antialiasing_SuperSampling();
		break;
	}
}
Esempio n. 8
0
void DrawScreenQuad(sImageInfo *pInfo, float x0, float y0, float width, float height)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	int w = pInfo->m_iWidth;
	int h = pInfo->m_iHeight;

	float fTexelW = 1.0f/(float)w;
	float fTexelH = 1.0f/(float)h;

	Vertex_VT quad[4];
	memcpy(quad, g_FullScreenQuad, sizeof(quad));

	quad[0].m_Position[0] = x0;
	quad[0].m_Position[1] = y0;

	quad[1].m_Position[0] = x0 + width;
	quad[1].m_Position[1] = y0;

	quad[2].m_Position[0] = x0;
	quad[2].m_Position[1] = y0 + height;

	quad[3].m_Position[0] = x0 + width;
	quad[3].m_Position[1] = y0 + height;

	for ( int i=0; i<4; i++ )
	{
		quad[i].m_Texcoord[0] += fTexelW*0.5f;
		quad[i].m_Texcoord[1] += fTexelH*0.5f;
	}

	device->SetFVF(D3DFVF_XYZ|D3DFVF_TEX1);
	device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(Vertex_VT));
}
Esempio n. 9
0
void AutoExposure(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	D3DXHANDLE shader = g_pExposureEffect->GetTechniqueByName("AutoExposure");
	g_pExposureEffect->SetTechnique(shader);

	Vector4 vMiddleGray(0.5f);
	Vector4 vMultiplierClamp(0.2f, 3.0f, 0.0f, 0.0f);

	D3DXHANDLE middlegray_var = g_pExposureEffect->GetParameterByName(NULL, "vMiddleGray");
	D3DXHANDLE clamp_var = g_pExposureEffect->GetParameterByName(NULL, "vMultiplierClamp");
	D3DXHANDLE image0_var = g_pExposureEffect->GetParameterByName(NULL, "Image");
	D3DXHANDLE image1_var = g_pExposureEffect->GetParameterByName(NULL, "Image2");

	g_pExposureEffect->SetVector(middlegray_var, (D3DXVECTOR4*)&vMiddleGray);
	g_pExposureEffect->SetVector(clamp_var, (D3DXVECTOR4*)&vMultiplierClamp);

	g_pExposureEffect->SetTexture(image0_var, g_pFrameBuffer[FULLSIZE]);
	g_pExposureEffect->SetTexture(image1_var, g_pFrameBuffer[LUMINANCE_TEMP]);

	g_pExposureEffect->Begin(NULL, 0);
	g_pExposureEffect->BeginPass(0);

	DrawFullScreenQuad(&g_ImageInfo);

	g_pExposureEffect->EndPass();
	g_pExposureEffect->End();
}
Esempio n. 10
0
// `使用Direct3D9來繪圖`
void RenderFrameDX9(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// `消除畫面`
	device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
	// `開始下繪圖指令`
	device->BeginScene(); 
	// `設定資料格式`
	device->SetFVF(D3DFVF_XYZ | D3DFVF_NORMAL);
	// `套用shader`
	device->SetVertexShader(g_pSelected_VS);
	device->SetPixelShader(g_pVertexColor_PS);
	// `設定光源`
	SetupLightingDX9();
	// `設定轉換矩陣`
	Matrix4x4 world_view_proj_matrix = g_world_matrix * g_view_proj_matrix;
	device->SetVertexShaderConstantF(0, &world_view_proj_matrix[0][0], 4);
	device->SetVertexShaderConstantF(4, &g_world_matrix[0][0], 4);
	// `鏡頭位置, 計算Specular會用到.`
	device->SetVertexShaderConstantF(8, &g_eye[0], 1);
	// `畫出格子`
	device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLESTRIP, 0, g_iNumGridVertices, g_iNumGridTriangles, 
		g_pGridIndices, D3DFMT_INDEX16, g_pGridVertices, sizeof(Vertex_V3N3) );
	// `宣告所有的繪圖指令都下完了`
	device->EndScene(); 
	// `把背景backbuffer的畫面呈現出來`
	device->Present( NULL, NULL, NULL, NULL );
}
Esempio n. 11
0
bool InitResourceDX9(void)
{
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// 設定視角轉換矩陣
	g_projection_matrix = GutMatrixPerspectiveRH_DirectX(g_fFOV, 1.0f, 0.1f, 100.0f);
	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &g_projection_matrix);

	// 關閉打光
	device->SetRenderState(D3DRS_LIGHTING, FALSE);
	// 改變三角形正面的面向
	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	device->CreateTexture(512, 512, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_pTexture, NULL);
	device->CreateDepthStencilSurface(512, 512, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, FALSE, &g_pDepthStencil, NULL);

	CGutModel::SetTexturePath("../../textures/");
	g_Model_DX9.ConvertToDX9Model(&g_Model);

	device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);

	device->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_NONE);

	device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);

	return true;
}
Esempio n. 12
0
bool InitResourceDX9(void)
{
	// `取得Direct3D9裝置`
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// `載入Pixel Shader`
	g_pVertexColor_PS = GutLoadPixelShaderDX9_HLSL("../../shaders/vertex_lighting_directional.shader", "PS", "ps_2_0");
	if ( g_pVertexColor_PS==NULL )
		return false;
	// `載入Directional Light Vertex Shader`
	g_pDirLight_VS = GutLoadVertexShaderDX9_HLSL("../../shaders/vertex_lighting_directional.shader", "VS", "vs_1_1");
	if ( g_pDirLight_VS==NULL )
		return false;
	// `載入Point Light Vertex Shader`
	g_pPointLight_VS = GutLoadVertexShaderDX9_HLSL("../../shaders/vertex_lighting_point.shader", "VS", "vs_1_1");
	if ( g_pPointLight_VS==NULL )
		return false;
	// `載入Spot Light Vertex Shader`
	g_pSpotLight_VS = GutLoadVertexShaderDX9_HLSL("../../shaders/vertex_lighting_spot.shader", "VS", "vs_1_1");
	if ( g_pSpotLight_VS==NULL )
		return false;

	// `視角轉換矩陣`
	g_proj_matrix = GutMatrixPerspectiveRH_DirectX(g_fFovW, 1.0f, 0.1f, 100.0f);
	// `鏡頭轉換矩陣`
	g_view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
	// `事先把view_matrix跟proj_matrix相乘`
	g_view_proj_matrix = g_view_matrix * g_proj_matrix;

	// `畫出正向跟反向的三角形`
	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	return true;
}
Esempio n. 13
0
void DrawObject(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	Matrix4x4 view_matrix = g_Control.GetViewMatrix();
	
	Matrix4x4 scale_matrix; 
	scale_matrix.Scale_Replace(g_fRippleSize, g_fRippleSize, 1.0f);
	
	Matrix4x4 world_matrix = g_orient_matrix * scale_matrix;
	world_matrix[3] = g_vPosition;

	device->SetVertexShader(NULL);
	device->SetPixelShader(NULL);

	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*) &g_proj_matrix);
	device->SetTransform(D3DTS_VIEW, (D3DMATRIX*) &view_matrix);
	device->SetTransform(D3DTS_WORLD, (D3DMATRIX*) &world_matrix);

	device->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
	device->SetRenderState(D3DRS_ZENABLE, TRUE);
	device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);

	g_Model_DX9.Render();

	device->SetRenderState(D3DRS_LIGHTING, FALSE);
}
Esempio n. 14
0
static void WaterSimulation(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	device->SetRenderTarget(0, g_pSurfaces[TEX_HEIGHT2]);
	device->SetDepthStencilSurface(NULL);

	D3DXHANDLE shader = g_pWaterEffect->GetTechniqueByName("WaterSimulation");
	D3DXHANDLE heightmap_current_var = g_pWaterEffect->GetParameterByName(NULL, "heightmap_current");
	D3DXHANDLE heightmap_prev_var = g_pWaterEffect->GetParameterByName(NULL, "heightmap_prev");
	D3DXHANDLE texturesize_var = g_pWaterEffect->GetParameterByName(NULL, "texture_size");
	D3DXHANDLE damping_var = g_pWaterEffect->GetParameterByName(NULL, "fDamping");
	
	Vector4 texturesize;
	texturesize[0] = 1.0f/(float)g_ImageInfo.m_iWidth;
	texturesize[1] = 1.0f/(float)g_ImageInfo.m_iHeight;
	texturesize[2] = (float)g_ImageInfo.m_iWidth;
	texturesize[3] = (float)g_ImageInfo.m_iHeight;

	g_pWaterEffect->SetTechnique(shader);
	g_pWaterEffect->SetTexture(heightmap_prev_var, g_pTextures[TEX_HEIGHT0]);
	g_pWaterEffect->SetTexture(heightmap_current_var, g_pTextures[TEX_HEIGHT1]);
	g_pWaterEffect->SetFloat(damping_var, 0.99f);
	g_pWaterEffect->SetVector(texturesize_var, (D3DXVECTOR4*)&texturesize);

	g_pWaterEffect->Begin(NULL,0);
	g_pWaterEffect->BeginPass(0);
		GutDrawFullScreenQuad_DX9(&g_ImageInfo);
	g_pWaterEffect->EndPass();
	g_pWaterEffect->End();
}
Esempio n. 15
0
void SetupLightingDX9(void)
{
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	device->SetRenderState(D3DRS_LIGHTING, TRUE);
	// 設定環境光
	device->SetRenderState(D3DRS_AMBIENT, ConvertToD3DCOLOR(g_vAmbientLight));
	// 設定光源
	device->LightEnable(0, TRUE);

	D3DLIGHT9 light;
	ZeroMemory( &light, sizeof(light) );

	light.Type = D3DLIGHT_POINT;

	light.Position = *(D3DVECTOR *) &g_Light.m_Position;
	light.Diffuse = *(D3DCOLORVALUE*) &g_Light.m_Diffuse;
	light.Specular = *(D3DCOLORVALUE*) &g_Light.m_Specular;
	light.Range = 100.0f;

	light.Attenuation0 = 1.0f;
	light.Attenuation1 = 0.0f;
	light.Attenuation2 = 0.0f;

	device->SetLight(0, &light);

}
Esempio n. 16
0
void ConverToLogLuminance(LPDIRECT3DTEXTURE9 pSource, sImageInfo *pInfo)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	device->SetRenderTarget(0, g_pFrameSurface[DOWNSAMPLED_256x256]);
	device->SetDepthStencilSurface(NULL);

	D3DXHANDLE shader = g_pExposureEffect->GetTechniqueByName("LogLuminance");
	g_pExposureEffect->SetTechnique(shader);

	D3DXHANDLE tablevar = g_pExposureEffect->GetParameterByName(NULL, "vLuminanceTable");
	D3DXHANDLE imagevar = g_pExposureEffect->GetParameterByName(NULL, "Image");

	Vector4 vTable(0.21f, 0.71f, 0.072f);
	g_pExposureEffect->SetVector(tablevar, (D3DXVECTOR4*)&vTable);
	g_pExposureEffect->SetTexture(imagevar, pSource);

	g_pExposureEffect->Begin(NULL, 0);
	g_pExposureEffect->BeginPass(0);

	DrawFullScreenQuad(pInfo);

	g_pExposureEffect->EndPass();
	g_pExposureEffect->End();
}
Esempio n. 17
0
void ResizeWindowDX9(int width, int height)
{
	// Reset Device
	GutResetGraphicsDeviceDX9();
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// 投影矩陣, 重設水平跟垂直方向的視角.
	float aspect = (float) height / (float) width;
	g_projection_matrix = GutMatrixPerspectiveRH_DirectX(g_fFOV, aspect, 0.1f, 100.0f);
	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &g_projection_matrix);
	// 關閉打光
	device->SetRenderState(D3DRS_LIGHTING, FALSE);
	// 改變三角形正面的面向
	//device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	// 使用自動normalize功能
	device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);

	device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);

	device->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_NONE);

}
Esempio n. 18
0
void RenderFrameDX9(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	device->BeginScene(); 

	Matrix4x4 view_matrix = g_Control.GetViewMatrix();
	Matrix4x4 world_matrix = g_Control.GetObjectMatrix();

	// normal pass
	if ( g_bPosteffect )
	{
		device->SetRenderTarget(0, g_pFrameSurface[FULLSIZE]);
		device->SetDepthStencilSurface(NULL);
	}
	else
	{
		device->SetRenderTarget(0, g_pMainSurface);
		device->SetDepthStencilSurface(NULL);
	}

	device->Clear(0, NULL, D3DCLEAR_TARGET, 0x0, 1.0f, 0);

	device->SetVertexShader(NULL);
	device->SetPixelShader(NULL);

	device->SetRenderState(D3DRS_LIGHTING, FALSE);
	device->SetRenderState(D3DRS_ZENABLE, FALSE);
	device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);

	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)&g_proj_matrix);
	device->SetTransform(D3DTS_VIEW, (D3DMATRIX*)&view_matrix);
	device->SetTransform(D3DTS_WORLD, (D3DMATRIX *)&world_matrix);

	g_Model_DX9.Render();

	if ( g_bPosteffect )
	{
		ConverToLogLuminance(g_pFrameBuffer[FULLSIZE], &g_ImageInfo);
		// ExpLuminance(g_pFrameSurface[FULLSIZE], g_pFrameBuffer[DOWNSAMPLED_256x256], &g_Image256x256);

		AverageLuminance(g_pFrameBuffer[DOWNSAMPLED_256x256]);
		ExpLuminance();
		AdaptiveLuminance();

		device->SetRenderTarget(0, g_pMainSurface);
		device->SetDepthStencilSurface(NULL);
		//device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0x0, 1.0f, 0);
		
		AutoExposure();

		if ( g_iMode & 0x01 )
		{
			DrawImage(g_pFrameBuffer[LUMINANCE_TEMP], &g_Image1x1, -1.0f, -1.0f, 0.1f, 0.1f);
			DrawImage(g_pFrameBuffer[LUMINANCE_CURRENT], &g_Image1x1, -0.9f, -1.0f, 0.1f, 0.1f);
		}
	}

	device->EndScene();
	device->Present( NULL, NULL, NULL, NULL );
}
Esempio n. 19
0
void InitStateDX9(void)
{
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	// 關閉打光
	device->SetRenderState(D3DRS_LIGHTING, FALSE);
	// 使用trilinear
	device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
	//
	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	//
	device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);

	device->GetRenderTarget(0, &g_pMainFrameBuffer);
	device->GetDepthStencilSurface(&g_pMainDepthBuffer);

	int width, height;
	GutGetWindowSize(width, height);

	g_iFrameBufferWidth = width * 2;
	g_iFrameBufferHeight = height * 2;

	device->CreateTexture(g_iFrameBufferWidth, g_iFrameBufferHeight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_pTexture, NULL);
	device->CreateDepthStencilSurface(g_iFrameBufferWidth, g_iFrameBufferHeight, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, FALSE, &g_pDepthStencil, NULL);
	g_pTexture->GetSurfaceLevel(0, &g_pFrameBuffer);
}
Esempio n. 20
0
bool InitResourceDX9(void)
{
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	// 設定視角轉換矩陣
	g_projection_matrix = GutMatrixOrthoRH_DirectX(g_fOrthoWidth, g_fOrthoHeight, 0.1f, 100.0f);
	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &g_projection_matrix);

	// 關閉打光
	device->SetRenderState(D3DRS_LIGHTING, FALSE);

	// 畫出正向跟反向的三角形
	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	// 載入貼圖
	g_pTexture0 = GutLoadTexture_DX9("../../textures/brickwall.tga");
	g_pTexture1 = GutLoadTexture_DX9("../../textures/spotlight_effect.tga");

	// trilinear
	device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

	// trilinear
	device->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

	return true;
}
Esempio n. 21
0
static void Antialiasing_None(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	device->BeginScene(); 

	// `設定轉換矩陣`
	Matrix4x4 ident_matrix; ident_matrix.Identity();

	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &ident_matrix);
	device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &ident_matrix);
	device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &g_world_matrix);

	device->SetRenderState(D3DRS_ZENABLE, FALSE);

	// `消除畫面`
	device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_RGBA(0,0,255,255), 1.0f, 0);
	// `畫出3角形`
	device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
	device->SetRenderState(D3DRS_TEXTUREFACTOR, 0xffffffff);
	device->SetFVF(D3DFVF_XYZ);
	device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, g_Triangle, sizeof(Vector4));
	// `宣告所有的繪圖指令都下完了`
	device->EndScene(); 
	// `把背景backbuffer的畫面呈現出來`
	device->Present( NULL, NULL, NULL, NULL );
}
Esempio n. 22
0
void ResizeWindowDX9(int width, int height)
{
	GutResetGraphicsDeviceDX9();
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// 投影矩陣, 重設水平跟垂直方向的視角.
	float aspect = (float) height / (float) width;
	g_fOrthoWidth = g_fOrthoSize;
	g_fOrthoHeight = g_fOrthoSize;
	if ( aspect > 1.0f )
		g_fOrthoHeight *= aspect;
	else
		g_fOrthoWidth /= aspect;

	g_projection_matrix = GutMatrixOrthoRH_DirectX(g_fOrthoWidth, g_fOrthoHeight, 0.1f, 100.0f);
	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &g_projection_matrix);
	// 計算出一個可以轉換到鏡頭座標系的矩陣
	//device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix);
	// 關閉打光
	device->SetRenderState(D3DRS_LIGHTING, FALSE);
	// 畫出正向跟反向的三角形
	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	// trilinear
	device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

	// trilinear
	device->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

}
Esempio n. 23
0
// 使用DirectX 9來繪圖
void RenderFrameDX9(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// 消除畫面
	device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
	// 開始下繪圖指令
	device->BeginScene(); 
	// 設定轉換矩陣
	Matrix4x4 view_matrix = g_Control.GetViewMatrix();
	Matrix4x4 world_matrix = g_Control.GetObjectMatrix();
	device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix);
	device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &world_matrix);
	// 在只傳入2維貼圖座標時, Direct3D使用3x3矩陣來轉換貼圖座標.
	Matrix4x4 converted_matrix3x3 = g_texture_matrix;
	converted_matrix3x3[2] = g_texture_matrix[3];
	device->SetTransform(D3DTS_TEXTURE0, (D3DMATRIX *) &converted_matrix3x3);
	//device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
	//device->SetTexture(0, g_pTexture);
	// 畫出矩形
	device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, g_Quad, sizeof(Vertex_VT));
	// 宣告所有的繪圖指令都下完了
	device->EndScene(); 
	// 把背景backbuffer的畫面呈現出來
	device->Present( NULL, NULL, NULL, NULL );
}
Esempio n. 24
0
static void RenderModelDX9(bool mirror, Vector4 *pPlane)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	Matrix4x4 view_matrix;
	Matrix4x4 world_matrix = g_Control.GetObjectMatrix();


	if ( mirror )
	{
		Vector4 vEye = g_Control.GetCameraPosition();
		Vector4 vUp = g_Control.m_vUp;
		Vector4 vLookAt = g_Control.m_vLookAt;

		Vector4 mirror_eye = MirrorPoint(vEye, *pPlane);
		Vector4 mirror_lookat = MirrorPoint(vLookAt, *pPlane);
		Vector4 mirror_up = MirrorPoint(vUp, *pPlane);

		view_matrix = GutMatrixLookAtRH(mirror_eye, mirror_lookat, mirror_up);

		g_mirror_view_matrix = view_matrix;
	}
	else
	{
		view_matrix = g_Control.GetViewMatrix();
	}

	device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix);
	device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &world_matrix);

	g_Model_DX9.Render();
}
Esempio n. 25
0
bool InitResourceDX9(void)
{
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// 設定視角轉換矩陣
	g_projection_matrix = GutMatrixPerspectiveRH_DirectX(g_fFOV, 1.0f, 0.1f, 100.0f);

	InitStateDX9();

	CGutModel::SetTexturePath("../../textures/");
	g_Model_DX9.ConvertToDX9Model(&g_Model);
	g_SpotLightModel_DX9.ConvertToDX9Model(&g_SpotLightModel);

	g_material_stencilpass.m_bCullFace = false;

	g_material_spotlightpass.m_bBlend = true;
	g_material_spotlightpass.m_SrcBlend = D3DBLEND_ONE;
	g_material_spotlightpass.m_DestBlend = D3DBLEND_ONE;

	g_material_spotlightpass.m_Material.Diffuse.r = 
		g_material_spotlightpass.m_Material.Diffuse.g = 
		g_material_spotlightpass.m_Material.Diffuse.b = 
		g_material_spotlightpass.m_Material.Diffuse.a = 0.3f;

	return true;
}
Esempio n. 26
0
bool InitResourceDX9(void)
{
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// 設定視角轉換矩陣
	int w, h;
	GutGetWindowSize(w, h);
	float aspect = (float) h / (float) w;
	g_projection_matrix = GutMatrixPerspectiveRH_DirectX(g_fFovW, aspect, 0.1f, 100.0f);
	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &g_projection_matrix);

	// 畫出正向跟反向的三角形
	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	CGutModel::SetTexturePath("../../textures/");

	for ( int t=0; t<MAX_NUM_TEXTURES; t++ )
	{
		// trilinear filter
		device->SetSamplerState(t, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
		device->SetSamplerState(t, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
		device->SetSamplerState(t, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
	}

	g_Model_DX9.ConvertToDX9Model(&g_Model);

	return true;
}
Esempio n. 27
0
void ResizeWindowDX9(int width, int height)
{
	// 取得Direct3D 9裝置
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	D3DPRESENT_PARAMETERS d3dpresent;

	ZeroMemory( &d3dpresent, sizeof(d3dpresent) );
	d3dpresent.Windowed = TRUE; // 使用視窗模式
	d3dpresent.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpresent.BackBufferFormat = D3DFMT_UNKNOWN; // 使用視窗模式可以不去設定
	d3dpresent.BackBufferCount = 1; // 提供一塊backbuffer
	d3dpresent.EnableAutoDepthStencil = TRUE; // 自動開啟DepthStencil Buffer
	d3dpresent.AutoDepthStencilFormat = D3DFMT_D24S8; // DepthStencil Buffer模式

	device->Reset(&d3dpresent);

	// 投影矩陣, 重設水平跟垂直方向的視角.
	float aspect = (float) height / (float) width;
	Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_DirectX(90.0f, aspect, 0.1f, 100.0f);
	device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &projection_matrix);
	// 關閉打光
	device->SetRenderState(D3DRS_LIGHTING, FALSE);
	// 改變三角形正面的面向
	device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
}
Esempio n. 28
0
// 使用DirectX 9來繪圖
void RenderFrameDX9(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	// 消除畫面
	device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
	// 開始下繪圖指令
	device->BeginScene(); 
	// 設定座標轉換矩陣
	Matrix4x4 view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
	device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix);
	device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &g_world_matrix);
	// 套用貼圖
	device->SetTexture(0, g_pTexture);
	// trilinear filter
	device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
	// 自動產生貼圖座標
	device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR);
	//device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3);
	// 使用自動normalize功能
	device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
	// 畫模型
	// 傳入0代表不套用模型中的材質, 經由外部來設定.
	g_Model_DX9.Render(0);
	// 宣告所有的繪圖指令都下完了
	device->EndScene(); 
	// 把背景backbuffer的畫面呈現出來
	device->Present( NULL, NULL, NULL, NULL );
}
Esempio n. 29
0
void RenderSolarSystemDX9(void)
{
	int index = g_FrameCount % 2;
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	LPDIRECT3DQUERY9 pQuery = g_pOcclusionQuery[index];

	// `太陽`
	device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &g_sun_matrix);
	g_Models_DX9[0].Render();
	// `地球`
	pQuery->Issue(D3DISSUE_BEGIN);
	device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &g_earth_matrix);
	g_Models_DX9[1].Render();
	pQuery->Issue(D3DISSUE_END);
	// `月亮`
	device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &g_moon_matrix);
	g_Models_DX9[2].Render();

	if ( g_FrameCount )
	{
		int num_loops = 0;
		int num_samples_passed = 0;
		// `去檢查前一個畫面的Occlusion Query結果`
		pQuery = g_pOcclusionQuery[(index + 1) % 2];
		while( pQuery->GetData(&num_samples_passed, 4, D3DGETDATA_FLUSH)==S_FALSE  )
		{
			// `結果可能還沒出來, 要再查詢一次.`
			num_loops++;
		}

		printf("Earth %s, queried %05d times\r", num_samples_passed ? "visible" : "disappear", num_loops);
	}

	g_FrameCount++;
}
Esempio n. 30
0
void CGutFontDX9::Render(void)
{
	if ( m_iNumCharacters==0 )
		return;

	LPDIRECT3DDEVICE9 pDevice = GutGetGraphicsDeviceDX9();
	// 套用字型貼圖
	sModelMaterial_DX9 mtl;
	mtl.m_bCullFace = false;
	mtl.m_pTextures[0] = m_pFontTexture;
	mtl.Submit();
	// 開啟Alpha Test
	pDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
	pDevice->SetRenderState(D3DRS_ALPHAREF, 128);
	pDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
	// 使用平行視角鏡頭
	Matrix4x4 proj_matrix = GutMatrixOrthoRH_DirectX(m_fWidth, m_fHeight, 0.0f, 1.0f);
	Matrix4x4 view_matrix; view_matrix.Identity();
	Matrix4x4 ident_matrix; ident_matrix.Identity();
	view_matrix[3].Set(-m_fWidth/2.0f, -m_fHeight/2.0f, 0.0f, 1.0f);
	// 設定轉換矩陣
	pDevice->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)&proj_matrix);
	pDevice->SetTransform(D3DTS_VIEW, (D3DMATRIX*)&view_matrix);
	pDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)&ident_matrix);
	// 設定頂點資料格式
	pDevice->SetFVF(D3DFVF_XYZ|D3DFVF_TEX1); 
	// 畫出所有的文字
	pDevice->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, 
		m_iNumCharacters*4, m_iNumCharacters*2, 
		m_pIndexArray, D3DFMT_INDEX16, 
		m_pVertexArray, sizeof(_FontVertex));
}