StaticMesh* GraphicsEngineImp::CreateStaticMesh(string filename, D3DXVECTOR3 pos, Material* material) { StaticMesh* mesh = new StaticMesh(pos, filename); // if it is in memory dont put it on another thread. if(!this->useLoadingThread && GetResourceManager()->HasMeshStripsResource(filename.c_str())) { bool success = mesh->LoadFromFile(filename); if(success) { this->dx->CreateStaticMesh(mesh); } MaloW::Array<MeshStrip*>* strips = mesh->GetStrips(); for(unsigned int i = 0; i < strips->size(); i++) { strips->get(i)->SetMaterial(material); if(i+1 < strips->size()) { material = new Material(material); } } } else { LoadMeshEvent* re = new LoadMeshEvent(filename, mesh, NULL, material); this->PutEvent(re); } return mesh; }
void DxManager::CreateStaticMesh(StaticMesh* mesh) { MaloW::Array<MeshStrip*>* strips = mesh->GetStrips(); for(int i = 0; i < strips->size(); i++) { MeshStrip* strip = strips->get(i); BUFFER_INIT_DESC bufferDesc; bufferDesc.ElementSize = sizeof(Vertex); bufferDesc.InitData = strip->getVerts(); // Last face black, should +1 this to solve it. bufferDesc.NumElements = strip->getNrOfVerts(); bufferDesc.Type = VERTEX_BUFFER; bufferDesc.Usage = BUFFER_DEFAULT; Buffer* verts = new Buffer(); if(FAILED(verts->Init(Dx_Device, Dx_DeviceContext, bufferDesc))) MaloW::Debug("Initiate Buffer Failed in DxManager"); Buffer* inds = NULL; if(strip->getIndicies()) { BUFFER_INIT_DESC bufferInds; bufferInds.ElementSize = sizeof(int); bufferInds.InitData = strip->getIndicies(); bufferInds.NumElements = strip->getNrOfIndicies(); bufferInds.Type = INDEX_BUFFER; bufferInds.Usage = BUFFER_DEFAULT; inds = new Buffer(); if(FAILED(inds->Init(Dx_Device, Dx_DeviceContext, bufferInds))) MaloW::Debug("CreateIndsBuffer Failed"); } ID3D11ShaderResourceView* texture = NULL; if(strip->GetTexturePath() != "") { D3DX11_IMAGE_LOAD_INFO loadInfo; ZeroMemory(&loadInfo, sizeof(D3DX11_IMAGE_LOAD_INFO)); loadInfo.BindFlags = D3D11_BIND_SHADER_RESOURCE; loadInfo.Format = DXGI_FORMAT_BC1_UNORM; if(FAILED(D3DX11CreateShaderResourceViewFromFile(Dx_Device, strip->GetTexturePath().c_str(), &loadInfo, NULL, &texture, NULL))) MaloW::Debug("Failed to load texture " + strip->GetTexturePath()); } Object3D* obj = new Object3D(verts, inds, texture, mesh->GetTopology()); strip->SetRenderObject(obj); } mesh->RecreateWorldMatrix(); RendererEvent* re = new RendererEvent("Add Mesh", mesh, NULL); this->PutEvent(re); }
void GraphicsEngineImp::Life() { while(this->stayAlive) { if(MaloW::ProcessEvent* ev = this->WaitEvent()) { this->loading = true; if(dynamic_cast<LoadMeshEvent*>(ev) != NULL) { string filename = ((LoadMeshEvent*)ev)->GetFileName(); if(StaticMesh* mesh = ((LoadMeshEvent*)ev)->GetStaticMesh()) { mesh->LoadFromFile(filename); this->dx->CreateStaticMesh(mesh); if(Material* material = ((LoadMeshEvent*)ev)->GetMaterial()) { MaloW::Array<MeshStrip*>* strips = mesh->GetStrips(); for(int i = 0; i < strips->size(); i++) { strips->get(i)->SetMaterial(material); if(i+1 < strips->size()) material = new Material(material); } } } else if(AnimatedMesh* mesh = ((LoadMeshEvent*)ev)->GetAnimatedMesh()) { //Check if loading the animation from file was successful... if(mesh->LoadFromFile(filename)) { //...and if so, add it this->dx->CreateAnimatedMesh(mesh); } else { //If not, delete the mesh. MaloW::Debug("Warning: Deleting animated mesh because of failure."); delete mesh; } } } delete ev; if(this->GetEventQueueSize() == 0) this->loading = false; } } }
void GraphicsEngineImp::Life() { while(this->stayAlive) { if(MaloW::ProcessEvent* ev = this->WaitEvent()) { this->loading = true; if( LoadMeshEvent* LME = dynamic_cast<LoadMeshEvent*>(ev) ) { string filename = LME->GetFileName(); if(StaticMesh* mesh = LME->GetStaticMesh()) { bool success = mesh->LoadFromFile(filename); if(success) { this->dx->CreateStaticMesh(mesh); } if(Material* material = LME->GetMaterial()) { MaloW::Array<MeshStrip*>* strips = mesh->GetStrips(); for(unsigned int i = 0; i < strips->size(); i++) { strips->get(i)->SetMaterial(material); if(i+1 < strips->size()) material = new Material(material); } } } else if(AnimatedMesh* mesh = LME->GetAnimatedMesh()) { //Check if loading the animation from file was successful... if(mesh->LoadFromFile(filename)) { //...and if so, add it this->dx->CreateAnimatedMesh(mesh); } else { //If not, delete the mesh. MaloW::Debug("Warning: Deleting animated mesh because of failure."); delete mesh; } } else if(FBXMesh* mesh = LME->GetFBXMesh()) { bool success = mesh->LoadFromFile(filename, this->fbx, this->dx->GetDevice(), this->dx->GetContextDevice()); if(success) { this->dx->CreateFBXMesh(mesh); } } } else if(PreLoadEvent* ple = dynamic_cast<PreLoadEvent*>(ev)) { GetResourceManager()->PreLoadResources(ple->GetNrOfResources(), ple->GetResourceFileNames()); } delete ev; if(this->GetEventQueueSize() == 0) this->loading = false; } } }
void Mesh::LoadFromFile(string file) { // if substr of the last 4 = .obj do this: - else load other format / print error ObjLoader oj; ObjData* od = oj.LoadObjFile(file); MaloW::Array<MaterialData>* mats = od->mats; for(int q = 0; q < mats->size(); q++) { bool hasFace = false; MeshStrip* strip = new MeshStrip(); int nrOfVerts = 0; Vertex* tempverts = new Vertex[od->faces->size()*3]; for(int i = 0; i < od->faces->size(); i++) { if(od->faces->get(i).material == mats->get(q).name) { int vertpos = od->faces->get(i).data[0][0] - 1; int textcoord = od->faces->get(i).data[0][1] - 1; int norm = od->faces->get(i).data[0][2] - 1; tempverts[nrOfVerts] = Vertex(od->vertspos->get(vertpos), od->textcoords->get(textcoord), od->vertsnorms->get(norm)); nrOfVerts++; vertpos = od->faces->get(i).data[2][0] - 1; textcoord = od->faces->get(i).data[2][1] - 1; norm = od->faces->get(i).data[2][2] - 1; tempverts[nrOfVerts] = Vertex(od->vertspos->get(vertpos), od->textcoords->get(textcoord), od->vertsnorms->get(norm)); nrOfVerts++; vertpos = od->faces->get(i).data[1][0] - 1; textcoord = od->faces->get(i).data[1][1] - 1; norm = od->faces->get(i).data[1][2] - 1; tempverts[nrOfVerts] = Vertex(od->vertspos->get(vertpos), od->textcoords->get(textcoord), od->vertsnorms->get(norm)); nrOfVerts++; hasFace = true; } } strip->setNrOfVerts(nrOfVerts); Vertex* verts = new Vertex[nrOfVerts]; for(int z = 0; z < nrOfVerts; z++) { verts[z] = tempverts[z]; } delete tempverts; strip->SetVerts(verts); strip->SetTexturePath(od->mats->get(q).texture); Material* mat = new Material(); mat->AmbientColor = od->mats->get(q).ka; if(mat->AmbientColor == D3DXVECTOR3(0.0f, 0.0f, 0.0f)) //////////// MaloW Fix, otherwise completely black with most objs mat->AmbientColor += D3DXVECTOR3(0.2f, 0.2f, 0.2f); //////////// MaloW Fix, otherwise completely black with most objs mat->DiffuseColor = od->mats->get(q).kd; if(mat->DiffuseColor == D3DXVECTOR3(0.0f, 0.0f, 0.0f)) //////////// MaloW Fix, otherwise completely black with most objs mat->DiffuseColor += D3DXVECTOR3(0.6f, 0.6f, 0.6f); //////////// MaloW Fix, otherwise completely black with most objs mat->SpecularColor = od->mats->get(q).ks; strip->SetMaterial(mat); if(hasFace) this->strips->add(strip); else delete strip; } this->topology = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST; delete od; }
bool PhysicsEngine::CollisionWithMapSimple(PowerBall* b, Map* map, Vector3 &normalPlane) { MaloW::Array<MeshStrip*>* temp = map->GetMesh()->GetStrips(); //int sizeMstrip = temp->size(); int sizeVertexS0 = temp->get(0)->getNrOfVerts(); Vertex* verts; //Vector3 origin = this->GetPositionVector3(); Vector3 origin = b->GetTempPosition(); Vector3 dir = b->GetVelocity(); Vector3 dirN = dir/dir.GetLength(); verts = temp->get(0)->getVerts(); /* for(int i = 0;i<sizeMstrip;i++) { } */ Vector3 p0,p1,p2, normal, v1,v2; float smalestTime = -1; bool firstHit = false; float u, v,t; float lengthProjN = 0; Vector3 p0Store, p1Store,p2Store, normalStore; Vector3 pos = Vector3(map->GetMesh()->GetPosition()); Vector3 posS = b->GetTempPosition();//this->GetPositionVector3(); Vector3 rayDirection; Vector3 scalingMesh = map->GetMesh()->GetScaling(); D3DXMATRIX quat; D3DXMatrixRotationQuaternion(&quat, &map->GetMesh()->GetRotation()); Matrix4 rotate(quat); rotate.TransposeThis(); Matrix4 scaling; scaling.SetScale(scalingMesh); Matrix4 translate; translate.SetTranslate(pos); Matrix4 world = translate*rotate*scaling; for(int i =0; i< sizeVertexS0; i+=3) { /* p0 = Vector3(verts[i].pos).GetComponentMultiplication(scalingMesh) + pos; p1 = Vector3(verts[i+1].pos).GetComponentMultiplication(scalingMesh) +pos; p2 = Vector3(verts[i+2].pos).GetComponentMultiplication(scalingMesh) + pos; */ p0 = world*Vector3(verts[i].pos); p1 = world*Vector3(verts[i+1].pos); p2 = world*Vector3(verts[i+2].pos); v1 = p1-p0; v2 = p2-p0; rayDirection = v1.GetCrossProduct(v2); rayDirection.normalize(); float tempLength; Vector3 ny; Vector3 projN; if(RayTriIntersect(origin , rayDirection, p0, p1, p2, u, v, t) ) { normal = rayDirection; ny = origin - p0; projN = normal*ny.GetDotProduct(normal); tempLength = projN.GetLength(); if(!firstHit) { firstHit = true; smalestTime = t; lengthProjN = tempLength; p0Store = p0; p1Store = p1; p2Store = p2; normalStore = normal; } else { if( tempLength < lengthProjN ) { smalestTime = t; lengthProjN = tempLength; p0Store = p0; p1Store = p1; p2Store = p2; normalStore = normal; } } } // check agains all edges Vector3 lineDirection; float scalarProj; Vector3 projOnLine; Vector3 normalToLine; // edge 1: ny = origin - p0; lineDirection = p1 - p0; scalarProj = (ny.GetDotProduct(lineDirection)/lineDirection.GetLengthSquared()); projOnLine = lineDirection * scalarProj; if( (scalarProj >= 0.0f) && (scalarProj <= 1) ) { normalToLine = ny - projOnLine; tempLength = normalToLine.GetLength(); if(!firstHit) { firstHit = true; lengthProjN = tempLength; p0Store = p0; p1Store = p1; p2Store = p2; normalStore = normalToLine; normalStore.normalize(); } else { if( tempLength < lengthProjN ) { lengthProjN = tempLength; p0Store = p0; p1Store = p1; p2Store = p2; normalStore = normalToLine; normalStore.normalize(); } } } // edge 2: ny = origin - p1; lineDirection = p2 - p1; scalarProj = (ny.GetDotProduct(lineDirection)/lineDirection.GetLengthSquared()); projOnLine = lineDirection * scalarProj; if( (scalarProj >= 0.0f) && (scalarProj <= 1) ) { normalToLine = ny - projOnLine; tempLength = normalToLine.GetLength(); if(!firstHit) { firstHit = true; lengthProjN = tempLength; p0Store = p0; p1Store = p1; p2Store = p2; normalStore = normalToLine; normalStore.normalize(); } else { if( tempLength < lengthProjN ) { lengthProjN = tempLength; p0Store = p0; p1Store = p1; p2Store = p2; normalStore = normalToLine; normalStore.normalize(); } } } // edge 3: ny = origin - p2; lineDirection = p0 - p2; scalarProj = (ny.GetDotProduct(lineDirection)/lineDirection.GetLengthSquared()); projOnLine = lineDirection * scalarProj; if( (scalarProj >= 0.0f) && (scalarProj <= 1) ) { normalToLine = ny - projOnLine; tempLength = normalToLine.GetLength(); if(!firstHit) { firstHit = true; lengthProjN = tempLength; p0Store = p0; p1Store = p1; p2Store = p2; normalStore = normalToLine; normalStore.normalize(); } else { if( tempLength < lengthProjN ) { lengthProjN = tempLength; p0Store = p0; p1Store = p1; p2Store = p2; normalStore = normal; } } } } if(firstHit) { // for checking if the ball are in the air not turned on at the moment, float eps = 0.5f; //0.001 if( (lengthProjN < (b->GetRadius() + eps)) && (lengthProjN > (b->GetRadius() - eps)) ) { b->SetNormalContact(normalStore); b->SetHasContact(true); } else { b->SetNormalContact(normalStore); b->SetHasContact(false); } if( lengthProjN <= b->GetRadius()) { Vector3 velNorm = b->GetVelocity(); velNorm.normalize(); if(normalStore.GetDotProduct(velNorm) >=0) return false; float diff = abs(lengthProjN- b->GetRadius()); //Vector3 newPo = origin -dirN*diff; //Vector3 projVel = normalStore * this->mVelocity.GetDotProduct(normalStore); Vector3 newPo = origin + normalStore*diff; /* if( projVel.GetDotProduct(normalStore) < 0.0f) { newPo = origin - normalStore*diff; return false; } else newPo = origin + normalStore*diff; */ //this->SetPosition(newPo); b->SetTempPosition(newPo); normalPlane = normalStore; //this->mNormalContact = normalPlane; //this->mHasContact = true; return true; } else { normalPlane = Vector3(0,0,0); //this->mNormalContact = normalPlane; //this->mHasContact = false; return false; } } normalPlane = Vector3(0,0,0); b->SetNormalContact(normalPlane); //this->mHasContact = false; return false; }
void AnimatedMesh::LoadFromFile(string file) { // if substr of the last 4 = .obj do this: - else load other format / print error ObjLoader oj; // Get the directory correct string tempFilename = file; string pathfolder = ""; size_t slashpos = tempFilename.find("/"); while(slashpos != string::npos) { slashpos = tempFilename.find("/"); pathfolder += tempFilename.substr(0, slashpos+1); tempFilename = tempFilename.substr(slashpos + 1); } ifstream anifile; anifile.open(file); if(anifile) { string line = ""; getline(anifile, line); int nrOfKeyframes = atoi(line.c_str()); for(int a = 0; a < nrOfKeyframes; a++) { int time = 0; string path = ""; getline(anifile, line); time = atoi(line.c_str()); getline(anifile, path); KeyFrame* frame = new KeyFrame(); frame->time = time; { ObjData* od = oj.LoadObjFile(pathfolder + path); MaloW::Array<MaterialData>* mats = od->mats; for(int q = 0; q < mats->size(); q++) { bool hasFace = false; MeshStrip* strip = new MeshStrip(); int nrOfVerts = 0; Vertex* tempverts = new Vertex[od->faces->size()*3]; for(int i = 0; i < od->faces->size(); i++) { if(od->faces->get(i).material == mats->get(q).name) { int vertpos = od->faces->get(i).data[0][0] - 1; int textcoord = od->faces->get(i).data[0][1] - 1; int norm = od->faces->get(i).data[0][2] - 1; tempverts[nrOfVerts] = Vertex(od->vertspos->get(vertpos), od->textcoords->get(textcoord), od->vertsnorms->get(norm)); nrOfVerts++; vertpos = od->faces->get(i).data[2][0] - 1; textcoord = od->faces->get(i).data[2][1] - 1; norm = od->faces->get(i).data[2][2] - 1; tempverts[nrOfVerts] = Vertex(od->vertspos->get(vertpos), od->textcoords->get(textcoord), od->vertsnorms->get(norm)); nrOfVerts++; vertpos = od->faces->get(i).data[1][0] - 1; textcoord = od->faces->get(i).data[1][1] - 1; norm = od->faces->get(i).data[1][2] - 1; tempverts[nrOfVerts] = Vertex(od->vertspos->get(vertpos), od->textcoords->get(textcoord), od->vertsnorms->get(norm)); nrOfVerts++; hasFace = true; } } strip->setNrOfVerts(nrOfVerts); Vertex* verts = new Vertex[nrOfVerts]; for(int z = 0; z < nrOfVerts; z++) { verts[z] = tempverts[z]; } delete tempverts; strip->SetVerts(verts); strip->SetTexturePath(od->mats->get(q).texture); Material* mat = new Material(); mat->AmbientColor = od->mats->get(q).ka; if(mat->AmbientColor == D3DXVECTOR3(0.0f, 0.0f, 0.0f)) //////////// MaloW Fix, otherwise completely black with most objs mat->AmbientColor += D3DXVECTOR3(0.2f, 0.2f, 0.2f); //////////// MaloW Fix, otherwise completely black with most objs mat->DiffuseColor = od->mats->get(q).kd; if(mat->DiffuseColor == D3DXVECTOR3(0.0f, 0.0f, 0.0f)) //////////// MaloW Fix, otherwise completely black with most objs mat->DiffuseColor += D3DXVECTOR3(0.6f, 0.6f, 0.6f); //////////// MaloW Fix, otherwise completely black with most objs mat->SpecularColor = od->mats->get(q).ks; strip->SetMaterial(mat); if(hasFace) frame->strips->add(strip); else delete strip; } this->topology = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST; delete od; } this->mKeyFrames->add(frame); } } else MaloW::Debug("Failed to open AnimatedMesh: " + file); }
void DxManager::CalculateCulling() { D3DXMATRIX view = this->camera->GetViewMatrix(); D3DXMATRIX proj = this->camera->GetProjectionMatrix(); /* float zMinimum = -proj._43 / proj._33; float r = this->params.FarClip / (this->params.FarClip - zMinimum); proj._33 = r; proj._43 = -r * zMinimum; */ D3DXMATRIX VP; D3DXMatrixMultiply(&VP, &view, &proj); // Calculate near plane of frustum. FrustrumPlanes[0].a = VP._14 + VP._13; FrustrumPlanes[0].b = VP._24 + VP._23; FrustrumPlanes[0].c = VP._34 + VP._33; FrustrumPlanes[0].d = VP._44 + VP._43; D3DXPlaneNormalize(&FrustrumPlanes[0], &FrustrumPlanes[0]); // Calculate far plane of frustum. FrustrumPlanes[1].a = VP._14 - VP._13; FrustrumPlanes[1].b = VP._24 - VP._23; FrustrumPlanes[1].c = VP._34 - VP._33; FrustrumPlanes[1].d = VP._44 - VP._43; D3DXPlaneNormalize(&FrustrumPlanes[1], &FrustrumPlanes[1]); // Calculate left plane of frustum. FrustrumPlanes[2].a = VP._14 + VP._11; FrustrumPlanes[2].b = VP._24 + VP._21; FrustrumPlanes[2].c = VP._34 + VP._31; FrustrumPlanes[2].d = VP._44 + VP._41; D3DXPlaneNormalize(&FrustrumPlanes[2], &FrustrumPlanes[2]); // Calculate right plane of frustum. FrustrumPlanes[3].a = VP._14 - VP._11; FrustrumPlanes[3].b = VP._24 - VP._21; FrustrumPlanes[3].c = VP._34 - VP._31; FrustrumPlanes[3].d = VP._44 - VP._41; D3DXPlaneNormalize(&FrustrumPlanes[3], &FrustrumPlanes[3]); // Calculate top plane of frustum. FrustrumPlanes[4].a = VP._14 - VP._12; FrustrumPlanes[4].b = VP._24 - VP._22; FrustrumPlanes[4].c = VP._34 - VP._32; FrustrumPlanes[4].d = VP._44 - VP._42; D3DXPlaneNormalize(&FrustrumPlanes[4], &FrustrumPlanes[4]); // Calculate bottom plane of frustum. FrustrumPlanes[5].a = VP._14 + VP._12; FrustrumPlanes[5].b = VP._24 + VP._22; FrustrumPlanes[5].c = VP._34 + VP._32; FrustrumPlanes[5].d = VP._44 + VP._42; D3DXPlaneNormalize(&FrustrumPlanes[5], &FrustrumPlanes[5]); //Terrain for(int i = 0; i < this->terrains.size(); i++) { Terrain* terr = this->terrains.get(i); float scale = max(terr->GetScale().x, max(terr->GetScale().y, terr->GetScale().z)); if(pe.FrustrumVsSphere(this->FrustrumPlanes, terr->GetBoundingSphere(), terr->GetWorldMatrix(), scale)) { terr->SetCulled(false); } else { terr->SetCulled(true); } } //Static meshes for(int i = 0; i < this->objects.size(); i++) { StaticMesh* ms = this->objects.get(i); MaloW::Array<MeshStrip*>* strips = ms->GetStrips(); for(int u = 0; u < strips->size(); u++) { MeshStrip* s = strips->get(u); float scale = max(ms->GetScaling().x, max(ms->GetScaling().y, ms->GetScaling().z)); if(pe.FrustrumVsSphere(this->FrustrumPlanes, s->GetBoundingSphere(), ms->GetWorldMatrix(), scale)) { s->SetCulled(false); } else { s->SetCulled(true); } } } //Animated meshes for(int i = 0; i < this->animations.size(); i++) { AnimatedMesh* ms = this->animations.get(i); MeshStrip* s = ms->GetStrips()->get(0); float scale = max(ms->GetScaling().x, max(ms->GetScaling().y, ms->GetScaling().z)); if(pe.FrustrumVsSphere(this->FrustrumPlanes, s->GetBoundingSphere(), ms->GetWorldMatrix(), scale)) { s->SetCulled(false); } else { s->SetCulled(true); } } }
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 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()); */ }