How to render multiple 3D objects separately? Pin
-
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 #include "CubeDemo.h"
#include "d3dApp.h"
#include "GfxStats.h"
#include "Vertex.h"
#include "DirectInput.h"
#include #include #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
-
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 #include "CubeDemo.h"
#include "d3dApp.h"
#include "GfxStats.h"
#include "Vertex.h"
#include "DirectInput.h"
#include #include #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
You're setting the world transform, then drawing the cube followed by the cylinder. Either draw the cylinder before applying the world transform for the cube, or apply a new world transform before drawing the cylinder.
-
You're setting the world transform, then drawing the cube followed by the cylinder. Either draw the cylinder before applying the world transform for the cube, or apply a new world transform before drawing the cylinder.
Thank you very much for your help.
-
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 #include "CubeDemo.h"
#include "d3dApp.h"
#include "GfxStats.h"
#include "Vertex.h"
#include "DirectInput.h"
#include #include #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