bool CModel::LoadSourceFile() { #ifdef TINKER_NO_TOOLS return false; #else CConversionScene* pScene = new CConversionScene(); CModelConverter c(pScene); if (!c.ReadModel(m_sFilename)) { delete pScene; return false; } g_asTextures.clear(); g_aaflData.clear(); g_aabbVisBounds = AABB(Vector(999, 999, 999), Vector(-999, -999, -999)); g_aabbPhysBounds = AABB(Vector(999, 999, 999), Vector(-999, -999, -999)); LoadSceneIntoToy(pScene); size_t iTextures = g_asTextures.size(); m_ahMaterials.resize(iTextures); m_aiVertexBuffers.resize(iTextures); m_aiVertexBufferSizes.resize(iTextures); for (size_t i = 0; i < iTextures; i++) { if (g_aaflData[i].size() == 0) continue; m_aiVertexBuffers[i] = CRenderer::LoadVertexDataIntoGL(g_aaflData[i].size()*4, &g_aaflData[i][0]); m_aiVertexBufferSizes[i] = g_aaflData[i].size()/FIXED_FLOATS_PER_VERTEX; CData oMaterialData; CData* pShader = oMaterialData.AddChild("Shader", "model"); pShader->AddChild("DiffuseTexture", g_asTextures[i]); m_ahMaterials[i] = CMaterialLibrary::AddMaterial(&oMaterialData); //TAssert(m_aiMaterials[i]); if (!m_ahMaterials[i].IsValid()) TError(tstring("Couldn't create fake material for texture \"") + g_asTextures[i] + "\"\n"); } m_aabbVisBoundingBox = g_aabbVisBounds; m_aabbPhysBoundingBox = g_aabbPhysBounds; delete pScene; return true; #endif }
tvector<itemType>::tvector(const tvector<itemType> & vec) // postcondition: vector is a copy of vec : mySize(vec.size()), myCapacity(vec.capacity()), myList(new itemType[myCapacity]) { int k; // copy elements for(k = 0; k < mySize; k++){ myList[k] = vec.myList[k]; } }
tvector<nr_type_t> operator * (tvector<nr_type_t> a, tmatrix<nr_type_t> b) { assert (a.size () == b.getRows ()); int r, c, n = b.getRows (); nr_type_t z; tvector<nr_type_t> res (n); for (c = 0; c < n; c++) { for (r = 0, z = 0; r < n; r++) z += a.get (r) * b.get (r, c); res.set (c, z); } return res; }
void LoadMeshInstanceIntoToy(CConversionScene* pScene, CConversionMeshInstance* pMeshInstance, const Matrix4x4& mParentTransformations) { if (!pMeshInstance->IsVisible()) return; CConversionMesh* pMesh = pMeshInstance->GetMesh(); for (size_t m = 0; m < pScene->GetNumMaterials(); m++) { for (size_t j = 0; j < pMesh->GetNumFaces(); j++) { size_t k; CConversionFace* pFace = pMesh->GetFace(j); if (pFace->m == ~0) continue; CConversionMaterial* pMaterial = NULL; CConversionMaterialMap* pConversionMaterialMap = pMeshInstance->GetMappedMaterial(pFace->m); if (!pConversionMaterialMap) continue; if (!pConversionMaterialMap->IsVisible()) continue; if (pConversionMaterialMap->m_iMaterial != m) continue; while (g_asTextures.size() <= pConversionMaterialMap->m_iMaterial) { g_asTextures.push_back(pScene->GetMaterial(pConversionMaterialMap->m_iMaterial)->GetDiffuseTexture()); g_aaflData.push_back(); } size_t iMaterial = pConversionMaterialMap->m_iMaterial; CConversionVertex* pVertex0 = pFace->GetVertex(0); for (k = 2; k < pFace->GetNumVertices(); k++) { CConversionVertex* pVertex1 = pFace->GetVertex(k-1); CConversionVertex* pVertex2 = pFace->GetVertex(k); AddVertex(iMaterial, mParentTransformations * pMesh->GetVertex(pVertex0->v), pMesh->GetUV(pVertex0->vu)); AddVertex(iMaterial, mParentTransformations * pMesh->GetVertex(pVertex1->v), pMesh->GetUV(pVertex1->vu)); AddVertex(iMaterial, mParentTransformations * pMesh->GetVertex(pVertex2->v), pMesh->GetUV(pVertex2->vu)); } } } }
void KickPlayer(class CCommand* pCommand, tvector<tstring>& asTokens, const tstring& sCommand) { if (!asTokens.size()) return; GameNetwork()->DisconnectClient(stoi(asTokens[0])); }
size_t FindEar(const tvector<vec3>& avecPoints) { size_t iPoints = avecPoints.size(); // A triangle is always an ear. if (iPoints <= 3) return 0; size_t i; vec3 vecFaceNormal; // Calculate the face normal. for (i = 0; i < iPoints; i++) { size_t iNext = (i+1)%iPoints; vec3 vecPoint = avecPoints[i]; vec3 vecNextPoint = avecPoints[iNext]; vecFaceNormal.x += (vecPoint.y - vecNextPoint.y) * (vecPoint.z + vecNextPoint.z); vecFaceNormal.y += (vecPoint.z - vecNextPoint.z) * (vecPoint.x + vecNextPoint.x); vecFaceNormal.z += (vecPoint.x - vecNextPoint.x) * (vecPoint.y + vecNextPoint.y); } vecFaceNormal.Normalize(); for (i = 0; i < iPoints; i++) { size_t iLast = i==0?iPoints-1:i-1; size_t iNext = i==iPoints-1?0:i+1; vec3 vecLast = avecPoints[iLast]; vec3 vecThis = avecPoints[i]; vec3 vecNext = avecPoints[iNext]; // Concave ones can not be ears. if ((vecLast-vecThis).Cross(vecLast-vecNext).Dot(vecFaceNormal) < 0) continue; bool bFoundPoint = false; for (size_t j = 0; j < iPoints; j++) { if (j == i || j == iLast || j == iNext) continue; if (PointInTriangle(avecPoints[j], vecLast, vecThis, vecNext)) { bFoundPoint = true; break; } } if (!bFoundPoint) return i; } return 0; }
void CApplication::ProcessJoystickInput() { if (g_aJoysticks.size() != MAX_JOYSTICKS) return; for (size_t i = 0; i < MAX_JOYSTICKS; i++) { CJoystick& oJoystick = g_aJoysticks[i]; if (!oJoystick.m_bPresent) continue; static tvector<float> aflAxis; aflAxis.resize(oJoystick.m_aflAxis.size()); glfwGetJoystickPos(i, &aflAxis[0], oJoystick.m_aflAxis.size()); for (size_t j = 0; j < oJoystick.m_aflAxis.size(); j++) { if (aflAxis[j] != oJoystick.m_aflAxis[j]) JoystickAxis(i, j, aflAxis[j], aflAxis[j]-oJoystick.m_aflAxis[j]); } oJoystick.m_aflAxis = aflAxis; static tvector<unsigned char> aiButtons; aiButtons.resize(oJoystick.m_iButtons); glfwGetJoystickButtons(i, &aiButtons[0], oJoystick.m_iButtons); for (size_t j = 0; j < oJoystick.m_iButtons; j++) { unsigned long long iButtonMask = (1<<j); if (aiButtons[j] == GLFW_PRESS && !(oJoystick.m_aiButtonStates&iButtonMask)) JoystickButtonPress(i, MapJoystickKey(j)); else if (aiButtons[j] == GLFW_RELEASE && (oJoystick.m_aiButtonStates&iButtonMask)) JoystickButtonRelease(i, MapJoystickKey(j)); if (aiButtons[j] == GLFW_PRESS) oJoystick.m_aiButtonStates |= iButtonMask; else oJoystick.m_aiButtonStates &= ~iButtonMask; } } }
void SetCVar(CCommand* pCommand, tvector<tstring>& asTokens, const tstring& sCommand) { CVar* pCVar = dynamic_cast<CVar*>(pCommand); TAssert(pCVar); if (!pCVar) return; if (asTokens.size() > 1) pCVar->SetValue(asTokens[1]); TMsg(sprintf(tstring("%s = %s\n"), pCVar->GetName().c_str(), pCVar->GetValue().c_str())); }
void CGameServer::DestroyAllEntities(const tvector<tstring>& asSpare, bool bRemakeGame) { if (!GameNetwork()->IsHost() && !IsLoading()) return; if (m_pWorkListener) m_pWorkListener->SetAction("Locating dead nodes", GameServer()->GetMaxEntities()); for (size_t i = 0; i < GameServer()->GetMaxEntities(); i++) { CBaseEntity* pEntity = CBaseEntity::GetEntity(i); if (!pEntity) continue; bool bSpare = false; for (size_t j = 0; j < asSpare.size(); j++) { if (asSpare[j] == pEntity->GetClassName()) { bSpare = true; break; } } if (bSpare) continue; pEntity->Delete(); if (m_pWorkListener) m_pWorkListener->WorkProgress(i); } if (m_pWorkListener) m_pWorkListener->SetAction("Clearing buffers", GameServer()->m_ahDeletedEntities.size()); for (size_t i = 0; i < GameServer()->m_ahDeletedEntities.size(); i++) { delete GameServer()->m_ahDeletedEntities[i]; if (m_pWorkListener) m_pWorkListener->WorkProgress(i); } GameServer()->m_ahDeletedEntities.clear(); if (CBaseEntity::GetNumEntities() == 0) CBaseEntity::s_iNextEntityListIndex = 0; if (bRemakeGame && GameNetwork()->IsHost()) m_hGame = CreateGame(); }
float DistanceToPolygon(const vec3& p, tvector<vec3>& v, vec3 n) { float flPlaneDistance = DistanceToPlane(p, v[0], n); size_t i; bool bFoundPoint = false; for (i = 0; i < v.size()-2; i++) { if (PointInTriangle(p, v[0], v[i+1], v[i+2])) { bFoundPoint = true; break; } } if (bFoundPoint) return flPlaneDistance; float flClosestPoint = -1; for (i = 0; i < v.size(); i++) { float flPointDistance = (v[i] - p).Length(); if (flClosestPoint == -1 || (flPointDistance < flClosestPoint)) flClosestPoint = flPointDistance; float flLineDistance; if (i == v.size() - 1) flLineDistance = DistanceToLineSegment(p, v[i], v[0]); else flLineDistance = DistanceToLineSegment(p, v[i], v[i+1]); if (flClosestPoint == -1 || (flLineDistance < flClosestPoint)) flClosestPoint = flLineDistance; } return flClosestPoint; }
bool CModel::Load(class CConversionScene* pScene, size_t iMesh) { g_aaflData.clear(); LoadMesh(pScene, iMesh); size_t iMaterials = g_aaflData.size(); for (size_t i = 0; i < iMaterials; i++) { if (g_aaflData[i].size() == 0) continue; m_aiVertexBuffers.push_back(CRenderer::LoadVertexDataIntoGL(g_aaflData[i].size()*4, &g_aaflData[i][0])); m_aiVertexBufferSizes.push_back(g_aaflData[i].size()/FIXED_FLOATS_PER_VERTEX); } m_aabbVisBoundingBox = g_aabbVisBounds; m_aabbPhysBoundingBox = g_aabbPhysBounds; return true; }
const tvector<itemType> & tvector<itemType>::operator = (const tvector<itemType> & rhs) // postcondition: normal assignment via copying has been performed; // if vector and rhs were different sizes, vector // has been resized to match the size of rhs { if (this != &rhs) // don't assign to self! { delete [] myList; // get rid of old storage myCapacity = rhs.capacity(); mySize = rhs.size(); myList = new itemType [myCapacity]; // allocate new storage // copy rhs int k; for(k=0; k < mySize; k++) { myList[k] = rhs.myList[k]; } } return *this; // permit a = b = c = d }
void FireInput(class CCommand* pCommand, tvector<tstring>& asTokens, const tstring& sCommand) { if (!CVar::GetCVarBool("cheats")) return; if (asTokens.size() < 3) { TMsg("Format: ent_input entityname input [optional args]\n"); return; } tvector<CBaseEntity*> apEntities; CBaseEntity::FindEntitiesByName(asTokens[1], apEntities); if (!apEntities.size()) { if (CVar::GetCVarBool("debug_entity_outputs")) TMsg("Console -> none\n"); else TError("No entities found that match name \"" + asTokens[1] + "\".\n"); return; } tstring sArgs; for (size_t i = 3; i < asTokens.size(); i++) sArgs += asTokens[i] + " "; for (size_t i = 0; i < apEntities.size(); i++) { CBaseEntity* pTargetEntity = apEntities[i]; if (CVar::GetCVarBool("debug_entity_outputs")) TMsg("Console -> " + tstring(pTargetEntity->GetClassName()) + "(\"" + pTargetEntity->GetName() + "\")." + asTokens[2] + "(\"" + sArgs + "\")\n"); pTargetEntity->CallInput(asTokens[2], sArgs); } }
bool ConvexHullIntersectsAABB(const AABB& box, const tvector<vec3>& avecPoints, const tvector<size_t>& aiTriangles) { TAssert(aiTriangles.size() % 3 == 0); vec3 vecCenter = box.Center(); vec3 n; for (size_t i = 0; i < aiTriangles.size(); i += 3) { const vec3& v1 = avecPoints[aiTriangles[i]]; const vec3& v2 = avecPoints[aiTriangles[i+1]]; const vec3& v3 = avecPoints[aiTriangles[i+2]]; n = (v2-v1).Cross(v3-v1).Normalized(); if (n.Dot(vecCenter-v1) < 0) continue; if (!TriangleIntersectsAABB(box, v1, v2, v3)) return false; } return true; }
nr_double_t norm (tvector<nr_type_t> a) { #if 0 nr_double_t k = 0; for (int i = 0; i < a.getSize (); i++) k += norm (a.get (i)); return n; #else nr_double_t scale = 0, n = 1, x, ax; for (int i = 0; i < a.getSize (); i++) { if ((x = real (a (i))) != 0) { ax = fabs (x); if (scale < ax) { x = scale / ax; n = 1 + n * x * x; scale = ax; } else { x = ax / scale; n += x * x; } } if ((x = imag (a (i))) != 0) { ax = fabs (x); if (scale < ax) { x = scale / ax; n = 1 + n * x * x; scale = ax; } else { x = ax / scale; n += x * x; } } } return scale * scale * n; #endif }
void CApplication::InitJoystickInput() { g_aJoysticks.resize(MAX_JOYSTICKS); for (size_t i = 0; i < MAX_JOYSTICKS; i++) { if (glfwGetJoystickParam(GLFW_JOYSTICK_1 + i, GLFW_PRESENT) == GL_TRUE) { g_aJoysticks[i].m_bPresent = true; g_aJoysticks[i].m_aflAxis.resize(glfwGetJoystickParam(GLFW_JOYSTICK_1 + i, GLFW_AXES)); for (size_t j = 0; j < g_aJoysticks[i].m_aflAxis.size(); j++) g_aJoysticks[i].m_aflAxis[j] = 0; g_aJoysticks[i].m_iButtons = glfwGetJoystickParam(GLFW_JOYSTICK_1 + i, GLFW_BUTTONS); g_aJoysticks[i].m_aiButtonStates = 0; TAssert(g_aJoysticks[i].m_iButtons < sizeof(g_aJoysticks[i].m_aiButtonStates)*8); } } }
// implementation: approximate gradient (if no analytic gradient provided) void eval_grad(const tvector& x, tvector& g) const { // accuracy epsilon as defined in: // see "Numerical optimization", Nocedal & Wright, 2nd edition, p.197 const tscalar dx = std::cbrt(tscalar(10) * std::numeric_limits<tscalar>::epsilon()); const tsize n = size(); tvector xp = x, xn = x; g.resize(n); for (tsize i = 0; i < n; i ++) { if (i > 0) { xp(i - 1) -= dx; xn(i - 1) += dx; } xp(i) += dx; xn(i) -= dx; g(i) = (m_op_fval(xp) - m_op_fval(xn)) / (xp(i) - xn(i)); } }
nr_type_t scalar (tvector<nr_type_t> a, tvector<nr_type_t> b) { assert (a.getSize () == b.getSize ()); nr_type_t n = 0; for (int i = 0; i < a.getSize (); i++) n += a.get (i) * b.get (i); return n; }
void tmatrix<nr_type_t>::setCol (int c, tvector<nr_type_t> v) { assert (c >= 0 && c < cols && v.getSize () == rows); nr_type_t * dst = &data[c]; nr_type_t * src = v.getData (); for (int r = 0; r < rows; r++, src++, dst += cols) *dst = *src; }
void tmatrix<nr_type_t>::setRow (int r, tvector<nr_type_t> v) { assert (r >= 0 && r < rows && v.getSize () == cols); nr_type_t * dst = &data[r * cols]; nr_type_t * src = v.getData (); memcpy (dst, src, sizeof (nr_type_t) * cols); }
void LoadMesh(CConversionScene* pScene, size_t iMesh) { TAssert(iMesh < pScene->GetNumMeshes()); if (iMesh >= pScene->GetNumMeshes()) return; // Reserve space for n+1, the last one represents the default material. g_aaflData.resize(pScene->GetNumMaterials()+1); tvector<Vector> avecPoints; tvector<size_t> aiPoints; CConversionMesh* pMesh = pScene->GetMesh(iMesh); for (size_t j = 0; j < pMesh->GetNumFaces(); j++) { CConversionFace* pFace = pMesh->GetFace(j); size_t iMaterial = pFace->m; if (iMaterial == ~0) iMaterial = pScene->GetNumMaterials(); CConversionVertex* pVertex0 = pFace->GetVertex(0); CConversionVertex* pVertex1 = pFace->GetVertex(1); CConversionVertex* pLastVertex = pFace->GetVertex(pFace->GetNumVertices()-1); avecPoints.clear(); aiPoints.clear(); for (size_t t = 0; t < pFace->GetNumVertices(); t++) { avecPoints.push_back(pMesh->GetVertex(pFace->GetVertex(t)->v)); aiPoints.push_back(t); } CConversionVertex* pVertex2; while (avecPoints.size() > 3) { size_t iEar = FindEar(avecPoints); size_t iLast = iEar==0?avecPoints.size()-1:iEar-1; size_t iNext = iEar==avecPoints.size()-1?0:iEar+1; pVertex0 = pFace->GetVertex(aiPoints[iLast]); pVertex1 = pFace->GetVertex(aiPoints[iEar]); pVertex2 = pFace->GetVertex(aiPoints[iNext]); AddVertex(iMaterial, pMesh->GetVertex(pVertex0->v), pMesh->GetUV(pVertex0->vu)); AddVertex(iMaterial, pMesh->GetVertex(pVertex1->v), pMesh->GetUV(pVertex1->vu)); AddVertex(iMaterial, pMesh->GetVertex(pVertex2->v), pMesh->GetUV(pVertex2->vu)); avecPoints.erase(avecPoints.begin()+iEar); aiPoints.erase(aiPoints.begin()+iEar); } TAssert(aiPoints.size() == 3); if (aiPoints.size() != 3) continue; pVertex0 = pFace->GetVertex(aiPoints[0]); pVertex1 = pFace->GetVertex(aiPoints[1]); pVertex2 = pFace->GetVertex(aiPoints[2]); AddVertex(iMaterial, pMesh->GetVertex(pVertex0->v), pMesh->GetUV(pVertex0->vu)); AddVertex(iMaterial, pMesh->GetVertex(pVertex1->v), pMesh->GetUV(pVertex1->vu)); AddVertex(iMaterial, pMesh->GetVertex(pVertex2->v), pMesh->GetUV(pVertex2->vu)); } }
tvector<nr_type_t> conj (tvector<nr_type_t> a) { int n = a.getSize (); tvector<nr_type_t> res (n); for (int i = 0; i < n; i++) res.set (i, conj (a.get (i))); return res; }
tvector<nr_type_t> operator + (nr_type_t s, tvector<nr_type_t> a) { int n = a.getSize (); tvector<nr_type_t> res (n); for (int i = 0; i < n; i++) res.set (i, s + a.get (i)); return res; }
bool operator > (tvector<nr_type_t> a, tvector<nr_type_t> b) { assert (a.getSize () == b.getSize ()); int n = a.getSize (); for (int i = 0; i < n; i++) if (a.get (i) <= b.get (i)) return false; return true; }
nr_type_t sum (tvector<nr_type_t> a) { nr_type_t res = 0; for (int i = 0; i < a.getSize (); i++) res += a.get (i); return res; }
void tvector<nr_type_t>::set (tvector<nr_type_t> a, int start, int stop) { for (int i = start; i < stop; i++) data[i] = a.get (i); }