Ejemplo n.º 1
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 );
}
Ejemplo n.º 2
0
static void Antialiasing_SuperSampling(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->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	device->SetRenderState(D3DRS_ZENABLE, FALSE);

	// `先把畫面畫在4倍大的Framebuffer上`
	{
		// `設定繪圖結果要畫在動態貼圖上`
		device->SetRenderTarget(0, g_pFrameBuffer);
		device->SetDepthStencilSurface(g_pDepthStencil);
		// `消除畫面`
		device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_RGBA(0,0,255,255), 1.0f, 0);
		// `設定轉換矩陣`
		device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &g_world_matrix);
		// `畫一片3角形`
		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));
	}

	// `再把畫面縮小放到原始大小的畫面上`
	{
		// `改用原本的Framebuffer`
		device->SetRenderTarget(0, g_pMainFrameBuffer);
		device->SetDepthStencilSurface(g_pMainDepthBuffer);
		// `設定要貼圖的內插`
		device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
		device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
		device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
		// `使用動態貼圖`
		device->SetTexture(0, g_pTexture);
		device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
		// `設定轉換矩陣`
		device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &ident_matrix);
		// `把貼圖套到整個畫面上`
		device->SetFVF(D3DFVF_XYZ | D3DFVF_TEX1);
		device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, g_Quad, sizeof(Vertex_VT));
	}

	// `宣告所有的繪圖指令都下完了`
	device->EndScene(); 
	// `把背景backbuffer的畫面呈現出來`
	device->Present( NULL, NULL, NULL, NULL );
}
static bool CopyRenderTargetToBackground()
{
	bool ret = false;

	LPDIRECT3DDEVICE9 device = (LPDIRECT3DDEVICE9)GetUseDirect3DDevice9();
	if (device == NULL) return nullptr;

	HRESULT hr;
	IDirect3DSurface9* tempRender = nullptr;
	IDirect3DSurface9* tempDepth = nullptr;

	hr = device->GetRenderTarget(0, &tempRender);
	if (FAILED(hr))
	{
		goto Exit;
	}

	hr = device->GetDepthStencilSurface(&tempDepth);
	if (FAILED(hr))
	{
		goto Exit;
	}

	device->SetRenderTarget(0, g_dx9_backgroundSurface);
	device->SetDepthStencilSurface(nullptr);

	D3DSURFACE_DESC desc;
	tempRender->GetDesc(&desc);
	const bool isSame = (desc.Width == g_backgroundWidth && desc.Height == g_backgroundHeight);

	device->StretchRect(
		tempRender,
		nullptr,
		g_dx9_backgroundSurface,
		nullptr,
		(isSame ? D3DTEXF_POINT : D3DTEXF_LINEAR)
		);

	device->SetRenderTarget(0, tempRender);
	device->SetDepthStencilSurface(tempDepth);
	
	ret = true;

Exit:;
	ES_SAFE_RELEASE(tempRender);
	ES_SAFE_RELEASE(tempDepth);

	return ret;
}
Ejemplo n.º 4
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();
}
Ejemplo n.º 5
0
void Render::Draw(IDirect3DSurface9* renderTarget, D3DXMATRIX* view, D3DXMATRIX* projection)
{
    // Get or create device
    LPDIRECT3DDEVICE9 device = GetDevice();
    if (device == NULL) return;

    // Load shaders if it is required
//    if (!EnsureShaders()) return;

    // Prepare depth surface
    D3DSURFACE_DESC renderTargetDescription;
    renderTarget->GetDesc(&renderTargetDescription);
    D3DSURFACE_DESC depthSurfaceDescription;
    if (depthSurface != NULL) depthSurface->GetDesc(&depthSurfaceDescription);
    if (depthSurface == NULL || depthSurfaceDescription.Width != renderTargetDescription.Width || depthSurfaceDescription.Height != renderTargetDescription.Height)
    {
        if (depthSurface != NULL) depthSurface->Release();
        device->CreateDepthStencilSurface(renderTargetDescription.Width, renderTargetDescription.Height, D3DFMT_D24X8, D3DMULTISAMPLE_NONE, 0, FALSE, &depthSurface, NULL);
        if (depthSurface == NULL) return;
    }

    device->SetRenderTarget(0, renderTarget);
    device->SetDepthStencilSurface(depthSurface);
    device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);

    if (lowPolySphere == NULL)
    {
        // Create spheres
        lowPolySphere = CreateSphere(2);
        middlePolySphere = CreateSphere(3);
        highPolySphere = CreateSphere(5);
        // Create cylinders
        lowPolyCylinder = CreateCylinder(3);
        middlePolyCylinder = CreateCylinder(12);
        highPolyCylinder = CreateCylinder(24);
    }



    // FIXME: light dir must be slightly different!
    D3DVECTOR lightDirection;
    lightDirection.x = view->_13;
    lightDirection.y = view->_23;
    lightDirection.z = view->_33;
    D3DVECTOR viewDirection;
    viewDirection.x = view->_13;
    viewDirection.y = view->_23;
    viewDirection.z = view->_33;
    elementMaterials[0].SetViewLightDirection(&viewDirection, &lightDirection);

    // Rendering
    device->BeginScene();


    DrawAtoms(view, projection);
    DrawBonds(view, projection);
    DrawResidues(view, projection);

    device->EndScene();
}
Ejemplo n.º 6
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();
}
Ejemplo n.º 7
0
void RenderFrameDX9(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	
	device->BeginScene(); 

	ParticleSimulation();
	RenderParticle();
	SwapTextures();

	float x = -1.0f;
	float y = -1.0f;
	float w = 0.4f;
	float h = 0.4f;

	device->SetRenderTarget(0, g_pMainFramebuffer);
	device->SetDepthStencilSurface(g_pMainDepthbuffer);

	if ( g_iMode & 0x01 )
	{
		DrawImage(g_pTextures[TEX_POSITION0], x, y, w, h, &g_ImageInfo);
		x+=w;
	}

	if ( g_iMode & 0x02 )
	{
		DrawImage(g_pTextures[TEX_VELOCITY1], x, y, w, h, &g_ImageInfo);
		x+=w;
	}

	device->EndScene(); 
    device->Present( NULL, NULL, NULL, NULL );
}
Ejemplo n.º 8
0
static void AddImpulse(void)
{
	static Vector4 vPosition(0.0f, 0.0f, 0.0f, 0.0f);
	//Vector4 vDiff = vPosition - g_vPosition;
	Vector4 vDiff = g_vPosition - vPosition;
	Vector4 vLength = vDiff.Length();

	if ( vLength[0]<2.0f )
		return;

	Vector4 vDir = vDiff / vLength;
	Vector4 vVec0(vDir[1],-vDir[0], 0.0f, 0.0f);
	Vector4 vVec1(vDir[0], vDir[1], 0.0f, 0.0f);

	vPosition = g_vPosition;

	Vector4 vVec0_old = g_orient_matrix[0];
	Vector4 vVec1_old = g_orient_matrix[1];

	Vector4 vVec0_new = VectorLerp(vVec0_old, vVec0, 0.2f);
	Vector4 vVec1_new = VectorLerp(vVec1_old, vVec1, 0.2f);
	vVec0_new.Normalize();
	vVec1_new.Normalize();
	Vector4 vVec2_new = Vector3CrossProduct(vVec0_new, vVec1_new);

	g_orient_matrix.Identity();
	g_orient_matrix[0] = vVec0_new;
	g_orient_matrix[1] = vVec1_new;
	g_orient_matrix[2] = vVec2_new;

	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

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

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

	Matrix4x4 wvp_matrix = g_orient_matrix * world_matrix * view_matrix * g_proj_matrix;

	D3DXHANDLE shader = g_pWaterEffect->GetTechniqueByName("AddImpulse");
	D3DXHANDLE wvp_matrix_var = g_pWaterEffect->GetParameterByName(NULL, "wvp_matrix");
	D3DXHANDLE force_var = g_pWaterEffect->GetParameterByName(NULL, "fForce");

	g_pWaterEffect->SetTechnique(shader);
	g_pWaterEffect->SetMatrix(wvp_matrix_var, (D3DXMATRIX *)&wvp_matrix);
	g_pWaterEffect->SetFloat(force_var, 0.05f);

	g_pWaterEffect->Begin(NULL, 0);
	g_pWaterEffect->BeginPass(0);
		g_Model_DX9.Render(0);
	g_pWaterEffect->EndPass();
	g_pWaterEffect->End();

	vPosition = g_vPosition;
}
Ejemplo n.º 9
0
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
static bool CopyRenderTargetToBackground()
{
	bool ret = false;

	HRESULT hr;
	IDirect3DSurface9* tempRender = nullptr;
	IDirect3DSurface9* tempDepth = nullptr;

	hr = g_d3d_device->GetRenderTarget(0, &tempRender);
	if (FAILED(hr))
	{
		goto Exit;
	}

	hr = g_d3d_device->GetDepthStencilSurface(&tempDepth);
	if (FAILED(hr))
	{
		goto Exit;
	}

	g_d3d_device->SetRenderTarget(0, g_backgroundSurface);
	g_d3d_device->SetDepthStencilSurface(nullptr);

	D3DSURFACE_DESC desc;
	tempRender->GetDesc(&desc);
	
	g_d3d_device->StretchRect(
		tempRender,
		nullptr,
		g_backgroundSurface,
		nullptr,
		D3DTEXF_POINT
		);

	g_d3d_device->SetRenderTarget(0, tempRender);
	g_d3d_device->SetDepthStencilSurface(tempDepth);
	
	ret = true;

Exit:;
	ES_SAFE_RELEASE(tempRender);
	ES_SAFE_RELEASE(tempDepth);

	return ret;
}
Ejemplo n.º 10
0
void RenderFrameDX9(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	
	device->BeginScene(); 

	AddImpulse();
	WaterSimulation();
	HeightmapToNormal();

	device->SetRenderTarget(0, g_pMainFramebuffer);
	device->SetDepthStencilSurface(g_pMainDepthbuffer);
	device->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0x0, 1.0f, 0);

	RenderWater();
	DrawObject();

	SwapHeightmaps();

	float x = -1.0f;
	float y = -1.0f;
	float w = 0.4f;
	float h = 0.4f;

	if ( g_iMode & 0x01 )
	{
		DrawImage(g_pTextures[TEX_HEIGHT2], x, y, w, h, &g_ImageInfo);
		x+=w;
	}

	if ( g_iMode & 0x02 )
	{
		D3DXHANDLE shader = g_pWaterEffect->GetTechniqueByName("Heightmap");
		D3DXHANDLE heightmap_var = g_pWaterEffect->GetTechniqueByName("heightmap_current");

		g_pWaterEffect->SetTechnique(shader);
		g_pWaterEffect->SetTexture(heightmap_var, g_pTextures[TEX_HEIGHT2]);

		g_pWaterEffect->Begin(NULL, 0);
		g_pWaterEffect->BeginPass(0);
			GutDrawScreenQuad_DX9(x, y, w, h, &g_ImageInfo);
		g_pWaterEffect->EndPass();
		g_pWaterEffect->End();

		x+=w;
	}

	if ( g_iMode & 0x04 )
	{
		DrawImage(g_pTextures[TEX_NORMAL], x, y, w, h, &g_ImageInfo);
		x+=w;
	}

	device->EndScene(); 
    device->Present( NULL, NULL, NULL, NULL );
}
Ejemplo n.º 11
0
void Graphics::restoreRenderTarget() {
	if (backBuffer != nullptr) {
		device->SetRenderTarget(0, backBuffer);
		device->SetRenderTarget(1, nullptr);
		backBuffer->Release();
		backBuffer = nullptr;
		device->SetDepthStencilSurface(depthBuffer);
		depthBuffer->Release();
		depthBuffer = nullptr;
	}
}
Ejemplo n.º 12
0
void Graphics::setRenderTarget(RenderTarget* target, int num) {
	//if (backBuffer != nullptr) backBuffer->Release();
	if (num == 0) {
		if (backBuffer == nullptr) {
			device->GetRenderTarget(0, &backBuffer);
			device->GetDepthStencilSurface(&depthBuffer);
		}
		affirm(device->SetDepthStencilSurface(target->depthSurface));
	}
	affirm(device->SetRenderTarget(num, target->colorSurface));
}
Ejemplo n.º 13
0
void Graphics4::restoreRenderTarget() {
	if (backBuffer != nullptr) {
		device->SetRenderTarget(0, backBuffer);
		device->SetRenderTarget(1, nullptr);
		backBuffer->Release();
		backBuffer = nullptr;
		device->SetDepthStencilSurface(depthBuffer);
		depthBuffer->Release();
		depthBuffer = nullptr;
		viewport(0, 0, _width, _height);
	}
}
Ejemplo n.º 14
0
void Graphics4::setRenderTargets(RenderTarget** targets, int count) {
	// if (backBuffer != nullptr) backBuffer->Release();

	System::makeCurrent(targets[0]->contextId);

	if (backBuffer == nullptr) {
		device->GetRenderTarget(0, &backBuffer);
		device->GetDepthStencilSurface(&depthBuffer);
	}
	Microsoft::affirm(device->SetDepthStencilSurface(targets[0]->depthSurface));
	for (int i = 0; i < count; ++i) {
		Microsoft::affirm(device->SetRenderTarget(i, targets[i]->colorSurface));
	}
}
Ejemplo n.º 15
0
void Graphics::setRenderTarget(RenderTarget* target, int num, int additionalTargets) {
	// if (backBuffer != nullptr) backBuffer->Release();

	System::makeCurrent(target->contextId);

	if (num == 0) {
		if (backBuffer == nullptr) {
			device->GetRenderTarget(0, &backBuffer);
			device->GetDepthStencilSurface(&depthBuffer);
		}
		affirm(device->SetDepthStencilSurface(target->depthSurface));
	}
	affirm(device->SetRenderTarget(num, target->colorSurface));
}
Ejemplo n.º 16
0
static void RenderParticle(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	int w, h; GutGetWindowSize(w, h);

	Matrix4x4 wvp_matrix = g_Control.GetObjectMatrix() * g_Control.GetViewMatrix() * g_proj_matrix;
	Vector4 ScreenSize( (float)w, (float)h, 0.0f, 0.0f);
	float fTanW = FastMath::Tan( FastMath::DegToRad(g_fFOV) );

	device->SetRenderTarget(0, g_pMainFramebuffer);
	device->SetDepthStencilSurface(g_pMainDepthbuffer);
	device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0x0, 1.0f, 0);

	D3DXHANDLE shader = g_pParticleRenderFX->GetTechniqueByName("RenderParticle");
	D3DXHANDLE positiontex_var = g_pParticleRenderFX->GetParameterByName(NULL, "PositionTex");
	D3DXHANDLE velocitytex_var = g_pParticleRenderFX->GetParameterByName(NULL, "VelocityTex");
	D3DXHANDLE wvp_matrix_var = g_pParticleRenderFX->GetParameterByName(NULL, "wvp_matrix");
	D3DXHANDLE tan_fov_var = g_pParticleRenderFX->GetParameterByName(NULL, "fTanW");
	D3DXHANDLE screensize_var = g_pParticleRenderFX->GetParameterByName(NULL, "ScreenSize");
	D3DXHANDLE diffusetex_var = g_pParticleRenderFX->GetParameterByName(NULL, "DiffuseTex");

	g_pParticleRenderFX->SetTechnique(shader);
	g_pParticleRenderFX->SetTexture(positiontex_var, g_pTextures[TEX_POSITION1]);
	g_pParticleRenderFX->SetTexture(velocitytex_var, g_pTextures[TEX_VELOCITY1]);
	g_pParticleRenderFX->SetMatrix(wvp_matrix_var, (D3DXMATRIX*)&wvp_matrix);
	g_pParticleRenderFX->SetVector(screensize_var, (D3DXVECTOR4*)&ScreenSize);
	g_pParticleRenderFX->SetFloat(tan_fov_var, fTanW);
	g_pParticleRenderFX->SetTexture(diffusetex_var, g_pParticleTexture);
	
	int num_processed_particles = (g_num_particles / g_texture_width) * g_texture_width;

	if ( num_processed_particles )
	{
		device->SetFVF(D3DFVF_TEX1);

		g_pParticleRenderFX->Begin(NULL,0);
		g_pParticleRenderFX->BeginPass(0);
			//device->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
			device->DrawPrimitiveUP(D3DPT_POINTLIST, num_processed_particles, g_pParticleArray, sizeof(sParticle));
			//device->SetRenderState(D3DRS_POINTSPRITEENABLE, FALSE);
		g_pParticleRenderFX->EndPass();
		g_pParticleRenderFX->End();
	}
}
Ejemplo n.º 17
0
static void HeightmapToNormal(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

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

	D3DXHANDLE shader = g_pWaterEffect->GetTechniqueByName("ConvertNormal");
	D3DXHANDLE heightmap_current_var = g_pWaterEffect->GetParameterByName(NULL, "heightmap_current");
	D3DXHANDLE normalscale_var = g_pWaterEffect->GetParameterByName(NULL, "fNormalScale");
	
	g_pWaterEffect->SetTechnique(shader);
	g_pWaterEffect->SetTexture(heightmap_current_var, g_pTextures[TEX_HEIGHT2]);
	g_pWaterEffect->SetFloat(normalscale_var, 1.0f);

	g_pWaterEffect->Begin(NULL,0);
	g_pWaterEffect->BeginPass(0);
		GutDrawFullScreenQuad_DX9(&g_ImageInfo);
	g_pWaterEffect->EndPass();
	g_pWaterEffect->End();
}
Ejemplo n.º 18
0
// `使用Direct3D9來繪圖`
void RenderFrameDX9(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	Matrix4x4 light_projection_matrix;
	Matrix4x4 light_view_matrix;

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

	// `開始下繪圖指令`
	device->BeginScene(); 

	// `把剪影畫在動態貼圖中`
	if(1)
	{
		//! 设置临时framebuffer
		LPDIRECT3DSURFACE9 pFrameBufferBackup, pDepthBufferBackup;
		device->GetRenderTarget(0, &pFrameBufferBackup); 
		pFrameBufferBackup->Release();
		device->GetDepthStencilSurface(&pDepthBufferBackup); 
		pDepthBufferBackup->Release();

		LPDIRECT3DSURFACE9 pSurface;
		g_pTexture->GetSurfaceLevel(0, &pSurface); 

		device->SetRenderTarget(0, pSurface);
		device->SetDepthStencilSurface(g_pDepthStencil);

		device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(255, 255, 255, 255), 1.0f, 0);

		Vector4 vLightPos = g_Light.m_Position;
		Vector4 vLightUp(0.0f, 1.0f, 0.0f);
		Vector4 vLightLookat(0.0f, 0.0f, 0.0f);
		// `把鏡頭放在光源的`
		light_projection_matrix = GutMatrixPerspectiveRH_DirectX(60.0f, 1.0f, 0.1f, 100.0f);
		light_view_matrix = GutMatrixLookAtRH(vLightPos, vLightLookat, vLightUp);
		// `設定轉換矩陣`
		device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *)&light_projection_matrix);
		device->SetTransform(D3DTS_VIEW, (D3DMATRIX *)&light_view_matrix);
		device->SetTransform(D3DTS_WORLD, (D3DMATRIX *)&world_matrix);

		D3DCOLORVALUE zero = {0.0f, 0.0f, 0.0f, 1.0f};

		sModelMaterial_DX9 material;
		material.m_Material.Ambient = zero;
		material.m_Material.Emissive = zero;
		material.m_Material.Diffuse = zero;
		material.m_Material.Specular = zero;
		material.m_bCullFace = false;

		material.Submit();

		device->SetRenderState(D3DRS_LIGHTING, 0);

		g_Model_DX9.Render(0);

		pSurface->Release();
		device->SetRenderTarget(0, pFrameBufferBackup);
		device->SetDepthStencilSurface(pDepthBufferBackup);
	}

	// `把上一個步驟的結果當成貼圖來使用`
	{
		// `消除畫面`
		device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 150, 255), 1.0f, 0);
		// `設定轉換矩陣`
		device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &g_projection_matrix);
		device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix);
		device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &world_matrix);


#define DRAW_TEAPORT_FIRST 1

		//! 画茶壶
		if (DRAW_TEAPORT_FIRST)
		{
			SetupLightingDX9();
			g_Model_DX9.Render();
		}

		//! 设置矩形的纹理
		sModelMaterial_DX9 material;
		material.m_pTextures[0] = g_pTexture;
		material.Submit();

		Matrix4x4 inv_view_matrix = view_matrix;
		inv_view_matrix.FastInvert();

		Matrix4x4 uv_offset_matrix;
		uv_offset_matrix.Identity();
		uv_offset_matrix.Scale(0.5f, -0.5f, 0.5f);
		uv_offset_matrix[3].Set(0.5f, 0.5f, 0.5f, 1.0f);

		Matrix4x4 texture_matrix = inv_view_matrix * light_view_matrix * light_projection_matrix * uv_offset_matrix;
		Matrix4x4 indent_matrix = Matrix4x4::IdentityMatrix();

		device->SetTransform(D3DTS_TEXTURE0, (D3DMATRIX *) &texture_matrix);
		device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &indent_matrix);

		device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);
		device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT4 |  D3DTTFF_PROJECTED );
		device->SetRenderState(D3DRS_LIGHTING, FALSE);
		// `畫出矩形`
		device->SetFVF(D3DFVF_XYZ);
		device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, g_Quad, sizeof(Vertex_VT));

		//! 关闭纹理矩阵
		device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);

		if (!DRAW_TEAPORT_FIRST)
		{
			//SetupLightingDX9();
			device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &world_matrix);
			g_Model_DX9.Render();
		}


	}
	// `宣告所有的繪圖指令都下完了`
	device->EndScene(); 
	// `把背景backbuffer的畫面呈現出來`
	device->Present( NULL, NULL, NULL, NULL );
}
Ejemplo n.º 19
0
HRESULT HookIDirect3DDevice9::SetDepthStencilSurface(LPVOID _this,
													 IDirect3DSurface9* pNewZStencil)
{
	LOG_API();
	return pD3Dev->SetDepthStencilSurface(pNewZStencil);
}
Ejemplo n.º 20
0
// 使用DirectX 9來繪圖
void RenderFrameDX9(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	LPDIRECT3DTEXTURE9 pBlurredTexture = NULL;

	Matrix4x4 light_projection_matrix;
	Matrix4x4 light_view_matrix;

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

	// 開始下繪圖指令
	device->BeginScene(); 
	{
		// 保存主framebuffer
		LPDIRECT3DSURFACE9 pFrameBufferBackup, pDepthBufferBackup;
		device->GetRenderTarget(0, &pFrameBufferBackup); pFrameBufferBackup->Release();
		device->GetDepthStencilSurface(&pDepthBufferBackup); pDepthBufferBackup->Release();
		// 取出動態貼圖中的surface
		LPDIRECT3DSURFACE9 pSurface;
		g_pTexture->GetSurfaceLevel(0, &pSurface); 
		// 把繪圖結果輸出到動態貼圖中
		device->SetRenderTarget(0, pSurface);
		device->SetDepthStencilSurface(g_pDepthStencil);
		// 清除畫面
		device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(255, 255, 255, 255), 1.0f, 0);
		// 設定光源位置
		Vector4 vLightPos = g_Light.m_Position;
		Vector4 vLightUp(0.0f, 1.0f, 0.0f);
		Vector4 vLightLookat(0.0f, 0.0f, 0.0f);

		light_projection_matrix = GutMatrixPerspectiveRH_DirectX(60.0f, 1.0f, 0.1f, 100.0f);
		light_view_matrix = GutMatrixLookAtRH(vLightPos, vLightLookat, vLightUp);
		// 把鏡頭放到光源位置來畫陰影
		device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *)&light_projection_matrix);
		device->SetTransform(D3DTS_VIEW, (D3DMATRIX *)&light_view_matrix);
		device->SetTransform(D3DTS_WORLD, (D3DMATRIX *)&world_matrix);

		// 把所有反射關閉,讓模型在畫面上呈現黑色。
		D3DCOLORVALUE zero = {0.0f, 0.0f, 0.0f, 1.0f};

		sModelMaterial_DX9 material;
		material.m_Material.Ambient = zero;
		material.m_Material.Emissive = zero;
		material.m_Material.Diffuse = zero;
		material.m_Material.Specular = zero;
		material.m_bCullFace = false;

		material.Submit();

		SetupLightingDX9();
		// 畫出模型
		g_Model_DX9.Render(0);
		// 告知direct3d9裝置rendertarget使用完畢
		pSurface->Release();
		// 還原主framebuffer
		device->SetRenderTarget(0, pFrameBufferBackup);
		device->SetDepthStencilSurface(pDepthBufferBackup);
	}

	// 把影子柔化
	{
		pBlurredTexture = BlurTexture(g_pTexture);
	}

	// 把上一個步驟的結果當成貼圖來使用
	{
		// 消除畫面
		device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 150, 255), 1.0f, 0);
		// 設定轉換矩陣
		device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &g_projection_matrix);
		device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix);
		device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &world_matrix);
		// 設定光源
		SetupLightingDX9();

		device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
		device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);

		device->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
		device->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);

		// 畫出茶壼
		g_Model_DX9.Render();

		sModelMaterial_DX9 material;
		material.m_pTextures[0] = pBlurredTexture;
		material.Submit();
		// 計算貼圖矩陣
		Matrix4x4 inv_view_matrix = g_Control.GetCameraMatrix();
		Matrix4x4 uv_offset_matrix;
		uv_offset_matrix.Identity();
		uv_offset_matrix.Scale(0.5f, -0.5f, 0.5f);
		uv_offset_matrix[3].Set(0.5f, 0.5f, 0.5f, 1.0f);

		Matrix4x4 texture_matrix = inv_view_matrix * light_view_matrix * light_projection_matrix * uv_offset_matrix;
		Matrix4x4 indent_matrix = Matrix4x4::IdentityMatrix();
		// 設定轉換矩陣
		device->SetTransform(D3DTS_TEXTURE0, (D3DMATRIX *) &texture_matrix);
		device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &indent_matrix);
		// 開啟自動產生貼圖座標功能
		device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);
		device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT4 | D3DTTFF_PROJECTED);

		device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
		device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);

		device->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
		device->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);

		// 畫出地表
		g_Terrain_DX9.Render(0);

		device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
		device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
	}

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

	// 把背景backbuffer的畫面呈現出來
	device->Present( NULL, NULL, NULL, NULL );
}
Ejemplo n.º 21
0
static LPDIRECT3DTEXTURE9 BlurTexture(LPDIRECT3DTEXTURE9 pSource)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	Matrix4x4 identiyMatrix = Matrix4x4::IdentityMatrix();

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

	LPDIRECT3DSURFACE9 pFrameBufferBackup, pDepthBufferBackup;
	device->GetRenderTarget(0, &pFrameBufferBackup); pFrameBufferBackup->Release();
	device->GetDepthStencilSurface(&pDepthBufferBackup); pDepthBufferBackup->Release();
	device->SetRenderState(D3DRS_CULLMODE , D3DCULL_NONE);

	float weight_table[7] = {1.0f, 3.0f, 4.0f, 5.0f, 4.0f, 3.0f, 1.0f};
	float uv_offset_table[7] = {-3.0f, -2.0f, -1.0f, 0.0f, 1.0f, 2.0f, 3.0f};

	float sum = 0.0f;

	for ( int i=0; i<7; i++ )
	{
		sum += weight_table[i];
	}

	for ( int i=0; i<7; i++ )
	{
		weight_table[i] /= sum;
	}

	Vector4 vOneTexel_X(1.0f/(float)g_framebuffer_w, 0.0f, 0.0f, 0.0f);
	Vector4 vOneTexel_Y(0.0f, 1.0f/(float)g_framebuffer_h, 0.0f, 0.0f);
	Vertex_VT temp_quad[4];

	LPDIRECT3DSURFACE9 pSurface;
	g_pBlurTextures[0]->GetSurfaceLevel(0, &pSurface); 

	device->SetRenderTarget(0, pSurface);
	device->SetDepthStencilSurface(NULL);
	device->SetRenderState(D3DRS_ZENABLE, FALSE);
	device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 0), 1.0f, 0);

	device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
	device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

	device->SetTexture(0, pSource);
	device->SetFVF(D3DFVF_XYZW|D3DFVF_TEX1); 

	// 設定顏色來源為 texture factor * texture
	device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
	device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);

	// X方向blur
	for ( int i=0; i<7; i++ )
	{
		Vector4 vOffset = uv_offset_table[i] * vOneTexel_X;

		for ( int p=0; p<4; p++ )
		{
			temp_quad[p].m_Position = g_Quad[p].m_Position;
			temp_quad[p].m_Texcoord = g_Quad[p].m_Texcoord + vOffset;
		}

		int weight = (int) (weight_table[i] * 255);
		device->SetRenderState(D3DRS_TEXTUREFACTOR, D3DCOLOR_RGBA(weight, weight, weight, weight));
		device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, temp_quad, sizeof(Vertex_VT));
	}

	pSurface->Release();

	g_pBlurTextures[1]->GetSurfaceLevel(0, &pSurface); 
	device->SetRenderTarget(0, pSurface);
	device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 0), 1.0f, 0);
	device->SetTexture(0, g_pBlurTextures[0]);

	// Y方向blur
	for ( int i=0; i<7; i++ )
	{
		Vector4 vOffset = uv_offset_table[i] * vOneTexel_Y;

		for ( int p=0; p<4; p++ )
		{
			temp_quad[p].m_Position = g_Quad[p].m_Position;
			temp_quad[p].m_Texcoord = g_Quad[p].m_Texcoord + vOffset;
		}

		int weight = (int) (weight_table[i] * 255);
		device->SetRenderState(D3DRS_TEXTUREFACTOR, D3DCOLOR_RGBA(weight, weight, weight, weight));
		device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, temp_quad, sizeof(Vertex_VT));
	}

	pSurface->Release();

	// 還原設定
	device->SetRenderState(D3DRS_TEXTUREFACTOR, D3DCOLOR_RGBA(255, 255, 255, 255));
	device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);
	device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
	device->SetRenderState(D3DRS_ZENABLE, TRUE);
	device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);

	device->SetRenderTarget(0, pFrameBufferBackup);
	device->SetDepthStencilSurface(pDepthBufferBackup);

	return g_pBlurTextures[1];
}
Ejemplo n.º 22
0
// 使用DirectX 9來繪圖
void RenderFrameDX9(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();
	Vector4 vPlane(0.0f, 0.0f, 1.0f, -g_mirror_z);

	// 開始下繪圖指令
	device->BeginScene(); 

	{
		LPDIRECT3DSURFACE9 pFrameBufferBackup, pDepthBufferBackup;
		device->GetRenderTarget(0, &pFrameBufferBackup); pFrameBufferBackup->Release();
		device->GetDepthStencilSurface(&pDepthBufferBackup); pDepthBufferBackup->Release();

		LPDIRECT3DSURFACE9 pSurface;
		g_pTexture->GetSurfaceLevel(0, &pSurface); 

		device->SetRenderTarget(0, pSurface);
		device->SetDepthStencilSurface(g_pDepthStencil);

		device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 200, 255), 1.0f, 0);

		RenderModelDX9(true, &vPlane);

		pSurface->Release();
		device->SetRenderTarget(0, pFrameBufferBackup);
		device->SetDepthStencilSurface(pDepthBufferBackup);
	}

	// 把上一個步驟的結果當成貼圖來使用
	{
		// 消除畫面
		device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 150, 255), 1.0f, 0);

		RenderModelDX9(false, NULL);

		Matrix4x4 identMat; identMat.Identity();
		device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &identMat);

		sModelMaterial_DX9 material;
		material.m_pTextures[0] = g_pTexture;
		material.Submit();

		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);

		Matrix4x4 uv_offset_matrix;
		uv_offset_matrix.Scale_Replace(0.5f, -0.5f, 1.0f);
		uv_offset_matrix[3].Set(0.5f, 0.5f, 0.5f, 1.0f);

		Matrix4x4 inv_view_matrix = g_Control.GetViewMatrix();
		inv_view_matrix.FastInvert();

		Matrix4x4 texture_matrix = inv_view_matrix * g_mirror_view_matrix * g_projection_matrix * uv_offset_matrix;

		device->SetTransform(D3DTS_TEXTURE0, (D3DMATRIX *) &texture_matrix);
		device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);
		// D3DTTFF_PROJECTED告知direct3d裝置texcoord需要除以w
		device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT4|D3DTTFF_PROJECTED);
		float v[12];
		for ( int i=0; i<4; i++ )
		{
			g_Quad[i].m_Position.StoreXYZ(&v[i*3]);
		}
		// 畫出矩形
		device->SetFVF(D3DFVF_XYZ);
		//device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, g_Quad, sizeof(Vertex_V));
		device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, v, 12);

	}

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

	// 把背景backbuffer的畫面呈現出來
	device->Present( NULL, NULL, NULL, NULL );
}
Ejemplo n.º 23
0
// 描画
void CDOFText::Draw()
{
	LPDIRECT3DDEVICE9 lpDevice = D3D::GetDevice();

	// シーン描画開始
	D3D::Begin();

	// カメラ、投影設定
	m_Camera.SetCameraPos( &D3DXVECTOR3( 0, 0, -10 ) );
	lpDevice->SetTransform( D3DTS_VIEW, m_Camera.GetMatrix() );
	lpDevice->SetTransform( D3DTS_PROJECTION, m_Proj.GetMatrix() );

	// バックバッファ保存
	lpDevice->GetRenderTarget( 0, &m_lpBackBuffer );
	lpDevice->GetDepthStencilSurface( &m_lpZBuffer );

	CXfile::SetDrawShader( false );

	// パス1
	// テクスチャに描画
	// レンダーターゲット変更
	lpDevice->SetRenderTarget( 0, m_SceneTex.GetSurface() );
	lpDevice->SetDepthStencilSurface( m_SceneTex.GetZbuffer() );

	lpDevice->Clear(
		0,NULL,
		D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
		D3DCOLOR_XRGB( 0, 0xff, 0xff ), 1.0f, 0);

	lpDevice->SetRenderState( D3DRS_FOGENABLE, TRUE );
	lpDevice->SetRenderState( D3DRS_FOGCOLOR, D3DCOLOR_XRGB( 0, 0x00, 0x80 ) );
	lpDevice->SetRenderState( D3DRS_FOGTABLEMODE, D3DFOG_LINEAR );
	lpDevice->SetRenderState( D3DRS_FOGSTART, GetAddress( 0.f ) );
	lpDevice->SetRenderState( D3DRS_FOGEND,	GetAddress( 100.f ) );

	// 描画
	CMatrix mRot, mTrans;
	mRot.SetRotY( timeGetTime() / 2 % 360 );
	
	lpDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
	
	// テストメッシュ
	for( int i = 0; i < 5; i++ )
	{
		mTrans.SetTrans( -2 + i * 2, 0, -3 + i * 3 );
		lpDevice->SetTransform( D3DTS_WORLD, &(mRot * mTrans) );
		m_Mesh.Draw( lpDevice );
	}

	lpDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
	lpDevice->SetTransform( D3DTS_WORLD, &CMatrix() );
	m_Sphere.Draw( lpDevice );

	// パス2
	// 縮小させる
	// レンダーターゲット変更
	// ぼかし用(ビューポートも変える)
	D3DVIEWPORT9 Vp = { 0, 0, WINDOW_WIDTH / 4, WINDOW_HEIGHT / 4, 0.f, 1.f };
	lpDevice->SetViewport( &Vp );
	lpDevice->SetRenderTarget( 0, m_MiniSizeTex.GetSurface() );
	lpDevice->SetDepthStencilSurface( m_MiniSizeTex.GetZbuffer() );

	lpDevice->Clear(
		0,NULL,
		D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
		D3DCOLOR_XRGB( 0xff, 0xff, 0 ), 1.0f, 0);

	lpDevice->SetTransform( D3DTS_VIEW, &CMatrix() );
	lpDevice->SetTransform( D3DTS_PROJECTION, &CMatrix() );

	// テクスチャに描画したものを流す
	m_BlurShader.SetTexture( "g_Texture", m_SceneTex.GetTexture() );

	// 解像度を渡す
	D3DXVECTOR2 TexPos( 0.5f / Vp.Width, 0.5f / Vp.Height );
	m_BlurShader.SetValue( "g_Offset", &TexPos, sizeof(D3DXVECTOR2) );

	// シェーダー描画開始
	m_BlurShader.Begin();
	m_BlurShader.BeginPass( 0 );

	m_Scene.SetVertex( 2 );
	m_Scene.Draw( lpDevice );

	m_BlurShader.EndPass();
	m_BlurShader.End();

	// パス3
	// ぼかし入れる
	// レンダーターゲット変更
	lpDevice->SetRenderTarget( 0, m_BlurTex.GetSurface() );
	lpDevice->SetDepthStencilSurface( m_BlurTex.GetZbuffer() );
	lpDevice->SetViewport( &Vp );

	lpDevice->Clear(
		0,NULL,
		D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
		D3DCOLOR_XRGB( 0xff, 0xff, 0 ), 1.0f, 0);

	// 縮小画像をシェーダーに流す
	m_BlurShader.SetTexture( "g_Texture", m_MiniSizeTex.GetTexture() );
	
	// シェーダー描画開始
	m_BlurShader.Begin();
	m_BlurShader.BeginPass( 1 );

	lpDevice->SetFVF( CBoard::FVF_VERTEX );
	m_Scene.SetVertex( 2 );
	m_Scene.Draw( lpDevice );

	m_BlurShader.EndPass();
	m_BlurShader.End();

	// パス4
	// 深度計算を行う
	// レンダーターゲット変更
	// (ビューポートももとに戻す)
	Vp.Width = WINDOW_WIDTH;
	Vp.Height = WINDOW_HEIGHT;
	lpDevice->SetViewport( &Vp );
	lpDevice->SetRenderTarget( 0, m_DepthTex.GetSurface() );
	lpDevice->SetDepthStencilSurface( m_DepthTex.GetZbuffer() );

	lpDevice->Clear(
		0,NULL,
		D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
		D3DCOLOR_XRGB( 0, 0xff, 0xff ), 1.0f, 0);

	// シェーダーに値を渡す
	lpDevice->SetTransform( D3DTS_VIEW, m_Camera.GetMatrix() );
	lpDevice->SetTransform( D3DTS_PROJECTION, m_Proj.GetMatrix() );
	
	m_DepthShader.SetValue( "g_Proj",  m_Proj.GetMatrix(), sizeof(D3DXMATRIX) );
	m_DepthShader.SetValue( "g_View",  m_Camera.GetMatrix(), sizeof(D3DXMATRIX) );

	// シェーダー描画開始
	m_DepthShader.Begin();
	m_DepthShader.BeginPass( 0 );

	CXfile::SetDrawShader( true );
	//CXfile::SetShaderDevice( &m_DepthShader, "g_mWVP" );

	for( int i = 0; i < 5; i++ )
	{
		mTrans.SetTrans( -2 + i * 2, 0, -3 + i * 3 );
		lpDevice->SetTransform( D3DTS_WORLD, &(mRot * mTrans) );
		m_Mesh.Draw( lpDevice );
	}

	//m_DepthShader.SetValue( "g_World", &CMatrix(), sizeof(D3DXMATRIX) );
	lpDevice->SetTransform( D3DTS_WORLD, &CMatrix() );
	m_Sphere.Draw( lpDevice );

	m_DepthShader.EndPass();
	m_DepthShader.End();

	// パス5
	// 被写界深度適用
	// バックバッファに描画
	// バックバッファにレンダーターゲットを戻す
	lpDevice->SetRenderTarget( 0, m_lpBackBuffer );
	lpDevice->SetDepthStencilSurface( m_lpZBuffer );
	Vp.Width = WINDOW_WIDTH;
	Vp.Height= WINDOW_HEIGHT;
	lpDevice->SetViewport( &Vp );

	lpDevice->Clear(
		0,NULL,
		D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
		D3DCOLOR_XRGB( 0xff, 0, 0xff ), 1.0f, 0);

	// シェーダーに値を渡す
	m_DOFShader.SetTexture( "g_SceneTex", m_SceneTex.GetTexture() );	// シーン
	m_DOFShader.SetTexture( "g_BlurTex", m_BlurTex.GetTexture() );		// ぼかし
	m_DOFShader.SetTexture( "g_DepthTex", m_DepthTex.GetTexture() );	// 深度
	
	lpDevice->SetTransform( D3DTS_VIEW, &CMatrix() );
	lpDevice->SetTransform( D3DTS_PROJECTION, &CMatrix() );
	lpDevice->SetTransform( D3DTS_WORLD, &CMatrix() );

	// 板ポリゴンに描画
	m_DOFShader.Begin();
	m_DOFShader.BeginPass( 0 );

	m_Scene.SetVertex( 2 );
	m_Scene.Draw( lpDevice );

	m_DOFShader.EndPass();
	m_DOFShader.End();

	/*Vp.Y		= 300.f;
	Vp.Height	= 600.f;
	lpDevice->SetViewport( &Vp );*/

	DrawSceneByPolygon(lpDevice);

	SAFE_RELEASE( m_lpBackBuffer );
	SAFE_RELEASE( m_lpZBuffer );

	// シーン終了
	D3D::End();
}
Ejemplo n.º 24
0
void AverageLuminance(LPDIRECT3DTEXTURE9 pSource)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

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

	D3DXHANDLE texoffset_var = g_pExposureEffect->GetParameterByName(NULL, "vTexOffset");
	D3DXHANDLE image_var = g_pExposureEffect->GetParameterByName(NULL, "Image");

	float fTexX, fTexY;
	Vector4 vTexOffset_256x256[16];
	Vector4 vTexOffset_64x64[16];
	Vector4 vTexOffset_16x16[16];
	Vector4 vTexOffset_4x4[16];
	int index = 0;

	index=0;
	fTexX = 1.0f/256.0f;
	fTexY = 1.0f/256.0f;

	for ( int y=0; y<4; y++ )
	{
		for ( int x=0; x<4; x++ )
		{
			vTexOffset_256x256[index].Set(x*fTexX, y*fTexY, 0.0f, 0.0f);
			index++;
		}
	}

	index=0;
	fTexX = 1.0f/64.0f;
	fTexY = 1.0f/64.0f;
	
	for ( int y=0; y<4; y++ )
	{
		for ( int x=0; x<4; x++ )
		{
			vTexOffset_64x64[index].Set(x*fTexX, y*fTexY, 0.0f, 0.0f);
			index++;
		}
	}

	index=0;
	fTexX = 1.0f/16.0f;
	fTexY = 1.0f/16.0f;

	for ( int y=0; y<4; y++ )
	{
		for ( int x=0; x<4; x++ )
		{
			vTexOffset_16x16[index].Set(x*fTexX, y*fTexY, 0.0f, 0.0f);
			index++;
		}
	}

	index=0;
	fTexX = 1.0f/4.0f;
	fTexY = 1.0f/4.0f;

	for ( int y=0; y<4; y++ )
	{
		for ( int x=0; x<4; x++ )
		{
			vTexOffset_4x4[index].Set(x*fTexX, y*fTexY, 0.0f, 0.0f);
			index++;
		}
	}

	// 256x256 -> 64x164
	{
		device->SetRenderTarget(0, g_pFrameSurface[DOWNSAMPLED_64x64]);
		device->SetDepthStencilSurface(NULL);

		g_pExposureEffect->SetVectorArray(texoffset_var, (D3DXVECTOR4*)vTexOffset_256x256, 16);
		g_pExposureEffect->SetTexture(image_var, pSource);

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

		DrawFullScreenQuad(&g_Image64x64);

		g_pExposureEffect->EndPass();
		g_pExposureEffect->End();
	}
	// 64x64 -> 16x16
	{
		device->SetRenderTarget(0, g_pFrameSurface[DOWNSAMPLED_16x16]);
		device->SetDepthStencilSurface(NULL);

		g_pExposureEffect->SetVectorArray(texoffset_var, (D3DXVECTOR4*)vTexOffset_64x64, 16);
		g_pExposureEffect->SetTexture(image_var, pSource);

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

		DrawFullScreenQuad(&g_Image64x64);

		g_pExposureEffect->EndPass();
		g_pExposureEffect->End();
	}
	// 16x16 -> 4x4
	{
		device->SetRenderTarget(0, g_pFrameSurface[DOWNSAMPLED_4x4]);

		g_pExposureEffect->SetVectorArray(texoffset_var, (D3DXVECTOR4*)vTexOffset_16x16, 16);
		g_pExposureEffect->SetTexture(image_var, g_pFrameBuffer[DOWNSAMPLED_16x16]);

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

		DrawFullScreenQuad(&g_Image16x16);

		g_pExposureEffect->EndPass();
		g_pExposureEffect->End();

	}
	// 4x4 -> 1x1
	{
		device->SetRenderTarget(0, g_pFrameSurface[LUMINANCE_CURRENT]);

		g_pExposureEffect->SetVectorArray(texoffset_var, (D3DXVECTOR4*)vTexOffset_4x4, 16);
		g_pExposureEffect->SetTexture(image_var, g_pFrameBuffer[DOWNSAMPLED_4x4]);

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

		DrawFullScreenQuad(&g_Image4x4);

		g_pExposureEffect->EndPass();
		g_pExposureEffect->End();
	}
}
Ejemplo n.º 25
0
static void ParticleSimulation(void)
{
	LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9();

	device->SetRenderTarget(0, g_pSurfaces[TEX_POSITION1]);
	device->SetRenderTarget(1, g_pSurfaces[TEX_VELOCITY1]);
	device->SetDepthStencilSurface(NULL);

	RECT rect;
	rect.left = 0;
	rect.right = g_texture_width;
	rect.top = 0;
	rect.bottom = g_num_particles / g_texture_width;
	
	if ( rect.bottom==0 )
		return;

	device->SetScissorRect(&rect);
	device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);

	D3DXHANDLE shader = g_pParticleFX->GetTechniqueByName("Simulate");
	D3DXHANDLE positiontex_var = g_pParticleFX->GetParameterByName(NULL, "PositionTex");
	D3DXHANDLE velocitytex_var = g_pParticleFX->GetParameterByName(NULL, "VelocityTex");
	D3DXHANDLE noisetex_var = g_pParticleFX->GetParameterByName(NULL, "NoiseTex");
	D3DXHANDLE force_var = g_pParticleFX->GetParameterByName(NULL, "Force");
	D3DXHANDLE time_var = g_pParticleFX->GetParameterByName(NULL, "fTimeAdvance");
	D3DXHANDLE rand_var = g_pParticleFX->GetParameterByName(NULL, "Rand");
	D3DXHANDLE tan_var = g_pParticleFX->GetParameterByName(NULL, "fTan");
	D3DXHANDLE liferange_var = g_pParticleFX->GetParameterByName(NULL, "LifeRange");
	D3DXHANDLE speedrange_var = g_pParticleFX->GetParameterByName(NULL, "SpeedRange");
	D3DXHANDLE sizerange_var = g_pParticleFX->GetParameterByName(NULL, "SizeRange");

	Vector4 vForce = g_vForce * g_fTimeAdvance;
	Vector4 vRand[3];
	Vector4 vLifeRange(1.0f, 2.0f, 0.0f, 0.0f);
	Vector4 vSpeedRange(1.0f, 2.0f, 0.0f, 0.0f);
	Vector4 vSizeRange(0.01f, 0.02f, 0.0f, 0.0f);

	for ( int i=0; i<3; i++ )
	{
		vRand[i][0] = float(rand()%1024)/1024.0f;
		vRand[i][1] = float(rand()%1024)/1024.0f;
		vRand[i][2] = float(rand()%1024)/1024.0f;
		vRand[i][3] = float(rand()%1024)/1024.0f;
	}

	g_pParticleFX->SetTechnique(shader);
	g_pParticleFX->SetTexture(positiontex_var, g_pTextures[TEX_POSITION0]);
	g_pParticleFX->SetTexture(velocitytex_var, g_pTextures[TEX_VELOCITY0]);
	g_pParticleFX->SetTexture(noisetex_var, g_pNoiseTexture);
	g_pParticleFX->SetVector(force_var, (D3DXVECTOR4*)&vForce);
	g_pParticleFX->SetVectorArray(rand_var, (D3DXVECTOR4*)&vRand, 3);
	g_pParticleFX->SetVector(liferange_var, (D3DXVECTOR4*)&vLifeRange);
	g_pParticleFX->SetVector(speedrange_var, (D3DXVECTOR4*)&vSpeedRange);
	g_pParticleFX->SetVector(sizerange_var, (D3DXVECTOR4*)&vSizeRange);
	g_pParticleFX->SetFloat(time_var, g_fTimeAdvance);
	g_pParticleFX->SetFloat(tan_var, FastMath::Tan(FastMath::DegToRad(g_fEmitTheta)));

	g_pParticleFX->Begin(NULL,0);
	g_pParticleFX->BeginPass(0);
		GutDrawFullScreenQuad_DX9(&g_ImageInfo);
	g_pParticleFX->EndPass();
	g_pParticleFX->End();

	device->SetRenderTarget(1, NULL);
	device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
}