• Advertisement
Sign in to follow this  

DirectX questions

This topic is 3318 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I am pretty new to DirectX programming and have attempted some examples (mainly from Frank Luna's book). The examples are pretty straight forward but still a bit basic. Anyway, I attempted to work on the framework and implement a simple scene that loads a .X mesh and displays it with some lighting effects. I got a bit stuck on this bit :
Device->BeginScene();


		//Activate an Effect
		mFX->SetTechnique(mhTech);
		//any effect parameters that need to be updated must be set
		//Set world matrix to idendity for sphere as no translation
		D3DXMatrixIdentity(&mWorld);//multiply by one
		
		mWorldView = mView*mWorld;
		mFX->SetMatrix(mhWorldView,&(mWorldView));
		//determine the inversetranspose matrices of worldview and view matrices
		D3DXMatrixInverse(&mWorldViewIT,NULL, &mWorldView);
		D3DXMatrixTranspose(&mWorldViewIT, &mWorldViewIT);
		mFX->SetMatrix(mhWorldViewIT,&(mWorldViewIT));
		mFX->SetMatrix(mhWVP,&(mWorld*mView*mproj));

		mFX->SetVector( mhvLightDirs, (D3DXVECTOR4*)&vLightDirs);
		mFX->SetVector( mhvLightCols, (D3DXVECTOR4*)&vLightCols);

		mcd = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f);
		mcs = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
		mca = D3DXCOLOR(0.0f, 0.0f, 0.4f, 1.0f);

		// Setup our lighting parameters
			mFX->SetValue(mhvLightDirs, &(vLightDirs),sizeof(D3DXVECTOR4));
			mFX->SetValue(mhDiffuseMtrl,&(mcd) ,sizeof(D3DXCOLOR));
			mFX->SetValue(mhSpecMtrl,&(mcs) ,sizeof(D3DXCOLOR));
			mFX->SetValue(mhAmbMtrl,&(mca) ,sizeof(D3DXCOLOR));
			mFX->SetValue(mhLightCol,&(lc) ,sizeof(D3DXCOLOR));

		//Begin passes
		UINT numPasses=0;
		mFX->Begin(&numPasses,0);
		mFX->BeginPass(0);	

		for(DWORD t = 0; t < numMaterials; t++)    // loop through each subset
			{	
				
				//Device->SetMaterial(&material);    // set the appropriate material for the subset
				mFX->SetValue(mhDiffuseMtrl,&(material[t].Diffuse) ,sizeof(D3DCOLORVALUE));
				mFX->SetValue(mhSpecMtrl,&(mcs) ,sizeof(D3DXCOLOR));
				mFX->SetValue(mhAmbMtrl,&(material[t].Ambient) ,sizeof(D3DCOLORVALUE));
				if(texture[t] != NULL)    // if the subset has a texture (if texture is not NULL)
				{
				mFX->SetTexture(mhgTex, texture[t]);    // ...then set the texture
				}
				else
				{
				mFX->SetTexture(mhgTex,mWhiteTex);    // ...then set the texture
				}

				mFX->CommitChanges();
				myMesh->DrawSubset(t);    // draw the subset
				//drawCube();
			}

		mFX->EndPass();	
		mFX->End();
This works fine a shows my model and I am able to rotate the camera around it. The thing is, I am trying to add a flat piece of terrain under the model. Right now I use the function drawCube(); to represent the terrain. I noticed that I can't translate the cube to where I wish it to be. No matter how I tried the cube just won't translate >< Now I know that I must be missing something (or perhaps have something to do with WVP transformations here). I am really new to this (and also graphics programming) so I hope someone could perhaps tell me what I am doing wrong here. Thanks

Share this post


Link to post
Share on other sites
Advertisement
Maybe I'm misreading, but I only see one spot where you're setting the concatenated World/Perspective/View matrix. Everything you draw from that point forward will therefore use that matrix. In case I'm not misreading, if you want to draw an object in a different place, you have to set a different matrix for that object.

Share this post


Link to post
Share on other sites
Quote:
Original post by CDProp
Maybe I'm misreading, but I only see one spot where you're setting the concatenated World/Perspective/View matrix. Everything you draw from that point forward will therefore use that matrix. In case I'm not misreading, if you want to draw an object in a different place, you have to set a different matrix for that object.


Hi and thanks for answering. Well you have not misread. To be honest I am not that sure about the WVP matrix used here. I have tried the below method but it still doesn't seem to work. First of all I am getting the errors that I am redefining
numPasses
(which I know I did, but not sure of how not to) and secondly if I take out that line, the cube won't render.


D3DXMatrixIdentity(&mWorld);//multiply by one
D3DXMATRIX temp, cube;
//Set world matrix to idendity
D3DXMatrixIdentity(&mWorld);
D3DXMatrixTranslation (&temp, 0, -1, 0);
temp = temp*cube;
Device->SetTransform(D3DTS_WORLD, &temp);
mWorldView = temp; //mView*mWorld;
mFX->SetMatrix(mhWorldView,&(mWorldView));
//determine the inversetranspose matrices of worldview and view matrices
D3DXMatrixInverse(&mWorldViewIT,NULL, &mWorldView);
D3DXMatrixTranspose(&mWorldViewIT, &mWorldViewIT);
mFX->SetMatrix(mhWorldViewIT,&(mWorldViewIT));
mFX->SetMatrix(mhWVP,&(mWorld*mView*mproj));

//Begin passes
UINT numPasses=0;
mFX->Begin(&numPasses,0);
//draw cube
for(int i=0; i < numPasses; ++i)
{
mFX->BeginPass(i);
drawCube();
mFX->EndPass();

}
mFX->End();

Share this post


Link to post
Share on other sites
Is that just a piece of the function? Because numPasses is only defined once in that code segment, but if that's only a piece of the function, it's possible that it's defined elsewhere as well. Keep in mind: you can only define a variable once.

Also, it is unnecessary to set numPasses to zero again, prior to rendering the cube, because the Begin() method, which you call on the next line, is going to assign a value to it, which is the number of passes in that effect technique.

So you should be able to take this line out entirely:

UINT numPasses=0;

And that should make the compile error go away.

I also have a few more comments. One is that you set mWorld to the identity matrix twice, which is probably unnecessary. Also, you declare two other matrices, temp and cube, but cube is not initialized before it is used. Check it out:

D3DXMATRIX temp, cube; // Declaring temp and cube
//Set world matrix to idendity
D3DXMatrixIdentity(&mWorld);
D3DXMatrixTranslation (&temp, 0, -1, 0); // Assigning to temp
temp = temp*cube; // multiplying temp times cube, but cube is unitialized!


Next, you call SetMatrix, passing in temp (well, passing in mWorldView, which is equal to temp), and I'm guessing that the reason the cube isn't drawing is because the matrix is full of garbage: cube was created, but not initialized, meaning it probably had garbage values, and because of this line:

temp = temp*cube;

...temp has garbage values as well. As does mWorldView, because of this line:

mWorldView = temp;

Finally, don't use SetTransform. It's a fixed-function pipeline thing, and it probably doesn't even work at all if you're using shaders.

Here is what I would do:


D3DXMatrixTranslation (&mWorld, 0, -1, 0);
mWorldView = mView*mWorld;
mFX->SetMatrix(mhWorldView,&(mWorldView));

//determine the inversetranspose matrices of worldview and view matrices
D3DXMatrixInverse(&mWorldViewIT, NULL, &mWorldView);
D3DXMatrixTranspose(&mWorldViewIT, &mWorldViewIT);
mFX->SetMatrix(mhWorldViewIT,&(mWorldViewIT));
mFX->SetMatrix(mhWVP,&(mWorld*mView*mproj));

//Begin passes
mFX->Begin(&numPasses,0);
//draw cube
for(int i=0; i < numPasses; ++i)
{
mFX->BeginPass(i);
drawCube();
mFX->EndPass();

}
mFX->End();



I didn't check it entirely for errors, but that should include everything I mentioned, at least.


Share this post


Link to post
Share on other sites
Thanks a lot! You are really godsend :) Been working alone and can't believe I missed that. One last question I promise, hopefully you can offer some insight to this one.

I have been working on a simple planar shadow scene. This code basically generates quad terrain and generate the shadow of a mesh model on it. At the moment everything looks fine (if the model floats above the ground). But when I try to play around with dynamically moving the camera (or the model in this case), things got a bit weird. At the moment the camera rotates around the stationary model. But when I try to move the camera around in the world things get really messed up.

Also I noticed the shadow is always a fixed distance from the model (if I translate model up to high the shadow will follow instead of "shrinking" on the terrain)

So basically I am partially stuck on this one as I am pretty sure the way I generate the "ground" and shadow on it is wrong...

In short, there is something wrong about how I project shadows in this code and I can't extend the camera's functionality to move around the world.I know it might be too much to ask but just hoping if you (or anyone) who is kind enough to have a look at what I have at the moment and maybe give some thoughts / comments.

Thanks! (I took out the irrelevant bits)




//
// Classes and Structures
//
struct Vertex
{

float x, y, z;
float nx, ny, nz;
float u, v;
static IDirect3DVertexDeclaration9* Decl;
};

void setUpRenderStates()
{
//disable z write enable as rendering transparent obf
Device->SetRenderState(D3DRS_ZWRITEENABLE,true);

}

void CreateFX()
{

D3DXCreateEffectFromFile(Device, "Phong_Shader.fx", 0, 0, D3DXSHADER_DEBUG,0, &mFX,&errors);
if(errors)
MessageBox(0, (char*)errors->GetBufferPointer(),0,0);

mhTech = mFX ->GetTechniqueByName("PhongShaderTech");
mhWVP = mFX->GetParameterByName(0, "gWVP");
mhWorldView = mFX->GetParameterByName(0, "gWorldView");
mhWorldViewIT = mFX->GetParameterByName(0, "gWorldViewIT");
mhProj=mFX->GetParameterByName(0, "gProj");
mhDiffuseMtrl=mFX->GetParameterByName(0,"gDiffuseMtrl");
mhSpecMtrl=mFX->GetParameterByName(0,"gSpecMtrl");
mhAmbMtrl=mFX->GetParameterByName(0,"gAmbMtrl");
mhvLightDirs= mFX->GetParameterByName(0,"gLightDir");//mFX->GetParameterByName(0,"gvLightDirs");
mhvLightCols=mFX->GetParameterByName(0,"gvLightCol");
mhgTex=mFX->GetParameterByName(0, "gTex");
}

void setCameraView(void)
{
D3DXMATRIX yRot;
D3DXMATRIX xRot;
D3DXMATRIX Trans;

D3DXMatrixRotationY(&(yRot), myCamera.rot.y);
D3DXMatrixRotationX(&(xRot), myCamera.rot.x);
D3DXMatrixTranslation(&Trans, -myCamera.pos.x, -myCamera.pos.y, -myCamera.pos.z);
//yRot*=Trans;
mView = xRot*yRot*Trans;

}

void SetUpLights()
{

vLightDirs =D3DXVECTOR4( 0.577f, -0.577f, 0.577f, 0.0);
D3DXVECTOR4 vLightCols = D3DXVECTOR4( 1.0f, 1.0f, 1.0f, 1.0f );

}

void Create_Plane() // Confusing name but it basically loads the .X model
{

// local variable ()
LPD3DXBUFFER bufMeshMaterial;

D3DXLoadMeshFromX("test.x", // load this file
D3DXMESH_SYSTEMMEM, // load the mesh into system memory
Device, // the Direct3D Device
NULL, // we aren't using adjacency
&bufMeshMaterial, // put the materials here
NULL, // we aren't using effect instances
&numMaterials, // the number of materials in this model
&myPlane); // put the mesh here
ID3DXMesh* temp=0;
myPlane->CloneMeshFVF( 0, D3DFVF_CUSTOMVERTEX, Device, &temp);
myPlane->Release();
myPlane = temp;
D3DXComputeNormals(myPlane, NULL);

D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufMeshMaterial->GetBufferPointer();

plane_material = new D3DMATERIAL9[numMaterials];
plane_texture = new LPDIRECT3DTEXTURE9[numMaterials];

for(DWORD i = 0; i < numMaterials; i++) // for each material...
{
plane_material = tempMaterials.MatD3D; // get the material info...
plane_material.Ambient = plane_material.Diffuse; // and make ambient the same as diffuse

// if there is a texture to load, load it
USES_CONVERSION; // allows certain string conversions
// if there is a texture to load, load it
if(FAILED(D3DXCreateTextureFromFile(Device,
tempMaterials.pTextureFilename,
&plane_texture)))
plane_texture = mWhiteTex; // if there is no texture, set the texture to white
}
};

void Render_Plane(bool shadow){
//Begin passes
UINT numPasses=0;
mFX->Begin(&numPasses,0);
mFX->BeginPass(0);

if(!shadow)
{
for(DWORD t = 0; t < numMaterials; t++) // loop through each subset
{

//Device->SetMaterial(&material); // set the appropriate material for the subset
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
mFX->SetValue(mhDiffuseMtrl,&(plane_material[t].Diffuse) ,sizeof(D3DCOLORVALUE));
mFX->SetValue(mhSpecMtrl,&(mcs) ,sizeof(D3DXCOLOR));
mFX->SetValue(mhAmbMtrl,&(plane_material[t].Ambient) ,sizeof(D3DCOLORVALUE));
if(plane_texture[t] != NULL) // if the subset has a texture (if texture is not NULL)
{
mFX->SetTexture(mhgTex, plane_texture[t]); // ...then set the texture
}
else
{
mFX->SetTexture(mhgTex,mWhiteTex); // ...then set the texture
}

mFX->CommitChanges();
myPlane->DrawSubset(t); // draw the subset
}
}
else
{
for(DWORD t = 0; t < numMaterials; t++) // loop through each subset
{

mcd = D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.5f);
mcs = D3DXCOLOR(0.0f,0.0f, 0.0f, 0.5f);
mca = D3DXCOLOR(0.0f, 0.0f,0.0f, 0.5f);

mFX->SetValue(mhDiffuseMtrl,&(mcd) ,sizeof(D3DXCOLOR));
mFX->SetValue(mhSpecMtrl,&(mcs) ,sizeof(D3DXCOLOR));
mFX->SetValue(mhAmbMtrl,&(mca) ,sizeof(D3DXCOLOR));
mFX->SetValue(mhLightCol,&(lc) ,sizeof(D3DXCOLOR));
//set alpha blend to trus so that the shadow is alpha blended
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);


//mFX->SetTexture(mhgTex,mWhiteTex); // ...then set the texture
mFX->CommitChanges();
myPlane->DrawSubset(t); // draw the subset

}
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
}

mFX->EndPass();
mFX->End();

};

void drawTexGround()
{
D3DXMATRIX temp;
D3DXMatrixIdentity(&temp);

mWorldView = temp*mWorld;
mFX->SetMatrix(mhWorldView,&(mWorldView));
//determine the inversetranspose matrices of worldview and view matrices
D3DXMatrixInverse(&mWorldViewIT,NULL, &mWorldView);
D3DXMatrixTranspose(&mWorldViewIT, &mWorldViewIT);
mFX->SetMatrix(mhWorldViewIT,&(mWorldViewIT));
mFX->SetMatrix(mhWVP,&(mWorld*temp*mproj));

//update colour of ground
mcd = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
mcs = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
mca = D3DXCOLOR(0.0f, 0.0f, 0.09f, 1.0f);
mFX->SetValue(mhDiffuseMtrl,&(mcd) ,sizeof(D3DXCOLOR));
mFX->SetValue(mhSpecMtrl,&(mcs) ,sizeof(D3DXCOLOR));
mFX->SetValue(mhAmbMtrl,&(mca) ,sizeof(D3DXCOLOR));
mFX->SetValue(mhLightCol,&(lc) ,sizeof(D3DXCOLOR));

//draw ground
//Begin passes
//update stencil to one as only want to draw shadow on the plane
UINT numPasses=1;
mFX->Begin(&numPasses,0);

for(int i=0; i < numPasses; ++i)
{
mFX->BeginPass(i);
mFX->SetTexture(mhgTex, gpGroundTex);
mFX->CommitChanges();
Device->SetStreamSource(0, gpGroundVB, 0, sizeof(Vertex));
Device->SetIndices(gpGroundIB);
Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0,size*size, 0, (size-1)*(size-1)*2);
mFX->EndPass();

}
mFX->End();
}
void drawShadow()
{
bool shadow = true;

Device->SetRenderState(D3DRS_STENCILENABLE, true);
Device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
Device->SetRenderState(D3DRS_STENCILREF, 0x10);
Device->SetRenderState(D3DRS_STENCILMASK, 0xffffffff);
Device->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff);
Device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
Device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
Device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);

// disable writes to the depth and back buffers
Device->SetRenderState(D3DRS_ZWRITEENABLE, false); //performs depth test and does a write if passes z test
// disable any writes to the depth buffer
Device->SetRenderState(D3DRS_ZWRITEENABLE, false);

//do not update back buffer
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

// Draw ground plane to stencil only.
drawTexGround();

// Re-enable depth writes
Device->SetRenderState( D3DRS_ZWRITEENABLE, true );//enable depth buffer writes

// Only draw shadowed cube to the pixels where the ground
// was drawn to.
Device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL);
Device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_INCR);

// Build shadow transformation.
//// Position shadow.
//plane defining normals (-ve y direction) and shortest distance to origin
D3DXPLANE groundPlane(0.0f,-1.0f, 0.0f, -2.0f);// plane zx at y = 0.1

//projection of shadow casting object from point of view of light
//source onto plane
D3DXMATRIX S;
D3DXMatrixShadow(&S, &vLightDirs, &groundPlane);

// Offset the shadow up slightly so that there is no
// z-fighting with the shadow and ground.
D3DXMATRIX eps;
D3DXMatrixTranslation(&eps, 0.0f,0.001f, 0.0f);

D3DXMATRIX worldShadow;
worldShadow = S*eps*mWorld;

mWorldView = mView*worldShadow;
mFX->SetMatrix(mhWorldView,&(mWorldView));
//determine the inversetranspose matrices of worldview and view matrices
D3DXMatrixInverse(&mWorldViewIT,NULL, &mWorldView);
D3DXMatrixTranspose(&mWorldViewIT, &mWorldViewIT);
mFX->SetMatrix(mhWorldViewIT,&(mWorldViewIT));
mFX->SetMatrix(mhWVP,&(worldShadow*mView*mproj));

//Begin passes
UINT numPasses=1;
mFX->Begin(&numPasses,0);
//draw cube
for(int i=0; i < numPasses; ++i)
{
mFX->BeginPass(i);
mFX->SetTexture(mhgTex, mWhiteTex);
mFX->CommitChanges();
Render_Plane(shadow);
mFX->EndPass();

}
mFX->End();

// Restore render states.
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
Device->SetRenderState( D3DRS_STENCILENABLE, false);//
}

void GenTriGrid(int numVertRows, int numVertCols,
float dx, float dz,
const D3DXVECTOR3& center,
std::vector<D3DXVECTOR3>& verts,
std::vector<DWORD>& indices)
{
int numVertices = numVertRows*numVertCols;
int numCellRows = numVertRows-1;
int numCellCols = numVertCols-1;

int numTris = numCellRows*numCellCols*2;

float width = (float)numCellCols * dx;
float depth = (float)numCellRows * dz;

verts.resize( numVertices);

// Offsets to translate grid from quadrant 4 to center of
// coordinate system.
float xOffset = -width * 0.5f;
float zOffset = depth * 0.5f;

int k = 0;
for(float i = 0; i < numVertRows; ++i)
{
for(float j = 0; j < numVertCols; ++j)
{
// Negate the depth coordinate to put in quadrant four.
// Then offset to center about coordinate system.
verts[k].x = j * dx + xOffset;
verts[k].z = -i * dz + zOffset;
verts[k].y = -1.0f;

// Translate so that the center of the grid is at the
// specified 'center' parameter.
D3DXMATRIX T;
D3DXMatrixTranslation(&T, center.x, center.y, center.z);
D3DXVec3TransformCoord(&verts[k], &verts[k], &T);

++k; // Next vertex
}
}

indices.resize(numTris * 3);

// Generate indices for each quad.
k = 0;
for(DWORD i = 0; i < (DWORD)numCellRows; ++i)
{
for(DWORD j = 0; j < (DWORD)numCellCols; ++j)
{
indices[k] = i * numVertCols + j;
indices[k + 1] = i * numVertCols + j + 1;
indices[k + 2] = (i+1) * numVertCols + j;

indices[k + 3] = (i+1) * numVertCols + j;
indices[k + 4] = i * numVertCols + j + 1;
indices[k + 5] = (i+1) * numVertCols + j + 1;

// next quad
k += 6;
}
}
}
void buildGroundGeometry()
{

std::vector<D3DXVECTOR3> verts;
std::vector<DWORD> indices;

GenTriGrid(size, size, 1.0f, 1.0f,
D3DXVECTOR3(0.0f, -2.0f, 0.0f), verts, indices);

int mNumGridVertices = size*size;
int mNumGridTriangles = (size-1)*(size-1)*2;

Device->CreateVertexBuffer(mNumGridVertices * sizeof(Vertex),
D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &gpGroundVB, 0);

Vertex* v = 0;
gpGroundVB->Lock(0, 0, (void**)&v, 0);

float texScale = 0.2f;
for(int i = 0; i < size; ++i)
{
for(int j = 0; j < size; ++j)
{
DWORD index = i * size + j;
v[index].x = verts[index].x;
v[index].y = verts[index].y;
v[index].z = verts[index].z;
v[index].nx = 0.0f;
v[index].ny = -1.0f;
v[index].nz = 0.0f;
v[index].u = ((float)j) * texScale;
v[index].v = ((float)i) * texScale;

}
}

gpGroundVB->Unlock();

Device->CreateIndexBuffer(mNumGridTriangles*3*sizeof(WORD), D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16, D3DPOOL_MANAGED, &gpGroundIB, 0);

// Now lock it to obtain a pointer to its internal data, and write the
// grid's index data.
WORD* k = 0;
gpGroundIB->Lock(0, 0, (void**)&k, 0);

for(DWORD i = 0; i < mNumGridTriangles*3; ++i)
k = (WORD)indices;
gpGroundIB->Unlock();
}
bool Setup()
{
D3DXCreateTextureFromFile(Device, "ground0.dds", &gpGroundTex);
D3DXCreateTextureFromFile(Device, "whitetex.dds", &mWhiteTex);

buildGroundGeometry();

//initialise camera
myCamera.pos= D3DXVECTOR3(1.0f, 1.0f, -25.0f);// -300.0f, 150.0f
myCamera.rot= D3DXVECTOR3(0.0f, 0.0f, 0.0f);

Create_Plane();

//create effects
CreateFX();

//Set up lights
SetUpLights();

//Set up the view matrix, stationary camera/view therefore does
//not require per frame updating
D3DXVECTOR3 pos(0.0f, 3.0f,-5.0f);
D3DXVECTOR3 target(0.0f, 0.0f,0.0f);
D3DXVECTOR3 up(0.0f, 1.0f,0.0f);
D3DXMatrixLookAtLH(&mView, &pos, &target, &up);

//Set world matrix to idendity
D3DXMatrixIdentity(&mWorld);

//
// Set the projection matrix.
//
D3DXMatrixPerspectiveFovLH(
&mproj, // result
D3DX_PI * 0.125f, // 90 - degrees
(float)Width / (float)Height, // aspect ratio
0.1f, // near plane
1000.0f); // far plane

setUpRenderStates();

return true;
}

void Cleanup()
{
//Clean up objects...

// close and release the 3D device
Device->Release();
return;
}

void ClearBuffer(DWORD dwColor)
{
Device->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER| D3DCLEAR_STENCIL, 0xffffffff , 1.0f, 0L );

}

bool Display(float timeDelta)
{

if( Device )
{
if( ::GetAsyncKeyState(VK_LEFT) & 0x8000f )
{
myCamera.rot.y+=(float)0.01;

}
if( ::GetAsyncKeyState(VK_RIGHT) & 0x8000f )
{
myCamera.rot.y-=(float)0.01;

}

if( ::GetAsyncKeyState(VK_UP) & 0x8000f )
{
myCamera.pos.z+=(float)0.1;
}

if( ::GetAsyncKeyState(VK_DOWN) & 0x8000f )
{
myCamera.pos.z-=(float)0.1;
}

if( ::GetAsyncKeyState(VK_HOME) & 0x8000f )
{
myCamera.rot.x-=(float)0.01;
}

if( ::GetAsyncKeyState(VK_END) & 0x8000f )
{
myCamera.rot.x+=(float)0.01;
}

setCameraView();
ClearBuffer(D3DCOLOR_XRGB(0,0,0));

Device->BeginScene();

//Activate an Effect
mFX->SetTechnique(mhTech);
//any effect parameters that need to be updated must be set
mFX->SetValue(mhvLightDirs, &(vLightDirs),sizeof(D3DXVECTOR4));
mFX->SetValue(mhLightCol,&(lc) ,sizeof(D3DXCOLOR));

drawTexGround();

mFX->SetVector( mhvLightDirs, (D3DXVECTOR4*)&vLightDirs);
mFX->SetVector( mhvLightCols, (D3DXVECTOR4*)&vLightCols);

//Set world matrix to idendity for sphere as no translation
D3DXMatrixIdentity(&mWorld);//multiply by one
Device->SetTransform(D3DTS_WORLD, &mWorld);
mWorldView = mView*mWorld;
mFX->SetMatrix(mhWorldView,&(mWorldView));
//determine the inversetranspose matrices of worldview and view matrices
D3DXMatrixInverse(&mWorldViewIT,NULL, &mWorldView);
D3DXMatrixTranspose(&mWorldViewIT, &mWorldViewIT);
mFX->SetMatrix(mhWorldViewIT,&(mWorldViewIT));
mFX->SetMatrix(mhWVP,&(mWorld*mView*mproj));


Render_Plane(false);
drawShadow();

Device->EndScene();
Device->Present(0,0,0,0);
}
return true;
}


Share this post


Link to post
Share on other sites
Someone else might have to help you with that, because I haven't done any shadowing myself, but one thing to keep in mind is that the shadow probably won't shrink. If you are using a directional light, then all of the light rays are parallel, in which case the shadow always remains the same size. If you are using a point light or a spot light or something like that, the shadow will actually get bigger as the object moves "up" toward the light source.

The only reason shadows appear to shrink in the real world is partly because of the diffraction of light (light 'bends around corners'), which your renderer likely doesn't simulate, but mostly because the sun itself is an area light, and area lights create soft shadows, and the blurriness of the edge of the shadow depends a lot on the distance between the shadow caster and the shadow receiver. So if your only problem is that the shadow isn't shrinking, then it might be doing what it's supposed to do.

Share this post


Link to post
Share on other sites
Ah yes , it's hard to properly describe what my scene looks like now. If I move the object slightly downwards on the y-axis, the shadow will be projected "beneath" the plane itself. If I translate it upwards the shadow will appear to float above the plane. Also , I can't rotate the plane. It seems to be fixed :(

Well anyway, thanks for your help! At least that's one hurdle over :D I will toy around with maybe making a smaller quad for the terrain instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement