107 GLuint shader = glCreateShader(shaderType);
108 std::string srcString = m_defineVersion
109 +
"#define MAX_JOINTS " + std::to_string(Settings::GetMaxJoints()) +
'\n'
110 +
"#define MAX_LIGHTS " + std::to_string(Settings::GetMaxLights()) +
'\n'
112 const char* src = srcString.c_str();
115 glShaderSource(shader, 1, &src,
nullptr);
116 glCompileShader(shader);
118 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
122 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
125 auto buf =
static_cast<char*
>(malloc(infoLen));
128 glGetShaderInfoLog(shader, infoLen,
nullptr, buf);
130 auto errorLog = std::string(
"[") + handle.GetName() +
"] Could not compile shader:" + buf;
131 SafeLog::Log(errorLog.c_str());
134 glDeleteShader(shader);
144 std::stringstream ss(source);
146 std::map<std::string, std::string> variables;
147 std::string type_str;
150 while (std::getline(ss, line)) {
152 int start_index = line.find(
"//[");
153 int end_index = line.find(
"]//");
155 if (start_index != std::string::npos && end_index != std::string::npos) {
159 type_str = line.substr(start_index, end_index - 3);
164 if (!type_str.empty()) {
166 int eoc_index = line.find(
';');
168 if (eoc_index != std::string::npos) {
170 int startIndex = line.substr(0, eoc_index).rfind(
' ');
171 int endIndex = line.rfind(
'[');
172 endIndex = endIndex == std::string::npos ? eoc_index : endIndex;
173 auto detail = line.substr(startIndex, endIndex - startIndex);
176 detail = trim(detail);
177 variables[type_str] = detail;
187 if (handle ==
nullptr)
return;
190 auto position = handle->AttributeLocation(
"att.position");
191 auto normal = handle->AttributeLocation(
"att.normal");
192 auto jointId = handle->AttributeLocation(
"att.joint_indices");
193 auto weight = handle->AttributeLocation(
"att.weight");
194 auto textureCoord = handle->AttributeLocation(
"att.tex_uv");
195 auto color = handle->AttributeLocation(
"att.color");
197 auto view = handle->UniformLocation(
"matrix.view");
198 auto model = handle->UniformLocation(
"matrix.model");
199 auto cameraPosition = handle->UniformLocation(
"vec3.camera");
200 auto projection = handle->UniformLocation(
"matrix.projection");
201 auto projectionInv = handle->UniformLocation(
"matrix.projection.inv");
202 auto viewInv = handle->UniformLocation(
"matrix.view.inv");
203 auto skinningMode = handle->UniformLocation(
"matrix.skinning_mode");
204 auto jointMatrix = handle->UniformLocation(
"matrix.joint");
205 auto lightPosition = handle->UniformLocation(
"light.position");
206 auto lightType = handle->UniformLocation(
"light.type");
207 auto lightRadius = handle->UniformLocation(
"light.radius");
208 auto lightColor = handle->UniformLocation(
"light.color");
209 auto lightShadowMap = handle->UniformLocation(
"light.shadow_map");
210 auto lightMatrix = handle->UniformLocation(
"light.matrix");
211 auto lightShadowMode = handle->UniformLocation(
"light.shadow_mode");
212 auto lightSize = handle->UniformLocation(
"light.size");
213 auto lightIrradiance = handle->UniformLocation(
"light.irradiance");
214 auto lightPrefilter = handle->UniformLocation(
"light.prefilter");
215 auto lightBrdf = handle->UniformLocation(
"light.brdf");
216 auto srcBuffer = handle->UniformLocation(
"buffer.source");
217 auto srcBufferSize = handle->UniformLocation(
"buffer.source.size");
219 handle->Attributes.Position = position !=
nullptr ? position->id : HANDLE_NULL;
220 handle->Attributes.Normal = normal !=
nullptr ? normal->id : HANDLE_NULL;
221 handle->Attributes.JointId = jointId !=
nullptr ? jointId->id : HANDLE_NULL;
222 handle->Attributes.Weight = weight !=
nullptr ? weight->id : HANDLE_NULL;
223 handle->Attributes.TextureCoord = textureCoord !=
nullptr ? textureCoord->id : HANDLE_NULL;
224 handle->Attributes.Color = color !=
nullptr ? color->id : HANDLE_NULL;
226 handle->Uniforms.ViewMatrix = view !=
nullptr ? view->id : HANDLE_NULL;
227 handle->Uniforms.ModelMatrix = model !=
nullptr ? model->id : HANDLE_NULL;
228 handle->Uniforms.CameraPosition = cameraPosition !=
nullptr ? cameraPosition->id : HANDLE_NULL;
229 handle->Uniforms.ProjectionMatrix = projection !=
nullptr ? projection->id : HANDLE_NULL;
230 handle->Uniforms.ProjectionInvMatrix = projectionInv !=
nullptr ? projectionInv->id : HANDLE_NULL;
231 handle->Uniforms.ViewInvMatrix = viewInv !=
nullptr ? viewInv->id : HANDLE_NULL;
232 handle->Uniforms.SkinningMode = skinningMode !=
nullptr ? skinningMode->id : HANDLE_NULL;
233 handle->Uniforms.JointMatrix = jointMatrix !=
nullptr ? jointMatrix->id : HANDLE_NULL;
234 handle->Uniforms.LightPosition = lightPosition !=
nullptr ? lightPosition->id : HANDLE_NULL;
235 handle->Uniforms.LightType = lightType !=
nullptr ? lightType->id : HANDLE_NULL;
236 handle->Uniforms.LightRadius = lightRadius !=
nullptr ? lightRadius->id : HANDLE_NULL;
237 handle->Uniforms.LightColor = lightColor !=
nullptr ? lightColor->id : HANDLE_NULL;
238 handle->Uniforms.LightShadowMap = lightShadowMap !=
nullptr ? lightShadowMap->id : HANDLE_NULL;
239 handle->Uniforms.LightMatrix = lightMatrix !=
nullptr ? lightMatrix->id : HANDLE_NULL;
240 handle->Uniforms.LightShadowMode = lightShadowMode !=
nullptr ? lightShadowMode->id : HANDLE_NULL;
241 handle->Uniforms.LightSize = lightSize !=
nullptr ? lightSize->id : HANDLE_NULL;
242 handle->Uniforms.LightIrradiance = lightIrradiance !=
nullptr ? lightIrradiance->id : HANDLE_NULL;
243 handle->Uniforms.LightPrefilter = lightPrefilter !=
nullptr ? lightPrefilter->id : HANDLE_NULL;
244 handle->Uniforms.LightBrdfLut = lightBrdf !=
nullptr ? lightBrdf->id : HANDLE_NULL;
245 handle->Uniforms.SourceBuffer = srcBuffer !=
nullptr ? srcBuffer->id : HANDLE_NULL;
246 handle->Uniforms.SourceBufferSize = srcBufferSize !=
nullptr ? srcBufferSize->id : HANDLE_NULL;
250 const mat4& projection,
const mat4& transform) {
251 if(handle.Uniforms.ViewMatrix >= 0)
252 glUniformMatrix4fv(handle.Uniforms.ViewMatrix, 1, 0, view.Pointer());
253 if(handle.Uniforms.ModelMatrix >= 0)
254 glUniformMatrix4fv(handle.Uniforms.ModelMatrix, 1, 0, transform.Pointer());
255 if(handle.Uniforms.ProjectionMatrix >= 0)
256 glUniformMatrix4fv(handle.Uniforms.ProjectionMatrix, 1, 0, projection.Pointer());
257 if(handle.Uniforms.CameraPosition >= 0)
258 glUniform3fv(handle.Uniforms.CameraPosition, 1, cameraPosition.Pointer());
260 if(handle.Uniforms.ProjectionInvMatrix >= 0)
261 glUniformMatrix4fv(handle.Uniforms.ProjectionInvMatrix, 1, 0, mat4::Invert(projection).Pointer());
262 if(handle.Uniforms.ViewInvMatrix >= 0)
263 glUniformMatrix4fv(handle.Uniforms.ViewInvMatrix, 1, 0, mat4::Invert(view).Pointer());
268 int stride = 4 *
sizeof(
vec3) +
sizeof(
vec2);
277 const std::vector<Attribute> attributes = {
278 { handle.Attributes.Position, 0, 3 },
279 { handle.Attributes.Normal,
sizeof(
vec3), 3 },
280 { handle.Attributes.TextureCoord, 2 *
sizeof(
vec3), 2 },
281 { handle.Attributes.Weight, 2 *
sizeof(
vec3) +
sizeof(
vec2), 3 },
282 { handle.Attributes.JointId, 3 *
sizeof(
vec3) +
sizeof(
vec2), 3 },
286 glBindVertexArray(meshId.m_vertexArray);
287 glBindBuffer(GL_ARRAY_BUFFER, meshId.m_vertexBuffer);
288 for (
const auto& attribute: attributes) {
289 if (attribute.id < 0)
continue;
290 glEnableVertexAttribArray(attribute.id);
291 glVertexAttribPointer(attribute.id, attribute.size, GL_FLOAT, GL_FALSE, stride, (GLvoid*) attribute.offset);
295 if (meshId.m_indexSize < 0) {
296 glDrawArrays(GL_TRIANGLES, 0, meshId.m_vertexSize);
298 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, meshId.m_indexBuffer);
299 glDrawElements(GL_TRIANGLES, meshId.m_indexSize * 3, GL_UNSIGNED_SHORT,
nullptr);
302 for (
const auto& attribute: attributes) {
303 if (attribute.id < 0)
continue;
304 glDisableVertexAttribArray(attribute.id);
306 glBindVertexArray(0);