void SetMatrices(BaseCamera* pCam, const D3DXMATRIX& mProj) { D3DXMATRIX mView; memcpy(&mView, pCam->getViewMatrix(), sizeof(float) * 16); // Set matrices D3DXMATRIX mWorld, mScale, mTrans; D3DXMatrixScaling(&mScale, WORLD_SCALE, WORLD_SCALE, WORLD_SCALE); // We keep the eye centered in the middle of the tile rings. The height map scrolls in the horizontal // plane instead. D3DXVECTOR3 eye; pCam->getFrom(&eye[0]); float snappedX = eye.x, snappedZ = eye.z; if (SNAP_GRID_SIZE > 0) { snappedX = floorf(snappedX / SNAP_GRID_SIZE) * SNAP_GRID_SIZE; snappedZ = floorf(snappedZ / SNAP_GRID_SIZE) * SNAP_GRID_SIZE; } const float dx = eye.x - snappedX; const float dz = eye.z - snappedZ; snappedX = eye.x - 2*dx; // Why the 2x? I'm confused. But it works. snappedZ = eye.z - 2*dz; D3DXMatrixTranslation(&mTrans, snappedX, 0, snappedZ); D3DXMatrixMultiply(&mWorld, &mScale, &mTrans); ID3DX11EffectMatrixVariable* pmWorldViewProj = g_pTerrainEffect->GetVariableByName("g_WorldViewProj")->AsMatrix(); ID3DX11EffectMatrixVariable* pmProj = g_pTerrainEffect->GetVariableByName("g_Proj")->AsMatrix(); ID3DX11EffectMatrixVariable* pmWorldViewProjLOD = g_pTerrainEffect->GetVariableByName("g_WorldViewProjLOD")->AsMatrix(); ID3DX11EffectMatrixVariable* pmWorldViewLOD = g_pTerrainEffect->GetVariableByName("g_WorldViewLOD")->AsMatrix(); assert(pmProj->IsValid()); assert(pmWorldViewProj->IsValid()); assert(pmWorldViewProjLOD->IsValid()); assert(pmWorldViewLOD->IsValid()); D3DXMATRIX mWorldView = mWorld * mView; D3DXMATRIX mWorldViewProj = mWorldView * mProj; pmWorldViewProj->SetMatrix((float*) &mWorldViewProj); pmProj->SetMatrix((float*) &mProj); // For LOD calculations, we always use the master camera's view matrix. D3DXMATRIX mWorldViewLOD = mWorld * mView; D3DXMATRIX mWorldViewProjLOD = mWorldViewLOD * mProj; pmWorldViewProjLOD->SetMatrix((float*) &mWorldViewProjLOD); pmWorldViewLOD->SetMatrix((float*) &mWorldViewLOD); // Due to the snapping tricks, the centre of projection moves by a small amount in the range ([0,2*dx],[0,2*dz]) // relative to the terrain. For frustum culling, we need this eye position. D3DXVECTOR3 cullingEye = eye; cullingEye.x -= snappedX; cullingEye.z -= snappedZ; g_pEyePosVar->SetFloatVector(cullingEye); g_pViewDirVar->SetFloatVector((float*)&mView._31); }
void Effect::AddVariable(ID3DX11EffectVariable* var, const D3DX11_EFFECT_VARIABLE_DESC* varDesc, const D3DX11_EFFECT_TYPE_DESC* typeDesc) { _ASSERT(var && varDesc && typeDesc); switch (typeDesc->Class) { case D3D_SVC_SCALAR : { ID3DX11EffectScalarVariable* scalarVar = var->AsScalar(); _ASSERT(scalarVar->IsValid()); scalarVars[varDesc->Name] = scalarVar; break; } case D3D_SVC_VECTOR : { ID3DX11EffectVectorVariable* vectorVar = var->AsVector(); _ASSERT(vectorVar->IsValid()); vectorVars[varDesc->Name] = vectorVar; break; } case D3D_SVC_MATRIX_COLUMNS: case D3D_SVC_MATRIX_ROWS: { ID3DX11EffectMatrixVariable* matrixVar = var->AsMatrix(); _ASSERT(matrixVar->IsValid()); matrixVars[varDesc->Name] = matrixVar; break; } case D3D_SVC_STRUCT: structVars[varDesc->Name] = var; break; case D3D_SVC_OBJECT: { ID3DX11EffectShaderResourceVariable* resourceVar = var->AsShaderResource(); if (resourceVar->IsValid()) { resourceVars[varDesc->Name] = resourceVar; } break; } default: _ASSERT(false); } }
void DX11Shader::setMatrix(const char* strName, glm::mat4x4 Matrix) { ID3DX11EffectMatrixVariable* pMatrixVariable; pMatrixVariable = m_pEffect->pEffect->GetVariableByName(strName)->AsMatrix(); if (pMatrixVariable->IsValid()) { pMatrixVariable->SetMatrix(glm::value_ptr(Matrix)); } else { EventManager::Instance().postMessage("Unknown matrix variable: %s in shader: %s", strName, m_pEffect->strName); } }
void EffectSystemD3D11::UpdateMatrix(int effectID, const char *name, const void *data) { ASSERT_EFFECT(effectID); ID3DX11EffectMatrixVariable *variable = nullptr; variable = mEffects[effectID]->GetVariableByName(name)->AsMatrix(); if (variable->IsValid()) { variable->SetMatrix((float*)data); } else { LogSystem::GetInstance().Log("effect matrix「%s」非法", name); return; } }