// |----------------------------------------------------------------------------| // | UpdateBuffers | // |----------------------------------------------------------------------------| bool Sentence::UpdateBuffers(char* string, Font* font) { DebugLog ("Sentence::UpdateBuffers() called.", DB_GRAPHICS, 2); int numLetters; VertexType* vertices; unsigned long* indices; D3D11_MAPPED_SUBRESOURCE mappedResource; VertexType* verticesPtr; float drawX, drawY; HRESULT result; // Get the number of letters in the sentence. numLetters = (int)strlen(string); if(numLetters > m_maxLength) { return false; } // Initialize the index and vertex arrays. indices = new unsigned long[m_indexCount]; vertices = new VertexType[m_vertexCount]; PopulateBuffers(vertices,indices); // Calculate the X and Y pixel position on the screen to start drawing to. drawX = (SCREEN_WIDTH / 2.0f) * -1.0f; drawY = SCREEN_HEIGHT / 2.0f; // Use the font class to build the vertex array from the sentence text and sentence draw location. font->BuildVertexArray(vertices, string, drawX, drawY); // Lock the vertex buffer so it can be written to. result = D3DManager::GetRef()->GetDeviceContext()->Map(m_vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if(FAILED(result)) { return false; } // Get a pointer to the data in the vertex buffer. verticesPtr = (VertexType*)mappedResource.pData; // Copy the data into the vertex buffer. memcpy(verticesPtr, (void*)vertices, (sizeof(VertexType) * m_vertexCount)); // Unlock the vertex buffer. D3DManager::GetRef()->GetDeviceContext()->Unmap(m_vertexBuffer, 0); // Release the vertex array as it is no longer needed. delete [] vertices; vertices = 0; return true; }
bool Mesh::loadFromFile(const char * path){ Assimp::Importer importer; const aiScene* scene = importer.ReadFile(path, 0); if (!scene) { fprintf(stderr, importer.GetErrorString()); getchar(); return false; } const aiMesh* mesh = scene->mMeshes[0]; // In this simple example code we always use the 1st mesh (in OBJ files there is often only one anyway) // Fill vertices positions vertices.reserve(mesh->mNumVertices); for (unsigned int i = 0; i < mesh->mNumVertices; i++){ aiVector3D pos = mesh->mVertices[i]; vertices.push_back(glm::vec3(pos.x, pos.y, pos.z)); } // Fill vertices texture coordinates uvs.reserve(mesh->mNumVertices); for (unsigned int i = 0; i < mesh->mNumVertices; i++){ aiVector3D UVW = mesh->mTextureCoords[0][i]; // Assume only 1 set of UV coords; AssImp supports 8 UV sets. uvs.push_back(glm::vec2(UVW.x, UVW.y)); } // Fill vertices normals normals.reserve(mesh->mNumVertices); for (unsigned int i = 0; i < mesh->mNumVertices; i++){ aiVector3D n = mesh->mNormals[i]; normals.push_back(glm::vec3(n.x, n.y, n.z)); } PopulateBarycentrics(); // Fill face indices indices.reserve(3 * mesh->mNumFaces); for (unsigned int i = 0; i < mesh->mNumFaces; i++){ // Assume the model has only triangles. indices.push_back(mesh->mFaces[i].mIndices[0]); indices.push_back(mesh->mFaces[i].mIndices[1]); indices.push_back(mesh->mFaces[i].mIndices[2]); } PopulateBuffers(); }