//! Handles client communication details. //! The server will loop until the client closes the socket waiting to read //! up to 1024 bytes off the socket. //! //! @param cliSock client socket //! void handleClient( Socket& cliSock ) { int rnb; static char buf[1024]; while( 1 ) { WarnIf( (rnb=recv(cliSock,buf,sizeof(buf),0)) == -1 ); if ( rnb == 0 ) break; std::cout << " RECIEVED " << rnb << " bytes\n"; int snb = htonl(rnb); WarnIf( (snb=send(cliSock, &snb, sizeof(snb), 0)) == -1 ); } }
Quaternion Lerp(QuatParam start, QuatParam end, float tValue) { if (!Math::InRange(tValue, 0.0f, 1.0f)) WarnIf(!Math::InRange(tValue, 0.0f, 1.0f), "Quaternion - Interpolation value is not in the range of [0, 1]"); float alpha = tValue; float oneMinusAlpha = 1.0f - alpha; Quaternion quaternion(start.x * oneMinusAlpha + end.x * alpha, start.y * oneMinusAlpha + end.y * alpha, start.z * oneMinusAlpha + end.z * alpha, start.w * oneMinusAlpha + end.w * alpha); Normalize(&quaternion); return quaternion; }
Quaternion Slerp(QuatParam start, QuatParam end, float tValue) { if (!Math::InRange(tValue, 0.0f, 1.0f)) WarnIf(!Math::InRange(tValue, 0.0f, 1.0f), "Quaternion - Interpolation value is not in the range of [0, 1]"); // // Quaternion Interpolation With Extra Spins, pp. 96f, 461f // Jack Morrison, Graphics Gems III, AP Professional // const float cSlerpEpsilon = 0.00001f; bool flip; float cosTheta = Dot(start, end); //Check to ensure that the shortest path is taken (cosine of the angle between //the two quaternions is positive). if((flip = (cosTheta < 0.0f))) { cosTheta = -cosTheta; } float startVal, endVal; if((1.0f - cosTheta) > cSlerpEpsilon) { float theta = Math::ArcCos(cosTheta); float sinTheta = Math::Sin(theta); startVal = Math::Sin((1.0f - tValue) * theta) / sinTheta; endVal = Math::Sin(tValue * theta) / sinTheta; } else { startVal = 1.0f - tValue; endVal = tValue; } if(flip) { endVal = -endVal; } return Quaternion(startVal * start.x + endVal * end.x, startVal * start.y + endVal * end.y, startVal * start.z + endVal * end.z, startVal * start.w + endVal * end.w); }
//! This is a simple server to test the ntee program. //! The server simply sends back to the client the number of bytes it //! just recieved from the client. //! int main(int argc, char** argv) { std::string USAGE("Usage: testSrv <server port>\n"); ErrIf( argc < 2 ).info(USAGE); // Convert the port number arg to an int in_port_t port; ErrIfCatch(boost::bad_lexical_cast, port = boost::lexical_cast<in_port_t>( argv[1] )) .info("%s is not a port number!\n",argv[1]); // Put the supplied ip addr into the sockaddr_in struct, we will reuse this // memory later to store client addresses in it as they connect. sockaddr_in addr; memset( &addr, 0, sizeof(addr) ); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); // Wildcard address kernel picks best bind addr.sin_port = htons(port); //** Get the server to accept stage Socket sock; SysErrIf( (sock=socket(AF_INET, SOCK_STREAM, 0)) == -1 ); SysErrIf( bind(sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1 ); SysErrIf( listen(sock, 5) == -1 ); //** Forever accept new connections and reply for( ;; ) { Socket cliSock; memset( &addr, 0, sizeof(addr) ); socklen_t len = sizeof(addr); SysErrIf( (cliSock=accept(sock,reinterpret_cast<sockaddr*>(&addr),&len)) == -1 ); char cliName[INET_ADDRSTRLEN]; WarnIf( inet_ntop(AF_INET, &addr, cliName, sizeof(cliName) ) == 0 ); std::cout << "CONNECTED to: " << ((cliName)?cliName:"Unknown") << ":" << addr.sin_port << "\n"; handleClient( cliSock ); } return 0; }
MeshData(const string& name) { static Assimp::Importer importer; D3DInfo& d3d = *D3DInfo::CurrentInstance(); // Read the mesh data from file using Asset Importer. const aiScene* scene = importer.ReadFile(config::Meshes + name, ImportSetting); if (!scene || !scene->mNumMeshes) { Warn("Mesh read failed for " + name); return; } const aiMesh* mesh = scene->mMeshes[0]; WarnIf(scene->mNumMeshes > 1, "Mesh " + name + " has more sub-meshes than are currently supported"); // Verify mesh texture coordinates and tangents. bool hasTexCoords = true; if (!mesh->HasTextureCoords(0)) { hasTexCoords = false; Warn("Mesh " + name + " doesn't have texture coordinates"); } bool hasTangents = true; if (!mesh->HasTangentsAndBitangents()) { hasTangents = false; Warn("Mesh " + name + " doesn't have tangents/bitangents"); } float minFloat = numeric_limits<float>::min(); float maxFloat = numeric_limits<float>::max(); boundingBox.max = { minFloat, minFloat, minFloat }; boundingBox.min = { maxFloat, maxFloat, maxFloat }; // Copy all vertices. vertices.resize(mesh->mNumVertices); for (size_t i = 0; i < mesh->mNumVertices; ++i) { vertices[i].position = reinterpret_cast<const float3&>(mesh->mVertices[i]); vertices[i].normal = reinterpret_cast<const float3&>(mesh->mNormals[i]); if (hasTexCoords) { vertices[i].tex = reinterpret_cast<const float2&>(mesh->mTextureCoords[0][i]); } if (hasTangents) { vertices[i].tangent = reinterpret_cast<const float3&>(mesh->mTangents[i]); vertices[i].bitangent = reinterpret_cast<const float3&>(mesh->mBitangents[i]); } // Determine the min and max extents of the mesh. if (vertices[i].position.x < boundingBox.min.x) boundingBox.min.x = vertices[i].position.x; if (vertices[i].position.y < boundingBox.min.y) boundingBox.min.y = vertices[i].position.y; if (vertices[i].position.z < boundingBox.min.z) boundingBox.min.z = vertices[i].position.z; if (vertices[i].position.x > boundingBox.max.x) boundingBox.max.x = vertices[i].position.x; if (vertices[i].position.y > boundingBox.max.y) boundingBox.max.y = vertices[i].position.y; if (vertices[i].position.z > boundingBox.max.z) boundingBox.max.z = vertices[i].position.z; } // Calculate the centroid of the mesh: centroid = min + ((max - min) / 2) XMVECTOR minVector = XMLoadFloat3(&boundingBox.min); XMVECTOR cornerToCorner = XMVectorSubtract(XMLoadFloat3(&boundingBox.max), minVector); XMStoreFloat3( &boundingBox.centroid, XMVectorAdd( minVector, XMVectorScale(cornerToCorner, 0.5f))); // Center the mesh on (0,0,0) by subtracting the centroid position from all vertex positions. for (auto& vertex : vertices) { vertex.position.x -= boundingBox.centroid.x; vertex.position.y -= boundingBox.centroid.y; vertex.position.z -= boundingBox.centroid.z; } // Copy all indices. indices.resize(mesh->mNumFaces * 3); for (size_t i = 0; i < mesh->mNumFaces; ++i) { indices[i * 3 + 0] = mesh->mFaces[i].mIndices[0]; indices[i * 3 + 1] = mesh->mFaces[i].mIndices[1]; indices[i * 3 + 2] = mesh->mFaces[i].mIndices[2]; } // Free the loaded scene. importer.FreeScene(); // Create the index buffer. D3D11_BUFFER_DESC bufferDesc; bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; bufferDesc.ByteWidth = indices.size() * sizeof(indices[0]); bufferDesc.CPUAccessFlags = 0; bufferDesc.MiscFlags = 0; bufferDesc.StructureByteStride = 0; bufferDesc.Usage = D3D11_USAGE_IMMUTABLE; D3D11_SUBRESOURCE_DATA initData; initData.pSysMem = indices.data(); initData.SysMemPitch = 0; initData.SysMemSlicePitch = 0; DX(d3d.Device->CreateBuffer(&bufferDesc, &initData, indexBuffer)); // Create the vertex buffer. bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; bufferDesc.ByteWidth = vertices.size() * sizeof(vertices[0]); initData.pSysMem = vertices.data(); DX(d3d.Device->CreateBuffer(&bufferDesc, &initData, vertexBuffer)); // Create the constant buffer. D3D11_BUFFER_DESC cbDesc; ZeroMemory(&cbDesc, sizeof(cbDesc)); cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; cbDesc.ByteWidth = sizeof(ObjectConstants); cbDesc.Usage = D3D11_USAGE_DEFAULT; DX(d3d.Device->CreateBuffer(&cbDesc, nullptr, constantBuffer)); }