/** * Initialize() */ bool Tank::Initialize() { vPos.y = TANK_BODY_H * 0.5f; speed = 1.0f; turn = D3DXToRadian(4.0f); shotInter = 0.0f; rotBody = 0.0f; rotHead = 0.0f; SetCamera(); //// メッシュ準備 // 本体 LPDIRECT3DDEVICE9 pDev = GameMain::GetD3DDevice(); HRESULT hr; hr = D3DXCreateBox(pDev, TANK_BODY_W, TANK_BODY_H, TANK_BODY_D, &pMeshBody, NULL); if (FAILED(hr)) { return false; } // 頭 hr = D3DXCreateBox(pDev, TANK_HEAD_W, TANK_HEAD_H, TANK_HEAD_D, &pMeshHead, NULL); if (FAILED(hr)) { return false; } // 砲塔 hr = D3DXCreateBox(pDev, TANK_GUN_W, TANK_GUN_H, TANK_GUN_D, &pMeshGun, NULL); if (FAILED(hr)) { return false; } //// マテリアル準備 ZeroMemory(&material, sizeof(D3DMATERIAL9)); material.Diffuse.r = material.Ambient.r = 0.0f; material.Diffuse.g = material.Ambient.g = 0.5f; material.Diffuse.b = material.Ambient.b = 0.2f; material.Diffuse.a = material.Ambient.a = 1.0f; return true; }
void DebugBox::CreateBox(float width, float height, float depth, uint8_t r, uint8_t g, uint8_t b, D3DVECTOR origin) { IDirect3DDevice9 *pDirect3DDevice = eae6320::Graphics::Context::getDirect3DDevice(); // Create the sphere mesh ID3DXMesh *pTempBoxMesh; ID3DXBuffer *pBoxBuffer; HRESULT result = D3DXCreateBox(pDirect3DDevice, width, height, depth, &pTempBoxMesh, &pBoxBuffer); assert(SUCCEEDED(result)); // Clone the mesh to allow color pTempBoxMesh->CloneMeshFVF(0, D3DFVF_XYZ | D3DFVF_DIFFUSE, pDirect3DDevice, &m_boxMesh); if (SUCCEEDED(m_boxMesh->GetVertexBuffer(&m_boxVertexBuffer))) { int nNumVerts = m_boxMesh->GetNumVertices(); sDebugVertex *pVertices = NULL; m_boxVertexBuffer->Lock(0, 0, (void**)&pVertices, 0); { for (int i = 0; i < nNumVerts; ++i) { pVertices[i].r = r; pVertices[i].g = g; pVertices[i].b = b; pVertices[i].a = 255; pVertices[i].x += origin.x; pVertices[i].y += origin.y; pVertices[i].z += origin.z; } } m_boxVertexBuffer->Unlock(); } }
HRESULT HelloShadowVolume::CreateOccluder(IDirect3DDevice8* pDevice, ID3DXMesh** ppOut) { ID3DXMesh* pMesh; D3DXCreateBox(pDevice, 2.0f, 2.0f, 2.0f, &pMesh, NULL); *ppOut = pMesh; return S_OK; }
void Mesh::CreateCube(double width, double height, double depth) { D3DXCreateBox(g_engine->getDevice(), width, height, depth, &mesh, NULL); materials = new D3DMATERIAL9[1]; ZeroMemory(&materials[0],sizeof(D3DMATERIAL9)); ++material_count; }
OBJECT::OBJECT(int t, D3DXVECTOR3 pos, D3DXVECTOR3 rot, D3DXVECTOR3 sca) { m_type = t; m_meshInstance.SetPosition(pos); m_meshInstance.SetRotation(rot); m_meshInstance.SetScale(sca); m_meshInstance.SetMesh(objectMeshes[m_type]); m_boxMesh = NULL; m_sphereMesh = NULL; if(m_type == DRAGON) m_name = "Dragon"; else if(m_type == BOB) m_name = "Brave Bob"; else if(m_type == RING) m_name = ""; m_BBox = m_meshInstance.GetBoundingBox(); m_BSphere = m_meshInstance.GetBoundingSphere(); D3DXCreateBox(m_meshInstance.m_pMesh->m_pDevice, m_BBox.max.x - m_BBox.min.x, m_BBox.max.y - m_BBox.min.y, m_BBox.max.z - m_BBox.min.z, &m_boxMesh, NULL); D3DXCreateSphere(m_meshInstance.m_pMesh->m_pDevice, m_BSphere.radius, 20, 20, &m_sphereMesh, NULL); }
void OnInitial(DEVICEINSTANCE *device) { D3DXMATRIX projection; D3DXMatrixPerspectiveFovLH(&projection, D3DX_PI / 2, 800 / 600.0f, 0.0f, 1000.0f); device->d3dDevice->SetTransform(D3DTS_PROJECTION, &projection); D3DXMATRIX view; D3DXMatrixLookAtLH(&view, &D3DXVECTOR3(0, 0, -5), &D3DXVECTOR3(0, 0, 0), &D3DXVECTOR3(0, 1, 0)); device->d3dDevice->SetTransform(D3DTS_VIEW, &view); D3DXCreateBox(device->d3dDevice, 2, 2, 2, &mesh1, NULL); D3DXCreateTeapot(device->d3dDevice, &mesh2, NULL); light.Type = D3DLIGHT_DIRECTIONAL; light.Direction = D3DXVECTOR3(1, -1, 1); light.Ambient = D3DXCOLOR(0.6f, 0.6f, 0.6f, 0.6f); light.Specular = D3DXCOLOR(0.3f, 0.3f, 0.3f, 0.3f); light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); material1.Ambient = material1.Diffuse = material1.Specular = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f); material1.Emissive = D3DXCOLOR(0, 0, 0, 1.0f); material1.Power = 1.0f; material2.Ambient = material2.Diffuse = material2.Specular = material2.Emissive = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); material2.Diffuse.a = 0.5f; }
HRESULT InitObject(void) { srand(unsigned(time(NULL))); if (FAILED(D3DXCreateFont(g_pDevice, 30, 0, 0, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, TEXT("宋体"), &g_pFont))) { return E_FAIL; } if (FAILED(D3DXCreateTeapot(g_pDevice, &g_pTeapot, NULL))) return E_FAIL; if (FAILED(D3DXCreateBox(g_pDevice, 2.0f, 2.0f, 2.0f, &g_pCube, NULL))) return E_FAIL; if (FAILED(D3DXCreateSphere(g_pDevice, 1.5f, 25, 25, &g_pSphere, NULL))) return E_FAIL; if (FAILED(D3DXCreateTorus(g_pDevice, 0.5f, 1.2f, 25, 25, &g_pTorus, NULL))) return E_FAIL; //设置材质 D3DMATERIAL9 material; ZeroMemory(&material, sizeof(D3DMATERIAL9)); material.Ambient = D3DXCOLOR(0.5f, 0.5f, 0.7f, 1.0f); //环境光 material.Diffuse = D3DXCOLOR(0.6f, 0.6f, 0.6f, 1.0f); //漫反射 material.Specular = D3DXCOLOR(0.3f, 0.3f, 0.3f, 0.3f); //镜面反射 material.Emissive = D3DXCOLOR(0.3f, 0.0f, 0.1f, 1.0f); g_pDevice->SetMaterial(&material); //设置光照 SetLight(g_pDevice, 1); g_pDevice->SetRenderState(D3DRS_LIGHTING, TRUE); g_pDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE); g_pDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE); //开启背面消隐 g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); return S_OK; }
bool init(HWND hWnd) { g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); if(!g_pD3D) { return false; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = true; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice); if(!g_pd3dDevice) { return false; } g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); D3DXCreateSphere(g_pd3dDevice, OBJECT_SIZE, OBJECT_PRECESSION, OBJECT_PRECESSION, &g_pSphereMesh, 0); D3DXCreateBox(g_pd3dDevice, OBJECT_SIZE, OBJECT_SIZE, OBJECT_SIZE, &g_pCubeMesh, 0); D3DXCreateTorus(g_pd3dDevice, OBJECT_SIZE / OBJECT_SIZE, OBJECT_SIZE, OBJECT_PRECESSION / (UINT) OBJECT_SIZE, OBJECT_PRECESSION, &g_pTorusMesh, 0); return true; }
bool ObjectInit(HWND hwnd) { srand(unsigned(time(nullptr))); PlaySound(_T("コミネリサ - Resuscitated Hope.wav"), nullptr, SND_ASYNC | SND_FILENAME | SND_LOOP); if (FAILED(D3DXCreateFont(gPD3DDevice, 38, 0, 0, 0, 0, 0, 0, 0, 0, _T("楷体"), &gPFont))) { return false; } D3DXCreateTeapot(gPD3DDevice, &gPTeapot, 0); D3DXCreateBox(gPD3DDevice, 2, 2, 2, &gPBox, 0); D3DXCreateTorus(gPD3DDevice, 1.0f, 2.0f, 25, 25, &gPTorus, 0); D3DXCreateSphere(gPD3DDevice, 2.0f, 25, 25, &gPSphere, 0); //D3DMATERIAL9 is a struct with diffuse, specular, ambient, and emissive; //There is also power typed float, however its usage not known yet. D3DMATERIAL9 material; ZeroMemory(&material, sizeof(material)); material.Ambient = D3DXCOLOR(0.5f, 0.5f, 0.7f, 1.0f); material.Diffuse = D3DXCOLOR(0.4f, 0.6f, 0.6f, 1.0f); material.Specular = D3DXCOLOR(0.3f, 0.3f, 0.3f, 1.0f); material.Emissive = D3DXCOLOR(0.3f, 0.0f, 0.1f, 1.0f); gPD3DDevice->SetMaterial(&material); return true; }
//-----------------------------------【Object_Init( )函数】-------------------------------------- // 描述:渲染资源初始化函数,在此函数中进行要被渲染的物体的资源的初始化 //-------------------------------------------------------------------------------------------------- HRESULT Objects_Init(HWND hwnd) { //创建字体 if(FAILED(D3DXCreateFont(g_pd3dDevice, 36, 0, 0, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, _T("微软雅黑"), &g_pFont))) return E_FAIL; srand(timeGetTime()); //用系统时间初始化随机种子 // 物体的创建 if(FAILED(D3DXCreateBox(g_pd3dDevice, 2, 2, 2, &g_cube, NULL))) //立方体的创建 return false; if(FAILED(D3DXCreateTeapot(g_pd3dDevice, &g_teapot, NULL))) //茶壶的创建 return false; if(FAILED(D3DXCreateSphere(g_pd3dDevice, 1.5, 25, 25, //球面体的创建 &g_sphere, NULL))) return false; if(FAILED(D3DXCreateTorus(g_pd3dDevice, 0.5f, 1.2f, 25, 25, //圆环体的创建 &g_torus, NULL))) return false; // 设置渲染状态 g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE); //关闭光照 g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); //开启背面消隐 g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME); //设置线框填充模式 return S_OK; }
bool initScene() { const double kFovY = 40; // Lighting pd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE); // Ambient pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(77, 77, 77)); // Directional D3DLIGHT9 light; light.Type = D3DLIGHT_DIRECTIONAL; D3DXVECTOR3 dir(0.0f, -0.4f, 1.0f); D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &dir); light.Diffuse.r = 0.9f; light.Diffuse.g = 0.9f; light.Diffuse.b = 0.9f; light.Ambient.r = 0.5f; light.Ambient.g = 0.5f; light.Ambient.b = 0.5f; light.Range = sqrtf(FLT_MAX); pd3dDevice->SetLight(0, &light); pd3dDevice->LightEnable(0, TRUE); // Create the cube HRESULT hr = D3DXCreateBox( pd3dDevice, gCubeEdgeLength, gCubeEdgeLength, gCubeEdgeLength, &gCube, NULL); gCubeMaterial.MatD3D.Diffuse.r = 0.5f; gCubeMaterial.MatD3D.Diffuse.g = 0.5f; gCubeMaterial.MatD3D.Diffuse.b = 0.5f; gCubeMaterial.MatD3D.Ambient.r = 0.5f; gCubeMaterial.MatD3D.Ambient.g = 0.5f; gCubeMaterial.MatD3D.Ambient.b = 0.5f; // Create the cursor hr = D3DXCreateSphere( pd3dDevice, gCursorRadius, 15, 15, &gCursor, NULL); gCursorMaterial.MatD3D.Diffuse.r = 0.0f; gCursorMaterial.MatD3D.Diffuse.g = 0.5f; gCursorMaterial.MatD3D.Diffuse.b = 0.5f; gCursorMaterial.MatD3D.Ambient.r = 0.0f; gCursorMaterial.MatD3D.Ambient.g = 0.5f; gCursorMaterial.MatD3D.Ambient.b = 0.5f; return true; }
// // Creates a box mex void DirectX::Mesh::createBox( float width, float height, float depth ) { D3DXCreateBox( DirectX::Manager::instance( )->getD3DDev( ), width, height, depth, &m_mesh, NULL ); update( ); // Register with the manager DirectX::Manager::instance()->addResource( this ); }
BOOL DMeshRender::CreateMeshBox(D3DXVECTOR3 size) { if (FAILED(D3DXCreateBox(DDEInitialize::gRootDevice, size.x, size.y, size.z, &m_pMess, nullptr))) return FALSE; CreateMaterial(); //m_isEnabled = TRUE; return TRUE; }
cGhostCube::cGhostCube() { D3DXCreateBox( g_pEngine->core->lpd3dd9, 1.05f, 1.05f, 1.05f, &m_meshCube, nullptr ); m_position = D3DXVECTOR3( 0, 0, 0 ); m_render = false; ZeroMemory( &m_color, sizeof( m_color ) ); }
void CBoundBox::CreateBox( const D3DXVECTOR3 *pVMin, const D3DXVECTOR3 *pVMax ) { m_fWidth = pVMax->x - pVMin->x; m_fHeight = pVMax->y - pVMin->y; m_fDepth = pVMax->z - pVMin->z; LPD3DXMESH tmpMesh; D3DXCreateBox( m_pDevice, m_fWidth, m_fHeight, m_fDepth, &tmpMesh, NULL ); tmpMesh->CloneMeshFVF( tmpMesh->GetOptions(), D3DFVF_XYZ|D3DFVF_DIFFUSE, m_pDevice, &m_box ); tmpMesh->CloneMeshFVF( tmpMesh->GetOptions(), D3DFVF_XYZ|D3DFVF_DIFFUSE, m_pDevice, &m_boxOrig ); tmpMesh->Release(); }
void CRenderPrimitive::SetGraphicBox(float Width, float Height, float Depth) { //create the render mesh ID3DXMesh *pMesh; D3DXCreateBox( g_pD3dDevice, Width, Height, Depth, &pMesh, NULL); mMesh.Create(g_pD3dDevice, pMesh, &mMaterial, 1); pMesh->Release(); }
// // Framework Functions // bool Setup() { // // Create the teapot geometry. // D3DXCreateTeapot(Device, &Teapot, 0); D3DXCreateBox(Device, 1.5f, 1.5f, 1.5f, &Box, 0); // // Position and aim the camera. // D3DXVECTOR3 position(0.0f, 0.0f, -3.0f); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMATRIX V; D3DXMatrixLookAtLH(&V, &position, &target, &up); Device->SetTransform(D3DTS_VIEW, &V); // // Set projection matrix. // D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.5f, // 90 - degree (float)Width / (float)Height, 1.0f, 1000.0f); Device->SetTransform(D3DTS_PROJECTION, &proj); // // Switch to wireframe mode. // Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); return true; }
HRESULT CBoxMesh::LoadResource(const LPTSTR szMeshName) { if(FAILED(D3DXCreateBox(_SINGLE(CDevice)->GetDevice(), 3.f, 3.f, 3.f, &m_pMesh, NULL))) { return E_FAIL; } m_tMaterial.Diffuse.a = 1.f; m_tMaterial.Diffuse.r = 1.f; m_tMaterial.Diffuse.g = 1.f; m_tMaterial.Diffuse.b = 1.f; m_tMaterial.Power = 0.2f; m_tMaterial.Specular = m_tMaterial.Diffuse; m_tMaterial.Ambient = m_tMaterial.Diffuse; SetSize(); return S_OK; }
void Cube::AddGeometry(LPDIRECT3DDEVICE9 pd3dDevice, CListGeometry& lstGeometry, CRayTraceView& rtv, const matrix& Matrix) const { LPDIRECT3DVERTEXBUFFER9 pVB; CUSTOMVERTEX* pVertices; matrix m = Matrix * m_Matrix; switch (rtv.m_ViewMode) { case CRayTraceView::eD3DWireFrame: { sp q[] = { sp(1,1,1), sp(1,1,-1), sp(-1,1,-1), sp(-1,-1,-1), sp(-1,-1,1), sp(1,-1,1), sp(1,-1,-1), sp(1,1,-1), sp(-1,1,-1), sp(-1,1,1), sp(-1,-1,1), sp(-1,-1,-1), sp(1,-1,-1), sp(1,1,-1), sp(1,1,1), sp(1,-1,1), sp(1,1,1), sp(-1,1,1), }; if (!InitVertexBuffer(pd3dDevice, pVB, pVertices, 18)) return; for (int i = 0; i < 18; i++) { sp p = m * q[i]; pVertices[i].position = D3DXVECTOR3((float)p.x, (float)p.y, (float)p.z); pVertices[i].normal = D3DXVECTOR3((float)p.x, (float)p.y, (float)p.z); } pVB->Unlock(); lstGeometry.AddTail(Geometry(this, pVB, D3DPT_LINESTRIP, 17)); } break; case CRayTraceView::eD3DFlatShading: case CRayTraceView::eD3DGouraudShading: LPD3DXMESH pMesh; if (FAILED(D3DXCreateBox(pd3dDevice, 1, 1, 1, &pMesh, NULL))) return; lstGeometry.AddTail(Geometry(this, pMesh, m)); break; } Node::AddGeometry(pd3dDevice, lstGeometry, rtv, m); }
OBB::OBB(D3DXVECTOR3 pos, D3DXVECTOR3 size, bool dynamic) { //Create Mesh m_pMesh = NULL; D3DXCreateBox(g_pDevice, size.x, size.y, size.z, &m_pMesh, NULL); //Create Motion State btQuaternion q(0.0f, 0.0f, 0.0f); btVector3 p(pos.x, pos.y, pos.z); btTransform startTrans(q, p); btMotionState *ms = new btDefaultMotionState(startTrans); //Create Collision Shape btCollisionShape *cs = new btBoxShape(btVector3(size.x * 0.5f, size.y * 0.5f, size.z * 0.5f)); //Create Rigid Body float mass = size.x * size.y * size.z; btVector3 localInertia(0,0,0); cs->calculateLocalInertia(mass,localInertia); if (!dynamic)mass = 0.0f; m_pBody = new btRigidBody(mass, ms, cs, localInertia); }
// Using the passed in parameters, w == width, h == height, d == depth, and color // creates a D3D mesh object that is a box. // Returns true of success, false otherwise bool CreateBox(float w, float h, float d, int color, ID3DXMesh **mesh) { assert(g3D->mDevice != NULL); assert(mesh != NULL); ID3DXMesh *tempMesh; // Temp D3D mesh object // Create the sphere if(D3DXCreateBox(g3D->mDevice, w, h, d, &tempMesh, NULL) != D3D_OK) return false; // Flag for how to create the D3D mesh. We want the vertex buffer and index // buffer memory to be managed by DirectX DWORD flag = D3DXMESH_VB_MANAGED | D3DXMESH_IB_MANAGED; // Copy the sphere, converting to our FVF if(tempMesh->CloneMeshFVF(flag, SVertexType, g3D->mDevice, mesh) != D3D_OK) return false; SVertex *v; // Lock the vertex data of our box if((*mesh)->LockVertexBuffer(0, (void**)&v) != D3D_OK) { (*mesh)->Release(); return false; } // Set the box's color for(unsigned int i = 0; i < (*mesh)->GetNumVertices(); ++i) v[i].color = color; // Unlock the vertex data (*mesh)->UnlockVertexBuffer(); tempMesh->Release(); // Free up the temporary mesh return true; }
LPD3DXMESH Cube::CreateMesh(float fWidth, float fHeight, float fDepth) { LPD3DXMESH pMesh, pTexMesh; if (FAILED(D3DXCreateBox(m_pd3dDevice, fWidth, fHeight, fDepth, &pMesh, NULL))) { return NULL; } if (FAILED(pMesh->CloneMeshFVF(D3DXMESH_SYSTEMMEM, Vertex::FVF_Flags | D3DFVF_NORMAL, this->m_pd3dDevice, &pTexMesh))) { return NULL; } pMesh->Release(); Vertex *pVertex; if (SUCCEEDED(pTexMesh->LockVertexBuffer(0, (void **)&pVertex))) { int numVertex = pTexMesh->GetNumVertices(); for (int i = 0; i < numVertex; ++i) { /* D3DXVECTOR3 v = pVertex->pos; D3DXVec3Normalize(&v, &v);*/ GetTexturePosition(pVertex->pos, pVertex->tu, pVertex->tv); ++pVertex; } pTexMesh->UnlockVertexBuffer(); } return pTexMesh; }
Geometry::Geometry(LPDIRECT3DDEVICE9 device, LPD3DXEFFECT shader, Shape shape, int divisions) : m_shape(shape), m_mesh(nullptr), m_texture(nullptr), m_shader(shader) { switch(shape) { case SPHERE: D3DXCreateSphere(device, 1.0f, divisions, divisions, &m_mesh, nullptr); break; case BOX: D3DXCreateBox(device, 1.0f, 1.0f, 1.0f, &m_mesh, nullptr); break; case CYLINDER: D3DXCreateCylinder(device, 1.0f, 1.0f, 1.0f, divisions, 1, &m_mesh, nullptr); break; } CreateMeshData<D3DXVertex, WORD>(true); }
bool CWall::create(IDirect3DDevice9* pDevice, float iwidth, float iheight, float idepth, Type type){ m_width = iwidth; m_depth = idepth; m_height = iheight; if (FAILED(D3DXCreateBox(pDevice, iwidth, iheight, idepth, &m_pMesh, 0))) return false; LPD3DXMESH newMesh = convertMesh(pDevice, m_pMesh); if (newMesh == nullptr){ m_pMesh->Release(); return false; } switch (type){ case Plane: textureFile = PLANE_TEXTURE; effectFile = PLANE_EFFECT; break; case Edge: effectFile = EDGE_EFFECT; textureFile = EDGE_TEXTURE; break; } m_texture = LoadTexture(pDevice, textureFile); m_effect = LoadShader(pDevice, effectFile); if (m_texture == nullptr || m_effect == nullptr) return false; m_pMesh->Release(); m_pMesh = newMesh; return true; }
int main(int argc, char *argv[]) { int ret = 0; try { // initialize directx IDirect3D9 *d3d = Direct3DCreate9(D3D_SDK_VERSION); if (!d3d) throw std::string("This application requires DirectX 9"); // create a window HWND hwnd = CreateWindowEx(0, "static", "GNU Rocket Example", WS_POPUP | WS_VISIBLE, 0, 0, width, height, 0, 0, GetModuleHandle(0), 0); // create the device IDirect3DDevice9 *device = NULL; static D3DPRESENT_PARAMETERS present_parameters = {width, height, D3DFMT_X8R8G8B8, 3, D3DMULTISAMPLE_NONE, 0, D3DSWAPEFFECT_DISCARD, 0, WINDOWED, 1, D3DFMT_D24S8, 0, WINDOWED ? 0 : D3DPRESENT_RATE_DEFAULT, 0 }; if (FAILED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device))) throw std::string("Failed to create device"); // init BASS int soundDevice = 1; if (!BASS_Init(soundDevice, 44100, 0, hwnd, 0)) throw std::string("Failed to init bass"); // load tune HSTREAM stream = BASS_StreamCreateFile(false, "tune.ogg", 0, 0, BASS_MP3_SETPOS | (!soundDevice ? BASS_STREAM_DECODE : 0)); if (!stream) throw std::string("Failed to open tune"); // let's just assume 150 BPM (this holds true for the included tune) float bpm = 150.0f; // setup timer and construct sync-device BassTimer timer(stream, bpm, 8); std::auto_ptr<sync::Device> syncDevice = std::auto_ptr<sync::Device>( sync::createDevice("sync", timer)); if (!syncDevice.get()) throw std::string("Failed to connect to host?"); // get tracks sync::Track &clearRTrack = syncDevice->getTrack("clear.r"); sync::Track &clearGTrack = syncDevice->getTrack("clear.g"); sync::Track &clearBTrack = syncDevice->getTrack("clear.b"); sync::Track &camRotTrack = syncDevice->getTrack("cam.rot"); sync::Track &camDistTrack = syncDevice->getTrack("cam.dist"); LPD3DXMESH cubeMesh = NULL; if (FAILED(D3DXCreateBox(device, 1.0f, 1.0f, 1.0f, &cubeMesh, NULL))) return -1; // let's roll! BASS_Start(); timer.play(); bool done = false; while (!done) { float row = float(timer.getRow()); if (!syncDevice->update(row)) done = true; // setup clear color D3DXCOLOR clearColor(clearRTrack.getValue(row), clearGTrack.getValue(row), clearBTrack.getValue(row), 0.0); // paint the window device->BeginScene(); device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, clearColor, 1.0f, 0); /* D3DXMATRIX world; device->SetTransform(D3DTS_WORLD, &world); */ float rot = camRotTrack.getValue(row); float dist = camDistTrack.getValue(row); D3DXVECTOR3 eye(sin(rot) * dist, 0, cos(rot) * dist); D3DXVECTOR3 at(0, 0, 0); D3DXVECTOR3 up(0, 1, 0); D3DXMATRIX view; D3DXMatrixLookAtLH(&view, &(eye + at), &at, &up); device->SetTransform(D3DTS_WORLD, &view); D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH(&proj, D3DXToRadian(60), 4.0f / 3, 0.1f, 1000.f); device->SetTransform(D3DTS_PROJECTION, &proj); cubeMesh->DrawSubset(0); device->EndScene(); device->Present(0, 0, 0, 0); BASS_Update(0); // decrease the chance of missing vsync MSG msg; while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); if (WM_QUIT == msg.message) done = true; if ((WM_KEYDOWN == msg.message) && (VK_ESCAPE == LOWORD(msg.wParam))) done = true; } } BASS_StreamFree(stream); BASS_Free(); device->Release(); d3d->Release(); DestroyWindow(hwnd); } catch (const std::exception &e) { #ifdef _CONSOLE fprintf(stderr, "*** error: %s\n", e.what()); #else MessageBox(NULL, e.what(), NULL, MB_OK | MB_ICONERROR | MB_SETFOREGROUND); #endif ret = -1; } catch (const std::string &str) { #ifdef _CONSOLE fprintf(stderr, "*** error: %s\n", str.c_str()); #else MessageBox(NULL, e.what(), NULL, MB_OK | MB_ICONERROR | MB_SETFOREGROUND); #endif ret = -1; } return ret; }
void Mesh::CreateCube(double width, double height, double depth) { D3DXCreateBox(g_engine->getDevice(), (float)width, (float)height, (float)depth, &mesh, NULL); }
//---------------------------------- //機能:メッシュを作成する関数 //引数:なし //戻値:成功or失敗 //---------------------------------- HRESULT BasicMesh::Create(DWORD type) { /*------立方体のメッシュを作成する---------- if (FAILED(D3DXCreateBox(g_pDevice, 1, 1, 1, &pMesh, NULL))) ------------------------------------------*/ /*------------スフィア(○)を作る---------- 第二引数は半径、正面から見た面の数、上から見た分割数、となる。 これらを設定して球体の滑らかさを作る if (FAILED(D3DXCreateSphere(g_pDevice, 1, 24, 24, &pMesh, NULL))) ------------------------------------------*/ /*---------------円柱を作る----------------- if (FAILED(D3DXCreateCylinder(g_pDevice, 1, 1, 3, 24, 2, &pMesh, NULL))) ------------------------------------------*/ /*--------トーラス(ドーナツ型)を作る------ 第二引数はドーナツの太さ、円の大きさ、横から見た分割数、正面から見た分割数 if (FAILED(D3DXCreateTorus(g_pDevice, 0.5, 1, 12, 24, &pMesh, NULL))) ------------------------------------------*/ //オマケ。DirectXには光の当たり具合、角度、影等を試せる関数が存在する //それがティーポットである。 //if (FAILED(D3DXCreateTeapot(g_pDevice, &pMesh, NULL))) //{ // MessageBox(0, "メッシュの作成に失敗しました", "BasicMesh", MB_OK); // return E_FAIL; //} //エラー処理用、一つにまとめる事で余計な手間を省ける HRESULT hr; //各モデルを詰め合わせたスイッチ文 switch (type) { case BMESH_BOX: hr = D3DXCreateBox(g_pDevice, 1, 1, 1, &pMesh, NULL); break; case BMESH_SPHERE: hr = D3DXCreateSphere(g_pDevice, 1, 24, 24, &pMesh, NULL); break; case BMESH_CYLINDER: hr = D3DXCreateCylinder(g_pDevice, 1, 1, 3, 24, 2, &pMesh, NULL); break; case BMESH_TORUS: hr = D3DXCreateTorus(g_pDevice, 0.5, 1, 12, 24, &pMesh, NULL); break; case BMESH_TEAPOT: hr = D3DXCreateTeapot(g_pDevice, &pMesh, NULL); break; } //どれが失敗してもhrにまとまっているので確認が楽 if (FAILED(hr)) { MessageBox(0, "メッシュの作成に失敗しました", "BasicMesh", MB_OK); return E_FAIL; } //下のほうで作ったInitMaterialを呼び出す。(呼び忘れ防止) InitMaterial(); return S_OK; }
void ModelCube::Initialize(float x, float y, float z) { D3DXCreateBox(m_Device, x, y, z, &mesh, NULL); }
HRESULT InitGeometry( ) { if (FAILED(D3DXCreateBox(g_pD3DDevice, 1.f, 1.f, 1.f, &g_pMesh, NULL))) { return E_FAIL; } D3DXVECTOR3 *vertices; g_pMesh->LockVertexBuffer( D3DLOCK_READONLY, (void**) &vertices ); D3DXComputeBoundingBox( vertices, g_pMesh->GetNumVertices(), g_pMesh->GetNumBytesPerVertex(), &g_MinPoint, &g_MaxPoint ); g_pMesh->UnlockVertexBuffer(); D3DXMATRIX matScale, matTrans, matRotateZ, matWorld; g_Box[0].BoxScaling = 1.5f; g_Box[0].CenterPos = D3DXVECTOR3( 0.f, 0.f, 0.f ); g_Box[0].BoxRotateZ = 0.f; g_Box[0].AxisDir[0] = D3DXVECTOR3( 1, 0, 0 ); g_Box[0].AxisDir[1] = D3DXVECTOR3( 0, 1, 0 ); g_Box[0].AxisDir[2] = D3DXVECTOR3( 0, 0, 1 ); for ( int i = 0; i < 3; ++i ) { g_Box[0].AxisLen[i] = 0.5f; D3DXVec3TransformNormal( &(g_Box[0].AxisDir[i]), &(g_Box[0].AxisDir[i]), &matRotateZ ); D3DXVec3Normalize( &( g_Box[0].AxisDir[i] ), &( g_Box[0].AxisDir[i] ) ); g_Box[0].AxisLen[i] = g_Box[0].AxisLen[i] * g_Box[0].BoxScaling; } D3DXMatrixTranslation( &matTrans, g_Box[0].CenterPos.x, g_Box[0].CenterPos.y, g_Box[0].CenterPos.z ); D3DXMatrixScaling( &matScale, g_Box[0].BoxScaling, g_Box[0].BoxScaling, g_Box[0].BoxScaling ); D3DXMatrixRotationZ( &matRotateZ, g_Box[0].BoxRotateZ ); matWorld = matRotateZ* matScale * matTrans; D3DXVec3TransformCoord( &g_Box[0].MinPoint, &g_MinPoint, &matWorld ); D3DXVec3TransformCoord( &g_Box[0].MaxPoint, &g_MaxPoint, &matWorld ); g_Box[1].BoxScaling = 2.f; g_Box[1].CenterPos = D3DXVECTOR3( 3.f, 3.f, 0.f ); g_Box[1].BoxRotateZ = 0.f; g_Box[1].AxisDir[0] = D3DXVECTOR3( 1, 0, 0 ); g_Box[1].AxisDir[1] = D3DXVECTOR3( 0, 1, 0 ); g_Box[1].AxisDir[2] = D3DXVECTOR3( 0, 0, 1 ); for ( int i = 0; i < 3; ++i ) { g_Box[1].AxisLen[i] = 0.5f; D3DXVec3TransformNormal( &( g_Box[0].AxisDir[i] ), &( g_Box[1].AxisDir[i] ), &matRotateZ ); D3DXVec3Normalize( &( g_Box[1].AxisDir[i] ), &( g_Box[1].AxisDir[i] ) ); g_Box[1].AxisLen[i] = g_Box[1].AxisLen[i] * g_Box[1].BoxScaling; } D3DXMatrixTranslation( &matTrans, g_Box[1].CenterPos.x, g_Box[1].CenterPos.y, g_Box[1].CenterPos.z ); D3DXMatrixScaling( &matScale, g_Box[1].BoxScaling, g_Box[1].BoxScaling, g_Box[1].BoxScaling ); D3DXMatrixRotationZ( &matRotateZ, g_Box[1].BoxRotateZ ); matWorld = matRotateZ* matScale * matTrans; D3DXVec3TransformCoord( &g_Box[1].MinPoint, &g_MinPoint, &matWorld ); D3DXVec3TransformCoord( &g_Box[1].MaxPoint, &g_MaxPoint, &matWorld ); return S_OK; }
// // Framework Functions // bool Setup() { // // Create the objects. // D3DXCreateTeapot( Device, &Objects[0], 0); D3DXCreateBox( Device, 2.0f, // width 2.0f, // height 2.0f, // depth &Objects[1], 0); // cylinder is built aligned on z-axis D3DXCreateCylinder( Device, 1.0f, // radius at negative z end 1.0f, // radius at positive z end 3.0f, // length of cylinder 10, // slices 10, // stacks &Objects[2], 0); D3DXCreateTorus( Device, 1.0f, // inner radius 3.0f, // outer radius 10, // sides 10, // rings &Objects[3], 0); D3DXCreateSphere( Device, 1.0f, // radius 10, // slices 10, // stacks &Objects[4], 0); // // Build world matrices - position the objects in world space. // For example, ObjWorldMatrices[1] will position Objects[1] at // (-5, 0, 5). Likewise, ObjWorldMatrices[2] will position // Objects[2] at (5, 0, 5). // D3DXMatrixTranslation(&ObjWorldMatrices[0], 0.0f, 0.0f, 0.0f); D3DXMatrixTranslation(&ObjWorldMatrices[1], -5.0f, 0.0f, 5.0f); D3DXMatrixTranslation(&ObjWorldMatrices[2], 5.0f, 0.0f, 5.0f); D3DXMatrixTranslation(&ObjWorldMatrices[3], -5.0f, 0.0f, -5.0f); D3DXMatrixTranslation(&ObjWorldMatrices[4], 5.0f, 0.0f, -5.0f); // // Set the projection matrix. // D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.5f, // 90 - degree (float)Width / (float)Height, 1.0f, 1000.0f); Device->SetTransform(D3DTS_PROJECTION, &proj); // // Switch to wireframe mode. // Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); return true; }