From 242cb573ce794cf58c8a299283afc30305726f10 Mon Sep 17 00:00:00 2001 From: victorfisac Date: Sat, 15 Jul 2017 16:30:07 +0200 Subject: [PATCH] Added useSampler assign in SetMaterialTexture and fixed model transform matrix data in shader --- src/models.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/rlgl.c | 17 ++++++++--------- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/models.c b/src/models.c index 475d8753e..62494dcfb 100644 --- a/src/models.c +++ b/src/models.c @@ -1318,6 +1318,9 @@ Material LoadMaterialPBR(Texture2D hdr, Color albedo, float metalness, float rou mat.shader.locs[LOC_TEXMAP_PREFILTER] = GetShaderLocation(mat.shader, "prefilterMap"); mat.shader.locs[LOC_TEXMAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT"); + // Set view matrix location + mat.shader.locs[LOC_MATRIX_VIEW] = GetShaderLocation(mat.shader, "viewPos"); + // Set up material properties color mat.maps[TEXMAP_ALBEDO].color = albedo; mat.maps[TEXMAP_NORMAL].color = (Color){ 128, 128, 255, 255 }; @@ -1365,6 +1368,46 @@ void UnloadMaterial(Material material) void SetMaterialTexture(Material *mat, int texmapType, Texture2D texture) { mat->maps[texmapType].tex = texture; + + int location = -1; + switch (texmapType) + { + case TEXMAP_ALBEDO: + { + location = GetShaderLocation(mat->shader, "albedo.useSampler"); + SetShaderValuei(mat->shader, location, (int [1]){ 1 }, 1); + } break; + case TEXMAP_NORMAL: + { + location = GetShaderLocation(mat->shader, "normals.useSampler"); + SetShaderValuei(mat->shader, location, (int [1]){ 1 }, 1); + } break; + case TEXMAP_METALNESS: + { + location = GetShaderLocation(mat->shader, "metalness.useSampler"); + SetShaderValuei(mat->shader, location, (int [1]){ 1 }, 1); + } break; + case TEXMAP_ROUGHNESS: + { + location = GetShaderLocation(mat->shader, "roughness.useSampler"); + SetShaderValuei(mat->shader, location, (int [1]){ 1 }, 1); + } break; + case TEXMAP_OCCLUSION: + { + location = GetShaderLocation(mat->shader, "occlusion.useSampler"); + SetShaderValuei(mat->shader, location, (int [1]){ 1 }, 1); + } break; + case TEXMAP_EMISSION: + { + location = GetShaderLocation(mat->shader, "emission.useSampler"); + SetShaderValuei(mat->shader, location, (int [1]){ 1 }, 1); + } break; + case TEXMAP_HEIGHT: + { + location = GetShaderLocation(mat->shader, "height.useSampler"); + SetShaderValuei(mat->shader, location, (int [1]){ 1 }, 1); + } break; + } } // Unset texture from material and unload it from GPU diff --git a/src/rlgl.c b/src/rlgl.c index e6ed8bb78..da2c7f693 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -2376,6 +2376,11 @@ void rlDrawMesh(Mesh mesh, Material material, Matrix transform) // Matrices and other values required by shader //----------------------------------------------------- + + // Calculate and send to shader model matrix (used by PBR shader) + SetShaderValueMatrix(material.shader, GetShaderLocation(material.shader, "mMatrix"), transform); + + // Upload to shader material.colDiffuse glUniform4f(material.shader.locs[LOC_COLOR_DIFFUSE], (float)material.maps[TEXMAP_DIFFUSE].color.r/255, (float)material.maps[TEXMAP_DIFFUSE].color.g/255, @@ -2404,12 +2409,9 @@ void rlDrawMesh(Mesh mesh, Material material, Matrix transform) if (material.maps[i].tex.id > 0) { glActiveTexture(GL_TEXTURE0 + i); - if ((i == TEXMAP_IRRADIANCE) || (i == TEXMAP_PREFILTER) || (i == TEXMAP_CUBEMAP)) - { - glBindTexture(GL_TEXTURE_CUBE_MAP, material.maps[i].tex.id); - } + if ((i == TEXMAP_IRRADIANCE) || (i == TEXMAP_PREFILTER) || (i == TEXMAP_CUBEMAP)) glBindTexture(GL_TEXTURE_CUBE_MAP, material.maps[i].tex.id); else glBindTexture(GL_TEXTURE_2D, material.maps[i].tex.id); - + glUniform1i(material.shader.locs[LOC_TEXMAP_DIFFUSE + i], i); } } @@ -2502,10 +2504,7 @@ void rlDrawMesh(Mesh mesh, Material material, Matrix transform) for (int i = 0; i < MAX_MATERIAL_TEXTURE_MAPS; i++) { glActiveTexture(GL_TEXTURE0 + i); // Set shader active texture - if ((i == TEXMAP_IRRADIANCE) || (i == TEXMAP_PREFILTER) || (i == TEXMAP_CUBEMAP)) - { - glBindTexture(GL_TEXTURE_CUBE_MAP, 0); - } + if ((i == TEXMAP_IRRADIANCE) || (i == TEXMAP_PREFILTER) || (i == TEXMAP_CUBEMAP)) glBindTexture(GL_TEXTURE_CUBE_MAP, 0); else glBindTexture(GL_TEXTURE_2D, 0); // Unbind current active texture }