Keep working on PBR...

Broken skybox sample!
This commit is contained in:
Ray 2017-07-15 14:29:09 +02:00
parent dc45f43855
commit 85507bcfc3
5 changed files with 29 additions and 59 deletions

View File

@ -22,7 +22,7 @@ int main()
InitWindow(screenWidth, screenHeight, "raylib [models] example - pbr material"); InitWindow(screenWidth, screenHeight, "raylib [models] example - pbr material");
// Define the camera to look into our 3d world // Define the camera to look into our 3d world
Camera camera = {{ 4.0f, 4.0f, 4.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; Camera camera = {{ 4.0f, 4.0f, 4.0f }, { 0.0f, 0.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
// Load model and PBR material // Load model and PBR material
Model model = LoadModel("resources/pbr/trooper.obj"); Model model = LoadModel("resources/pbr/trooper.obj");
@ -37,17 +37,16 @@ int main()
SetMaterialTexture(&model.material, TEXMAP_ROUGHNESS, LoadTexture("resources/pbr/trooper_roughness.png")); SetMaterialTexture(&model.material, TEXMAP_ROUGHNESS, LoadTexture("resources/pbr/trooper_roughness.png"));
SetMaterialTexture(&model.material, TEXMAP_OCCLUSION, LoadTexture("resources/pbr/trooper_ao.png")); SetMaterialTexture(&model.material, TEXMAP_OCCLUSION, LoadTexture("resources/pbr/trooper_ao.png"));
SetShaderValuei(model.material.shader, GetShaderLocation(model.material.shader, "normals.enabled"), (int[1]){ 1 }, 1);
// Set textures filtering for better quality // Set textures filtering for better quality
SetTextureFilter(model.material.maps[TEXMAP_ALBEDO].tex, FILTER_BILINEAR); SetTextureFilter(model.material.maps[TEXMAP_ALBEDO].tex, FILTER_BILINEAR);
SetTextureFilter(model.material.maps[TEXMAP_NORMAL].tex, FILTER_BILINEAR); SetTextureFilter(model.material.maps[TEXMAP_NORMAL].tex, FILTER_BILINEAR);
SetTextureFilter(model.material.maps[TEXMAP_METALNESS].tex, FILTER_BILINEAR); SetTextureFilter(model.material.maps[TEXMAP_METALNESS].tex, FILTER_BILINEAR);
SetTextureFilter(model.material.maps[TEXMAP_ROUGHNESS].tex, FILTER_BILINEAR); SetTextureFilter(model.material.maps[TEXMAP_ROUGHNESS].tex, FILTER_BILINEAR);
SetTextureFilter(model.material.maps[TEXMAP_OCCLUSION].tex, FILTER_BILINEAR); SetTextureFilter(model.material.maps[TEXMAP_OCCLUSION].tex, FILTER_BILINEAR);
for (int i = 0; i < 12; i++) int viewPosLoc = GetShaderLocation(model.material.shader, "viewPos");
{
//printf("TexMap %i ID: %i\n", i, model.material.maps[i].tex.id);
}
SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
@ -63,7 +62,7 @@ int main()
// Send to material PBR shader camera view position // Send to material PBR shader camera view position
float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z }; float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
SetShaderValue(model.material.shader, model.material.shader.locs[LOC_MATRIX_VIEW], cameraPos, 3); SetShaderValue(model.material.shader, viewPosLoc, cameraPos, 3);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw

View File

@ -24,23 +24,17 @@ int main()
// Define the camera to look into our 3d world // Define the camera to look into our 3d world
Camera camera = {{ 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f }; Camera camera = {{ 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
// Load skybox model and shader
#define PATH_SKYBOX_VS "resources/shaders/skybox.vs" // Path to skybox vertex shader
#define PATH_SKYBOX_FS "resources/shaders/skybox.fs" // Path to skybox vertex shader
#define PATH_TEXTURES_HDR "resources/pinetree.hdr"
#define LOC_CUSTOM_SKYRESOLUTION 15
Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f); Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
Model skybox = LoadModelFromMesh(cube, false); Model skybox = LoadModelFromMesh(cube, false);
skybox.material.shader = LoadShader(PATH_SKYBOX_VS, PATH_SKYBOX_FS); skybox.material.shader = LoadShader("resources/shaders/skybox.vs", "resources/shaders/skybox.fs");
Texture2D texHDR = LoadTexture(PATH_TEXTURES_HDR); Texture2D texHDR = LoadTexture("resources/pinetree.hdr");
skybox.material.maps[TEXMAP_CUBEMAP].tex = rlGenMapCubemap(texHDR, 512); skybox.material.maps[TEXMAP_CUBEMAP].tex = rlGenMapCubemap(texHDR, 512);
SetShaderValuei(skybox.material.shader, GetShaderLocation(skybox.material.shader, "environmentMap"), (int[1]){ TEXMAP_CUBEMAP }, 1); SetShaderValuei(skybox.material.shader, GetShaderLocation(skybox.material.shader, "environmentMap"), (int[1]){ TEXMAP_CUBEMAP }, 1);
// Get skybox shader locations // Get skybox shader locations
int skyProjectionLoc = GetShaderLocation(skybox.material.shader, "projection"); int skyProjectionLoc = GetShaderLocation(skybox.material.shader, "projection");
skybox.material.shader.locs[LOC_MATRIX_VIEW] = GetShaderLocation(skybox.material.shader, "view"); int skyViewLoc = GetShaderLocation(skybox.material.shader, "view");
// Then before rendering, configure the viewport to the actual screen dimensions // Then before rendering, configure the viewport to the actual screen dimensions
Matrix proj = MatrixPerspective(60.0, (double)GetScreenWidth()/(double)GetScreenHeight(), 0.01, 1000.0); Matrix proj = MatrixPerspective(60.0, (double)GetScreenWidth()/(double)GetScreenHeight(), 0.01, 1000.0);
@ -60,7 +54,7 @@ int main()
UpdateCamera(&camera); // Update camera UpdateCamera(&camera); // Update camera
Matrix view = MatrixLookAt(camera.position, camera.target, camera.up); Matrix view = MatrixLookAt(camera.position, camera.target, camera.up);
SetShaderValueMatrix(skybox.material.shader, skybox.material.shader.locs[LOC_MATRIX_VIEW], view); SetShaderValueMatrix(skybox.material.shader, skyViewLoc, view);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw

View File

@ -16,9 +16,10 @@
#define LIGHT_POINT 1 #define LIGHT_POINT 1
struct MaterialProperty { struct MaterialProperty {
vec3 color; int enabled;
int useSampler;
sampler2D sampler; sampler2D sampler;
float value;
vec3 color;
}; };
struct Light { struct Light {
@ -65,7 +66,6 @@ const float PI = 3.14159265359;
// Output fragment color // Output fragment color
out vec4 finalColor; out vec4 finalColor;
vec3 ComputeMaterialProperty(MaterialProperty property);
float DistributionGGX(vec3 N, vec3 H, float roughness); float DistributionGGX(vec3 N, vec3 H, float roughness);
float GeometrySchlickGGX(float NdotV, float roughness); float GeometrySchlickGGX(float NdotV, float roughness);
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness); float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness);
@ -73,14 +73,6 @@ vec3 fresnelSchlick(float cosTheta, vec3 F0);
vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness); vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness);
vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir); vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir);
vec3 ComputeMaterialProperty(MaterialProperty property)
{
//if (property.useSampler == 1) return texture(property.sampler, texCoord).rgb;
//else return property.color;
return texture(property.sampler, texCoord).rgb;
}
float DistributionGGX(vec3 N, vec3 H, float roughness) float DistributionGGX(vec3 N, vec3 H, float roughness)
{ {
float a = roughness*roughness; float a = roughness*roughness;
@ -181,20 +173,18 @@ void main()
vec3 refl = reflect(-view, normal); vec3 refl = reflect(-view, normal);
// Check if parallax mapping is enabled and calculate texture coordinates to use based on height map // Check if parallax mapping is enabled and calculate texture coordinates to use based on height map
//if (height.useSampler == 1) texCoord = ParallaxMapping(fragTexCoord, view); if (height.enabled == 1) texCoord = ParallaxMapping(fragTexCoord, view);
//else texCoord = fragTexCoord; // Use default texture coordinates else texCoord = fragTexCoord; // Use default texture coordinates
texCoord = fragTexCoord; // Use default texture coordinates
// Fetch material values from texture sampler or color attributes // Fetch material values from texture sampler or color attributes
vec3 color = pow(texture(albedo.sampler, texCoord).rgb, vec3(2.2));//pow(ComputeMaterialProperty(albedo), vec3(2.2)); vec3 color = pow(texture(albedo.sampler, texCoord).rgb, vec3(2.2));
vec3 metal = texture(metalness.sampler, texCoord).rgb; //ComputeMaterialProperty(metalness); vec3 metal = texture(metalness.sampler, texCoord).rgb;
vec3 rough = texture(roughness.sampler, texCoord).rgb; //ComputeMaterialProperty(roughness); vec3 rough = texture(roughness.sampler, texCoord).rgb;
vec3 emiss = texture(emission.sampler, texCoord).rgb; //ComputeMaterialProperty(emission); vec3 emiss = vec3(0);//texture(emission.sampler, texCoord).rgb;
vec3 ao = texture(occlusion.sampler, texCoord).rgb; //ComputeMaterialProperty(occlusion); vec3 ao = texture(occlusion.sampler, texCoord).rgb;
// Check if normal mapping is enabled // Check if normal mapping is enabled
if (normals.useSampler == 1) if (normals.enabled == 1)
{ {
// Fetch normal map color and transform lighting values to tangent space // Fetch normal map color and transform lighting values to tangent space
normal = texture(normals.sampler, texCoord).rgb; //ComputeMaterialProperty(normals); normal = texture(normals.sampler, texCoord).rgb; //ComputeMaterialProperty(normals);

View File

@ -1318,9 +1318,6 @@ 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_PREFILTER] = GetShaderLocation(mat.shader, "prefilterMap");
mat.shader.locs[LOC_TEXMAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT"); 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 // Set up material properties color
mat.maps[TEXMAP_ALBEDO].color = albedo; mat.maps[TEXMAP_ALBEDO].color = albedo;
mat.maps[TEXMAP_NORMAL].color = (Color){ 128, 128, 255, 255 }; mat.maps[TEXMAP_NORMAL].color = (Color){ 128, 128, 255, 255 };

View File

@ -2376,28 +2376,12 @@ void rlDrawMesh(Mesh mesh, Material material, Matrix transform)
// Matrices and other values required by shader // Matrices and other values required by shader
//----------------------------------------------------- //-----------------------------------------------------
/*
// Calculate and send to shader model matrix
Matrix matScale = MatrixScale(scale.x, scale.y, scale.z);
Matrix matRotation = MatrixRotate(rotationAxis, rotationAngle*DEG2RAD);
Matrix matTranslation = MatrixTranslate(position.x, position.y, position.z);
Matrix transform = MatrixMultiply(MatrixMultiply(matScale, matRotation), matTranslation);
SetShaderValueMatrix(mat.shader, GetShaderLocation(mat.shader, "mMatrix"), transform);
*/
// Upload to shader material.colDiffuse // Upload to shader material.colDiffuse
glUniform4f(material.shader.locs[LOC_COLOR_DIFFUSE], (float)material.maps[TEXMAP_DIFFUSE].color.r/255, glUniform4f(material.shader.locs[LOC_COLOR_DIFFUSE], (float)material.maps[TEXMAP_DIFFUSE].color.r/255,
(float)material.maps[TEXMAP_DIFFUSE].color.g/255, (float)material.maps[TEXMAP_DIFFUSE].color.g/255,
(float)material.maps[TEXMAP_DIFFUSE].color.b/255, (float)material.maps[TEXMAP_DIFFUSE].color.b/255,
(float)material.maps[TEXMAP_DIFFUSE].color.a/255); (float)material.maps[TEXMAP_DIFFUSE].color.a/255);
// TODO: Upload to shader material.colAmbient (if available)
//if (material.shader.locs[LOC_TEXTURE_COLOR02] != -1)
// glUniform4f(material.shader.locs[LOC_TEXTURE_COLOR02], (float)material.colAmbient.r/255,
// (float)material.colAmbient.g/255,
// (float)material.colAmbient.b/255,
// (float)material.colAmbient.a/255);
// Upload to shader material.colSpecular (if available) // Upload to shader material.colSpecular (if available)
if (material.shader.locs[LOC_COLOR_SPECULAR] != -1) if (material.shader.locs[LOC_COLOR_SPECULAR] != -1)
glUniform4f(material.shader.locs[LOC_COLOR_SPECULAR], (float)material.maps[TEXMAP_SPECULAR].color.r/255, glUniform4f(material.shader.locs[LOC_COLOR_SPECULAR], (float)material.maps[TEXMAP_SPECULAR].color.r/255,
@ -2420,7 +2404,10 @@ void rlDrawMesh(Mesh mesh, Material material, Matrix transform)
if (material.maps[i].tex.id > 0) if (material.maps[i].tex.id > 0)
{ {
glActiveTexture(GL_TEXTURE0 + i); 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); else glBindTexture(GL_TEXTURE_2D, material.maps[i].tex.id);
glUniform1i(material.shader.locs[LOC_TEXMAP_DIFFUSE + i], i); glUniform1i(material.shader.locs[LOC_TEXMAP_DIFFUSE + i], i);
@ -2515,7 +2502,10 @@ void rlDrawMesh(Mesh mesh, Material material, Matrix transform)
for (int i = 0; i < MAX_MATERIAL_TEXTURE_MAPS; i++) for (int i = 0; i < MAX_MATERIAL_TEXTURE_MAPS; i++)
{ {
glActiveTexture(GL_TEXTURE0 + i); // Set shader active texture 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 else glBindTexture(GL_TEXTURE_2D, 0); // Unbind current active texture
} }