PeriDyno 1.0.0
Loading...
Searching...
No Matches
GLPhotorealisticInstanceRender.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
18
23
25 {
26 return "Photorealistic Instance Render";
27 }
28
30 {
31 mXTransformBuffer.create(GL_ARRAY_BUFFER, GL_DYNAMIC_DRAW);
32
34 }
35
41
48
49
51 {
52 auto inst = this->inTransform()->constDataPtr();
53
54 if (this->inTransform()->isModified())
55 {
56 auto texMesh = this->inTextureMesh()->constDataPtr();
57
58 mOffset.assign(inst->index());
59 mLists.assign(inst->lists());
60
61 mXTransformBuffer.load(inst->elements());
62 }
63
65 }
66
68 {
69 struct {
70 glm::vec3 color;
71 float metallic;
72 float roughness;
73 float alpha;
74 } pbr;
75
76 auto& vertices = mTextureMesh.vertices();
77 auto& normals = mTextureMesh.normals();
78 auto& texCoords = mTextureMesh.texCoords();
79
80 mShaderProgram->use();
81
82 // setup uniforms
83 if (normals.count() > 0
84 && mTangent.count() > 0
85 && mBitangent.count() > 0
86 && normals.count() == mTangent.count()
87 && normals.count() == mBitangent.count())
88 {
89 mShaderProgram->setInt("uVertexNormal", 1);
90 normals.bindBufferBase(9);
91 mTangent.bindBufferBase(12);
92 mBitangent.bindBufferBase(13);
93 }
94 else
95 mShaderProgram->setInt("uVertexNormal", 0);
96
97 mShaderProgram->setInt("uInstanced", 1);
98
99 //Reset the model transform
100 RenderParams rp = rparams;
101 rp.transforms.model = glm::mat4{ 1.0 };
102 mRenderParamsUBlock.load((void*)&rp, sizeof(RenderParams));
103 mRenderParamsUBlock.bindBufferBase(0);
104
105 vertices.bindBufferBase(8);
106 texCoords.bindBufferBase(10);
107
108 auto& shapes = mTextureMesh.shapes();
109 for (int i = 0; i < shapes.size(); i++)
110 {
111 auto shape = shapes[i];
112 auto mtl = shape->material;
113
114 // material
115 if(mtl != nullptr)
116 {
117 pbr.color = { mtl->baseColor.x, mtl->baseColor.y, mtl->baseColor.z };
118 pbr.metallic = mtl->metallic;
119 pbr.roughness = mtl->roughness;
120 pbr.alpha = mtl->alpha;
121 mPBRMaterialUBlock.load((void*)&pbr, sizeof(pbr));
122 mPBRMaterialUBlock.bindBufferBase(1);
123
124 // bind textures
125 {
126 // reset
127 glActiveTexture(GL_TEXTURE10); // color
128 glBindTexture(GL_TEXTURE_2D, 0);
129 glActiveTexture(GL_TEXTURE11); // bump map
130 glBindTexture(GL_TEXTURE_2D, 0);
131
132 if (mtl->texColor.isValid()) {
133 mShaderProgram->setInt("uColorMode", 2);
134 mtl->texColor.bind(GL_TEXTURE10);
135 }
136 else {
137 mShaderProgram->setInt("uColorMode", 1);
138 }
139
140 if (mtl->texBump.isValid()) {
141 mtl->texBump.bind(GL_TEXTURE11);
142 mShaderProgram->setFloat("uBumpScale", mtl->bumpScale);
143 }
144 }
145 }
146 else
147 {
148 auto color = this->varBaseColor()->getValue();
149 pbr.color = { color.r, color.g, color.b };
150 pbr.metallic = this->varMetallic()->getValue();
151 pbr.roughness = this->varRoughness()->getValue();
152 pbr.alpha = this->varAlpha()->getValue();
153 mPBRMaterialUBlock.load((void*)&pbr, sizeof(pbr));
154 mPBRMaterialUBlock.bindBufferBase(1);
155
156 mShaderProgram->setInt("uColorMode", 1);
157 }
158
159
160 int numTriangles = shape->glVertexIndex.count();
161
162 mVAO.bind();
163
164 // setup VAO binding...
165 {
166 // vertex index
167 shape->glVertexIndex.bind();
168 glEnableVertexAttribArray(0);
169 glVertexAttribIPointer(0, 1, GL_INT, sizeof(int), (void*)0);
170
171 if (shape->glNormalIndex.count() == numTriangles) {
172 shape->glNormalIndex.bind();
173 glEnableVertexAttribArray(1);
174 glVertexAttribIPointer(1, 1, GL_INT, sizeof(int), (void*)0);
175 }
176 else
177 {
178 glDisableVertexAttribArray(1);
179 glVertexAttribI4i(1, -1, -1, -1, -1);
180 }
181
182 if (shape->glTexCoordIndex.count() == numTriangles) {
183 shape->glTexCoordIndex.bind();
184 glEnableVertexAttribArray(2);
185 glVertexAttribIPointer(2, 1, GL_INT, sizeof(int), (void*)0);
186 }
187 else
188 {
189 glDisableVertexAttribArray(2);
190 glVertexAttribI4i(2, -1, -1, -1, -1);
191 }
192
193 }
194 if (mOffset.size() >= shapes.size())
195 {
196 uint offset_i = sizeof(Transform3f) * mOffset[i];
197 mVAO.bindVertexBuffer(&mXTransformBuffer, 3, 3, GL_FLOAT, sizeof(Transform3f), offset_i + 0, 1);
198 // bind the scale vector
199 mVAO.bindVertexBuffer(&mXTransformBuffer, 4, 3, GL_FLOAT, sizeof(Transform3f), offset_i + sizeof(Vec3f), 1);
200 // bind the rotation matrix
201 mVAO.bindVertexBuffer(&mXTransformBuffer, 5, 3, GL_FLOAT, sizeof(Transform3f), offset_i + 2 * sizeof(Vec3f), 1);
202 mVAO.bindVertexBuffer(&mXTransformBuffer, 6, 3, GL_FLOAT, sizeof(Transform3f), offset_i + 3 * sizeof(Vec3f), 1);
203 mVAO.bindVertexBuffer(&mXTransformBuffer, 7, 3, GL_FLOAT, sizeof(Transform3f), offset_i + 4 * sizeof(Vec3f), 1);
204 mVAO.bind();
205 glDrawArraysInstanced(GL_TRIANGLES, 0, numTriangles * 3, mLists[i].size());
206
207 }
208 else
209 {
210 printf( "GLPhotorealisticInstanceRender::inTransform Is Error !!!!!!\n");
211 }
212 mVAO.unbind();
213
214 }
215 }
216}
#define IMPLEMENT_CLASS(name)
Definition Object.h:79
void paintGL(const RenderParams &rparams) override
virtual std::string caption() override
Return the caption.
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Transform< float, 3 > Transform3f
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
unsigned int uint
Definition VkProgram.h:14
struct dyno::RenderParams::Transform transforms