2023-06-14 16:52:36 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
2016-07-16 19:42:28 -04:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "scene/attribute.h"
|
|
|
|
|
#include "scene/camera.h"
|
|
|
|
|
#include "scene/mesh.h"
|
2016-07-16 19:42:28 -04:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "subd/patch.h"
|
|
|
|
|
#include "subd/patch_table.h"
|
|
|
|
|
#include "subd/split.h"
|
2016-07-16 19:42:28 -04:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "util/algorithm.h"
|
2016-07-16 19:42:28 -04:00
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
|
|
# include <opensubdiv/far/patchMap.h>
|
2020-03-19 09:33:03 +01:00
|
|
|
# include <opensubdiv/far/patchTableFactory.h>
|
|
|
|
|
# include <opensubdiv/far/primvarRefiner.h>
|
|
|
|
|
# include <opensubdiv/far/topologyRefinerFactory.h>
|
2016-07-16 22:57:06 -04:00
|
|
|
|
|
|
|
|
/* specializations of TopologyRefinerFactory for ccl::Mesh */
|
|
|
|
|
|
2024-12-26 17:53:59 +01:00
|
|
|
namespace OpenSubdiv::v3_6_0::Far {
|
2016-07-16 22:57:06 -04:00
|
|
|
template<>
|
|
|
|
|
bool TopologyRefinerFactory<ccl::Mesh>::resizeComponentTopology(TopologyRefiner &refiner,
|
|
|
|
|
ccl::Mesh const &mesh)
|
|
|
|
|
{
|
2020-11-04 11:17:38 +01:00
|
|
|
setNumBaseVertices(refiner, mesh.get_verts().size());
|
|
|
|
|
setNumBaseFaces(refiner, mesh.get_num_subd_faces());
|
2016-07-16 22:57:06 -04:00
|
|
|
|
2020-11-04 11:17:38 +01:00
|
|
|
for (int i = 0; i < mesh.get_num_subd_faces(); i++) {
|
|
|
|
|
setNumBaseFaceVertices(refiner, i, mesh.get_subd_num_corners()[i]);
|
2016-07-16 22:57:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
bool TopologyRefinerFactory<ccl::Mesh>::assignComponentTopology(TopologyRefiner &refiner,
|
|
|
|
|
ccl::Mesh const &mesh)
|
|
|
|
|
{
|
2020-11-04 11:17:38 +01:00
|
|
|
const ccl::array<int> &subd_face_corners = mesh.get_subd_face_corners();
|
|
|
|
|
const ccl::array<int> &subd_start_corner = mesh.get_subd_start_corner();
|
|
|
|
|
const ccl::array<int> &subd_num_corners = mesh.get_subd_num_corners();
|
2016-07-16 22:57:06 -04:00
|
|
|
|
2020-11-04 11:17:38 +01:00
|
|
|
for (int i = 0; i < mesh.get_num_subd_faces(); i++) {
|
2016-07-16 22:57:06 -04:00
|
|
|
IndexArray face_verts = getBaseFaceVertices(refiner, i);
|
|
|
|
|
|
2020-11-04 11:17:38 +01:00
|
|
|
int start_corner = subd_start_corner[i];
|
|
|
|
|
int *corner = &subd_face_corners[start_corner];
|
2016-07-16 22:57:06 -04:00
|
|
|
|
2020-11-04 11:17:38 +01:00
|
|
|
for (int j = 0; j < subd_num_corners[i]; j++, corner++) {
|
2016-07-16 22:57:06 -04:00
|
|
|
face_verts[j] = *corner;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
bool TopologyRefinerFactory<ccl::Mesh>::assignComponentTags(TopologyRefiner &refiner,
|
|
|
|
|
ccl::Mesh const &mesh)
|
|
|
|
|
{
|
Subdivision: add support for vertex creasing
This adds vertex creasing support for OpenSubDiv for modeling, rendering,
Alembic and USD I/O.
For modeling, vertex creasing follows the edge creasing implementation with an
operator accessible through the Vertex menu in Edit Mode, and some parameter in
the properties panel. The option in the Subsurf and Multires to use edge
creasing also affects vertex creasing.
The vertex crease data is stored as a CustomData layer, unlike edge creases
which for now are stored in `MEdge`, but will in the future also be moved to
a `CustomData` layer. See comments for details on the difference in behavior
for the `CD_CREASE` layer between egdes and vertices.
For Cycles this adds sockets on the Mesh node to hold data about which vertices
are creased (one socket for the indices, one for the weigths).
Viewport rendering of vertex creasing reuses the same color scheme as for edges
and creased vertices are drawn bigger than uncreased vertices.
For Alembic and USD, vertex crease support follows the edge crease
implementation, they are always read, but only exported if a `Subsurf` modifier
is present on the Mesh.
Reviewed By: brecht, fclem, sergey, sybren, campbellbarton
Differential Revision: https://developer.blender.org/D10145
2022-01-20 12:20:30 +01:00
|
|
|
/* Historical maximum crease weight used at Pixar, influencing the maximum in OpenSubDiv. */
|
|
|
|
|
static constexpr float CREASE_SCALE = 10.0f;
|
|
|
|
|
|
2020-11-04 11:17:38 +01:00
|
|
|
size_t num_creases = mesh.get_subd_creases_weight().size();
|
Subdivision: add support for vertex creasing
This adds vertex creasing support for OpenSubDiv for modeling, rendering,
Alembic and USD I/O.
For modeling, vertex creasing follows the edge creasing implementation with an
operator accessible through the Vertex menu in Edit Mode, and some parameter in
the properties panel. The option in the Subsurf and Multires to use edge
creasing also affects vertex creasing.
The vertex crease data is stored as a CustomData layer, unlike edge creases
which for now are stored in `MEdge`, but will in the future also be moved to
a `CustomData` layer. See comments for details on the difference in behavior
for the `CD_CREASE` layer between egdes and vertices.
For Cycles this adds sockets on the Mesh node to hold data about which vertices
are creased (one socket for the indices, one for the weigths).
Viewport rendering of vertex creasing reuses the same color scheme as for edges
and creased vertices are drawn bigger than uncreased vertices.
For Alembic and USD, vertex crease support follows the edge crease
implementation, they are always read, but only exported if a `Subsurf` modifier
is present on the Mesh.
Reviewed By: brecht, fclem, sergey, sybren, campbellbarton
Differential Revision: https://developer.blender.org/D10145
2022-01-20 12:20:30 +01:00
|
|
|
size_t num_vertex_creases = mesh.get_subd_vert_creases().size();
|
|
|
|
|
|
|
|
|
|
/* The last loop is over the vertices, so early exit to avoid iterating them needlessly. */
|
|
|
|
|
if (num_creases == 0 && num_vertex_creases == 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-07-16 22:57:06 -04:00
|
|
|
|
2020-11-04 11:17:38 +01:00
|
|
|
for (int i = 0; i < num_creases; i++) {
|
|
|
|
|
ccl::Mesh::SubdEdgeCrease crease = mesh.get_subd_crease(i);
|
|
|
|
|
Index edge = findBaseEdge(refiner, crease.v[0], crease.v[1]);
|
2016-07-16 22:57:06 -04:00
|
|
|
|
|
|
|
|
if (edge != INDEX_INVALID) {
|
Subdivision: add support for vertex creasing
This adds vertex creasing support for OpenSubDiv for modeling, rendering,
Alembic and USD I/O.
For modeling, vertex creasing follows the edge creasing implementation with an
operator accessible through the Vertex menu in Edit Mode, and some parameter in
the properties panel. The option in the Subsurf and Multires to use edge
creasing also affects vertex creasing.
The vertex crease data is stored as a CustomData layer, unlike edge creases
which for now are stored in `MEdge`, but will in the future also be moved to
a `CustomData` layer. See comments for details on the difference in behavior
for the `CD_CREASE` layer between egdes and vertices.
For Cycles this adds sockets on the Mesh node to hold data about which vertices
are creased (one socket for the indices, one for the weigths).
Viewport rendering of vertex creasing reuses the same color scheme as for edges
and creased vertices are drawn bigger than uncreased vertices.
For Alembic and USD, vertex crease support follows the edge crease
implementation, they are always read, but only exported if a `Subsurf` modifier
is present on the Mesh.
Reviewed By: brecht, fclem, sergey, sybren, campbellbarton
Differential Revision: https://developer.blender.org/D10145
2022-01-20 12:20:30 +01:00
|
|
|
setBaseEdgeSharpness(refiner, edge, crease.crease * CREASE_SCALE);
|
2016-07-16 22:57:06 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Subdivision: add support for vertex creasing
This adds vertex creasing support for OpenSubDiv for modeling, rendering,
Alembic and USD I/O.
For modeling, vertex creasing follows the edge creasing implementation with an
operator accessible through the Vertex menu in Edit Mode, and some parameter in
the properties panel. The option in the Subsurf and Multires to use edge
creasing also affects vertex creasing.
The vertex crease data is stored as a CustomData layer, unlike edge creases
which for now are stored in `MEdge`, but will in the future also be moved to
a `CustomData` layer. See comments for details on the difference in behavior
for the `CD_CREASE` layer between egdes and vertices.
For Cycles this adds sockets on the Mesh node to hold data about which vertices
are creased (one socket for the indices, one for the weigths).
Viewport rendering of vertex creasing reuses the same color scheme as for edges
and creased vertices are drawn bigger than uncreased vertices.
For Alembic and USD, vertex crease support follows the edge crease
implementation, they are always read, but only exported if a `Subsurf` modifier
is present on the Mesh.
Reviewed By: brecht, fclem, sergey, sybren, campbellbarton
Differential Revision: https://developer.blender.org/D10145
2022-01-20 12:20:30 +01:00
|
|
|
std::map<int, float> vertex_creases;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num_vertex_creases; ++i) {
|
|
|
|
|
const int vertex_idx = mesh.get_subd_vert_creases()[i];
|
|
|
|
|
const float weight = mesh.get_subd_vert_creases_weight()[i];
|
|
|
|
|
|
|
|
|
|
vertex_creases[vertex_idx] = weight * CREASE_SCALE;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-04 11:17:38 +01:00
|
|
|
for (int i = 0; i < mesh.get_verts().size(); i++) {
|
Subdivision: add support for vertex creasing
This adds vertex creasing support for OpenSubDiv for modeling, rendering,
Alembic and USD I/O.
For modeling, vertex creasing follows the edge creasing implementation with an
operator accessible through the Vertex menu in Edit Mode, and some parameter in
the properties panel. The option in the Subsurf and Multires to use edge
creasing also affects vertex creasing.
The vertex crease data is stored as a CustomData layer, unlike edge creases
which for now are stored in `MEdge`, but will in the future also be moved to
a `CustomData` layer. See comments for details on the difference in behavior
for the `CD_CREASE` layer between egdes and vertices.
For Cycles this adds sockets on the Mesh node to hold data about which vertices
are creased (one socket for the indices, one for the weigths).
Viewport rendering of vertex creasing reuses the same color scheme as for edges
and creased vertices are drawn bigger than uncreased vertices.
For Alembic and USD, vertex crease support follows the edge crease
implementation, they are always read, but only exported if a `Subsurf` modifier
is present on the Mesh.
Reviewed By: brecht, fclem, sergey, sybren, campbellbarton
Differential Revision: https://developer.blender.org/D10145
2022-01-20 12:20:30 +01:00
|
|
|
float sharpness = 0.0f;
|
|
|
|
|
std::map<int, float>::const_iterator iter = vertex_creases.find(i);
|
|
|
|
|
|
|
|
|
|
if (iter != vertex_creases.end()) {
|
|
|
|
|
sharpness = iter->second;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
ConstIndexArray vert_edges = getBaseVertexEdges(refiner, i);
|
|
|
|
|
|
|
|
|
|
if (vert_edges.size() == 2) {
|
Subdivision: add support for vertex creasing
This adds vertex creasing support for OpenSubDiv for modeling, rendering,
Alembic and USD I/O.
For modeling, vertex creasing follows the edge creasing implementation with an
operator accessible through the Vertex menu in Edit Mode, and some parameter in
the properties panel. The option in the Subsurf and Multires to use edge
creasing also affects vertex creasing.
The vertex crease data is stored as a CustomData layer, unlike edge creases
which for now are stored in `MEdge`, but will in the future also be moved to
a `CustomData` layer. See comments for details on the difference in behavior
for the `CD_CREASE` layer between egdes and vertices.
For Cycles this adds sockets on the Mesh node to hold data about which vertices
are creased (one socket for the indices, one for the weigths).
Viewport rendering of vertex creasing reuses the same color scheme as for edges
and creased vertices are drawn bigger than uncreased vertices.
For Alembic and USD, vertex crease support follows the edge crease
implementation, they are always read, but only exported if a `Subsurf` modifier
is present on the Mesh.
Reviewed By: brecht, fclem, sergey, sybren, campbellbarton
Differential Revision: https://developer.blender.org/D10145
2022-01-20 12:20:30 +01:00
|
|
|
const float sharpness0 = refiner.getLevel(0).getEdgeSharpness(vert_edges[0]);
|
|
|
|
|
const float sharpness1 = refiner.getLevel(0).getEdgeSharpness(vert_edges[1]);
|
|
|
|
|
|
|
|
|
|
sharpness += ccl::min(sharpness0, sharpness1);
|
|
|
|
|
sharpness = ccl::min(sharpness, CREASE_SCALE);
|
|
|
|
|
}
|
2016-07-16 22:57:06 -04:00
|
|
|
|
Subdivision: add support for vertex creasing
This adds vertex creasing support for OpenSubDiv for modeling, rendering,
Alembic and USD I/O.
For modeling, vertex creasing follows the edge creasing implementation with an
operator accessible through the Vertex menu in Edit Mode, and some parameter in
the properties panel. The option in the Subsurf and Multires to use edge
creasing also affects vertex creasing.
The vertex crease data is stored as a CustomData layer, unlike edge creases
which for now are stored in `MEdge`, but will in the future also be moved to
a `CustomData` layer. See comments for details on the difference in behavior
for the `CD_CREASE` layer between egdes and vertices.
For Cycles this adds sockets on the Mesh node to hold data about which vertices
are creased (one socket for the indices, one for the weigths).
Viewport rendering of vertex creasing reuses the same color scheme as for edges
and creased vertices are drawn bigger than uncreased vertices.
For Alembic and USD, vertex crease support follows the edge crease
implementation, they are always read, but only exported if a `Subsurf` modifier
is present on the Mesh.
Reviewed By: brecht, fclem, sergey, sybren, campbellbarton
Differential Revision: https://developer.blender.org/D10145
2022-01-20 12:20:30 +01:00
|
|
|
if (sharpness != 0.0f) {
|
2016-07-16 22:57:06 -04:00
|
|
|
setBaseVertexSharpness(refiner, i, sharpness);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
bool TopologyRefinerFactory<ccl::Mesh>::assignFaceVaryingTopology(TopologyRefiner & /*refiner*/,
|
|
|
|
|
ccl::Mesh const & /*mesh*/)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
void TopologyRefinerFactory<ccl::Mesh>::reportInvalidTopology(TopologyError /*err_code*/,
|
2016-09-26 03:42:09 +02:00
|
|
|
char const * /*msg*/,
|
|
|
|
|
ccl::Mesh const & /*mesh*/)
|
2016-07-16 22:57:06 -04:00
|
|
|
{
|
|
|
|
|
}
|
2024-12-26 17:53:59 +01:00
|
|
|
} // namespace OpenSubdiv::v3_6_0::Far
|
2016-07-16 22:57:06 -04:00
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
|
|
using namespace OpenSubdiv;
|
|
|
|
|
|
|
|
|
|
/* struct that implements OpenSubdiv's vertex interface */
|
|
|
|
|
|
|
|
|
|
template<typename T> struct OsdValue {
|
|
|
|
|
T value;
|
|
|
|
|
|
2024-12-26 17:53:59 +01:00
|
|
|
OsdValue() = default;
|
2016-07-16 22:57:06 -04:00
|
|
|
|
2024-12-26 17:53:59 +01:00
|
|
|
void Clear(void * /*unused*/ = nullptr)
|
2016-07-16 22:57:06 -04:00
|
|
|
{
|
|
|
|
|
memset(&value, 0, sizeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddWithWeight(OsdValue<T> const &src, float weight)
|
|
|
|
|
{
|
|
|
|
|
value += src.value * weight;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<> void OsdValue<uchar4>::AddWithWeight(OsdValue<uchar4> const &src, float weight)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
value[i] += (uchar)(src.value[i] * weight);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* class for holding OpenSubdiv data used during tessellation */
|
|
|
|
|
|
|
|
|
|
class OsdData {
|
2024-12-26 17:53:59 +01:00
|
|
|
Mesh *mesh = nullptr;
|
2016-07-16 22:57:06 -04:00
|
|
|
vector<OsdValue<float3>> verts;
|
2024-12-26 17:53:59 +01:00
|
|
|
Far::TopologyRefiner *refiner = nullptr;
|
|
|
|
|
Far::PatchTable *patch_table = nullptr;
|
|
|
|
|
Far::PatchMap *patch_map = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
public:
|
2024-12-26 17:53:59 +01:00
|
|
|
OsdData() = default;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
~OsdData()
|
|
|
|
|
{
|
|
|
|
|
delete refiner;
|
|
|
|
|
delete patch_table;
|
|
|
|
|
delete patch_map;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
void build_from_mesh(Mesh *mesh_)
|
|
|
|
|
{
|
|
|
|
|
mesh = mesh_;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
/* type and options */
|
|
|
|
|
Sdc::SchemeType type = Sdc::SCHEME_CATMARK;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
Sdc::Options options;
|
|
|
|
|
options.SetVtxBoundaryInterpolation(Sdc::Options::VTX_BOUNDARY_EDGE_ONLY);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
/* create refiner */
|
|
|
|
|
refiner = Far::TopologyRefinerFactory<Mesh>::Create(
|
|
|
|
|
*mesh, Far::TopologyRefinerFactory<Mesh>::Options(type, options));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
/* adaptive refinement */
|
2016-09-16 18:07:24 -04:00
|
|
|
int max_isolation = calculate_max_isolation();
|
2016-07-16 22:57:06 -04:00
|
|
|
refiner->RefineAdaptive(Far::TopologyRefiner::AdaptiveOptions(max_isolation));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
/* create patch table */
|
|
|
|
|
Far::PatchTableFactory::Options patch_options;
|
|
|
|
|
patch_options.endCapType = Far::PatchTableFactory::Options::ENDCAP_GREGORY_BASIS;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
patch_table = Far::PatchTableFactory::Create(*refiner, patch_options);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
/* interpolate verts */
|
|
|
|
|
int num_refiner_verts = refiner->GetNumVerticesTotal();
|
|
|
|
|
int num_local_points = patch_table->GetNumLocalPoints();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
verts.resize(num_refiner_verts + num_local_points);
|
2020-11-04 11:17:38 +01:00
|
|
|
for (int i = 0; i < mesh->get_verts().size(); i++) {
|
|
|
|
|
verts[i].value = mesh->get_verts()[i];
|
2016-07-16 22:57:06 -04:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-08-17 23:49:55 +02:00
|
|
|
OsdValue<float3> *src = verts.data();
|
2016-07-16 22:57:06 -04:00
|
|
|
for (int i = 0; i < refiner->GetMaxLevel(); i++) {
|
|
|
|
|
OsdValue<float3> *dest = src + refiner->GetLevel(i).GetNumVertices();
|
|
|
|
|
Far::PrimvarRefiner(*refiner).Interpolate(i + 1, src, dest);
|
|
|
|
|
src = dest;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 06:32:39 -04:00
|
|
|
if (num_local_points) {
|
2024-12-26 17:53:59 +01:00
|
|
|
patch_table->ComputeLocalPointValues(verts.data(), &verts[num_refiner_verts]);
|
2018-04-03 23:36:35 -04:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
/* create patch map */
|
|
|
|
|
patch_map = new Far::PatchMap(*patch_table);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
void subdivide_attribute(Attribute &attr)
|
|
|
|
|
{
|
|
|
|
|
Far::PrimvarRefiner primvar_refiner(*refiner);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
if (attr.element == ATTR_ELEMENT_VERTEX) {
|
|
|
|
|
int num_refiner_verts = refiner->GetNumVerticesTotal();
|
|
|
|
|
int num_local_points = patch_table->GetNumLocalPoints();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
attr.resize(num_refiner_verts + num_local_points);
|
|
|
|
|
attr.flags |= ATTR_FINAL_SIZE;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-08-17 23:49:55 +02:00
|
|
|
char *src = attr.buffer.data();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
for (int i = 0; i < refiner->GetMaxLevel(); i++) {
|
|
|
|
|
char *dest = src + refiner->GetLevel(i).GetNumVertices() * attr.data_sizeof();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-12-26 17:53:59 +01:00
|
|
|
if (ccl::Attribute::same_storage(attr.type, TypeFloat)) {
|
2016-07-16 22:57:06 -04:00
|
|
|
primvar_refiner.Interpolate(i + 1, (OsdValue<float> *)src, (OsdValue<float> *&)dest);
|
|
|
|
|
}
|
2024-12-26 17:53:59 +01:00
|
|
|
else if (ccl::Attribute::same_storage(attr.type, TypeFloat2)) {
|
2019-03-05 14:54:54 +01:00
|
|
|
primvar_refiner.Interpolate(i + 1, (OsdValue<float2> *)src, (OsdValue<float2> *&)dest);
|
2023-02-27 10:39:19 +01:00
|
|
|
// float3 is not interchangeable with float4 and so needs to be handled
|
|
|
|
|
// separately
|
2019-03-05 14:54:54 +01:00
|
|
|
}
|
2024-12-26 17:53:59 +01:00
|
|
|
else if (ccl::Attribute::same_storage(attr.type, TypeFloat4)) {
|
2016-07-16 22:57:06 -04:00
|
|
|
primvar_refiner.Interpolate(i + 1, (OsdValue<float4> *)src, (OsdValue<float4> *&)dest);
|
|
|
|
|
}
|
2023-02-27 10:39:19 +01:00
|
|
|
else {
|
|
|
|
|
primvar_refiner.Interpolate(i + 1, (OsdValue<float3> *)src, (OsdValue<float3> *&)dest);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
src = dest;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 06:32:39 -04:00
|
|
|
if (num_local_points) {
|
2024-12-26 17:53:59 +01:00
|
|
|
if (ccl::Attribute::same_storage(attr.type, TypeFloat)) {
|
2018-04-03 23:36:35 -04:00
|
|
|
patch_table->ComputeLocalPointValues(
|
2024-12-26 17:53:59 +01:00
|
|
|
(OsdValue<float> *)attr.buffer.data(),
|
2018-04-03 23:36:35 -04:00
|
|
|
(OsdValue<float> *)&attr.buffer[num_refiner_verts * attr.data_sizeof()]);
|
2019-03-05 14:54:54 +01:00
|
|
|
}
|
2024-12-26 17:53:59 +01:00
|
|
|
else if (ccl::Attribute::same_storage(attr.type, TypeFloat2)) {
|
2019-03-05 14:54:54 +01:00
|
|
|
patch_table->ComputeLocalPointValues(
|
2024-12-26 17:53:59 +01:00
|
|
|
(OsdValue<float2> *)attr.buffer.data(),
|
2019-03-05 14:54:54 +01:00
|
|
|
(OsdValue<float2> *)&attr.buffer[num_refiner_verts * attr.data_sizeof()]);
|
2018-04-03 23:36:35 -04:00
|
|
|
}
|
2024-12-26 17:53:59 +01:00
|
|
|
else if (ccl::Attribute::same_storage(attr.type, TypeFloat4)) {
|
2023-02-27 10:39:19 +01:00
|
|
|
// float3 is not interchangeable with float4 and so needs to be handled
|
|
|
|
|
// separately
|
2018-04-03 23:36:35 -04:00
|
|
|
patch_table->ComputeLocalPointValues(
|
2024-12-26 17:53:59 +01:00
|
|
|
(OsdValue<float4> *)attr.buffer.data(),
|
2018-04-03 23:36:35 -04:00
|
|
|
(OsdValue<float4> *)&attr.buffer[num_refiner_verts * attr.data_sizeof()]);
|
2016-07-16 22:57:06 -04:00
|
|
|
}
|
2023-02-27 10:39:19 +01:00
|
|
|
else {
|
|
|
|
|
// float3 is not interchangeable with float4 and so needs to be handled
|
|
|
|
|
// separately
|
|
|
|
|
patch_table->ComputeLocalPointValues(
|
2024-12-26 17:53:59 +01:00
|
|
|
(OsdValue<float3> *)attr.buffer.data(),
|
2023-02-27 10:39:19 +01:00
|
|
|
(OsdValue<float3> *)&attr.buffer[num_refiner_verts * attr.data_sizeof()]);
|
|
|
|
|
}
|
2016-07-16 22:57:06 -04:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2016-07-16 22:57:06 -04:00
|
|
|
else if (attr.element == ATTR_ELEMENT_CORNER || attr.element == ATTR_ELEMENT_CORNER_BYTE) {
|
|
|
|
|
// TODO(mai): fvar interpolation
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2016-09-16 18:07:24 -04:00
|
|
|
int calculate_max_isolation()
|
|
|
|
|
{
|
|
|
|
|
/* loop over all edges to find longest in screen space */
|
|
|
|
|
const Far::TopologyLevel &level = refiner->GetLevel(0);
|
2020-11-04 11:17:38 +01:00
|
|
|
const SubdParams *subd_params = mesh->get_subd_params();
|
|
|
|
|
Transform objecttoworld = subd_params->objecttoworld;
|
|
|
|
|
Camera *cam = subd_params->camera;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-09-16 18:07:24 -04:00
|
|
|
float longest_edge = 0.0f;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-09-16 18:07:24 -04:00
|
|
|
for (size_t i = 0; i < level.GetNumEdges(); i++) {
|
|
|
|
|
Far::ConstIndexArray verts = level.GetEdgeVertices(i);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-11-04 11:17:38 +01:00
|
|
|
float3 a = mesh->get_verts()[verts[0]];
|
|
|
|
|
float3 b = mesh->get_verts()[verts[1]];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-09-16 18:07:24 -04:00
|
|
|
float edge_len;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-09-16 18:07:24 -04:00
|
|
|
if (cam) {
|
|
|
|
|
a = transform_point(&objecttoworld, a);
|
|
|
|
|
b = transform_point(&objecttoworld, b);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-09-16 18:07:24 -04:00
|
|
|
edge_len = len(a - b) / cam->world_to_raster_size((a + b) * 0.5f);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
edge_len = len(a - b);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-09-16 18:07:24 -04:00
|
|
|
longest_edge = max(longest_edge, edge_len);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-09-16 18:07:24 -04:00
|
|
|
/* calculate isolation level */
|
2020-11-04 11:17:38 +01:00
|
|
|
int isolation = (int)(log2f(max(longest_edge / subd_params->dicing_rate, 1.0f)) + 1.0f);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-09-16 18:07:24 -04:00
|
|
|
return min(isolation, 10);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
friend struct OsdPatch;
|
|
|
|
|
friend class Mesh;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* ccl::Patch implementation that uses OpenSubdiv for eval */
|
|
|
|
|
|
|
|
|
|
struct OsdPatch : Patch {
|
|
|
|
|
OsdData *osd_data;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-12-26 17:53:59 +01:00
|
|
|
OsdPatch() = default;
|
2023-03-29 16:50:54 +02:00
|
|
|
OsdPatch(OsdData *data) : osd_data(data) {}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-12-26 17:53:59 +01:00
|
|
|
void eval(float3 *P, float3 *dPdu, float3 *dPdv, float3 *N, float u, float v) override
|
2016-07-16 22:57:06 -04:00
|
|
|
{
|
2021-11-17 17:22:05 +01:00
|
|
|
const Far::PatchTable::PatchHandle *handle = osd_data->patch_map->FindPatch(
|
|
|
|
|
patch_index, (double)u, (double)v);
|
2016-07-16 22:57:06 -04:00
|
|
|
assert(handle);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
float p_weights[20], du_weights[20], dv_weights[20];
|
|
|
|
|
osd_data->patch_table->EvaluateBasis(*handle, u, v, p_weights, du_weights, dv_weights);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
Far::ConstIndexArray cv = osd_data->patch_table->GetPatchVertices(*handle);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
float3 du, dv;
|
2024-12-26 17:53:59 +01:00
|
|
|
if (P) {
|
2021-02-17 01:47:18 +01:00
|
|
|
*P = zero_float3();
|
2024-12-26 17:53:59 +01:00
|
|
|
}
|
2021-02-17 01:47:18 +01:00
|
|
|
du = zero_float3();
|
|
|
|
|
dv = zero_float3();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
for (int i = 0; i < cv.size(); i++) {
|
|
|
|
|
float3 p = osd_data->verts[cv[i]].value;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-12-26 17:53:59 +01:00
|
|
|
if (P) {
|
2016-07-16 22:57:06 -04:00
|
|
|
*P += p * p_weights[i];
|
2024-12-26 17:53:59 +01:00
|
|
|
}
|
2016-07-16 22:57:06 -04:00
|
|
|
du += p * du_weights[i];
|
|
|
|
|
dv += p * dv_weights[i];
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-12-26 17:53:59 +01:00
|
|
|
if (dPdu) {
|
2016-07-16 22:57:06 -04:00
|
|
|
*dPdu = du;
|
2024-12-26 17:53:59 +01:00
|
|
|
}
|
|
|
|
|
if (dPdv) {
|
2016-07-16 22:57:06 -04:00
|
|
|
*dPdv = dv;
|
2024-12-26 17:53:59 +01:00
|
|
|
}
|
2016-09-14 19:34:43 -04:00
|
|
|
if (N) {
|
|
|
|
|
*N = cross(du, dv);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-09-14 19:34:43 -04:00
|
|
|
float t = len(*N);
|
|
|
|
|
*N = (t != 0.0f) ? *N / t : make_float3(0.0f, 0.0f, 1.0f);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2016-09-14 19:34:43 -04:00
|
|
|
}
|
2016-07-16 22:57:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
void Mesh::tessellate(DiagSplit *split)
|
|
|
|
|
{
|
2020-11-04 11:17:38 +01:00
|
|
|
/* reset the number of subdivision vertices, in case the Mesh was not cleared
|
|
|
|
|
* between calls or data updates */
|
|
|
|
|
num_subd_verts = 0;
|
|
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
|
OsdData osd_data;
|
|
|
|
|
bool need_packed_patch_table = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
if (subdivision_type == SUBDIVISION_CATMULL_CLARK) {
|
2020-11-04 11:17:38 +01:00
|
|
|
if (get_num_subd_faces()) {
|
2016-08-23 14:49:10 -04:00
|
|
|
osd_data.build_from_mesh(this);
|
|
|
|
|
}
|
2016-07-16 22:57:06 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
/* force linear subdivision if OpenSubdiv is unavailable to avoid
|
|
|
|
|
* falling into catmull-clark code paths by accident
|
|
|
|
|
*/
|
|
|
|
|
subdivision_type = SUBDIVISION_LINEAR;
|
|
|
|
|
|
|
|
|
|
/* force disable attribute subdivision for same reason as above */
|
2024-12-26 19:41:25 +01:00
|
|
|
for (Attribute &attr : subd_attributes.attributes) {
|
2016-07-16 22:57:06 -04:00
|
|
|
attr.flags &= ~ATTR_SUBDIVIDED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-04 11:17:38 +01:00
|
|
|
int num_faces = get_num_subd_faces();
|
2016-07-16 19:42:28 -04:00
|
|
|
|
|
|
|
|
Attribute *attr_vN = subd_attributes.find(ATTR_STD_VERTEX_NORMAL);
|
2024-12-26 17:53:55 +01:00
|
|
|
float3 *vN = (attr_vN) ? attr_vN->data_float3() : nullptr;
|
2016-07-16 19:42:28 -04:00
|
|
|
|
2019-08-14 01:53:09 -04:00
|
|
|
/* count patches */
|
|
|
|
|
int num_patches = 0;
|
2016-07-16 19:42:28 -04:00
|
|
|
for (int f = 0; f < num_faces; f++) {
|
2020-11-04 11:17:38 +01:00
|
|
|
SubdFace face = get_subd_face(f);
|
2016-07-16 19:42:28 -04:00
|
|
|
|
|
|
|
|
if (face.is_quad()) {
|
2019-08-14 01:53:09 -04:00
|
|
|
num_patches++;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
num_patches += face.num_corners;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-16 19:42:28 -04:00
|
|
|
|
2019-08-14 01:53:09 -04:00
|
|
|
/* build patches from faces */
|
2016-07-16 22:57:06 -04:00
|
|
|
#ifdef WITH_OPENSUBDIV
|
2019-08-14 01:53:09 -04:00
|
|
|
if (subdivision_type == SUBDIVISION_CATMULL_CLARK) {
|
|
|
|
|
vector<OsdPatch> osd_patches(num_patches, &osd_data);
|
|
|
|
|
OsdPatch *patch = osd_patches.data();
|
2016-07-16 19:42:28 -04:00
|
|
|
|
2019-08-14 01:53:09 -04:00
|
|
|
for (int f = 0; f < num_faces; f++) {
|
2020-11-04 11:17:38 +01:00
|
|
|
SubdFace face = get_subd_face(f);
|
2016-07-16 22:57:06 -04:00
|
|
|
|
2019-08-14 01:53:09 -04:00
|
|
|
if (face.is_quad()) {
|
|
|
|
|
patch->patch_index = face.ptex_offset;
|
|
|
|
|
patch->from_ngon = false;
|
|
|
|
|
patch->shader = face.shader;
|
|
|
|
|
patch++;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
for (int corner = 0; corner < face.num_corners; corner++) {
|
|
|
|
|
patch->patch_index = face.ptex_offset + corner;
|
|
|
|
|
patch->from_ngon = true;
|
|
|
|
|
patch->shader = face.shader;
|
|
|
|
|
patch++;
|
|
|
|
|
}
|
2016-07-16 19:42:28 -04:00
|
|
|
}
|
2019-08-14 01:53:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* split patches */
|
|
|
|
|
split->split_patches(osd_patches.data(), sizeof(OsdPatch));
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-07-16 22:57:06 -04:00
|
|
|
#endif
|
2019-08-14 01:53:09 -04:00
|
|
|
{
|
|
|
|
|
vector<LinearQuadPatch> linear_patches(num_patches);
|
|
|
|
|
LinearQuadPatch *patch = linear_patches.data();
|
|
|
|
|
|
|
|
|
|
for (int f = 0; f < num_faces; f++) {
|
2020-11-04 11:17:38 +01:00
|
|
|
SubdFace face = get_subd_face(f);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-08-14 01:53:09 -04:00
|
|
|
if (face.is_quad()) {
|
|
|
|
|
float3 *hull = patch->hull;
|
|
|
|
|
float3 *normals = patch->normals;
|
|
|
|
|
|
|
|
|
|
patch->patch_index = face.ptex_offset;
|
|
|
|
|
patch->from_ngon = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2016-07-16 22:57:06 -04:00
|
|
|
hull[i] = verts[subd_face_corners[face.start_corner + i]];
|
2016-07-16 19:42:28 -04:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
if (face.smooth) {
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
normals[i] = vN[subd_face_corners[face.start_corner + i]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
float3 N = face.normal(this);
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
normals[i] = N;
|
|
|
|
|
}
|
2016-07-16 19:42:28 -04:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
swap(hull[2], hull[3]);
|
|
|
|
|
swap(normals[2], normals[3]);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-08-14 01:53:09 -04:00
|
|
|
patch->shader = face.shader;
|
|
|
|
|
patch++;
|
2016-07-16 19:42:28 -04:00
|
|
|
}
|
2019-08-14 01:53:09 -04:00
|
|
|
else {
|
|
|
|
|
/* ngon */
|
2021-02-17 01:47:18 +01:00
|
|
|
float3 center_vert = zero_float3();
|
|
|
|
|
float3 center_normal = zero_float3();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
float inv_num_corners = 1.0f / float(face.num_corners);
|
|
|
|
|
for (int corner = 0; corner < face.num_corners; corner++) {
|
|
|
|
|
center_vert += verts[subd_face_corners[face.start_corner + corner]] * inv_num_corners;
|
|
|
|
|
center_normal += vN[subd_face_corners[face.start_corner + corner]] * inv_num_corners;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
for (int corner = 0; corner < face.num_corners; corner++) {
|
2019-08-14 01:53:09 -04:00
|
|
|
float3 *hull = patch->hull;
|
|
|
|
|
float3 *normals = patch->normals;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-08-14 01:53:09 -04:00
|
|
|
patch->patch_index = face.ptex_offset + corner;
|
|
|
|
|
patch->from_ngon = true;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-08-14 01:53:09 -04:00
|
|
|
patch->shader = face.shader;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
hull[0] =
|
|
|
|
|
verts[subd_face_corners[face.start_corner + mod(corner + 0, face.num_corners)]];
|
|
|
|
|
hull[1] =
|
|
|
|
|
verts[subd_face_corners[face.start_corner + mod(corner + 1, face.num_corners)]];
|
|
|
|
|
hull[2] =
|
|
|
|
|
verts[subd_face_corners[face.start_corner + mod(corner - 1, face.num_corners)]];
|
|
|
|
|
hull[3] = center_vert;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
hull[1] = (hull[1] + hull[0]) * 0.5;
|
|
|
|
|
hull[2] = (hull[2] + hull[0]) * 0.5;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
if (face.smooth) {
|
|
|
|
|
normals[0] =
|
|
|
|
|
vN[subd_face_corners[face.start_corner + mod(corner + 0, face.num_corners)]];
|
|
|
|
|
normals[1] =
|
|
|
|
|
vN[subd_face_corners[face.start_corner + mod(corner + 1, face.num_corners)]];
|
|
|
|
|
normals[2] =
|
|
|
|
|
vN[subd_face_corners[face.start_corner + mod(corner - 1, face.num_corners)]];
|
|
|
|
|
normals[3] = center_normal;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
normals[1] = (normals[1] + normals[0]) * 0.5;
|
|
|
|
|
normals[2] = (normals[2] + normals[0]) * 0.5;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
float3 N = face.normal(this);
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
normals[i] = N;
|
2016-07-16 19:42:28 -04:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-14 01:53:09 -04:00
|
|
|
patch++;
|
2016-07-16 19:42:28 -04:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2019-08-14 01:53:09 -04:00
|
|
|
|
|
|
|
|
/* split patches */
|
|
|
|
|
split->split_patches(linear_patches.data(), sizeof(LinearQuadPatch));
|
2016-07-16 19:42:28 -04:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
/* interpolate center points for attributes */
|
2024-12-26 19:41:25 +01:00
|
|
|
for (Attribute &attr : subd_attributes.attributes) {
|
2016-07-16 22:57:06 -04:00
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
|
if (subdivision_type == SUBDIVISION_CATMULL_CLARK && attr.flags & ATTR_SUBDIVIDED) {
|
|
|
|
|
if (attr.element == ATTR_ELEMENT_CORNER || attr.element == ATTR_ELEMENT_CORNER_BYTE) {
|
|
|
|
|
/* keep subdivision for corner attributes disabled for now */
|
|
|
|
|
attr.flags &= ~ATTR_SUBDIVIDED;
|
|
|
|
|
}
|
2020-11-04 11:17:38 +01:00
|
|
|
else if (get_num_subd_faces()) {
|
2016-07-16 22:57:06 -04:00
|
|
|
osd_data.subdivide_attribute(attr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 22:57:06 -04:00
|
|
|
need_packed_patch_table = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
char *data = attr.data();
|
|
|
|
|
size_t stride = attr.data_sizeof();
|
|
|
|
|
int ngons = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
switch (attr.element) {
|
|
|
|
|
case ATTR_ELEMENT_VERTEX: {
|
|
|
|
|
for (int f = 0; f < num_faces; f++) {
|
2020-11-04 11:17:38 +01:00
|
|
|
SubdFace face = get_subd_face(f);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
if (!face.is_quad()) {
|
|
|
|
|
char *center = data + (verts.size() - num_subd_verts + ngons) * stride;
|
|
|
|
|
attr.zero_data(center);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
float inv_num_corners = 1.0f / float(face.num_corners);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
for (int corner = 0; corner < face.num_corners; corner++) {
|
|
|
|
|
attr.add_with_weight(center,
|
|
|
|
|
data + subd_face_corners[face.start_corner + corner] * stride,
|
|
|
|
|
inv_num_corners);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
ngons++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-23 21:10:22 +10:00
|
|
|
break;
|
|
|
|
|
}
|
2016-07-16 19:42:28 -04:00
|
|
|
case ATTR_ELEMENT_VERTEX_MOTION: {
|
|
|
|
|
// TODO(mai): implement
|
2023-09-23 21:10:22 +10:00
|
|
|
break;
|
|
|
|
|
}
|
2016-07-16 19:42:28 -04:00
|
|
|
case ATTR_ELEMENT_CORNER: {
|
|
|
|
|
for (int f = 0; f < num_faces; f++) {
|
2020-11-04 11:17:38 +01:00
|
|
|
SubdFace face = get_subd_face(f);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
if (!face.is_quad()) {
|
|
|
|
|
char *center = data + (subd_face_corners.size() + ngons) * stride;
|
|
|
|
|
attr.zero_data(center);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
float inv_num_corners = 1.0f / float(face.num_corners);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
for (int corner = 0; corner < face.num_corners; corner++) {
|
|
|
|
|
attr.add_with_weight(
|
|
|
|
|
center, data + (face.start_corner + corner) * stride, inv_num_corners);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
ngons++;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2016-07-16 19:42:28 -04:00
|
|
|
}
|
2023-09-23 21:10:22 +10:00
|
|
|
break;
|
|
|
|
|
}
|
2016-07-16 19:42:28 -04:00
|
|
|
case ATTR_ELEMENT_CORNER_BYTE: {
|
|
|
|
|
for (int f = 0; f < num_faces; f++) {
|
2020-11-04 11:17:38 +01:00
|
|
|
SubdFace face = get_subd_face(f);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
if (!face.is_quad()) {
|
|
|
|
|
uchar *center = (uchar *)data + (subd_face_corners.size() + ngons) * stride;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
float inv_num_corners = 1.0f / float(face.num_corners);
|
2021-02-17 01:47:18 +01:00
|
|
|
float4 val = zero_float4();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
for (int corner = 0; corner < face.num_corners; corner++) {
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
val[i] += float(*(data + (face.start_corner + corner) * stride + i)) *
|
|
|
|
|
inv_num_corners;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2016-07-16 19:42:28 -04:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
center[i] = uchar(min(max(val[i], 0.0f), 255.0f));
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-07-16 19:42:28 -04:00
|
|
|
ngons++;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2016-07-16 19:42:28 -04:00
|
|
|
}
|
2023-09-23 21:10:22 +10:00
|
|
|
break;
|
|
|
|
|
}
|
2016-07-16 19:42:28 -04:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-16 22:57:06 -04:00
|
|
|
|
|
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
|
/* pack patch tables */
|
|
|
|
|
if (need_packed_patch_table) {
|
|
|
|
|
delete patch_table;
|
|
|
|
|
patch_table = new PackedPatchTable;
|
|
|
|
|
patch_table->pack(osd_data.patch_table);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2016-07-16 19:42:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|