Showing the changes to libraries/render-utils/src/Model.cpp (1 of 8 files). Show all
Unified · Split
| @@ -765,6 +765,29 @@ void Model::renderSetup(RenderArgs* args) { · 764 unmodified lines | ||
| 765 | 765 | } |
| 766 | 766 | } |
| 767 | 767 | |
| 768 | +bool Model::addToScene(std::shared_ptr<render::Scene> scene, render::PendingChanges& pendingChanges) { | |
| 769 | + bool somethingAdded = false; | |
| 770 | + // allow the attachments to add to scene | |
| 771 | + foreach (Model* attachment, _attachments) { | |
| 772 | + bool attachementSomethingAdded = attachment->addToScene(scene, pendingChanges); | |
| 773 | + somethingAdded = somethingAdded || attachementSomethingAdded; | |
| 774 | + } | |
| 775 | + | |
| 776 | + // TODO --- need to do something here | |
| 777 | + | |
| 778 | + return somethingAdded; | |
| 779 | +} | |
| 780 | + | |
| 781 | +void Model::removeFromScene(std::shared_ptr<render::Scene> scene, render::PendingChanges& pendingChanges) { | |
| 782 | + // allow the attachments to remove to scene | |
| 783 | + foreach (Model* attachment, _attachments) { | |
| 784 | + attachment->removeFromScene(scene, pendingChanges); | |
| 785 | + } | |
| 786 | + | |
| 787 | + // TODO --- need to do something here | |
| 788 | + | |
| 789 | +} | |
| 790 | + | |
| 768 | 791 | bool Model::render(RenderArgs* renderArgs, float alpha) { |
| 769 | 792 | PROFILE_RANGE(__FUNCTION__); |
| 770 | 793 | |
| @@ -2068,7 +2091,235 @@ bool Model::renderInScene(float alpha, RenderArgs* args) { · 1297 unmodified lines | ||
| 2068 | 2091 | return true; |
| 2069 | 2092 | } |
| 2070 | 2093 | |
| 2094 | +class TransparentMeshPart { | |
| 2095 | +public: | |
| 2096 | + TransparentMeshPart(Model* model, int meshIndex, int partIndex) : model(model), meshIndex(meshIndex), partIndex(partIndex) { } | |
| 2097 | + typedef render::Payload<TransparentMeshPart> Payload; | |
| 2098 | + typedef Payload::DataPointer Pointer; | |
| 2099 | + | |
| 2100 | + Model* model; | |
| 2101 | + int meshIndex; | |
| 2102 | + int partIndex; | |
| 2103 | +}; | |
| 2104 | + | |
| 2105 | +namespace render { | |
| 2106 | + template <> const ItemKey payloadGetKey(const TransparentMeshPart::Pointer& payload) { | |
| 2107 | + return ItemKey::Builder::transparentShape(); | |
| 2108 | + } | |
| 2109 | + | |
| 2110 | + template <> const Item::Bound payloadGetBound(const TransparentMeshPart::Pointer& payload) { | |
| 2111 | + if (payload) { | |
| 2112 | + return payload->model->getPartBounds(payload->meshIndex, payload->partIndex); | |
| 2113 | + } | |
| 2114 | + return render::Item::Bound(); | |
| 2115 | + } | |
| 2116 | + template <> void payloadRender(const TransparentMeshPart::Pointer& payload, RenderArgs* args) { | |
| 2117 | + if (args) { | |
| 2118 | + args->_elementsTouched++; | |
| 2119 | + return payload->model->renderPart(args, payload->meshIndex, payload->partIndex, true); | |
| 2120 | + } | |
| 2121 | + } | |
| 2122 | +} | |
| 2123 | + | |
| 2124 | +class OpaqueMeshPart { | |
| 2125 | +public: | |
| 2126 | + OpaqueMeshPart(Model* model, int meshIndex, int partIndex) : model(model), meshIndex(meshIndex), partIndex(partIndex) { } | |
| 2127 | + typedef render::Payload<OpaqueMeshPart> Payload; | |
| 2128 | + typedef Payload::DataPointer Pointer; | |
| 2129 | + | |
| 2130 | + Model* model; | |
| 2131 | + int meshIndex; | |
| 2132 | + int partIndex; | |
| 2133 | +}; | |
| 2134 | + | |
| 2135 | +namespace render { | |
| 2136 | + template <> const ItemKey payloadGetKey(const OpaqueMeshPart::Pointer& payload) { | |
| 2137 | + return ItemKey::Builder::opaqueShape(); | |
| 2138 | + } | |
| 2139 | + | |
| 2140 | + template <> const Item::Bound payloadGetBound(const OpaqueMeshPart::Pointer& payload) { | |
| 2141 | + if (payload) { | |
| 2142 | + return payload->model->getPartBounds(payload->meshIndex, payload->partIndex); | |
| 2143 | + } | |
| 2144 | + return render::Item::Bound(); | |
| 2145 | + } | |
| 2146 | + template <> void payloadRender(const OpaqueMeshPart::Pointer& payload, RenderArgs* args) { | |
| 2147 | + if (args) { | |
| 2148 | + args->_elementsTouched++; | |
| 2149 | + return payload->model->renderPart(args, payload->meshIndex, payload->partIndex, false); | |
| 2150 | + } | |
| 2151 | + } | |
| 2152 | +} | |
| 2153 | + | |
| 2154 | +AABox Model::getPartBounds(int meshIndex, int partIndex) { | |
| 2155 | + const FBXGeometry& geometry = _geometry->getFBXGeometry(); | |
| 2156 | + const FBXMesh& mesh = geometry.meshes.at(meshIndex); | |
| 2157 | + AABox partBox = mesh._mesh.evalPartBound(partIndex); | |
| 2158 | + return partBox; | |
| 2159 | +} | |
| 2160 | + | |
| 2161 | +void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool translucent) { | |
| 2162 | + auto textureCache = DependencyManager::get<TextureCache>(); | |
| 2163 | + | |
| 2164 | + const float DEFAULT_ALPHA_THRESHOLD = 0.5f; | |
| 2165 | + | |
| 2166 | + int i = meshIndex; | |
| 2167 | + int j = partIndex; | |
| 2168 | + gpu::Batch& batch = *(args->_batch); | |
| 2169 | + auto mode = args->_renderMode; | |
| 2170 | + auto alphaThreshold = DEFAULT_ALPHA_THRESHOLD; // FIX ME | |
| 2171 | + const FBXGeometry& geometry = _geometry->getFBXGeometry(); | |
| 2172 | + const QVector<NetworkMesh>& networkMeshes = _geometry->getMeshes(); | |
| 2173 | + | |
| 2174 | + const NetworkMesh& networkMesh = networkMeshes.at(meshIndex); | |
| 2175 | + const FBXMesh& mesh = geometry.meshes.at(meshIndex); | |
| 2176 | + const MeshState& state = _meshStates.at(meshIndex); | |
| 2177 | + | |
| 2178 | + int vertexCount = mesh.vertices.size(); // NOTE: This seems wrong, shouldn't it be the part's vertex count? | |
| 2179 | + | |
| 2180 | + | |
| 2181 | + bool translucentMesh = networkMesh.getTranslucentPartCount(mesh) == networkMesh.parts.size(); | |
| 2182 | + bool hasTangents = !mesh.tangents.isEmpty(); | |
| 2183 | + bool hasSpecular = mesh.hasSpecularTexture(); | |
| 2184 | + bool hasLightmap = mesh.hasEmissiveTexture(); | |
| 2185 | + bool isSkinned = state.clusterMatrices.size() > 1; | |
| 2186 | + bool wireframe = isWireframe(); | |
| 2187 | + | |
| 2188 | + if (wireframe) { | |
| 2189 | + translucentMesh = hasTangents = hasSpecular = hasLightmap = isSkinned = false; | |
| 2190 | + } | |
| 2191 | + | |
| 2192 | + Locations* locations = nullptr; | |
| 2193 | + pickPrograms(batch, mode, translucent, alphaThreshold, hasLightmap, hasTangents, hasSpecular, isSkinned, wireframe, | |
| 2194 | + args, locations); | |
| 2195 | + | |
| 2196 | + if (state.clusterMatrices.size() > 1) { | |
| 2197 | + GLBATCH(glUniformMatrix4fv)(locations->clusterMatrices, state.clusterMatrices.size(), false, | |
| 2198 | + (const float*)state.clusterMatrices.constData()); | |
| 2199 | + batch.setModelTransform(Transform()); | |
| 2200 | + } else { | |
| 2201 | + batch.setModelTransform(Transform(state.clusterMatrices[0])); | |
| 2202 | + } | |
| 2203 | + | |
| 2204 | + if (mesh.blendshapes.isEmpty()) { | |
| 2205 | + batch.setInputFormat(networkMesh._vertexFormat); | |
| 2206 | + batch.setInputStream(0, *networkMesh._vertexStream); | |
| 2207 | + } else { | |
| 2208 | + batch.setInputFormat(networkMesh._vertexFormat); | |
| 2209 | + batch.setInputBuffer(0, _blendedVertexBuffers[i], 0, sizeof(glm::vec3)); | |
| 2210 | + batch.setInputBuffer(1, _blendedVertexBuffers[i], vertexCount * sizeof(glm::vec3), sizeof(glm::vec3)); | |
| 2211 | + batch.setInputStream(2, *networkMesh._vertexStream); | |
| 2212 | + } | |
| 2213 | + | |
| 2214 | + if (mesh.colors.isEmpty()) { | |
| 2215 | + GLBATCH(glColor4f)(1.0f, 1.0f, 1.0f, 1.0f); | |
| 2216 | + } | |
| 2217 | + | |
| 2218 | + qint64 offset = 0; | |
| 2219 | + | |
| 2220 | + const NetworkMeshPart& networkPart = networkMesh.parts.at(j); | |
| 2221 | + const FBXMeshPart& part = mesh.parts.at(j); | |
| 2222 | + model::MaterialPointer material = part._material; | |
| 2223 | + if ((networkPart.isTranslucent() || part.opacity != 1.0f) != translucent) { | |
| 2224 | + offset += (part.quadIndices.size() + part.triangleIndices.size()) * sizeof(int); | |
| 2225 | + //continue; | |
| 2226 | + // FIX ME!!! | |
| 2227 | + } | |
| 2228 | + | |
| 2229 | + // apply material properties | |
| 2230 | + if (mode == RenderArgs::SHADOW_RENDER_MODE) { | |
| 2231 | + /// GLBATCH(glBindTexture)(GL_TEXTURE_2D, 0); | |
| 2232 | + } else { | |
| 2233 | + const bool wantDebug = false; | |
| 2234 | + if (wantDebug) { | |
| 2235 | + qCDebug(renderutils) << "Material Changed ---------------------------------------------"; | |
| 2236 | + qCDebug(renderutils) << "part INDEX:" << j; | |
| 2237 | + qCDebug(renderutils) << "NEW part.materialID:" << part.materialID; | |
| 2238 | + } | |
| 2239 | + | |
| 2240 | + if (locations->materialBufferUnit >= 0) { | |
| 2241 | + batch.setUniformBuffer(locations->materialBufferUnit, material->getSchemaBuffer()); | |
| 2242 | + } | |
| 2243 | + | |
| 2244 | + Texture* diffuseMap = networkPart.diffuseTexture.data(); | |
| 2245 | + if (mesh.isEye && diffuseMap) { | |
| 2246 | + diffuseMap = (_dilatedTextures[i][j] = | |
| 2247 | + static_cast<DilatableNetworkTexture*>(diffuseMap)->getDilatedTexture(_pupilDilation)).data(); | |
| 2248 | + } | |
| 2249 | + static bool showDiffuse = true; | |
| 2250 | + if (showDiffuse && diffuseMap) { | |
| 2251 | + batch.setUniformTexture(0, diffuseMap->getGPUTexture()); | |
| 2252 | + | |
| 2253 | + } else { | |
| 2254 | + batch.setUniformTexture(0, textureCache->getWhiteTexture()); | |
| 2255 | + } | |
| 2256 | + | |
| 2257 | + if (locations->texcoordMatrices >= 0) { | |
| 2258 | + glm::mat4 texcoordTransform[2]; | |
| 2259 | + if (!part.diffuseTexture.transform.isIdentity()) { | |
| 2260 | + part.diffuseTexture.transform.getMatrix(texcoordTransform[0]); | |
| 2261 | + } | |
| 2262 | + if (!part.emissiveTexture.transform.isIdentity()) { | |
| 2263 | + part.emissiveTexture.transform.getMatrix(texcoordTransform[1]); | |
| 2264 | + } | |
| 2265 | + GLBATCH(glUniformMatrix4fv)(locations->texcoordMatrices, 2, false, (const float*) &texcoordTransform); | |
| 2266 | + } | |
| 2267 | + | |
| 2268 | + if (!mesh.tangents.isEmpty()) { | |
| 2269 | + Texture* normalMap = networkPart.normalTexture.data(); | |
| 2270 | + batch.setUniformTexture(1, !normalMap ? | |
| 2271 | + textureCache->getBlueTexture() : normalMap->getGPUTexture()); | |
| 2272 | + | |
| 2273 | + } | |
| 2274 | + | |
| 2275 | + if (locations->specularTextureUnit >= 0) { | |
| 2276 | + Texture* specularMap = networkPart.specularTexture.data(); | |
| 2277 | + batch.setUniformTexture(locations->specularTextureUnit, !specularMap ? | |
| 2278 | + textureCache->getWhiteTexture() : specularMap->getGPUTexture()); | |
| 2279 | + } | |
| 2280 | + | |
| 2281 | + if (args) { | |
| 2282 | + args->_materialSwitches++; | |
| 2283 | + } | |
| 2284 | + | |
| 2285 | + // HACK: For unkwon reason (yet!) this code that should be assigned only if the material changes need to be called for every | |
| 2286 | + // drawcall with an emissive, so let's do it for now. | |
| 2287 | + if (locations->emissiveTextureUnit >= 0) { | |
| 2288 | + // assert(locations->emissiveParams >= 0); // we should have the emissiveParams defined in the shader | |
| 2289 | + float emissiveOffset = part.emissiveParams.x; | |
| 2290 | + float emissiveScale = part.emissiveParams.y; | |
| 2291 | + GLBATCH(glUniform2f)(locations->emissiveParams, emissiveOffset, emissiveScale); | |
| 2292 | + | |
| 2293 | + Texture* emissiveMap = networkPart.emissiveTexture.data(); | |
| 2294 | + batch.setUniformTexture(locations->emissiveTextureUnit, !emissiveMap ? | |
| 2295 | + textureCache->getWhiteTexture() : emissiveMap->getGPUTexture()); | |
| 2296 | + } | |
| 2297 | + | |
| 2298 | + } | |
| 2299 | + | |
| 2300 | + if (part.quadIndices.size() > 0) { | |
| 2301 | + batch.drawIndexed(gpu::QUADS, part.quadIndices.size(), offset); | |
| 2302 | + offset += part.quadIndices.size() * sizeof(int); | |
| 2303 | + } | |
| 2304 | + | |
| 2305 | + if (part.triangleIndices.size() > 0) { | |
| 2306 | + batch.drawIndexed(gpu::TRIANGLES, part.triangleIndices.size(), offset); | |
| 2307 | + offset += part.triangleIndices.size() * sizeof(int); | |
| 2308 | + } | |
| 2309 | + | |
| 2310 | + if (args) { | |
| 2311 | + const int INDICES_PER_TRIANGLE = 3; | |
| 2312 | + const int INDICES_PER_QUAD = 4; | |
| 2313 | + args->_trianglesRendered += part.triangleIndices.size() / INDICES_PER_TRIANGLE; | |
| 2314 | + args->_quadsRendered += part.quadIndices.size() / INDICES_PER_QUAD; | |
| 2315 | + } | |
| 2316 | + | |
| 2317 | +} | |
| 2318 | + | |
| 2071 | 2319 | void Model::segregateMeshGroups() { |
| 2320 | + | |
| 2321 | + //qDebug() << "Model::segregateMeshGroups() ------------------------------------------------"; | |
| 2322 | + | |
| 2072 | 2323 | _renderBuckets.clear(); |
| 2073 | 2324 | |
| 2074 | 2325 | const FBXGeometry& geometry = _geometry->getFBXGeometry(); |
| @@ -2086,6 +2337,7 @@ void Model::segregateMeshGroups() { · 11 unmodified lines | ||
| 2086 | 2337 | const NetworkMesh& networkMesh = networkMeshes.at(i); |
| 2087 | 2338 | const FBXMesh& mesh = geometry.meshes.at(i); |
| 2088 | 2339 | const MeshState& state = _meshStates.at(i); |
| 2340 | + | |
| 2089 | 2341 | |
| 2090 | 2342 | bool translucentMesh = networkMesh.getTranslucentPartCount(mesh) == networkMesh.parts.size(); |
| 2091 | 2343 | bool hasTangents = !mesh.tangents.isEmpty(); |
| @@ -2097,6 +2349,23 @@ void Model::segregateMeshGroups() { · 5 unmodified lines | ||
| 2097 | 2349 | if (wireframe) { |
| 2098 | 2350 | translucentMesh = hasTangents = hasSpecular = hasLightmap = isSkinned = false; |
| 2099 | 2351 | } |
| 2352 | + | |
| 2353 | + // Debug... | |
| 2354 | + //qDebug() << "Mesh parts..." << mesh._mesh.getNumParts(); | |
| 2355 | + int totalParts = mesh._mesh.getNumParts(); | |
| 2356 | + for (int partIndex = 0; partIndex < totalParts; partIndex++) { | |
| 2357 | + AABox boxPart = mesh._mesh.evalPartBound(partIndex); | |
| 2358 | + | |
| 2359 | + // this is a good place to create our renderPayloads | |
| 2360 | + if (translucentMesh) { | |
| 2361 | + //qDebug() << "Transparent Mesh parts[" << partIndex << "].box=" << boxPart; | |
| 2362 | + _transparentRenderItems << std::shared_ptr<TransparentMeshPart>(new TransparentMeshPart(this, i, partIndex)); | |
| 2363 | + } else { | |
| 2364 | + //qDebug() << "Opaque Mesh parts[" << partIndex << "].box=" << boxPart; | |
| 2365 | + _opaqueRenderItems << std::shared_ptr<OpaqueMeshPart>(new OpaqueMeshPart(this, i, partIndex)); | |
| 2366 | + } | |
| 2367 | + } | |
| 2368 | + | |
| 2100 | 2369 | |
| 2101 | 2370 | QString materialID; |
| 2102 | 2371 | |