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; }
VOID Render() { // 背景为蓝色 g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // 茶壶的材料颜色也定义在这个函数中 SetupLights(); // Setup the world, view, and projection matrices SetupMatrices(); // Render the vertex buffer contents g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); //创建一个茶壶 D3DXMATRIX Worlds; D3DXCreateTeapot(g_pd3dDevice, &Objects, 0); Objects->DrawSubset(0); //释放Mesh(网格) Objects->Release(); Objects = 0; // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); //Objects->Release(); }
HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) { MessageBoxA(NULL, "Create D3D9 object failed!", "Error", 0) ; return E_FAIL; } ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; // use window mode, not full screen 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 ) ) ) { MessageBoxA(NULL, "Create D3D9 device failed!", "Error", 0) ; return E_FAIL; } // Create teapot D3DXCreateTeapot(g_pd3dDevice, &g_pTeapotMesh, NULL) ; // Create vertex buffer InitVB(); return S_OK; }
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; }
HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) { MessageBoxA(NULL, "Create D3D9 object failed!", "Error", 0) ; return E_FAIL; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; // use window mode, not full screen 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 ) ) ) { MessageBoxA(NULL, "Create D3D9 device failed!", "Error", 0) ; return E_FAIL; } // Disable lighting, since we didn't specify color for vertex g_pd3dDevice->SetRenderState( D3DRS_LIGHTING , FALSE ); // Create teapot D3DXCreateTeapot(g_pd3dDevice, &g_pTeapotMesh, NULL) ; return S_OK; }
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; }
virtual bool Setup(HINSTANCE hInstance, int width, int height, bool windowed, D3DDEVTYPE deviceType) { if (!D3DBase::Setup(hInstance, width, height, windowed, deviceType)) { return false; } D3DXCreateTeapot(pDevice_, &pTeapot_, 0); 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); pDevice_->SetTransform(D3DTS_VIEW, &v); D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 2, (float)width / (float)height, 1.f, 1000.f); pDevice_->SetTransform(D3DTS_PROJECTION, &proj); pDevice_->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); //_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); return true; }
// // Sets up this object with some data // Returns true if successful // bool MeshBasic::CreateObject(IDirect3DDevice9* a_pDevice) { if (!a_pDevice) { return false; } // Create a shape in our mesh helper HRESULT result = D3DXCreateTeapot(a_pDevice,&m_pMesh,NULL); //@TODO: Create a D3DX mesh! result = D3DXCreateTeapot() or D3DXCreateTorus() or some other D3DXCreate... if (result != D3D_OK) { return false; } // Set up our material ZeroMemory(&m_mat, sizeof(m_mat)); // Zero out the material (ie. all colours black, power set to zero) //m_mat.Ambient = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); //m_mat.Specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); m_mat.Diffuse = D3DXCOLOR(3.0f, 3.0f, 3.0f, 3.0f); //Well look at more material stuff later 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; }
AmbientDiffuseDemo::AmbientDiffuseDemo(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 = 10.0f; mCameraRotationY = 1.2 * D3DX_PI; mCameraHeight = 5.0f; mLightVecW = D3DXVECTOR3(0.0, 0.0f, -1.0f); mDiffuseMtrl = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f); mDiffuseLight = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); mAmbientMtrl = D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f); mAmbientLight = D3DXCOLOR(0.4f, 0.4f, 0.4f, 1.0f); D3DXMatrixIdentity(&mWorld); HR(D3DXCreateTeapot(gd3dDevice, &mTeapot, 0)); buildFX(); onResetDevice(); InitAllVertexDeclarations(); }
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 DMeshRender::CreateMeshTeapot() { if (FAILED(D3DXCreateTeapot(DDEInitialize::gRootDevice, &m_pMess, nullptr))) return FALSE; CreateMaterial(); m_isEnabled = TRUE; return TRUE; }
// 进入循环之前的设置 bool SetUp() { HRESULT hr = 0; hr = D3DXCreateTeapot(g_pDevice, &g_pTeapot,NULL); if (FAILED(hr)) { MessageBox(0, "Create teapot failed!", 0, 0); return false; } BYTE* v = NULL; g_pTeapot->LockVertexBuffer(0, (void**)&v); hr = D3DXComputeBoundingSphere( (D3DXVECTOR3*)v, g_pTeapot->GetNumVertices(), D3DXGetFVFVertexSize(g_pTeapot->GetFVF()), &g_BSephere._center, &g_BSephere._radius ); g_pTeapot->UnlockVertexBuffer(); if (FAILED(hr)) { MessageBox(0, "Compute Sephere failed!", 0, 0); return false; } hr = D3DXCreateSphere(g_pDevice, g_BSephere._radius, 20, 20, &g_pSephere, NULL); if (FAILED(hr)) { MessageBox(0, "Create Sephere failed!", 0, 0); return false; } D3DXVECTOR3 dir(0.707f, -0.0f, 0.707f); D3DLIGHT9 light = d3d9::InitDirLight(&dir, &d3d9::WHITE_COLOR); g_pDevice->SetLight(0, &light); g_pDevice->LightEnable(0, true); g_pDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true); g_pDevice->SetRenderState(D3DRS_SPECULARENABLE, false); D3DXVECTOR3 pos(0.0f, 0.0f, -10.0f); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMATRIX view; D3DXMatrixLookAtLH(&view, &pos, &target, &up); g_pDevice->SetTransform(D3DTS_VIEW, &view); // 竖直视角,宽/高比,近裁剪面,远裁剪面 D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI*0.25f, (float)Width / (float)Height, 1.0f, 1000.0f); g_pDevice->SetTransform(D3DTS_PROJECTION, &proj); return true; }
BOOL RenderPick::Init(UINT width, UINT height, HWND hwnd, BOOL windowed, D3DDEVTYPE devType) { HRESULT hr = Render::Init(width, height, hwnd, windowed, devType); SGL_FAILED_DO(hr, MYTRACE_DX("Render::Init", hr); return FALSE); // Create the teapot. ID3DXMesh* teapot; D3DXCreateTeapot(m_D3DDev, &teapot, NULL); m_Teapot.Attach(teapot); // Compute the bounding sphere. BYTE* v = 0; teapot->LockVertexBuffer(0, (void**) &v); D3DXComputeBoundingSphere((D3DXVECTOR3*) v, teapot->GetNumVertices(), D3DXGetFVFVertexSize(teapot->GetFVF()), &m_BSphere.center, &m_BSphere.radius); teapot->UnlockVertexBuffer(); // Build a sphere mesh that describes the teapot's bounding sphere. ID3DXMesh* sphere; D3DXCreateSphere(m_D3DDev, m_BSphere.radius, 20, 20, &sphere, NULL); m_Sphere.Attach(sphere); // Set light. D3DXVECTOR3 dir(0.707f, -0.0f, 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, FALSE); // Set view matrix. D3DXVECTOR3 pos(0.0f, 0.0f, -10.0f); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMATRIX V; D3DXMatrixLookAtLH(&V, &pos, &target, &up); m_D3DDev->SetTransform(D3DTS_VIEW, &V); // Set projection matrix. D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.25f, (float) width / (float) height, 1.0f, 1000.0f); m_D3DDev->SetTransform(D3DTS_PROJECTION, &proj); // Setup a basic scene. m_BasicScene.reset(new BasicScene(m_D3DDev)); if (!m_BasicScene->Init()) return FALSE; if (!InitFont()) return FALSE; return TRUE; }
HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) { MessageBoxA(NULL, "Create D3D9 object failed!", "Error", 0) ; return E_FAIL; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; // use window mode, not full screen 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 ) ) ) { MessageBoxA(NULL, "Create D3D9 device failed!", "Error", 0) ; return E_FAIL; } // Disable lighting, since we didn't specify color for vertex g_pd3dDevice->SetRenderState( D3DRS_LIGHTING , FALSE ); // Create teapot D3DXCreateTeapot(g_pd3dDevice, &g_pTeapotMesh, NULL) ; // wire frame g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); // Load surface from file HRESULT hr = D3DXLoadSurfaceFromFile( g_pBackgroundSurface, NULL, NULL, "chessboard.jpg", NULL, D3DX_DEFAULT, 0, NULL ); if(FAILED(hr)) { //D3DERR_INVALIDCALL D3DXERR_INVALIDDATA MessageBox(NULL, "Create surface from file failed!", "Error", 0); return E_FAIL; } return S_OK; }
MirrorDemo::MirrorDemo(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 = 15.0f; mCameraRotationY = 1.4f * D3DX_PI; mCameraHeight = 5.0f; mLightVecW = D3DXVECTOR3(0.0, 0.707f, -0.707f); mDiffuseLight = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); mAmbientLight = D3DXCOLOR(0.6f, 0.6f, 0.6f, 1.0f); mSpecularLight = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); mWhiteMtrl.ambient = WHITE; mWhiteMtrl.diffuse = WHITE; mWhiteMtrl.spec = WHITE * 0.8f; mWhiteMtrl.specPower = 16.0f; D3DXMatrixIdentity(&mRoomWorld); D3DXMatrixTranslation(&mTeapotWorld, 0.0f, 3.0f, -6.0f); HR(D3DXCreateTextureFromFile(gd3dDevice, "checkboard.dds", &mFloorTex)); HR(D3DXCreateTextureFromFile(gd3dDevice, "brick2.dds", &mWallTex)); HR(D3DXCreateTextureFromFile(gd3dDevice, "ice.dds", &mMirrorTex)); HR(D3DXCreateTextureFromFile(gd3dDevice, "brick1.dds", &mTeapotTex)); HR(D3DXCreateTeapot(gd3dDevice, &mTeapot, 0)); // Generate texture coordinates for the teapot. genSphericalTexCoords(); // Room geometry count. mGfxStats->addVertices(24); mGfxStats->addTriangles(8); // We draw the teapot twice--once normal and once reflected. mGfxStats->addVertices(mTeapot->GetNumVertices() * 2); mGfxStats->addTriangles(mTeapot->GetNumFaces() * 2); buildRoomGeometry(); buildFX(); onResetDevice(); }
BOOL Setup() { InitMaterial(); D3DXCreateTeapot( g_pDevice, &g_pMesh, 0 ); ////////////////////////////////////////////////////////////////////////////// D3DXVECTOR3 pos(0,0,-5); D3DXVECTOR3 target(0,0,0); D3DXVECTOR3 up(0,1,0); D3DXMatrixLookAtLH( &g_mCamera, &pos, &target, &up ); g_pDevice->SetTransform( D3DTS_VIEW, &g_mCamera ); D3DXMATRIX proj; // D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI *.5f, (float)WIDTH/(float)HEIGHT, 1.0f, 1000.0f ); D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI *.25f, (float)WIDTH/(float)HEIGHT, 1.0f, 1000.0f ); g_pDevice->SetTransform( D3DTS_PROJECTION, &proj ); D3DXCreateTextureFromFile( g_pDevice, "crate.jpg", &g_pTex1); D3DXCreateTextureFromFile( g_pDevice, "Maksim022.bmp", &g_pTex2); g_pDevice->CreateVertexBuffer( 6*sizeof(SVtxNormTex), 0, D3DLVERTEX::FVF, D3DPOOL_MANAGED, &g_pvtxBuff, NULL ); g_pDevice->CreateVertexBuffer( 6*sizeof(SVtxNormTex), 0, D3DLVERTEX::FVF, D3DPOOL_MANAGED, &g_pvtxBuff2, NULL ); D3DLVERTEX *pv; g_pvtxBuff->Lock(0, 0, (void**)&pv, 0); // A vertex pv[ 0] = D3DLVERTEX(Vector3(-1.0f, -1.0f, .25f), Vector3(0,0,-1), D3DXCOLOR(1,0,0,0), 0, 1); pv[ 1] = D3DLVERTEX(Vector3(-1.0f, 1.0f, .25f), Vector3(0,0,-1), D3DXCOLOR(1,0,0,0), 0, 0); pv[ 2] = D3DLVERTEX(Vector3( 1.0f, 1.0f, .25f), Vector3(0,0,-1), D3DXCOLOR(1,0,0,0), 1, 0); pv[ 3] = D3DLVERTEX(Vector3(-1.0f, -1.0f, .25f), Vector3(0,0,-1), D3DXCOLOR(1,0,0,0), 0, 1); pv[ 4] = D3DLVERTEX(Vector3( 1.0f, 1.0f, .25f), Vector3(0,0,-1), D3DXCOLOR(1,0,0,0), 1, 0); pv[ 5] = D3DLVERTEX(Vector3( 1.0f, -1.0f, .25f), Vector3(0,0,-1), D3DXCOLOR(1,0,0,0), 1, 1); g_pvtxBuff->Unlock(); g_pvtxBuff2->Lock(0, 0, (void**)&pv, 0); // B vertex pv[ 0] = D3DLVERTEX(Vector3(1.0f, -1.0f, 1.25f), Vector3(0,0,-1), D3DXCOLOR(0,0,1,0), 0, 1); pv[ 1] = D3DLVERTEX(Vector3(1.0f, 1.0f, 1.25f), Vector3(0,0,-1), D3DXCOLOR(0,0,1,0), 0, 0); pv[ 2] = D3DLVERTEX(Vector3(3.0f, 1.0f, 1.25f), Vector3(0,0,-1), D3DXCOLOR(0,0,1,0), 1, 0); pv[ 3] = D3DLVERTEX(Vector3(1.0f, -1.0f, 1.25f), Vector3(0,0,-1), D3DXCOLOR(0,0,1,0), 0, 1); pv[ 4] = D3DLVERTEX(Vector3(3.0f, 1.0f, 1.25f), Vector3(0,0,-1), D3DXCOLOR(0,0,1,0), 1, 0); pv[ 5] = D3DLVERTEX(Vector3(3.0f, -1.0f, 1.25f), Vector3(0,0,-1), D3DXCOLOR(0,0,1,0), 1, 1); g_pvtxBuff2->Unlock(); return TRUE; }
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; }
//------------------------------------------------------------------------- bool CCameraTest::Setup(IDirect3DDevice9* pDevice,int nWidth,int nHeight) { if (0 == pDevice) { return false; } m_Device = pDevice; m_nHeight = nHeight; m_nWidth = nWidth; // 初始化场景纹理 { m_Device->CreateVertexBuffer( 6 * sizeof(VertexCarema), 0, VertexCarema::FVF, D3DPOOL_MANAGED, &m_Floor, 0); VertexCarema* v = 0; m_Floor->Lock(0, 0, (void**)&v, 0); v[0] = VertexCarema(-20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f); v[1] = VertexCarema(-20.0f, -2.5f, 20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f); v[2] = VertexCarema( 20.0f, -2.5f, 20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f); v[3] = VertexCarema(-20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f); v[4] = VertexCarema( 20.0f, -2.5f, 20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f); v[5] = VertexCarema( 20.0f, -2.5f, -20.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f); m_Floor->Unlock(); //D3DXCreateCylinder(m_Device, 0.5f, 0.5f, 5.0f, 20, 20, &m_Mesh, 0); D3DXCreateTeapot(m_Device,&m_Mesh,0); D3DXCreateTextureFromFile( m_Device, s_szDesert, &m_Texture); } D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.25f, // 45 - degree (float)m_nWidth / (float)m_nHeight, 1.0f, 1000.0f); m_Device->SetTransform(D3DTS_PROJECTION, &proj); return true; }
void D3D9Mesh::CreateTeapot(float radius) { //just call the DirectX function to create the teapot FreeMemory(); LPD3DXMESH teapot; D3DXCreateTeapot(GetD3DDevice(), &teapot, NULL); teapot->CloneMeshFVF(D3DMeshOptions, D3DMeshFVF, GetD3DDevice(), &_Mesh); teapot->Release(); Stretch(radius); SetColor(RGBColor::White); GenerateNormals(); }
HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) { MessageBoxA(NULL, "Create D3D9 object failed!", "Error", 0) ; return E_FAIL; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; // use window mode, not full screen d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.EnableAutoDepthStencil = TRUE ; d3dpp.AutoDepthStencilFormat = D3DFMT_D16 ; // Create device if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { MessageBoxA(NULL, "Create D3D9 device failed!", "Error", 0) ; return E_FAIL; } g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE) ; // Set view matrix D3DXVECTOR3 eyePt(0, 1.0f, -5.0f) ; D3DXVECTOR3 lookAt(0, 0.0f, 0) ; D3DXVECTOR3 upVec(0, 1.0f, 0) ; g_pCamera->SetViewParams(eyePt, lookAt, upVec) ; // Set projection matrix g_pCamera->SetProjParams(D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ; g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP ); // Create teapot D3DXCreateTeapot(g_pd3dDevice, &g_pTeapotMesh, NULL) ; return S_OK; }
void OnInitial(DEVICEINSTANCE *device) { D3DXMATRIX projection; D3DXMatrixPerspectiveFovLH(&projection, D3DX_PI / 4, 800 / 600.0f, 0.0001f, 1000.0f); device->d3dDevice->SetTransform(D3DTS_PROJECTION, &projection); D3DXLoadMeshFromX(L"skybox.x", 0, device->d3dDevice, nullptr, nullptr, nullptr, nullptr, &skybox); D3DXCreateTeapot(device->d3dDevice, &teapot, nullptr); D3DXCreateTextureFromFile(device->d3dDevice, L"sky copy.png", &texture); lastMouseX = GetMouseX(); lastMouseY = GetMouseY(); }
void MyTeapot::InitTeapot( void ) { D3DXCreateTeapot(g_pDevice, &m_pTeapot, NULL); ZeroMemory(&m_Mtrl, sizeof(m_Mtrl)); m_Mtrl.Diffuse = D3DXCOLOR(1,1,1,0.5f); m_Mtrl.Ambient = D3DXCOLOR(1,1,1,1); m_Mtrl.Specular = D3DXCOLOR(1,1,1,1); m_Mtrl.Power = 30.0f; if( FAILED(D3DXCreateTextureFromFile(g_pDevice, "./Data/SphereMap/spheremap.bmp", &m_envTexture)) ) { MessageBox(NULL, "Env Texture load fail", "error", MB_OK); } }
Teapot::Teapot() { D3DXCreateTeapot(theDevice, &m_mesh, nullptr); ID3DXBuffer* errorBuffer; D3DXCreateEffectFromFile(theDevice, "Teapot.fx", 0, 0, D3DXSHADER_DEBUG, 0, &m_effect, &errorBuffer); if (errorBuffer) { Error((char*)errorBuffer->GetBufferPointer()); errorBuffer->Release(); } m_hWorldViewProj = m_effect->GetParameterByName(0, "matWorldViewProj"); }
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; }
// // 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; }
StencilMirrorDemo::StencilMirrorDemo(HINSTANCE hInstance, std::wstring winCaption) : D3DApp(hInstance, winCaption) { InitAllVertexDeclarations(); mGfxStats = new GfxStats(); mCameraRadius = 15.0f; mCameraRotationY = 1.4f * D3DX_PI; mCameraHeight = 5.0f; mLightVecW = D3DXVECTOR3(0.0f, 0.707f, -0.707f); mDiffuseLight = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); mAmbientLight = D3DXCOLOR(0.6f, 0.6f, 0.6f, 1.0f); mSpecularLight = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); mWhiteMtrl.ambient = WHITE; mWhiteMtrl.diffuse = WHITE; mWhiteMtrl.spec = WHITE * 0.8f; mWhiteMtrl.specPower = 16.0f; D3DXMatrixIdentity(&mRoomWorld); D3DXMatrixTranslation(&mTeapotWorld, 0.0f, 3.0f, -6.0f); HR(D3DXCreateTextureFromFile(gd3dDevice, L"../src/chap13/StencilMirror/checkboard.dds", &mFloorTex)); HR(D3DXCreateTextureFromFile(gd3dDevice, L"../src/chap13/StencilMirror/brick2.dds", &mWallTex)); HR(D3DXCreateTextureFromFile(gd3dDevice, L"../src/chap13/StencilMirror/ice.dds", &mMirrorTex)); HR(D3DXCreateTextureFromFile(gd3dDevice, L"../src/chap13/StencilMirror/brick1.dds", &mTeapotTex)); HR(D3DXCreateTeapot(gd3dDevice, &mTeapot, 0)); genSphericalTexCoords(); mGfxStats->addVertices(24); mGfxStats->addTriangles(8); mGfxStats->addVertices(mTeapot->GetNumVertices()*2); mGfxStats->addTriangles(mTeapot->GetNumFaces()*2); buildRoomGeometry(); buildFX(); onResetDevice(); }
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; }
HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) { MessageBoxA(NULL, "Create D3D9 object failed!", "Error", 0) ; return E_FAIL; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; // use window mode, not full screen d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.EnableAutoDepthStencil = TRUE ; d3dpp.AutoDepthStencilFormat = D3DFMT_D16 ; // Create device if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { MessageBoxA(NULL, "Create D3D9 device failed!", "Error", 0) ; return E_FAIL; } // Create teapot D3DXCreateTeapot(g_pd3dDevice, &g_pTeapotMesh, NULL) ; // Setup a Yellow material D3DCOLOR red = D3DCOLOR_XRGB(255, 255, 0); SetupMaterial(red); // Setup a white light D3DCOLOR white = D3DCOLOR_XRGB(255, 255, 255); D3DXVECTOR3 position(10, 10, -5); SetupPointLight(position, white); return S_OK; }
bool InitVertexBuffer() { //D3DXCreateBox(g_pDevice, 2, 2, 2, &g_pMesh, 0); //D3DXCreateSphere(g_pDevice, 1, 10, 10, &g_pMesh, 0); //D3DXCreateCylinder(g_pDevice, 1, 1, 2, 10, 10, &g_pMesh, 0); D3DXCreateTeapot( g_pDevice, &g_pMesh, 0) ; //D3DXCreateTorus(g_pDevice, 0.5f, 2.f, 20, 20, &g_pMesh, 0) ; Matrix44 V; Vector3 dir = Vector3(0,0,0)-Vector3(0,0,-5); dir.Normalize(); V.SetView(Vector3(0,0,-5), dir, Vector3(0,1,0)); g_pDevice->SetTransform(D3DTS_VIEW, (D3DXMATRIX*)&V); Matrix44 proj; proj.SetProjection(D3DX_PI * 0.5f, (float)WINSIZE_X / (float) WINSIZE_Y, 1.f, 1000.0f) ; g_pDevice->SetTransform(D3DTS_PROJECTION, (D3DXMATRIX*)&proj) ; g_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); return true; }