1 //
2 //   Copyright 2015 Pixar
3 //
4 //   Licensed under the Apache License, Version 2.0 (the "Apache License")
5 //   with the following modification; you may not use this file except in
6 //   compliance with the Apache License and the following modification to it:
7 //   Section 6. Trademarks. is deleted and replaced with:
8 //
9 //   6. Trademarks. This License does not grant permission to use the trade
10 //      names, trademarks, service marks, or product names of the Licensor
11 //      and its affiliates, except as required to comply with Section 4(c) of
12 //      the License and to reproduce the content of the NOTICE file.
13 //
14 //   You may obtain a copy of the Apache License at
15 //
16 //       http://www.apache.org/licenses/LICENSE-2.0
17 //
18 //   Unless required by applicable law or agreed to in writing, software
19 //   distributed under the Apache License with the above modification is
20 //   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 //   KIND, either express or implied. See the Apache License for the specific
22 //   language governing permissions and limitations under the Apache License.
23 //
24 
25 #ifndef OPENSUBDIV_EXAMPLES_GL_SHARE_TOPOLOGY_SCENE_BASE_H
26 #define OPENSUBDIV_EXAMPLES_GL_SHARE_TOPOLOGY_SCENE_BASE_H
27 
28 #include "glLoader.h"
29 
30 #include <opensubdiv/far/patchDescriptor.h>
31 #include <opensubdiv/far/patchTable.h>
32 #include <opensubdiv/osd/bufferDescriptor.h>
33 
34 struct Shape;
35 
36 class SceneBase {
37 public:
38     enum EndCap      { kEndCapBSplineBasis,
39                        kEndCapGregoryBasis };
40 
41     struct Options {
OptionsOptions42         Options() : adaptive(true), endCap(kEndCapGregoryBasis) { }
43 
44         bool adaptive;
45         int endCap;
46     };
47 
48     struct Object {
49         int topologyIndex;
50         int vertsOffset;        // an offset within the VBO
51     };
52 
53     struct PatchArray {
54         OpenSubdiv::Far::PatchDescriptor desc;
55         int numPatches;
56         int indexOffset;        // an offset within the index buffer
57         int primitiveIDOffset;  // an offset within the patch param buffer
58     };
59     typedef std::vector<PatchArray> PatchArrayVector;
60 
61     struct Topology {
62         int numVerts;
63         PatchArrayVector patchArrays;
64         std::vector<float> restPosition;
65     };
66 
67     struct Batch {
68         OpenSubdiv::Far::PatchDescriptor desc;
69         int count;
70         int stride;
71         GLuint dispatchBuffer;
72     };
73     typedef std::vector<Batch> BatchVector;
74 
75     /// Constructor.
76     SceneBase(Options const &options);
77 
78     /// Destructor.
79     virtual ~SceneBase();
80 
81     /// trivial accessors
GetNumObjects()82     int GetNumObjects() const { return (int)_objects.size(); }
83 
GetPatchArrays(int object)84     PatchArrayVector const &GetPatchArrays(int object) const {
85         return _topologies[_objects[object].topologyIndex].patchArrays;
86     }
87 
GetBatches()88     BatchVector const &GetBatches() {
89         if (_batches.empty()) buildBatches();
90         return _batches;
91     }
92 
GetVertsOffset(int object)93     int GetVertsOffset(int object) const {
94         return _objects[object].vertsOffset;
95     }
96 
GetRestPosition(int object)97     std::vector<float> const &GetRestPosition(int object) const {
98         return _topologies[_objects[object].topologyIndex].restPosition;
99     }
100 
GetPatchParamTexture()101     GLuint GetPatchParamTexture() const {
102         return _patchParamTexture;
103     }
104 
GetIndexBuffer()105     GLuint GetIndexBuffer() const {
106         return _indexBuffer;
107     }
108 
109     // allocate batched vbo
110     virtual size_t AllocateVBO(int numVerts,
111                                OpenSubdiv::Osd::BufferDescriptor const &vertexDesc,
112                                OpenSubdiv::Osd::BufferDescriptor const &varyingDesc,
113                                bool interleaved) = 0;
114 
115     // refine an object
116     virtual void Refine(int object) =0;
117 
118     virtual void Synchronize() = 0;
119 
120     virtual void UpdateVertexBuffer(int vertsOffset, std::vector<float> const &src)=0;
121 
122     virtual void UpdateVaryingBuffer(int vertsOffset, std::vector<float> const &src)=0;
123 
124     virtual GLuint BindVertexBuffer() = 0;
125 
126     virtual GLuint BindVaryingBuffer() = 0;
127 
128     // build batched index buffer and patchparam texture buffer
129     size_t CreateIndexBuffer();
130 
131     void AddTopology(Shape const *shape, int level, bool varying);
132 
133     int AddObjects(int numObjects);
134 
GetStencilTableSize()135     size_t GetStencilTableSize() const { return _stencilTableSize; }
136 
137 protected:
138     int createStencilTable(Shape const *shape, int level, bool varying,
139                            OpenSubdiv::Far::PatchTable const **patchTableOut);
140 
141     void buildBatches();
142 
143     virtual size_t createMeshRefiner(
144         OpenSubdiv::Far::StencilTable const * vertexStencils,
145         OpenSubdiv::Far::StencilTable const * varyingStencils,
146         int numControlVertices) = 0;
147 
148     Options _options;
149 
150     std::vector<Object> _objects;
151     std::vector<Topology> _topologies;
152     std::vector<OpenSubdiv::Far::PatchTable const *> _patchTables;
153     GLuint _indexBuffer;
154     GLuint _patchParamTexture;
155     BatchVector _batches;
156     size_t _stencilTableSize;
157 
158 };
159 
160 #endif  // OPENSUBDIV_EXAMPLES_GL_SHARE_TOPOLOGY_SCENE_BASE_H
161