File: meshLoader.h

package info (click to toggle)
opensubdiv 3.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,132 kB
  • sloc: cpp: 137,820; python: 1,069; objc: 412; javascript: 361; lisp: 216; ansic: 170; makefile: 24
file content (209 lines) | stat: -rw-r--r-- 7,276 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
//   Copyright 2021 Pixar
//
//   Licensed under the Apache License, Version 2.0 (the "Apache License")
//   with the following modification; you may not use this file except in
//   compliance with the Apache License and the following modification to it:
//   Section 6. Trademarks. is deleted and replaced with:
//
//   6. Trademarks. This License does not grant permission to use the trade
//      names, trademarks, service marks, or product names of the Licensor
//      and its affiliates, except as required to comply with Section 4(c) of
//      the License and to reproduce the content of the NOTICE file.
//
//   You may obtain a copy of the Apache License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the Apache License with the above modification is
//   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//   KIND, either express or implied. See the Apache License for the specific
//   language governing permissions and limitations under the Apache License.
//

#include "../../../regression/common/far_utils.h"

#include <opensubdiv/far/topologyRefiner.h>
#include <opensubdiv/far/topologyDescriptor.h>

#include <cstdio>
#include <cstring>
#include <fstream>
#include <sstream>

//  Utilities local to this tutorial:
namespace tutorial {

using namespace OpenSubdiv;

//
//  Create a TopologyRefiner from default geometry:
//
Far::TopologyRefiner *
dfltTopologyRefiner(std::vector<float> & posVector,
                    std::vector<float> & uvVector) {

    //
    //  Default topology and positions for a cube:
    //
    int dfltNumFaces = 6;
    int dfltNumVerts = 8;
    int dfltNumUVs   = 16;

    int dfltFaceSizes[6] = { 4, 4, 4, 4, 4, 4 };

    int dfltFaceVerts[24] = { 0, 1, 3, 2,
                              2, 3, 5, 4,
                              4, 5, 7, 6,
                              6, 7, 1, 0,
                              1, 7, 5, 3,
                              6, 0, 2, 4 };

    float dfltPositions[8][3] = {{ -0.5f, -0.5f,  0.5f },
                                 {  0.5f, -0.5f,  0.5f },
                                 { -0.5f,  0.5f,  0.5f },
                                 {  0.5f,  0.5f,  0.5f },
                                 { -0.5f,  0.5f, -0.5f },
                                 {  0.5f,  0.5f, -0.5f },
                                 { -0.5f, -0.5f, -0.5f },
                                 {  0.5f, -0.5f, -0.5f }};

    int dfltFaceFVars[24] = {  9, 10, 14, 13,
                               4,  0,  1,  5,
                               5,  1,  2,  6,
                               6,  2,  3,  7,
                              10, 11, 15, 14,
                               8,  9, 13, 12 };

    float dfltUVs[16][2] = {{ 0.05f, 0.05f },
                            { 0.35f, 0.15f },
                            { 0.65f, 0.15f },
                            { 0.95f, 0.05f },
                            { 0.05f, 0.35f },
                            { 0.35f, 0.45f },
                            { 0.65f, 0.45f },
                            { 0.95f, 0.35f },
                            { 0.05f, 0.65f },
                            { 0.35f, 0.55f },
                            { 0.65f, 0.55f },
                            { 0.95f, 0.65f },
                            { 0.05f, 0.95f },
                            { 0.35f, 0.85f },
                            { 0.65f, 0.85f },
                            { 0.95f, 0.95f }};

    posVector.resize(8 * 3);
    std::memcpy(&posVector[0], dfltPositions, 8 * 3 * sizeof(float));

    uvVector.resize(16 * 2);
    std::memcpy(&uvVector[0], dfltUVs, 16 * 2 * sizeof(float));

    //
    //  Initialize a Far::TopologyDescriptor, from which to create
    //  the Far::TopologyRefiner:
    //
    typedef Far::TopologyDescriptor Descriptor;

    Descriptor::FVarChannel uvChannel;
    uvChannel.numValues    = dfltNumUVs;
    uvChannel.valueIndices = dfltFaceFVars;

    Descriptor topDescriptor;
    topDescriptor.numVertices        = dfltNumVerts;
    topDescriptor.numFaces           = dfltNumFaces;
    topDescriptor.numVertsPerFace    = dfltFaceSizes;
    topDescriptor.vertIndicesPerFace = dfltFaceVerts;
    topDescriptor.numFVarChannels    = 1;
    topDescriptor.fvarChannels       = &uvChannel;

    Sdc::SchemeType schemeType = Sdc::SCHEME_CATMARK;

    Sdc::Options schemeOptions;
    schemeOptions.SetVtxBoundaryInterpolation(
                            Sdc::Options::VTX_BOUNDARY_EDGE_ONLY);
    schemeOptions.SetFVarLinearInterpolation(
                            Sdc::Options::FVAR_LINEAR_CORNERS_ONLY);

    typedef Far::TopologyRefinerFactory<Descriptor> RefinerFactory;

    Far::TopologyRefiner * topRefiner =
        RefinerFactory::Create(topDescriptor,
            RefinerFactory::Options(schemeType, schemeOptions));
    assert(topRefiner);
    return topRefiner;
}

//
//  Create a TopologyRefiner from a specified Obj file:
//
Far::TopologyRefiner *
readTopologyRefiner(std::string const & objFileName,
                    Sdc::SchemeType schemeType,
                    std::vector<float> & posVector,
                    std::vector<float> & uvVector) {

    const char *  filename = objFileName.c_str();
    const Shape * shape = 0;

    std::ifstream ifs(filename);
    if (ifs) {
        std::stringstream ss;
        ss << ifs.rdbuf();
        ifs.close();
        std::string shapeString = ss.str();

        shape = Shape::parseObj(
            shapeString.c_str(), ConvertSdcTypeToShapeScheme(schemeType), false);
        if (shape == 0) {
            fprintf(stderr,
                "Error:  Cannot create Shape from Obj file '%s'\n", filename);
            return 0;
        }
    } else {
        fprintf(stderr, "Error:  Cannot open Obj file '%s'\n", filename);
        return 0;
    }

    Sdc::SchemeType sdcType    = GetSdcType(*shape);
    Sdc::Options    sdcOptions = GetSdcOptions(*shape);

    Far::TopologyRefiner * refiner = Far::TopologyRefinerFactory<Shape>::Create(
        *shape, Far::TopologyRefinerFactory<Shape>::Options(sdcType, sdcOptions));
    if (refiner == 0) {
        fprintf(stderr,
            "Error:  Unable to construct TopologyRefiner from Obj file '%s'\n",
            filename);
        return 0;
    }

    int numVertices = refiner->GetNumVerticesTotal();
    posVector.resize(numVertices * 3);
    std::memcpy(&posVector[0], &shape->verts[0], 3*numVertices*sizeof(float));

    uvVector.resize(0);
    if (refiner->GetNumFVarChannels()) {
        int numUVs = refiner->GetNumFVarValuesTotal(0);
        uvVector.resize(numUVs * 2);
        std::memcpy(&uvVector[0], &shape->uvs[0], 2 * numUVs*sizeof(float));
    }

    delete shape;
    return refiner;
}

Far::TopologyRefiner *
createTopologyRefiner(std::string const & objFileName,
                      Sdc::SchemeType schemeType,
                      std::vector<float> & posVector,
                      std::vector<float> & uvVector) {

    if (objFileName.empty()) {
        return dfltTopologyRefiner(posVector, uvVector);
    } else {
        return readTopologyRefiner(objFileName, schemeType,
                                   posVector, uvVector);
    }
}

} // end namespace