void Lighting::ComputeSpotMatrix(RenderLight *Light) { D3DXVECTOR3 Test,At,Up,Pos; D3DXMATRIX MatTemp,MatTex,MatWorld,MatView, MatProj; D3DXMATRIX MatWorldViewProj; Pos.x = Light->m_Pos.x; Pos.y = Light->m_Pos.y; Pos.z = Light->m_Pos.z; At.x = Pos.x - Light->m_Dir.x; At.y = Pos.y - Light->m_Dir.y; At.z = Pos.z - Light->m_Dir.z; Up.x = 0.0f; Up.y = 1.0f; Up.z = 0.0f; D3DXMatrixLookAtLH(&MatView,&Pos,&At,&Up); D3DXMatrixPerspectiveFovLH(&MatProj, DEG_RAD(Light->m_Angle), 1.0f, 1.0f, 50.0f); m_Device->GetTransform(D3DTS_WORLD,&MatWorld); D3DXMatrixMultiply(&MatTemp,&MatWorld,&MatView); D3DXMatrixMultiply(&MatWorldViewProj,&MatTemp,&MatProj); D3DXMatrixIdentity(&MatTex); D3DXMatrixMultiply(&MatTex,&MatWorldViewProj,&m_MatScaleBias); D3DXMatrixTranspose(&MatTex,&MatTex); m_Device->SetVertexShaderConstantF(CV_TEXMAT_0,(float*)&MatTex(0,0),4); }
bool RenderMesh::Evaluate(IDirect3DDevice8 *Device, Mesh *aMesh, int MatIndex, bool NegScale) { int i,j; unsigned short *Indice; SHADERVERTEX *Vertices; Tab<Vert3> Verts; Tab<Face3> Faces; float U,V; int A,B,C; if(!Device || !aMesh) { SAFE_RELEASE(m_VB); SAFE_RELEASE(m_IB); m_Valid = false; return(false); } if(!m_Valid) { SAFE_RELEASE(m_VB); SAFE_RELEASE(m_IB); ConvertFaces(aMesh,MatIndex,Verts,Faces,NegScale); m_NumVert = Verts.Count(); m_NumFace = Faces.Count(); if(m_NumVert == 0 || m_NumFace == 0) { m_Valid = true; return(true); } if(FAILED(Device->CreateVertexBuffer(m_NumVert * sizeof(SHADERVERTEX), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED,&m_VB))) { goto FAILED; } if(FAILED(Device->CreateIndexBuffer(m_NumFace * 3 * sizeof(unsigned short), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED,&m_IB))) { goto FAILED; } if(FAILED(m_IB->Lock(0,m_NumFace * 3 * sizeof(unsigned short), (BYTE**)&Indice,D3DLOCK_DISCARD))) { goto FAILED; } if(FAILED(m_VB->Lock(0,0,(BYTE**)&Vertices,D3DLOCK_DISCARD))) { goto FAILED; } for(i=0; i < m_NumVert; i++) { Vertices[i].m_Pos = Verts[i].m_Pos; Vertices[i].m_Normal = Verts[i].m_Normal; Vertices[i].m_S = Verts[i].m_S; for(j=0; j < MAX_TMUS; j++) { U = Verts[i].m_UV[j].x; V = Verts[i].m_UV[j].y; U = -U; Rotate2DPoint(U,V,DEG_RAD(180.0f)); Vertices[i].m_UV[j].x = U; Vertices[i].m_UV[j].y = V; } } for(i=0; i < m_NumFace; i++) { A = Faces[i].m_Num[0]; B = Faces[i].m_Num[1]; C = Faces[i].m_Num[2]; Indice[i * 3 + 0] = A; Indice[i * 3 + 1] = B; Indice[i * 3 + 2] = C; } m_VB->Unlock(); m_IB->Unlock(); m_Valid = true; return(true); FAILED: SAFE_RELEASE(m_VB); SAFE_RELEASE(m_IB); m_Valid = false; return(false); } return(true); }
void RenderMesh::ComputeVertexNormals(Mesh *aMesh, Tab<BasisVert> &FNorms, Tab<VNormal> &VNorms, bool NegScale) { Face *Face; Point3 *Verts; Point3 Vt0,Vt1,Vt2; Point3 Normal; int i,j,k,A,B,C; int NumUV,NumVert,NumFace; UVVert *UVVert; TVFace *UVFace; Vert3 Vert[3]; Point3 S,T; Point3 Edge01,Edge02; Point3 Cp; float U0,V0,U1,V1,U2,V2; int UVCount; unsigned long Sg; NumUV = aMesh->getNumMaps(); NumVert = aMesh->getNumVerts(); NumFace = aMesh->getNumFaces(); Face = aMesh->faces; Verts = aMesh->verts; VNorms.SetCount(NumVert); FNorms.SetCount(NumFace); if(NumUV > MAX_TMUS + 1) { NumUV = MAX_TMUS + 1; } for(i=0; i < NumVert; i++) { VNorms[i].Clear(); } for(i=0; i < NumFace; i++, Face++) { A = Face->v[gVIndex[0]]; B = Face->v[gVIndex[1]]; C = Face->v[gVIndex[2]]; Vt0 = Verts[A]; Vt1 = Verts[B]; Vt2 = Verts[C]; Normal = (Vt1 - Vt0) ^ (Vt2 - Vt0); Point3Normalize(Normal); for(j=0; j < 3; j++) { UVCount = 0; for(k=0;k<m_MapChannels.Count();k++) { int index = m_MapChannels[k]; if(aMesh->getNumMapVerts(index)) { UVVert = aMesh->mapVerts(index); UVFace = aMesh->mapFaces(index); Vert[j].m_UV[k].x = UVVert[UVFace[i].t[gVIndex[j]]].x; Vert[j].m_UV[k].y = UVVert[UVFace[i].t[gVIndex[j]]].y; } else { Vert[j].m_UV[k].x = 0.0f; Vert[j].m_UV[k].y = 0.0f; } } } S.Set(0.0f,0.0f,0.0f); T.Set(0.0f,0.0f,0.0f); U0 = -Vert[0].m_UV[NORMAL_UV].x; V0 = Vert[0].m_UV[NORMAL_UV].y; Rotate2DPoint(U0,V0,DEG_RAD(180.0f)); U1 = -Vert[1].m_UV[NORMAL_UV].x; V1 = Vert[1].m_UV[NORMAL_UV].y; Rotate2DPoint(U1,V1,DEG_RAD(180.0f)); U2 = -Vert[2].m_UV[NORMAL_UV].x; V2 = Vert[2].m_UV[NORMAL_UV].y; Rotate2DPoint(U2,V2,DEG_RAD(180.0f)); // x, s, t Edge01 = Point3(Vt1.x - Vt0.x, U1 - U0, V1 - V0); Edge02 = Point3(Vt2.x - Vt0.x, U2 - U0, V2 - V0); Cp = CrossProd(Edge01,Edge02); Point3Normalize(Cp); if(fabs(Cp.x) > 0.0001f) { S.x = -Cp.y / Cp.x; T.x = -Cp.z / Cp.x; } // y, s, t Edge01 = Point3(Vt1.y - Vt0.y, U1 - U0, V1 - V0); Edge02 = Point3(Vt2.y - Vt0.y, U2 - U0, V2 - V0); Cp = CrossProd(Edge01,Edge02); Point3Normalize(Cp); if(fabs(Cp.x) > 0.0001f) { S.y = -Cp.y / Cp.x; T.y = -Cp.z / Cp.x; } // z, s, t Edge01 = Point3(Vt1.z - Vt0.z, U1 - U0, V1 - V0); Edge02 = Point3(Vt2.z - Vt0.z, U2 - U0, V2 - V0); Cp = CrossProd(Edge01,Edge02); Point3Normalize(Cp); if(fabs(Cp.x) > 0.0001f) { S.z = -Cp.y / Cp.x; T.z = -Cp.z / Cp.x; } Point3Normalize(S); Point3Normalize(T); Sg = Face->smGroup; if(Sg) { VNorms[A].AddNormal(Normal,Sg,S,T); VNorms[B].AddNormal(Normal,Sg,S,T); VNorms[C].AddNormal(Normal,Sg,S,T); } else { T = CrossProd(S,Normal); Point3Normalize(T); S = CrossProd(Normal,T); Point3Normalize(S); FNorms[i].m_Normal = Normal; FNorms[i].m_S = S; FNorms[i].m_T = T; FNorms[i].m_SxT = Normal; } } for(i=0; i < NumVert; i++) { VNorms[i].Normalize(); } }