KVertex kKinect::getPosition(void) { mContext.WaitAndUpdateAll(); /* Anzahl der User auslesen und in Objekten speichern */ // number of users read and save to objects nUsers=1; user.GetUsers(pUser, nUsers); if(nUsers>0) { /* User dem Skeleton zuweisen */ // assign users to skeleton xn::SkeletonCapability pSkeleton = user.GetSkeletonCap(); if(skeleton!=0) { delete skeleton; skeleton = 0; } skeleton=new xn::SkeletonCapability(pSkeleton); if(skeleton->IsCalibrated(pUser[0])) { /* Alle Körperteile auswählen */ // select all parts of the body skeleton->SetSkeletonProfile(XN_SKEL_PROFILE_ALL); /* Kopf initialisieren */ // head initialize XnSkeletonJointTransformation head; skeleton->StartTracking(pUser[0]); skeleton->GetSkeletonJoint(pUser[0], XN_SKEL_HEAD, head); if(head.position.fConfidence && head.orientation.fConfidence) { /* std::cout << "x: " << head.position.position.X << ", " << "y: " << head.position.position.Y << ", " << "z: " << head.position.position.Z << " " << "[x*x2]: "<<(head.position.position.X*KProgram::x2) <<", "<< "[-y*y2]: "<<(-(head.position.position.Y+200.0)*KProgram::y2)<<", "<< "[z*z2]: "<<(head.position.position.Z*KProgram::z2) << std::endl; */ //"x2: " << head.position.position.X/SCREEN_HEIGTH_MM << //"y2: " << (head.position.position.Y+200.0)/SCREEN_HEIGTH_MM << //std::endl; return KVertex(head.position.position.X*KProgram::x2, -(head.position.position.Y+200.0)*KProgram::y2, head.position.position.Z*KProgram::z2, KRGBColor()); } } //else //printf("[kKinect] skeleton->IsCalibrated for user 0 returning false...\n"); } //else // printf("[kKinect] nUsers is 0, sorry sucker. Or hit 'c'[Enter] to calibrate a user...\n"); return KVertex(); }
void KItems::createItems(void) { // If there is something to create... do it if( mItemNumber > 0 ){ // Create a field to save circles in mItemField = new KCircle[mItemNumber]; mItemFieldSize = mItemNumber; // Create the depthstep and the startdepth float depthStep = 1.0f / mItemFieldSize; float startDepth = mItemsInFront*depthStep; // Initalize the random std::srand(static_cast<unsigned int>(std::time(0))); //Create the circles: for(int i = 0 ; i < mItemFieldSize ; i++){ mItemField[i].mCenter = KVertex( 0.7f * ((std::rand()%1001) / 1000.0f - 0.5f) + 0.5f, 0.7f * ((std::rand()%1001) / 1000.0f - 0.5f) + 0.5f, startDepth - i * depthStep, KRGBColor(ITEM_COLOR)); mItemField[i].mRadius = ITEM_SIZE; //DEBUG if(mItemField[i].mCenter.mX >= 0.95f || mItemField[i].mCenter.mX <= 0.05 || mItemField[i].mCenter.mY >= 0.95 || mItemField[i].mCenter.mY <= 0.05) int x = 1+1; // Make the nearer nearer to the center if(i < mItemsInFront){ mItemField[i].mCenter.mX = (mItemField[i].mCenter.mX - 0.5f) / 2.0f + 0.5f; mItemField[i].mCenter.mY = (mItemField[i].mCenter.mY - 0.5f) / 2.0f + 0.5f; } } } }
struct KModel *KLoadModel(const std::string &path) { // get data from file FILE *fp = fopen(path.c_str(), "r"); std::string line; std::vector<KVertex> vertices; std::vector<KFace> indices; while ((line = KReadLine(fp)) != "end_of_file") { if (line.empty()) continue; switch (line[0]) { case 'v': { float x, y, z; if (sscanf(line.c_str(), "v %f %f %f", &x, &y, &z) == 3) { vertices.push_back(KVertex(x,y,z)); } } break; case 'f': { int i0, i1, i2; if (sscanf(line.c_str(), "f %d %d %d", &i0, &i1, &i2) == 3) { indices.push_back(KFace(i0-1, i1-1, i2-1)); } } break; } } fclose(fp); // make KModel struct KModel *pModel = (struct KModel *)malloc(sizeof(struct KModel)); memset(pModel, 0, sizeof(struct KModel)); IDirect3DDevice9* pd3dDevice = KCDXGetDevice(); HRESULT hr = pd3dDevice->CreateVertexDeclaration(DeclElements, &pModel->decl); KCDX_HR_FAILCHECK(hr, "KModel, pd3dDevice->CreateVertexDeclaration"); size_t i; pd3dDevice->CreateVertexBuffer(vertices.size()*sizeof(KVertex), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &pModel->vb, NULL); VOID* pVertices; pModel->vb->Lock(0, vertices.size()*sizeof(KVertex), &pVertices, 0); for (i = 0; i < vertices.size(); i++) { *((KVertex*)pVertices + i) = vertices[i]; } pModel->vb->Unlock(); pd3dDevice->CreateIndexBuffer(indices.size()*sizeof(KFace), 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &pModel->ib, NULL); VOID *pIndices; pModel->ib->Lock(0, indices.size()*sizeof(KFace), &pIndices, 0); for (i = 0; i < indices.size(); i++) { *((KFace*)pIndices + i) = indices[i]; } pModel->ib->Unlock(); pModel->vertexCount = vertices.size(); pModel->faceCount = indices.size(); return pModel; }
void OpenGLMeshPrivate::create(const KHalfEdgeMesh &mesh) { // Helpers m_aabb = KAabbBoundingVolume(mesh.aabb()); KHalfEdgeMesh::FaceContainer const &faces = mesh.faces(); KHalfEdgeMesh::VertexContainer const &vertices = mesh.vertices(); size_t verticesSize = sizeof(KVertex) * vertices.size(); size_t indicesCount = faces.size() * 3; size_t indicesSize = sizeof(uint32_t) * indicesCount; OpenGLBuffer::RangeAccessFlags flags = OpenGLBuffer::RangeInvalidate | OpenGLBuffer::RangeUnsynchronized | OpenGLBuffer::RangeWrite; // Create Buffers m_elementCount = static_cast<GLsizei>(indicesCount); m_vertexArrayObject.create(); m_vertexBuffer.create(); m_indexBuffer.create(); // Bind mesh m_vertexArrayObject.bind(); m_vertexBuffer.bind(); m_indexBuffer.bind(); // Allocate Mesh m_vertexBuffer.allocate(verticesSize); m_indexBuffer.allocate(indicesSize); KVertex *vertDest = (KVertex*)m_vertexBuffer.mapRange(0, verticesSize, flags); uint32_t *indDest = (uint32_t*)m_indexBuffer.mapRange(0, indicesSize, flags); // Iterators uint32_t *baseIndDest; const KHalfEdgeMesh::HalfEdge *halfEdge; // Construct Mesh for (size_t i = 0; i < vertices.size(); ++i) { vertDest[i] = KVertex(vertices[i].position, vertices[i].normal); } for (size_t i = 0; i < faces.size(); ++i) { baseIndDest = &indDest[3 * i]; halfEdge = mesh.halfEdge(faces[i].first); baseIndDest[0] = halfEdge->to - 1; halfEdge = mesh.halfEdge(halfEdge->next); baseIndDest[1] = halfEdge->to - 1; halfEdge = mesh.halfEdge(halfEdge->next); baseIndDest[2] = halfEdge->to - 1; } // Setup Vertex Pointers vertexAttribPointer(0, KVertex::PositionTupleSize, OpenGLElementType::Float, false, KVertex::stride(), KVertex::positionOffset()); vertexAttribPointer(1, KVertex::NormalTupleSize, OpenGLElementType::Float, true, KVertex::stride(), KVertex::normalOffset()); // Finalize Construction m_indexBuffer.unmap(); m_vertexBuffer.unmap(); m_vertexArrayObject.release(); }