PeriDyno 1.0.0
Loading...
Searching...
No Matches
GLSurfaceVisualModule.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("surface_renderer");
17
18 this->varBaseColor()->setValue(Color::SteelBlue2());
19
20 this->inColor()->tagOptional(true);
21
22 this->inNormal()->tagOptional(true);
23 this->inNormalIndex()->tagOptional(true);
24 this->inTexCoord()->tagOptional(true);
25 this->inTexCoordIndex()->tagOptional(true);
26
27#ifdef CUDA_BACKEND
28 this->inColorTexture()->tagOptional(true);
29 this->inBumpMap()->tagOptional(true);
30#endif
31 }
32
34 {
35// mIndexBuffer.release();
36// mVertexBuffer.release();
37// mNormalBuffer.release();
38// mColorBuffer.release();
39//
40// triangles.clear();
41// vertices.clear();
42// normals.clear();
43// colors.clear();
44 }
45
47 {
48 return "Surface Visual Module";
49 }
50
52 {
53 // create vertex buffer and vertex array object
54 mVAO.create();
55
56 mVertexIndex.create(GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW);
57 mNormalIndex.create(GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW);
58 mTexCoordIndex.create(GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW);
59
60 mVertexPosition.create(GL_SHADER_STORAGE_BUFFER, GL_DYNAMIC_DRAW);
61 mVertexColor.create(GL_SHADER_STORAGE_BUFFER, GL_DYNAMIC_DRAW);
62 mNormal.create(GL_SHADER_STORAGE_BUFFER, GL_DYNAMIC_DRAW);
63 mTexCoord.create(GL_SHADER_STORAGE_BUFFER, GL_DYNAMIC_DRAW);
64
65 // create shader program
67 SURFACE_VERT, sizeof(SURFACE_VERT),
68 SURFACE_FRAG, sizeof(SURFACE_FRAG),
69 SURFACE_GEOM, sizeof(SURFACE_GEOM));
70
71 // create shader uniform buffer
72 mRenderParamsUBlock.create(GL_UNIFORM_BUFFER, GL_DYNAMIC_DRAW);
73 mPBRMaterialUBlock.create(GL_UNIFORM_BUFFER, GL_DYNAMIC_DRAW);
74
75 return true;
76 }
77
79 {
80 mShaderProgram->release();
81 delete mShaderProgram;
83
84 // vertex array object
85 mVAO.release();
86
87 // vertex array buffer
88 mVertexIndex.release();
89 mNormalIndex.release();
90 mTexCoordIndex.release();
91
92 // shader storage buffer
93 mVertexColor.release();
94 mVertexPosition.release();
95 mNormal.release();
96 mTexCoord.release();
97
98 // release uniform block
99 mRenderParamsUBlock.release();
100 mPBRMaterialUBlock.release();
101 }
102
104 {
105 mNumTriangles = mVertexIndex.count();
106 if (mNumTriangles == 0) return;
107
108 mVertexIndex.updateGL();
109 // normal
110 if (mNormalIndex.count() == mNumTriangles) {
111 mNormalIndex.updateGL();
112 }
113 // texcoord
114 if (mTexCoordIndex.count() == mNumTriangles) {
115 mTexCoordIndex.updateGL();
116 }
117
118 // update shader storage buffer
119 mVertexPosition.updateGL();
120
121 // vertex color
122 if (this->varColorMode()->currentKey() == EColorMode::CM_Vertex) {
123 mVertexColor.updateGL();
124 }
125 // vertex normal
126 if(this->varUseVertexNormal()->getValue()) {
127 mNormal.updateGL();
128 }
129
130 // texture coordinates
131 if (mTexCoord.count() > 0) {
132 mTexCoord.updateGL();
133 }
134
135#ifdef CUDA_BACKEND
136 // update texture content
137 mColorTexture.updateGL();
138#endif
139
140 glCheckError();
141 }
142
144 {
145 // update data
146 auto triSet = this->inTriangleSet()->constDataPtr();
147 auto indices = triSet->getTriangles();
148 auto vertices = triSet->getPoints();
149
150 mVertexIndex.load(indices);
151 mVertexPosition.load(vertices);
152
153 if (this->varColorMode()->getValue() == EColorMode::CM_Vertex &&
154 !this->inColor()->isEmpty() &&
155 this->inColor()->getDataPtr()->size() == vertices.size())
156 {
157 auto colors = this->inColor()->getData();
158 mVertexColor.load(colors);
159 }
160
161 // generate per-vertex normal
162 if (this->varUseVertexNormal()->getValue())
163 {
164#ifdef CUDA_BACKEND
165 //TODO: optimize the performance
166 if (this->inNormal()->isEmpty()) {
167 //triSet->update();
168 auto normals = triSet->getVertexNormals();
169 mNormal.load(normals);
170 }
171 else
172 {
173 mNormal.load(this->inNormal()->constData());
174 // has separate normal index?
175 if (!this->inNormalIndex()->isEmpty())
176 mNormalIndex.load(this->inNormalIndex()->constData());
177 }
178#endif
179 }
180
181 // texture coordinates
182 {
183 if (!this->inTexCoord()->isEmpty()) {
184 mTexCoord.load(this->inTexCoord()->constData());
185 }
186
187 if (!this->inTexCoordIndex()->isEmpty()) {
188 mTexCoordIndex.load(this->inTexCoordIndex()->constData());
189 }
190 }
191
192#ifdef CUDA_BACKEND
193 // texture
194 if (!inColorTexture()->isEmpty()) {
195 mColorTexture.load(inColorTexture()->constData());
196 }
197
198 if (!inBumpMap()->isEmpty()) {
199 mBumpMap.load(inBumpMap()->constData());
200 }
201#endif
202
203 }
204
206 {
207 if (mNumTriangles == 0)
208 return;
209
210 mShaderProgram->use();
211
212 if (rparams.mode == GLRenderMode::COLOR) {
213 }
214 else if (rparams.mode == GLRenderMode::SHADOW) {
215 }
216 else if (rparams.mode == GLRenderMode::TRANSPARENCY) {
217 }
218 else {
219 printf("GLSurfaceVisualModule: Unknown render mode!\n");
220 return;
221 }
222
223 // material
224 {
225 struct {
226 glm::vec3 color;
227 float metallic;
228 float roughness;
229 float alpha;
230 } pbr;
231 auto color = this->varBaseColor()->getValue();
232 pbr.color = { color.r, color.g, color.b };
233 pbr.metallic = this->varMetallic()->getValue();
234 pbr.roughness = this->varRoughness()->getValue();
235 pbr.alpha = this->varAlpha()->getValue();
236 mPBRMaterialUBlock.load((void*)&pbr, sizeof(pbr));
237 }
238
239 // setup uniforms
240 mShaderProgram->setInt("uVertexNormal", this->varUseVertexNormal()->getValue());
241 mShaderProgram->setInt("uColorMode", this->varColorMode()->currentKey());
242 mShaderProgram->setInt("uInstanced", mInstanceCount > 0);
243
244 // setup uniform buffer
245 mRenderParamsUBlock.load((void*)&rparams, sizeof(RenderParams));
246
247 // uniform block binding
248 mRenderParamsUBlock.bindBufferBase(0);
249 mPBRMaterialUBlock.bindBufferBase(1);
250
251 // bind vertex data
252 {
253 mVertexPosition.bindBufferBase(8);
254 mNormal.bindBufferBase(9);
255 mTexCoord.bindBufferBase(10);
256 mVertexColor.bindBufferBase(11);
257 }
258
259 // bind textures
260 {
261 // reset
262 glActiveTexture(GL_TEXTURE10); // color
263 glBindTexture(GL_TEXTURE_2D, 0);
264 glActiveTexture(GL_TEXTURE11); // bump map
265 glBindTexture(GL_TEXTURE_2D, 0);
266
267#ifdef CUDA_BACKEND
268 if (mColorTexture.isValid()) mColorTexture.bind(GL_TEXTURE10);
269 if (mBumpMap.isValid()) mBumpMap.bind(GL_TEXTURE11);
270#endif
271 }
272
273 mVAO.bind();
274
275 // setup VAO binding...
276 {
277 // vertex index
278 mVertexIndex.bind();
279 glEnableVertexAttribArray(0);
280 glVertexAttribIPointer(0, 1, GL_INT, sizeof(int), (void*)0);
281
282 if (mNormalIndex.count() == mNumTriangles) {
283 mNormalIndex.bind();
284 glEnableVertexAttribArray(1);
285 glVertexAttribIPointer(1, 1, GL_INT, sizeof(int), (void*)0);
286 }
287 else
288 {
289 glDisableVertexAttribArray(1);
290 glVertexAttribI4i(1, -1, -1, -1, -1);
291 }
292
293 if (mTexCoordIndex.count() == mNumTriangles) {
294 mTexCoordIndex.bind();
295 glEnableVertexAttribArray(2);
296 glVertexAttribIPointer(2, 1, GL_INT, sizeof(int), (void*)0);
297 }
298 else
299 {
300 glDisableVertexAttribArray(2);
301 glVertexAttribI4i(2, -1, -1, -1, -1);
302 }
303
304 //if (mInstanceCount > 0)
305 //{
306
307 //}
308 //else
309 //{
310 // // instance transforms
311 // glDisableVertexAttribArray(3);
312 // glDisableVertexAttribArray(4);
313 // glDisableVertexAttribArray(5);
314 // glDisableVertexAttribArray(6);
315 // glDisableVertexAttribArray(7);
316 // glDisableVertexAttribArray(8);
317 //}
318 }
319
320 if(mInstanceCount > 0)
321 glDrawArraysInstanced(GL_TRIANGLES, 0, mNumTriangles * 3, mInstanceCount);
322 else
323 glDrawArrays(GL_TRIANGLES, 0, mNumTriangles * 3);
324
325 glCheckError();
326 mVAO.unbind();
327 }
328}
#define glCheckError()
#define IMPLEMENT_CLASS(name)
Definition Object.h:79
static Color SteelBlue2()
Definition Color.h:268
virtual void releaseGL() override
virtual void paintGL(const RenderParams &rparams) override
virtual std::string caption() override
Return the caption.
XBuffer< TopologyModule::Triangle > mNormalIndex
virtual bool initializeGL() override
XBuffer< TopologyModule::Triangle > mVertexIndex
XBuffer< TopologyModule::Triangle > mTexCoordIndex
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
static const int SHADOW
static const int COLOR
static const int TRANSPARENCY