diff --git a/examples/shaders/resources/models/old_car_new.glb b/examples/shaders/resources/models/old_car_new.glb new file mode 100644 index 00000000..119995ca Binary files /dev/null and b/examples/shaders/resources/models/old_car_new.glb differ diff --git a/examples/shaders/resources/models/plane.glb b/examples/shaders/resources/models/plane.glb new file mode 100644 index 00000000..452e1c5b Binary files /dev/null and b/examples/shaders/resources/models/plane.glb differ diff --git a/examples/shaders/resources/old_car_d.png b/examples/shaders/resources/old_car_d.png new file mode 100644 index 00000000..d8b3c833 Binary files /dev/null and b/examples/shaders/resources/old_car_d.png differ diff --git a/examples/shaders/resources/old_car_e.png b/examples/shaders/resources/old_car_e.png new file mode 100644 index 00000000..23f01c0f Binary files /dev/null and b/examples/shaders/resources/old_car_e.png differ diff --git a/examples/shaders/resources/old_car_mra.png b/examples/shaders/resources/old_car_mra.png new file mode 100644 index 00000000..0fb46b33 Binary files /dev/null and b/examples/shaders/resources/old_car_mra.png differ diff --git a/examples/shaders/resources/old_car_n.png b/examples/shaders/resources/old_car_n.png new file mode 100644 index 00000000..11f689fe Binary files /dev/null and b/examples/shaders/resources/old_car_n.png differ diff --git a/examples/shaders/resources/road_a.png b/examples/shaders/resources/road_a.png new file mode 100644 index 00000000..10377739 Binary files /dev/null and b/examples/shaders/resources/road_a.png differ diff --git a/examples/shaders/resources/road_mra.png b/examples/shaders/resources/road_mra.png new file mode 100644 index 00000000..988c839c Binary files /dev/null and b/examples/shaders/resources/road_mra.png differ diff --git a/examples/shaders/resources/road_n.png b/examples/shaders/resources/road_n.png new file mode 100644 index 00000000..a5f3548c Binary files /dev/null and b/examples/shaders/resources/road_n.png differ diff --git a/examples/shaders/resources/shaders/glsl100/ighting.fs b/examples/shaders/resources/shaders/glsl100/ighting.fs new file mode 100644 index 00000000..e6faece7 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl100/ighting.fs @@ -0,0 +1,75 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec3 fragPosition; +varying vec2 fragTexCoord; +varying vec4 fragColor; +varying vec3 fragNormal; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// NOTE: Add here your custom variables + +#define MAX_LIGHTS 4 +#define LIGHT_DIRECTIONAL 0 +#define LIGHT_POINT 1 + +struct Light { + int enabled; + int type; + vec3 position; + vec3 target; + vec4 color; +}; + +// Input lighting values +uniform Light lights[MAX_LIGHTS]; +uniform vec4 ambient; +uniform vec3 viewPos; + +void main() +{ + // Texel color fetching from texture sampler + vec4 texelColor = texture2D(texture0, fragTexCoord); + vec3 lightDot = vec3(0.0); + vec3 normal = normalize(fragNormal); + vec3 viewD = normalize(viewPos - fragPosition); + vec3 specular = vec3(0.0); + + // NOTE: Implement here your fragment shader code + + for (int i = 0; i < MAX_LIGHTS; i++) + { + if (lights[i].enabled == 1) + { + vec3 light = vec3(0.0); + + if (lights[i].type == LIGHT_DIRECTIONAL) + { + light = -normalize(lights[i].target - lights[i].position); + } + + if (lights[i].type == LIGHT_POINT) + { + light = normalize(lights[i].position - fragPosition); + } + + float NdotL = max(dot(normal, light), 0.0); + lightDot += lights[i].color.rgb*NdotL; + + float specCo = 0.0; + if (NdotL > 0.0) specCo = pow(max(0.0, dot(viewD, reflect(-(light), normal))), 16.0); // 16 refers to shine + specular += specCo; + } + } + + vec4 finalColor = (texelColor*((colDiffuse + vec4(specular, 1.0))*vec4(lightDot, 1.0))); + finalColor += texelColor*(ambient/10.0); + + // Gamma correction + gl_FragColor = pow(finalColor, vec4(1.0/2.2)); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl100/lighting.vs b/examples/shaders/resources/shaders/glsl100/lighting.vs new file mode 100644 index 00000000..f973d2cd --- /dev/null +++ b/examples/shaders/resources/shaders/glsl100/lighting.vs @@ -0,0 +1,59 @@ +#version 100 + +// Input vertex attributes +attribute vec3 vertexPosition; +attribute vec2 vertexTexCoord; +attribute vec3 vertexNormal; +attribute vec4 vertexColor; + +// Input uniform values +uniform mat4 mvp; +uniform mat4 matModel; + +// Output vertex attributes (to fragment shader) +varying vec3 fragPosition; +varying vec2 fragTexCoord; +varying vec4 fragColor; +varying vec3 fragNormal; + +// NOTE: Add here your custom variables + +// https://github.com/glslify/glsl-inverse +mat3 inverse(mat3 m) +{ + float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2]; + float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2]; + float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2]; + + float b01 = a22*a11 - a12*a21; + float b11 = -a22*a10 + a12*a20; + float b21 = a21*a10 - a11*a20; + + float det = a00*b01 + a01*b11 + a02*b21; + + return mat3(b01, (-a22*a01 + a02*a21), (a12*a01 - a02*a11), + b11, (a22*a00 - a02*a20), (-a12*a00 + a02*a10), + b21, (-a21*a00 + a01*a20), (a11*a00 - a01*a10))/det; +} + +// https://github.com/glslify/glsl-transpose +mat3 transpose(mat3 m) +{ + return mat3(m[0][0], m[1][0], m[2][0], + m[0][1], m[1][1], m[2][1], + m[0][2], m[1][2], m[2][2]); +} + +void main() +{ + // Send vertex attributes to fragment shader + fragPosition = vec3(matModel*vec4(vertexPosition, 1.0)); + fragTexCoord = vertexTexCoord; + fragColor = vertexColor; + + mat3 normalMatrix = transpose(inverse(mat3(matModel))); + fragNormal = normalize(normalMatrix*vertexNormal); + + // Calculate final vertex position + gl_Position = mvp*vec4(vertexPosition, 1.0); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl100/pbr.fs b/examples/shaders/resources/shaders/glsl100/pbr.fs new file mode 100644 index 00000000..b94974d7 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl100/pbr.fs @@ -0,0 +1,156 @@ +#version 100 + +precision mediump float; + +#define MAX_LIGHTS 4 +#define LIGHT_DIRECTIONAL 0 +#define LIGHT_POINT 1 +#define PI 3.14159265358979323846 + +struct Light { + int enabled; + int type; + vec3 position; + vec3 target; + vec4 color; + float intensity; +}; + +// Input vertex attributes (from vertex shader) +varying in vec3 fragPosition; +varying in vec2 fragTexCoord; +varying in vec4 fragColor; +varying in vec3 fragNormal; +varying in vec4 shadowPos; +varying in mat3 TBN; + + +// Input uniform values +uniform int numOfLights; +uniform sampler2D albedoMap; +uniform sampler2D mraMap; +uniform sampler2D normalMap; +uniform sampler2D emissiveMap; // r: Hight g:emissive + +uniform vec2 tiling; +uniform vec2 offset; + +uniform int useTexAlbedo; +uniform int useTexNormal; +uniform int useTexMRA; +uniform int useTexEmissive; + +uniform vec4 albedoColor; +uniform vec4 emissiveColor; +uniform float normalValue; +uniform float metallicValue; +uniform float roughnessValue; +uniform float aoValue; +uniform float emissivePower; + +// Input lighting values +uniform Light lights[MAX_LIGHTS]; +uniform vec3 viewPos; + +uniform vec3 ambientColor; +uniform float ambient; + +// refl in range 0 to 1 +// returns base reflectivity to 1 +// incrase reflectivity when surface view at larger angle +vec3 schlickFresnel(float hDotV,vec3 refl) +{ + return refl + (1.0 - refl) * pow(1.0 - hDotV,5.0); +} + +float ggxDistribution(float nDotH,float roughness) +{ + float a = roughness * roughness * roughness * roughness; + float d = nDotH * nDotH * (a - 1.0) + 1.0; + d = PI * d * d; + return a / max(d,0.0000001); +} + +float geomSmith(float nDotV,float nDotL,float roughness) +{ + float r = roughness + 1.0; + float k = r * r / 8.0; + float ik = 1.0 - k; + float ggx1 = nDotV / (nDotV * ik + k); + float ggx2 = nDotL / (nDotL * ik + k); + return ggx1 * ggx2; +} + +vec3 pbr(){ + vec3 albedo = texture2D(albedoMap,vec2(fragTexCoord.x*tiling.x+offset.x,fragTexCoord.y*tiling.y+offset.y)).rgb; + albedo = vec3(albedoColor.x*albedo.x,albedoColor.y*albedo.y,albedoColor.z*albedo.z); + float metallic = clamp(metallicValue,0.0,1.0); + float roughness = clamp(roughnessValue,0.0,1.0); + float ao = clamp(aoValue,0.0,1.0); + if(useTexMRA == 1) { + vec4 mra = texture2D(mraMap, vec2(fragTexCoord.x * tiling.x + offset.x, fragTexCoord.y * tiling.y + offset.y)); + metallic = clamp(mra.r+metallicValue,0.04,1.0); + roughness = clamp(mra.g+roughnessValue,0.04,1.0); + ao = (mra.b+aoValue)*0.5; + } + + + + vec3 N = normalize(fragNormal); + if(useTexNormal == 1) { + N = texture2D(normalMap, vec2(fragTexCoord.x * tiling.x + offset.y, fragTexCoord.y * tiling.y + offset.y)).rgb; + N = normalize(N * 2.0 - 1.0); + N = normalize(N * TBN); + } + + vec3 V = normalize(viewPos - fragPosition); + + vec3 e = vec3(0); + e = (texture2D(emissiveMap, vec2(fragTexCoord.x*tiling.x+offset.x, fragTexCoord.y*tiling.y+offset.y)).rgb).g * emissiveColor.rgb*emissivePower * float(useTexEmissive); + + //return N;//vec3(metallic,metallic,metallic); + //if dia-electric use base reflectivity of 0.04 otherwise ut is a metal use albedo as base reflectivity + vec3 baseRefl = mix(vec3(0.04),albedo.rgb,metallic); + vec3 Lo = vec3(0.0); // acumulate lighting lum + + for(int i=0;i 0.0) specCo = pow(max(0.0, dot(viewD, reflect(-(light), normal))), 16.0); // 16 refers to shine + specular += specCo; + } + } + + vec4 finalColor = (texelColor*((colDiffuse + vec4(specular, 1.0))*vec4(lightDot, 1.0))); + finalColor += texelColor*(ambient/10.0); + + // Gamma correction + gl_FragColor = pow(finalColor, vec4(1.0/2.2)); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl330/lighting.vs b/examples/shaders/resources/shaders/glsl330/lighting.vs new file mode 100644 index 00000000..6b176354 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl330/lighting.vs @@ -0,0 +1,32 @@ +#version 330 + +// Input vertex attributes +in vec3 vertexPosition; +in vec2 vertexTexCoord; +in vec3 vertexNormal; +in vec4 vertexColor; + +// Input uniform values +uniform mat4 mvp; +uniform mat4 matModel; +uniform mat4 matNormal; + +// Output vertex attributes (to fragment shader) +out vec3 fragPosition; +out vec2 fragTexCoord; +out vec4 fragColor; +out vec3 fragNormal; + +// NOTE: Add here your custom variables + +void main() +{ + // Send vertex attributes to fragment shader + fragPosition = vec3(matModel*vec4(vertexPosition, 1.0)); + fragTexCoord = vertexTexCoord; + fragColor = vertexColor; + fragNormal = normalize(vec3(matNormal*vec4(vertexNormal, 1.0))); + + // Calculate final vertex position + gl_Position = mvp*vec4(vertexPosition, 1.0); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl330/pbr.fs b/examples/shaders/resources/shaders/glsl330/pbr.fs new file mode 100644 index 00000000..d3e08473 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl330/pbr.fs @@ -0,0 +1,162 @@ +#version 330 + +#define MAX_LIGHTS 4 +#define LIGHT_DIRECTIONAL 0 +#define LIGHT_POINT 1 +#define PI 3.14159265358979323846 + +struct Light { + int enabled; + int type; + vec3 position; + vec3 target; + vec4 color; + float intensity; +}; + +// Input vertex attributes (from vertex shader) +in vec3 fragPosition; +in vec2 fragTexCoord; +in vec4 fragColor; +in vec3 fragNormal; +in vec4 shadowPos; +in mat3 TBN; + +// Output fragment color +out vec4 finalColor; + +// Input uniform values +uniform int numOfLights; +uniform sampler2D albedoMap; +uniform sampler2D mraMap; +uniform sampler2D normalMap; +uniform sampler2D emissiveMap; // r: Hight g:emissive + +uniform vec2 tiling; +uniform vec2 offset; + +uniform int useTexAlbedo; +uniform int useTexNormal; +uniform int useTexMRA; +uniform int useTexEmissive; + +uniform vec4 albedoColor; +uniform vec4 emissiveColor; +uniform float normalValue; +uniform float metallicValue; +uniform float roughnessValue; +uniform float aoValue; +uniform float emissivePower; + +// Input lighting values +uniform Light lights[MAX_LIGHTS]; +uniform vec3 viewPos; + +uniform vec3 ambientColor; +uniform float ambient; + +// Reflectivity in range 0.0 to 1.0 +// NOTE: Reflectivity is increased when surface view at larger angle +vec3 SchlickFresnel(float hDotV,vec3 refl) +{ + return refl + (1.0 - refl)*pow(1.0 - hDotV, 5.0); +} + +float GgxDistribution(float nDotH,float roughness) +{ + float a = roughness * roughness * roughness * roughness; + float d = nDotH * nDotH * (a - 1.0) + 1.0; + d = PI * d * d; + return a / max(d,0.0000001); +} + +float GeomSmith(float nDotV,float nDotL,float roughness) +{ + float r = roughness + 1.0; + float k = r*r / 8.0; + float ik = 1.0 - k; + float ggx1 = nDotV/(nDotV*ik + k); + float ggx2 = nDotL/(nDotL*ik + k); + return ggx1*ggx2; +} + +vec3 ComputePBR() +{ + vec3 albedo = texture(albedoMap,vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y)).rgb; + albedo = vec3(albedoColor.x*albedo.x, albedoColor.y*albedo.y, albedoColor.z*albedo.z); + + float metallic = clamp(metallicValue, 0.0, 1.0); + float roughness = clamp(roughnessValue, 0.0, 1.0); + float ao = clamp(aoValue, 0.0, 1.0); + + if (useTexMRA == 1) + { + vec4 mra = texture(mraMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y))*useTexMRA; + metallic = clamp(mra.r + metallicValue, 0.04, 1.0); + roughness = clamp(mra.g + roughnessValue, 0.04, 1.0); + ao = (mra.b + aoValue)*0.5; + } + + vec3 N = normalize(fragNormal); + if (useTexNormal == 1) + { + N = texture(normalMap, vec2(fragTexCoord.x*tiling.x + offset.y, fragTexCoord.y*tiling.y + offset.y)).rgb; + N = normalize(N*2.0 - 1.0); + N = normalize(N*TBN); + } + + vec3 V = normalize(viewPos - fragPosition); + + vec3 emissive = vec3(0); + emissive = (texture(emissiveMap, vec2(fragTexCoord.x*tiling.x+offset.x, fragTexCoord.y*tiling.y+offset.y)).rgb).g * emissiveColor.rgb*emissivePower * useTexEmissive; + + // return N;//vec3(metallic,metallic,metallic); + // if dia-electric use base reflectivity of 0.04 otherwise ut is a metal use albedo as base reflectivity + vec3 baseRefl = mix(vec3(0.04), albedo.rgb, metallic); + vec3 lightAccum = vec3(0.0); // Acumulate lighting lum + + for (int i = 0; i < numOfLights; i++) + { + vec3 L = normalize(lights[i].position - fragPosition); // Compute light vector + vec3 H = normalize(V + L); // Compute halfway bisecting vector + float dist = length(lights[i].position - fragPosition); // Compute distance to light + float attenuation = 1.0/(dist*dist*0.23); // Compute attenuation + vec3 radiance = lights[i].color.rgb*lights[i].intensity*attenuation; // Compute input radiance, light energy comming in + + // Cook-Torrance BRDF distribution function + float nDotV = max(dot(N,V), 0.0000001); + float nDotL = max(dot(N,L), 0.0000001); + float hDotV = max(dot(H,V), 0.0); + float nDotH = max(dot(N,H), 0.0); + float D = GgxDistribution(nDotH, roughness); // Larger the more micro-facets aligned to H + float G = GeomSmith(nDotV, nDotL, roughness); // Smaller the more micro-facets shadow + vec3 F = SchlickFresnel(hDotV, baseRefl); // Fresnel proportion of specular reflectance + + vec3 spec = (D*G*F)/(4.0*nDotV*nDotL); + + // Difuse and spec light can't be above 1.0 + // kD = 1.0 - kS diffuse component is equal 1.0 - spec comonent + vec3 kD = vec3(1.0) - F; + + // Mult kD by the inverse of metallnes, only non-metals should have diffuse light + kD *= 1.0 - metallic; + lightAccum += ((kD*albedo.rgb/PI + spec)*radiance*nDotL)*lights[i].enabled; // Angle of light has impact on result + } + + vec3 ambientFinal = (ambientColor + albedo)*ambient*0.5; + + return ambientFinal + lightAccum*ao + emissive; +} + +void main() +{ + vec3 color = ComputePBR(); + + // HDR tonemapping + color = pow(color, color + vec3(1.0)); + + // Gamma correction + color = pow(color, vec3(1.0/2.2)); + + finalColor = vec4(color, 1.0); +} \ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl330/pbr.vs b/examples/shaders/resources/shaders/glsl330/pbr.vs new file mode 100644 index 00000000..2842a944 --- /dev/null +++ b/examples/shaders/resources/shaders/glsl330/pbr.vs @@ -0,0 +1,48 @@ +#version 330 + +// Input vertex attributes +in vec3 vertexPosition; +in vec2 vertexTexCoord; +in vec3 vertexNormal; +in vec3 vertexTangent; +in vec4 vertexColor; + +// Input uniform values +uniform mat4 mvp; +uniform mat4 matModel; +uniform mat4 matNormal; +uniform vec3 lightPos; +uniform vec4 difColor; + +// Output vertex attributes (to fragment shader) +out vec3 fragPosition; +out vec2 fragTexCoord; +out vec4 fragColor; +out vec3 fragNormal; +out mat3 TBN; + +const float normalOffset = 0.1; + +void main() +{ + // Compute binormal from vertex normal and tangent + vec3 vertexBinormal = cross(vertexNormal, vertexTangent); + + // Compute fragment normal based on normal transformations + mat3 normalMatrix = transpose(inverse(mat3(matModel))); + + // Compute fragment position based on model transformations + fragPosition = vec3(matModel*vec4(vertexPosition, 1.0f)); + + fragTexCoord = vertexTexCoord*2.0; + fragNormal = normalize(normalMatrix*vertexNormal); + vec3 fragTangent = normalize(normalMatrix*vertexTangent); + fragTangent = normalize(fragTangent - dot(fragTangent, fragNormal)*fragNormal); + vec3 fragBinormal = normalize(normalMatrix*vertexBinormal); + fragBinormal = cross(fragNormal, fragTangent); + + TBN = transpose(mat3(fragTangent, fragBinormal, fragNormal)); + + // Calculate final vertex position + gl_Position = mvp*vec4(vertexPosition, 1.0); +} \ No newline at end of file diff --git a/examples/shaders/rlights.h b/examples/shaders/rlights.h new file mode 100644 index 00000000..0ebc43af --- /dev/null +++ b/examples/shaders/rlights.h @@ -0,0 +1,170 @@ +/********************************************************************************************** +* +* raylib.lights - Some useful functions to deal with lights data +* +* CONFIGURATION: +* +* #define RLIGHTS_IMPLEMENTATION +* Generates the implementation of the library into the included file. +* If not defined, the library is in header only mode and can be included in other headers +* or source files without problems. But only ONE file should hold the implementation. +* +* LICENSE: zlib/libpng +* +* Copyright (c) 2017-2024 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5) +* +* This software is provided "as-is", without any express or implied warranty. In no event +* will the authors be held liable for any damages arising from the use of this software. +* +* Permission is granted to anyone to use this software for any purpose, including commercial +* applications, and to alter it and redistribute it freely, subject to the following restrictions: +* +* 1. The origin of this software must not be misrepresented; you must not claim that you +* wrote the original software. If you use this software in a product, an acknowledgment +* in the product documentation would be appreciated but is not required. +* +* 2. Altered source versions must be plainly marked as such, and must not be misrepresented +* as being the original software. +* +* 3. This notice may not be removed or altered from any source distribution. +* +**********************************************************************************************/ + +#ifndef RLIGHTS_H +#define RLIGHTS_H + +//---------------------------------------------------------------------------------- +// Defines and Macros +//---------------------------------------------------------------------------------- +#define MAX_LIGHTS 4 // Max dynamic lights supported by shader + +//---------------------------------------------------------------------------------- +// Types and Structures Definition +//---------------------------------------------------------------------------------- + +// Light data +typedef struct { + int type; + bool enabled; + Vector3 position; + Vector3 target; + Color color; + float attenuation; + + // Shader locations + int enabledLoc; + int typeLoc; + int positionLoc; + int targetLoc; + int colorLoc; + int attenuationLoc; +} Light; + +// Light type +typedef enum { + LIGHT_DIRECTIONAL = 0, + LIGHT_POINT +} LightType; + +#ifdef __cplusplus +extern "C" { // Prevents name mangling of functions +#endif + +//---------------------------------------------------------------------------------- +// Module Functions Declaration +//---------------------------------------------------------------------------------- +Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader); // Create a light and get shader locations +void UpdateLightValues(Shader shader, Light light); // Send light properties to shader + +#ifdef __cplusplus +} +#endif + +#endif // RLIGHTS_H + + +/*********************************************************************************** +* +* RLIGHTS IMPLEMENTATION +* +************************************************************************************/ + +#if defined(RLIGHTS_IMPLEMENTATION) + +#include "raylib.h" + +//---------------------------------------------------------------------------------- +// Defines and Macros +//---------------------------------------------------------------------------------- +// ... + +//---------------------------------------------------------------------------------- +// Types and Structures Definition +//---------------------------------------------------------------------------------- +// ... + +//---------------------------------------------------------------------------------- +// Global Variables Definition +//---------------------------------------------------------------------------------- +static int lightsCount = 0; // Current amount of created lights + +//---------------------------------------------------------------------------------- +// Module specific Functions Declaration +//---------------------------------------------------------------------------------- +// ... + +//---------------------------------------------------------------------------------- +// Module Functions Definition +//---------------------------------------------------------------------------------- + +// Create a light and get shader locations +Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader) +{ + Light light = { 0 }; + + if (lightsCount < MAX_LIGHTS) + { + light.enabled = true; + light.type = type; + light.position = position; + light.target = target; + light.color = color; + + // NOTE: Lighting shader naming must be the provided ones + light.enabledLoc = GetShaderLocation(shader, TextFormat("lights[%i].enabled", lightsCount)); + light.typeLoc = GetShaderLocation(shader, TextFormat("lights[%i].type", lightsCount)); + light.positionLoc = GetShaderLocation(shader, TextFormat("lights[%i].position", lightsCount)); + light.targetLoc = GetShaderLocation(shader, TextFormat("lights[%i].target", lightsCount)); + light.colorLoc = GetShaderLocation(shader, TextFormat("lights[%i].color", lightsCount)); + + UpdateLightValues(shader, light); + + lightsCount++; + } + + return light; +} + +// Send light properties to shader +// NOTE: Light shader locations should be available +void UpdateLightValues(Shader shader, Light light) +{ + // Send to shader light enabled state and type + SetShaderValue(shader, light.enabledLoc, &light.enabled, SHADER_UNIFORM_INT); + SetShaderValue(shader, light.typeLoc, &light.type, SHADER_UNIFORM_INT); + + // Send to shader light position values + float position[3] = { light.position.x, light.position.y, light.position.z }; + SetShaderValue(shader, light.positionLoc, position, SHADER_UNIFORM_VEC3); + + // Send to shader light target position values + float target[3] = { light.target.x, light.target.y, light.target.z }; + SetShaderValue(shader, light.targetLoc, target, SHADER_UNIFORM_VEC3); + + // Send to shader light color values + float color[4] = { (float)light.color.r/(float)255, (float)light.color.g/(float)255, + (float)light.color.b/(float)255, (float)light.color.a/(float)255 }; + SetShaderValue(shader, light.colorLoc, color, SHADER_UNIFORM_VEC4); +} + +#endif // RLIGHTS_IMPLEMENTATION \ No newline at end of file diff --git a/examples/shaders/shaders_basic_lighting.cpp b/examples/shaders/shaders_basic_lighting.cpp new file mode 100644 index 00000000..1ef00ce5 --- /dev/null +++ b/examples/shaders/shaders_basic_lighting.cpp @@ -0,0 +1,145 @@ +/******************************************************************************************* +* +* raylib [shaders] example - basic lighting +* +* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, +* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. +* +* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3). +* +* Example originally created with raylib 3.0, last time updated with raylib 4.2 +* +* Example contributed by Chris Camacho (@codifies) and reviewed by Ramon Santamaria (@raysan5) +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +* Copyright (c) 2019-2024 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib-cpp.hpp" + +#include "raymath.h" + +#define RLIGHTS_IMPLEMENTATION +#include "rlights.h" + +#if defined(PLATFORM_DESKTOP) +#define GLSL_VERSION 330 +#else // PLATFORM_ANDROID, PLATFORM_WEB +#define GLSL_VERSION 100 +#endif + +#include + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting"); + + // Define the camera to look into our 3d world + raylib::Camera camera; + camera.position = (Vector3){ 2.0f, 4.0f, 6.0f }; // Camera position + camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target) + camera.fovy = 45.0f; // Camera field-of-view Y + camera.projection = CAMERA_PERSPECTIVE; // Camera projection type + + // Load basic lighting shader + raylib::Shader shader (TextFormat("resources/shaders/glsl%i/lighting.vs", GLSL_VERSION), + TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION)); + // Get some required shader locations + shader.locs[SHADER_LOC_VECTOR_VIEW] = shader.GetLocation("viewPos"); + // NOTE: "matModel" location name is automatically assigned on shader loading, + // no need to get the location again if using that uniform name + //shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel"); + + // Ambient light level (some basic lighting) + int ambientLoc = shader.GetLocation("ambient"); + std::array ambientValues = {0.1f, 0.1f, 0.1f, 1.0f}; + shader.SetValue(ambientLoc, ambientValues.data(), SHADER_UNIFORM_VEC4); + + // Create lights + std::array lights = { + CreateLight(LIGHT_POINT, (Vector3) {-2, 1, -2}, Vector3Zero(), YELLOW, shader), + CreateLight(LIGHT_POINT, (Vector3) {2, 1, 2}, Vector3Zero(), RED, shader), + CreateLight(LIGHT_POINT, (Vector3) {-2, 1, 2}, Vector3Zero(), GREEN, shader), + CreateLight(LIGHT_POINT, (Vector3) {2, 1, -2}, Vector3Zero(), BLUE, shader), + }; + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + camera.Update(CAMERA_ORBITAL); + + // Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f }) + std::array cameraPos = { camera.position.x, camera.position.y, camera.position.z }; + SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos.data(), SHADER_UNIFORM_VEC3); + + // Check key inputs to enable/disable lights + if (IsKeyPressed(KEY_Y)) { lights[0].enabled = !lights[0].enabled; } + if (IsKeyPressed(KEY_R)) { lights[1].enabled = !lights[1].enabled; } + if (IsKeyPressed(KEY_G)) { lights[2].enabled = !lights[2].enabled; } + if (IsKeyPressed(KEY_B)) { lights[3].enabled = !lights[3].enabled; } + + // Update light values (actually, only enable/disable them) + for (const auto& light : lights) UpdateLightValues(shader, light); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + BeginMode3D(camera); + + BeginShaderMode(shader); + + DrawPlane(Vector3Zero(), (Vector2) { 10.0, 10.0 }, WHITE); + DrawCube(Vector3Zero(), 2.0, 4.0, 2.0, WHITE); + + EndShaderMode(); + + // Draw spheres to show where the lights are + for (const auto& light : lights) + { + if (light.enabled) DrawSphereEx(light.position, 0.2f, 8, 8, light.color); + else DrawSphereWires(light.position, 0.2f, 8, 8, ColorAlpha(light.color, 0.3f)); + } + + DrawGrid(10, 1.0f); + + EndMode3D(); + + DrawFPS(10, 10); + + DrawText("Use keys [Y][R][G][B] to toggle lights", 10, 40, 20, DARKGRAY); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + //UnloadShader(shader); // Unload shader + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/shaders/shaders_basic_pbr.cpp b/examples/shaders/shaders_basic_pbr.cpp new file mode 100644 index 00000000..3afeb8ff --- /dev/null +++ b/examples/shaders/shaders_basic_pbr.cpp @@ -0,0 +1,333 @@ +/******************************************************************************************* +* +* raylib [shaders] example - Basic PBR +* +* Example originally created with raylib 5.0, last time updated with raylib 5.1-dev +* +* Example contributed by Afan OLOVCIC (@_DevDad) and reviewed by Ramon Santamaria (@raysan5) +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +* Copyright (c) 2023-2024 Afan OLOVCIC (@_DevDad) +* +* Model: "Old Rusty Car" (https://skfb.ly/LxRy) by Renafox, +* licensed under Creative Commons Attribution-NonCommercial +* (http://creativecommons.org/licenses/by-nc/4.0/) +* +********************************************************************************************/ + +#include "raylib-cpp.hpp" + +#if defined(PLATFORM_DESKTOP) +#define GLSL_VERSION 330 +#else // PLATFORM_ANDROID, PLATFORM_WEB +#define GLSL_VERSION 120 +#endif + +#include + +static const int MAX_LIGHTS = 4; // Max dynamic lights supported by shader + +//---------------------------------------------------------------------------------- +// Types and Structures Definition +//---------------------------------------------------------------------------------- + +// Light type +enum class LightType { + DIRECTIONAL = 0, + POINT, + SPOT +}; + +// Light data +struct Light { + LightType type; + int enabled; + raylib::Vector3 position; + raylib::Vector3 target; + std::array color; + float intensity; + + // Shader light parameters locations + int typeLoc; + int enabledLoc; + int positionLoc; + int targetLoc; + int colorLoc; + int intensityLoc; +}; + +//---------------------------------------------------------------------------------- +// Global Variables Definition +//---------------------------------------------------------------------------------- +static int lightCount = 0; // Current number of dynamic lights that have been created + +//---------------------------------------------------------------------------------- +// Module specific Functions Declaration +//---------------------------------------------------------------------------------- +// Create a light and get shader locations +static Light CreateLight(size_t index, LightType type, Vector3 position, Vector3 target, Color color, float intensity, Shader shader); + +// Update light properties on shader +// NOTE: Light shader locations should be available +static void UpdateLight(Shader shader, const Light& light); + +//---------------------------------------------------------------------------------- +// Main Entry Point +//---------------------------------------------------------------------------------- +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + SetConfigFlags(FLAG_MSAA_4X_HINT); + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic pbr"); + + // Define the camera to look into our 3d world + raylib::Camera camera; + camera.position = (Vector3){ 2.0f, 2.0f, 6.0f }; // Camera position + camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target) + camera.fovy = 45.0f; // Camera field-of-view Y + camera.projection = CAMERA_PERSPECTIVE; // Camera projection type + + // Load PBR shader and setup all required locations + raylib::Shader shader (TextFormat("resources/shaders/glsl%i/pbr.vs", GLSL_VERSION), + TextFormat("resources/shaders/glsl%i/pbr.fs", GLSL_VERSION)); + shader.locs[SHADER_LOC_MAP_ALBEDO] = GetShaderLocation(shader, "albedoMap"); + // WARNING: Metalness, roughness, and ambient occlusion are all packed into a MRA texture + // They are passed as to the SHADER_LOC_MAP_METALNESS location for convenience, + // shader already takes care of it accordingly + shader.locs[SHADER_LOC_MAP_METALNESS] = GetShaderLocation(shader, "mraMap"); + shader.locs[SHADER_LOC_MAP_NORMAL] = GetShaderLocation(shader, "normalMap"); + // WARNING: Similar to the MRA map, the emissive map packs different information + // into a single texture: it stores height and emission data + // It is binded to SHADER_LOC_MAP_EMISSION location an properly processed on shader + shader.locs[SHADER_LOC_MAP_EMISSION] = GetShaderLocation(shader, "emissiveMap"); + shader.locs[SHADER_LOC_COLOR_DIFFUSE] = GetShaderLocation(shader, "albedoColor"); + + // Setup additional required shader locations, including lights data + shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos"); + int lightCountLoc = GetShaderLocation(shader, "numOfLights"); + int maxLightCount = MAX_LIGHTS; + shader.SetValue(lightCountLoc, &maxLightCount, SHADER_UNIFORM_INT); + + // Setup ambient color and intensity parameters + float ambientIntensity = 0.02f; + raylib::Color ambientColor = (Color){ 26, 32, 135, 255 }; + raylib::Vector3 ambientColorNormalized = (Vector3){ ambientColor.r/255.0f, ambientColor.g/255.0f, ambientColor.b/255.0f }; + shader.SetValue(shader.GetLocation("ambientColor"), &ambientColorNormalized, SHADER_UNIFORM_VEC3); + shader.SetValue(shader.GetLocation("ambient"), &ambientIntensity, SHADER_UNIFORM_FLOAT); + + // Get location for shader parameters that can be modified in real time + int emissiveIntensityLoc = shader.GetLocation("emissivePower"); + int emissiveColorLoc = shader.GetLocation("emissiveColor"); + int textureTilingLoc = shader.GetLocation("tiling"); + + // Load old car model using PBR maps and shader + // WARNING: We know this model consists of a single model.meshes[0] and + // that model.materials[0] is by default assigned to that mesh + // There could be more complex models consisting of multiple meshes and + // multiple materials defined for those meshes... but always 1 mesh = 1 material + raylib::Model car ("resources/models/old_car_new.glb"); + + // Assign already setup PBR shader to model.materials[0], used by models.meshes[0] + car.materials[0].shader = shader; + + // Setup materials[0].maps default parameters + car.materials[0].maps[MATERIAL_MAP_ALBEDO].color = WHITE; + car.materials[0].maps[MATERIAL_MAP_METALNESS].value = 0.0f; + car.materials[0].maps[MATERIAL_MAP_ROUGHNESS].value = 0.0f; + car.materials[0].maps[MATERIAL_MAP_OCCLUSION].value = 1.0f; + car.materials[0].maps[MATERIAL_MAP_EMISSION].color = (Color){ 255, 162, 0, 255 }; + + // Setup materials[0].maps default textures + car.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = LoadTexture("resources/old_car_d.png"); + car.materials[0].maps[MATERIAL_MAP_METALNESS].texture = LoadTexture("resources/old_car_mra.png"); + car.materials[0].maps[MATERIAL_MAP_NORMAL].texture = LoadTexture("resources/old_car_n.png"); + car.materials[0].maps[MATERIAL_MAP_EMISSION].texture = LoadTexture("resources/old_car_e.png"); + + // Load floor model mesh and assign material parameters + // NOTE: A basic plane shape can be generated instead of being loaded from a model file + raylib::Model floor ("resources/models/plane.glb"); + //Mesh floorMesh = GenMeshPlane(10, 10, 10, 10); + //GenMeshTangents(&floorMesh); // TODO: Review tangents generation + //Model floor = LoadModelFromMesh(floorMesh); + + // Assign material shader for our floor model, same PBR shader + floor.materials[0].shader = shader; + + floor.materials[0].maps[MATERIAL_MAP_ALBEDO].color = WHITE; + floor.materials[0].maps[MATERIAL_MAP_METALNESS].value = 0.0f; + floor.materials[0].maps[MATERIAL_MAP_ROUGHNESS].value = 0.0f; + floor.materials[0].maps[MATERIAL_MAP_OCCLUSION].value = 1.0f; + floor.materials[0].maps[MATERIAL_MAP_EMISSION].color = BLACK; + + floor.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = LoadTexture("resources/road_a.png"); + floor.materials[0].maps[MATERIAL_MAP_METALNESS].texture = LoadTexture("resources/road_mra.png"); + floor.materials[0].maps[MATERIAL_MAP_NORMAL].texture = LoadTexture("resources/road_n.png"); + + // Models texture tiling parameter can be stored in the Material struct if required (CURRENTLY NOT USED) + // NOTE: Material.params[4] are available for generic parameters storage (float) + Vector2 carTextureTiling = (Vector2){ 0.5f, 0.5f }; + Vector2 floorTextureTiling = (Vector2){ 0.5f, 0.5f }; + + // Create some lights + std::array lights = { + CreateLight(0, LightType::POINT, (Vector3) {-1.0f, 1.0f, -2.0f}, (Vector3) {0.0f, 0.0f, 0.0f}, YELLOW, 4.0f, shader), + CreateLight(1, LightType::POINT, (Vector3){ 2.0f, 1.0f, 1.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, GREEN, 3.3f, shader), + CreateLight(2, LightType::POINT, (Vector3){ -2.0f, 1.0f, 1.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, RED, 8.3f, shader), + CreateLight(3, LightType::POINT, (Vector3){ 1.0f, 1.0f, -2.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, BLUE, 2.0f, shader), + }; + + // Setup material texture maps usage in shader + // NOTE: By default, the texture maps are always used + int usage = 1; + SetShaderValue(shader, GetShaderLocation(shader, "useTexAlbedo"), &usage, SHADER_UNIFORM_INT); + SetShaderValue(shader, GetShaderLocation(shader, "useTexNormal"), &usage, SHADER_UNIFORM_INT); + SetShaderValue(shader, GetShaderLocation(shader, "useTexMRA"), &usage, SHADER_UNIFORM_INT); + SetShaderValue(shader, GetShaderLocation(shader, "useTexEmissive"), &usage, SHADER_UNIFORM_INT); + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //--------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + camera.Update(CAMERA_ORBITAL); + + // Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f }) + std::array cameraPos = {camera.position.x, camera.position.y, camera.position.z}; + SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos.data(), SHADER_UNIFORM_VEC3); + + // Check key inputs to enable/disable lights + if (IsKeyPressed(KEY_ONE)) { lights[2].enabled = !lights[2].enabled; } + if (IsKeyPressed(KEY_TWO)) { lights[1].enabled = !lights[1].enabled; } + if (IsKeyPressed(KEY_THREE)) { lights[3].enabled = !lights[3].enabled; } + if (IsKeyPressed(KEY_FOUR)) { lights[0].enabled = !lights[0].enabled; } + + // Update light values on shader (actually, only enable/disable them) + for (int i = 0; i < MAX_LIGHTS; i++) UpdateLight(shader, lights[i]); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(BLACK); + + BeginMode3D(camera); + + // Set floor model texture tiling and emissive color parameters on shader + SetShaderValue(shader, textureTilingLoc, &floorTextureTiling, SHADER_UNIFORM_VEC2); + raylib::Vector4 floorEmissiveColor = ColorNormalize(floor.materials[0].maps[MATERIAL_MAP_EMISSION].color); + SetShaderValue(shader, emissiveColorLoc, &floorEmissiveColor, SHADER_UNIFORM_VEC4); + + DrawModel(floor, (Vector3){ 0.0f, 0.0f, 0.0f }, 5.0f, WHITE); // Draw floor model + + // Set old car model texture tiling, emissive color and emissive intensity parameters on shader + SetShaderValue(shader, textureTilingLoc, &carTextureTiling, SHADER_UNIFORM_VEC2); + raylib::Vector4 carEmissiveColor = ColorNormalize(car.materials[0].maps[MATERIAL_MAP_EMISSION].color); + SetShaderValue(shader, emissiveColorLoc, &carEmissiveColor, SHADER_UNIFORM_VEC4); + float emissiveIntensity = 0.01f; + SetShaderValue(shader, emissiveIntensityLoc, &emissiveIntensity, SHADER_UNIFORM_FLOAT); + + car.Draw((Vector3){ 0.0f, 0.0f, 0.0f }, 0.005f, WHITE); // Draw car model + + // Draw spheres to show the lights positions + for (const auto& light : lights) + { + Color lightColor = (Color){ static_cast(light.color[0]*255), + static_cast(light.color[1]*255), + static_cast(light.color[2]*255), + static_cast(light.color[3]*255) }; + + if (light.enabled) DrawSphereEx(light.position, 0.2f, 8, 8, lightColor); + else DrawSphereWires(light.position, 0.2f, 8, 8, ColorAlpha(lightColor, 0.3f)); + } + + EndMode3D(); + + DrawText("Toggle lights: [1][2][3][4]", 10, 40, 20, LIGHTGRAY); + + DrawText("(c) Old Rusty Car model by Renafox (https://skfb.ly/LxRy)", screenWidth - 320, screenHeight - 20, 10, LIGHTGRAY); + + DrawFPS(10, 10); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + // Unbind (disconnect) shader from car.material[0] + // to avoid UnloadMaterial() trying to unload it automatically + car.materials[0].shader = (Shader){ 0 }; + UnloadMaterial(car.materials[0]); + car.materials[0].maps = NULL; + //UnloadModel(car); + + floor.materials[0].shader = (Shader){ 0 }; + UnloadMaterial(floor.materials[0]); + floor.materials[0].maps = NULL; + //UnloadModel(floor); + + //UnloadShader(shader); // Unload Shader + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} + +// Create light with provided data +// NOTE: It updated the global lightCount and it's limited to MAX_LIGHTS +static Light CreateLight(size_t index, LightType type, Vector3 position, Vector3 target, Color color, float intensity, Shader shader) +{ + Light light; + + light.enabled = 1; + light.type = type; + light.position = position; + light.target = target; + light.color[0] = (float)color.r/255.0f; + light.color[1] = (float)color.g/255.0f; + light.color[2] = (float)color.b/255.0f; + light.color[3] = (float)color.a/255.0f; + light.intensity = intensity; + + // NOTE: Shader parameters names for lights must match the requested ones + light.enabledLoc = GetShaderLocation(shader, TextFormat("lights[%i].enabled", index)); + light.typeLoc = GetShaderLocation(shader, TextFormat("lights[%i].type", index)); + light.positionLoc = GetShaderLocation(shader, TextFormat("lights[%i].position", index)); + light.targetLoc = GetShaderLocation(shader, TextFormat("lights[%i].target", index)); + light.colorLoc = GetShaderLocation(shader, TextFormat("lights[%i].color", index)); + light.intensityLoc = GetShaderLocation(shader, TextFormat("lights[%i].intensity", index)); + + UpdateLight(shader, light); + + return light; +} + +// Send light properties to shader +// NOTE: Light shader locations should be available +static void UpdateLight(Shader shader, const Light& light) +{ + SetShaderValue(shader, light.enabledLoc, &light.enabled, SHADER_UNIFORM_INT); + SetShaderValue(shader, light.typeLoc, &light.type, SHADER_UNIFORM_INT); + + // Send to shader light position values + std::array position = { light.position.x, light.position.y, light.position.z }; + SetShaderValue(shader, light.positionLoc, position.data(), SHADER_UNIFORM_VEC3); + + // Send to shader light target position values + std::array target = { light.target.x, light.target.y, light.target.z }; + SetShaderValue(shader, light.targetLoc, target.data(), SHADER_UNIFORM_VEC3); + SetShaderValue(shader, light.colorLoc, light.color.data(), SHADER_UNIFORM_VEC4); + SetShaderValue(shader, light.intensityLoc, &light.intensity, SHADER_UNIFORM_FLOAT); +} \ No newline at end of file diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 83a1951a..5f2f46d2 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -32,6 +32,7 @@ set(RAYLIB_CPP_HEADERS raymath.hpp Rectangle.hpp RenderTexture.hpp + ShaderUnmanaged.hpp Shader.hpp Sound.hpp Text.hpp diff --git a/include/Shader.hpp b/include/Shader.hpp index 2dfb45fe..5ad75018 100644 --- a/include/Shader.hpp +++ b/include/Shader.hpp @@ -6,26 +6,15 @@ #include "./raylib.hpp" #include "./raylib-cpp-utils.hpp" #include "Texture.hpp" +#include "ShaderUnmanaged.hpp" namespace raylib { /** * Shader type (generic) */ -class Shader : public ::Shader { +class Shader : public ShaderUnmanaged { public: - Shader(const ::Shader& shader) { - set(shader); - } - - Shader(unsigned int id, int* locs = nullptr) : ::Shader{id, locs} {} - - Shader(const std::string& vsFileName, const std::string& fsFileName) { - set(::LoadShader(vsFileName.c_str(), fsFileName.c_str())); - } - - Shader(const char* vsFileName, const char* fsFileName) { - set(::LoadShader(vsFileName, fsFileName)); - } + using ShaderUnmanaged::ShaderUnmanaged; Shader(const Shader&) = delete; @@ -36,40 +25,6 @@ class Shader : public ::Shader { other.locs = nullptr; } - /** - * Load shader from files and bind default locations. - * - * @see ::LoadShader - */ - static ::Shader Load(const std::string& vsFileName, const std::string& fsFileName) { - return ::LoadShader(vsFileName.c_str(), fsFileName.c_str()); - } - - static ::Shader Load(const char* vsFileName, const char* fsFileName) { - return ::LoadShader(vsFileName, fsFileName); - } - - /** - * Load a shader from memory. - * - * @see ::LoadShaderFromMemory - */ - static ::Shader LoadFromMemory(const std::string& vsCode, const std::string& fsCode) { - return ::LoadShaderFromMemory(vsCode.c_str(), fsCode.c_str()); - } - - static ::Shader LoadFromMemory(const char* vsCode, const char* fsCode) { - return ::LoadShaderFromMemory(vsCode, fsCode); - } - - GETTER(unsigned int, Id, id) - GETTER(int*, Locs, locs) - - Shader& operator=(const ::Shader& shader) { - set(shader); - return *this; - } - Shader& operator=(const Shader&) = delete; Shader& operator=(Shader&& other) noexcept { @@ -101,93 +56,6 @@ class Shader : public ::Shader { ::UnloadShader(*this); } } - - /** - * Begin custom shader drawing. - */ - Shader& BeginMode() { - ::BeginShaderMode(*this); - return *this; - } - - /** - * End custom shader drawing (use default shader). - */ - Shader& EndMode() { - ::EndShaderMode(); - return *this; - } - - /** - * Get shader uniform location - * - * @see GetShaderLocation() - */ - int GetLocation(const std::string& uniformName) const { - return ::GetShaderLocation(*this, uniformName.c_str()); - } - - /** - * Get shader attribute location - * - * @see GetShaderLocationAttrib() - */ - int GetLocationAttrib(const std::string& attribName) const { - return ::GetShaderLocationAttrib(*this, attribName.c_str()); - } - - /** - * Set shader uniform value - * - * @see SetShaderValue() - */ - Shader& SetValue(int uniformLoc, const void* value, int uniformType) { - ::SetShaderValue(*this, uniformLoc, value, uniformType); - return *this; - } - - /** - * Set shader uniform value vector - * - * @see SetShaderValueV() - */ - Shader& SetValue(int uniformLoc, const void* value, int uniformType, int count) { - ::SetShaderValueV(*this, uniformLoc, value, uniformType, count); - return *this; - } - - /** - * Set shader uniform value (matrix 4x4) - * - * @see SetShaderValueMatrix() - */ - Shader& SetValue(int uniformLoc, const ::Matrix& mat) { - ::SetShaderValueMatrix(*this, uniformLoc, mat); - return *this; - } - - /** - * Set shader uniform value for texture - * - * @see SetShaderValueTexture() - */ - Shader& SetValue(int uniformLoc, const ::Texture2D& texture) { - ::SetShaderValueTexture(*this, uniformLoc, texture); - return *this; - } - - /** - * Retrieves whether or not the shader is ready. - */ - bool IsReady() const { - return id != 0 && locs != nullptr; - } - - protected: - void set(const ::Shader& shader) { - id = shader.id; - locs = shader.locs; - } }; } // namespace raylib diff --git a/include/ShaderUnmanaged.hpp b/include/ShaderUnmanaged.hpp new file mode 100644 index 00000000..1c3b288e --- /dev/null +++ b/include/ShaderUnmanaged.hpp @@ -0,0 +1,156 @@ +#ifndef RAYLIB_CPP_INCLUDE_UNMANAGEDSHADER_HPP_ +#define RAYLIB_CPP_INCLUDE_UNMANAGEDSHADER_HPP_ + +#include +#include + +#include "./raylib.hpp" +#include "./raylib-cpp-utils.hpp" +#include "Texture.hpp" + +namespace raylib { + +/** + * Shader type (generic), not managed by C++ RAII. + */ +class ShaderUnmanaged : public ::Shader { + public: + ShaderUnmanaged() : ::Shader{rlGetShaderIdDefault(), rlGetShaderLocsDefault()} {} + + ShaderUnmanaged(const ::Shader& shader) { + set(shader); + } + + ShaderUnmanaged(unsigned int id, int* locs = nullptr) : ::Shader{id, locs} {} + + ShaderUnmanaged(const std::string& vsFileName, const std::string& fsFileName) { + set(::LoadShader(vsFileName.c_str(), fsFileName.c_str())); + } + ShaderUnmanaged(const char* vsFileName, const char* fsFileName) { + set(::LoadShader(vsFileName, fsFileName)); + } + + /** + * Load shader from files and bind default locations. + * + * @see ::LoadShader + */ + static ::Shader Load(const std::string& vsFileName, const std::string& fsFileName) { + return ::LoadShader(vsFileName.c_str(), fsFileName.c_str()); + } + static ::Shader Load(const char* vsFileName, const char* fsFileName) { + return ::LoadShader(vsFileName, fsFileName); + } + + /** + * Load a shader from memory. + * + * @see ::LoadShaderFromMemory + */ + static ::Shader LoadFromMemory(const std::string& vsCode, const std::string& fsCode) { + return ::LoadShaderFromMemory(vsCode.c_str(), fsCode.c_str()); + } + static ::Shader LoadFromMemory(const char* vsCode, const char* fsCode) { + return ::LoadShaderFromMemory(vsCode, fsCode); + } + + GETTER(unsigned int, Id, id) + GETTER(int*, Locs, locs) + + ShaderUnmanaged& operator=(const ::Shader& shader) { + set(shader); + return *this; + } + + /** + * Begin custom shader drawing. + */ + ShaderUnmanaged& BeginMode() { + ::BeginShaderMode(*this); + return *this; + } + + /** + * End custom shader drawing (use default shader). + */ + ShaderUnmanaged& EndMode() { + ::EndShaderMode(); + return *this; + } + + /** + * Get shader uniform location + * + * @see GetShaderLocation() + */ + int GetLocation(const std::string& uniformName) const { + return ::GetShaderLocation(*this, uniformName.c_str()); + } + + /** + * Get shader attribute location + * + * @see GetShaderLocationAttrib() + */ + int GetLocationAttrib(const std::string& attribName) const { + return ::GetShaderLocationAttrib(*this, attribName.c_str()); + } + + /** + * Set shader uniform value + * + * @see SetShaderValue() + */ + ShaderUnmanaged& SetValue(int uniformLoc, const void* value, int uniformType) { + ::SetShaderValue(*this, uniformLoc, value, uniformType); + return *this; + } + + /** + * Set shader uniform value vector + * + * @see SetShaderValueV() + */ + ShaderUnmanaged& SetValue(int uniformLoc, const void* value, int uniformType, int count) { + ::SetShaderValueV(*this, uniformLoc, value, uniformType, count); + return *this; + } + + /** + * Set shader uniform value (matrix 4x4) + * + * @see SetShaderValueMatrix() + */ + ShaderUnmanaged& SetValue(int uniformLoc, const ::Matrix& mat) { + ::SetShaderValueMatrix(*this, uniformLoc, mat); + return *this; + } + + /** + * Set shader uniform value for texture + * + * @see SetShaderValueTexture() + */ + ShaderUnmanaged& SetValue(int uniformLoc, const ::Texture2D& texture) { + ::SetShaderValueTexture(*this, uniformLoc, texture); + return *this; + } + + /** + * Retrieves whether or not the shader is ready. + */ + bool IsReady() const { + return id != 0 && locs != nullptr; + } + + protected: + void set(const ::Shader& shader) { + id = shader.id; + locs = shader.locs; + } +}; +} // namespace raylib + +using RShaderUnmanaged = raylib::ShaderUnmanaged; + +#endif // RAYLIB_CPP_INCLUDE_UNMANAGEDSHADER_HPP_