I would like to control two 3D objects separately using direct3d library.
I drawn one cube and one cylinder. Unfortunately when I move the cube by the keyboard, I move the cylinder too. How to move only one of the 3d objects?
Regards
My code is here:
#include <windows.h>
#include "CubeDemo.h"
#include "d3dApp.h"
#include "GfxStats.h"
#include "Vertex.h"
#include "DirectInput.h"
#include <crtdbg.h>
#include <list>
#include "d3dUtil.h"
CubeDemo::CubeDemo(HINSTANCE hInstance, std::string winCaption,
D3DDEVTYPE devType, DWORD requestedVP)
: D3DApp(hInstance, winCaption, devType, requestedVP), MAX_SPEED(1500.0f), ACCELE(1000.0f)
{
mGfxStats = new GfxStats();
mCameraRadius = 10.0f;
mCameraRotationY = 1.2 * D3DX_PI;
xPos = 0.0f;
yPos = 0.0f;
mCameraHeight = 1.0f;
HR(D3DXCreateCylinder(gd3dDevice, 4.0f, 5.0f, 6.0f, 10, 4, &mCylinder, 0));
int numCylVerts = mCylinder->GetNumVertices() * 14;
int numCylTris = mCylinder->GetNumFaces() * 14;
mGfxStats->addVertices(mNumGridVertices);
mGfxStats->addVertices(numCylVerts);
mGfxStats->addTriangles(mNumGridTriangles);
mGfxStats->addTriangles(numCylTris);
buildGeoBuffers();
buildFX();
buildVertexBuffer();
buildIndexBuffer();
onResetDevice();
InitAllVertexDeclarations();
checkDeviceCaps();
}
void CubeDemo::buildFX()
{
ID3DXBuffer* errors = 0;
HR(D3DXCreateEffectFromFile(gd3dDevice, "transform.fx",
0, 0, D3DXSHADER_DEBUG, 0, &mFX, &errors));
if( errors )
MessageBox(0, (char*)errors->GetBufferPointer(), 0, 0);
mhTech = mFX->GetTechniqueByName("TransformTech");
mhWVP = mFX->GetParameterByName(0, "gWVP");
}
bool CubeDemo::checkDeviceCaps()
{
D3DCAPS9 caps;
HR(gd3dDevice->GetDeviceCaps(&caps));
if (caps.VertexShaderVersion < D3DVS_VERSION(3, 0))
return false;
if (caps.VertexShaderVersion < D3DPS_VERSION(3, 0))
return true;
}
void CubeDemo::drawCylinders()
{
D3DXMATRIX T, R;
D3DXMatrixRotationX(&R, D3DX_PI * 0.5f);
int z = 30;
D3DXVECTOR3 pos(100, 0, 0);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&mView, &pos, &target, &up);
D3DXMatrixTranslation(&T, -100.0f, 3.0f, (float)z);
HR(mFX->SetMatrix(mhWVP, &(R * T * mView * mProj)));
HR(mFX->CommitChanges());
HR(mCylinder->DrawSubset(0));
}
void CubeDemo::buildVertexBuffer()
{
HR(gd3dDevice->CreateVertexBuffer(8 * sizeof (VertexPos), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mVB, 0));
VertexPos* v = 0;
HR(mVB->Lock(0, 0, (void**)&v, 0));
v[0] = VertexPos(-1.0f, -1.0f, -1.0f);
v[1] = VertexPos(-1.0f, 1.0f, -1.0f);
v[2] = VertexPos( 1.0f, 1.0f, -1.0f);
v[3] = VertexPos( 1.0f, -1.0f, -1.0f);
v[4] = VertexPos(-1.0f, -1.0f, 1.0f);
v[5] = VertexPos(-1.0f, 1.0f, 1.0f);
v[6] = VertexPos( 1.0f, 1.0f, 1.0f);
v[7] = VertexPos( 1.0f, -1.0f, 1.0f);
HR(mVB->Unlock());
}
void CubeDemo::buildIndexBuffer()
{
HR(gd3dDevice->CreateIndexBuffer(36 * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIB, 0));
WORD* k = 0;
HR(mIB->Lock(0, 0, (void**)&k, 0));
k[0] = 0; k[1] = 1; k[2] = 2;
k[3] = 0; k[4] = 2; k[5] = 3;
k[6] = 4; k[7] = 6; k[8] = 5;
k[9] = 4; k[10] = 7; k[11] = 6;
k[12] = 4; k[13] = 5; k[14] = 1;
k[15] = 4; k[16] = 1; k[17] = 0;
k[18] = 3; k[19] = 2; k[20] = 6;
k[21] = 3; k[22] = 6; k[23] = 7;
k[24] = 1; k[25] = 5; k[26] = 6;
k[27] = 1; k[28] = 6; k[29] = 2;
k[30] = 4; k[31] = 0; k[32] = 3;
k[33] = 4; k[34] = 3; k[35] = 7;
HR(mIB->Unlock());
}
CubeDemo::~CubeDemo()
{
delete mGfxStats;
ReleaseCOM(mVB);
ReleaseCOM(mIB);
ReleaseCOM(mCylinder);
DestroyAllVertexDeclarations();
}
void CubeDemo::onLostDevice()
{
mGfxStats->onLostDevice();
}
void CubeDemo::onResetDevice()
{
mGfxStats->onResetDevice();
buildProjMtx();
}
void CubeDemo::buildProjMtx()
{
float w = (float)md3dPP.BackBufferWidth;
float h = (float)md3dPP.BackBufferHeight;
D3DXMatrixPerspectiveFovLH(&mProj, D3DX_PI * 0.25f, w/h,
1.0f, 5000.0f);
}
void CubeDemo::updateScene(float dt)
{
mGfxStats->setVertexCount(8);
mGfxStats->setTriCount(12);
mGfxStats->update(dt);
gDInput->poll();
if( gDInput->keyDown(DIK_W) )
xPos = xPos + 0.001f;
if( gDInput->keyDown(DIK_S) )
xPos = xPos - 0.001f;
if( gDInput->keyDown(DIK_A) )
yPos = yPos - 0.001f;
if( gDInput->keyDown(DIK_D) )
yPos = yPos + 0.001f;
mCameraRotationY += gDInput->mouseDX() / 50.0f;
mCameraRadius += gDInput->mouseDY() / 50.0f;
if( fabsf(mCameraRotationY) >= 2.0f * D3DX_PI )
mCameraRotationY = 0.0f;
if( mCameraRadius < 5.0f )
mCameraRadius = 5.0f;
buildViewMtx();
}
void CubeDemo::buildViewMtx()
{
D3DXVECTOR3 pos(0, 0, 100);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(1.0f, 0.0f, 0.0f);
D3DXMatrixLookAtLH(&mView, &pos, &target, &up);
}
void CubeDemo::drawScene()
{
HR(gd3dDevice->Clear(0, 0,
D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));
HR(gd3dDevice->BeginScene());
HR(gd3dDevice->SetStreamSource(0, mVB, 0, sizeof(VertexPos)));
HR(gd3dDevice->SetIndices(mIB));
HR(gd3dDevice->SetVertexDeclaration(VertexPos::Decl));
HR(mFX->SetTechnique(mhTech));
D3DXMATRIX W;
D3DXMatrixIdentity(&W);
HR(gd3dDevice->SetTransform(D3DTS_WORLD, &W));
HR(gd3dDevice->SetTransform(D3DTS_VIEW, &mView));
HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &mProj));
D3DXMATRIX matTranslate;
D3DXMatrixTranslation(&matTranslate, xPos, yPos, 0.0f);
HR(gd3dDevice->SetTransform(D3DTS_WORLD, &matTranslate));
HR(gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME));
HR(gd3dDevice->DrawIndexedPrimitive(
D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12));
drawCylinders();
HR(gd3dDevice->EndScene());
HR(gd3dDevice->Present(0, 0, 0, 0));
}
void CubeDemo::buildGeoBuffers()
{
std::vector<D3DXVECTOR3> verts;
std::vector<DWORD> indices;
GenTriGrid(100, 100, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 0.0f), verts, indices);
mNumGridVertices = 100*100;
mNumGridTriangles = 99*99*2;
HR(gd3dDevice->CreateVertexBuffer(mNumGridVertices * sizeof(VertexPos),
D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mVB, 0));
VertexPos* v = 0;
HR(mVB->Lock(0, 0, (void**)&v, 0));
for(DWORD i = 0; i < mNumGridVertices; ++i)
v[i] = verts[i];
HR(mVB->Unlock());
HR(gd3dDevice->CreateIndexBuffer(mNumGridTriangles*3*sizeof(WORD), D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIB, 0));
WORD* k = 0;
HR(mIB->Lock(0, 0, (void**)&k, 0));
for(DWORD i = 0; i < mNumGridTriangles*3; ++i)
k[i] = (WORD)indices[i];
HR(mIB->Unlock());
}
modified 15-Jul-16 4:34am.
|