HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Create device if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } // Wire frame mode. g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME) ; // Disable lighting. g_pd3dDevice->SetRenderState( D3DRS_LIGHTING , FALSE ); // Create geometries. D3DXCreateTeapot(g_pd3dDevice, &g_pMeshes[0], NULL) ; // Teapot D3DXCreateSphere(g_pd3dDevice, 1.2f, 20, 20, &g_pMeshes[1], NULL) ; // Sphere D3DXCreateCylinder(g_pd3dDevice, 1.0f, 1.0f, 2.0f, 20, 5, &g_pMeshes[2], NULL) ; // Cylinder D3DXCreateTorus(g_pd3dDevice, 0.5f, 1.0f, 15, 15, &g_pMeshes[3], NULL) ; // Torus return S_OK; }
Diagnostic::Diagnostic(LPDIRECT3DDEVICE9 d3ddev, std::shared_ptr<Shader> boundsShader) : m_d3ddev(d3ddev), m_sphere(nullptr), m_cylinder(nullptr), m_shader(boundsShader), m_showText(false), m_showDiagnostics(false) { D3DXCreateSphere(d3ddev, 1.0f, MESH_SEGMENTS, MESH_SEGMENTS, &m_sphere, NULL); D3DXCreateCylinder(d3ddev, CYLINDER_SIZE, CYLINDER_SIZE, 1.0f, MESH_SEGMENTS, 1, &m_cylinder, NULL); m_colourmap.insert(ColorMap::value_type(RED, D3DXVECTOR3(1.0f, 0.0f, 0.0f))); m_colourmap.insert(ColorMap::value_type(GREEN, D3DXVECTOR3(0.0f, 1.0f, 0.0f))); m_colourmap.insert(ColorMap::value_type(BLUE, D3DXVECTOR3(0.0f, 0.0f, 1.0f))); m_colourmap.insert(ColorMap::value_type(WHITE, D3DXVECTOR3(1.0f, 1.0f, 1.0f))); m_colourmap.insert(ColorMap::value_type(YELLOW, D3DXVECTOR3(1.0f, 1.0f, 0.0f))); const int border = 10; m_text.reset(new Text()); if(!m_text->Load(d3ddev, false, TEXT_WEIGHT, TEXT_SIZE, DT_LEFT, border, border, WINDOW_WIDTH-border, WINDOW_HEIGHT-border)) { m_text = nullptr; } }
void Mesh::CreateCylinder(double radius1, double radius2, double length, int slices, int stacks) { D3DXCreateCylinder(g_engine->getDevice(), (float)radius1, (float)radius2, (float)length, slices, stacks, &mesh, NULL); materials = new D3DMATERIAL9[1]; ZeroMemory(&materials[0], sizeof(D3DMATERIAL9)); ++material_count; }
SphereCylDemo::SphereCylDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP) : D3DApp(hInstance, winCaption, devType, requestedVP) { if(!checkDeviceCaps()) { MessageBox(0, "checkDeviceCaps() Failed", 0, 0); PostQuitMessage(0); } InitAllVertexDeclarations(); mGfxStats = new GfxStats(); mCameraRadius = 50.0f; mCameraRotationY = 1.2 * D3DX_PI; mCameraHeight = 20.0f; mAmbientLight = WHITE; mDiffuseLight = WHITE; mSpecLight = WHITE; mLightVecW = D3DXVECTOR3(0.0, 0.0f, -1.0f); mGridMtrl = Mtrl(WHITE*0.7f, WHITE, WHITE*0.5f, 16.0f); mCylinderMtrl = Mtrl(WHITE*0.4f, WHITE, WHITE*0.8f, 8.0f); mSphereMtrl = Mtrl(WHITE*0.4f, WHITE, WHITE*0.8f, 8.0f); HR(D3DXCreateCylinder(gd3dDevice, 1.0f, 1.0f, 6.0f, 20, 20, &mCylinder, 0)); HR(D3DXCreateSphere(gd3dDevice, 1.0f, 20, 20, &mSphere, 0)); genSphericalTexCoords(); genCylTexCoords(Z_AXIS); HR(D3DXCreateTextureFromFile(gd3dDevice, "marble.bmp", &mSphereTex)); HR(D3DXCreateTextureFromFile(gd3dDevice, "stone2.dds", &mCylTex)); HR(D3DXCreateTextureFromFile(gd3dDevice, "ground0.dds", &mGridTex)); buildGeoBuffers(); buildFX(); // 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 numSphereVerts = mSphere->GetNumVertices() * 14; int numCylTris = mCylinder->GetNumFaces() * 14; int numSphereTris = mSphere->GetNumFaces() * 14; mGfxStats->addVertices(mNumGridVertices); mGfxStats->addVertices(numCylVerts); mGfxStats->addVertices(numSphereVerts); mGfxStats->addTriangles(mNumGridTriangles); mGfxStats->addTriangles(numCylTris); mGfxStats->addTriangles(numSphereTris); onResetDevice(); }
void Cylinder3D::Create(IDirect3DDevice9* gd3dDevice) { HR(D3DXCreateCylinder(gd3dDevice, mRadiusBottom, mRadiusTop, mLength, mSliceCount, mStackCount, &mpMesh, NULL)); GenerateTBNData(); mNumVertices = mpMesh->GetNumVertices(); mNumTriangles = mpMesh->GetNumFaces(); mpMesh->GetVertexBuffer(&m_VertexBuffer); mpMesh->GetIndexBuffer(&m_IndexBuffer); buildTexCoords(gd3dDevice); }
bool Setup() { //create objects D3DXCreateTeapot(Device, &Objects[0], 0); D3DXCreateSphere(Device, 1.0f, 20, 20, &Objects[1], 0); D3DXCreateTorus(Device, 0.5f, 1.0f, 20, 20, &Objects[2], 0); D3DXCreateCylinder(Device, 0.5f, 0.5f, 2.0f, 20, 20, &Objects[3], 0); //build world martices - position the objects in world space. D3DXMatrixTranslation(&Worlds[0], 0.0f, 2.0f, 0.0f); D3DXMatrixTranslation(&Worlds[1], 0.0f, 2.0f, -3.0f); D3DXMatrixTranslation(&Worlds[2], -3.0f, 0.0f, 0.0f); D3DXMatrixTranslation(&Worlds[3], 3.0f, 0.0f, 0.0f); //setup the object's materials Mtrls[0] = d3d::RED_MTRL; Mtrls[1] = d3d::BLUE_MTRL; Mtrls[2] = d3d::GREEN_MTRL; Mtrls[3] = d3d::YELLOW_MTRL; // Setup a directional light. /*D3DXVECTOR3 dir(1.0f, -0.0f, 0.25f); D3DXCOLOR c = d3d::WHITE; D3DLIGHT9 dirLight = d3d::InitDirectionalLight(&dir, &c);*/ D3DXVECTOR3 pos(5.0f, 50.0f, 50.0f); D3DXCOLOR c = d3d::WHITE; D3DLIGHT9 point = d3d::InitPointLight(&pos, &c); //D3DXVECTOR3 pos(0.0f, 0.0f, -8.0f); //D3DXVECTOR3 dir(0.0f, 0.0f, 1.0f); //D3DXCOLOR c = d3d::WHITE; //D3DLIGHT9 Spot = d3d::InitSpotLight(&pos, &dir, &c); // Set and Enable the light. Device->SetLight(0, &point); Device->LightEnable(0, true); // Set lighting related render states. Device->SetRenderState(D3DRS_NORMALIZENORMALS, true); Device->SetRenderState(D3DRS_SPECULARENABLE, false); // Set the projection matrix. D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.25f, // 45 - degree (float)Width / (float)Height, 1.0f, 1000.0f); Device->SetTransform(D3DTS_PROJECTION, &proj); return true; }
void CRenderPrimitive::SetGraphicCylinder(float radius, float length) { ID3DXMesh *pMesh; D3DXCreateCylinder( g_pD3dDevice, radius, radius, length, 8, 1, &pMesh, NULL); mMesh.Create(g_pD3dDevice, pMesh, &mMaterial, 1); pMesh->Release(); mIsCylinder = 1; }
SpotlightDemo::SpotlightDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP) : D3DApp(hInstance, winCaption, devType, requestedVP) { if(!checkDeviceCaps()) { MessageBox(0, "checkDeviceCaps() Failed", 0, 0); PostQuitMessage(0); } mGfxStats = new GfxStats(); mCameraRadius = 50.0f; mCameraRotationY = 1.2 * D3DX_PI; mCameraHeight = 20.0f; mAmbientLight = 0.4f*WHITE; mDiffuseLight = WHITE; mSpecLight = WHITE; mAttenuation012 = D3DXVECTOR3(1.0f, 0.0f, 0.0f); mSpotPower = 16.0f; mGridMtrl = Mtrl(BLUE, BLUE, WHITE, 16.0f); mCylinderMtrl = Mtrl(RED, RED, WHITE, 8.0f); mSphereMtrl = Mtrl(GREEN, GREEN, WHITE, 8.0f); HR(D3DXCreateCylinder(gd3dDevice, 1.0f, 1.0f, 6.0f, 20, 20, &mCylinder, 0)); HR(D3DXCreateSphere(gd3dDevice, 1.0f, 20, 20, &mSphere, 0)); buildGeoBuffers(); buildFX(); // 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 numSphereVerts = mSphere->GetNumVertices() * 14; int numCylTris = mCylinder->GetNumFaces() * 14; int numSphereTris = mSphere->GetNumFaces() * 14; mGfxStats->addVertices(mNumGridVertices); mGfxStats->addVertices(numCylVerts); mGfxStats->addVertices(numSphereVerts); mGfxStats->addTriangles(mNumGridTriangles); mGfxStats->addTriangles(numCylTris); mGfxStats->addTriangles(numSphereTris); onResetDevice(); InitAllVertexDeclarations(); }
BOOL BasicScene::Init() { IDirect3DVertexBuffer9* floor; m_D3DDev->CreateVertexBuffer(6 * sizeof(Vertex), 0, Vertex::FVF, D3DPOOL_MANAGED, &floor, NULL); m_Floor.Attach(floor); Vertex* v = 0; floor->Lock(0, 0, (void**) &v, 0); v[0] = Vertex(-20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f); v[1] = Vertex(-20.0f, -2.5f, 20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f); v[2] = Vertex( 20.0f, -2.5f, 20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f); v[3] = Vertex(-20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f); v[4] = Vertex( 20.0f, -2.5f, 20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f); v[5] = Vertex( 20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f); floor->Unlock(); ID3DXMesh* pillar; D3DXCreateCylinder(m_D3DDev, 0.5f, 0.5f, 5.0f, 20, 20, &pillar, NULL); m_Pillar.Attach(pillar); IDirect3DTexture9* tex; HRESULT hr = D3DXCreateTextureFromFile(m_D3DDev, TextureFile, &tex); SGL_FAILED_DO(hr, MYTRACE_DX("D3DXCreateTextureFromFile", hr); return FALSE); m_FloorTex.Attach(tex); // Pre-Render Setup m_D3DDev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); m_D3DDev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); m_D3DDev->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT); D3DXVECTOR3 dir(0.707f, -0.707f, 0.707f); D3DXCOLOR clr(1.0f, 1.0f, 1.0f, 1.0f); D3DLIGHT9 light; SGL::InitDirLight(&light, dir, clr); m_D3DDev->SetLight(0, &light); m_D3DDev->LightEnable(0, TRUE); m_D3DDev->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE); m_D3DDev->SetRenderState(D3DRS_SPECULARENABLE, TRUE); return TRUE; }
virtual bool Setup(HINSTANCE hInstance, int width, int height, bool windowed, D3DDEVTYPE deviceType) { CallBaseSetup; // create objects D3DXCreateTeapot(pDevice_, &objects_[0], 0); D3DXCreateSphere(pDevice_, 1.f, 20, 20, &objects_[1], 0); D3DXCreateTorus(pDevice_, .5f, 1.f, 20, 20, &objects_[2], 0); D3DXCreateCylinder(pDevice_, .5f, .5f, 2.f, 20, 20, &objects_[3], 0); // build world matrices --position the objects in the world space D3DXMatrixTranslation(&worlds_[0], 0.f, 2.f, 0.f); D3DXMatrixTranslation(&worlds_[1], 0.f, -2.f, -0.f); D3DXMatrixTranslation(&worlds_[2], -3.f, 0.f, 0.f); D3DXMatrixTranslation(&worlds_[3], 3.f, 0.f, 0.f); // setup the object's materials mtrls_[0] = RED_MTRL; mtrls_[1] = BLUE_MTRL; mtrls_[2] = GREEN_MTRL; mtrls_[3] = YELLOW_MTRL; // setup a directional light D3DXVECTOR3 dir(1.f, -0.f, 0.25f); D3DXCOLOR c = WHITE; D3DLIGHT9 dirLight = InitDirectionalLight(&dir, &c); // set and enable the light pDevice_->SetLight(0, &dirLight); pDevice_->LightEnable(0, true); // set lighting related render states pDevice_->SetRenderState(D3DRS_NORMALIZENORMALS, true); pDevice_->SetRenderState(D3DRS_SPECULARENABLE, false); // set the projection matrix D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.25f, (float)width / (float)height, 1.f, 1000.f); pDevice_->SetTransform(D3DTS_PROJECTION, &proj); return true; }
bool Setup() { //与网格关联的设备,接收网格指针,LPD3DXBUFFER* D3DXCreateTeapot(Device, &Objects[0], 0); //设备,半径,切片,堆叠,接收,LPD3DXBUFFER* D3DXCreateSphere(Device, 1.0f, 20, 20, &Objects[1], 0); //设备,内半径,外半径,边,吊环,接收,LPD3DXBUFFER* D3DXCreateTorus(Device, 0.5f, 1.0f, 20, 20, &Objects[2], 0); //设备,radius at negative z end,radius at positive z end, //length of cylinder, slices, stacks,接收,LPD3DXBUFFER* D3DXCreateCylinder(Device, 0.5f, 0.5f, 2.0f, 20, 20, &Objects[3], 0); D3DXMatrixTranslation(&Worlds[0], 0.0f, 2.0f, 0.0f); D3DXMatrixTranslation(&Worlds[1], 0.0f, -2.0f, 0.0f); D3DXMatrixTranslation(&Worlds[2], -3.0f, 0.0f, 0.0f); D3DXMatrixTranslation(&Worlds[3], 3.0f, 0.0f, 0.0f); Mtrls[0] = d3d::RED_MTRL; Mtrls[1] = d3d::BLUE_MTRL; Mtrls[2] = d3d::GREEN_MTRL; Mtrls[3] = d3d::YELLOW_MTRL; D3DXVECTOR3 pos(0.0f, 0.0f, 0.0f); D3DXCOLOR c = d3d::WHITE; D3DLIGHT9 point = d3d::InitPointLight(&pos, &c); Device->SetLight(0, &point); Device->LightEnable(0, true); Device->SetRenderState(D3DRS_NORMALIZENORMALS, true); Device->SetRenderState(D3DRS_SPECULARENABLE, false); D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.25f, // 45 - degree (float)Width / (float)Height, 1.0f, 1000.0f); Device->SetTransform(D3DTS_PROJECTION, &proj); return true; }
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 Setup() { D3DXCreateTeapot(Device, &Objects[0], 0); D3DXCreateSphere(Device, 1.0f, 20, 20, &Objects[1], 0); D3DXCreateTorus(Device, 0.5f, 1.0f, 20, 20, &Objects[2], 0); D3DXCreateCylinder(Device, 0.5f, 0.5f, 2.0f, 20, 20, &Objects[3], 0); D3DXMatrixTranslation(&Worlds[0], 0.0f, 2.0f, 0.0f); D3DXMatrixTranslation(&Worlds[1], 0.0f, -2.0f, 0.0f); D3DXMatrixTranslation(&Worlds[2], -3.0f, 0.0f, 0.0f); D3DXMatrixTranslation(&Worlds[3], 3.0f, 0.0f, 0.0f); Mtrls[0] = byhj::RED_MTRL; Mtrls[1] = byhj::BLUE_MTRL; Mtrls[2] = byhj::GREEN_MTRL; Mtrls[3] = byhj::YELLOW_MTRL; D3DXVECTOR3 dir(1.0f, -0.0f, 0.25f); D3DXCOLOR color = byhj::WHITE; D3DLIGHT9 dirLight = byhj::InitDirectionalLight(&dir, &color); Device->SetLight(0, &dirLight); Device->LightEnable(0, true); Device->SetRenderState(D3DRS_NORMALIZENORMALS, true); Device->SetRenderState(D3DRS_SPECULARENABLE, false); //set the projection matrix D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.5f, (float)Width / (float)Height, 1.0f, 1000.0f); Device->SetTransform(D3DTS_PROJECTION, &proj); return true; }
//---------------------------------- //機能:メッシュを作成する関数 //引数:なし //戻値:成功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; }
bool d3d::DrawBasicScene(IDirect3DDevice9* device, float scale) { static IDirect3DVertexBuffer9* floor = 0; static IDirect3DTexture9* tex = 0; static ID3DXMesh* pillar = 0; HRESULT hr = 0; if( device == 0 ) { if( floor && tex && pillar ) { // they already exist, destroy them d3d::Release<IDirect3DVertexBuffer9*>(floor); d3d::Release<IDirect3DTexture9*>(tex); d3d::Release<ID3DXMesh*>(pillar); } } else if( !floor && !tex && !pillar ) { // they don't exist, create them device->CreateVertexBuffer( 6 * sizeof(d3d::Vertex), 0, d3d::Vertex::FVF, D3DPOOL_MANAGED, &floor, 0); Vertex* v = 0; floor->Lock(0, 0, (void**)&v, 0); v[0] = Vertex(-20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f); v[1] = Vertex(-20.0f, -2.5f, 20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f); v[2] = Vertex( 20.0f, -2.5f, 20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f); v[3] = Vertex(-20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f); v[4] = Vertex( 20.0f, -2.5f, 20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f); v[5] = Vertex( 20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f); floor->Unlock(); D3DXCreateCylinder(device, 0.5f, 0.5f, 5.0f, 20, 20, &pillar, 0); D3DXCreateTextureFromFile( device, "desert.bmp", &tex); } else { // // Pre-Render Setup // device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT); D3DXVECTOR3 dir(0.707f, -0.707f, 0.707f); D3DXCOLOR col(1.0f, 1.0f, 1.0f, 1.0f); D3DLIGHT9 light = d3d::InitDirectionalLight(&dir, &col); device->SetLight(0, &light); device->LightEnable(0, true); device->SetRenderState(D3DRS_NORMALIZENORMALS, true); device->SetRenderState(D3DRS_SPECULARENABLE, true); // // Render // D3DXMATRIX T, R, P, S; D3DXMatrixScaling(&S, scale, scale, scale); // used to rotate cylinders to be parallel with world's y-axis D3DXMatrixRotationX(&R, -D3DX_PI * 0.5f); // draw floor D3DXMatrixIdentity(&T); T = T * S; device->SetTransform(D3DTS_WORLD, &T); device->SetMaterial(&d3d::WHITE_MTRL); device->SetTexture(0, tex); device->SetStreamSource(0, floor, 0, sizeof(Vertex)); device->SetFVF(Vertex::FVF); device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2); // draw pillars device->SetMaterial(&d3d::BLUE_MTRL); device->SetTexture(0, 0); for(int i = 0; i < 5; i++) { D3DXMatrixTranslation(&T, -5.0f, 0.0f, -15.0f + (i * 7.5f)); P = R * T * S; device->SetTransform(D3DTS_WORLD, &P); pillar->DrawSubset(0); D3DXMatrixTranslation(&T, 5.0f, 0.0f, -15.0f + (i * 7.5f)); P = R * T * S; device->SetTransform(D3DTS_WORLD, &P); pillar->DrawSubset(0); } } return true; }
// 进入循环之前的设置 bool SetUp() { HRESULT hr = 0; hr = D3DXCreateTeapot(g_pDevice, &g_ppMesh[0], NULL); if (FAILED(hr)) return false; hr = D3DXCreateSphere(g_pDevice, 1.0f, 20, 20, &g_ppMesh[1], NULL); if (FAILED(hr)) return false; hr = D3DXCreateTorus(g_pDevice, 0.5f, 1.0f, 20, 20, &g_ppMesh[2], NULL); if (FAILED(hr)) return false; hr = D3DXCreateCylinder(g_pDevice, 0.5f, 1.0f, 2.0f, 20, 20, &g_ppMesh[3], NULL); if (FAILED(hr)) return false; D3DXMatrixTranslation(&g_WorldMatrix[0], 0.0f, 2.0f, 0.0f); D3DXMatrixTranslation(&g_WorldMatrix[1], 0.0f, -2.0f, 0.0f); D3DXMatrixTranslation(&g_WorldMatrix[2], -3.0f, 0.0f, 0.0f); D3DXMatrixTranslation(&g_WorldMatrix[3], 3.0f, 0.0f, 0.0f); g_Color[0] = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f); g_Color[1] = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 1.0f); g_Color[2] = D3DXVECTOR4(0.0f, 0.0f, 1.0f, 1.0f); g_Color[3] = D3DXVECTOR4(1.0f, 1.0f, 0.0f, 1.0f); if (!d3d9::CheckShaderVersion(g_pDevice)) { MessageBox(0, "Shader version can not support!", 0, 0); return false; } ID3DXBuffer *pShaderCache; ID3DXBuffer *pErrorMsg; hr = D3DXCompileShaderFromFile( "Toon.txt", NULL, NULL, "Main", "vs_1_1", D3DXSHADER_DEBUG, &pShaderCache, &pErrorMsg, &g_pConstTable ); if (pErrorMsg) { MessageBox(0, (char*)pErrorMsg->GetBufferPointer(), 0, 0); d3d9::Release(pErrorMsg); return false; } d3d9::Release(pErrorMsg); if (!g_pConstTable) { MessageBox(0, "Const table is NULL!", 0, 0); return false; } if (FAILED(hr)) { MessageBox(0, "Failed to compile shader from file!", 0, 0); return false; } hr = g_pDevice->CreateVertexShader((DWORD*)pShaderCache->GetBufferPointer(), &g_pVertexShader); if (FAILED(hr)) { MessageBox(0, "Failed to create vertex shader!", 0, 0); d3d9::Release(pShaderCache); return false; } d3d9::Release(pShaderCache); hr = D3DXCreateTextureFromFile(g_pDevice, "toonshade.bmp", &g_pShadeTex); if (FAILED(hr)) { MessageBox(0, "Failed to create texture from file!", 0, 0); return false; } g_pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); g_pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); g_pDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE); g_hWorldViewMatrix = g_pConstTable->GetConstantByName(NULL, "WorldViewMatrix"); if (!g_hWorldViewMatrix) { MessageBox(0, "Get WorldViewMatrix failed!", 0, 0); return false; } g_hWorldViewProjMatrix = g_pConstTable->GetConstantByName(NULL, "WorldViewProjMatrix"); if (!g_hWorldViewProjMatrix) { MessageBox(0, "Get WorldViewProjMatrix failed!", 0, 0); return false; } g_hColor = g_pConstTable->GetConstantByName(NULL, "Color"); if (!g_hColor) { MessageBox(0, "Get Color failed!", 0, 0); return false; } g_hLightDir = g_pConstTable->GetConstantByName(NULL, "DirToLight"); if (!g_hLightDir) { MessageBox(0, "Get DirToLight failed!", 0, 0); return false; } g_pConstTable->SetDefaults(g_pDevice); D3DXVECTOR4 dirtolight(-0.57f, 0.57f, -0.57f, 0.0f); g_pConstTable->SetVector(g_pDevice, g_hLightDir, &dirtolight); // 竖直视角,宽/高比,近裁剪面,远裁剪面 D3DXMatrixPerspectiveFovLH(&g_ProjMatrix, D3DX_PI*0.25f, (float)Width / (float)Height, 1.0f, 100.0f); return true; }
bool draw_basic_scene(IDirect3DDevice9* device, float scale) { static IDirect3DVertexBuffer9* floor_vb = NULL; static IDirect3DTexture9* texture = NULL; static ID3DXMesh* pillar_mesh = NULL; if(device == NULL) { if(floor_vb && texture && pillar_mesh) { // they already exist, destroy them. safe_release<IDirect3DVertexBuffer9*>(floor_vb); safe_release<IDirect3DTexture9*>(texture); safe_release<ID3DXMesh*>(pillar_mesh); } } else if(floor_vb == NULL && texture == NULL && pillar_mesh == NULL) { // they don't exist, create them. device->CreateVertexBuffer(6 * sizeof(cTextureVertex), 0, TEXTURE_VERTEX_FVF, D3DPOOL_MANAGED, &floor_vb, 0); cTextureVertex* v; floor_vb->Lock(0, 0, (void**)&v, 0); v[0] = cTextureVertex(-20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f); v[1] = cTextureVertex(-20.0f, -2.5f, 20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f); v[2] = cTextureVertex( 20.0f, -2.5f, 20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f); v[3] = cTextureVertex(-20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f); v[4] = cTextureVertex( 20.0f, -2.5f, 20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f); v[5] = cTextureVertex( 20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f); floor_vb->Unlock(); D3DXCreateCylinder(device, 0.5f, 0.5f, 5.0f, 20, 20, &pillar_mesh, NULL); D3DXCreateTextureFromFile(device, L"desert.bmp", &texture); } else { // pre-render setup device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT); D3DXVECTOR3 dir(0.707f, -0.707f, 0.707f); D3DXCOLOR color(1.0f, 1.0f, 1.0f, 1.0f); D3DLIGHT9 light = init_directional_light(&dir, &color); device->SetLight(0, &light); device->LightEnable(0, TRUE); device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE); device->SetRenderState(D3DRS_SPECULARENABLE, TRUE); // render D3DXMATRIX transform_matrix, rotate_matrix, pillor_matrix, scale_matrix; D3DXMatrixScaling(&scale_matrix, scale, scale, scale); // used to rotate cylinders to be parallel with world's y-axis D3DXMatrixRotationX(&rotate_matrix, -D3DX_PI * 0.5f); // draw floor D3DXMatrixIdentity(&transform_matrix); transform_matrix *= scale_matrix; device->SetTransform(D3DTS_WORLD, &transform_matrix); device->SetMaterial(&WHITE_MATERIAL); device->SetTexture(0, texture); device->SetStreamSource(0, floor_vb, 0, sizeof(cTextureVertex)); device->SetFVF(TEXTURE_VERTEX_FVF); device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2); // draw pillars device->SetMaterial(&BLUE_MATERIAL); device->SetTexture(0, NULL); for(int i = 0; i < 5; i++) { // left pillar D3DXMatrixTranslation(&transform_matrix, -5.0f, 0.0f, -15.0f + (i * 7.5f)); pillor_matrix = rotate_matrix * transform_matrix * scale_matrix; device->SetTransform(D3DTS_WORLD, &pillor_matrix); pillar_mesh->DrawSubset(0); // right pillar D3DXMatrixTranslation(&transform_matrix, 5.0f, 0.0f, -15.0f + (i * 7.5f)); pillor_matrix = rotate_matrix * transform_matrix * scale_matrix; device->SetTransform(D3DTS_WORLD, &pillor_matrix); pillar_mesh->DrawSubset(0); } } return true; }
// // 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; }
//-----------------------------------【Object_Init( )函数】-------------------------------------- // 描述:渲染资源初始化函数,在此函数中进行要被渲染的物体的资源的初始化 //-------------------------------------------------------------------------------------------------- HRESULT Objects_Init(HWND hwnd) { //创建字体 if(FAILED(D3DXCreateFont(g_pd3dDevice, 36, 0, 0, 1000, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, _T("Calibri"), &g_pTextFPS))) return E_FAIL; if (FAILED(D3DXCreateFont(g_pd3dDevice, 20, 0, 1000, 0, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, _T("华文中宋"), &g_pTextAdaperName))) return E_FAIL; if (FAILED(D3DXCreateFont(g_pd3dDevice, 23, 0, 1000, 0, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, _T("微软雅黑"), &g_pTextHelper))) return E_FAIL; if (FAILED(D3DXCreateFont(g_pd3dDevice, 26, 0, 1000, 0, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 0, _T("黑体"), &g_pTextInfo))) return E_FAIL; LPD3DXBUFFER pAdjBuffer = NULL; LPD3DXBUFFER pMtrlBuffer = NULL; if (FAILED(D3DXLoadMeshFromX(L"Warden.X", D3DXMESH_MANAGED, g_pd3dDevice, &pAdjBuffer, &pMtrlBuffer, NULL, &g_dwNumMtrls, &g_pMesh))) //if (FAILED(D3DXLoadMeshFromX(L"WYJ.X", D3DXMESH_MANAGED, g_pd3dDevice, &pAdjBuffer, &pMtrlBuffer, NULL, &g_dwNumMtrls, &g_pMesh))) return E_FAIL; D3DXMATERIAL *pMtrls = (D3DXMATERIAL *)pMtrlBuffer->GetBufferPointer(); g_pMaterials = new D3DMATERIAL9[g_dwNumMtrls]; g_pTextures = new LPDIRECT3DTEXTURE9[g_dwNumMtrls]; for (DWORD i = 0; i < g_dwNumMtrls; i++) { g_pMaterials[i] = pMtrls[i].MatD3D; g_pMaterials[i].Ambient = g_pMaterials[i].Diffuse; g_pTextures[i] = NULL; D3DXCreateTextureFromFileA(g_pd3dDevice, pMtrls[i].pTextureFilename, &g_pTextures[i]); } SAFE_RELEASE(pAdjBuffer); SAFE_RELEASE(pMtrlBuffer); g_pd3dDevice->CreateVertexBuffer(4 * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &g_pVertexBuffer, 0); CUSTOMVERTEX *pVertices = NULL; g_pVertexBuffer->Lock(0, 0, (void **)&pVertices, 0); pVertices[0] = CUSTOMVERTEX(-500.0f, 0.0f, -500.0f, 0.0f, 1.0f, 0.0f, 0.0f, 50.0f); pVertices[1] = CUSTOMVERTEX(-500.0f, 0.0f, 500.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f); pVertices[2] = CUSTOMVERTEX(500.0f, 0.0f, -500.0f, 0.0f, 1.0f, 0.0f, 50.0f, 50.0f); pVertices[3] = CUSTOMVERTEX(500.0f, 0.0f, 500.0f, 0.0f, 1.0f, 0.0f, 50.0f, 0.0f); g_pVertexBuffer->Unlock(); D3DXCreateTextureFromFile(g_pd3dDevice, L"grass.jpg", &g_pTexture); D3DXCreateCylinder(g_pd3dDevice, 10.0f, 10.0f, 500.0f, 60, 60, &g_cylinder, 0); g_MaterialCylinder.Ambient = D3DXCOLOR(0.9f, 0.0f, 0.8f, 1.0f); g_MaterialCylinder.Diffuse = D3DXCOLOR(0.9f, 0.0f, 0.8f, 1.0f); g_MaterialCylinder.Specular = D3DXCOLOR(0.9f, 0.2f, 0.9f, 0.9f); g_MaterialCylinder.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.9f, 1.0f); D3DLIGHT9 light; ZeroMemory(&light, sizeof(light)); light.Type = D3DLIGHT_DIRECTIONAL; light.Ambient = D3DXCOLOR(0.7f, 0.7f, 0.7f, 1.0f); light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); light.Specular = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); light.Direction = D3DXVECTOR3(1.0f, 0.0f, 0.0f); g_pd3dDevice->SetLight(0, &light); g_pd3dDevice->LightEnable(0, true); g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true); g_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true); g_pCamera = new CameraClass(g_pd3dDevice); g_pCamera->SetCameraPosition(&D3DXVECTOR3(0.0f, 200.0f, -300.0f)); g_pCamera->SetTargetPosition(&D3DXVECTOR3(0.0f, 300.0f, 0.0f)); g_pCamera->SetViewMatrix(); g_pCamera->SetProjMatrix(); g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); g_pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); //g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); return S_OK; }
// // Framework Functions // bool Setup() { // // Create objects. // D3DXCreateTeapot(Device, &Objects[0], 0); D3DXCreateSphere(Device, 1.0f, 20, 20, &Objects[1], 0); D3DXCreateTorus(Device, 0.5f, 1.0f, 20, 20, &Objects[2], 0); D3DXCreateCylinder(Device, 0.5f, 0.5f, 2.0f, 20, 20, &Objects[3], 0); // // Build world matrices - position the objects in world space. // D3DXMatrixTranslation(&Worlds[0], 0.0f, 2.0f, 0.0f); D3DXMatrixTranslation(&Worlds[1], 0.0f, -2.0f, 0.0f); D3DXMatrixTranslation(&Worlds[2], -3.0f, 0.0f, 0.0f); D3DXMatrixTranslation(&Worlds[3], 3.0f, 0.0f, 0.0f); D3DXMATRIX Rx; D3DXMatrixRotationX(&Rx, D3DX_PI * 0.5f); Worlds[3] = Rx * Worlds[3]; // // Setup the object's materials. // Mtrls[0] = d3d::RED_MTRL; Mtrls[1] = d3d::BLUE_MTRL; Mtrls[2] = d3d::GREEN_MTRL; Mtrls[3] = d3d::YELLOW_MTRL; for(int i = 0; i < 4; i++) Mtrls[i].Power = 20.0f; // // Setup a spot light // D3DXVECTOR3 pos(0.0f, 0.0f, -5.0f); D3DXVECTOR3 dir(0.0f, 0.0f, 1.0f); D3DXCOLOR c = d3d::WHITE; Spot = d3d::InitSpotLight(&pos, &dir, &c); // // Set and Enable spotlight. // Device->SetLight(0, &Spot); Device->LightEnable(0, true); // // Set light related render states. // Device->SetRenderState(D3DRS_NORMALIZENORMALS, true); Device->SetRenderState(D3DRS_SPECULARENABLE, true); // // Position and aim the camera. // D3DXVECTOR3 position( 0.0f, 0.0f, -5.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 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); return true; }