PeriDyno 1.0.0
Loading...
Searching...
No Matches
GLTextureMesh.cpp
Go to the documentation of this file.
1#include "GLTextureMesh.h"
2
3#include "glad/glad.h"
4
5namespace dyno
6{
13
18
20 {
21 }
22
24 {
25 texColor.release();
26 texBump.release();
27 }
28
30 {
31 texColor.updateGL();
32 texBump.updateGL();
33 }
34
39 {
40 }
41
43 {
44 }
45
47 {
48 if (!mInitialized)
49 {
50 glVertexIndex.create(GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW);
51 glNormalIndex.create(GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW);
52 glTexCoordIndex.create(GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW);
53
54 mInitialized = true;
55 }
56 }
57
59 {
60 glVertexIndex.release();
61 glNormalIndex.release();
62 glTexCoordIndex.release();
63 }
64
66 {
67 if (!mInitialized)
68 create();
69
70 glVertexIndex.updateGL();
71 glNormalIndex.updateGL();
72 glTexCoordIndex.updateGL();
73 }
74
75
79
83
85 {
86 if (!mInitialized)
87 {
88 mVertices.create(GL_SHADER_STORAGE_BUFFER, GL_DYNAMIC_DRAW);
89 mNormal.create(GL_SHADER_STORAGE_BUFFER, GL_DYNAMIC_DRAW);
90 mTexCoord.create(GL_SHADER_STORAGE_BUFFER, GL_DYNAMIC_DRAW);
91
92 mInitialized = true;
93 }
94 }
95
97 {
98 mVertices.release();
99 mNormal.release();
100 mTexCoord.release();
101
102 for (auto m : mMaterials) {
103 m->release();
104 }
105
106 for (auto s : mShapes) {
107 s->release();
108 }
109
110 mMaterials.clear();
111 mShapes.clear();
112 }
113
114 void GLTextureMesh::load(const std::shared_ptr<TextureMesh> mesh)
115 {
116 if (mesh == nullptr)
117 return;
118
119 mVertices.load(mesh->vertices());
120 mNormal.load(mesh->normals());
121 mTexCoord.load(mesh->texCoords());
122
123 uint shapeNum = mesh->shapes().size();
124
125 if (mShapes.size() != shapeNum)
126 {
127 mShapes.resize(shapeNum);
128 for (uint i = 0; i < shapeNum; i++)
129 {
130 mShapes[i] = std::make_shared<GLShape>();
131 }
132 }
133
134 uint matNum = mesh->materials().size();
135 if (mMaterials.size() != matNum)
136 {
137 mMaterials.resize(matNum);
138 for (uint i = 0; i < matNum; i++)
139 {
140 mMaterials[i] = std::make_shared<GLMaterial>();
141 }
142 }
143
144 std::map<std::shared_ptr<Material>, uint> mapper;
145
146 for (uint i = 0; i < matNum; i++)
147 {
148 mMaterials[i]->baseColor = mesh->materials()[i]->baseColor;
149 mMaterials[i]->metallic = mesh->materials()[i]->metallic;
150 mMaterials[i]->roughness = mesh->materials()[i]->roughness;
151 mMaterials[i]->metallic = mesh->materials()[i]->metallic;
152 mMaterials[i]->bumpScale = mesh->materials()[i]->bumpScale;
153 mMaterials[i]->texColor.load(mesh->materials()[i]->texColor);
154 mMaterials[i]->texBump.load(mesh->materials()[i]->texBump);
155
156 mapper[mesh->materials()[i]] = i;
157 }
158
159 for (uint i = 0; i < shapeNum; i++)
160 {
161 mShapes[i]->glVertexIndex.load(mesh->shapes()[i]->vertexIndex);
162 mShapes[i]->glNormalIndex.load(mesh->shapes()[i]->normalIndex);
163 mShapes[i]->glTexCoordIndex.load(mesh->shapes()[i]->texCoordIndex);
164
165 Vec3f S = mesh->shapes()[i]->boundingTransform.scale();
166 Mat3f R = mesh->shapes()[i]->boundingTransform.rotation();
167 Vec3f T = mesh->shapes()[i]->boundingTransform.translation();
168
169 Mat3f RS = R * Mat3f(
170 S[0], 0, 0,
171 0, S[1], 0,
172 0, 0, S[2]);
173
174 glm::mat4 tm = glm::mat4{
175 RS(0, 0), RS(1, 0), RS(2, 0), 0,
176 RS(0, 1), RS(1, 1), RS(2, 1), 0,
177 RS(0, 2), RS(1, 2), RS(2, 2), 0,
178 T[0], T[1], T[2], 1 };
179
180 mShapes[i]->transform = tm;
181
182 //Setup the material for each shape
183 auto test = mapper[mesh->shapes()[i]->material];
184 auto testsm = mesh->shapes()[i]->material;
185
186 if (mesh->shapes()[i]->material != NULL)
187 {
188 mShapes[i]->material = mMaterials[mapper[mesh->shapes()[i]->material]];
189 }
190 else
191 {
192 mShapes[i]->material = NULL;
193 }
194
195 }
196
197 mapper.clear();
198 }
199
201 {
202 if (!mInitialized)
203 create();
204
205 mVertices.updateGL();
206 mNormal.updateGL();
207 mTexCoord.updateGL();
208
209 for (uint i = 0; i < mShapes.size(); i++)
210 {
211 mShapes[i]->updateGL();
212 }
213
214 for (uint i = 0; i < mMaterials.size(); i++)
215 {
216 mMaterials[i]->updateGL();
217 }
218 }
219
220}
~GLMaterial() override
void release() override
void create() override
XTexture2D< dyno::Vec4f > texBump
XTexture2D< dyno::Vec4f > texColor
XBuffer< dyno::TopologyModule::Triangle > glNormalIndex
void release() override
~GLShape() override
XBuffer< dyno::TopologyModule::Triangle > glTexCoordIndex
void create() override
XBuffer< dyno::TopologyModule::Triangle > glVertexIndex
XBuffer< Vec2f > mTexCoord
std::vector< std::shared_ptr< GLShape > > mShapes
void load(const std::shared_ptr< TextureMesh > mesh)
XBuffer< Vec3f > mNormal
std::vector< std::shared_ptr< GLMaterial > > mMaterials
XBuffer< Vec3f > mVertices
#define T(t)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
SquareMatrix< float, 3 > Mat3f
Definition Matrix3x3.h:92
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
unsigned int uint
Definition VkProgram.h:14