Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 21 Jun 2012
Offline Last Active Feb 28 2013 02:23 PM

Topics I've Started

Multiple viewports and shadow maps -- overwriting or not initialised properly?

13 January 2013 - 10:45 AM

Hi. N00b here.

I wrote a class to create a graph of a surface and this class includes a shadow map. With one object it works fine. With multiple objects, strange things are happening. See picture - for all six objects the same calls are made, only the viewport coordinates are different. Only one of them displays the shadow correctly. The rendering sequence of the objects makes a difference to the behaviour, so it seems to me it is a problem with the way the shadow maps get initialised.


For the attached picture, this was the way the objects were treated:


surfacegraph Graph3,  Graph4, Graph2, Graph5, Graph6,Graph;




	Graph2.Render(); // the rendering sequence appears to make a big difference
	Graph4.Render(); // to the shadow maps that result



This creates the picture where Graph2 displays correctly, Graph displays with no shadow and all the rest display completely in shadow.


However I don't see why the objects are affecting each others' shadow maps.


This is some of the class definition:


class surfacegraph
	static const int SHADOWMAPRESOLUTION = 4096;

	int perRow; int texWidth,texHeight; int numVertices, numTriangles;
	IDirect3DVertexBuffer9* TerrainVertexBuffer;
	IDirect3DIndexBuffer9* TerrainIndexBuffer;
	IDirect3DTexture9*  texture_grid;

	D3DVIEWPORT9  shadow_vp;

	ID3DXEffect* mFX;

	D3DXMATRIXA16 matView;
	D3DXMATRIXA16 matProj;
	DirLight mLight;
	Mtrl     mWhiteMtrl;

	D3DXVECTOR3 Eye; // eye position
	// for shadow map:
	IDirect3DSurface9* pSurface;   // never gets used for anything though - not sure why this was here
	DrawableTex2D* mShadowMap;   
	D3DXHANDLE   mhTech;
	D3DXHANDLE   mhTex;
	D3DXHANDLE   mhEyePos;         // for specular
	D3DXHANDLE   mhDiffuseMtrl;
	D3DXHANDLE   mhAmbientMtrl;
	D3DXHANDLE   mhLightVector;
	D3DXHANDLE   mhLightDiffuseColour;
	D3DXHANDLE   mhLightAmbientColour;
	D3DXHANDLE   mhLightSpecularColour;
	D3DXHANDLE   mhSpecularMtrl;
	D3DXHANDLE   mhSpecularPower;
	D3DXHANDLE   mhBuildShadowMapTech;
	D3DXHANDLE   mhShadowMap;
	D3DXHANDLE   mhSwitch;

	float xyscale,nscale;

	HRESULT Initialise(  parameters  );
	HRESULT SetxzDataAutomatic (float minimum, float maximum, float in_xyscale);
        HRESULT SetDataFromDoubleScaled(double **, double);
	HRESULT SetDataAgain(float **);
	HRESULT SetColourData(float **, float **, float **);

	VOID surfacegraph::Render(void);



The following is the start of the Render method for the class. See code comments.


VOID surfacegraph::Render()
	// without this^^^, 5 graphs, all except the last rendered, appear as small shadows on white


	// completely whites out all but last-to-render graph. No idea why. 
	// That may suggest that Device->Clear is affecting the back buffer but we also know it is affecting the shadow map!
	Direct3D.pd3dDevice->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
				D3DCOLOR_XRGB( 255, 255, 255 ), 1.0f, 0 );   
                // this clearly does affect shadow map successfully...        

                    //    0x00000000, 1.0f, 0 );		
                    // because this^^^^^ leads to everything being in shadow
	UINT numPasses = 0;
	DXChk(mFX->Begin(&numPasses, 0));
	DXChk(mFX->SetMatrix(mhLightWVP, &(mLightVP)));
     Direct3D.pd3dDevice->SetStreamSource(0, TerrainVertexBuffer, 0, sizeof(VertexPNT));
	for (UINT ii = 0; ii < numPasses; ++ii)
	// axis drawing in fixed function pipeline:



In case it is necessary background information, the "DrawableTex2D" class is 


class DrawableTex2D
	IDirect3DTexture9*    mTex;
	ID3DXRenderToSurface* mRTS;
	IDirect3DSurface9*    mTopSurf;

	UINT         mWidth;
	UINT         mHeight;
	UINT         mMipLevels;
	D3DFORMAT    mTexFormat;
	bool         mUseDepthBuffer;
	D3DFORMAT    mDepthFormat;
	D3DVIEWPORT9 mViewPort;
	bool         mAutoGenMips;

        DrawableTex2D(UINT width, UINT height, UINT mipLevels,
		D3DFORMAT texFormat, bool useDepthBuffer,
		D3DFORMAT depthFormat, D3DVIEWPORT9& viewport, bool autoGenMips);

	IDirect3DTexture9* d3dTex();

	void beginScene();
	void endScene();
	void onLostDevice();
	void onResetDevice();

	DrawableTex2D(const DrawableTex2D& rhs);
	DrawableTex2D& operator=(const DrawableTex2D& rhs);

IDirect3DTexture9* DrawableTex2D::d3dTex()
	return mTex;

void DrawableTex2D::onResetDevice()

	DXChk(D3DXCreateTexture(Direct3D.pd3dDevice, mWidth, mHeight, mMipLevels, usage, mTexFormat, D3DPOOL_DEFAULT, &mTex));
	DXChk(D3DXCreateRenderToSurface(Direct3D.pd3dDevice, mWidth, mHeight, mTexFormat, mUseDepthBuffer, mDepthFormat, &mRTS));
	DXChk(mTex->GetSurfaceLevel(0, &mTopSurf));

void DrawableTex2D::beginScene()
	mRTS->BeginScene(mTopSurf, &mViewPort);
void DrawableTex2D::endScene()


The following method is called, once only, after the graph object has been declared.


HRESULT surfacegraph::Initialise(  parameters  )

	shadow_vp.X = 0; shadow_vp.Y = 0;
	shadow_vp.Height = SHADOWMAPRESOLUTION;
	shadow_vp.MinZ = 0.0f;
	shadow_vp.MaxZ = 1.0f;
	mShadowMap = new DrawableTex2D(SHADOWMAPRESOLUTION, SHADOWMAPRESOLUTION, 1, D3DFMT_R32F, true, D3DFMT_D24X8, shadow_vp, false);
	vp.X = left;
	vp.Y = top;
	vp.Width = width;
	vp.Height = height;
	vp.MinZ = 0.0f;
	vp.MaxZ = 0.0f;

	float GlobalAspectRatio = ((float)vp.Width)/(float)vp.Height;
	// Create the FX from a .fx file.
	ID3DXBuffer* errors = 0;
	D3DXCreateEffectFromFile(Direct3D.pd3dDevice, "test1.fx", 
		0, 0, D3DXSHADER_DEBUG, 0, &mFX, &errors);
	if( errors ) 
		MessageBox(0, (char*)errors->GetBufferPointer(), 0, 0);	
	// Obtain handles.
	mhTech = mFX->GetTechniqueByName("WarwickTech");



	static D3DXVECTOR3 vLookatPt( 0.0f, 1.2f, 0.0f );
	static D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
	Eye = in_Eye;

	D3DXMatrixLookAtLH( &matView, &Eye, &vLookatPt, &vUpVec );	
	D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4.0f, GlobalAspectRatio, 1.0f, 12.0f);
	mLight.dirW    = D3DXVECTOR3(-1.0f, -0.5f, 0.5f);// remember y is height
	D3DXVec3Normalize(&mLight.dirW, &mLight.dirW);
	mLight.ambient = D3DXCOLOR(0.4f, 0.4f, 0.4f, 1.0f);
	mLight.diffuse = D3DXCOLOR(0.6f, 0.6f, 0.6f, 1.0f);
	mLight.spec    = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);
	mWhiteMtrl.ambient = WHITE*1.0f;
	mWhiteMtrl.diffuse = WHITE*1.0f;
	mWhiteMtrl.spec    = WHITE*1.0f;
	mWhiteMtrl.specPower = 8.0f;
	// Shadow mapping:
	D3DXMATRIX lightView;
	D3DXVECTOR3 lightPosW(20.0f, 10.0f, -10.0f);
	D3DXVECTOR3 lightTargetW(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 lightUpW(0.0f, 1.0f, 0.0f);	

	D3DXMatrixLookAtLH(&lightView, &lightPosW, &lightTargetW, &lightUpW);
	D3DXMATRIX lightLens;
	float lightFOV = D3DX_PI*0.25f;
	D3DXMatrixPerspectiveFovLH(&lightLens, lightFOV, 1.0f, 1.0f, 100.0f);
	mLightVP = lightView*lightLens;

        // graph data:
	perRow = nperRow;

	numVertices = perRow*perRow;
	numTriangles = 2*(perRow-1)*(perRow-1);
	texWidth = perRow;
	texHeight = perRow;
	definedindices = 0;
	if (
	Direct3D.pd3dDevice->CreateIndexBuffer(numTriangles*3*sizeof(DWORD),D3DUSAGE_WRITEONLY,	D3DFMT_INDEX32,	D3DPOOL_MANAGED,&TerrainIndexBuffer,0),2)
	DXChk(	D3DXCreateTexture( Direct3D.pd3dDevice, texWidth,texHeight, 0,0,D3DFMT_X8R8G8B8,D3DPOOL_MANAGED, &texture_grid),3)			
		return E_FAIL;
	D3DSURFACE_DESC textureDesc;
	texture_grid->GetLevelDesc(0, &textureDesc);
	if ( textureDesc.Format != D3DFMT_X8R8G8B8 ) return E_FAIL;
	return S_OK;


Sorry to provide so much code, I'm not all that sure what is going on here. I have tried tinkering with what happens at the start of the Render method but I don't seem to know what I'm doing, the results (as described in the code comments) are hard for me to interpret.


Grateful for any suggestions.


I am happy to dropbox the whole source if anyone wishes to tinker with it.

Texturing artifact of some kinds

23 July 2012 - 03:49 AM

Hi. I mentioned before my unconventional decision to use DX9 as a rendering platform. I've now all but completed this project but something does not look quite right. I'm drawing a dish which is yellow on the inside with purple outside it; the back of the dish shows a purple stripe, which it should not.

I tried it with two different sorts of meshes. One of them is with points arranged in concentric circles and an algorithm that basically sweeps anticlockwise over points on the previous (inner) circle to create triangles for each point on each circle.
The other is square mesh with two triangles per square. I always use this square mesh to define the texture dynamically, and it is defined in such a way that radially equal points always get the same colour.
I calculate vertex normals in the main program and light the vertices in a vertex shader. I also do shadow mapping in the pixel shader but I'm pretty sure that's nothing to do with the problem.

Here are some of my output avi files
'radial' mesh:
'square' mesh:
Those are both so high resolution that it's likely 1 triangle per pixel or more. That may have something to do with the problem although I don't see why it should do. It probably is best to focus attention on the cutaway animation - the lower one - but see also on the upper one, it can be seen that going upwards, the stripes form a sinusoidal pattern.

Here is a somewhat lower resolution mesh. There is some mistexturing probably because the wall of the dish is very thin. 'radial' mesh.

I tried to upload all the code, because I don't know which bit is relevant, but it says 'You aren't permitted to upload this kind of file'. Don't know why text files cause such umbrage but there you go.

Anyway maybe looking at this, someone will have some suggestion of what could be going wrong.
I've checked that the computed normals all have the same sign for the y-component, and tried inverting them. And I'm certain the texture doesn't have a bizarre purple region. I also can't believe it's meant to look this way with the lighting. I'm at my wits' end, any suggestions appreciated. Posted Image

Fixed function and shader not doing same thing?

21 June 2012 - 03:50 PM

Hi, first time I have posted here. I am more-or-less a novice with DirectX. I am working on a graphical output for my scientific simulation. The reason I chose DX over ray-tracing is partly that I already started learning DX, and partly just that I am not actually looking for realistic graphics - e.g. I want to light things arbitrarily with silly ambient lighting inside a deep dark well, but enough about that.

The problem I am having is the following.

I make my surface graph using a shader which is pretty simple and based on the book by Frank Luna.
I am feeding a vertex buffer which (I have checked with watches) has x,z coordinates that populate a square (-3,3)x(-3,3).
That appeared to work.

Then I am also trying to draw axes, in the fixed function pipeline, by using DrawPrimitiveUP. At the moment I am trying to draw one line with D3DPT_LINESTRIP and passing data (-3.02,0,3.02),(-3.02,0,-3.02).
The line appears, but well away from the edge of the graph! If I instead set (-2.4,0,2.4),(-2.4,0,-2.4) then it appears still a bit outside the graph square.
(Meanwhile, if I draw line (3,0,3),(3,10,3) then it looks like it appears in the correct place, though I'm still checking that further.)

I have checked, checked, checked that the view*projection matrix being used in SetTransform is the same as is passed to the shader, and the shader does not do anything funny.

Any ideas what could be going on I would welcome. Quite happy to provide more information if there's anything you'd like to ask.