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 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)); } }
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)); } }