Added useSampler assign in SetMaterialTexture and fixed model transform matrix data in shader

This commit is contained in:
victorfisac 2017-07-15 16:30:07 +02:00
parent 4a7c0618c6
commit 242cb573ce
2 changed files with 51 additions and 9 deletions

View File

@ -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

View File

@ -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,10 +2409,7 @@ 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
}