Click here to Skip to main content
15,881,204 members
Home / Discussions / Graphics
   

Graphics

 
QuestionGetting a sense of Depth and 3D space from images Pin
Member 1233692920-Dec-16 5:19
Member 1233692920-Dec-16 5:19 
AnswerRe: Getting a sense of Depth and 3D space from images Pin
Gerry Schmitz21-Dec-16 8:41
mveGerry Schmitz21-Dec-16 8:41 
QuestionHow to extract geometry from Google Earth? Pin
Member 123369294-Oct-16 1:08
Member 123369294-Oct-16 1:08 
SuggestionRe: How to extract geometry from Google Earth? Pin
Richard MacCutchan4-Oct-16 3:48
mveRichard MacCutchan4-Oct-16 3:48 
AnswerRe: How to extract geometry from Google Earth? Pin
leon de boer18-Oct-16 9:48
leon de boer18-Oct-16 9:48 
QuestionD2D + WPF + SwapChain = ??? Pin
Max Dhom17-May-16 0:56
Max Dhom17-May-16 0:56 
QuestionHow to create complex 3d Objects with direct3d? Pin
Member 1226818310-Apr-16 6:17
Member 1226818310-Apr-16 6:17 
QuestionHow to render multiple 3D objects separately? Pin Pin
Member 1226818327-Mar-16 4:24
Member 1226818327-Mar-16 4:24 
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:

C#
#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();
	// 10 units from origin.
	mCameraRadius = 10.0f;
	// Somewhere in the 3rd quadrant of xz-plane.
	mCameraRotationY = 1.2 * D3DX_PI;
	xPos = 0.0f;
	yPos = 0.0f;
	// 5 units off the ground.
	mCameraHeight = 1.0f;
	HR(D3DXCreateCylinder(gd3dDevice, 4.0f, 5.0f, 6.0f, 10, 4, &mCylinder, 0));
	// If you look at the drawCylinders and drawSpheres functions, you see
	// that we draw 14 cylinders and 14 spheres.
	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()
{
	// Create the FX from a .fx file.
	ID3DXBuffer* errors = 0;
	HR(D3DXCreateEffectFromFile(gd3dDevice, "transform.fx", 
		0, 0, D3DXSHADER_DEBUG, 0, &mFX, &errors));
	if( errors )
		MessageBox(0, (char*)errors->GetBufferPointer(), 0, 0);
	// Obtain handles.
	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()
{
	// Obtain a pointer to a new vertex buffer.
	HR(gd3dDevice->CreateVertexBuffer(8 * sizeof (VertexPos), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mVB, 0));
	// Now lock it to obtain a pointer to its internal data,
	// and write the cube's vertex data.
	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()
{
	// Obtain a pointer to a new index buffer.
	HR(gd3dDevice->CreateIndexBuffer(36 * sizeof(WORD),	D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIB, 0));
	// Now lock it to obtain a pointer to its internal data,
	// and write the cube's index data.
	WORD* k = 0;
	HR(mIB->Lock(0, 0, (void**)&k, 0));
 
	// Front face.
	k[0] = 0; k[1] = 1; k[2] = 2;
	k[3] = 0; k[4] = 2; k[5] = 3;
	
	// Back face.
	k[6] = 4; k[7] = 6; k[8] = 5;
	k[9] = 4; k[10] = 7; k[11] = 6;
	
	// Left face.
	k[12] = 4; k[13] = 5; k[14] = 1;
	k[15] = 4; k[16] = 1; k[17] = 0;
	
	// Right face.
	k[18] = 3; k[19] = 2; k[20] = 6;
	k[21] = 3; k[22] = 6; k[23] = 7;
	
	// Top face.
	k[24] = 1; k[25] = 5; k[26] = 6;
	k[27] = 1; k[28] = 6; k[29] = 2;
	
	// Bottom face.
	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)
{
	// One cube has 8 vertices and 12 triangles.
	mGfxStats->setVertexCount(8);
	mGfxStats->setTriCount(12);
	mGfxStats->update(dt);
	// Get snapshot of input devices.
	gDInput->poll();
	// Check input.
	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;
	// Divide by 50 to make mouse less sensitive.
	mCameraRotationY += gDInput->mouseDX() / 50.0f;
	mCameraRadius += gDInput->mouseDY() / 50.0f;
	// If we rotate over 360 degrees, just roll back to 0
	if( fabsf(mCameraRotationY) >= 2.0f * D3DX_PI )
		mCameraRotationY = 0.0f;
	// Don't let radius get too small.
	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()
{
	// Clear the back buffer and depth buffer.
	HR(gd3dDevice->Clear(0, 0,
		D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));
	HR(gd3dDevice->BeginScene());
	// Let Direct3D know the vertex buffer, index buffer, and vertex
	// declaration we are using.
	
	HR(gd3dDevice->SetStreamSource(0, mVB, 0, sizeof(VertexPos)));
	HR(gd3dDevice->SetIndices(mIB));
	HR(gd3dDevice->SetVertexDeclaration(VertexPos::Decl));
	
	// Setup the rendering FX
	HR(mFX->SetTechnique(mhTech));
	// World matrix is identity.
	D3DXMATRIX W;
	D3DXMatrixIdentity(&W);
	HR(gd3dDevice->SetTransform(D3DTS_WORLD, &W));
	HR(gd3dDevice->SetTransform(D3DTS_VIEW, &mView));
	HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &mProj));
 
	D3DXMATRIX matTranslate;    // a matrix to store the translation information

// build a matrix to move the model 12 units along the x-axis and 4 units along the y-axis// store it to matTranslate
	D3DXMatrixTranslation(&matTranslate, xPos, yPos, 0.0f);
// tell Direct3D about our matrix
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());
	// Present the back buffer.
	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);
	// Save vertex count and triangle count for DrawIndexedPrimitive arguments.
	mNumGridVertices  = 100*100;
	mNumGridTriangles = 99*99*2;
	// Obtain a pointer to a new vertex buffer.
	HR(gd3dDevice->CreateVertexBuffer(mNumGridVertices * sizeof(VertexPos), 
		D3DUSAGE_WRITEONLY,	0, D3DPOOL_MANAGED, &mVB, 0));
 
	// Now lock it to obtain a pointer to its internal data, and write the
	// grid's vertex data.
	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());
	// Obtain a pointer to a new index buffer.
	HR(gd3dDevice->CreateIndexBuffer(mNumGridTriangles*3*sizeof(WORD), D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIB, 0));
	// Now lock it to obtain a pointer to its internal data, and write the
	// grid's index data.
	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.

AnswerRe: How to render multiple 3D objects separately? Pin Pin
Graham Breach27-Mar-16 23:29
Graham Breach27-Mar-16 23:29 
GeneralRe: How to render multiple 3D objects separately? Pin Pin
Member 122681833-Apr-16 6:41
Member 122681833-Apr-16 6:41 
AnswerRe: How to render multiple 3D objects separately? Pin Pin
Forrest9012-Jul-16 1:36
Forrest9012-Jul-16 1:36 
GeneralRe: How to render multiple 3D objects separately? Pin Pin
Romain3413-Jul-16 22:33
Romain3413-Jul-16 22:33 
QuestionD2D/GDI+ Interop Problem Pin
Member 1171275327-Feb-16 19:21
Member 1171275327-Feb-16 19:21 
AnswerRe: D2D/GDI+ Interop Problem Pin
Richard MacCutchan27-Feb-16 21:11
mveRichard MacCutchan27-Feb-16 21:11 
QuestionDirect3d Game programming error Pin
Member 1226818323-Feb-16 22:12
Member 1226818323-Feb-16 22:12 
SuggestionRe: Direct3d Game programming error Pin
Richard Deeming24-Feb-16 1:05
mveRichard Deeming24-Feb-16 1:05 
GeneralRe: Direct3d Game programming error Pin
Member 1226818324-Feb-16 20:15
Member 1226818324-Feb-16 20:15 
GeneralRe: Direct3d Game programming error Pin
Pete O'Hanlon24-Feb-16 22:03
mvePete O'Hanlon24-Feb-16 22:03 
GeneralRe: Direct3d Game programming error Pin
Archard25215-Jul-16 0:06
Archard25215-Jul-16 0:06 
GeneralRe: Direct3d Game programming error Pin
Richard MacCutchan24-Feb-16 1:19
mveRichard MacCutchan24-Feb-16 1:19 
AnswerRe: Direct3d Game programming error Pin
Pete O'Hanlon24-Feb-16 2:16
mvePete O'Hanlon24-Feb-16 2:16 
GeneralRe: Direct3d Game programming error Pin
Member 1226818324-Feb-16 20:17
Member 1226818324-Feb-16 20:17 
GeneralRe: Direct3d Game programming error Pin
Richard MacCutchan24-Feb-16 21:40
mveRichard MacCutchan24-Feb-16 21:40 
GeneralRe: Direct3d Game programming error Pin
Member 1226818325-Feb-16 0:40
Member 1226818325-Feb-16 0:40 
GeneralRe: Direct3d Game programming error Pin
Richard MacCutchan25-Feb-16 1:01
mveRichard MacCutchan25-Feb-16 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.