void SwordObj::init( ID3D10EffectTechnique* t, ID3D10EffectMatrixVariable* f, ID3D10EffectMatrixVariable* w, Geometry* g, Vertex v1, Vertex v2) { up = -1; power = 1; rising = false; cuts = 1; theta = -1; reset = false; hit = false; setRadius(150); mfxDiffuseMapVar = mFX->GetVariableByName("gDiffuseMap")->AsShaderResource(); mfxSpecMapVar = mFX->GetVariableByName("gSpecMap")->AsShaderResource(); mfxTexMtxVar = mFX->GetVariableByName("gTexMtx")->AsMatrix(); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"Textures\\Metal3.jpg", 0, 0, &mDiffuseMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"Textures/defaultspec.dds", 0, 0, &mSpecMapRV, 0 )); GeoObject::init(t,f,w,g,v1,v2); }
bool DirectX10Renderer::Initialise(HWND* handle) { DEBUG_OUT("DirectX10Renderer::Initialise"); //window handle hWnd = handle; //get window dimensions RECT rc; GetClientRect( *hWnd, &rc ); m_ScreenWidth = rc.right - rc.left; m_ScreenHeight = rc.bottom - rc.top; m_CameraX = 0; m_CameraY = 0; if(!CreateSwapChainAndDevice()) return false; if(!ResizeScreen()) return false; if(!LoadShadersAndCreateInputLayouts()) return false; if(!InitialiseBuffers()) return false; // Load textures if(FAILED(D3DX10CreateShaderResourceViewFromFile(pD3DDevice, "./textures/tiles.png", NULL, NULL, &m_SpriteTexture, NULL))) { return FatalError("Could not load sprite texture"); } if(FAILED(D3DX10CreateShaderResourceViewFromFile(pD3DDevice, "./fonts/default.png", NULL, NULL, &m_FontTexture, NULL))) { return FatalError("Could not load font texture"); } //everything completed successfully return true; }
void Material::loadAlphaMap() { ID3D10Device* device = GraphicsManager::getInstance()->getDevice(); //load alpha-map, if the image cannot be found, the defaultalphamap will be loaded instead if(FAILED(D3DX10CreateShaderResourceViewFromFile(device, alphaMapFileName.c_str(), NULL, NULL, &g_pAlphaResource, NULL))) if(FAILED(D3DX10CreateShaderResourceViewFromFile(device, "Images/DefaultAlpha.jpg", NULL, NULL, &g_pAlphaResource, NULL))) MessageBox(0, "Failed to create shader resource in Material ", "SR Error", 0); }
void Material::loadImageData() { ID3D10Device* device = GraphicsManager::getInstance()->getDevice(); //Load image, if the image cannot be found, the defaulttexture will be loaded instead if(FAILED(D3DX10CreateShaderResourceViewFromFile(device, textureFileName.c_str(), NULL, NULL, &g_pTextureResource, NULL))) if(FAILED(D3DX10CreateShaderResourceViewFromFile(device, "Images/default.png", NULL, NULL, &g_pTextureResource, NULL))) MessageBox(0, "Failed to create shader resource in Material ", "SR Error", 0); }
Texture *DX10Render::loadTextureFromFile(const wchar_t *file) { DX10Texture *result = new DX10Texture(); HRESULT hr = D3DX10CreateShaderResourceViewFromFile(device, file, NULL, NULL, &result->resourceView, NULL); if (FAILED(hr)) { switch (hr) { case D3D10_ERROR_FILE_NOT_FOUND: { //log() << "Texture file \"" << file << "\" does not exists" << std::endl; break; } default: { //log() << "Failed to load texture \"" << file << "\" does not exists" << std::endl; break; } } delete result; return NULL; } // Now when we have pointer to shader resource interface we can get pointer to texture itself ID3D10Resource *resource = NULL; result->resourceView->GetResource(&resource); resource->QueryInterface(__uuidof(ID3D10Texture2D), (void**)&result->texture); resource->Release(); return result; }
// Creates a DirectX specific material from an imported material bool CMesh::CreateMaterialDX ( const SMeshMaterial& material, SMeshMaterialDX* materialDX ) { // Load shaders for render method materialDX->renderMethod = material.renderMethod; if (!PrepareMethod( materialDX->renderMethod )) { return false; } // Copy colours and shininess from material materialDX->diffuseColour = D3DXCOLOR( material.diffuseColour.r, material.diffuseColour.g, material.diffuseColour.b, material.diffuseColour.a ); materialDX->specularColour = D3DXCOLOR( material.specularColour.r, material.specularColour.g, material.specularColour.b, material.specularColour.a ); materialDX->specularPower = material.specularPower; // Load material textures materialDX->numTextures = material.numTextures; for (TUInt32 texture = 0; texture < material.numTextures; ++texture) { string fullFileName = MediaFolder + material.textureFileNames[texture]; if (FAILED( D3DX10CreateShaderResourceViewFromFile( g_pd3dDevice, fullFileName.c_str(), NULL, NULL, &materialDX->textures[texture], NULL ) )) { string errorMsg = "Error loading texture " + fullFileName; SystemMessageBox( errorMsg.c_str(), "Mesh Error" ); return false; } } return true; }
bool TextureClass::Initialize(ID3D10Device* device, WCHAR* filename) { HRESULT result; result = D3DX10CreateShaderResourceViewFromFile(device, filename, NULL, NULL, &m_texture, NULL); if (FAILED(result)) { return false; } return true; }
bool TextureArrayClass::Initialize(ID3D10Device* device, WCHAR* filename1, WCHAR* filename2) { HRESULT result; // Load the first texture in. result = D3DX10CreateShaderResourceViewFromFile(device, filename1, NULL, NULL, &m_textures[0], NULL); if(FAILED(result)) { return false; } // Load the second texture in. result = D3DX10CreateShaderResourceViewFromFile(device, filename2, NULL, NULL, &m_textures[1], NULL); if(FAILED(result)) { return false; } return true; }
void CrateApp::initApp() { D3DApp::initApp(); mClearColor = D3DXCOLOR(0.9f, 0.9f, 0.9f, 1.0f); buildFX(); buildVertexLayouts(); mCrateMesh.init(md3dDevice, 1.0f); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"10.jpg", 0, 0, &mDiffuseMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"defaultspec.dds", 0, 0, &mSpecMapRV, 0 )); mParallelLight.dir = D3DXVECTOR3(0.57735f, -0.57735f, 0.57735f); mParallelLight.ambient = D3DXCOLOR(0.4f, 0.4f, 0.4f, 1.0f); mParallelLight.diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); mParallelLight.specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); }
void Sky::init(ID3D10Device* device, BREAKOUT* g, float radius) { md3dDevice = device; HR(D3DX10CreateShaderResourceViewFromFile(device, L".\\textures\\BackgroundCube.dds", 0, 0, &mCubeMap, 0 )); game = g; mTech = g->skyFX->GetTechniqueByName("SkyTech"); mfxWVPVar = g->skyFX->GetVariableByName("gWVP")->AsMatrix(); mfxCubeMapVar = g->skyFX->GetVariableByName("gCubeMap")->AsShaderResource(); std::vector<D3DXVECTOR3> vertices; std::vector<DWORD> indices; BuildGeoSphere(2, radius, vertices, indices); std::vector<SkyVertex> skyVerts(vertices.size()); for(size_t i = 0; i < vertices.size(); ++i) { // Scale on y-axis to turn into an ellipsoid to make a flatter Sky surface skyVerts[i].pos = 0.5f*vertices[i]; } D3D10_BUFFER_DESC vbd; vbd.Usage = D3D10_USAGE_IMMUTABLE; vbd.ByteWidth = sizeof(SkyVertex) * (UINT)skyVerts.size(); vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER; vbd.CPUAccessFlags = 0; vbd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA vinitData; vinitData.pSysMem = &skyVerts[0]; HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB)); mNumIndices = (UINT)indices.size(); D3D10_BUFFER_DESC ibd; ibd.Usage = D3D10_USAGE_IMMUTABLE; ibd.ByteWidth = sizeof(DWORD) * mNumIndices; ibd.BindFlags = D3D10_BIND_INDEX_BUFFER; ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA iinitData; iinitData.pSysMem = &indices[0]; HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mIB)); }
HResource* D3DCore_Impl::Resource_Load( LPCWSTR FileName ) { ID3D10ShaderResourceView *spriteTextureRV = NULL; D3DX10_IMAGE_LOAD_INFO loadInfo; ZeroMemory( &loadInfo, sizeof(D3DX10_IMAGE_LOAD_INFO) ); loadInfo.BindFlags = D3D10_BIND_SHADER_RESOURCE; loadInfo.Format = DXGI_FORMAT_BC1_UNORM; ID3D10ShaderResourceView *pSRView = NULL; HR( D3DX10CreateShaderResourceViewFromFile( md3dDevice, FileName, &loadInfo, NULL, &spriteTextureRV, NULL ) ); mResourceList.push_back( spriteTextureRV ); return spriteTextureRV; }
ID3D10ShaderResourceView* Sprite::createTex(char* p_filename) { char stringBuffer[MAX_PATH]; GetModuleFileName( NULL, stringBuffer, MAX_PATH ); std::string::size_type charPosition = string( stringBuffer ).find_last_of( "\\/" ); std::string baseDirectory = string( stringBuffer ).substr( 0, charPosition); charPosition = baseDirectory.find_last_of( "\\/" ); baseDirectory = baseDirectory.substr( 0, charPosition); string resourceFolder = baseDirectory.append("\\PacMan\\Resources\\Sprites\\"); resourceFolder = resourceFolder.append(p_filename); ID3D10ShaderResourceView* rv = 0; D3DX10CreateShaderResourceViewFromFile(m_d3dDevice, resourceFolder.c_str(), 0, 0, &rv, 0 ); return rv; }
bool TextureClass::Initialize(ID3D10Device* device, string filename) { HRESULT result; //-- convert from string to LPCWSTR --// wstring test = std::wstring(filename.begin(), filename.end()); LPCWSTR sw = test.c_str (); // Load the texture in. result = D3DX10CreateShaderResourceViewFromFile(device, sw, NULL, NULL, &m_texture, NULL); if (FAILED(result)) { return false; } return true; }
//-------------------------------------------------------------------------------------- // Helper to load a VB and IB from a mesh //-------------------------------------------------------------------------------------- HRESULT LoadMesh( ID3D10Device* pd3dDevice ) { HRESULT hr = S_OK; struct OLD_VERT { D3DXVECTOR3 Pos; D3DXVECTOR3 Norm; D3DXVECTOR2 Tex; }; V_RETURN( g_Mesh.Create( pd3dDevice, L"rook.sdkmesh", true ) ); // Load the Texture WCHAR strPath[MAX_PATH] = {0}; DXUTFindDXSDKMediaFileCch( strPath, sizeof( strPath ) / sizeof( WCHAR ), L"rook_diff.dds" ); hr = D3DX10CreateShaderResourceViewFromFile( pd3dDevice, strPath, NULL, NULL, &g_pMeshTexRV, NULL ); return hr; }
TextureID Texture_Create( const char* filename ) { unsigned handle = 0; // Create from the free list if we can if( Texture_freelist_count > 0 ) { handle = Texture_freelist[ Texture_freelist_count - 1 ]; Texture_freelist_count--; } else // And only expand the range if free list is empty { assert( Texture_Count < MAX_TEXTURES ); handle = Texture_Count; Texture_Count++; } //load the texture Texture* Texture = &Textures[ handle ]; HRESULT load_texture = D3DX10CreateShaderResourceViewFromFile( dx_device, filename, NULL, NULL, &Texture->texture, NULL ); assert( SUCCEEDED( load_texture ) ); ID3D10Resource* res; Texture->texture->GetResource( &res ); D3D10_TEXTURE2D_DESC desc2D; ((ID3D10Texture2D*)res)->GetDesc(&desc2D); /*Texture->transform.position[0] = x / GAME_RESOLUTION_X; Texture->transform.position[1] = y / GAME_RESOLUTION_Y; Texture->transform.position[2] = 0; Texture->transform.scale[0] = (float)(desc2D.Width/(float)GAME_RESOLUTION_X); Texture->transform.scale[1] = (float)(desc2D.Height/(float)GAME_RESOLUTION_Y); Texture->transform.rotation = 180;*/ res->Release(); return handle; }
SCTReturn SCTTextureD3D10::Initialize(const WCHAR *filename, bool asCubeMap) { // Load the texture HRESULT result; if(asCubeMap) { D3DX10_IMAGE_LOAD_INFO loadInfo; loadInfo.MiscFlags = D3D10_RESOURCE_MISC_TEXTURECUBE; ID3D10Texture2D* tex = 0; result = D3DX10CreateTextureFromFile(mpDevice, filename, &loadInfo, 0, (ID3D10Resource**)&tex, 0); D3D10_TEXTURE2D_DESC texDesc; tex->GetDesc(&texDesc); D3D10_SHADER_RESOURCE_VIEW_DESC viewDesc; viewDesc.Format = texDesc.Format; viewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURECUBE; viewDesc.TextureCube.MipLevels = texDesc.MipLevels; viewDesc.TextureCube.MostDetailedMip = 0; result = mpDevice->CreateShaderResourceView(tex, &viewDesc, &mpShaderResourceView); ReleaseCOM(tex); } else { result = D3DX10CreateShaderResourceViewFromFile(mpDevice, filename, NULL, NULL, &mpShaderResourceView, NULL); } if(result != S_OK) return FAIL; return OK; }
void ColoredCubeApp::initApp() { D3DApp::initApp(); myOutFile.open("debug.txt"); fx::InitAll(md3dDevice); mBox.init(md3dDevice, 1.0f); mPlane.init(md3dDevice, 1.0f); //we send scale of 1.0f what does that mean?? mTree.initObject(md3dDevice); std::string treeFileName = "MediumPolyTree.3ds"; mTree.load(md3dDevice, treeFileName); mTree.createTexturesAll(L"LeafCol.jpg", L"LeafAlpha.jpg", L"mySpec.jpg", L"LeafnormalX_normals.PNG"); mTree.setCubeMap(Object::createCubeTex(md3dDevice, L"grassenvmap1024.dds")); mTree.rotate(-1.5f,0.0f,0.0f); mTree.translate(3.0f,-3.0f,-2.6f); mObjBox.initObject(md3dDevice); std::string boxFileName = "man2.fbx"; mObjBox.load(md3dDevice, boxFileName); mObjBox.createTexturesAll( L"manD.jpg", L"defaultAlpha.jpg", L"defaultspec.dds", L"manN.jpg"); mObjBox.createTexturesAt(0, L"bricks.dds", L"defaultAlpha.jpg", L"spec.dds", L"womanHairN.jpg"); mObjBox.setCubeMap(Object::createCubeTex(md3dDevice, L"grassenvmap1024.dds")); mObjBox.rotate(0.0f,0.0f,3.14f); mObjBox.translate(-3.0f,0.0f,2.5f); mObjBox.scale(2.0f,2.0f,2.0f); buildFX(); buildVertexLayouts(); //mLights[0].dir = D3DXVECTOR3(0.57735f, -0.57735f, 0.57735f); mLights[0].dir = D3DXVECTOR3(0.576f, -0.576f, -0.576f); mLights[0].ambient = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f); mLights[0].diffuse = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f); mLights[0].specular = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); // Pointlight--position is changed every frame to animate. mLights[1].pos = D3DXVECTOR3(2.0f,2.0f,2.0f); mLights[1].ambient = D3DXCOLOR(0.8f, 0.8f, 0.0f, 1.0f); mLights[1].diffuse = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); mLights[1].specular = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); mLights[1].att.x = 0.0f; mLights[1].att.y = 1.0f; mLights[1].att.z = 0.0f; mLights[1].range = 120.0f; animationTimeElapsed = 0.0f; animationTimePrev = mTimer.getGameTime(); for(int i = 0; i < fireFrameCount; i++) { std::wostringstream fileName; fileName << L"FireAnim\\Fire"; if(i+1 < 10) fileName << L"00"; else if(i+1 < 100) fileName << L"0"; fileName << i+1 << L".bmp"; std::wstring wbuffer = fileName.str(); LPCWSTR usableName = wbuffer.c_str(); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, usableName, 0, 0, &mFireAnimationMapRVs[i], 0)); } HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"bricks.dds", 0, 0, &mCrateMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"bricks_normal.dds", 0, 0, &mDefaultNormalMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"stone_diffuse.dds", 0, 0, &mGrassMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"stone_normal.dds", 0, 0, &mBrickNormalMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"spec.dds", 0, 0, &mSpecularMapRV, 0 )); // If not, create it. D3DX10_IMAGE_LOAD_INFO loadInfo; loadInfo.MiscFlags = D3D10_RESOURCE_MISC_TEXTURECUBE; ID3D10Texture2D* tex = 0; HR(D3DX10CreateTextureFromFile(md3dDevice, L"grassenvmap1024.dds", &loadInfo, 0, (ID3D10Resource**)&tex, 0) ); D3D10_TEXTURE2D_DESC texDesc; tex->GetDesc(&texDesc); D3D10_SHADER_RESOURCE_VIEW_DESC viewDesc; viewDesc.Format = texDesc.Format; viewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURECUBE; viewDesc.TextureCube.MipLevels = texDesc.MipLevels; viewDesc.TextureCube.MostDetailedMip = 0; HR(md3dDevice->CreateShaderResourceView(tex, &viewDesc, &mCubeMapRV)); ReleaseCOM(tex); mSky.init(md3dDevice, mCubeMapRV, 5000.0f); }
//-------------------------------------------------------------------------------------- // Helper to load a VB and IB from a mesh //-------------------------------------------------------------------------------------- HRESULT LoadMesh( ID3D10Device* pd3dDevice ) { static float treeVertexData[] = { 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, -0.237213f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, -0.237213f, 0.999001f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, -0.237213f, 0.999001f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, -0.237213f, -0.194237f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, -0.237213f, 0.999001f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, 0.762787f, -0.194237f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, 0.762787f, -0.194237f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, 0.762787f, 0.999001f, 0.000000f, 0.606364f, 1.514307f, 0.440550f, -0.245871f, 0.952700f, -0.178636f, 0.900607f, -0.999001f, 0.600000f, 0.803003f, 1.102575f, 1.045742f, 0.122381f, 0.171919f, 0.977480f, 0.926464f, -0.753354f, 0.600000f, 1.242701f, 1.102575f, 0.440550f, 0.967456f, 0.171919f, -0.185666f, 0.821507f, -0.753354f, 0.600000f, -0.173708f, 1.280021f, 0.534618f, 0.093915f, 0.952699f, -0.289039f, 0.105600f, -0.859222f, 0.600000f, -0.688515f, 0.868289f, 0.908647f, -0.891821f, 0.171919f, 0.418449f, 0.126479f, -0.613575f, 0.600000f, 0.022931f, 0.868289f, 1.139810f, 0.475540f, 0.171919f, 0.862732f, 0.032802f, -0.613575f, 0.600000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.193009f, 1.358116f, -0.594020f, 0.093915f, 0.952699f, 0.289039f, 0.391077f, -0.905815f, 0.600000f, 0.003630f, 0.946384f, -1.199212f, 0.475540f, 0.171919f, -0.862732f, 0.464690f, -0.660168f, 0.600000f, -0.707816f, 0.946384f, -0.968049f, -0.891821f, 0.171919f, -0.418450f, 0.371931f, -0.660168f, 0.600000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, -0.237213f, -0.194237f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, -0.237213f, -0.194237f, 0.000000f, 0.803003f, 1.102575f, 1.045742f, 0.122381f, 0.171919f, 0.977480f, -0.073536f, -0.753354f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.803003f, 1.102575f, 1.045742f, 0.122381f, 0.171919f, 0.977480f, 0.926464f, -0.753354f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, 0.762787f, -0.194237f, 0.000000f, 1.242701f, 1.102575f, 0.440550f, 0.967456f, 0.171919f, -0.185666f, 0.821507f, -0.753354f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 1.242701f, 1.102575f, 0.440550f, 0.967456f, 0.171919f, -0.185666f, -0.178493f, -0.753354f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, -0.237213f, -0.194237f, 0.000000f, 1.242701f, 1.102575f, 0.440550f, 0.967456f, 0.171919f, -0.185666f, -0.178493f, -0.753354f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 0.606364f, 1.514307f, 0.440550f, -0.245871f, 0.952700f, -0.178636f, -0.099393f, -0.999001f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.606364f, 1.514307f, 0.440550f, -0.245871f, 0.952700f, -0.178636f, -0.099393f, -0.999001f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 0.606364f, 1.514307f, 0.440550f, -0.245871f, 0.952700f, -0.178636f, -0.099393f, -0.999001f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.803003f, 1.102575f, 1.045742f, 0.122381f, 0.171919f, 0.977480f, -0.073536f, -0.753354f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, 0.003630f, 0.946384f, -1.199212f, 0.475540f, 0.171919f, -0.862732f, 0.464690f, -0.660168f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.003630f, 0.946384f, -1.199212f, 0.475540f, 0.171919f, -0.862732f, 0.464690f, -0.660168f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.707816f, 0.946384f, -0.968049f, -0.891821f, 0.171919f, -0.418450f, 0.371931f, -0.660168f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.707816f, 0.946384f, -0.968049f, -0.891821f, 0.171919f, -0.418450f, 0.371931f, -0.660168f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.707816f, 0.946384f, -0.968049f, -0.891821f, 0.171919f, -0.418450f, 0.371931f, -0.660168f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.193009f, 1.358116f, -0.594020f, 0.093915f, 0.952699f, 0.289039f, 0.391077f, -0.905815f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, -0.193009f, 1.358116f, -0.594020f, 0.093915f, 0.952699f, 0.289039f, 0.391077f, -0.905815f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.193009f, 1.358116f, -0.594020f, 0.093915f, 0.952699f, 0.289039f, 0.391077f, -0.905815f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.003630f, 0.946384f, -1.199212f, 0.475540f, 0.171919f, -0.862732f, 0.464690f, -0.660168f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, -0.688515f, 0.868289f, 0.908647f, -0.891821f, 0.171919f, 0.418449f, 0.126479f, -0.613575f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.688515f, 0.868289f, 0.908647f, -0.891821f, 0.171919f, 0.418449f, 0.126479f, -0.613575f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.022931f, 0.868289f, 1.139810f, 0.475540f, 0.171919f, 0.862732f, 0.032802f, -0.613575f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 0.022931f, 0.868289f, 1.139810f, 0.475540f, 0.171919f, 0.862732f, 0.032802f, -0.613575f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.022931f, 0.868289f, 1.139810f, 0.475540f, 0.171919f, 0.862732f, 0.032802f, -0.613575f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.173708f, 1.280021f, 0.534618f, 0.093915f, 0.952699f, -0.289039f, 0.105600f, -0.859222f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.173708f, 1.280021f, 0.534618f, 0.093915f, 0.952699f, -0.289039f, 0.105600f, -0.859222f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.173708f, 1.280021f, 0.534618f, 0.093915f, 0.952699f, -0.289039f, 0.105600f, -0.859222f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.688515f, 0.868289f, 0.908647f, -0.891821f, 0.171919f, 0.418449f, 0.126479f, -0.613575f, 0.000000f, }; static DWORD treeIndexData[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113 }; HRESULT hr = S_OK; g_NumIndices = sizeof( treeIndexData ) / sizeof( DWORD ); g_VertStride = sizeof( TREE_VERTEX ); D3D10_BUFFER_DESC bd; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof( treeVertexData ); bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA InitData; InitData.pSysMem = treeVertexData; V_RETURN( pd3dDevice->CreateBuffer( &bd, &InitData, &g_pVB ) ); bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof( treeIndexData ); bd.BindFlags = D3D10_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; InitData.pSysMem = treeIndexData; V_RETURN( pd3dDevice->CreateBuffer( &bd, &InitData, &g_pIB ) ); // Load the Texture WCHAR strPath[MAX_PATH] = {0}; DXUTFindDXSDKMediaFileCch( strPath, sizeof( strPath ) / sizeof( WCHAR ), L"bark_diff.dds" ); hr = D3DX10CreateShaderResourceViewFromFile( pd3dDevice, strPath, NULL, NULL, &g_pMeshTexRV, NULL ); return hr; }
Scene::Scene(HWND TheWindow, unsigned int WindowWidth, unsigned int WindowHeight): MyD3D10Code::Direct3D10Class(TheWindow, WindowWidth, WindowHeight), m_FX(0), m_Tech(0), m_fxWVPVar(0), m_VertexLayout(0), m_Theta(0.0f), m_Phi(PI*0.25f), m_Box(m_Direct3DDevice), // Delete 5 below variables later from init list m_FxEyePosVar(0), m_FxLightVar(0), m_SpecMapRV(0), m_FxSpecMapVar(0), m_FxTexMatVar(0), m_FxBoxWorldVar(0), m_EnvironmentMapRV(0), m_Roof(m_Direct3DDevice), m_Floor(m_Direct3DDevice) { // Initialise world view projection matrix D3DXMatrixIdentity(&m_WorldViewProjection); // Set the position of the camera GetCamera().position() = D3DXVECTOR3(0.0f, 1.8f, -10.0f); // Set the camera up float aspect = (float)m_WindowWidth/m_WindowHeight; // Used in D3DXMatrixPerspectiveFovLH() GetCamera().setLens(0.25f*PI, aspect, 0.5f, 1000.0f); // Create effects and vertex layouts BuildFX(); BuildVertexLayouts(); fx::InitAll(m_Direct3DDevice); InputLayout::InitAll(m_Direct3DDevice); // Initialise texture manager GetTextureMgr().init(m_Direct3DDevice); // Create crate resource view HR( D3DX10CreateShaderResourceViewFromFile(m_Direct3DDevice, // The device to create the texture with L"GreyBricks.jpg", // The name of the file 0, // Optional image info, specify null to use source dimensions and to creat a full chain of mipmap levels 0, // Used to spawn a new thread to load resources, pPump &m_DiffuseMapRV, // The pointer that is used to interface with the new resource 0) ); // Used with pPump // Create specular map resource view HR( D3DX10CreateShaderResourceViewFromFile(m_Direct3DDevice, // The device to create the texture with L"defaultspec.dds", // The name of the file to sue 0, // Optional image info, specify null to use source dimensions and to creat a full chain of mipmap levels 0, // Used to spawn a new thread to load resources, pPump &m_SpecMapRV, // The pointer that is used to interface with the new resource 0) ); // Used with pPump // Create crate resource view HR( D3DX10CreateShaderResourceViewFromFile(m_Direct3DDevice, // The device to create the texture with L"Tiles.jpg", // The name of the file 0, // Optional image info, specify null to use source dimensions and to creat a full chain of mipmap levels 0, // Used to spawn a new thread to load resources, pPump &m_RoofTilesRV, // The pointer that is used to interface with the new resource 0) ); // Used with pPump // Create crate resource view HR( D3DX10CreateShaderResourceViewFromFile(m_Direct3DDevice, // The device to create the texture with L"Grass.jpg", // The name of the file 0, // Optional image info, specify null to use source dimensions and to creat a full chain of mipmap levels 0, // Used to spawn a new thread to load resources, pPump &m_GrassRV, // The pointer that is used to interface with the new resource 0) ); // Used with pPump // Set the sky texture up m_EnvironmentMapRV = GetTextureMgr().createCubeTex(L"grassenvmap1024.dds"); // Set up parallel light m_ParallelLight.dir = D3DXVECTOR3(0.57735f, -0.57735f, -0.25f); // Reveresed m_ParallelLight.ambient = D3DXCOLOR(0.4f, 0.4f, 0.4f, 1.0f); m_ParallelLight.diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); m_ParallelLight.specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); // Set box position m_Box.SetPosition(0.0f, 1.0f, 0.0f); m_Floor.ScaleXZ(10, 10); // Initialise sky m_Sky.init(m_Direct3DDevice, m_EnvironmentMapRV, 5000.0f); }
void Mountains::init(ID3D10Device* device, DWORD m, DWORD n, float dx) { mDevice = device; mNumRows = m; mNumCols = n; mNumVertices = m*n; mNumFaces = (m-1)*(n-1)*2; std::vector<Vertex> vertices(mNumVertices); float halfWidth = (n-1)*dx*0.5f; float halfDepth = (m-1)*dx*0.5f; float du = 1.0f / (n-1); float dv = 1.0f / (m-1); for(DWORD i = 0; i < m; ++i) { float z = halfDepth - i*dx; for(DWORD j = 0; j < n; ++j) { float x = -halfWidth + j*dx; float y = getHeight(x,z); vertices[i*n+j].Pos = D3DXVECTOR3(x, y, z); // Stretch texture over grid. vertices[i*n+j].Tex.x = j*du; vertices[i*n+j].Tex.y = i*dv; // Compute the normals for the texture D3DXVECTOR3 normal; normal.x = -0.03f*z*cosf(0.1f*x) - 0.3f*cosf(0.1f*z); normal.y = 1.0f; normal.z = -0.3f*sinf(0.1f*x) + 0.03f*x*sinf(0.1f*z); D3DXVec3Normalize(&vertices[i*n+j].normal, &normal); } } D3D10_BUFFER_DESC vbd; vbd.Usage = D3D10_USAGE_IMMUTABLE; vbd.ByteWidth = sizeof(Vertex) * mNumVertices; vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER; vbd.CPUAccessFlags = 0; vbd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA vinitData; vinitData.pSysMem = &vertices[0]; HR(mDevice->CreateBuffer(&vbd, &vinitData, &mVertexBuffer)); std::vector<DWORD> indices(mNumFaces*3); // 3 indices per face // Iterate over each quad and compute indices. int k = 0; for(DWORD i = 0; i < m-1; ++i) { for(DWORD j = 0; j < n-1; ++j) { indices[k] = i*n+j; indices[k+1] = i*n+j+1; indices[k+2] = (i+1)*n+j; indices[k+3] = (i+1)*n+j; indices[k+4] = i*n+j+1; indices[k+5] = (i+1)*n+j+1; k += 6; // next quad } } D3D10_BUFFER_DESC ibd; ibd.Usage = D3D10_USAGE_IMMUTABLE; ibd.ByteWidth = sizeof(DWORD) * mNumFaces*3; ibd.BindFlags = D3D10_BIND_INDEX_BUFFER; ibd.CPUAccessFlags = 0; ibd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA iinitData; iinitData.pSysMem = &indices[0]; HR(mDevice->CreateBuffer(&ibd, &iinitData, &mIndexBuffer)); HR(D3DX10CreateShaderResourceViewFromFile(mDevice, L"grass.dds", 0, 0, &mGrassMapRV, 0 )); }
void AM_Model::updateDevice(ID3D10Device *dev, ID3D10Effect *effect, ID3D10EffectTechnique *t) { D3D10_BUFFER_DESC bd; D3D10_SUBRESOURCE_DATA initData; D3DX10_IMAGE_LOAD_INFO loadInfo; char* n; bool alreadyLoaded; MaterialInfo *ci; //vertex buffer description bd.Usage = D3D10_USAGE_DYNAMIC; bd.ByteWidth = sizeof(MMDVERTEX) * _numVertex; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bd.MiscFlags = 0; //vertex buffer data initData.pSysMem = cVertices; initData.SysMemPitch = 0; initData.SysMemSlicePitch = 0; dev->CreateBuffer(&bd, &initData, &v_buffer); //create the vertex buffer //index buffer description bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof(unsigned short) * _numSurface; bd.BindFlags = D3D10_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; initData.pSysMem = _surfaceList; dev->CreateBuffer(&bd, &initData, &i_buffer); //create the index buffer //texture load info (for textures and sphere maps) ZeroMemory(&loadInfo, sizeof(D3DX10_IMAGE_LOAD_INFO)); loadInfo.BindFlags = D3D10_BIND_SHADER_RESOURCE; loadInfo.Format = DXGI_FORMAT_BC1_UNORM; //load all textures and prepare them for D3D for (int i=0;i<(int)_numMaterial;i++) { alreadyLoaded = false; ci = _material[i].getInfo(); HRESULT h; if (_material[i].hasTexture()) { //if the material has a texture with it n = ci->TextureFile; //get the texture's name if (i != 0) { //only run this check if this is not the first material for (int j=i-1; j>=0;j--) { //check to see if this texture was already loaded if (_material[j].hasTexture() && !strcmp(n, _material[j].getInfo()->TextureFile)) { //if it was already loaded, copy the memory address //this saves memory usage texResViews[i] = texResViews[j]; alreadyLoaded = true; break; } } } if (!alreadyLoaded) { //D3D doesn't support loading TGA natively //if TGA texture, use our own TGA parser to prepare the texture for D3D std::string ext = std::string(ci->TextureFile).substr(std::string(ci->TextureFile).find_last_of('.')+1); if (!strcmp(ext.c_str(), "tga")) h = TGALoader::CreateShaderResourceViewFromTGAFile(dev, ci->TextureFile, &loadInfo, &(texResViews[i])); else h = D3DX10CreateShaderResourceViewFromFile(dev, ci->TextureFile, &loadInfo, NULL, &(texResViews[i]), NULL); if (FAILED(h)) { printf("load %s failed\n", ci->TextureFile); texResViews[i] = NULL; } } alreadyLoaded = false; } if (_material[i].hasSphere()) { //if the material has a sphere map with it n = ci->SphereFile; //get the sphere map's name if (i != 0) { //only run this check if this is not the first material for (int j=i-1; j>=0;j--) { //check to see if this sphere map texture was already loaded if (_material[j].hasSphere() && !strcmp(n, _material[j].getInfo()->SphereFile)) { //if it was already loaded, copy the memory address //this saves memory usage sphereResViews[i] = sphereResViews[j]; alreadyLoaded = true; break; } } } if (!alreadyLoaded) { //create the shader resource view h = D3DX10CreateShaderResourceViewFromFile(dev,ci->SphereFile, &loadInfo, NULL, &(sphereResViews[i]), NULL); if (FAILED(h)) sphereResViews[i] = NULL; } } } this->dev = dev; this->effect = effect; this->effectTech = t; //get pointers to variables in shader amb = effect->GetVariableByName("materialAmbient")->AsVector(); dif = effect->GetVariableByName("materialDiffuse")->AsVector(); spec = effect->GetVariableByName("materialSpecular")->AsVector(); shine = effect->GetVariableByName("shine")->AsScalar(); alpha = effect->GetVariableByName("alpha")->AsScalar(); useTex = effect->GetVariableByName("useTexture")->AsScalar(); useToon = effect->GetVariableByName("useToon")->AsScalar(); sphType = effect->GetVariableByName("sphereType")->AsScalar(); tex = effect->GetVariableByName("texDiffuse")->AsShaderResource(); sph = effect->GetVariableByName("sphTexture")->AsShaderResource(); toon = effect->GetVariableByName("toonTexture")->AsShaderResource(); palette = effect->GetVariableByName("MatrixPalette")->AsMatrix(); world = effect->GetVariableByName("worldMatrix")->AsMatrix(); lightsOff = effect->GetVariableByName("lightsOff")->AsScalar(); extraBright = effect->GetVariableByName("extraBright")->AsScalar(); fadeCoef = effect->GetVariableByName("fadeCoef")->AsScalar(); //set up D3D to use the new vertex & index buffers UINT stride = sizeof(MMDVERTEX); UINT offset = 0; dev->IASetVertexBuffers(0, 1, &v_buffer, &stride, &offset); dev->IASetIndexBuffer(i_buffer, DXGI_FORMAT_R16_UINT, 0); dev->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); }
HRESULT fp_RenderRaytrace::OnD3D10CreateDevice( ID3D10Device* D3DDevice, const DXGI_SURFACE_DESC* BackBufferSurfaceDesc, void* UserContext ) { HRESULT hr; V_RETURN(CreateVolumeTexture(D3DDevice)); // CreateWValsMulParticleMass texture D3D10_TEXTURE1D_DESC texDesc; texDesc.BindFlags = D3D10_BIND_SHADER_RESOURCE; texDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; texDesc.MipLevels = 1; texDesc.ArraySize = 1; texDesc.Format = DXGI_FORMAT_R32_FLOAT; texDesc.MiscFlags = 0; texDesc.Usage = D3D10_USAGE_DYNAMIC; texDesc.Width = m_WValsMulParticleMassLength = 32; V_RETURN(D3DDevice->CreateTexture1D(&texDesc, NULL, &m_WValsMulParticleMassTexture)); // Create CreateWValsMulParticleMass shader resource view D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc; ZeroMemory( &srvDesc, sizeof(srvDesc) ); srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE1D; srvDesc.Texture3D.MipLevels = 1; srvDesc.Texture3D.MostDetailedMip = 0; V_RETURN(D3DDevice->CreateShaderResourceView(m_WValsMulParticleMassTexture, &srvDesc, &m_WValsMulParticleMassSRV)); // Create environment maps WCHAR absolutePath[MAX_PATH], absoluteDir[MAX_PATH], defaultCubeMapPath[MAX_PATH]; StringCchPrintf(defaultCubeMapPath, MAX_PATH, L"%s%s", FP_RENDER_RAYTRACE_CUBEMAP_DIR, FP_RENDER_RAYTRACE_DEFAULT_CUBEMAP); V_RETURN(DXUTFindDXSDKMediaFileCch(absolutePath, MAX_PATH, defaultCubeMapPath)); std::wstring absolutePathString = absolutePath; SIZE_T lastSlash = absolutePathString.find_last_of('\\'); StringCchCopy(absoluteDir, lastSlash + 2, absolutePath); fp_Util::ListDirectory(&m_CubeMapNames, absoluteDir, L"dds"); for(size_t i=0, size=m_CubeMapNames.size(); i < size; i++) { if(m_CubeMapNames[i].compare(FP_RENDER_RAYTRACE_DEFAULT_CUBEMAP) == 0) m_CurrentCubeMap = (int)i; StringCchPrintf(absolutePath, MAX_PATH, L"%s%s", absoluteDir, m_CubeMapNames[i].c_str()); m_EnvironmentMapSRV.push_back(NULL); V_RETURN(D3DX10CreateShaderResourceViewFromFile(D3DDevice, absolutePath, NULL, NULL, &m_EnvironmentMapSRV.back(), NULL)); } // Read the D3DX effect file m_Effect = fp_Util::LoadEffect(D3DDevice, FP_RENDER_RAYTRACE_EFFECT_FILE); // Obtain technique objects m_TechRenderRaytrace = m_Effect->GetTechniqueByName("RenderRaytrace"); // Obtain effect variables m_EffectVarCornersPos = m_Effect->GetVariableByName("g_CornersPos") ->AsVector(); m_EffectVarHalfParticleVoxelDiameter = m_Effect->GetVariableByName( "g_HalfParticleVoxelDiameter")->AsScalar(); m_EffectVarParticleVoxelRadius = m_Effect->GetVariableByName( "g_ParticleVoxelRadius")->AsScalar(); m_EffectVarVolumeDimensions = m_Effect->GetVariableByName("g_VolumeDimensions") ->AsVector(); m_EffectVarWorldToNDS = m_Effect->GetVariableByName("g_WorldToNDS")->AsMatrix(); m_EffectVarWValsMulParticleMass = m_Effect->GetVariableByName( "g_WValsMulParticleMass")->AsShaderResource(); m_EffectVarBBoxStart = m_Effect->GetVariableByName( "g_BBoxStart")->AsVector(); m_EffectVarBBoxSize = m_Effect->GetVariableByName( "g_BBoxSize")->AsVector(); m_EffectVarWorld = m_Effect->GetVariableByName( "g_World")->AsMatrix(); m_EffectVarWorldView = m_Effect->GetVariableByName( "g_WorldView")->AsMatrix(); m_EffectVarWorldViewProjection = m_Effect->GetVariableByName( "g_WorldViewProjection")->AsMatrix(); m_EffectVarInvView = m_Effect->GetVariableByName( "g_InvView")->AsMatrix(); m_EffectVarExitPoint = m_Effect->GetVariableByName( "g_ExitPoint")->AsShaderResource(); m_EffectVarStepSize = m_Effect->GetVariableByName( "g_StepSize")->AsScalar(); m_EffectVarVolumeSizeRatio = m_Effect->GetVariableByName( "g_VolumeSizeRatio")->AsVector(); m_EffectVarDensityGrid = m_Effect->GetVariableByName( "g_DensityGrid")->AsShaderResource(); m_EffectVarIsoLevel = m_Effect->GetVariableByName( "g_IsoLevel")->AsScalar(); m_EffectVarTexDelta = m_Effect->GetVariableByName( "g_TexDelta")->AsVector(); m_EffectVarEnvironmentMap = m_Effect->GetVariableByName( "g_Environment")->AsShaderResource(); m_EffectVarRefractionRatio = m_Effect->GetVariableByName( "g_RefractionRatio")->AsScalar(); m_EffectVarRefractionRatioSq = m_Effect->GetVariableByName( "g_RefractionRatioSq")->AsScalar(); m_EffectVarR0 = m_Effect->GetVariableByName( "g_R0")->AsScalar(); m_EffectVarOneMinusR0 = m_Effect->GetVariableByName( "g_OneMinusR0")->AsScalar(); m_EffectVarRefractionRatio_2 = m_Effect->GetVariableByName( "g_RefractionRatio_2")->AsScalar(); m_EffectVarRefractionRatioSq_2 = m_Effect->GetVariableByName( "g_RefractionRatioSq_2")->AsScalar(); m_EffectVarR0_2 = m_Effect->GetVariableByName( "g_R0_2")->AsScalar(); m_EffectVarOneMinusR0_2 = m_Effect->GetVariableByName( "g_OneMinusR0_2")->AsScalar(); BOOL allValid = m_EffectVarCornersPos->IsValid() != 0; allValid |= m_EffectVarHalfParticleVoxelDiameter->IsValid() != 0; allValid |= m_EffectVarParticleVoxelRadius->IsValid() != 0; allValid |= m_EffectVarVolumeDimensions->IsValid() != 0; allValid |= m_EffectVarWorldToNDS->IsValid() != 0; allValid |= m_EffectVarWValsMulParticleMass->IsValid() != 0; allValid |= m_EffectVarBBoxStart->IsValid() != 0; allValid |= m_EffectVarBBoxSize->IsValid() != 0; allValid |= m_EffectVarInvView->IsValid() != 0; allValid |= m_EffectVarWorldViewProjection->IsValid() != 0; allValid |= m_EffectVarWorldView->IsValid() != 0; allValid |= m_EffectVarWorld->IsValid() != 0; allValid |= m_EffectVarExitPoint->IsValid() != 0; allValid |= m_EffectVarStepSize->IsValid() != 0; allValid |= m_EffectVarVolumeSizeRatio->IsValid() != 0; allValid |= m_EffectVarDensityGrid->IsValid() != 0; allValid |= m_EffectVarIsoLevel->IsValid() != 0; allValid |= m_EffectVarTexDelta->IsValid() != 0; allValid |= m_EffectVarEnvironmentMap->IsValid() != 0; allValid |= m_EffectVarRefractionRatio->IsValid() != 0; allValid |= m_EffectVarRefractionRatioSq->IsValid() != 0; allValid |= m_EffectVarR0->IsValid() != 0; allValid |= m_EffectVarOneMinusR0->IsValid() != 0; allValid |= m_EffectVarRefractionRatio_2->IsValid() != 0; allValid |= m_EffectVarRefractionRatioSq_2->IsValid() != 0; allValid |= m_EffectVarR0_2->IsValid() != 0; allValid |= m_EffectVarOneMinusR0_2->IsValid() != 0; if(!allValid) return E_FAIL; // Set effect variables as needed V_RETURN(m_EffectVarVolumeDimensions->SetIntVector((int*)&m_VolumeDimensions)); m_NeedPerPixelStepSize = m_VolumeDimensions.x != m_VolumeDimensions.y || m_VolumeDimensions.x != m_VolumeDimensions.z; SetVoxelSize(m_VoxelSize); D3DXVECTOR3 start = m_BBox.GetStart(); SetVolumeStartPos(&start); SetStepScale(m_StepScale); float maxDim = (float) m_VolumeDimensions.Max(); D3DXVECTOR3 volumeSizeRatio = D3DXVECTOR3(m_VolumeDimensions.x / maxDim, m_VolumeDimensions.y / maxDim, m_VolumeDimensions.y / maxDim); V_RETURN(m_EffectVarVolumeSizeRatio->SetFloatVector((float*)&volumeSizeRatio)); V_RETURN(m_EffectVarIsoLevel->SetFloat(m_IsoLevel)); D3DXVECTOR3 texDelta; texDelta.x = 1.0f / m_VolumeDimensions.x; texDelta.y = 1.0f / m_VolumeDimensions.y; texDelta.z = 1.0f / m_VolumeDimensions.z; V_RETURN(m_EffectVarTexDelta->SetFloatVector((float*)&texDelta)); D3DXVECTOR3 environmentBoxSize = m_EnvironmentBox.GetSize(); SetRefractionRatio(FP_RAYTRACE_DEFAULT_REFRACTION_RATIO); // Create vertex buffer D3D10_PASS_DESC passDesc; V_RETURN( m_TechRenderRaytrace->GetPassByIndex(0)->GetDesc(&passDesc)); int numElements = sizeof(fp_SplatParticleVertex::Layout) / sizeof(fp_SplatParticleVertex::Layout[0]); V_RETURN(D3DDevice->CreateInputLayout(fp_SplatParticleVertex::Layout, numElements, passDesc.pIAInputSignature, passDesc.IAInputSignatureSize, &m_SplatParticleVertexLayout)); D3D10_BUFFER_DESC bufferDesc; bufferDesc.Usage = D3D10_USAGE_DYNAMIC; bufferDesc.ByteWidth = m_NumParticles * sizeof(fp_SplatParticleVertex); bufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER; bufferDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; bufferDesc.MiscFlags = 0; V_RETURN(D3DDevice->CreateBuffer(&bufferDesc, NULL, &m_SplatParticleVertexBuffer)); m_BBox.OnD3D10CreateDevice(D3DDevice, m_TechRenderRaytrace); m_EnvironmentBox.OnD3D10CreateDevice(D3DDevice, m_TechRenderRaytrace); return S_OK; }
// assumes that deck is full of cards // if bFull, create a full deck, otherwise create an empty deck // empty decks get cards from other non empty decks HRESULT CDeck::InitSprite(ID3D10Device* pD3D10Device, bool bFull) { HRESULT hr = S_OK; D3DX10_IMAGE_INFO InfoFromFile; D3DX10_IMAGE_LOAD_INFO LoadImageInfo; const_deckIterator deckitr; int i; DWORD nDirectoryLength = 255; TCHAR lpCurrentDir[255]; if (bFull) { // index 0 refers to back side of card LPTSTR lpCardName[NUM_CARD_VIEWS] = { L"b1fv", L"h1", L"h2", L"h3", L"h4", L"h5", L"h6", L"h7", L"h8", L"h9", L"h10", L"hj", L"hq", L"hk", L"d1", L"d2", L"d3", L"d4", L"d5", L"d6", L"d7", L"d8", L"d9", L"d10", L"dj", L"dq", L"dk", L"c1", L"c2", L"c3", L"c4", L"c5", L"c6", L"c7", L"c8", L"c9", L"c10", L"cj", L"cq", L"ck", L"s1", L"s2", L"s3", L"s4", L"s5", L"s6", L"s7", L"s8", L"s9", L"s10", L"sj", L"sq", L"sk", }; // set up file path GetCurrentDirectory(nDirectoryLength, lpCurrentDir); lstrcat(lpCurrentDir, (LPCTSTR)L"\\Graphics\\"); deckitr = getDeckIterator(); for (i=0;i<NUM_CARD_VIEWS;i++) { if (m_cardGraphics[i].pShaderResource == NULL) { m_cardGraphics[i].ID = i; m_cardGraphics[i].lpCardDesc[0] = '\0'; lstrcpy((LPWSTR)m_cardGraphics[i].lpFileName, lpCurrentDir); lstrcat((LPWSTR)m_cardGraphics[i].lpFileName, lpCardName[i]); lstrcat((LPWSTR)m_cardGraphics[i].lpFileName, L".png"); hr = D3DX10GetImageInfoFromFile((LPWSTR)m_cardGraphics[i].lpFileName, NULL, &InfoFromFile, NULL); if (FAILED(hr)) { return -1; } LoadImageInfo.Width = InfoFromFile.Width; LoadImageInfo.Height = InfoFromFile.Height; LoadImageInfo.Depth = InfoFromFile.Depth; LoadImageInfo.FirstMipLevel = 1; LoadImageInfo.MipLevels = InfoFromFile.MipLevels; LoadImageInfo.Usage = D3D10_USAGE_DEFAULT; LoadImageInfo.BindFlags = D3D10_BIND_SHADER_RESOURCE; LoadImageInfo.CpuAccessFlags = 0; LoadImageInfo.MiscFlags = 0; LoadImageInfo.Format = InfoFromFile.Format; LoadImageInfo.Filter = D3DX10_FILTER_NONE; LoadImageInfo.MipFilter = D3DX10_FILTER_NONE; LoadImageInfo.pSrcInfo = &InfoFromFile; hr = D3DX10CreateShaderResourceViewFromFile(pD3D10Device, (LPWSTR)m_cardGraphics[i].lpFileName, &LoadImageInfo, NULL, &(m_cardGraphics[i].pShaderResource), NULL); if (FAILED(hr)) { return hr; } // now assign textures to each card. // assume that deck has not been shuffled // and is in same order as lpCardName array. if ((deckitr != NULL ) && (i >= 1)) { POINT pt; pt.x = 100; pt.y = 100; deckitr->data->setFrontTexture(m_cardGraphics[i].pShaderResource); deckitr->data->setBackTexture(m_cardGraphics[0].pShaderResource); deckitr->data->setSpriteWidth(InfoFromFile.Width); deckitr->data->setSpriteLength(InfoFromFile.Height); deckitr->data->setFileName(m_cardGraphics[i].lpFileName); deckitr->data->setLocation(pt); deckitr = deckitr->next; } } else { if ((deckitr != NULL ) && (i >= 1)) { deckitr->data->setFrontTexture(m_cardGraphics[i].pShaderResource); deckitr->data->setBackTexture(m_cardGraphics[0].pShaderResource); deckitr = deckitr->next; } } } } return S_OK; }
//-------------------------------------------------------------------------------------- // Create any D3D10 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D10CreateDevice( ID3D10Device* pd3dDevice, const DXGI_SURFACE_DESC* pBufferSurfaceDesc, void* pUserContext ) { HRESULT hr = S_OK; // Read the D3DX effect file DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3D10_SHADER_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3D10_SHADER_DEBUG; #endif hr = D3DX10CreateEffectFromFile( L"Tutorial08.fx", NULL, NULL, "fx_4_0", dwShaderFlags, 0, pd3dDevice, NULL, NULL, &g_pEffect, NULL, NULL ); if( FAILED( hr ) ) { MessageBox( NULL, L"The FX file cannot be located. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK ); V_RETURN( hr ); } g_pTechnique = g_pEffect->GetTechniqueByName( "Render" ); g_pWorldVariable = g_pEffect->GetVariableByName( "World" )->AsMatrix(); g_pViewVariable = g_pEffect->GetVariableByName( "View" )->AsMatrix(); g_pProjectionVariable = g_pEffect->GetVariableByName( "Projection" )->AsMatrix(); g_pMeshColorVariable = g_pEffect->GetVariableByName( "vMeshColor" )->AsVector(); g_pDiffuseVariable = g_pEffect->GetVariableByName( "txDiffuse" )->AsShaderResource(); // Define the input layout D3D10_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; UINT numElements = sizeof( layout ) / sizeof( layout[0] ); // Create the input layout D3D10_PASS_DESC PassDesc; g_pTechnique->GetPassByIndex( 0 )->GetDesc( &PassDesc ); V_RETURN( pd3dDevice->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pVertexLayout ) ); // Set the input layout pd3dDevice->IASetInputLayout( g_pVertexLayout ); // Create vertex buffer SimpleVertex vertices[] = { { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) }, { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) }, { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) }, { D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) }, { D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) }, { D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) }, { D3DXVECTOR3( -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) }, }; D3D10_BUFFER_DESC bd; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof( SimpleVertex ) * 24; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA InitData; InitData.pSysMem = vertices; V_RETURN( pd3dDevice->CreateBuffer( &bd, &InitData, &g_pVertexBuffer ) ); // Set vertex buffer UINT stride = sizeof( SimpleVertex ); UINT offset = 0; pd3dDevice->IASetVertexBuffers( 0, 1, &g_pVertexBuffer, &stride, &offset ); // Create index buffer // Create vertex buffer DWORD indices[] = { 3,1,0, 2,1,3, 6,4,5, 7,4,6, 11,9,8, 10,9,11, 14,12,13, 15,12,14, 19,17,16, 18,17,19, 22,20,21, 23,20,22 }; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof( DWORD ) * 36; bd.BindFlags = D3D10_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; InitData.pSysMem = indices; V_RETURN( pd3dDevice->CreateBuffer( &bd, &InitData, &g_pIndexBuffer ) ); // Set index buffer pd3dDevice->IASetIndexBuffer( g_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 ); // Set primitive topology pd3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); // Load the Texture hr = D3DX10CreateShaderResourceViewFromFile( pd3dDevice, L"seafloor.dds", NULL, NULL, &g_pTextureRV, NULL ); // Initialize the world matrices D3DXMatrixIdentity( &g_World ); // Initialize the view matrix D3DXVECTOR3 Eye( 0.0f, 3.0f, -6.0f ); D3DXVECTOR3 At( 0.0f, 1.0f, 0.0f ); D3DXVECTOR3 Up( 0.0f, 1.0f, 0.0f ); D3DXMatrixLookAtLH( &g_View, &Eye, &At, &Up ); // Update Variables that never change g_pViewVariable->SetMatrix( ( float* )&g_View ); g_pDiffuseVariable->SetResource( g_pTextureRV ); return S_OK; }
bool InitResourceDX10(void) { g_pDevice = GutGetGraphicsDeviceDX10(); ID3D10Blob *pVSCode = NULL; // 載入Vertex Shader g_pVertexShader = GutLoadVertexShaderDX10_HLSL("../../shaders/texture_dx10.shader", "VS", "vs_4_0", &pVSCode); if ( NULL==g_pVertexShader ) return false; // 載入Pixel Shader g_pPixelShader = GutLoadPixelShaderDX10_HLSL("../../shaders/texture_dx10.shader", "PS", "ps_4_0"); if ( NULL==g_pPixelShader ) return false; if ( D3D_OK!=D3DX10CreateShaderResourceViewFromFile(g_pDevice, "../../textures/lena.bmp", NULL, NULL, &g_pTexture, NULL) ) return false; // 設定Vertex資料格式 D3D10_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 } }; if ( D3D_OK != g_pDevice->CreateInputLayout( layout, sizeof(layout)/sizeof(D3D10_INPUT_ELEMENT_DESC), pVSCode->GetBufferPointer(), pVSCode->GetBufferSize(), &g_pVertexLayout ) ) return false; SAFE_RELEASE(pVSCode); D3D10_BUFFER_DESC cbDesc; // vertex buffer cbDesc.ByteWidth = sizeof(Vertex_VT) * 4; cbDesc.Usage = D3D10_USAGE_DYNAMIC ; cbDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER; cbDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; cbDesc.MiscFlags = 0; D3D10_SUBRESOURCE_DATA subDesc; ZeroMemory(&subDesc, sizeof(subDesc)); subDesc.pSysMem = g_Quad; // 配置一塊可以存放Vertex的記憶體, 也就是Vertex Buffer. if ( D3D_OK != g_pDevice->CreateBuffer( &cbDesc, &subDesc, &g_pVertexBuffer ) ) return false; // 配置Shader參數的記憶體空間 cbDesc.ByteWidth = sizeof(Matrix4x4); cbDesc.Usage = D3D10_USAGE_DYNAMIC; cbDesc.BindFlags = D3D10_BIND_CONSTANT_BUFFER; cbDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; cbDesc.MiscFlags = 0; if ( D3D_OK != g_pDevice->CreateBuffer( &cbDesc, NULL, &g_pConstantBuffer ) ) return false; // 計算投影矩陣 g_proj_matrix = GutMatrixPerspectiveRH_DirectX(g_fFovW, 1.0f, 0.1f, 100.0f); // rasterizer state物件 { D3D10_RASTERIZER_DESC desc; GutSetDX10DefaultRasterizerDesc(desc); desc.CullMode = D3D10_CULL_NONE; desc.FrontCounterClockwise = true; desc.ScissorEnable = true; if ( D3D_OK != g_pDevice->CreateRasterizerState(&desc, &g_pRasterizerState) ) return false; g_pDevice->RSSetState(g_pRasterizerState); } g_view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up); return true; }
//-------------------------------------------------------------------------------------- // Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED) // and aren't tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D10CreateDevice( ID3D10Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( g_SettingsDlg.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( D3DX10CreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont10 ) ); V_RETURN( D3DX10CreateSprite( pd3dDevice, 512, &g_pSprite10 ) ); g_pTxtHelper = new CDXUTTextHelper( NULL, NULL, g_pFont10, g_pSprite10, 15 ); #define IDC_WIREFRAME 10 g_SampleUI.GetCheckBox( IDC_WIREFRAME )->SetVisible( false ); V_RETURN( LoadEffect10( pd3dDevice ) ); WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"PIXWorkshop\\Terrain1.bmp" ) ); V_RETURN( g_Terrain.LoadTerrain( str, g_SqrtNumTiles, g_SidesPerTile, g_fWorldScale, g_fHeightScale, 1000, 1.0f, 2.0f ) ); ResetBalls(); // Create a Vertex Decl for the terrain and basic meshes const D3D10_INPUT_ELEMENT_DESC basiclayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; D3D10_PASS_DESC PassDesc; g_pRenderTerrain->GetPassByIndex( 0 )->GetDesc( &PassDesc ); V_RETURN( pd3dDevice->CreateInputLayout( basiclayout, sizeof( basiclayout ) / sizeof( basiclayout[0] ), PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pBasicDecl10 ) ); // Create a Vertex Decl for the ball const D3D10_INPUT_ELEMENT_DESC balllayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 1, DXGI_FORMAT_R32G32B32_FLOAT, 1, 0, D3D10_INPUT_PER_INSTANCE_DATA, 1 }, }; g_pRenderBall->GetPassByIndex( 0 )->GetDesc( &PassDesc ); V_RETURN( pd3dDevice->CreateInputLayout( balllayout, sizeof( balllayout ) / sizeof( balllayout[0] ), PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pBallDecl10 ) ); // Create a Vertex Decl for the grass const D3D10_INPUT_ELEMENT_DESC grasslayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 1, DXGI_FORMAT_R32G32B32_FLOAT, 1, 0, D3D10_INPUT_PER_INSTANCE_DATA, 1 }, }; g_pRenderGrass->GetPassByIndex( 0 )->GetDesc( &PassDesc ); V_RETURN( pd3dDevice->CreateInputLayout( grasslayout, sizeof( grasslayout ) / sizeof( grasslayout[0] ), PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pGrassDecl10 ) ); // Load terrain device objects V_RETURN( g_Terrain.OnCreateDevice( pd3dDevice ) ); // Load a mesh g_BallMesh.Create( pd3dDevice, L"PIXWorkshop\\lowpolysphere.sdkmesh" ); g_SkyMesh.Create( pd3dDevice, L"PIXWorkshop\\desertsky.sdkmesh" ); // Create a VB for the stream data D3D10_BUFFER_DESC BufferDesc; BufferDesc.ByteWidth = NUM_PLAYERS * sizeof( D3DXVECTOR3 ); BufferDesc.Usage = D3D10_USAGE_DYNAMIC; BufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER; BufferDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; BufferDesc.MiscFlags = 0; V_RETURN( pd3dDevice->CreateBuffer( &BufferDesc, NULL, &g_pStreamDataVB10 ) ); // Create a VB for the grass instances BufferDesc.ByteWidth = g_SqrtNumTiles * g_SqrtNumTiles * sizeof( D3DXVECTOR3 ); V_RETURN( pd3dDevice->CreateBuffer( &BufferDesc, NULL, &g_pGrassDataVB10 ) ); // Load a texture for the mesh V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"PIXWorkshop\\Terrain1.bmp" ) ); V_RETURN( D3DX10CreateShaderResourceViewFromFile( pd3dDevice, str, NULL, NULL, &g_pHeightTexRV, NULL ) ); V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"PIXWorkshop\\Terrain1_Norm.dds" ) ); V_RETURN( D3DX10CreateShaderResourceViewFromFile( pd3dDevice, str, NULL, NULL, &g_pNormalTexRV, NULL ) ); V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"PIXWorkshop\\grass_v3_dark_tex.dds" ) ); V_RETURN( D3DX10CreateShaderResourceViewFromFile( pd3dDevice, str, NULL, NULL, &g_pGrassTexRV, NULL ) ); V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"PIXWorkshop\\Dirt_Diff.dds" ) ); V_RETURN( D3DX10CreateShaderResourceViewFromFile( pd3dDevice, str, NULL, NULL, &g_pDirtTexRV, NULL ) ); V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"PIXWorkshop\\Grass_Diff.dds" ) ); V_RETURN( D3DX10CreateShaderResourceViewFromFile( pd3dDevice, str, NULL, NULL, &g_pGroundGrassTexRV, NULL ) ); V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"PIXWorkshop\\Terrain1_Mask.dds" ) ); V_RETURN( D3DX10CreateShaderResourceViewFromFile( pd3dDevice, str, NULL, NULL, &g_pMaskTexRV, NULL ) ); V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"PIXWorkshop\\Terrain1_ShadeNormals.dds" ) ); V_RETURN( D3DX10CreateShaderResourceViewFromFile( pd3dDevice, str, NULL, NULL, &g_pShadeNormalTexRV, NULL ) ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 20.0f, -50.0f ); D3DXVECTOR3 vecAt ( 0.0f, 5.0f, 0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); return S_OK; }