RenderingMesh* COLRenderWidget::createRenderingMesh(Mesh* mesh) { Matrix4 fModelMat = Matrix4::Identity; MeshFrame* frame = mesh->getFrame(); if (frame) { fModelMat = fModelMat * frame->getAbsoluteModelMatrix(); } RenderingPrimitiveFormat rpf; switch (mesh->getVertexFormat()) { case VertexFormatPoints: rpf = RenderingPrimitivePoints; break; case VertexFormatLines: rpf = RenderingPrimitiveLines; break; case VertexFormatTriangles: rpf = RenderingPrimitiveTriangles; break; case VertexFormatTriangleStrips: rpf = RenderingPrimitiveTriangleStrip; break; } //uint32_t flags = RenderingMesh::EnableShaderPluginUniformBuffers | RenderingMesh::HasTransparency; uint32_t flags = RenderingMesh::EnableShaderPluginUniformBuffers; DefaultRenderingMesh* rm = new DefaultRenderingMesh ( rpf, flags, mesh->getVertexCount(), 0, NULL, 0, mesh->getDataBuffer(), mesh->getIndexBuffer(), mesh->getVertexOffset(), mesh->getVertexStride(), mesh->getSubmeshIDOffset(), mesh->getSubmeshIDStride(), mesh->getNormalOffset(), mesh->getNormalStride(), mesh->getTexCoordOffset(), mesh->getTexCoordStride(), mesh->getVertexColorOffset(), mesh->getVertexColorStride(), -1, 0, -1, 0 ); rm->setModelMatrix(fModelMat); for (Mesh::SubmeshIterator sit = mesh->getSubmeshBegin() ; sit != mesh->getSubmeshEnd() ; sit++) { Submesh* submesh = *sit; Material* mat = submesh->getMaterial(); GLuint oglTex = 0; uint8_t r = 255; uint8_t g = 255; uint8_t b = 255; uint8_t a = 255; if (mat) { mat->getColor(r, g, b, a); } RenderingSubmesh* sm = new RenderingSubmesh(rm, submesh->getIndexCount(), submesh->getIndexOffset(), oglTex); sm->setMaterialColor(r, g, b, a); } return rm; }
void Mesh::link() { if (isLinked) return; // Build the data buffer size_t numSubmeshes = submeshes.size(); glBindBuffer(GL_ARRAY_BUFFER, dataBuffer); char* bufData = (char*) glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY); char* newData = new char[dataBufferSingleSize * numSubmeshes]; for (uint8_t i = 0 ; i < numSubmeshes ; i++) { memcpy(newData + i*dataBufferSingleSize, bufData, dataBufferSingleSize); if (submeshIDOffs != -1) { for (size_t j = 0 ; j < vertexCount ; j++) { newData[i*dataBufferSingleSize + submeshIDOffs + j*submeshIDStride] = i; } } } glUnmapBuffer(GL_ARRAY_BUFFER); glBufferData(GL_ARRAY_BUFFER, dataBufferSingleSize * numSubmeshes, newData, GL_STATIC_DRAW); delete[] newData; // Build the index buffer if (indexBuffer == 0) glGenBuffers(1, &indexBuffer); bool hasCompiledSubmeshes = false; //GLuint bufSize = 0; size_t numIndices = 0; for (SubmeshIterator it = submeshes.begin() ; it != submeshes.end() ; it++) { Submesh* submesh = *it; if (submesh->isLinked()) hasCompiledSubmeshes = true; numIndices += submesh->getIndexCount(); } uint32_t* newIndices = new uint32_t[numIndices]; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); uint32_t* oldIndices; if (hasCompiledSubmeshes) oldIndices = (uint32_t*) glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY); size_t offset = 0; for (SubmeshIterator it = submeshes.begin() ; it != submeshes.end() ; it++) { Submesh* submesh = *it; int ic = submesh->getIndexCount(); if (submesh->isLinked()) { memcpy(newIndices + offset, oldIndices + submesh->getIndexOffset(), ic*4); } else { memcpy(newIndices + offset, submesh->indices, ic*4); } submesh->setLinked(offset); offset += ic; } if (hasCompiledSubmeshes) glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices*4, newIndices, GL_STATIC_DRAW); delete[] newIndices; isLinked = true; }