• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Warwick Michael Dumas

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

2 posts in this topic

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.


Share this post

Link to post
Share on other sites

Can you describe what the arguments are for your Graph.Initialize function?  If they are the viewport coordinates, then it seems that they may be overlapping somehow - but that might just be because I'm not sure how they are interpreted.


Share this post

Link to post
Share on other sites

Sure here are the arguments

HRESULT surfacegraph::Initialise(int nperRow, // data points per row if square grid data to be supplied
int left, int top, int width, int height, float fscale, D3DXVECTOR3 in_Eye,
bool Polar = 0, 
int number_in_outermost = 0, // data points in outermost circle if radially distributed data
int number_circles = 0,    // number of circles              if radially distributed data
float radius_outermost = 0.0f    //  max radius              if radially distributed data



The viewport coordinates are left,top,width,height. (And it's not radially distributed data). The calls to Initialise are in the top code box above. So yes, they are a bit overlapping. I think that should be OK though... the rendering of each object is meant to be independent.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0