PeriDyno 1.0.0
Loading...
Searching...
No Matches
GLPhotorealisticRender.cpp
Go to the documentation of this file.
2#include "Utility.h"
3
4#include <glad/glad.h>
5
6#include "surface.vert.h"
7#include "surface.frag.h"
8#include "surface.geom.h"
9
10namespace dyno
11{
13
15 {
16 this->setName("ObjMeshRenderer");
17
18 this->varMaterialIndex()->attach(std::make_shared<FCallBackFunc>(
19 [=]() {
20 uint idx = this->varMaterialIndex()->getValue();
21
22 if (mTextureMesh.materials().size() > idx)
23 {
24 auto material = mTextureMesh.materials()[idx];
25
26 this->varAlpha()->setValue(material->alpha);
27 this->varMetallic()->setValue(material->metallic);
28 this->varRoughness()->setValue(material->roughness);
29 this->varBaseColor()->setValue(Color(material->baseColor.x, material->baseColor.y, material->baseColor.z));
30 }
31 }));
32
33 this->varMetallic()->attach(
34 std::make_shared<FCallBackFunc>(
35 [=]() {
36 uint idx = this->varMaterialIndex()->getValue();
37
38 if (mTextureMesh.materials().size() > idx)
39 {
40 auto material = mTextureMesh.materials()[idx];
41
42 material->metallic = this->varMetallic()->getValue();
43 }
44 }));
45
46 this->varRoughness()->attach(
47 std::make_shared<FCallBackFunc>(
48 [=]() {
49 uint idx = this->varMaterialIndex()->getValue();
50
51 if (mTextureMesh.materials().size() > idx)
52 {
53 auto material = mTextureMesh.materials()[idx];
54
55 material->roughness = this->varRoughness()->getValue();
56 }
57 }));
58
59 this->varAlpha()->attach(
60 std::make_shared<FCallBackFunc>(
61 [=]() {
62 uint idx = this->varMaterialIndex()->getValue();
63
64 if (mTextureMesh.materials().size() > idx)
65 {
66 auto material = mTextureMesh.materials()[idx];
67
68 material->roughness = this->varAlpha()->getValue();
69 }
70 }));
71
72#ifdef CUDA_BACKEND
73 mTangentSpaceConstructor = std::make_shared<ConstructTangentSpace>();
74 this->inTextureMesh()->connect(mTangentSpaceConstructor->inTextureMesh());
75#endif
76 }
77
81
83 {
84 return "Photorealistic Render";
85 }
86
88 {
89 // create vertex buffer and vertex array object
90 mVAO.create();
91 // create shader program
93 SURFACE_VERT, sizeof(SURFACE_VERT),
94 SURFACE_FRAG, sizeof(SURFACE_FRAG),
95 SURFACE_GEOM, sizeof(SURFACE_GEOM));
96 // create shader uniform buffer
97 mRenderParamsUBlock.create(GL_UNIFORM_BUFFER, GL_DYNAMIC_DRAW);
98 mPBRMaterialUBlock.create(GL_UNIFORM_BUFFER, GL_DYNAMIC_DRAW);
99
100#ifdef CUDA_BACKEND
101 mTangent.create(GL_SHADER_STORAGE_BUFFER, GL_DYNAMIC_DRAW);
102 mBitangent.create(GL_SHADER_STORAGE_BUFFER, GL_DYNAMIC_DRAW);
103#endif
104
105 mShapeTransform.create(GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW);
106
107 return true;
108 }
109
111 {
112 mShaderProgram->release();
113 delete mShaderProgram;
114
115 mTangent.release();
116 mBitangent.release();
117
118 mRenderParamsUBlock.release();
119 mPBRMaterialUBlock.release();
120
121 mVAO.release();
122
123 mShapeTransform.release();
124
125 mTextureMesh.release();
126 }
127
129 {
130 mTangent.updateGL();
131 mBitangent.updateGL();
132
133 mShapeTransform.updateGL();
134
135 mTextureMesh.updateGL();
136
137 glCheckError();
138 }
139
141 {
142 if (this->inTextureMesh()->isModified()) {
143 mTextureMesh.load(this->inTextureMesh()->constDataPtr());
144 this->varMaterialIndex()->setRange(0, mTextureMesh.materials().size() - 1);
145 }
146
147#ifdef CUDA_BACKEND
148 auto texMesh = this->inTextureMesh()->constDataPtr();
149 if (texMesh->materials().size() > 0)
150 {
151 mTangentSpaceConstructor->update();
152
153 if (!mTangentSpaceConstructor->outTangent()->isEmpty())
154 {
155 mTangent.load(mTangentSpaceConstructor->outTangent()->constData());
156 mBitangent.load(mTangentSpaceConstructor->outBitangent()->constData());
157 }
158 }
159#endif
160 }
161
163 {
164 struct {
165 glm::vec3 color;
166 float metallic;
167 float roughness;
168 float alpha;
169 } pbr;
170
171 mShaderProgram->use();
172
173 auto& vertices = mTextureMesh.vertices();
174 auto& normals = mTextureMesh.normals();
175 auto& texCoords = mTextureMesh.texCoords();
176
177 // setup uniforms
178 if (normals.count() > 0
179 && mTangent.count() > 0
180 && mBitangent.count() > 0
181 && normals.count() == mTangent.count()
182 && normals.count() == mBitangent.count())
183 {
184 mShaderProgram->setInt("uVertexNormal", 1);
185 normals.bindBufferBase(9);
186 mTangent.bindBufferBase(12);
187 mBitangent.bindBufferBase(13);
188 }
189 else
190 mShaderProgram->setInt("uVertexNormal", 0);
191
192 mShaderProgram->setInt("uInstanced", 0);
193
194 vertices.bindBufferBase(8);
195 texCoords.bindBufferBase(10);
196
197 auto& shapes = mTextureMesh.shapes();
198 for (int i = 0; i < shapes.size(); i++)
199 {
200 auto shape = shapes[i];
201 if (shape->material != nullptr)
202 {
203 auto mtl = shape->material;
204
205 //
206 {
207 RenderParams pm_i = rparams;
208
209 pm_i.transforms.model = shape->transform;
210
211 mRenderParamsUBlock.load((void*)&pm_i, sizeof(RenderParams));
212 mRenderParamsUBlock.bindBufferBase(0);
213 }
214
215 // material
216 {
217 pbr.color = { mtl->baseColor.x, mtl->baseColor.y, mtl->baseColor.z };
218 pbr.metallic = mtl->metallic;
219 pbr.roughness = mtl->roughness;
220 pbr.alpha = mtl->alpha;
221 mPBRMaterialUBlock.load((void*)&pbr, sizeof(pbr));
222 mPBRMaterialUBlock.bindBufferBase(1);
223 }
224
225 // bind textures
226 {
227 // reset
228 glActiveTexture(GL_TEXTURE10); // color
229 glBindTexture(GL_TEXTURE_2D, 0);
230 glActiveTexture(GL_TEXTURE11); // bump map
231 glBindTexture(GL_TEXTURE_2D, 0);
232
233 if (mtl->texColor.isValid()) {
234 mShaderProgram->setInt("uColorMode", 2);
235 mtl->texColor.bind(GL_TEXTURE10);
236 }
237 else {
238 mtl->texColor.unbind();
239 mShaderProgram->setInt("uColorMode", 0);
240 }
241
242 if (mtl->texBump.isValid()) {
243 mtl->texBump.bind(GL_TEXTURE11);
244 mShaderProgram->setFloat("uBumpScale", mtl->bumpScale);
245 }
246 }
247 }
248 else
249 {
250 RenderParams pm_i = rparams;
251 pm_i.transforms.model = shape->transform;
252
253 // setup uniform buffer
254 mRenderParamsUBlock.load((void*)&rparams, sizeof(RenderParams));
255 mRenderParamsUBlock.bindBufferBase(0);
256
257 // material
258 {
259 struct {
260 glm::vec3 color;
261 float metallic;
262 float roughness;
263 float alpha;
264 } pbr;
265 auto color = this->varBaseColor()->getValue();
266 pbr.color = { color.r, color.g, color.b };
267 pbr.metallic = this->varMetallic()->getValue();
268 pbr.roughness = this->varRoughness()->getValue();
269 pbr.alpha = this->varAlpha()->getValue();
270 mPBRMaterialUBlock.load((void*)&pbr, sizeof(pbr));
271 mPBRMaterialUBlock.bindBufferBase(1);
272 }
273
274 mShaderProgram->setInt("uColorMode", 0);
275 }
276
277 int numTriangles = shape->glVertexIndex.count();
278
279 mVAO.bind();
280
281 // setup VAO binding...
282 {
283 // vertex index
284 shape->glVertexIndex.bind();
285 glEnableVertexAttribArray(0);
286 glVertexAttribIPointer(0, 1, GL_INT, sizeof(int), (void*)0);
287
288 if (shape->glNormalIndex.count() == numTriangles) {
289 shape->glNormalIndex.bind();
290 glEnableVertexAttribArray(1);
291 glVertexAttribIPointer(1, 1, GL_INT, sizeof(int), (void*)0);
292 }
293 else
294 {
295 glDisableVertexAttribArray(1);
296 glVertexAttribI4i(1, -1, -1, -1, -1);
297 }
298
299 if (shape->glTexCoordIndex.count() == numTriangles) {
300 shape->glTexCoordIndex.bind();
301 glEnableVertexAttribArray(2);
302 glVertexAttribIPointer(2, 1, GL_INT, sizeof(int), (void*)0);
303 }
304 else
305 {
306 glDisableVertexAttribArray(2);
307 glVertexAttribI4i(2, -1, -1, -1, -1);
308 }
309 }
310
311 glDrawArrays(GL_TRIANGLES, 0, numTriangles * 3);
312
313 glCheckError();
314 mVAO.unbind();
315 }
316 }
317
318
319}
#define glCheckError()
#define IMPLEMENT_CLASS(name)
Definition Object.h:79
virtual std::string caption() override
Return the caption.
XBuffer< Transform3f > mShapeTransform
virtual void paintGL(const RenderParams &rparams) override
void setName(std::string name)
Definition Module.cpp:187
static Program * createProgramSPIRV(const void *vs, size_t vs_len, const void *fs, size_t fs_len, const void *gs=0, size_t gs_len=0)
Definition Shader.cpp:202
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
unsigned int uint
Definition VkProgram.h:14
struct dyno::RenderParams::Transform transforms