bool ParticleSystemClass::UpdateBuffers(ID3D11DeviceContext* deviceContext)//is called every frame and rebuilds the entire dynamic vertexbuffer with the updated position of all the particles in the particle system { int index, i; HRESULT result; D3D11_MAPPED_SUBRESOURCE mappedResource; VertexType* verticesPtr; //initialize vertex array to zeroes at first memset(m_vertices, 0, (sizeof(VertexType) *m_vertexCount)); //build the vertex array from the particle list array. each particle is a quad of two tris index = 0; for(i=0; i<m_currentParticleCount; i++) { //bottom left m_vertices[index].position = D3DXVECTOR3(m_particleList[i].positionX - m_particleSize, m_particleList[i].positionY - m_particleSize, m_particleList[i].positionZ); m_vertices[index].position = m_vertices[index].position.operator+(D3DXVECTOR3(-m_particleList[i].positionX, -m_particleList[i].positionY, -m_particleList[i].positionZ)); D3DXVec3TransformCoord( &m_vertices[index].position, &m_vertices[index].position, &RotateParticle( D3DXVECTOR3(m_particleList[i].positionX, m_particleList[i].positionY, m_particleList[i].positionZ), m_vertices[index].position, m_particleList[i].m_rotationX, m_particleList[i].m_rotationY, m_particleList[i].m_rotationZ) ); m_vertices[index].position = m_vertices[index].position.operator+(D3DXVECTOR3(m_particleList[i].positionX, m_particleList[i].positionY, m_particleList[i].positionZ)); m_vertices[index].texture = D3DXVECTOR2(0.0f, 1.0f); m_vertices[index].color = D3DXVECTOR4(m_particleList[i].red, m_particleList[i].green, m_particleList[i].blue, 1.0f); index++; //top left m_vertices[index].position = D3DXVECTOR3(m_particleList[i].positionX - m_particleSize, m_particleList[i].positionY + m_particleSize, m_particleList[i].positionZ); m_vertices[index].position = m_vertices[index].position.operator+(D3DXVECTOR3(-m_particleList[i].positionX, -m_particleList[i].positionY, -m_particleList[i].positionZ)); D3DXVec3TransformCoord( &m_vertices[index].position, &m_vertices[index].position, &RotateParticle( D3DXVECTOR3(m_particleList[i].positionX, m_particleList[i].positionY, m_particleList[i].positionZ), m_vertices[index].position, m_particleList[i].m_rotationX, m_particleList[i].m_rotationY, m_particleList[i].m_rotationZ) ); m_vertices[index].position = m_vertices[index].position.operator+(D3DXVECTOR3(m_particleList[i].positionX, m_particleList[i].positionY, m_particleList[i].positionZ)); m_vertices[index].texture = D3DXVECTOR2(0.0f, 0.0f); m_vertices[index].color = D3DXVECTOR4(m_particleList[i].red, m_particleList[i].green, m_particleList[i].blue, 1.0f); index++; //bottom right m_vertices[index].position = D3DXVECTOR3(m_particleList[i].positionX + m_particleSize, m_particleList[i].positionY - m_particleSize, m_particleList[i].positionZ); m_vertices[index].position = m_vertices[index].position.operator+(D3DXVECTOR3(-m_particleList[i].positionX, -m_particleList[i].positionY, -m_particleList[i].positionZ)); D3DXVec3TransformCoord( &m_vertices[index].position, &m_vertices[index].position, &RotateParticle( D3DXVECTOR3(m_particleList[i].positionX, m_particleList[i].positionY, m_particleList[i].positionZ), m_vertices[index].position, m_particleList[i].m_rotationX, m_particleList[i].m_rotationY, m_particleList[i].m_rotationZ) ); m_vertices[index].position = m_vertices[index].position.operator+(D3DXVECTOR3(m_particleList[i].positionX, m_particleList[i].positionY, m_particleList[i].positionZ)); m_vertices[index].texture = D3DXVECTOR2(1.0f, 1.0f); m_vertices[index].color = D3DXVECTOR4(m_particleList[i].red, m_particleList[i].green, m_particleList[i].blue, 1.0f); index++; //bottom right m_vertices[index].position = D3DXVECTOR3(m_particleList[i].positionX + m_particleSize, m_particleList[i].positionY - m_particleSize, m_particleList[i].positionZ); m_vertices[index].position = m_vertices[index].position.operator+(D3DXVECTOR3(-m_particleList[i].positionX, -m_particleList[i].positionY, -m_particleList[i].positionZ)); D3DXVec3TransformCoord( &m_vertices[index].position, &m_vertices[index].position, &RotateParticle( D3DXVECTOR3(m_particleList[i].positionX, m_particleList[i].positionY, m_particleList[i].positionZ), m_vertices[index].position, m_particleList[i].m_rotationX, m_particleList[i].m_rotationY, m_particleList[i].m_rotationZ) ); m_vertices[index].position = m_vertices[index].position.operator+(D3DXVECTOR3(m_particleList[i].positionX, m_particleList[i].positionY, m_particleList[i].positionZ)); m_vertices[index].texture = D3DXVECTOR2(1.0f, 1.0f); m_vertices[index].color = D3DXVECTOR4(m_particleList[i].red, m_particleList[i].green, m_particleList[i].blue, 1.0f); index++; //top left m_vertices[index].position = D3DXVECTOR3(m_particleList[i].positionX - m_particleSize, m_particleList[i].positionY + m_particleSize, m_particleList[i].positionZ); m_vertices[index].position = m_vertices[index].position.operator+(D3DXVECTOR3(-m_particleList[i].positionX, -m_particleList[i].positionY, -m_particleList[i].positionZ)); D3DXVec3TransformCoord( &m_vertices[index].position, &m_vertices[index].position, &RotateParticle( D3DXVECTOR3(m_particleList[i].positionX, m_particleList[i].positionY, m_particleList[i].positionZ), m_vertices[index].position, m_particleList[i].m_rotationX, m_particleList[i].m_rotationY, m_particleList[i].m_rotationZ) ); m_vertices[index].position = m_vertices[index].position.operator+(D3DXVECTOR3(m_particleList[i].positionX, m_particleList[i].positionY, m_particleList[i].positionZ)); m_vertices[index].texture = D3DXVECTOR2(0.0f, 0.0f); m_vertices[index].color = D3DXVECTOR4(m_particleList[i].red, m_particleList[i].green, m_particleList[i].blue, 1.0f); index++; //top right m_vertices[index].position = D3DXVECTOR3(m_particleList[i].positionX + m_particleSize, m_particleList[i].positionY + m_particleSize, m_particleList[i].positionZ); m_vertices[index].position = m_vertices[index].position.operator+(D3DXVECTOR3(-m_particleList[i].positionX, -m_particleList[i].positionY, -m_particleList[i].positionZ)); D3DXVec3TransformCoord( &m_vertices[index].position, &m_vertices[index].position, &RotateParticle( D3DXVECTOR3(m_particleList[i].positionX, m_particleList[i].positionY, m_particleList[i].positionZ), m_vertices[index].position, m_particleList[i].m_rotationX, m_particleList[i].m_rotationY, m_particleList[i].m_rotationZ) ); m_vertices[index].position = m_vertices[index].position.operator+(D3DXVECTOR3(m_particleList[i].positionX, m_particleList[i].positionY, m_particleList[i].positionZ)); m_vertices[index].texture = D3DXVECTOR2(1.0f, 0.0f); m_vertices[index].color = D3DXVECTOR4(m_particleList[i].red, m_particleList[i].green, m_particleList[i].blue, 1.0f); index++; } //lock the vertex Buffer result = deviceContext->Map(m_vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if(FAILED(result)) { return false; } //get a pointer to the data in the vertex buffer verticesPtr = (VertexType*)mappedResource.pData; //copy the data into the vertex buffer memcpy(verticesPtr, (void*)m_vertices, (sizeof(VertexType) *m_vertexCount)); //unlock the vertex buffer deviceContext->Unmap(m_vertexBuffer, 0); return true; }
void slides_render(float t) { Sleep(10); if(slides_current_texture!=slides_texture) { if(slides_tex) { slides_tex->Release(); slides_tex=NULL; } slides_current_texture=slides_texture; CreateThread(NULL, 0, slides_decode, NULL, 0, NULL); } g_pd3dDevice->BeginScene(); g_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0xff000000, 1.0f, 0L ); // Render the secondary color surface to the screen struct VERTEX { D3DXVECTOR4 p; FLOAT tu, tv; }; VERTEX v[4]; v[0].p = D3DXVECTOR4( SX( 0) - 0.5f, SY( 48) - 0.5f, 0, 1 ); v[0].tu = 0; v[0].tv = 0; v[1].p = D3DXVECTOR4( SX(640) - 0.5f, SY( 48) - 0.5f, 0, 1 ); v[1].tu = 1; v[1].tv = 0; v[2].p = D3DXVECTOR4( SX(640) - 0.5f, SY(432) - 0.5f, 0, 1 ); v[2].tu = 1; v[2].tv =1; v[3].p = D3DXVECTOR4( SX( 0) - 0.5f, SY(432) - 0.5f, 0, 1 ); v[3].tu = 0; v[3].tv = 1; m_pd3dDevice->SetFVF( D3DFVF_XYZRHW|D3DFVF_TEX1); m_pd3dDevice->SetTexture( 0, slides_tex ); m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0); m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE); m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_TFACTOR); m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR ); m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP ); m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER , D3DTEXF_LINEAR); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER , D3DTEXF_LINEAR); int color=CLAMP(fade)*255; m_pd3dDevice->SetRenderState( D3DRS_TEXTUREFACTOR, 0xff<<24|(0x010101*color)); m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE ); m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE ); g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ZERO ); m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE); if(slides_tex) WRAP(m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, v, 6*sizeof(FLOAT) )); m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE); m_pd3dDevice->SetTexture( 0, NULL ); g_pd3dDevice->EndScene(); // g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); }
//------------------------------------------------------------- // Name: Render() // Desc: 화면 렌더 //------------------------------------------------------------- HRESULT CMyD3DApplication::Render() { D3DXMATRIX m, mT, mR, mView, mProj; D3DXMATRIX mWorld; D3DXVECTOR4 v, light_pos, eye_pos; DWORD i; //--------------------------------------------------------- // 렌더 //--------------------------------------------------------- if( SUCCEEDED( m_pd3dDevice->BeginScene() ) ) { // 렌더링 타겟 클리어 m_pd3dDevice->Clear(0L, NULL , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER , 0x0060c0, 1.0f, 0L); //----------------------------------------------------- // 지형렌더 //----------------------------------------------------- // 월드행렬 D3DXMatrixScaling( &m, 3.0f, 3.0f, 3.0f ); D3DXMatrixRotationY( &mR, D3DX_PI ); D3DXMatrixTranslation( &mT, 0.0f,-2.0f ,0.0f ); mWorld = m * mR * mT; // 행렬설정 m_pd3dDevice->SetTransform( D3DTS_WORLD, &mWorld); m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_mView ); m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_mProj ); TSS( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ); TSS( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); m_pMeshBg->Render( m_pd3dDevice ); if( m_pEffect != NULL ) { //------------------------------------------------- // 셰이더 설정 //------------------------------------------------- m_pEffect->SetTechnique( m_hTechnique ); m_pEffect->Begin( NULL, 0 ); m_pEffect->Pass( 0 ); //------------------------------------------------- // 주전자 렌더 //------------------------------------------------- // 월드행렬(회전) D3DXMatrixRotationY( &mWorld, m_fTime ); // 로컬-투영 변환행렬 m = mWorld * m_mView * m_mProj; m_pEffect->SetMatrix( m_hmWVP, &m ); // 광원의 방향(로컬좌표계) light_pos = D3DXVECTOR4( -0.577f, -0.577f, -0.577f,0); D3DXMatrixInverse( &m, NULL, &mWorld); D3DXVec4Transform( &v, &light_pos, &m ); D3DXVec3Normalize( (D3DXVECTOR3 *)&v, (D3DXVECTOR3 *)&v ); v.w = -0.7f; // 환경광 강도 m_pEffect->SetVector( m_hvLightDir, &v ); // 시점(로컬좌표계) m = mWorld * m_mView; D3DXMatrixInverse( &m, NULL, &m); v = D3DXVECTOR4( 0, 0, 0, 1); D3DXVec4Transform( &v, &v, &m ); m_pEffect->SetVector( m_hvEyePos, &v ); // 법선맵 m_pEffect->SetTexture( m_htNormalMap, m_pNormalMap ); // 정점선언 m_pd3dDevice->SetVertexDeclaration( m_pDecl ); D3DMATERIAL9 *pMtrl = m_pMesh->m_pMaterials; for( i=0; i<m_pMesh->m_dwNumMaterials; i++ ) { v.x = pMtrl->Diffuse.r; v.y = pMtrl->Diffuse.g; v.z = pMtrl->Diffuse.b; m_pEffect->SetVector( m_hvColor, &v ); m_pEffect->SetTexture( m_htDecaleTex, m_pMesh->m_pTextures[i] ); m_pMesh->m_pLocalMesh->DrawSubset( i ); // 렌더 pMtrl++; } m_pEffect->End(); } // 도움말 출력 RenderText(); // 렌더종료 m_pd3dDevice->EndScene(); } return S_OK; }
void DxManager::RenderShadowMap() { // Set sun-settings this->Shader_DeferredLightning->SetBool("UseSun", this->useSun); if(this->useSun) { this->Shader_DeferredLightning->SetStructMemberAsFloat4("sun", "Direction", D3DXVECTOR4(this->sun.direction, 0.0f)); this->Shader_DeferredLightning->SetStructMemberAsFloat4("sun", "LightColor", D3DXVECTOR4(this->sun.lightColor, 0.0f)); this->Shader_DeferredLightning->SetStructMemberAsFloat("sun", "LightIntensity", this->sun.intensity); //Shader_ShadowMap->Apply(0); // Dont know why the f**k this has to be here, but it does, otherwise textures wont be sent when rendering objects. **Texture error** } // If special circle is used if(this->specialCircleParams.x) //if inner radius > 0, then send/set data { this->Shader_DeferredLightning->SetFloat4("dataPPHA", this->specialCircleParams); } // Generate and send shadowmaps to the main-shader if(!this->lights.size()) { for (int l = 0; l < this->lights.size(); l++) { Dx_DeviceContext->OMSetRenderTargets(0, 0, this->lights[l]->GetShadowMapDSV()); D3D11_VIEWPORT wp = this->lights[l]->GetShadowMapViewPort(); Dx_DeviceContext->RSSetViewports(1, &wp); Dx_DeviceContext->ClearDepthStencilView(this->lights[l]->GetShadowMapDSV(), D3D11_CLEAR_DEPTH, 1.0f, 0); //Static meshes for(int i = 0; i < this->objects.size(); i++) { if(!this->objects[i]->IsUsingInvisibility()) { MaloW::Array<MeshStrip*>* strips = this->objects[i]->GetStrips(); D3DXMATRIX wvp = this->objects[i]->GetWorldMatrix() * this->lights[l]->GetViewProjMatrix(); this->Shader_ShadowMap->SetMatrix("LightWVP", wvp); for(int u = 0; u < strips->size(); u++) { Object3D* obj = strips->get(u)->GetRenderObject(); Dx_DeviceContext->IASetPrimitiveTopology(obj->GetTopology()); Buffer* verts = obj->GetVertBuff(); if(verts) verts->Apply(); Shader_ShadowMap->SetBool("textured", false); Buffer* inds = obj->GetIndsBuff(); if(inds) inds->Apply(); Shader_ShadowMap->Apply(0); //Draw if(inds) Dx_DeviceContext->DrawIndexed(inds->GetElementCount(), 0, 0); else Dx_DeviceContext->Draw(verts->GetElementCount(), 0); } } } //Animated meshes for(int i = 0; i < this->animations.size(); i++) { if(!this->animations[i]->IsUsingInvisibility()) { KeyFrame* one = NULL; KeyFrame* two = NULL; float t = 0.0f; this->animations[i]->SetCurrentTime(this->Timer); this->animations[i]->GetCurrentKeyFrames(&one, &two, t); MaloW::Array<MeshStrip*>* stripsOne = one->strips; MaloW::Array<MeshStrip*>* stripsTwo = two->strips; //set shader data (per object) D3DXMATRIX wvp = this->animations[i]->GetWorldMatrix() * this->lights[l]->GetViewProjMatrix(); this->Shader_ShadowMapAnimated->SetMatrix("LightWVP", wvp); this->Shader_ShadowMapAnimated->SetFloat("t", t); for(int u = 0; u < stripsOne->size(); u++) { Object3D* objOne = stripsOne->get(u)->GetRenderObject(); Object3D* objTwo = stripsTwo->get(u)->GetRenderObject(); this->Dx_DeviceContext->IASetPrimitiveTopology(objOne->GetTopology()); Buffer* vertsOne = objOne->GetVertBuff(); Buffer* vertsTwo = objTwo->GetVertBuff(); ID3D11Buffer* vertexBuffers [] = {vertsOne->GetBufferPointer(), vertsTwo->GetBufferPointer()}; UINT strides [] = {sizeof(Vertex), sizeof(Vertex)}; UINT offsets [] = {0, 0}; this->Dx_DeviceContext->IASetVertexBuffers(0, 2, vertexBuffers, strides, offsets); Shader_ShadowMapAnimated->Apply(0); this->Dx_DeviceContext->Draw(vertsOne->GetElementCount(), 0); } } } D3DXMATRIX lvp = this->lights[l]->GetViewProjMatrix(); // Forward //this->Shader_ForwardRendering->SetResourceAtIndex(l, "ShadowMap", this->lights[l]->GetShadowMapSRV()); //this->Shader_ForwardRendering->SetStructMemberAtIndexAsMatrix(l, "lights", "LightViewProj", lvp); //this->Shader_ForwardRendering->SetStructMemberAtIndexAsFloat4(l, "lights", "LightPosition", D3DXVECTOR4(this->lights[l]->GetPosition(), 1)); //this->Shader_ForwardRendering->SetStructMemberAtIndexAsFloat4(l, "lights", "LightColor", D3DXVECTOR4(this->lights[l]->GetColor(), 1)); //this->Shader_ForwardRendering->SetStructMemberAtIndexAsFloat(l, "lights", "LightIntensity", this->lights[l]->GetIntensity()); // For deferred: this->Shader_DeferredLightning->SetResourceAtIndex(l, "ShadowMap", this->lights[l]->GetShadowMapSRV()); this->Shader_DeferredLightning->SetStructMemberAtIndexAsMatrix(l, "lights", "LightViewProj", lvp); this->Shader_DeferredLightning->SetStructMemberAtIndexAsFloat4(l, "lights", "LightPosition", D3DXVECTOR4(this->lights[l]->GetPosition(), 1)); this->Shader_DeferredLightning->SetStructMemberAtIndexAsFloat4(l, "lights", "LightColor", D3DXVECTOR4(this->lights[l]->GetColor(), 1)); this->Shader_DeferredLightning->SetStructMemberAtIndexAsFloat(l, "lights", "LightIntensity", this->lights[l]->GetIntensity()); // For deferred quad: //this->Shader_DeferredQuad->SetResourceAtIndex(l, "ShadowMap", this->lights[l]->GetShadowMapSRV()); //this->Shader_DeferredQuad->SetStructMemberAtIndexAsMatrix(l, "lights", "LightViewProj", lvp); //this->Shader_DeferredQuad->SetStructMemberAtIndexAsFloat4(l, "lights", "LightPosition", D3DXVECTOR4(this->lights[l]->GetPosition(), 1)); //this->Shader_DeferredQuad->SetStructMemberAtIndexAsFloat4(l, "lights", "LightColor", D3DXVECTOR4(this->lights[l]->GetColor(), 1)); //this->Shader_DeferredQuad->SetStructMemberAtIndexAsFloat(l, "lights", "LightIntensity", this->lights[l]->GetIntensity()); } } float PCF_SIZE = (float)this->params.ShadowMapSettings + 1; float PCF_SQUARED = 1 / (PCF_SIZE * PCF_SIZE); /* // Forward this->Shader_ForwardRendering->SetFloat("PCF_SIZE", PCF_SIZE); this->Shader_ForwardRendering->SetFloat("PCF_SIZE_SQUARED", PCF_SQUARED); this->Shader_ForwardRendering->SetFloat("SMAP_DX", 1.0f / (256 * pow(2.0f, this->params.ShadowMapSettings/2))); this->Shader_ForwardRendering->SetFloat("NrOfLights", (float)this->lights.size()); */ // Deferred: this->Shader_DeferredLightning->SetFloat("SMAP_DX", 1.0f / (256.0f * pow(2.0f, this->params.ShadowMapSettings / 2.0f))); this->Shader_DeferredLightning->SetFloat("PCF_SIZE", PCF_SIZE); this->Shader_DeferredLightning->SetFloat("PCF_SIZE_SQUARED", PCF_SQUARED); //this->Shader_DeferredLightning->SetFloat("SMAP_DX", 1.0f / 256.0f); this->Shader_DeferredLightning->SetFloat("NrOfLights", (float)this->lights.size()); /* // for deferred quad: this->Shader_DeferredQuad->SetFloat("PCF_SIZE", PCF_SIZE); this->Shader_DeferredQuad->SetFloat("PCF_SIZE_SQUARED", PCF_SQUARED); this->Shader_DeferredQuad->SetFloat("SMAP_DX", 1.0f / (256 * pow(2.0f, this->params.ShadowMapSettings/2))); this->Shader_DeferredQuad->SetFloat("NrOfLights", (float)this->lights.size()); */ }
void GraphicalPlane::Render(const Camera* camera) { // 描画しないならここで関数終了 if(!_renders) return; // 分割読み込みした場合の画像範囲選択 if(_previousNumber != _number) { Vertex* vertex; _mesh->GetMesh()->LockVertexBuffer( 0, (void**)&vertex ); vertex[0]._uv.x = (float)_rects[_number].left / _textures[0]->GetImageInfo().Width; vertex[0]._uv.y = (float)_rects[_number].bottom / _textures[0]->GetImageInfo().Height; vertex[1]._uv.x = (float)_rects[_number].right / _textures[0]->GetImageInfo().Width; vertex[1]._uv.y = (float)_rects[_number].bottom / _textures[0]->GetImageInfo().Height; vertex[2]._uv.x = (float)_rects[_number].left / _textures[0]->GetImageInfo().Width; vertex[2]._uv.y = (float)_rects[_number].top / _textures[0]->GetImageInfo().Height; vertex[3]._uv.x = (float)_rects[_number].right / _textures[0]->GetImageInfo().Width; vertex[3]._uv.y = (float)_rects[_number].top / _textures[0]->GetImageInfo().Height; _mesh->GetMesh()->UnlockIndexBuffer(); _previousNumber = _number; } // ワールド行列設定 Matrix SclMtx, RotMtx, PosMtx, WldMtx, WVPMtx; // 拡縮 D3DXMatrixScaling(&SclMtx, _scale.x, _scale.y, _scale.z); // 回転 // クォータニオンか回転行列かXYZ指定か this->Evaluate(); RotMtx = _worldRotationMatrix; // ビルボードの場合 if(_isBillBoard) { Vector3 cameraPosition = camera->GetEye() ; GetBillBoardRotation(&_position, &cameraPosition, &RotMtx); } // 位置 D3DXMatrixTranslation(&PosMtx, _position.x, _position.y, _position.z); // カリングを設定 GraphicsManager::_device->SetRenderState(D3DRS_CULLMODE, _cullingState); // デバッグ用 //GraphicsManager::_device->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME ); // シェーダを使用する場合カメラのビュー行列(0)、プロジェクション行列(1)をワールド行列に合成 WldMtx = SclMtx * RotMtx * PosMtx; WVPMtx = WldMtx * camera->GetMatrix(ViewBehavior::VIEW) * camera->GetMatrix(ViewBehavior::PROJECTION); // カメラの座標をシェーダに使用するための行列変換 Matrix CamMtx = WldMtx * camera->GetMatrix(ViewBehavior::VIEW); D3DXMatrixInverse(&CamMtx, NULL, &CamMtx); Vector4 EyePos = Vector4( camera->GetEye().x, camera->GetEye().y, camera->GetEye().z, 1 ); EyePos.Transform(CamMtx); D3DXVec4Normalize((D3DXVECTOR4*)&EyePos, (D3DXVECTOR4*)&EyePos); // シェーダ設定 _shader->SetTechnique(); // シェーダにワールド * ビュー * プロジェクション行列を渡す _shader->SetWVPMatrix(WVPMtx); // シェーダー特有の値の設定 _shader->ApplyEffect(RotMtx, EyePos); HRESULT hr; // 3D モデルのパーツ分ループして描画 for(size_t i = 0 ; i < _mesh->GetMaterialNumber(); i++) { // テクスチャが存在しない場合のカラー D3DXVECTOR4 vec4 = D3DXVECTOR4(1.0,1.0,1.0,1.0); // 格パーツに対応するテクスチャを設定 // シェーダにテクスチャを渡す if(NULL != _textures[i]) { LPDIRECT3DTEXTURE9 texture = _textures[i]->GetTextureData(); // シェーダにカラーを渡す _shader->SetColor(_colorRGBA); _shader->SetTexture(texture); }else _shader->SetColor(vec4); // シェーダの使用開始 _shader->BeginShader(); // シェーダのパス設定 _shader->BeginPass(_addsBlend); // パーツの描画 if(SUCCEEDED(GraphicsManager::_device->BeginScene())) { _mesh->GetMesh()->DrawSubset(i); V(GraphicsManager::_device->EndScene()); } // パス終了 _shader->EndPass(); // シェーダ終了 _shader->EndShader(); } }
void CIFControl::UpdateRotateBuffer( float fAngle ) { float xs, ys; float tx1, ty1, tx2, ty2; int id; int texIdx; int texSizeX; int texSizeY; int curIdx; DWORD color; float width= (float)m_info[0].sizeY * 0.5f; float x, y; vec3_t molt, molb, mort, morb; molt[0] = -width; molt[1] = -width; molt[2] = 0.0f; mort[0] = width; mort[1] = -width; mort[2] = 0.0f; morb[0] = width; morb[1] = width; morb[2] = 0.0f; molb[0] = -width; molb[1] = width; molb[2] = 0.0f; CalcZAxisRotate( fAngle , molt, mort ); CalcZAxisRotate( fAngle , molb, morb ); molt[0] += width; molt[1] += width; mort[0] += width; mort[1] += width; morb[0] += width; morb[1] += width; molb[0] += width; molb[1] += width; x = (float)m_info[0].clientX; y = (float)m_info[0].clientY; molt[0] += x; molt[1] += y; mort[0] += x; mort[1] += y; morb[0] += x; morb[1] += y; molb[0] += x; molb[1] += y; curIdx = m_current; texIdx = m_info[m_current].texIndex; id = m_info[curIdx].ID; if( texIdx >=0 ) { texSizeX = g_ifMng->m_texInfo[texIdx].texSizeX; texSizeY = g_ifMng->m_texInfo[texIdx].texSizeY; } else { texSizeX = 1; texSizeY = 1; } xs = (float)m_info[m_current].sizeX; ys = (float)m_info[m_current].sizeY; tx1 = (float)m_info[m_current].texCoordX; ty1 = (float)m_info[m_current].texCoordY; tx2 = (float)m_info[m_current].texCoordX + xs; ty2 = (float)m_info[m_current].texCoordY + ys; tx1 /= (float)texSizeX; ty1 /= (float)texSizeY; tx2 /= (float)texSizeX; ty2 /= (float)texSizeY; if( id != -1 ) { if( g_ifMng->m_iBackPos[id].clientX == IF_POSITION_FULL ) xs = (float)g_ifMng->m_clientWidth; if( g_ifMng->m_iBackPos[id].clientY == IF_POSITION_FULL ) ys = (float)g_ifMng->m_clientHeight; } IFTEXTUREVERTEX *pVertices = NULL; pVertices = (IFTEXTUREVERTEX *)m_vexbuf->Lock(); if( m_info[m_current].texIndex != -1 ) color = 0xffffffff; else color = 0xff000000; if( texIdx == -2 ) { color = 0xc0000000; } float tempzpos = 0.0f; float tempwpos = 1.0f; pVertices[0].position = D3DXVECTOR4( molb[0] - 0.5f, molb[1] - 0.5f, tempzpos, tempwpos ); pVertices[0].color = color; pVertices[0].tu = tx1; pVertices[0].tv = ty2; pVertices[1].position = D3DXVECTOR4( molt[0] - 0.5f, molt[1] - 0.5f, tempzpos, tempwpos ); pVertices[1].color = color; pVertices[1].tu = tx1; pVertices[1].tv = ty1; pVertices[2].position = D3DXVECTOR4( morb[0] - 0.5f, morb[1] - 0.5f, tempzpos, tempwpos ); pVertices[2].color = color; pVertices[2].tu = tx2; pVertices[2].tv = ty2; pVertices[3].position = D3DXVECTOR4( mort[0] - 0.5f, mort[1] - 0.5f, tempzpos, tempwpos ); pVertices[3].color = color; pVertices[3].tu = tx2; pVertices[3].tv = ty1; pVertices[4].position = D3DXVECTOR4( morb[0] - 0.5f, morb[1] - 0.5f, tempzpos, tempwpos ); pVertices[4].color = color; pVertices[4].tu = tx2; pVertices[4].tv = ty2; pVertices[5].position = D3DXVECTOR4( molt[0] - 0.5f, molt[1] - 0.5f, tempzpos, tempwpos ); pVertices[5].color = color; pVertices[5].tu = tx1; pVertices[5].tv = ty1; m_vexbuf->Unlock(); }
//************************************************************************************************************* void Render(float alpha, float elapsedtime) { static float time = 0; D3DXMATRIX view, proj, vp; D3DXMATRIX world; D3DXMATRIX inv; D3DXVECTOR4 amblight(0.2f, 0.2f, 0.2f, 1); D3DXVECTOR4 intensity(0.8f, 0.8f, 0.8f, 1); D3DXVECTOR4 zero(0, 0, 0, 1); D3DXVECTOR3 lightpos(0, 0, -10); D3DXVECTOR3 eye(0, 0, -5.2f); D3DXVECTOR3 look(0, 0.5f, 0); D3DXVECTOR3 up(0, 1, 0); D3DXVECTOR3 p1, p2; D3DXVECTOR2 orient = cameraangle.smooth(alpha); D3DXVECTOR2 light = lightangle.smooth(alpha); time += elapsedtime; // setup light D3DXMatrixRotationYawPitchRoll(&view, light.x, light.y, 0); D3DXVec3TransformCoord(&lightpos, &lightpos, &view); // TODO: no need to calculate every frame for( int i = 0; i < NUM_OBJECTS; ++i ) { FindSilhouette(objects[i], (D3DXVECTOR3&)lightpos); ExtrudeSilhouette(objects[i], (D3DXVECTOR3&)lightpos); } // setup camera D3DXMatrixRotationYawPitchRoll(&view, orient.x, orient.y, 0); D3DXVec3TransformCoord(&eye, &eye, &view); D3DXMatrixLookAtLH(&view, &eye, &look, &up); D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, (float)screenwidth / (float)screenheight, 0.1f, 20); // put far plane to infinity proj._33 = 1; proj._43 = -0.1f; D3DXMatrixMultiply(&vp, &view, &proj); D3DXMatrixScaling(&world, 5, 0.1f, 5); // specular effect uniforms specular->SetMatrix("matViewProj", &vp); specular->SetVector("eyePos", (D3DXVECTOR4*)&eye); specular->SetVector("lightPos", (D3DXVECTOR4*)&lightpos); specular->SetVector("ambient", &zero); // it's a f**k-up specular->SetVector("lightColor", &intensity); // lazy to tonemap ambient->SetMatrix("matViewProj", &vp); ambient->SetVector("ambient", &amblight); if( SUCCEEDED(device->BeginScene()) ) { device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0xff6694ed, 1.0f, 0); // STEP 1: z pass ambient->SetTechnique("ambientlight"); ambient->SetMatrix("matViewProj", &vp); DrawScene(ambient); // STEP 2: draw shadow with depth fail method device->SetRenderState(D3DRS_COLORWRITEENABLE, 0); device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); device->SetRenderState(D3DRS_STENCILENABLE, TRUE); device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP); device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP); device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_INCR); device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); extrude->SetTechnique("extrude"); extrude->SetMatrix("matViewProj", &vp); extrude->Begin(0, 0); extrude->BeginPass(0); { for( int i = 0; i < NUM_OBJECTS; ++i ) DrawShadowVolume(objects[i]); device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_DECR); device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); for( int i = 0; i < NUM_OBJECTS; ++i ) DrawShadowVolume(objects[i]); } extrude->EndPass(); extrude->End(); device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_ALPHA); // STEP 3: multipass lighting device->SetRenderState(D3DRS_ZENABLE, TRUE); device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); device->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP); device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_GREATER); device->SetRenderState(D3DRS_STENCILREF, 1); DrawScene(specular); device->SetRenderState(D3DRS_STENCILENABLE, FALSE); device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); if( drawsilhouette ) { amblight = D3DXVECTOR4(1, 1, 0, 0.5f); // reuse whatever we can... extrude->SetVector("ambient", &amblight); extrude->Begin(0, 0); extrude->BeginPass(0); device->SetVertexDeclaration(shadowdecl); for( int i = 0; i < NUM_OBJECTS; ++i ) { const ShadowCaster& caster = objects[i]; D3DXVECTOR4* verts = (D3DXVECTOR4*)malloc(caster.silhouette.size() * 2 * sizeof(D3DXVECTOR4)); for( size_t j = 0; j < caster.silhouette.size(); ++j ) { const Edge& e = caster.silhouette[j]; verts[j * 2 + 0] = D3DXVECTOR4(e.v1, 1); verts[j * 2 + 1] = D3DXVECTOR4(e.v2, 1); } extrude->SetMatrix("matWorld", &caster.world); extrude->CommitChanges(); device->DrawPrimitiveUP(D3DPT_LINELIST, caster.silhouette.size(), verts, sizeof(D3DXVECTOR4)); free(verts); } extrude->EndPass(); extrude->End(); extrude->SetVector("ambient", &zero); } if( drawvolume ) { device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); amblight = D3DXVECTOR4(1, 1, 0, 0.5f); extrude->SetVector("ambient", &amblight); extrude->Begin(0, 0); extrude->BeginPass(0); for( int i = 0; i < NUM_OBJECTS; ++i ) DrawShadowVolume(objects[i]); extrude->EndPass(); extrude->End(); extrude->SetVector("ambient", &zero); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); } // render text device->SetFVF(D3DFVF_XYZRHW|D3DFVF_TEX1); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); device->SetTexture(0, text); device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, textvertices, 6 * sizeof(float)); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_ZENABLE, TRUE); device->SetTexture(0, 0); device->EndScene(); } device->Present(NULL, NULL, NULL, NULL); }
void LightClass::SetDiffuseColor(float red, float green, float blue, float alpha) { m_diffuseColor = D3DXVECTOR4(red, green, blue, alpha); return; }
void object::render() { SetShader(); d3ddevice->SetVertexShaderConstant( 0, &D3DXVECTOR4(0,0,0,0), 1 ); d3ddevice->SetVertexShaderConstant( 1, &D3DXVECTOR4(1,1,1,1), 1 ); d3ddevice->SetVertexShaderConstant( 80, &D3DXVECTOR4(0.5f,0.5f,0.5f,0.5f), 1 ); D3DXMATRIX mat; D3DXMATRIX matworldview; D3DXMATRIX temp; D3DXVECTOR4 light_vec; //D3DXVECTOR4 eye_vec; D3DXMatrixIdentity(&matworld); D3DXMatrixTranslation(&temp,radius_of_rotation,0,0); D3DXMatrixMultiply( &matworld, &matworld, &temp ); D3DXMatrixRotationY(&temp,angle); D3DXMatrixMultiply( &matworld, &matworld, &temp ); D3DXMatrixTranslation(&temp,center_pos.x,center_pos.y,center_pos.z); D3DXMatrixMultiply( &matworld, &matworld, &temp ); D3DXMatrixMultiply( &matworldview, &matworld, &matView ); D3DXMatrixMultiply( &mat, &matworldview, &matProj ); D3DXMatrixTranspose( &mat, &mat ); d3ddevice->SetVertexShaderConstant( 2, &mat, 4 ); D3DXMatrixInverse(&temp,NULL,&matworld); light_vec.x = light_pos[0]; light_vec.y = light_pos[1]; light_vec.z = light_pos[2]; light_vec.w = 1; eye_pos.x = 0; eye_pos.y = 0; eye_pos.z = -15; eye_pos.w = 1; D3DXVec4Transform(&light_vec,&light_vec,&temp); D3DXVec4Transform(&eye_pos,&eye_pos,&temp); //D3DXVec3Normalize(&light_vec,&light_vec); //D3DXMatrixTranspose( &temp, &temp ); //d3ddevice->SetVertexShaderConstant( 6, &temp, 4 ); // Declare and define the constant vertex color. D3DXVECTOR4 diff_color; diff_color = color; diff_color *= diffuse_amount; diff_color.w = 1; d3ddevice->SetVertexShaderConstant( 10, &color, 1 ); d3ddevice->SetPixelShaderConstant( 2, &diff_color, 1 ); D3DXVECTOR4 spec_color; spec_color = color; spec_color += D3DXVECTOR4(1,1,1,1); spec_color *= 0.5; spec_color *= specular_amount; spec_color.w = 1; d3ddevice->SetPixelShaderConstant( 4, &spec_color, 1 ); d3ddevice->SetVertexShaderConstant( 11, &light_vec, 1 ); d3ddevice->SetVertexShaderConstant( 13, &eye_pos, 1 ); d3ddevice->SetVertexShaderConstant( 12, &ambient, 1 ); D3DXVECTOR4 amb_color; amb_color = diff_color+spec_color; amb_color *= 0.1f; //amb_color.x *= color[0]; //amb_color.y *= color[1]; //amb_color.z *= color[2]; amb_color.w = 1; d3ddevice->SetPixelShaderConstant( 3, &amb_color, 1 ); //d3ddevice->SetPixelShaderConstant( 5, &D3DXVECTOR4(0,0,0,0), 1 ); //set texture if(texture != NULL) d3ddevice->SetTexture(0,texture); if(power_texture != NULL) d3ddevice->SetTexture(1,power_texture); d3ddevice->SetStreamSource( 0, vertex_buffer, sizeof(CUSTOMVERTEX) ); d3ddevice->SetIndices(index_buffer,0); d3ddevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0,size, 0,size2); //mesh->DrawSubset(1); }
void D3DLight::SetDiffuseColor(float red, float green, float blue, float alpha) { diffuseColor = D3DXVECTOR4(red, green, blue, alpha); }
//************************************************************************************************************* void Render(float alpha, float elapsedtime) { D3DXMATRIX view, proj; D3DXMATRIX inv, vp, tmp; D3DXMATRIX lightview, lightproj, lightvp; D3DXVECTOR2 orient = cameraangle.smooth(alpha); D3DXVECTOR4 lightpos(0, 0, -5, 0); D3DXVECTOR3 look(0, 0.5f, 0), up(0, 1, 0); D3DXVECTOR3 eye(0, 0, -5.2f); D3DXVECTOR4 clipplanes(0, 0, 0, 0); D3DXVECTOR4 texelsize(1.0f / SHADOWMAP_SIZE, 1.0f / SHADOWMAP_SIZE, 0, 0); // setup camera D3DXMatrixRotationYawPitchRoll(&tmp, orient.x, orient.y, 0); D3DXVec3TransformCoord(&eye, &eye, &tmp); D3DXMatrixLookAtLH(&view, &eye, &look, &up); D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, (float)screenwidth / (float)screenheight, 0.1f, 20); D3DXMatrixMultiply(&vp, &view, &proj); // setup light orient = lightangle.smooth(alpha); look = D3DXVECTOR3(0, 0, 0); D3DXMatrixRotationYawPitchRoll(&tmp, orient.x, orient.y, 0); D3DXVec4Transform(&lightpos, &lightpos, &tmp); D3DXMatrixLookAtLH(&lightview, (D3DXVECTOR3*)&lightpos, &look, &up); DXFitToBox(lightproj, clipplanes, lightview, scenebb); D3DXMatrixMultiply(&lightvp, &lightview, &lightproj); if( SUCCEEDED(device->BeginScene()) ) { switch( shadowtech ) { case 0: RenderWithPCF(vp, eye, lightview, lightproj, lightpos, clipplanes, D3DXVECTOR4(0, 0, 0, 0)); break; case 1: RenderWithPCF(vp, eye, lightview, lightproj, lightpos, clipplanes, texelsize); break; case 2: RenderWithIrregularPCF(vp, eye, lightview, lightproj, lightpos, clipplanes, texelsize); break; case 3: RenderWithVariance(vp, eye, lightview, lightproj, lightpos, clipplanes); break; case 4: RenderWithConvolution(vp, eye, lightview, lightproj, lightpos, clipplanes); break; case 5: RenderWithExponential(vp, eye, lightview, lightproj, lightpos, clipplanes); break; case 6: RenderWithExponentialVariance(vp, eye, lightview, lightproj, lightpos, clipplanes); break; case 7: RenderWithPCSS(vp, eye, lightview, lightproj, lightpos, clipplanes, texelsize); break; default: device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); break; } // render text device->SetFVF(D3DFVF_XYZRHW|D3DFVF_TEX1); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); device->SetTexture(0, text); device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, textvertices, 6 * sizeof(float)); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_ZENABLE, TRUE); device->EndScene(); } device->Present(NULL, NULL, NULL, NULL); }
bool TerrainClass::InitializeBuffers(ID3D11Device* device) { VertexType* vertices; unsigned long* indices; int index, i, j; float positionX, positionZ; D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc; D3D11_SUBRESOURCE_DATA vertexData, indexData; HRESULT result; // Calculate the number of vertices in the terrain mesh. m_vertexCount = (m_terrainWidth - 1) * (m_terrainHeight - 1) * 8; // Set the index count to the same as the vertex count. m_indexCount = m_vertexCount; // Create the vertex array. vertices = new VertexType[m_vertexCount]; if(!vertices) { return false; } // Create the index array. indices = new unsigned long[m_indexCount]; if(!indices) { return false; } // Initialize the index to the vertex array. index = 0; // Load the vertex and index arrays with the terrain data. for(j=0; j<(m_terrainHeight-1); j++) { for(i=0; i<(m_terrainWidth-1); i++) { // LINE 1 // Upper left. positionX = (float)i; positionZ = (float)(j+1); vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ); vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // Upper right. positionX = (float)(i+1); positionZ = (float)(j+1); vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ); vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // LINE 2 // Upper right. positionX = (float)(i+1); positionZ = (float)(j+1); vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ); vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // Bottom right. positionX = (float)(i+1); positionZ = (float)j; vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ); vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // LINE 3 // Bottom right. positionX = (float)(i+1); positionZ = (float)j; vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ); vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // Bottom left. positionX = (float)i; positionZ = (float)j; vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ); vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // LINE 4 // Bottom left. positionX = (float)i; positionZ = (float)j; vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ); vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; // Upper left. positionX = (float)i; positionZ = (float)(j+1); vertices[index].position = D3DXVECTOR3(positionX, 0.0f, positionZ); vertices[index].color = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f); indices[index] = index; index++; } } // Set up the description of the static vertex buffer. vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT; vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount; vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vertexBufferDesc.CPUAccessFlags = 0; vertexBufferDesc.MiscFlags = 0; vertexBufferDesc.StructureByteStride = 0; // Give the subresource structure a pointer to the vertex data. vertexData.pSysMem = vertices; vertexData.SysMemPitch = 0; vertexData.SysMemSlicePitch = 0; // Now create the vertex buffer. result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer); if(FAILED(result)) { return false; } // Set up the description of the static index buffer. indexBufferDesc.Usage = D3D11_USAGE_DEFAULT; indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount; indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; indexBufferDesc.CPUAccessFlags = 0; indexBufferDesc.MiscFlags = 0; indexBufferDesc.StructureByteStride = 0; // Give the subresource structure a pointer to the index data. indexData.pSysMem = indices; indexData.SysMemPitch = 0; indexData.SysMemSlicePitch = 0; // Create the index buffer. result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer); if(FAILED(result)) { return false; } // Release the arrays now that the buffers have been created and loaded. delete [] vertices; vertices = 0; delete [] indices; indices = 0; return true; }
void noisemaker::render(const D3DXMATRIXA16 *mProjector) { this->mProjector = *mProjector; if (RenderToSurface->BeginScene(rendered_texture_surface,NULL)) { DWORD itime = timeGetTime() & ((1<<23) - 1); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); device->Clear(0, NULL, D3DCLEAR_TARGET , D3DCOLOR_XRGB(0,0,0), 1.0f, 0 ); anim_effect->Begin(NULL,NULL); anim_effect->Pass(0); double r_scale = prm->params[p_fScale].fData, r_timemulti = prm->params[p_fAnimspeed].fData, r_strength = 1.0f, r_falloff = prm->params[p_fFalloff].fData; // normalize r_strength float sum=0; for(int i=0; i<octaves; i++) { sum += r_strength; r_strength *= r_falloff; } r_strength = 1.0f / sum; for(i=0; i<octaves; i++) { unsigned int image[3]; float amount[3]; double dImage, fraction = modf((double)itime*r_timemulti,&dImage); int iImage = (int)dImage; amount[0] = pow(sin((fraction+2)*PI/3),2)/1.5; amount[1] = pow(sin((fraction+1)*PI/3),2)/1.5; amount[2] = pow(sin((fraction)*PI/3),2)/1.5; image[0] = (iImage) & (animlength-1); image[1] = (iImage+1) & (animlength-1); image[2] = (iImage+2) & (animlength-1); anim_effect->SetFloat("a",amount[0]); anim_effect->SetFloat("b",amount[1]); anim_effect->SetFloat("c",amount[2]); anim_effect->SetFloat("scale",r_scale); anim_effect->SetFloat("strength",r_strength); anim_effect->SetVector("tc_offset_a", &D3DXVECTOR4(offsets[image[0]].x,offsets[image[0]].y,0,1)); anim_effect->SetVector("tc_offset_b", &D3DXVECTOR4(offsets[image[1]].x,offsets[image[1]].y,0,1)); anim_effect->SetVector("tc_offset_c", &D3DXVECTOR4(offsets[image[2]].x,offsets[image[2]].y,0,1)); anim_effect->SetTexture("NoiseTex",source_texture); anim_effect->SetMatrix("mProjector",mProjector); device->SetStreamSource( 0, VB, 0, sizeof(NOISEVERTEX) ); device->SetFVF( D3DFVF_NOISEVERTEX ); device->SetIndices( IB ); device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, (patches_x+1)*(patches_y+1), 0, 2*patches_x*patches_y-1 ); r_scale *= 2; r_timemulti *= 1.5; r_strength *= r_falloff; } RenderToSurface->EndScene(0); device->SetTexture(0,NULL); device->SetTexture(1,NULL); device->SetTexture(2,NULL); anim_effect->End(); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO); } }
bool ParticleSystemClass::UpdateBuffers(ID3D11DeviceContext* deviceContext) { int index, i; HRESULT result; D3D11_MAPPED_SUBRESOURCE mappedResource; VertexType* verticesPtr; // Initialize vertex array to zeros at first. memset(m_vertices, 0, (sizeof(VertexType)* m_vertexCount)); // Now build the vertex array from the particle list array. Each particle is a quad made out of two triangles. index = 0; for (i = 0; i<m_currentParticleCount; i++) { // Bottom left. m_vertices[index].position = D3DXVECTOR3(m_particleList[i].positionX - m_particleSize, m_particleList[i].positionY - m_particleSize, m_particleList[i].positionZ); m_vertices[index].texture = D3DXVECTOR2(0.0f, 1.0f); m_vertices[index].color = D3DXVECTOR4(m_particleList[i].red, m_particleList[i].green, m_particleList[i].blue, 1.0f); index++; // Top left. m_vertices[index].position = D3DXVECTOR3(m_particleList[i].positionX - m_particleSize, m_particleList[i].positionY + m_particleSize, m_particleList[i].positionZ); m_vertices[index].texture = D3DXVECTOR2(0.0f, 0.0f); m_vertices[index].color = D3DXVECTOR4(m_particleList[i].red, m_particleList[i].green, m_particleList[i].blue, 1.0f); index++; // Bottom right. m_vertices[index].position = D3DXVECTOR3(m_particleList[i].positionX + m_particleSize, m_particleList[i].positionY - m_particleSize, m_particleList[i].positionZ); m_vertices[index].texture = D3DXVECTOR2(1.0f, 1.0f); m_vertices[index].color = D3DXVECTOR4(m_particleList[i].red, m_particleList[i].green, m_particleList[i].blue, 1.0f); index++; // Bottom right. m_vertices[index].position = D3DXVECTOR3(m_particleList[i].positionX + m_particleSize, m_particleList[i].positionY - m_particleSize, m_particleList[i].positionZ); m_vertices[index].texture = D3DXVECTOR2(1.0f, 1.0f); m_vertices[index].color = D3DXVECTOR4(m_particleList[i].red, m_particleList[i].green, m_particleList[i].blue, 1.0f); index++; // Top left. m_vertices[index].position = D3DXVECTOR3(m_particleList[i].positionX - m_particleSize, m_particleList[i].positionY + m_particleSize, m_particleList[i].positionZ); m_vertices[index].texture = D3DXVECTOR2(0.0f, 0.0f); m_vertices[index].color = D3DXVECTOR4(m_particleList[i].red, m_particleList[i].green, m_particleList[i].blue, 1.0f); index++; // Top right. m_vertices[index].position = D3DXVECTOR3(m_particleList[i].positionX + m_particleSize, m_particleList[i].positionY + m_particleSize, m_particleList[i].positionZ); m_vertices[index].texture = D3DXVECTOR2(1.0f, 0.0f); m_vertices[index].color = D3DXVECTOR4(m_particleList[i].red, m_particleList[i].green, m_particleList[i].blue, 1.0f); index++; } // Lock the vertex buffer. result = deviceContext->Map(m_vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { return false; } // Get a pointer to the data in the vertex buffer. verticesPtr = (VertexType*)mappedResource.pData; // Copy the data into the vertex buffer. memcpy(verticesPtr, (void*)m_vertices, (sizeof(VertexType)* m_vertexCount)); // Unlock the vertex buffer. deviceContext->Unmap(m_vertexBuffer, 0); return true; }
void CIFControl::UpdateBuffer( int zx, int zy ) { float x, y, xs, ys; float tx1, ty1, tx2, ty2; int id; int texIdx; int texSizeX; int texSizeY; int curIdx; DWORD color; curIdx = m_current; texIdx = m_info[m_current].texIndex; id = m_info[curIdx].ID; if( texIdx >=0 ) { texSizeX = g_ifMng->m_texInfo[texIdx].texSizeX; texSizeY = g_ifMng->m_texInfo[texIdx].texSizeY; } else { texSizeX = 1; texSizeY = 1; } x = (float)m_info[curIdx].clientX; y = (float)m_info[curIdx].clientY; xs = (float)m_info[m_current].sizeX; ys = (float)m_info[m_current].sizeY; tx1 = (float)m_info[m_current].texCoordX; ty1 = (float)m_info[m_current].texCoordY; tx2 = (float)m_info[m_current].texCoordX + xs; ty2 = (float)m_info[m_current].texCoordY + ys; tx1 /= (float)texSizeX; ty1 /= (float)texSizeY; tx2 /= (float)texSizeX; ty2 /= (float)texSizeY; xs = zx; ys = zy; IFTEXTUREVERTEX *pVertices = NULL; pVertices = (IFTEXTUREVERTEX *)m_vexbuf->Lock(); if( pVertices == NULL ) return; if( !m_fadeState ) { if( m_info[m_current].texIndex != -1 ) color = 0xffffffff; else color = 0xff000000; } else { color = m_fadeColor; } if( texIdx == -2 ) { color = 0xc0000000; } float tempzpos = 0.0f; float tempwpos = 1.0f; pVertices[0].position = D3DXVECTOR4( x - 0.5f, y + ys - 0.5f, tempzpos, tempwpos ); pVertices[0].color = color; pVertices[0].tu = tx1; pVertices[0].tv = ty2; pVertices[1].position = D3DXVECTOR4( x - 0.5f, y - 0.5f, tempzpos, tempwpos ); pVertices[1].color = color; pVertices[1].tu = tx1; pVertices[1].tv = ty1; pVertices[2].position = D3DXVECTOR4( x + xs - 0.5f, y + ys - 0.5f, tempzpos, tempwpos ); pVertices[2].color = color; pVertices[2].tu = tx2; pVertices[2].tv = ty2; pVertices[3].position = D3DXVECTOR4( x + xs - 0.5f, y - 0.5f, tempzpos, tempwpos ); pVertices[3].color = color; pVertices[3].tu = tx2; pVertices[3].tv = ty1; pVertices[4].position = D3DXVECTOR4( x + xs - 0.5f, y + ys - 0.5f, tempzpos, tempwpos ); pVertices[4].color = color; pVertices[4].tu = tx2; pVertices[4].tv = ty2; pVertices[5].position = D3DXVECTOR4( x - 0.5f, y - 0.5f, tempzpos, tempwpos ); pVertices[5].color = color; pVertices[5].tu = tx1; pVertices[5].tv = ty1; m_vexbuf->Unlock(); }
HRESULT CD3DFont::DrawText( FLOAT sx, FLOAT sy, DWORD dwColor, const TCHAR* strText, DWORD dwFlags ) { if( m_pd3dDevice == NULL ) return E_FAIL; // Setup renderstate m_pStateBlockSaved->Capture(); m_pStateBlockDrawText->Apply(); m_pd3dDevice->SetFVF( D3DFVF_FONT2DVERTEX ); m_pd3dDevice->SetPixelShader( NULL ); m_pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof(FONT2DVERTEX) ); // Set filter states if( dwFlags & D3DFONT_FILTERED ) { m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); } // Center the text block in the viewport if( dwFlags & D3DFONT_CENTERED_X ) { D3DVIEWPORT9 vp; m_pd3dDevice->GetViewport( &vp ); const TCHAR* strTextTmp = strText; float xFinal = 0.0f; while( *strTextTmp ) { TCHAR c = *strTextTmp++; if( c == _T('\n') ) break; // Isn't supported. if( (c-32) < 0 || (c-32) >= 128-32 ) continue; FLOAT tx1 = m_fTexCoords[c-32][0]; FLOAT tx2 = m_fTexCoords[c-32][2]; FLOAT w = (tx2-tx1) * m_dwTexWidth / m_fTextScale; xFinal += w - (2 * m_dwSpacing); } sx = (vp.Width-xFinal)/2.0f; } if( dwFlags & D3DFONT_CENTERED_Y ) { D3DVIEWPORT9 vp; m_pd3dDevice->GetViewport( &vp ); float fLineHeight = ((m_fTexCoords[0][3]-m_fTexCoords[0][1])*m_dwTexHeight); sy = (vp.Height-fLineHeight)/2; } // Adjust for character spacing sx -= m_dwSpacing; FLOAT fStartX = sx; // Fill vertex buffer FONT2DVERTEX* pVertices = NULL; DWORD dwNumTriangles = 0; m_pVB->Lock( 0, 0, (void**)&pVertices, D3DLOCK_DISCARD ); while( *strText ) { TCHAR c = *strText++; if( c == _T('\n') ) { sx = fStartX; sy += (m_fTexCoords[0][3]-m_fTexCoords[0][1])*m_dwTexHeight; } if( (c-32) < 0 || (c-32) >= 128-32 ) continue; FLOAT tx1 = m_fTexCoords[c-32][0]; FLOAT ty1 = m_fTexCoords[c-32][1]; FLOAT tx2 = m_fTexCoords[c-32][2]; FLOAT ty2 = m_fTexCoords[c-32][3]; FLOAT w = (tx2-tx1) * m_dwTexWidth / m_fTextScale; FLOAT h = (ty2-ty1) * m_dwTexHeight / m_fTextScale; if( c != _T(' ') ) { *pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+h-0.5f,0.9f,1.0f), dwColor, tx1, ty2 ); *pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+0-0.5f,0.9f,1.0f), dwColor, tx1, ty1 ); *pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+h-0.5f,0.9f,1.0f), dwColor, tx2, ty2 ); *pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+0-0.5f,0.9f,1.0f), dwColor, tx2, ty1 ); *pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+h-0.5f,0.9f,1.0f), dwColor, tx2, ty2 ); *pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+0-0.5f,0.9f,1.0f), dwColor, tx1, ty1 ); dwNumTriangles += 2; if( dwNumTriangles*3 > (MAX_NUM_VERTICES-6) ) { // Unlock, render, and relock the vertex buffer m_pVB->Unlock(); m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, dwNumTriangles ); pVertices = NULL; m_pVB->Lock( 0, 0, (void**)&pVertices, D3DLOCK_DISCARD ); dwNumTriangles = 0L; } } sx += w - (2 * m_dwSpacing); } // Unlock and render the vertex buffer m_pVB->Unlock(); if( dwNumTriangles > 0 ) m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, dwNumTriangles ); // Restore the modified renderstates m_pStateBlockSaved->Apply(); return S_OK; }
void CIFControl::Update3DBuffer( float z, float w ) { float x, y, xs, ys; float tx1, ty1, tx2, ty2; int id; int texIdx; int texSizeX; int texSizeY; int curIdx; DWORD color; curIdx = m_current; texIdx = m_info[m_current].texIndex; id = m_info[curIdx].ID; if( texIdx >=0 ) { texSizeX = g_ifMng->m_texInfo[texIdx].texSizeX; texSizeY = g_ifMng->m_texInfo[texIdx].texSizeY; } else { texSizeX = 1; texSizeY = 1; } x = (float)m_info[curIdx].clientX; y = (float)m_info[curIdx].clientY; xs = (float)m_info[m_current].sizeX; ys = (float)m_info[m_current].sizeY; tx1 = (float)m_info[m_current].texCoordX; ty1 = (float)m_info[m_current].texCoordY; tx2 = (float)m_info[m_current].texCoordX + xs; ty2 = (float)m_info[m_current].texCoordY + ys; tx1 /= (float)texSizeX; ty1 /= (float)texSizeY; tx2 /= (float)texSizeX; ty2 /= (float)texSizeY; if( id != -1 ) { if( g_ifMng->m_iBackPos[id].clientX == IF_POSITION_FULL ) xs = (float)g_ifMng->m_clientWidth; if( g_ifMng->m_iBackPos[id].clientY == IF_POSITION_FULL ) ys = (float)g_ifMng->m_clientHeight; } IFTEXTUREVERTEX *pVertices = NULL; pVertices = (IFTEXTUREVERTEX *)m_vexbuf->Lock(); if( !m_fadeState ) { if( m_info[m_current].texIndex != -1 ) color = 0xffffffff; else color = 0xff000000; } else { color = m_fadeColor; } if( texIdx == -2 ) { color = 0xc0000000; } float tempzpos = z; float tempwpos = w; pVertices[0].position = D3DXVECTOR4( x - 0.5f, y + ys - 0.5f, tempzpos, tempwpos ); pVertices[0].color = color; pVertices[0].tu = tx1; pVertices[0].tv = ty2; pVertices[1].position = D3DXVECTOR4( x - 0.5f, y - 0.5f, tempzpos, tempwpos ); pVertices[1].color = color; pVertices[1].tu = tx1; pVertices[1].tv = ty1; pVertices[2].position = D3DXVECTOR4( x + xs - 0.5f, y + ys - 0.5f, tempzpos, tempwpos ); pVertices[2].color = color; pVertices[2].tu = tx2; pVertices[2].tv = ty2; pVertices[3].position = D3DXVECTOR4( x + xs - 0.5f, y - 0.5f, tempzpos, tempwpos ); pVertices[3].color = color; pVertices[3].tu = tx2; pVertices[3].tv = ty1; pVertices[4].position = D3DXVECTOR4( x + xs - 0.5f, y + ys - 0.5f, tempzpos, tempwpos ); pVertices[4].color = color; pVertices[4].tu = tx2; pVertices[4].tv = ty2; pVertices[5].position = D3DXVECTOR4( x - 0.5f, y - 0.5f, tempzpos, tempwpos ); pVertices[5].color = color; pVertices[5].tu = tx1; pVertices[5].tv = ty1; m_vexbuf->Unlock(); }
void ShadingDemo::Init(ID3D11Device* p_pDevice, ID3D11DeviceContext* p_pDevCon) { m_pDevice = p_pDevice; m_pDevCon = p_pDevCon; m_pTexture_Dif = new Texture("rocks_dif.jpg", p_pDevice); m_pTexture_Nrm = new Texture("rocks_nrm.dds", p_pDevice); IndexCount = (100 * 100 * 6); // ------------------------------- LightData -------------------------------------------------- m_LightingData.AmbientLightColor = D3DXVECTOR4(0.1f, 0.1f, 0.1f, 0); m_LightingData.DirectionalLightColor = D3DXVECTOR4(255, 246, 145, 0) / 255.0f; m_LightingData.DirectionalLightDir = D3DXVECTOR4(0, -0.7f, -0.7f, 0); m_LightingData.PointLightColor = D3DXVECTOR4(1, 0, 0, 0); m_LightingData.PointLightPos = D3DXVECTOR3(5, 6, 5); m_LightingData.PointLightRange = 10; m_LightingData.PointLightColor2 = D3DXVECTOR4(0, 0, 1, 0); m_LightingData.PointLightPos2 = D3DXVECTOR3(5, 6, 5); m_LightingData.PointLightRange2 = 10; // ----------------------------- VertexBuffer ------------------------------------------------- D3D11_BUFFER_DESC _VBufferDesc; ZeroMemory(&_VBufferDesc, sizeof(_VBufferDesc)); _VBufferDesc.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_VERTEX_BUFFER; _VBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_FLAG::D3D11_CPU_ACCESS_WRITE; _VBufferDesc.Usage = D3D11_USAGE::D3D11_USAGE_DYNAMIC; _VBufferDesc.ByteWidth = ((100 + 1) * (100 + 1)) * sizeof(ShadingDemo_Vertex); // Anzahl an Objekten (In diesem Fall Vertices) * Größe von eínem Objekt m_pDevice->CreateBuffer(&_VBufferDesc, nullptr, &m_pVertexBuffer); ShadingDemo_Vertex* _Array = new ShadingDemo_Vertex[(100 + 1) * (100 + 1)]; //int i = 0; // Mit 2 Forschleifen durch die Plane wandern und Vertices erstellen for (int y = 0; y <= 100; y++) { for (int x = 0; x <= 100; x++) { D3DXVECTOR3 Position = D3DXVECTOR3(x / 10.0f, GetHeightAt(x / 10.0f, y / 10.0f), y / 10.0f); D3DXVECTOR3 Position0p = D3DXVECTOR3(x / 10.0f, GetHeightAt(x / 10.0f, (y + 1) / 10.0f), (y + 1) / 10.0f); D3DXVECTOR3 Position0n = D3DXVECTOR3(x / 10.0f, GetHeightAt(x / 10.0f, (y - 1) / 10.0f), (y - 1) / 10.0f); D3DXVECTOR3 Positionn0 = D3DXVECTOR3((x - 1) / 10.0f, GetHeightAt((x - 1) / 10.0f, y / 10.0f), y / 10.0f); D3DXVECTOR3 Positionp0 = D3DXVECTOR3((x + 1) / 10.0f, GetHeightAt((x + 1) / 10.0f, y / 10.0f), y / 10.0f); D3DXVECTOR3 Dir0n = Position0n - Position; D3DXVECTOR3 Dir0p = Position0p - Position; D3DXVECTOR3 Dirn0 = Positionn0 - Position; D3DXVECTOR3 Dirp0 = Positionp0 - Position; D3DXVec3Normalize(&Dir0n, &Dir0n); D3DXVec3Normalize(&Dir0p, &Dir0p); D3DXVec3Normalize(&Dirn0, &Dirn0); D3DXVec3Normalize(&Dirp0, &Dirp0); D3DXVECTOR3 Normal = D3DXVECTOR3(0, 0, 0); D3DXVECTOR3 Temp; D3DXVec3Cross(&Temp, &Dirp0, &Dir0n); Normal += Temp; D3DXVec3Cross(&Temp, &Dir0p, &Dirp0); Normal += Temp; D3DXVec3Cross(&Temp, &Dirn0, &Dir0p); Normal += Temp; D3DXVec3Cross(&Temp, &Dir0n, &Dirn0); Normal += Temp; D3DXVec3Normalize(&(_Array[y * (100 + 1) + x].m_Normal), &Normal); //1. Tangente in eine RIchtung setzen _Array[y * (100 + 1) + x].m_Tangent = D3DXVECTOR3(1, 0, 0); //2. Anhand der Tangenten die BiTangente Berechnen D3DXVec3Cross(&(_Array[y * (100 + 1) + x].m_BiTangent), &(_Array[y * (100 + 1) + x].m_Normal), &(_Array[y * (100 + 1) + x].m_Tangent)); //3. Tangente neuberechnen mithilfe von BiTangent und Normal D3DXVec3Cross(&(_Array[y * (100 + 1) + x].m_Tangent), &(_Array[y * (100 + 1) + x].m_BiTangent), &(_Array[y * (100 + 1) + x].m_Normal)); _Array[y * (100 + 1) + x].m_Color = D3DXVECTOR4(1, 1, 1, 1); _Array[y * (100 + 1) + x].m_Position = Position; _Array[y * (100 + 1) + x].m_UV = D3DXVECTOR2(x / 100.0f, y / 100.0f); } } //int i = 0; //for (int y = 0; y <= p_YSize; y++) //{ // for (int x = 0; x <= p_XSize; x++) // { // _Array[i].m_Color = D3DXVECTOR4(1, 1, 1, 1); // _Array[i].m_Position = D3DXVECTOR3(x, 0, y); // i++; // } //} D3D11_MAPPED_SUBRESOURCE _VertexBufferMapped; m_pDevCon->Map(m_pVertexBuffer, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &_VertexBufferMapped); memcpy(_VertexBufferMapped.pData, _Array, ((100 + 1) * (100 + 1)) * sizeof(ShadingDemo_Vertex)); m_pDevCon->Unmap(m_pVertexBuffer, 0); // ------------------------------------ IndexBuffer ----------------------------------------------- D3D11_BUFFER_DESC _IndexBufferDesc; ZeroMemory(&_IndexBufferDesc, sizeof(_IndexBufferDesc)); _IndexBufferDesc.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_INDEX_BUFFER; _IndexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_FLAG::D3D11_CPU_ACCESS_WRITE; _IndexBufferDesc.Usage = D3D11_USAGE::D3D11_USAGE_DYNAMIC; _IndexBufferDesc.ByteWidth = (100 * 100 * 6)*sizeof(DWORD); m_pDevice->CreateBuffer(&_IndexBufferDesc, nullptr, &m_pIndexBuffer); DWORD* _Indices = new DWORD[100 * 100 * 6]; for (int y = 0; y < 100 ; y++) { for (int x = 0; x < 100; x++) { int _CurrentQuad = 100 * y + x; int UntenLinks = y * (100 + 1) + x; int UntenRechts = UntenLinks + 1; int ObenLinks = UntenLinks + (100 + 1); int ObenRechts = ObenLinks + 1; _Indices[_CurrentQuad * 6 + 0] = UntenLinks; _Indices[_CurrentQuad * 6 + 1] = ObenLinks; _Indices[_CurrentQuad * 6 + 2] = UntenRechts; _Indices[_CurrentQuad * 6 + 3] = UntenRechts; _Indices[_CurrentQuad * 6 + 4] = ObenLinks; _Indices[_CurrentQuad * 6 + 5] = ObenRechts; } } D3D11_MAPPED_SUBRESOURCE _IndexBufferMapped; m_pDevCon->Map(m_pIndexBuffer, 0, D3D11_MAP::D3D11_MAP_WRITE_DISCARD, 0, &_IndexBufferMapped); memcpy(_IndexBufferMapped.pData, _Indices, (100 * 100* 6)*sizeof(DWORD)); m_pDevCon->Unmap(m_pIndexBuffer, 0); // ----------------------- Shader ---------------------------------------------------------------------------------- ID3D10Blob* _pError; ID3D10Blob* _pVertexShader; HRESULT HRES = D3DX11CompileFromFile(TEXT("ShadingDemo.hlsl"), nullptr, nullptr, "VShader", "vs_5_0", 0, 0, nullptr, &_pVertexShader, &_pError, nullptr); if (HRES != S_OK) { // Fehlerbehandlung MessageBox(0, (char*)_pError->GetBufferPointer(), "Fehler im VertexShader", MB_OK); PostQuitMessage(0); return; } m_pDevice->CreateVertexShader(_pVertexShader->GetBufferPointer(), _pVertexShader->GetBufferSize(), nullptr, &m_pVertexShader); // ------------------------------------- PixelShader -------------------------------------------------- ID3D10Blob* _pPixelShader; HRES = D3DX11CompileFromFile(TEXT("ShadingDemo.hlsl"), nullptr, nullptr, "PShader", "ps_5_0", 0, 0, nullptr, &_pPixelShader, &_pError, nullptr); if (HRES != S_OK) { // Fehlerbehandlung MessageBox(0, (char*)_pError->GetBufferPointer(), "Fehler im PixelShader", MB_OK); PostQuitMessage(0); return; } m_pDevice->CreatePixelShader(_pPixelShader->GetBufferPointer(), _pPixelShader->GetBufferSize(), nullptr, &m_pPixelShader); // ----------------------------------------- InputLayout ----------------------------------------------------------- D3D11_INPUT_ELEMENT_DESC _IED[6]; _IED[0].AlignedByteOffset = 0; _IED[0].Format = DXGI_FORMAT::DXGI_FORMAT_R32G32B32_FLOAT; _IED[0].InputSlot = 0; _IED[0].InputSlotClass = D3D11_INPUT_CLASSIFICATION::D3D11_INPUT_PER_VERTEX_DATA; _IED[0].InstanceDataStepRate = 0; _IED[0].SemanticName = "POSITION"; _IED[0].SemanticIndex = 0; _IED[1].AlignedByteOffset = 12; _IED[1].Format = DXGI_FORMAT::DXGI_FORMAT_R32G32B32A32_FLOAT; _IED[1].InputSlot = 0; _IED[1].InputSlotClass = D3D11_INPUT_CLASSIFICATION::D3D11_INPUT_PER_VERTEX_DATA; _IED[1].InstanceDataStepRate = 0; _IED[1].SemanticName = "COLOR"; _IED[1].SemanticIndex = 0; _IED[2].AlignedByteOffset = 28; _IED[2].Format = DXGI_FORMAT::DXGI_FORMAT_R32G32_FLOAT; _IED[2].InputSlot = 0; _IED[2].InputSlotClass = D3D11_INPUT_CLASSIFICATION::D3D11_INPUT_PER_VERTEX_DATA; _IED[2].InstanceDataStepRate = 0; _IED[2].SemanticName = "TEXCOORD"; _IED[2].SemanticIndex = 0; _IED[3].AlignedByteOffset = 36; _IED[3].Format = DXGI_FORMAT::DXGI_FORMAT_R32G32B32_FLOAT; _IED[3].InputSlot = 0; _IED[3].InputSlotClass = D3D11_INPUT_CLASSIFICATION::D3D11_INPUT_PER_VERTEX_DATA; _IED[3].InstanceDataStepRate = 0; _IED[3].SemanticName = "NORMAL"; _IED[3].SemanticIndex = 0; _IED[4].AlignedByteOffset = 48; _IED[4].Format = DXGI_FORMAT::DXGI_FORMAT_R32G32B32_FLOAT; _IED[4].InputSlot = 0; _IED[4].InputSlotClass = D3D11_INPUT_CLASSIFICATION::D3D11_INPUT_PER_VERTEX_DATA; _IED[4].InstanceDataStepRate = 0; _IED[4].SemanticName = "NORMAL"; _IED[4].SemanticIndex = 1; _IED[5].AlignedByteOffset = 60; _IED[5].Format = DXGI_FORMAT::DXGI_FORMAT_R32G32B32_FLOAT; _IED[5].InputSlot = 0; _IED[5].InputSlotClass = D3D11_INPUT_CLASSIFICATION::D3D11_INPUT_PER_VERTEX_DATA; _IED[5].InstanceDataStepRate = 0; _IED[5].SemanticName = "NORMAL"; _IED[5].SemanticIndex = 2; m_pDevice->CreateInputLayout(_IED, 6, _pVertexShader->GetBufferPointer(), _pVertexShader->GetBufferSize(), &m_pInputLayout); // ------------------------------------- Constant Buffer -------------------------------------------------------------------- D3D11_BUFFER_DESC _MatrixBufferDesc; ZeroMemory(&_MatrixBufferDesc, sizeof(_MatrixBufferDesc)); _MatrixBufferDesc.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_CONSTANT_BUFFER; _MatrixBufferDesc.ByteWidth = sizeof(ShadingDemo_MatrixBuffer); _MatrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_FLAG::D3D11_CPU_ACCESS_WRITE; _MatrixBufferDesc.Usage = D3D11_USAGE::D3D11_USAGE_DYNAMIC; m_pDevice->CreateBuffer(&_MatrixBufferDesc, nullptr, &m_pMatrixConstantBuffer); D3D11_BUFFER_DESC _PixelBufferDesc; ZeroMemory(&_PixelBufferDesc, sizeof(_PixelBufferDesc)); _PixelBufferDesc.BindFlags = D3D11_BIND_FLAG::D3D11_BIND_CONSTANT_BUFFER; _PixelBufferDesc.ByteWidth = sizeof(ShadingDemo_LightingBuffer); _PixelBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_FLAG::D3D11_CPU_ACCESS_WRITE; _PixelBufferDesc.Usage = D3D11_USAGE::D3D11_USAGE_DYNAMIC; m_pDevice->CreateBuffer(&_PixelBufferDesc, nullptr, &m_pPixelConstantBuffer); // ----------------------------------------- Sampler State ------------------------------------------------------------------ D3D11_SAMPLER_DESC _SamplerStateDesc; ZeroMemory(&_SamplerStateDesc, sizeof(_SamplerStateDesc)); _SamplerStateDesc.AddressU = D3D11_TEXTURE_ADDRESS_MODE::D3D11_TEXTURE_ADDRESS_WRAP; _SamplerStateDesc.AddressV = D3D11_TEXTURE_ADDRESS_MODE::D3D11_TEXTURE_ADDRESS_WRAP; _SamplerStateDesc.AddressW = D3D11_TEXTURE_ADDRESS_MODE::D3D11_TEXTURE_ADDRESS_CLAMP; _SamplerStateDesc.Filter = D3D11_FILTER::D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR; _SamplerStateDesc.ComparisonFunc = D3D11_COMPARISON_FUNC::D3D11_COMPARISON_NEVER; _SamplerStateDesc.MaxAnisotropy = 16; _SamplerStateDesc.MinLOD = 0; _SamplerStateDesc.MaxLOD = 10; _SamplerStateDesc.MipLODBias = -2; m_pDevice->CreateSamplerState(&_SamplerStateDesc, &m_pTextureSampler); }
CLight::CLight() { m_colorAmbiant = D3DXVECTOR4(0.2f, 0.2f, 0.2f, 1.0f); m_colorDiffuse = D3DXVECTOR4(0.5f, 0.5f, 0.5f, 1.0f); m_direction = D3DXVECTOR3(-0.01f, -0.4f, 0.0f); }
#include "FreeCameraController.h" #include "KeyChain.h" const D3DXVECTOR4 FreeCameraController::default_forward_ = D3DXVECTOR4(0,0,-1,0); const D3DXVECTOR4 FreeCameraController::default_up_ = D3DXVECTOR4(0, 1, 0,0); const D3DXVECTOR4 FreeCameraController::default_right_ = D3DXVECTOR4(1, 0, 0 ,0); FreeCameraController::FreeCameraController() { yaw_ = 0.0f; pitch_ = 0.0f; field_of_view_ = 45.0f; near_ = 0.1f; far_ = 100.0f; } void FreeCameraController::tick() { float multiplier = 0.1f; if (key_chain.key(KeyType::right_mouse_button)) { D3DXVECTOR2 delta = key_chain.get_mouse_move(); pitch_ += delta.y * 0.001; yaw_ -= delta.x * 0.001; while(yaw_ > PI) { yaw_ -= PI; }
//************************************************************************************************************* void ExtrudeSilhouette(ShadowCaster& caster, const D3DXVECTOR3& lightpos) { D3DXVECTOR4* vdata = 0; WORD* idata = 0; D3DXMATRIX inv; D3DXVECTOR3 lp; D3DXVECTOR3 center(0, 0, 0); D3DXVECTOR3 ltocenter; float weight = 0.5f / caster.silhouette.size(); caster.vertices->Lock(0, 0, (void**)&vdata, D3DLOCK_DISCARD); caster.indices->Lock(0, 0, (void**)&idata, D3DLOCK_DISCARD); D3DXMatrixInverse(&inv, NULL, &caster.world); D3DXVec3TransformCoord(&lp, &lightpos, &inv); for( size_t i = 0; i < caster.silhouette.size(); ++i ) { const Edge& e = caster.silhouette[i]; center += e.v1 * weight; center += e.v2 * weight; } ltocenter = center - lp; D3DXVec3Normalize(<ocenter, <ocenter); ltocenter *= VOLUME_OFFSET; for( size_t i = 0; i < caster.silhouette.size(); ++i ) { const Edge& e = caster.silhouette[i]; vdata[i * 4 + 0] = D3DXVECTOR4(e.v1 + ltocenter, 1); vdata[i * 4 + 1] = D3DXVECTOR4(e.v1 - lp, 0); vdata[i * 4 + 2] = D3DXVECTOR4(e.v2 + ltocenter, 1); vdata[i * 4 + 3] = D3DXVECTOR4(e.v2 - lp, 0); idata[i * 6 + 0] = i * 4 + 0; idata[i * 6 + 1] = i * 4 + 1; idata[i * 6 + 2] = i * 4 + 2; idata[i * 6 + 3] = i * 4 + 2; idata[i * 6 + 4] = i * 4 + 1; idata[i * 6 + 5] = i * 4 + 3; } // back cap vdata[caster.silhouette.size() * 4] = D3DXVECTOR4(center - lp, 0); idata += caster.silhouette.size() * 6; for( size_t i = 0; i < caster.silhouette.size(); ++i ) { const Edge& e = caster.silhouette[i]; idata[i * 3 + 0] = i * 4 + 3; idata[i * 3 + 1] = i * 4 + 1; idata[i * 3 + 2] = caster.silhouette.size() * 4; } // front cap WORD* idata2 = NULL; D3DXVECTOR3* vdata2 = NULL; WORD verticesadded = caster.silhouette.size() * 4 + 1; DWORD indicesadded = caster.silhouette.size() * 9; WORD i1, i2, i3; D3DXVECTOR3 *v1, *v2, *v3; D3DXVECTOR3 a, b, n; float dist; WORD* added = (WORD*)malloc(caster.caster->GetNumVertices() * sizeof(WORD)); memset(added, 0, caster.caster->GetNumVertices() * sizeof(WORD)); //vdata += caster.silhouette.size() * 4 + 1; idata += caster.silhouette.size() * 3; caster.caster->LockVertexBuffer(D3DLOCK_READONLY, (void**)&vdata2); caster.caster->LockIndexBuffer(D3DLOCK_READONLY, (void**)&idata2); for( DWORD i = 0; i < caster.caster->GetNumFaces() * 3; i += 3 ) { i1 = idata2[i + 0]; i2 = idata2[i + 1]; i3 = idata2[i + 2]; v1 = (vdata2 + i1); v2 = (vdata2 + i2); v3 = (vdata2 + i3); // OPTIM: store normals a = v2->operator -(*v1); b = v3->operator -(*v1); D3DXVec3Cross(&n, &a, &b); dist = D3DXVec3Dot(&n, &lp) - D3DXVec3Dot(&n, v1); if( dist > 0 ) { if( !added[i1] ) { vdata[verticesadded] = D3DXVECTOR4(*v1 + ltocenter, 1); added[i1] = verticesadded; ++verticesadded; } if( !added[i2] ) { vdata[verticesadded] = D3DXVECTOR4(*v2 + ltocenter, 1); added[i2] = verticesadded; ++verticesadded; } if( !added[i3] ) { vdata[verticesadded] = D3DXVECTOR4(*v3 + ltocenter, 1); added[i3] = verticesadded; ++verticesadded; } idata[0] = added[i1]; idata[1] = added[i2]; idata[2] = added[i3]; indicesadded += 3; idata += 3; } } free(added); caster.caster->UnlockIndexBuffer(); caster.caster->UnlockVertexBuffer(); caster.indices->Unlock(); caster.vertices->Unlock(); caster.numvertices = verticesadded; caster.numfaces = indicesadded / 3; #ifdef _DEBUG // as kissing frogs... DWORD test1 = VOLUME_NUMVERTICES(caster.caster->GetNumVertices()); DWORD test2 = VOLUME_NUMINDICES(caster.caster->GetNumFaces()); if( (verticesadded > test1) || (indicesadded > test2) ) { std::cout << "Buffer overflow!\n"; ::_CrtDbgBreak(); } #endif }
/** Binds a pipeline-state */ void D3D11GraphicsEngineQueued::BindPipelineState(const PipelineState* state) { D3D11PipelineState* s = (D3D11PipelineState*)state; D3D11PipelineState* b = (D3D11PipelineState*)BoundPipelineStateByThread[GetCurrentThreadId()]; ID3D11DeviceContext* context = GetDeferredContextByThread(); if(!b) b = (D3D11PipelineState*)&DefaultPipelineState; // Bind state if(b->BlendState != s->BlendState) SC_DBG(context->OMSetBlendState(s->BlendState, (float *)&D3DXVECTOR4(0, 0, 0, 0), 0xFFFFFFFF), GothicRendererInfo::SC_BS); if(b->SamplerState != s->SamplerState) SC_DBG(context->PSSetSamplers(0, 1, &s->SamplerState), GothicRendererInfo::SC_SMPL); if(b->DepthStencilState != s->DepthStencilState) SC_DBG(context->OMSetDepthStencilState(s->DepthStencilState, 0), GothicRendererInfo::SC_DSS); if(b->RasterizerState != s->RasterizerState) SC_DBG(context->RSSetState(s->RasterizerState), GothicRendererInfo::SC_RS); // Bind constantbuffers (They are likely to change for every object) if(!s->ConstantBuffersVS.empty() && s->ConstantBuffersVS != b->ConstantBuffersVS)SC_DBG(context->VSSetConstantBuffers(0, s->ConstantBuffersVS.size(), &s->ConstantBuffersVS[0]),GothicRendererInfo::SC_CB); if(!s->ConstantBuffersPS.empty() && s->ConstantBuffersPS != b->ConstantBuffersPS)SC_DBG(context->PSSetConstantBuffers(0, s->ConstantBuffersPS.size(), &s->ConstantBuffersPS[0]),GothicRendererInfo::SC_CB); if(!s->ConstantBuffersHDS.empty() && s->ConstantBuffersHDS != b->ConstantBuffersHDS)SC_DBG(context->HSSetConstantBuffers(0, s->ConstantBuffersHDS.size(), &s->ConstantBuffersHDS[0]),GothicRendererInfo::SC_CB); if(!s->ConstantBuffersHDS.empty() && s->ConstantBuffersHDS != b->ConstantBuffersHDS)SC_DBG(context->DSSetConstantBuffers(0, s->ConstantBuffersHDS.size(), &s->ConstantBuffersHDS[0]),GothicRendererInfo::SC_CB); if(!s->ConstantBuffersGS.empty() && s->ConstantBuffersGS != b->ConstantBuffersGS)SC_DBG(context->GSSetConstantBuffers(0, s->ConstantBuffersGS.size(), &s->ConstantBuffersGS[0]),GothicRendererInfo::SC_CB); // Vertexbuffers UINT off[] = {0,0}; if(memcmp(s->BaseState.VertexBuffers, b->BaseState.VertexBuffers, sizeof(b->BaseState.VertexBuffers)) != 0) SC_DBG(context->IASetVertexBuffers(0, s->VertexBuffers.size(), &s->VertexBuffers[0], s->BaseState.VertexStride, off), GothicRendererInfo::SC_VB); if(!s->StructuredBuffersVS.empty() && memcmp(s->BaseState.StructuredBuffersVS, b->BaseState.StructuredBuffersVS, sizeof(b->BaseState.StructuredBuffersVS)) != 0) SC_DBG(context->VSSetShaderResources(0, 1, &s->StructuredBuffersVS[0]), GothicRendererInfo::SC_VB); if(s->IndexBuffer != b->IndexBuffer) SC_DBG(context->IASetIndexBuffer(s->IndexBuffer, s->BaseState.IndexStride == 4 ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R16_UINT, 0), GothicRendererInfo::SC_IB); // Shaders if(s->VertexShader != b->VertexShader) SC_DBG(context->VSSetShader(s->VertexShader, NULL, NULL), GothicRendererInfo::SC_VS); if(s->InputLayout != b->InputLayout) SC_DBG(context->IASetInputLayout(s->InputLayout), GothicRendererInfo::SC_IL); if(s->PixelShader != b->PixelShader) SC_DBG(context->PSSetShader(s->PixelShader, NULL, NULL), GothicRendererInfo::SC_PS); if(s->HullShader != b->HullShader) SC_DBG(context->HSSetShader(s->HullShader, NULL, NULL), GothicRendererInfo::SC_HS); if(s->DomainShader != b->DomainShader) SC_DBG(context->DSSetShader(s->DomainShader, NULL, NULL), GothicRendererInfo::SC_DS); if(s->GeometryShader != b->GeometryShader) SC_DBG(context->GSSetShader(s->GeometryShader, NULL, NULL), GothicRendererInfo::SC_GS); // Rendertargets if(memcmp(s->RenderTargetViews, b->RenderTargetViews, sizeof(void*) * s->NumRenderTargetViews) != 0 || s->DepthStencilView != b->DepthStencilView) SC_DBG(context->OMSetRenderTargets(s->NumRenderTargetViews, s->RenderTargetViews, s->DepthStencilView), GothicRendererInfo::SC_RTVDSV); // Textures if(memcmp(s->Textures, b->Textures, sizeof(void*) * s->BaseState.NumTextures) != 0) SC_DBG(context->PSSetShaderResources(0, s->BaseState.NumTextures, s->Textures), GothicRendererInfo::SC_TX); // Primitive topology //Context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // Replace old state // FIXME: Might not threadsave BoundPipelineStateByThread[GetCurrentThreadId()] = s; }
void DxManager::RenderCascadedShadowMap() { if(this->useSun) { D3DXMATRIX wvp; D3DXMatrixIdentity(&wvp); for (int l = 0; l < this->csm->GetNrOfCascadeLevels(); l++) { this->Dx_DeviceContext->OMSetRenderTargets(0, 0, this->csm->GetShadowMapDSV(l)); D3D11_VIEWPORT wp = this->csm->GetShadowMapViewPort(l); this->Dx_DeviceContext->RSSetViewports(1, &wp); this->Dx_DeviceContext->ClearDepthStencilView(this->csm->GetShadowMapDSV(l), D3D11_CLEAR_DEPTH, 1.0f, 0); //Terrain for(int i = 0; i < this->terrains.size(); i++) { //Matrices wvp = this->terrains[i]->GetWorldMatrix() * this->csm->GetViewProjMatrix(l); this->Shader_ShadowMap->SetMatrix("LightWVP", wvp); //Input Assembler this->Dx_DeviceContext->IASetPrimitiveTopology(this->terrains[i]->GetTopology()); //Vertex data Buffer* verts = this->terrains[i]->GetVertexBufferPointer(); Buffer* inds = this->terrains[i]->GetIndexBufferPointer(); if(verts) { inds->Apply(); } if(inds) { verts->Apply(); } //Apply Shader this->Shader_ShadowMap->Apply(0); //Draw if(inds) { this->Dx_DeviceContext->DrawIndexed(inds->GetElementCount(), 0, 0); } else { this->Dx_DeviceContext->Draw(verts->GetElementCount(), 0); } } //Static meshes for(int i = 0; i < this->objects.size(); i++) { if(!this->objects[i]->IsUsingInvisibility()) { MaloW::Array<MeshStrip*>* strips = this->objects[i]->GetStrips(); wvp = this->objects[i]->GetWorldMatrix() * this->csm->GetViewProjMatrix(l); this->Shader_ShadowMap->SetMatrix("LightWVP", wvp); for(int u = 0; u < strips->size(); u++) { Object3D* obj = strips->get(u)->GetRenderObject(); Dx_DeviceContext->IASetPrimitiveTopology(obj->GetTopology()); Buffer* verts = obj->GetVertBuff(); if(verts) verts->Apply(); Buffer* inds = obj->GetIndsBuff(); if(inds) inds->Apply(); Shader_ShadowMap->Apply(0); // draw if(inds) Dx_DeviceContext->DrawIndexed(inds->GetElementCount(), 0, 0); else Dx_DeviceContext->Draw(verts->GetElementCount(), 0); } } } D3DXMATRIX lvp = this->csm->GetViewProjMatrix(l); // For deferred: this->Shader_DeferredLightning->SetResourceAtIndex(l, "CascadedShadowMap", this->csm->GetShadowMapSRV(l)); this->Shader_DeferredLightning->SetStructMemberAtIndexAsMatrix(l, "cascades", "viewProj", lvp); } float PCF_SIZE = (float)this->params.ShadowMapSettings + 1; float PCF_SQUARED = 1 / (PCF_SIZE * PCF_SIZE); this->Shader_DeferredLightning->SetFloat("SMAP_DX", 1.0f / (256.0f * pow(2.0f, this->params.ShadowMapSettings / 2.0f))); this->Shader_DeferredLightning->SetFloat("PCF_SIZE", PCF_SIZE); this->Shader_DeferredLightning->SetFloat("PCF_SIZE_SQUARED", PCF_SQUARED); this->Shader_DeferredLightning->SetFloat("NrOfCascades", (float)this->csm->GetNrOfCascadeLevels()); this->Shader_DeferredLightning->SetFloat4("CascadeLevels", D3DXVECTOR4(this->csm->GetSplitDepth(0), this->csm->GetSplitDepth(1), this->csm->GetSplitDepth(2), this->csm->GetSplitDepth(3))); } }
void Cube::Initialize(D3DXMATRIX world, float height, float width, float depht, ID3D11Device* g_Device, ID3D11DeviceContext* g_DeviceContext, IShader* shader ) { VERTEX::VertexPNT mesh[] = { {D3DXVECTOR4(-width, height,-depht ,1) ,D3DXVECTOR4(0,0,-1, 0), D3DXVECTOR2(0 ,0)}, {D3DXVECTOR4(width,height,-depht ,1) ,D3DXVECTOR4(0,0,-1, 0), D3DXVECTOR2(1 ,0)}, {D3DXVECTOR4(-width,-height,-depht ,1) ,D3DXVECTOR4(0,0,-1, 0), D3DXVECTOR2(0 ,1)}, {D3DXVECTOR4(-width,-height,-depht ,1) ,D3DXVECTOR4(0,0,-1, 0), D3DXVECTOR2(0, 1)}, {D3DXVECTOR4(width,height,-depht ,1) ,D3DXVECTOR4(0,0,-1, 0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(width,-height, -depht ,1) ,D3DXVECTOR4(0,0,-1, 0), D3DXVECTOR2(1, 1)}, {D3DXVECTOR4(width,height,-depht ,1) ,D3DXVECTOR4(1,0,0,0), D3DXVECTOR2(0, 0)}, {D3DXVECTOR4(width,height,depht ,1) ,D3DXVECTOR4(1,0,0,0), D3DXVECTOR2(1,0)}, {D3DXVECTOR4(width,-height,-depht ,1) ,D3DXVECTOR4(1,0,0,0), D3DXVECTOR2(0, 1)}, {D3DXVECTOR4(width,-height,-depht ,1) ,D3DXVECTOR4(1,0,0,0), D3DXVECTOR2(0, 1)}, {D3DXVECTOR4(width,height,depht ,1) ,D3DXVECTOR4(1,0,0,0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(width,-height,depht ,1) ,D3DXVECTOR4(1,0,0,0), D3DXVECTOR2(1, 1)}, {D3DXVECTOR4(width,height,depht ,1) ,D3DXVECTOR4(0,0,1,0), D3DXVECTOR2(0,0 )}, {D3DXVECTOR4(-width,height,depht ,1) ,D3DXVECTOR4(0,0,1,0), D3DXVECTOR2( 1 , 0)}, {D3DXVECTOR4(width,-height,depht ,1) ,D3DXVECTOR4(0,0,1,0), D3DXVECTOR2(0 , 1)}, {D3DXVECTOR4(width,-height,depht ,1) ,D3DXVECTOR4(0,0,1,0), D3DXVECTOR2(0 , 1)}, {D3DXVECTOR4(-width,height,depht ,1) ,D3DXVECTOR4(0,0,1,0), D3DXVECTOR2( 1 , 0)}, {D3DXVECTOR4(-width,-height,depht ,1) ,D3DXVECTOR4(0,0,1,0), D3DXVECTOR2( 1 , 1)}, {D3DXVECTOR4(-width,height,depht ,1) ,D3DXVECTOR4(-1,0,0,0), D3DXVECTOR2(0 , 0)}, {D3DXVECTOR4(-width,height,-depht ,1) ,D3DXVECTOR4(-1,0,0,0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(-width,-height,depht ,1) ,D3DXVECTOR4(-1,0,0,0), D3DXVECTOR2(0,1)}, {D3DXVECTOR4(-width,-height,depht ,1) ,D3DXVECTOR4(-1,0,0,0), D3DXVECTOR2(0, 1)}, {D3DXVECTOR4(-width,height,-depht ,1) ,D3DXVECTOR4(-1,0,0,0), D3DXVECTOR2( 1, 0)}, {D3DXVECTOR4(-width,-height,-depht ,1) ,D3DXVECTOR4(-1,0,0,0), D3DXVECTOR2(1,1)}, {D3DXVECTOR4(-width,height,depht ,1) ,D3DXVECTOR4(0,1,0,0), D3DXVECTOR2( 0 , 0)}, {D3DXVECTOR4(width,height,depht ,1) ,D3DXVECTOR4(0,1,0,0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(-width,height,-depht ,1) ,D3DXVECTOR4(0,1,0,0), D3DXVECTOR2( 0,1)}, {D3DXVECTOR4(-width,height,-depht ,1) ,D3DXVECTOR4(0,1,0,0), D3DXVECTOR2( 0,1)}, {D3DXVECTOR4(width,height,depht ,1) ,D3DXVECTOR4(0,1,0,0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(width,height,-depht ,1) ,D3DXVECTOR4(0,1,0,0), D3DXVECTOR2(1, 1)}, {D3DXVECTOR4(-width,-height,-depht ,1) ,D3DXVECTOR4(0,-1,0,0), D3DXVECTOR2(0, 0)}, {D3DXVECTOR4(width,-height,-depht ,1) ,D3DXVECTOR4(0,-1,0,0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(-width,-height,depht ,1) ,D3DXVECTOR4(0,-1,0,0), D3DXVECTOR2(0,1)}, {D3DXVECTOR4(-width,-height,depht ,1) ,D3DXVECTOR4(0,-1,0,0), D3DXVECTOR2(0,1)}, {D3DXVECTOR4(width,-height,-depht ,1) ,D3DXVECTOR4(0,-1,0,0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(width,-height,depht ,1) ,D3DXVECTOR4(0,-1,0,0), D3DXVECTOR2(1, 1)} }; /*VERTEX::VertexPT mesh[] = { {D3DXVECTOR4(-width, height,-depht ,1) , D3DXVECTOR2(0 ,0)}, {D3DXVECTOR4(width,height,-depht ,1) , D3DXVECTOR2(1 ,0)}, {D3DXVECTOR4(-width,-height,-depht ,1) , D3DXVECTOR2(0 ,1)}, {D3DXVECTOR4(-width,-height,-depht ,1) , D3DXVECTOR2(0, 1)}, {D3DXVECTOR4(width,height,-depht ,1) , D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(width,-height, -depht ,1) , D3DXVECTOR2(1, 1)}, {D3DXVECTOR4(width,height,-depht ,1) , D3DXVECTOR2(0, 0)}, {D3DXVECTOR4(width,height,depht ,1) , D3DXVECTOR2(1,0)}, {D3DXVECTOR4(width,-height,-depht ,1) , D3DXVECTOR2(0, 1)}, {D3DXVECTOR4(width,-height,-depht ,1) , D3DXVECTOR2(0, 1)}, {D3DXVECTOR4(width,height,depht ,1) , D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(width,-height,depht ,1) , D3DXVECTOR2(1, 1)}, {D3DXVECTOR4(width,height,depht ,1) , D3DXVECTOR2(0,0 )}, {D3DXVECTOR4(-width,height,depht ,1) , D3DXVECTOR2( 1 , 0)}, {D3DXVECTOR4(width,-height,depht ,1) , D3DXVECTOR2(0 , 1)}, {D3DXVECTOR4(width,-height,depht ,1) , D3DXVECTOR2(0 , 1)}, {D3DXVECTOR4(-width,height,depht ,1) , D3DXVECTOR2( 1 , 0)}, {D3DXVECTOR4(-width,-height,depht ,1) , D3DXVECTOR2( 1 , 1)}, {D3DXVECTOR4(-width,height,depht ,1), D3DXVECTOR2(0 , 0)}, {D3DXVECTOR4(-width,height,-depht ,1), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(-width,-height,depht ,1), D3DXVECTOR2(0,1)}, {D3DXVECTOR4(-width,-height,depht ,1), D3DXVECTOR2(0, 1)}, {D3DXVECTOR4(-width,height,-depht ,1), D3DXVECTOR2( 1, 0)}, {D3DXVECTOR4(-width,-height,-depht ,1), D3DXVECTOR2(1,1)}, {D3DXVECTOR4(-width,height,depht ,1), D3DXVECTOR2( 0 , 0)}, {D3DXVECTOR4(width,height,depht ,1), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(-width,height,-depht ,1), D3DXVECTOR2( 0,1)}, {D3DXVECTOR4(-width,height,-depht ,1), D3DXVECTOR2( 0,1)}, {D3DXVECTOR4(width,height,depht ,1), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(width,height,-depht ,1), D3DXVECTOR2(1, 1)}, {D3DXVECTOR4(-width,-height,-depht ,1) , D3DXVECTOR2(0, 0)}, {D3DXVECTOR4(width,-height,-depht ,1) , D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(-width,-height,depht ,1) , D3DXVECTOR2(0,1)}, {D3DXVECTOR4(-width,-height,depht ,1) , D3DXVECTOR2(0,1)}, {D3DXVECTOR4(width,-height,-depht ,1) , D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(width,-height,depht ,1) , D3DXVECTOR2(1, 1)} }; BaseBuffer::BUFFER_INIT_DESC bufferDesc; bufferDesc.dc = D3DShell::self()->getDeviceContext(); bufferDesc.device = D3DShell::self()->getDevice(); bufferDesc.elementSize = sizeof(VERTEX::VertexPT); bufferDesc.data = mesh; bufferDesc.nrOfElements = 6; bufferDesc.type = BUFFER_FLAG::TYPE_VERTEX_BUFFER; bufferDesc.usage = BUFFER_FLAG::USAGE_DEFAULT; this->m_VertexBuffer = new BaseBuffer(); if(FAILED(m_VertexBuffer->Initialize(bufferDesc))) { MessageBox(0, L"Could not initialize planeVertexBuffer! Plane.cpp - Initialize", L"Error", MB_OK); }*/ VERTEX::VertexPNT mesh2[] = { {D3DXVECTOR4(-1, 1,-1 ,1) ,D3DXVECTOR4(0,0,-1, 0), D3DXVECTOR2(0 ,0)}, {D3DXVECTOR4(1,1,-1 ,1) ,D3DXVECTOR4(0,0,-1, 0), D3DXVECTOR2(1 ,0)}, {D3DXVECTOR4(-1,-1,-1 ,1) ,D3DXVECTOR4(0,0,-1, 0), D3DXVECTOR2(0 ,1)}, {D3DXVECTOR4(-1, -1,-1 ,1) ,D3DXVECTOR4(0,0,-1, 0), D3DXVECTOR2(0, 1)}, {D3DXVECTOR4(1,1,-1 ,1) ,D3DXVECTOR4(0,0,-1, 0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(1,-1, -1 ,1) ,D3DXVECTOR4(0,0,-1, 0), D3DXVECTOR2(1, 1)}, {D3DXVECTOR4(1, 1, -1 ,1) ,D3DXVECTOR4(1,0,0,0), D3DXVECTOR2(0, 0)}, {D3DXVECTOR4(1,1,1 ,1) ,D3DXVECTOR4(1,0,0,0), D3DXVECTOR2(1,0)}, {D3DXVECTOR4(1,-1,-1 ,1) ,D3DXVECTOR4(1,0,0,0), D3DXVECTOR2(0, 1)}, {D3DXVECTOR4(1,-1, -1 ,1) ,D3DXVECTOR4(1,0,0,0), D3DXVECTOR2(0, 1)}, {D3DXVECTOR4(1,1,1 ,1) ,D3DXVECTOR4(1,0,0,0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(1,-1, 1 ,1) ,D3DXVECTOR4(1,0,0,0), D3DXVECTOR2(1, 1)}, {D3DXVECTOR4(1, 1, 1 ,1) ,D3DXVECTOR4(0,0,1,0), D3DXVECTOR2(0,0 )}, {D3DXVECTOR4(-1,1,1 ,1) ,D3DXVECTOR4(0,0,1,0), D3DXVECTOR2( 1 , 0)}, {D3DXVECTOR4(1,-1,1 ,1) ,D3DXVECTOR4(0,0,1,0), D3DXVECTOR2(0 , 1)}, {D3DXVECTOR4(1,-1, 1 ,1) ,D3DXVECTOR4(0,0,1,0), D3DXVECTOR2(0 , 1)}, {D3DXVECTOR4(-1,1,1 ,1) ,D3DXVECTOR4(0,0,1,0), D3DXVECTOR2( 1 , 0)}, {D3DXVECTOR4(-1,-1, 1 ,1) ,D3DXVECTOR4(0,0,1,0), D3DXVECTOR2( 1 , 1)}, {D3DXVECTOR4(-1, 1, 1 ,1) ,D3DXVECTOR4(-1,0,0,0), D3DXVECTOR2(0 , 0)}, {D3DXVECTOR4(-1,1,-1 ,1) ,D3DXVECTOR4(-1,0,0,0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(-1,-1,1 ,1) ,D3DXVECTOR4(-1,0,0,0), D3DXVECTOR2(0,1)}, {D3DXVECTOR4(-1,-1, 1 ,1) ,D3DXVECTOR4(-1,0,0,0), D3DXVECTOR2(0, 1)}, {D3DXVECTOR4(-1,1,-1 ,1) ,D3DXVECTOR4(-1,0,0,0), D3DXVECTOR2( 1, 0)}, {D3DXVECTOR4(-1,-1, -1 ,1) ,D3DXVECTOR4(-1,0,0,0), D3DXVECTOR2(1,1)}, {D3DXVECTOR4(-1, 1, 1 ,1) ,D3DXVECTOR4(0,1,0,0), D3DXVECTOR2( 0 , 0)}, {D3DXVECTOR4(1,1,1 ,1) ,D3DXVECTOR4(0,1,0,0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(-1,1,-1 ,1) ,D3DXVECTOR4(0,1,0,0), D3DXVECTOR2( 0,1)}, {D3DXVECTOR4(-1,1, -1 ,1) ,D3DXVECTOR4(0,1,0,0), D3DXVECTOR2( 0,1)}, {D3DXVECTOR4(1,1,1 ,1) ,D3DXVECTOR4(0,1,0,0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(1,1, -1 ,1) ,D3DXVECTOR4(0,1,0,0), D3DXVECTOR2(1, 1)}, {D3DXVECTOR4(-1, -1, -1 ,1) ,D3DXVECTOR4(0,-1,0,0), D3DXVECTOR2(0, 0)}, {D3DXVECTOR4(1,-1,-1 ,1) ,D3DXVECTOR4(0,-1,0,0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(-1,-1,1 ,1) ,D3DXVECTOR4(0,-1,0,0), D3DXVECTOR2(0,1)}, {D3DXVECTOR4(-1,-1, 1 ,1) ,D3DXVECTOR4(0,-1,0,0), D3DXVECTOR2(0,1)}, {D3DXVECTOR4(1,-1,-1 ,1) ,D3DXVECTOR4(0,-1,0,0), D3DXVECTOR2(1, 0)}, {D3DXVECTOR4(1,-1, 1 ,1) ,D3DXVECTOR4(0,-1,0,0), D3DXVECTOR2(1, 1)} }; BaseBuffer::BUFFER_INIT_DESC bufferDesc; /*bufferDesc.dc = D3DShell::self()->getDeviceContext(); bufferDesc.device = D3DShell::self()->getDevice(); bufferDesc.elementSize = sizeof(VERTEX::VertexPNT); bufferDesc.data = mesh; bufferDesc.nrOfElements = 36; bufferDesc.type = BUFFER_FLAG::TYPE_CONSTANT_VS_BUFFER; bufferDesc.usage = BUFFER_FLAG::USAGE_DYNAMIC_CPU_WRITE_DISCARD; this->m_VertexBuffer = new BaseBuffer(); if(FAILED(m_VertexBuffer->Initialize(bufferDesc))) { MessageBox(0, L"Could not initialize planeVertexBuffer! Plane.cpp - Initialize", L"Error", MB_OK); } bufferDesc.dc = D3DShell::self()->getDeviceContext(); bufferDesc.device = D3DShell::self()->getDevice(); bufferDesc.elementSize = sizeof(VERTEX::VertexPNT); bufferDesc.data = mesh2; bufferDesc.nrOfElements = 36; bufferDesc.type = BUFFER_FLAG::TYPE_CONSTANT_VS_BUFFER; bufferDesc.usage = BUFFER_FLAG::USAGE_DYNAMIC_CPU_WRITE_DISCARD; this->m_VertexBuffer2 = new BaseBuffer(); if(FAILED(m_VertexBuffer2->Initialize(bufferDesc))) { MessageBox(0, L"Could not initialize planeVertexBuffer! Plane.cpp - Initialize", L"Error", MB_OK); } int index []= {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}; bufferDesc.dc = D3DShell::self()->getDeviceContext(); bufferDesc.device = D3DShell::self()->getDevice(); bufferDesc.elementSize = sizeof(int); bufferDesc.data = index; bufferDesc.nrOfElements = 36; bufferDesc.type = BUFFER_FLAG::TYPE_INDEX_BUFFER; bufferDesc.usage = BUFFER_FLAG::USAGE_DEFAULT; m_IndexBuffer = new BaseBuffer(); if(FAILED(m_IndexBuffer->Initialize(bufferDesc))) { MessageBox(0, L"Could not initialize planeIndexBuffer! Plane.cpp - Initialize", L"Error", MB_OK); }*/ D3DXMatrixIdentity(&m_world); D3DXMatrixIdentity(&m_rotation); D3DXMatrixIdentity(&m_scale); m_tranlate = world; m_shader = shader; std::vector<WCHAR*> text; text.push_back(L"..\\Resources\\Textures\\Baron_Nashor.png"); text.push_back(L"..\\Resources\\Textures\\Baron_NashorBump.png"); text.push_back(L"..\\Resources\\Textures\\Baron_NashorSpec.png"); addTextures(text); ObjectMaterial::OBJECT_MATERIAL_DESC mat; mat.dc = D3DShell::self()->getDeviceContext(); mat.device= D3DShell::self()->getDevice(); mat.ambientTexture = L"..\\Resources\\Textures\\Baron_Nashor.png"; mat.diffuseTexture = L"..\\Resources\\Textures\\Baron_NashorBump.png"; mat.ambient = D3DXVECTOR4(0,0,0,0); mat.diffuse = D3DXVECTOR4(0,0,0,0); mat.specular = D3DXVECTOR4(1,0,0,0); mat.specualarPow = 2; this->m_material.CreateMaterial(mat); createAnimationBuffer(); //VERTEX::VertexPNT* vertexBuffer1 = (VERTEX::VertexPNT*) this->m_VertexBuffer->Map(); //VERTEX::VertexPNT* vertexBuffer2 = (VERTEX::VertexPNT*) this->m_VertexBuffer2->Map(); VERTEX::Animation_2Frame animationMesh[36]; for(int i=0; i<36; i++) { animationMesh[i].position0 = mesh[i].position; animationMesh[i].normal0 = mesh[i].normal; animationMesh[i].texcoord0 = mesh[i].texcoord; animationMesh[i].position1 = mesh2[i].position; animationMesh[i].normal1 = mesh2[i].normal; animationMesh[i].texcoord1 = mesh2[i].texcoord; } //m_VertexBuffer->Unmap(); //m_VertexBuffer2->Unmap(); bufferDesc.dc = D3DShell::self()->getDeviceContext(); bufferDesc.device = D3DShell::self()->getDevice(); bufferDesc.elementSize = sizeof(VERTEX::Animation_2Frame); bufferDesc.data = animationMesh; bufferDesc.nrOfElements = 36; bufferDesc.type = BUFFER_FLAG::TYPE_VERTEX_BUFFER; bufferDesc.usage = BUFFER_FLAG::USAGE_DEFAULT; this->m_AnimationBuffer = new BaseBuffer(); if(FAILED(m_AnimationBuffer->Initialize(bufferDesc))) { MessageBox(0, L"Could not initialize animationbuffer! Plane.cpp - Initialize", L"Error", MB_OK); } WeightCB weight; weight.weight1 = 1; bufferDesc.dc = D3DShell::self()->getDeviceContext(); bufferDesc.device = D3DShell::self()->getDevice(); bufferDesc.elementSize = sizeof(WeightCB); bufferDesc.data = &weight; bufferDesc.nrOfElements = 1; bufferDesc.type = BUFFER_FLAG::TYPE_CONSTANT_VS_BUFFER; bufferDesc.usage = BUFFER_FLAG::USAGE_DYNAMIC_CPU_WRITE_DISCARD; this->m_weightBuffer = new BaseBuffer(); if(FAILED(m_weightBuffer->Initialize(bufferDesc))) { MessageBox(0, L"Could not initialize weightbuffer! Plane.cpp - Initialize", L"Error", MB_OK); } }
bool ModelClass::InitializeBuffers(ID3D11Device * device) { VertexType* vertices; unsigned long * indices; D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc; D3D11_SUBRESOURCE_DATA vertexData, indexData; HRESULT result; // 정점 배열의 길이를 설정합니다. m_vertexCount = 3; // 인덱스 배열의 길이를 설정합니다. m_indexCount = 3; // 정점 배열을 생성합니다. vertices = new VertexType[m_vertexCount]; if (!vertices) { return false; } // 인덱스 배열을 생성합니다. indices = new unsigned long[m_indexCount]; if (!indices) return false; // 정점 배열에 값을 넣습니다. vertices[0].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);// 왼쪽 아래. vertices[0].color = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f); vertices[1].position = D3DXVECTOR3(0.0f, 1.0f, 0.0f); // 상단 가운데. vertices[1].color = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f); vertices[2].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);// 오른쪽 아래. vertices[2].color = D3DXVECTOR4(1.0f, 0.0f, 0.0f, 1.0f); // 인덱스 배열에 값을 넣습니다. indices[0] = 0; // 왼쪽 아래 Bottom left. indices[1] = 1; // 상단 가운데. indices[2] = 2; // 오른쪽 위. // 정점 버퍼의 description을 작성합니다. vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT; vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount; vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vertexBufferDesc.CPUAccessFlags = 0; vertexBufferDesc.MiscFlags = 0; vertexBufferDesc.StructureByteStride = 0; // 정점 데이터를 가리키는 보조 리소스 구조체를 작성합니다. vertexData.pSysMem = vertices; vertexData.SysMemPitch = 0; vertexData.SysMemSlicePitch = 0; // 정점 버퍼를 생성합니다. result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer); if (FAILED(result)) { return false; } // 인덱스 버퍼의 description을 작성합니다. indexBufferDesc.Usage = D3D11_USAGE_DEFAULT; indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount; indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; indexBufferDesc.CPUAccessFlags = 0; indexBufferDesc.MiscFlags = 0; indexBufferDesc.StructureByteStride = 0; // 인덱스 데이터를 가리키는 보조 리소스 구조체를 작성합니다. indexData.pSysMem = indices; indexData.SysMemPitch = 0; indexData.SysMemSlicePitch = 0; // 인덱스 버퍼를 생성합니다. result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer); if (FAILED(result)) { return false; } // 생성되고 값이 할당된 정점 버퍼와 인덱스 버퍼를 해제합니다. delete[] vertices; vertices = 0; delete[] indices; indices = 0; return true; }
PFX_Transform::PFX_Transform() : Parent( this ) { mTCoefs[ 0 ] = D3DXVECTOR4( 1, 1, 1, 1 ); mTCoefs[ 1 ] = D3DXVECTOR4( 0, 0, 0, 0 ); }
/* * \brief Render the gizmo */ void CGizmo::Render( D3DXMATRIX worldMatrix, //!< D3DXMATRIX viewMatrix, //!< D3DXMATRIX projectionMatrix, //!< CCamera *camera ) { m_gizmoMesh->PrepareRender(m_renderer); // move to the position, but offset the y coordinate by one, to compensate for it been drawn around 0, 0, 0 local space IBrush *const brush = m_brush[m_currentBrush]; D3DXMATRIX xformmat, scalemat; D3DXMatrixTranslation(&xformmat, m_position.x, m_position.y < 2.0f ? 2.0f : m_position.y + 2.0f, m_position.z); D3DXMatrixScaling(&scalemat, static_cast<float>(brush->GetSize()) * 0.4f, 1.0f, static_cast<float>(brush->GetSize()) * 0.4f); D3DXMatrixMultiply (&worldMatrix, &scalemat, &xformmat); // Transpose the matrices to prepare them for the shader. D3DXMatrixTranspose(&worldMatrix, &worldMatrix); D3DXMatrixTranspose(&viewMatrix, &viewMatrix); D3DXMatrixTranspose(&projectionMatrix, &projectionMatrix); // Lock the constant buffer so it can be written to. HRESULT result; D3D11_MAPPED_SUBRESOURCE mappedResource; result = m_renderer->GetDeviceContext()->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) return; // Get a pointer to the data in the constant buffer. CGizmo::MatrixBuffer *const matrixDataPtr = (CGizmo::MatrixBuffer*)mappedResource.pData; // Copy the matrices into the constant buffer. matrixDataPtr->world = worldMatrix; matrixDataPtr->view = viewMatrix; matrixDataPtr->projection = projectionMatrix; // Unlock the constant buffer. m_renderer->GetDeviceContext()->Unmap(m_matrixBuffer, 0); result = m_renderer->GetDeviceContext()->Map(m_gizmoBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) return; // Get a pointer to the data in the constant buffer. CGizmo::GizmoBuffer *const gizmoDataPtr = (CGizmo::GizmoBuffer*)mappedResource.pData; // Copy the data into the constant buffer. // color based upon gizmo state if (m_gizmoState == GizmoState::Free) { // green gizmoDataPtr->color = D3DXVECTOR4(0.25f * (brush->GetStrength() * brush->GetStrength()), 0.6f * brush->GetStrength(), 0.1f * (3.0f - brush->GetStrength()), 0.01f); } else { // red gizmoDataPtr->color = D3DXVECTOR4(0.6f, 0.0f, 0.0f, 0.01f); } // Unlock the constant buffer. m_renderer->GetDeviceContext()->Unmap(m_gizmoBuffer, 0); // Finally set the constant buffer in the vertex shader with the updated values. m_renderer->GetDeviceContext()->VSSetConstantBuffers(0, 1, &m_matrixBuffer); m_renderer->GetDeviceContext()->VSSetConstantBuffers(1, 1, &m_gizmoBuffer); // Set the vertex input layout. m_renderer->GetDeviceContext()->IASetInputLayout(m_layout); // Set the vertex and pixel shaders that will be used to render this triangle. m_renderer->GetDeviceContext()->VSSetShader(m_vertexShader, NULL, 0); m_renderer->GetDeviceContext()->PSSetShader(m_pixelShader, NULL, 0); m_renderer->EnableAlphaBlending(true); // Render the triangle. m_gizmoMesh->Draw(m_renderer); m_renderer->EnableAlphaBlending(false); }
//------------------------------------------------------------- // Name: RenderScene() // Desc: ȭ�� ���� //------------------------------------------------------------- void CMyD3DApplication::RenderScene() { D3DXMATRIX m, mT, mR, mView, mProj; D3DXMATRIX mWorld; D3DXVECTOR4 v, light_pos, eye_pos; DWORD i; //----------------------------------------------------- // �������� //----------------------------------------------------- // ������� D3DXMatrixScaling( &m, 3.0f, 3.0f, 3.0f ); D3DXMatrixRotationY( &mR, D3DX_PI ); D3DXMatrixTranslation( &mT, 0.0f,-2.0f ,0.0f ); mWorld = m * mR * mT; // ��ļ��� m_pd3dDevice->SetTransform( D3DTS_WORLD, &mWorld); m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_mView ); m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_mProj ); TSS( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ); TSS( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); SAMP( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); SAMP( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); m_pMeshBg->Render( m_pd3dDevice ); if( m_pEffect != NULL ) { //------------------------------------------------- // ���̴� ���� //------------------------------------------------- m_pEffect->SetTechnique( m_hTechnique ); m_pEffect->Begin( NULL, 0 ); m_pEffect->Pass( 0 ); //------------------------------------------------- // ������ ���� //------------------------------------------------- // �������(ȸ��) D3DXMatrixRotationY( &mWorld, m_fTime ); // ����-������ȯ��� m = mWorld * m_mView * m_mProj; m_pEffect->SetMatrix( m_hmWVP, &m ); // ��������(������ǥ��) light_pos = D3DXVECTOR4( -0.577f, -0.577f, -0.577f,0); D3DXMatrixInverse( &m, NULL, &mWorld); D3DXVec4Transform( &v, &light_pos, &m ); D3DXVec3Normalize( (D3DXVECTOR3 *)&v, (D3DXVECTOR3 *)&v ); v.w = -0.5f; // ȯ�汤��� m_pEffect->SetVector( m_hvLightDir, &v ); // ����(������ǥ��) m = mWorld * m_mView; D3DXMatrixInverse( &m, NULL, &m); v = D3DXVECTOR4( 0, 0, 0, 1); D3DXVec4Transform( &v, &v, &m ); m_pEffect->SetVector( m_hvEyePos, &v ); // ������ m_pEffect->SetTexture( m_htNormalMap, m_pNormalMap ); // �������� m_pd3dDevice->SetVertexDeclaration( m_pDecl ); D3DMATERIAL9 *pMtrl = m_pMesh->m_pMaterials; for( i=0; i<m_pMesh->m_dwNumMaterials; i++ ) { v.x = pMtrl->Diffuse.r; v.y = pMtrl->Diffuse.g; v.z = pMtrl->Diffuse.b; m_pEffect->SetVector( m_hvColor, &v ); m_pEffect->SetTexture( m_htDecaleTex, m_pMesh->m_pTextures[i] ); m_pMesh->m_pLocalMesh->DrawSubset( i ); // ���� pMtrl++; } m_pEffect->End(); } }
bool GraphicsClass::Render() { D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix; D3DXMATRIX rotatex, rotatey, rotatez; D3DXMatrixIdentity(&rotatex); D3DXMatrixIdentity(&rotatey); D3DXMatrixIdentity(&rotatez); bool result; if(changeRotation == true) { rotation += (float)D3DX_PI * 0.001f; } if(rotation > 360.0f) { rotation -= 360.0f; } if(changeAnimation == true) { step++; if(step%1 == 0) { animationcycle += 1; step = 0; } } if (animationcycle >= loader->getAnimation()->GetFramesNumber()) { animationcycle = 2; } lightcycle += PI/180; if (lightcycle >= 2*PI) { lightcycle = 0; } m_DX->BeginScene(0.5f, 0.5f, 1.0f, 0.0f); // Generate the view matrix based on the camera's position. cam->Render(); // Get the world, view, and projection matrices from the camera and d3d objects. cam->GetViewMatrix(viewMatrix); m_DX->GetWorldMatrix(worldMatrix); m_DX->GetProjectionMatrix(projectionMatrix); D3DXMatrixRotationZ(&rotatey, D3DX_PI/8+rotation); D3DXMatrixRotationY(&rotatex, D3DX_PI/4); D3DXMATRIX *bonematrices = new D3DXMATRIX[loader->getSkeleton()->GetBonesCount()]; D3DXMATRIX *previousmatrices = new D3DXMATRIX[loader->getSkeleton()->GetBonesCount()]; for(int j = 0; j<loader->getSkeleton()->GetBonesCount(); j++) { bonematrices[j] = loader->getAnimation()->GetSkeleton(animationcycle)->GetBone(loader->getSkeleton()->GetBoneIndex(j))->GetTransformation();//sprawdzic getindex bone, chyba indeksy sa zle //bonematrices[j] = loader->getSkeleton()->GetBone(loader->getSkeleton()->GetBoneIndex(j))->GetTransformation(); if (animationcycle > 2) { previousmatrices[j] = loader->getAnimation()->GetSkeleton(animationcycle-1)->GetBone(loader->getSkeleton()->GetBoneIndex(j))->GetTransformation(); } else { previousmatrices[j] = loader->getAnimation()->GetSkeleton(loader->getAnimation()->GetFramesNumber()-1)->GetBone(loader->getSkeleton()->GetBoneIndex(j))->GetTransformation(); } continue; } renderer->RenderScene(m_DX->GetDeviceContext()); result = m_ColorShader->Render(m_DX->GetDeviceContext(), loader->getIndexMaxCount(), worldMatrix*rotatey, viewMatrix, projectionMatrix, cam->GetPosition(), D3DXVECTOR3(2.0f, 5.0f, -0.2f) , D3DXVECTOR4(0.1f, 0.1f, 0.1f, 0.1f), D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f), D3DXVECTOR4 (1.0f, 1.0f, 1.0f, 1.0f), 32.0f, renderer->GetTexture(), bonematrices, loader->getSkeleton()->GetBonesCount()); if(!result) { return false; } m_DX->TurnOnAlphaBlending(); for(int i=0; i<numLayers; i++) { float layer = float(i) / numLayers; float length = hairGaps * layer; int usedLayer = generatedLayers-(int)floor(((float)generatedLayers/(float)numLayers)*i)-1; wind; result = m_FurShader->Render(m_DX->GetDeviceContext(), loader->getIndexMaxCount(), worldMatrix*rotatey, viewMatrix, projectionMatrix, furGenerator->GetTexture(usedLayer), length, bonematrices, previousmatrices, loader->getSkeleton()->GetBonesCount(), cam->GetPosition(), D3DXVECTOR3(100*(float)cos(lightcycle),50,-100*(float)sin(lightcycle)), cam->GetPosition(), wind, gravity, dampening, stiffness, reflect); if(!result) { return false; } } m_DX->TurnOffAlphaBlending(); m_DX->EndScene(); return true; }
bool Lighting::EvaluateAmbient(LPDIRECT3DDEVICE9 Device, RenderMesh *RMesh, ShaderMat *Mat) { int i; if(!Device || !RMesh || !m_Ready) { return(false); } Device->SetVertexShaderConstantF(CV_GLOBAL_AMBIENT,(float*)&D3DXVECTOR4(0.0f, 0.0f, 0.0f, 0.0f),1); Device->SetRenderState(D3DRS_ZWRITEENABLE,TRUE); // // Clear texture stages // for(i=0; i < MAX_TMUS; i++) { Device->SetTexture(i,NULL); Device->SetTextureStageState(i,D3DTSS_TEXCOORDINDEX,i); Device->SetTextureStageState(i,D3DTSS_COLOROP,D3DTOP_DISABLE); Device->SetTextureStageState(i,D3DTSS_ALPHAOP,D3DTOP_DISABLE); Device->SetSamplerState(i,D3DSAMP_ADDRESSU,D3DTADDRESS_WRAP); Device->SetSamplerState(i,D3DSAMP_ADDRESSV,D3DTADDRESS_WRAP); Device->SetSamplerState(i,D3DSAMP_ADDRESSW,D3DTADDRESS_WRAP); Device->SetRenderState((D3DRENDERSTATETYPE)(D3DRS_WRAP0 + i),D3DWRAPCOORD_3); } Device->SetVertexShader(m_VertexHandle[SHADER_AMBIENT]); if(Mat->m_AlphaOn) { m_Device->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE); m_Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); m_Device->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA); Mat->m_Shader[CHANNEL_DIFFUSE].SetTexture(Device); Device->SetVertexShaderConstantF(CV_MAT_AMBIENT,(float*)&D3DXVECTOR4(Mat->m_Ambient.r, Mat->m_Ambient.g, Mat->m_Ambient.b, 1.0f),1); Device->SetPixelShader(m_PixelHandle[SHADER_AMBIENT]); } else { Device->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE); Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); Device->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ZERO); Device->SetVertexShaderConstantF(CV_MAT_AMBIENT,(float*)&D3DXVECTOR4(Mat->m_Ambient.r, Mat->m_Ambient.g, Mat->m_Ambient.b, 0.0f),1); Device->SetPixelShader(0); } RMesh->Render(Device); return(true); }