wip: pbr support

main
Dominik Madarász 2024-03-20 18:33:02 +01:00
parent c724645949
commit 920d8ecad4
23 changed files with 8292 additions and 7241 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
default
.art*.zip
__pycache__
.vs

View File

@ -761,7 +761,7 @@ if "!cc!"=="cl" (
if "!build!"=="ret" (
set args=-DENABLE_RETAIL -Dmain=WinMain !args!
set args=/nologo /Zi /MT /openmp /DNDEBUG=3 !args! /Os /Ox /O2 /Oy /GL /GF /Gw /arch:AVX2 /link /OPT:ICF /LTCG
set args=/nologo /Zi /MT /DNDEBUG=3 !args! /Os /Ox /O2 /Oy /GL /GF /Gw /arch:AVX2 /link /OPT:ICF /LTCG
)
if "!build!"=="rel" (
set args=/nologo /Zi /MT /openmp /DNDEBUG=2 !args! /Os /Ox /O2 /Oy /GL /GF /Gw /arch:AVX2 /link /OPT:ICF /LTCG
@ -793,7 +793,7 @@ if "!cc!"=="cl" (
if "!build!"=="ret" (
set args=-DENABLE_RETAIL -Dmain=WinMain !args!
set args=!warnings! /nologo /Zi /MT /openmp /DNDEBUG=3 !args! /Os /Ox /O2 /Oy /GF /Gw /arch:AVX2
set args=!warnings! /nologo /Zi /MT /DNDEBUG=3 !args! /Os /Ox /O2 /Oy /GF /Gw /arch:AVX2
)
if "!build!"=="rel" (
set args=!warnings! /nologo /Zi /MT /openmp /DNDEBUG=2 !args! /Os /Ox /O2 /Oy /GF /Gw /arch:AVX2

View File

@ -1250,6 +1250,31 @@ typedef struct mesh_t {
void mesh_render_prim(mesh_t *sm, unsigned prim);
void mesh_destroy(mesh_t *m);
aabb mesh_bounds(mesh_t *m);
enum SKYBOX_FLAGS {
SKYBOX_RAYLEIGH,
SKYBOX_CUBEMAP,
SKYBOX_PBR,
};
typedef struct skybox_t {
handle program;
mesh_t geometry;
cubemap_t cubemap;
cubemap_t env_cubemap;
int flags;
int framebuffers[6];
int textures[6];
float *pixels;
texture_t refl, env;
} skybox_t;
skybox_t skybox(const char *panorama_or_cubemap_folder, int flags);
skybox_t skybox_pbr(const char *refl_map, const char *env_map);
int skybox_render(skybox_t *sky, mat44 proj, mat44 view);
void skybox_destroy(skybox_t *sky);
void skybox_mie_calc_sh(skybox_t *sky, float sky_intensity);
void skybox_sh_reset(skybox_t *sky);
void skybox_sh_add_light(skybox_t *sky, vec3 light, vec3 dir, float strength);
int skybox_push_state(skybox_t *sky, mat44 proj, mat44 view);
int skybox_pop_state();
enum MATERIAL_ENUMS {
MAX_CHANNELS_PER_MATERIAL = 8
};
@ -1309,12 +1334,20 @@ enum MODEL_FLAGS {
MODEL_MATCAPS = 16,
MODEL_RIMLIGHT = 32,
};
enum SHADING_MODE {
SHADING_NONE,
SHADING_PHONG,
SHADING_PBR,
};
typedef struct model_t {
struct iqm_t *iqm;
int shading;
unsigned num_textures;
handle *textures;
char **texture_names;
material_t* materials;
pbr_material_t pbr_material;
texture_t sky_refl, sky_env;
texture_t lightmap;
float *lmdata;
unsigned num_meshes;
@ -1335,6 +1368,7 @@ typedef struct model_t {
unsigned billboard;
float *instanced_matrices;
unsigned num_instances;
int stored_flags;
} model_t;
enum BILLBOARD_MODE {
BILLBOARD_X = 0x1,
@ -1349,6 +1383,8 @@ enum BILLBOARD_MODE {
float model_animate_clip(model_t, float curframe, int minframe, int maxframe, bool loop);
float model_animate_blends(model_t m, anim_t *primary, anim_t *secondary, float delta);
aabb model_aabb(model_t, mat44 transform);
void model_shading(model_t*, int shading);
void model_skybox(model_t*, skybox_t sky, bool load_sh);
void model_render(model_t, mat44 proj, mat44 view, mat44 model, int shader);
void model_render_skeleton(model_t, mat44 model);
void model_render_instanced(model_t, mat44 proj, mat44 view, mat44 *models, int shader, unsigned count);
@ -1375,23 +1411,6 @@ typedef struct lightmap_t {
void lightmap_setup(lightmap_t *lm, int w, int h);
void lightmap_bake(lightmap_t *lm, int bounces, void (*drawscene)(lightmap_t *lm, model_t *m, float *view, float *proj, void *userdata), void (*progressupdate)(float progress), void *userdata);
void lightmap_destroy(lightmap_t *lm);
typedef struct skybox_t {
handle program;
mesh_t geometry;
cubemap_t cubemap;
int flags;
int framebuffers[6];
int textures[6];
float *pixels;
} skybox_t;
skybox_t skybox(const char *panorama_or_cubemap_folder, int flags);
int skybox_render(skybox_t *sky, mat44 proj, mat44 view);
void skybox_destroy(skybox_t *sky);
void skybox_mie_calc_sh(skybox_t *sky, float sky_intensity);
void skybox_sh_reset(skybox_t *sky);
void skybox_sh_add_light(skybox_t *sky, vec3 light, vec3 dir, float strength);
int skybox_push_state(skybox_t *sky, mat44 proj, mat44 view);
int skybox_pop_state();
void viewport_color(unsigned color);
void viewport_clear(bool color, bool depth);
void viewport_clip(vec2 from, vec2 to);

View File

@ -20,6 +20,8 @@ int main() {
// load model
model_t m1 = model("suzanne.obj", MODEL_NO_ANIMATIONS);
model_t m2 = model("suzanne.obj", MODEL_NO_ANIMATIONS|MODEL_MATCAPS);
model_t m3 = model("damagedhelmet.gltf", MODEL_NO_ANIMATIONS);
model_shading(&m3, SHADING_PBR);
// spawn object1 (diffuse)
object_t* obj1 = scene_spawn();
@ -37,7 +39,7 @@ int main() {
object_move(obj2, vec3(-10+5*2,0,-10));
object_pivot(obj2, vec3(0,90,0));
// spawn object2 (video)
// spawn object3 (video)
object_t* obj3 = scene_spawn();
object_model(obj3, m1);
object_diffuse(obj3, video_textures(v)[0]);
@ -45,10 +47,20 @@ int main() {
object_move(obj3, vec3(-10+5*1,0,-10));
object_pivot(obj3, vec3(0,90,0));
// spawn object4 (pbr)
object_t* obj4 = scene_spawn();
object_model(obj4, m3);
object_scale(obj4, vec3(3,3,3));
object_move(obj4, vec3(-10+6*3,0,-10));
object_pivot(obj4, vec3(0,90,0));
// create point light
light_t* l = scene_spawn_light();
light_type(l, LIGHT_POINT);
// load skybox
scene_get_active()->skybox = skybox_pbr("hdr/Tokyo_BigSight_1k.hdr","hdr/Tokyo_BigSight_Env.hdr");
while(window_swap() && !input(KEY_ESC)) {
// draw environment
ddraw_grid(0);

View File

@ -24,6 +24,9 @@ uniform bool u_rimambient = true;
in vec3 v_normal, v_normal_ws;
in vec2 v_texcoord, v_texcoord2;
in vec4 v_color;
in vec3 v_tangent;
in vec3 v_binormal;
in vec3 v_to_camera;
out vec4 fragcolor;
@ -104,6 +107,240 @@ vec3 shading_phong(light_t l) {
}
#endif
#ifdef SHADING_PBR
uniform vec2 resolution = vec2(640.0,480.0); // debug options below use this (USE_MAP_DEBUGGING, USE_AMBIENT_DEBUGGING)
#define USE_BRUTEFORCE_IRRADIANCE false // Samples irradiance from tex_skysphere when enabled.
#define USE_WRAPAROUND_SPECULAR true // Makes silhouettes more reflective to avoid black pixels.
#define USE_SPECULAR_AO_ATTENUATION true // Dampens IBL specular ambient with AO if enabled.
#define USE_NORMAL_VARIATION_TO_ROUGHNESS true // Increases roughness if normal map has variation and was minified.
#define USE_MAP_DEBUGGING false // Shows all ColorMaps as horizontal bars
#define USE_AMBIENT_DEBUGGING false // Splits the screen in two and shows image-based specular (left), full shading (middle), diffuse shading (right).
#define BOOST_LIGHTING 2.00f // Multiplies analytic light's color with this constant because otherwise they look really pathetic.
#define BOOST_SPECULAR 1.50f
#define BOOST_NOISE 2.50f
struct ColorMap
{
bool has_tex;
vec4 color;
};
uniform ColorMap map_albedo; uniform sampler2D map_albedo_tex;
uniform ColorMap map_diffuse; uniform sampler2D map_diffuse_tex;
uniform ColorMap map_specular; uniform sampler2D map_specular_tex; // not used
uniform ColorMap map_normals; uniform sampler2D map_normals_tex;
uniform ColorMap map_roughness; uniform sampler2D map_roughness_tex;
uniform ColorMap map_metallic; uniform sampler2D map_metallic_tex;
uniform ColorMap map_ao; uniform sampler2D map_ao_tex;
uniform ColorMap map_ambient; uniform sampler2D map_ambient_tex;
uniform ColorMap map_emissive; uniform sampler2D map_emissive_tex;
#define sample_colormap(ColorMap_, uv_) \
(ColorMap_.has_tex ? texture( ColorMap_##_tex, uv_ ) : ColorMap_.color)
uniform float skysphere_rotation;
uniform float skysphere_mip_count;
uniform float exposure=1;
// uniform uint frame_count;
uniform float specular_shininess;
uniform sampler2D tex_skysphere;
uniform sampler2D tex_skyenv;
uniform sampler2D tex_brdf_lut;
uniform bool has_tex_skysphere;
uniform bool has_tex_skyenv;
const float PI = 3.1415926536;
// MurMurHash 3 finalizer. Implementation is in public domain.
uint hash( uint h )
{
h ^= h >> 16;
h *= 0x85ebca6bU;
h ^= h >> 13;
h *= 0xc2b2ae35U;
h ^= h >> 16;
return h;
}
// Random function using the idea of StackOverflow user "Spatial" https://stackoverflow.com/a/17479300
// Creates random 23 bits and puts them into the fraction bits of an 32-bit float.
float random( uvec3 h )
{
uint m = hash(h.x ^ hash( h.y ) ^ hash( h.z ));
return uintBitsToFloat( ( m & 0x007FFFFFu ) | 0x3f800000u ) - 1.;
}
float random( vec3 v )
{
return random(floatBitsToUint( v ));
}
vec3 fresnel_schlick( vec3 H, vec3 V, vec3 F0 )
{
float cosTheta = clamp( dot( H, V ), 0., 1. );
return F0 + ( vec3( 1.0 ) - F0 ) * pow( 1. - cosTheta, 5.0 );
}
// A Fresnel term that dampens rough specular reflections.
// https://seblagarde.wordpress.com/2011/08/17/hello-world/
vec3 fresnel_schlick_roughness( vec3 H, vec3 V, vec3 F0, float roughness )
{
float cosTheta = clamp( dot( H, V ), 0., 1. );
return F0 + ( max( vec3( 1.0 - roughness ), F0 ) - F0 ) * pow( 1. - cosTheta, 5.0 );
}
float distribution_ggx( vec3 N, vec3 H, float roughness )
{
float a = roughness * roughness;
float a2 = a * a;
float NdotH = max( 0., dot( N, H ) );
float factor = NdotH * NdotH * ( a2 - 1. ) + 1.;
return a2 / ( PI * factor * factor );
}
float geometry_schlick_ggx( vec3 N, vec3 V, float k )
{
float NdotV = max( 0., dot( N, V ) );
return NdotV / (NdotV * ( 1. - k ) + k );
}
float geometry_smith( vec3 N, vec3 V, vec3 L, float roughness )
{
#if 1 // original
float r = roughness + 1.;
float k = (r * r) / 8.;
#elif 0 // vries
float a = roughness;
float k = (a * a) / 2.0;
#elif 0 // vries improved?
float a = roughness * roughness;
float k = a / 2.0;
#endif
return geometry_schlick_ggx( N, V, k ) * geometry_schlick_ggx( N, L, k );
}
vec2 sphere_to_polar( vec3 normal )
{
normal = normalize( normal );
return vec2( ( atan( normal.z, normal.x ) + skysphere_rotation ) / PI / 2.0 + 0.5, acos( normal.y ) / PI );
}
// Our vertically GL_CLAMPed textures seem to blend towards black when sampling the half-pixel edge.
// Not sure if it has a border, or this if is a driver bug, but can repro on multiple nvidia cards.
// Knowing the texture height we can limit sampling to the centers of the top and bottom pixel rows.
vec2 sphere_to_polar_clamp_y( vec3 normal, float texture_height )
{
normal = normalize( normal );
return vec2( ( atan( normal.z, normal.x ) + skysphere_rotation ) / PI / 2.0 + 0.5, clamp(acos( normal.y ) / PI, 0.5 / texture_height, 1.0 - 0.5 / texture_height) );
}
vec3 sample_sky( vec3 normal )
{
vec2 polar = sphere_to_polar( normal );
return texture( tex_skysphere, polar ).rgb * exposure;
}
// Takes samples around the hemisphere, converts them to radiances via weighting and
// returns a normalized sum.
vec3 sample_irradiance_slow( vec3 normal, vec3 vertex_tangent )
{
float delta = 0.10;
vec3 up = abs( normal.y ) < 0.999 ? vec3( 0., 1., 0. ) : vec3( 0., 0., 1. );
vec3 tangent_x = normalize( cross( up, normal ) );
vec3 tangent_y = cross( normal, tangent_x );
int numIrradianceSamples = 0;
vec3 irradiance = vec3(0.);
for ( float phi = 0.; phi < 2. * PI ; phi += delta )
{
for ( float theta = 0.; theta < 0.5 * PI; theta += delta )
{
vec3 tangent_space = vec3(
sin( theta ) * cos( phi ),
sin( theta ) * sin( phi ),
cos( theta ) );
vec3 world_space = tangent_space.x * tangent_x + tangent_space.y + tangent_y + tangent_space.z * normal;
vec3 color = sample_sky( world_space );
irradiance += color * cos( theta ) * sin( theta );
numIrradianceSamples++;
}
}
irradiance = PI * irradiance / float( numIrradianceSamples );
return irradiance;
}
vec3 sample_irradiance_fast( vec3 normal, vec3 vertex_tangent )
{
// Sample the irradiance map if it exists, otherwise fall back to blurred reflection map.
if ( has_tex_skyenv )
{
vec2 polar = sphere_to_polar_clamp_y( normal, 180.0 );
return textureLod( tex_skyenv, polar, 0.0 ).rgb * exposure;
}
else
{
vec2 polar = sphere_to_polar( normal );
return textureLod( tex_skysphere, polar, 0.80 * skysphere_mip_count ).rgb * exposure;
}
}
vec3 specular_ibl( vec3 V, vec3 N, float roughness, vec3 fresnel )
{
// What we'd like to do here is take a LOT of skybox samples around the reflection
// vector R according to the BRDF lobe.
//
// Unfortunately it's not possible in real time so we use the following UE4 style approximations:
// 1. Integrate incoming light and BRDF separately ("split sum approximation")
// 2. Assume V = R = N so that we can just blur the skybox and sample that.
// 3. Bake the BRDF integral into a lookup texture so that it can be computed in constant time.
//
// Here we also simplify approximation #2 by using bilinear mipmaps with a magic formula instead
// of properly convolving it with a GGX lobe.
//
// For details, see Brian Karis, "Real Shading in Unreal Engine 4", 2013.
vec3 R = 2. * dot( V, N ) * N - V;
vec2 polar = sphere_to_polar( R );
// Map roughness from range [0, 1] into a mip LOD [0, skysphere_mip_count].
// The magic numbers were chosen empirically.
float mip = 0.9 * skysphere_mip_count * pow(roughness, 0.25 * BOOST_SPECULAR);
vec3 prefiltered = textureLod( tex_skysphere, polar, mip ).rgb * exposure;
float NdotV = dot( N, V );
// dot( N, V ) seems to produce negative values so we can try to stretch it a bit behind the silhouette
// to avoid black pixels.
if (USE_WRAPAROUND_SPECULAR)
{
NdotV = NdotV * 0.9 + 0.1;
}
NdotV = min(0.99, max(0.01, NdotV));
// A precomputed lookup table contains a scale and a bias term for specular intensity (called "fresnel" here).
// See equation (8) in Karis' course notes mentioned above.
vec2 envBRDF = texture( tex_brdf_lut, vec2(NdotV, 1.0-roughness) ).xy; // (NdotV,1-roughtness) for green top-left (NdotV,roughness) for green bottom-left
vec3 specular = prefiltered * (fresnel * envBRDF.x + vec3(envBRDF.y));
return specular;
}
#endif
vec3 lighting() {
vec3 lit = vec3(0,0,0);
#ifndef SHADING_NONE
@ -111,6 +348,8 @@ vec3 lighting() {
#ifdef SHADING_PHONG
lit += shading_phong(u_lights[i]);
#endif
#ifdef SHADING_PBR
#endif
}
#endif
return lit;
@ -151,7 +390,9 @@ void main() {
fragcolor = vec4(diffuse.rgb*u_litboost, 1.0);
}
#else
#endif
#ifdef SHADING_PHONG
void main() {
vec3 n = normalize(v_normal_ws);
@ -162,7 +403,7 @@ void main() {
if( (result.x*result.x+result.y*result.y+result.z*result.z) > 0.0 ) lit = vec4(result, 1.0);
}
// analytical lights (phong shading)
// analytical lights
lit += vec4(lighting(), 0.0);
// base
@ -207,3 +448,286 @@ void main() {
#endif
}
#endif
#ifdef SHADING_PBR
void main(void)
{
vec3 baseColor = vec3( 0.5, 0.5, 0.5 );
float roughness = 1.0;
float metallic = 0.0;
float ao = 1.0;
float alpha = 1.0;
vec4 baseColor_alpha;
if ( map_albedo.has_tex )
baseColor_alpha = sample_colormap( map_albedo, v_texcoord );
else
baseColor_alpha = sample_colormap( map_diffuse, v_texcoord );
baseColor = baseColor_alpha.xyz;
alpha = baseColor_alpha.w;
if( map_metallic.has_tex && map_roughness.has_tex ) {
metallic = sample_colormap( map_metallic, v_texcoord ).x;
roughness = sample_colormap( map_roughness, v_texcoord ).x;
}
else if( map_roughness.has_tex ) {
//< @r-lyeh, metalness B, roughness G, (@todo: self-shadowing occlusion R; for now, any of R/B are metallic)
metallic = sample_colormap( map_roughness, v_texcoord ).b + sample_colormap( map_roughness, v_texcoord ).r;
roughness = sample_colormap( map_roughness, v_texcoord ).g;
}
if ( map_ao.has_tex )
ao = sample_colormap( map_ao, v_texcoord ).x;
else if ( map_ambient.has_tex )
ao = sample_colormap( map_ambient, v_texcoord ).x;
vec3 emissive = sample_colormap( map_emissive, v_texcoord ).rgb;
vec3 normalmap = texture( map_normals_tex, v_texcoord ).xyz * vec3(2.0) - vec3(1.0);
float normalmap_mip = textureQueryLod( map_normals_tex, v_texcoord ).x;
float normalmap_length = length(normalmap);
normalmap /= normalmap_length;
vec3 normal = v_normal_ws;
if ( map_normals.has_tex )
{
// Mikkelsen's tangent space normal map decoding. See http://mikktspace.com/ for rationale.
vec3 bi = cross( v_normal_ws, v_tangent );
vec3 nmap = normalmap.xyz;
normal = nmap.x * v_tangent + nmap.y * bi + nmap.z * v_normal_ws;
}
normal = normalize( normal );
if( USE_MAP_DEBUGGING && !USE_AMBIENT_DEBUGGING )
{
vec3 c = vec3(1., 0., 0.);
float x = gl_FragCoord.x / resolution.x;
float y = gl_FragCoord.y / resolution.y;
if ( y < (7.0/7.0) ) c = vec3(.5) + .5*v_normal_ws;
if ( y < (6.0/7.0) ) c = vec3(.5) + .5*normalmap;
if ( y < (5.0/7.0) ) c = vec3(ao);
if ( y < (4.0/7.0) ) c = vec3(emissive);
if ( y < (3.0/7.0) ) c = vec3(metallic);
if ( y < (2.0/7.0) ) c = vec3(roughness);
if ( y < (1.0/7.0) ) c = baseColor;
fragcolor = vec4(c, 1.);
return;
}
if (USE_NORMAL_VARIATION_TO_ROUGHNESS)
{
// Try to reduce specular aliasing by increasing roughness when minified normal maps have high variation.
float variation = 1. - pow( normalmap_length, 8. );
float minification = clamp( normalmap_mip - 2., 0., 1. );
roughness = mix( roughness, 1.0, variation * minification );
}
fragcolor = baseColor_alpha;
vec3 N = normal;
vec3 V = normalize( v_to_camera );
vec3 Lo = vec3(0.);
vec3 F0 = vec3(0.04);
F0 = mix( F0, baseColor, metallic );
bool use_ibl = has_tex_skysphere;
// Add contributions from analytic lights.
{
for ( int i = 0; i < u_num_lights; i++ )
{
light_t l = u_lights[i];
vec3 lightDir;
float attenuation = 1.0;
if (l.type == LIGHT_DIRECTIONAL) {
lightDir = normalize(-l.dir);
} else if (l.type == LIGHT_POINT || l.type == LIGHT_SPOT) {
vec3 toLight = l.pos - v_position_ws;
lightDir = normalize(toLight);
float distance = length(toLight);
attenuation = 1.0 / (l.constant + l.linear * distance + l.quadratic * (distance * distance));
if (l.type == LIGHT_SPOT) {
float angle = dot(l.dir, -lightDir);
if (angle > l.outerCone) {
float intensity = (angle-l.outerCone)/(l.innerCone-l.outerCone);
attenuation *= clamp(intensity, 0.0, 1.0);
} else {
attenuation = 0.0;
}
}
}
// fast-rejection for faraway vertices
if (attenuation <= 0.01) {
continue;
}
// vec3 n = normalize(v_normal_ws);
// float diffuse = max(dot(n, lightDir), 0.0);
// vec3 halfVec = normalize(lightDir + u_cam_dir);
// float specular = pow(max(dot(n, halfVec), 0.0), l.power);
// return (attenuation*l.ambient + diffuse*attenuation*l.diffuse + specular*attenuation*l.specular);
vec3 radiance = l.diffuse;
vec3 L = normalize( lightDir );
vec3 H = normalize( u_cam_dir + L );
vec3 F = fresnel_schlick( H, u_cam_dir, F0 );
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= 1.0 - metallic;
// Premultiplied alpha applied to the diffuse component only
kD *= alpha;
float D = distribution_ggx( N, H, roughness );
float G = geometry_smith( N, u_cam_dir, L, roughness );
vec3 num = D * F * G;
float denom = 4. * max( 0., dot( N, u_cam_dir ) ) * max( 0., dot( N, L ) );
vec3 specular = kS * (num / max( 0.001, denom ));
float NdotL = max( 0., dot( N, L ) );
Lo += ( kD * ( baseColor / PI ) + specular ) * radiance * NdotL * attenuation;
}
}
vec3 ambient = sample_colormap( map_ambient, v_texcoord ).xyz;
vec3 diffuse_ambient;
vec3 specular_ambient;
if ( use_ibl )
{
// Image based lighting.
// Based on https://learnopengl.com/PBR/IBL/Diffuse-irradiance
vec3 irradiance = vec3(0.);
if ( USE_BRUTEFORCE_IRRADIANCE )
{
irradiance = sample_irradiance_slow( normal, v_tangent );
}
else
{
irradiance = sample_irradiance_fast( normal, v_tangent );
}
// Compute the Fresnel term for a perfect mirror reflection with L = R.
// In this case the halfway vector H = N.
//
// We use a modified Fresnel function that dampens specular reflections of very
// rough surfaces to avoid too bright pixels at grazing angles.
vec3 F = fresnel_schlick_roughness( N, V, F0, roughness );
vec3 kS = F;
// Subtract the amount of reflected light (specular) to get the energy left for
// absorbed (diffuse) light.
vec3 kD = vec3(1.) - kS;
// Metallic surfaces have only a specular reflection.
kD *= 1.0 - metallic;
// Premultiplied alpha applied to the diffuse component only
kD *= alpha;
// Modulate the incoming lighting with the diffuse color: some wavelengths get absorbed.
diffuse_ambient = irradiance * baseColor;
// Ambient light also has a specular part.
specular_ambient = specular_ibl( V, normal, roughness, F );
// Ambient occlusion tells us the fraction of sky light that reaches this point.
if (USE_SPECULAR_AO_ATTENUATION)
{
ambient = ao * (kD * diffuse_ambient + specular_ambient);
}
else
{
// We don't attenuate specular_ambient ambient here with AO which might cause flickering in dark cavities.
ambient = ao * (kD * diffuse_ambient) + specular_ambient;
}
}
vec3 color = (ambient + Lo) + emissive;
if ( USE_AMBIENT_DEBUGGING )
{
float y = gl_FragCoord.y / resolution.y;
if( USE_MAP_DEBUGGING && y > 0.5 )
{
if ( (y-0.5) < (7.0/7.0/2.0) ) color = vec3(.5) + .5*v_normal_ws;
if ( (y-0.5) < (6.0/7.0/2.0) ) color = vec3(.5) + .5*normalmap;
if ( (y-0.5) < (5.0/7.0/2.0) ) color = vec3(ao);
if ( (y-0.5) < (4.0/7.0/2.0) ) color = vec3(emissive);
if ( (y-0.5) < (3.0/7.0/2.0) ) color = vec3(metallic);
if ( (y-0.5) < (2.0/7.0/2.0) ) color = vec3(roughness);
if ( (y-0.5) < (1.0/7.0/2.0) ) color = baseColor;
} else {
float x = gl_FragCoord.x / resolution.x;
if ( x < 0.33 )
color = specular_ambient;
else if( x > 0.66 )
color = diffuse_ambient;
}
}
// dither with noise.
// float dither = random( uvec3( floatBitsToUint( gl_FragCoord.xy ), frame_count ) );
// color += BOOST_NOISE * vec3( (-1.0/256.) + (2./256.) * dither );
#if 0 // original
// basic tonemap and gamma correction
color = color / ( vec3(1.) + color );
color = pow( color, vec3(1. / 2.2) );
#elif 0
// filmic tonemapper
vec3 linearColor = color;
vec3 x = max(vec3(0.0), linearColor - 0.004);
color = (x * (6.2 * x + 0.5)) / (x * (6.2 * x + 1.7) + 0.06);
// gamma correction
// color = pow( color, vec3(1. / 2.2) );
#elif 1
// aces film (CC0, src: https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/)
vec3 x = color;
float a = 2.51f;
float b = 0.03f;
float c = 2.43f;
float d = 0.59f;
float e = 0.14f;
color = clamp((x*(a*x+b))/(x*(c*x+d)+e), 0.0, 1.0);
// gamma correction
color = pow( color, vec3(1. / 2.2) );
#endif
// Technically this alpha may be too transparent, if there is a lot of reflected light we wouldn't
// see the background, maybe we can approximate it well enough by adding a fresnel term
fragcolor = vec4( color * shadowing().xyz, alpha );
// rimlight
#ifdef RIM
{
vec3 n = normalize(mat3(M) * v_normal_ws); // convert normal to view space
vec3 p = (M * vec4(v_position,1.0)).xyz; // convert position to view space
vec3 v = vec3(0,-1,0);
if (!u_rimambient) {
v = normalize(u_rimpivot-p);
}
float rim = 1.0 - max(dot(v,n), 0.0);
vec3 col = u_rimcolor*(pow(smoothstep(1.0-u_rimrange.x,u_rimrange.y,rim), u_rimrange.z));
fragcolor += vec4(col, 1.0);}
#endif
}
#endif

View File

@ -62,12 +62,16 @@ out vec4 v_color;
out vec3 v_position, v_position_ws;
out vec3 v_normal, v_normal_ws;
out vec2 v_texcoord, v_texcoord2;
out vec3 v_tangent;
out vec3 v_binormal;
out vec3 v_viewpos;
out vec3 v_to_camera;
// shadow
uniform mat4 model, view;
uniform mat4 model, view, inv_view;
uniform mat4 cameraToShadowProjector;
out vec4 vneye;
out vec4 vpeye;
@ -153,6 +157,24 @@ void main() {
modelView = view * l_model;
}
v_position_ws = (l_model * vec4( objPos, 1.0 )).xyz;
gl_Position = P * modelView * vec4( objPos, 1.0 );
v_tangent = normalize(mat3(att_instanced_matrix) * att_tangent.xyz);
#if 0
// compute tangent T and bitangent B
vec3 Q1 = dFdx(att_position);
vec3 Q2 = dFdy(att_position);
vec2 st1 = dFdx(att_texcoord);
vec2 st2 = dFdy(att_texcoord);
vec3 T = normalize(Q1*st2.t - Q2*st1.t);
vec3 B = normalize(-Q1*st2.s + Q2*st1.s);
vec3 binormal = B;
#else
vec3 binormal = cross(att_normal, att_tangent.xyz) * att_tangent.w;
#endif
v_binormal = normalize(mat3(att_instanced_matrix) * binormal);
vec4 finalPos = modelView * vec4( objPos, 1.0 );
vec3 to_camera = normalize( -finalPos.xyz );
v_to_camera = mat3( inv_view ) * to_camera;
gl_Position = P * finalPos;
do_shadow();
}

View File

@ -17445,6 +17445,42 @@ API void mesh_render_prim(mesh_t *sm, unsigned prim);
API void mesh_destroy(mesh_t *m);
API aabb mesh_bounds(mesh_t *m);
// -----------------------------------------------------------------------------
// skyboxes
enum SKYBOX_FLAGS {
SKYBOX_RAYLEIGH,
SKYBOX_CUBEMAP,
SKYBOX_PBR,
};
typedef struct skybox_t {
handle program;
mesh_t geometry;
cubemap_t cubemap;
cubemap_t env_cubemap;
int flags;
// mie
int framebuffers[6];
int textures[6];
float *pixels;
// pbr
texture_t refl, env;
} skybox_t;
API skybox_t skybox(const char *panorama_or_cubemap_folder, int flags);
API skybox_t skybox_pbr(const char *refl_map, const char *env_map);
API int skybox_render(skybox_t *sky, mat44 proj, mat44 view);
API void skybox_destroy(skybox_t *sky);
API void skybox_mie_calc_sh(skybox_t *sky, float sky_intensity);
API void skybox_sh_reset(skybox_t *sky);
API void skybox_sh_add_light(skybox_t *sky, vec3 light, vec3 dir, float strength);
API int skybox_push_state(skybox_t *sky, mat44 proj, mat44 view); // @to deprecate
API int skybox_pop_state(); // @to deprecate
// -----------------------------------------------------------------------------
// materials
@ -17529,22 +17565,24 @@ enum MODEL_FLAGS {
MODEL_RIMLIGHT = 32,
};
//@todo: make this data-driven
// enum SHADING_MODE {
// SHADING_NONE,
// SHADING_PHONG,
// SHADING_CARTOON,
// // SHADING_PBR,
// };
enum SHADING_MODE {
SHADING_NONE,
SHADING_PHONG,
SHADING_PBR,
};
typedef struct model_t {
struct iqm_t *iqm; // private
int shading; // based on SHADING_MODE
unsigned num_textures;
handle *textures;
char **texture_names;
array(material_t) materials;
pbr_material_t pbr_material;
texture_t sky_refl, sky_env;
texture_t lightmap;
float *lmdata;
@ -17569,6 +17607,8 @@ typedef struct model_t {
float *instanced_matrices;
unsigned num_instances;
int stored_flags;
} model_t;
enum BILLBOARD_MODE {
@ -17586,6 +17626,8 @@ API float model_animate(model_t, float curframe);
API float model_animate_clip(model_t, float curframe, int minframe, int maxframe, bool loop);
API float model_animate_blends(model_t m, anim_t *primary, anim_t *secondary, float delta);
API aabb model_aabb(model_t, mat44 transform);
API void model_shading(model_t*, int shading);
API void model_skybox(model_t*, skybox_t sky, bool load_sh);
API void model_render(model_t, mat44 proj, mat44 view, mat44 model, int shader);
API void model_render_skeleton(model_t, mat44 model);
API void model_render_instanced(model_t, mat44 proj, mat44 view, mat44 *models, int shader, unsigned count);
@ -17625,31 +17667,6 @@ API void lightmap_setup(lightmap_t *lm, int w, int h);
API void lightmap_bake(lightmap_t *lm, int bounces, void (*drawscene)(lightmap_t *lm, model_t *m, float *view, float *proj, void *userdata), void (*progressupdate)(float progress), void *userdata);
API void lightmap_destroy(lightmap_t *lm);
// -----------------------------------------------------------------------------
// skyboxes
typedef struct skybox_t {
handle program;
mesh_t geometry;
cubemap_t cubemap;
int flags;
// mie
int framebuffers[6];
int textures[6];
float *pixels;
} skybox_t;
API skybox_t skybox(const char *panorama_or_cubemap_folder, int flags);
API int skybox_render(skybox_t *sky, mat44 proj, mat44 view);
API void skybox_destroy(skybox_t *sky);
API void skybox_mie_calc_sh(skybox_t *sky, float sky_intensity);
API void skybox_sh_reset(skybox_t *sky);
API void skybox_sh_add_light(skybox_t *sky, vec3 light, vec3 dir, float strength);
API int skybox_push_state(skybox_t *sky, mat44 proj, mat44 view); // @to deprecate
API int skybox_pop_state(); // @to deprecate
// -----------------------------------------------------------------------------
// post-fxs
@ -370091,7 +370108,7 @@ GLuint shader_compile( GLenum type, const char *source ) {
// dump log with line numbers
shader_print( source );
PRINTF("!ERROR: shader_compile(): %s\n%s\n", type == GL_VERTEX_SHADER ? "Vertex" : "Fragment", buf);
PANIC("!ERROR: shader_compile(): %s\n%s\n", type == GL_VERTEX_SHADER ? "Vertex" : "Fragment", buf);
return 0;
}
@ -370112,7 +370129,7 @@ unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char
}
}
const char *glsl_version = ifdef(ems, "300 es", "150");
const char *glsl_version = ifdef(ems, "300 es", "400");
if(gs)
gs = gs && gs[0] == '#' && gs[1] == 'v' ? gs : va("#version %s\n%s\n%s", glsl_version, glsl_defines, gs ? gs : "");
@ -370680,7 +370697,6 @@ static
int allocate_texture_unit() {
static int textureUnit = 0, totalTextureUnits = 0;
do_once glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &totalTextureUnits);
ASSERT(textureUnit < totalTextureUnits, "%d texture units exceeded", totalTextureUnits);
return textureUnit++;
}
@ -371621,7 +371637,7 @@ skybox_t skybox(const char *asset, int flags) {
mesh_update(&sky.geometry, "p3", 0,countof(vertices),vertices, countof(indices),indices, MESH_TRIANGLE_STRIP);
// sky program
sky.flags = flags ? flags : !!asset; // either cubemap or rayleigh
sky.flags = flags && flags != SKYBOX_PBR ? flags : !!asset ? SKYBOX_CUBEMAP : SKYBOX_RAYLEIGH; // either cubemap or rayleigh
sky.program = shader(vfs_read("shaders/vs_3_3_skybox.glsl"),
sky.flags ? vfs_read("fs_3_4_skybox.glsl") : vfs_read("shaders/fs_3_4_skybox_rayleigh.glsl"),
"att_position", "fragcolor", NULL);
@ -371664,6 +371680,77 @@ skybox_t skybox(const char *asset, int flags) {
return sky;
}
static inline
texture_t load_env_tex( const char *pathfile, unsigned flags ) {
int flags_hdr = strendi(pathfile, ".hdr") ? TEXTURE_FLOAT | TEXTURE_RGBA : 0;
texture_t t = texture(pathfile, flags | TEXTURE_LINEAR | TEXTURE_MIPMAPS | TEXTURE_REPEAT | flags_hdr);
glBindTexture( GL_TEXTURE_2D, t.id );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
return t;
}
skybox_t skybox_pbr(const char *refl_map, const char *env_map) {
skybox_t sky = {0};
// sky mesh
vec3 vertices[] = {{+1,-1,+1},{+1,+1,+1},{+1,+1,-1},{-1,+1,-1},{+1,-1,-1},{-1,-1,-1},{-1,-1,+1},{-1,+1,+1}};
unsigned indices[] = { 0, 1, 2, 3, 4, 5, 6, 3, 7, 1, 6, 0, 4, 2 };
mesh_update(&sky.geometry, "p3", 0,countof(vertices),vertices, countof(indices),indices, MESH_TRIANGLE_STRIP);
// sky program
sky.flags = SKYBOX_PBR;
sky.program = shader(vfs_read("shaders/vs_3_3_skybox.glsl"),
sky.flags ? vfs_read("fs_3_4_skybox.glsl") : vfs_read("shaders/fs_3_4_skybox_rayleigh.glsl"),
"att_position", "fragcolor", NULL);
// sky cubemap & SH
if( refl_map ) {
int is_panorama = vfs_size( refl_map );
if( is_panorama ) { // is file
stbi_hdr_to_ldr_gamma(1.2f);
image_t panorama = image( refl_map, IMAGE_RGBA );
sky.cubemap = cubemap( panorama, 0 ); // RGBA required
image_destroy(&panorama);
} else { // is folder
image_t images[6] = {0};
images[0] = image( va("%s/posx", refl_map), IMAGE_RGB ); // cubepx
images[1] = image( va("%s/negx", refl_map), IMAGE_RGB ); // cubenx
images[2] = image( va("%s/posy", refl_map), IMAGE_RGB ); // cubepy
images[3] = image( va("%s/negy", refl_map), IMAGE_RGB ); // cubeny
images[4] = image( va("%s/posz", refl_map), IMAGE_RGB ); // cubepz
images[5] = image( va("%s/negz", refl_map), IMAGE_RGB ); // cubenz
sky.cubemap = cubemap6( images, 0 );
for( int i = 0; i < countof(images); ++i ) image_destroy(&images[i]);
}
sky.refl = load_env_tex(refl_map, TEXTURE_SRGB);
}
if( env_map ) {
int is_panorama = vfs_size( env_map );
if( is_panorama ) { // is file
stbi_hdr_to_ldr_gamma(1.2f);
image_t panorama = image( env_map, IMAGE_RGBA );
sky.env_cubemap = cubemap( panorama, 0 ); // RGBA required
image_destroy(&panorama);
} else { // is folder
image_t images[6] = {0};
images[0] = image( va("%s/posx", env_map), IMAGE_RGB ); // cubepx
images[1] = image( va("%s/negx", env_map), IMAGE_RGB ); // cubenx
images[2] = image( va("%s/posy", env_map), IMAGE_RGB ); // cubepy
images[3] = image( va("%s/negy", env_map), IMAGE_RGB ); // cubeny
images[4] = image( va("%s/posz", env_map), IMAGE_RGB ); // cubepz
images[5] = image( va("%s/negz", env_map), IMAGE_RGB ); // cubenz
sky.env_cubemap = cubemap6( images, 0 );
for( int i = 0; i < countof(images); ++i ) image_destroy(&images[i]);
}
sky.env = load_env_tex(env_map, TEXTURE_SRGB);
}
return sky;
}
void skybox_mie_calc_sh(skybox_t *sky, float sky_intensity) {
unsigned WIDTH = 1024, HEIGHT = 1024;
int last_fb;
@ -372917,6 +373004,11 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view,
if ((loc = glGetUniformLocation(shader, "view")) >= 0) {
glUniformMatrix4fv(loc, 1, GL_FALSE, view);
}
if ((loc = glGetUniformLocation(shader, "inv_view")) >= 0) {
mat44 inv_view;
invert44( inv_view, view);
glUniformMatrix4fv(loc, 1, GL_FALSE, inv_view);
}
if ((loc = glGetUniformLocation(shader, "P")) >= 0) {
glUniformMatrix4fv(loc, 1, GL_FALSE, proj);
}
@ -372931,6 +373023,36 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view,
if ((loc = glGetUniformLocation(shader, "u_matcaps")) >= 0) {
glUniform1i(loc, m.flags & MODEL_MATCAPS ? GL_TRUE:GL_FALSE);
}
if (m.shading == SHADING_PBR) {
const pbr_material_t *material = &m.pbr_material;
shader_colormap( "map_diffuse", material->diffuse );
shader_colormap( "map_normals", material->normals );
shader_colormap( "map_specular", material->specular );
shader_colormap( "map_albedo", material->albedo );
shader_colormap( "map_roughness", material->roughness );
shader_colormap( "map_metallic", material->metallic );
shader_colormap( "map_ao", material->ao );
shader_colormap( "map_ambient", material->ambient );
shader_colormap( "map_emissive", material->emissive );
shader_float( "specular_shininess", material->specular_shininess ); // unused, basic_specgloss.fs only
shader_vec2( "resolution", vec2(window_width(),window_height()));
bool has_tex_skysphere = m.sky_refl.id != texture_checker().id;
bool has_tex_skyenv = m.sky_env.id != texture_checker().id;
shader_bool( "has_tex_skysphere", has_tex_skysphere );
shader_bool( "has_tex_skyenv", has_tex_skyenv );
if( has_tex_skysphere ) {
float mipCount = floor( log2( m.sky_refl.h ) );
shader_texture("tex_skysphere", m.sky_refl);
shader_float( "skysphere_mip_count", mipCount );
}
if( has_tex_skyenv ) {
shader_texture( "tex_skyenv", m.sky_env );
}
shader_texture( "tex_brdf_lut", brdf_lut() );
}
}
static
void model_set_state(model_t m) {
@ -373368,6 +373490,9 @@ bool model_load_textures(iqm_t *q, const struct iqmheader *hdr, model_t *model,
model_t model_from_mem(const void *mem, int len, int flags) {
model_t m = {0};
m.stored_flags = flags;
m.shading = SHADING_PHONG;
const char *ptr = (const char *)mem;
// can't cache shader programs since we enable features via flags here
// static int shaderprog = -1;
@ -373648,6 +373773,7 @@ void model_draw_call(model_t m, int shader) {
for(int i = 0; i < q->nummeshes; i++) {
struct iqmmesh *im = &q->meshes[i];
if (m.shading != SHADING_PBR) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, q->textures[i] );
glUniform1i(glGetUniformLocation(shader, "u_texture2d"), 0 );
@ -373664,6 +373790,7 @@ void model_draw_call(model_t m, int shader) {
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m.lightmap.id);
glUniform1i(glGetUniformLocation(shader, "u_lightmap"), 1 );
}
glDrawElementsInstanced(GL_TRIANGLES, 3*im->num_triangles, GL_UNSIGNED_INT, &tris[im->first_triangle], m.num_instances);
profile_incstat("Render.num_drawcalls", +1);
@ -373693,6 +373820,33 @@ void model_render(model_t m, mat44 proj, mat44 view, mat44 model, int shader) {
model_render_instanced(m, proj, view, (mat44*)model, shader, 1);
}
void model_shading(model_t *m, int shading) {
m->shading = shading;
int flags = m->stored_flags;
// load pbr material if SHADING_PBR was selected
if (shading == SHADING_PBR && array_count(m->materials) > 0) {
pbr_material(&m->pbr_material, m->materials[0].name);
}
// rebind shader
// @fixme: destroy old shader program
const char *symbols[] = { "{{include-shadowmap}}", vfs_read("shaders/fs_0_0_shadowmap_lit.glsl") }; // #define RIM
int shaderprog = shader(strlerp(1,symbols,vfs_read("shaders/vs_323444143_16_3322_model.glsl")), strlerp(1,symbols,vfs_read("shaders/fs_32_4_model.glsl")), //fs,
"att_position,att_texcoord,att_normal,att_tangent,att_instanced_matrix,,,,att_indexes,att_weights,att_vertexindex,att_color,att_bitangent,att_texcoord2","fragColor",
va("%s,%s", shading == SHADING_PBR ? "SHADING_PBR" : "SHADING_PHONG", (flags&MODEL_RIMLIGHT)?"RIM":""));
m->program = shaderprog;
}
void model_skybox(model_t *mdl, skybox_t sky, bool load_sh) {
if (load_sh) {
shader_vec3v("u_coefficients_sh", 9, sky.cubemap.sh);
}
mdl->sky_refl = sky.refl;
mdl->sky_env = sky.env;
}
// static
aabb aabb_transform( aabb A, mat44 M ) {
// Based on "Transforming Axis-Aligned Bounding Boxes" by Jim Arvo, 1990
@ -375393,13 +375547,16 @@ void scene_render(int flags) {
shader_vec3v("u_coefficients_sh", 9, last_scene->skybox.cubemap.sh);
}
model_skybox(model, last_scene->skybox, 0);
if (anim) {
float delta = window_delta() * obj->anim_speed;
model->curframe = model_animate_clip(*model, model->curframe + delta, anim->from, anim->to, anim->flags & ANIM_LOOP );
}
model->billboard = obj->billboard;
model_render(*model, cam->proj, cam->view, obj->transform, 0);
model_render(*model, cam->proj, cam->view, obj->transform, model->program);
if( do_retexturing ) {
for(int i = 0; i < model->iqm->nummeshes; ++i) {
@ -378862,9 +379019,9 @@ static void mpeg_video_callback( plm_t* plm, plm_frame_t* frame, void* user ) {
if(v->paused) return;
if(v->has_ycbcr) {
mpeg_update_texture(GL_TEXTURE0, v->textureY.id, &frame->y);
mpeg_update_texture(GL_TEXTURE1, v->textureCb.id, &frame->cb);
mpeg_update_texture(GL_TEXTURE2, v->textureCr.id, &frame->cr);
mpeg_update_texture(GL_TEXTURE4, v->textureY.id, &frame->y);
mpeg_update_texture(GL_TEXTURE5, v->textureCb.id, &frame->cb);
mpeg_update_texture(GL_TEXTURE6, v->textureCr.id, &frame->cr);
} else {
plm_frame_to_rgb( frame, v->surface, v->texture.w * 3 );
texture_update( &v->texture, v->texture.w, v->texture.h, v->texture.n, v->surface, v->texture.flags );

View File

@ -81,7 +81,7 @@ GLuint shader_compile( GLenum type, const char *source ) {
// dump log with line numbers
shader_print( source );
PRINTF("!ERROR: shader_compile(): %s\n%s\n", type == GL_VERTEX_SHADER ? "Vertex" : "Fragment", buf);
PANIC("!ERROR: shader_compile(): %s\n%s\n", type == GL_VERTEX_SHADER ? "Vertex" : "Fragment", buf);
return 0;
}
@ -102,7 +102,7 @@ unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char
}
}
const char *glsl_version = ifdef(ems, "300 es", "150");
const char *glsl_version = ifdef(ems, "300 es", "400");
if(gs)
gs = gs && gs[0] == '#' && gs[1] == 'v' ? gs : va("#version %s\n%s\n%s", glsl_version, glsl_defines, gs ? gs : "");
@ -670,7 +670,6 @@ static
int allocate_texture_unit() {
static int textureUnit = 0, totalTextureUnits = 0;
do_once glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &totalTextureUnits);
ASSERT(textureUnit < totalTextureUnits, "%d texture units exceeded", totalTextureUnits);
return textureUnit++;
}
@ -1611,7 +1610,7 @@ skybox_t skybox(const char *asset, int flags) {
mesh_update(&sky.geometry, "p3", 0,countof(vertices),vertices, countof(indices),indices, MESH_TRIANGLE_STRIP);
// sky program
sky.flags = flags ? flags : !!asset; // either cubemap or rayleigh
sky.flags = flags && flags != SKYBOX_PBR ? flags : !!asset ? SKYBOX_CUBEMAP : SKYBOX_RAYLEIGH; // either cubemap or rayleigh
sky.program = shader(vfs_read("shaders/vs_3_3_skybox.glsl"),
sky.flags ? vfs_read("fs_3_4_skybox.glsl") : vfs_read("shaders/fs_3_4_skybox_rayleigh.glsl"),
"att_position", "fragcolor", NULL);
@ -1654,6 +1653,77 @@ skybox_t skybox(const char *asset, int flags) {
return sky;
}
static inline
texture_t load_env_tex( const char *pathfile, unsigned flags ) {
int flags_hdr = strendi(pathfile, ".hdr") ? TEXTURE_FLOAT | TEXTURE_RGBA : 0;
texture_t t = texture(pathfile, flags | TEXTURE_LINEAR | TEXTURE_MIPMAPS | TEXTURE_REPEAT | flags_hdr);
glBindTexture( GL_TEXTURE_2D, t.id );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
return t;
}
skybox_t skybox_pbr(const char *refl_map, const char *env_map) {
skybox_t sky = {0};
// sky mesh
vec3 vertices[] = {{+1,-1,+1},{+1,+1,+1},{+1,+1,-1},{-1,+1,-1},{+1,-1,-1},{-1,-1,-1},{-1,-1,+1},{-1,+1,+1}};
unsigned indices[] = { 0, 1, 2, 3, 4, 5, 6, 3, 7, 1, 6, 0, 4, 2 };
mesh_update(&sky.geometry, "p3", 0,countof(vertices),vertices, countof(indices),indices, MESH_TRIANGLE_STRIP);
// sky program
sky.flags = SKYBOX_PBR;
sky.program = shader(vfs_read("shaders/vs_3_3_skybox.glsl"),
sky.flags ? vfs_read("fs_3_4_skybox.glsl") : vfs_read("shaders/fs_3_4_skybox_rayleigh.glsl"),
"att_position", "fragcolor", NULL);
// sky cubemap & SH
if( refl_map ) {
int is_panorama = vfs_size( refl_map );
if( is_panorama ) { // is file
stbi_hdr_to_ldr_gamma(1.2f);
image_t panorama = image( refl_map, IMAGE_RGBA );
sky.cubemap = cubemap( panorama, 0 ); // RGBA required
image_destroy(&panorama);
} else { // is folder
image_t images[6] = {0};
images[0] = image( va("%s/posx", refl_map), IMAGE_RGB ); // cubepx
images[1] = image( va("%s/negx", refl_map), IMAGE_RGB ); // cubenx
images[2] = image( va("%s/posy", refl_map), IMAGE_RGB ); // cubepy
images[3] = image( va("%s/negy", refl_map), IMAGE_RGB ); // cubeny
images[4] = image( va("%s/posz", refl_map), IMAGE_RGB ); // cubepz
images[5] = image( va("%s/negz", refl_map), IMAGE_RGB ); // cubenz
sky.cubemap = cubemap6( images, 0 );
for( int i = 0; i < countof(images); ++i ) image_destroy(&images[i]);
}
sky.refl = load_env_tex(refl_map, TEXTURE_SRGB);
}
if( env_map ) {
int is_panorama = vfs_size( env_map );
if( is_panorama ) { // is file
stbi_hdr_to_ldr_gamma(1.2f);
image_t panorama = image( env_map, IMAGE_RGBA );
sky.env_cubemap = cubemap( panorama, 0 ); // RGBA required
image_destroy(&panorama);
} else { // is folder
image_t images[6] = {0};
images[0] = image( va("%s/posx", env_map), IMAGE_RGB ); // cubepx
images[1] = image( va("%s/negx", env_map), IMAGE_RGB ); // cubenx
images[2] = image( va("%s/posy", env_map), IMAGE_RGB ); // cubepy
images[3] = image( va("%s/negy", env_map), IMAGE_RGB ); // cubeny
images[4] = image( va("%s/posz", env_map), IMAGE_RGB ); // cubepz
images[5] = image( va("%s/negz", env_map), IMAGE_RGB ); // cubenz
sky.env_cubemap = cubemap6( images, 0 );
for( int i = 0; i < countof(images); ++i ) image_destroy(&images[i]);
}
sky.env = load_env_tex(env_map, TEXTURE_SRGB);
}
return sky;
}
void skybox_mie_calc_sh(skybox_t *sky, float sky_intensity) {
unsigned WIDTH = 1024, HEIGHT = 1024;
int last_fb;
@ -2907,6 +2977,11 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view,
if ((loc = glGetUniformLocation(shader, "view")) >= 0) {
glUniformMatrix4fv(loc, 1, GL_FALSE, view);
}
if ((loc = glGetUniformLocation(shader, "inv_view")) >= 0) {
mat44 inv_view;
invert44( inv_view, view);
glUniformMatrix4fv(loc, 1, GL_FALSE, inv_view);
}
if ((loc = glGetUniformLocation(shader, "P")) >= 0) {
glUniformMatrix4fv(loc, 1, GL_FALSE, proj);
}
@ -2921,6 +2996,36 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view,
if ((loc = glGetUniformLocation(shader, "u_matcaps")) >= 0) {
glUniform1i(loc, m.flags & MODEL_MATCAPS ? GL_TRUE:GL_FALSE);
}
if (m.shading == SHADING_PBR) {
const pbr_material_t *material = &m.pbr_material;
shader_colormap( "map_diffuse", material->diffuse );
shader_colormap( "map_normals", material->normals );
shader_colormap( "map_specular", material->specular );
shader_colormap( "map_albedo", material->albedo );
shader_colormap( "map_roughness", material->roughness );
shader_colormap( "map_metallic", material->metallic );
shader_colormap( "map_ao", material->ao );
shader_colormap( "map_ambient", material->ambient );
shader_colormap( "map_emissive", material->emissive );
shader_float( "specular_shininess", material->specular_shininess ); // unused, basic_specgloss.fs only
shader_vec2( "resolution", vec2(window_width(),window_height()));
bool has_tex_skysphere = m.sky_refl.id != texture_checker().id;
bool has_tex_skyenv = m.sky_env.id != texture_checker().id;
shader_bool( "has_tex_skysphere", has_tex_skysphere );
shader_bool( "has_tex_skyenv", has_tex_skyenv );
if( has_tex_skysphere ) {
float mipCount = floor( log2( m.sky_refl.h ) );
shader_texture("tex_skysphere", m.sky_refl);
shader_float( "skysphere_mip_count", mipCount );
}
if( has_tex_skyenv ) {
shader_texture( "tex_skyenv", m.sky_env );
}
shader_texture( "tex_brdf_lut", brdf_lut() );
}
}
static
void model_set_state(model_t m) {
@ -3358,6 +3463,9 @@ bool model_load_textures(iqm_t *q, const struct iqmheader *hdr, model_t *model,
model_t model_from_mem(const void *mem, int len, int flags) {
model_t m = {0};
m.stored_flags = flags;
m.shading = SHADING_PHONG;
const char *ptr = (const char *)mem;
// can't cache shader programs since we enable features via flags here
// static int shaderprog = -1;
@ -3638,6 +3746,7 @@ void model_draw_call(model_t m, int shader) {
for(int i = 0; i < q->nummeshes; i++) {
struct iqmmesh *im = &q->meshes[i];
if (m.shading != SHADING_PBR) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, q->textures[i] );
glUniform1i(glGetUniformLocation(shader, "u_texture2d"), 0 );
@ -3654,6 +3763,7 @@ void model_draw_call(model_t m, int shader) {
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m.lightmap.id);
glUniform1i(glGetUniformLocation(shader, "u_lightmap"), 1 );
}
glDrawElementsInstanced(GL_TRIANGLES, 3*im->num_triangles, GL_UNSIGNED_INT, &tris[im->first_triangle], m.num_instances);
profile_incstat("Render.num_drawcalls", +1);
@ -3683,6 +3793,33 @@ void model_render(model_t m, mat44 proj, mat44 view, mat44 model, int shader) {
model_render_instanced(m, proj, view, (mat44*)model, shader, 1);
}
void model_shading(model_t *m, int shading) {
m->shading = shading;
int flags = m->stored_flags;
// load pbr material if SHADING_PBR was selected
if (shading == SHADING_PBR && array_count(m->materials) > 0) {
pbr_material(&m->pbr_material, m->materials[0].name);
}
// rebind shader
// @fixme: destroy old shader program
const char *symbols[] = { "{{include-shadowmap}}", vfs_read("shaders/fs_0_0_shadowmap_lit.glsl") }; // #define RIM
int shaderprog = shader(strlerp(1,symbols,vfs_read("shaders/vs_323444143_16_3322_model.glsl")), strlerp(1,symbols,vfs_read("shaders/fs_32_4_model.glsl")), //fs,
"att_position,att_texcoord,att_normal,att_tangent,att_instanced_matrix,,,,att_indexes,att_weights,att_vertexindex,att_color,att_bitangent,att_texcoord2","fragColor",
va("%s,%s", shading == SHADING_PBR ? "SHADING_PBR" : "SHADING_PHONG", (flags&MODEL_RIMLIGHT)?"RIM":""));
m->program = shaderprog;
}
void model_skybox(model_t *mdl, skybox_t sky, bool load_sh) {
if (load_sh) {
shader_vec3v("u_coefficients_sh", 9, sky.cubemap.sh);
}
mdl->sky_refl = sky.refl;
mdl->sky_env = sky.env;
}
// static
aabb aabb_transform( aabb A, mat44 M ) {
// Based on "Transforming Axis-Aligned Bounding Boxes" by Jim Arvo, 1990

View File

@ -423,6 +423,42 @@ API void mesh_render_prim(mesh_t *sm, unsigned prim);
API void mesh_destroy(mesh_t *m);
API aabb mesh_bounds(mesh_t *m);
// -----------------------------------------------------------------------------
// skyboxes
enum SKYBOX_FLAGS {
SKYBOX_RAYLEIGH,
SKYBOX_CUBEMAP,
SKYBOX_PBR,
};
typedef struct skybox_t {
handle program;
mesh_t geometry;
cubemap_t cubemap;
cubemap_t env_cubemap;
int flags;
// mie
int framebuffers[6];
int textures[6];
float *pixels;
// pbr
texture_t refl, env;
} skybox_t;
API skybox_t skybox(const char *panorama_or_cubemap_folder, int flags);
API skybox_t skybox_pbr(const char *refl_map, const char *env_map);
API int skybox_render(skybox_t *sky, mat44 proj, mat44 view);
API void skybox_destroy(skybox_t *sky);
API void skybox_mie_calc_sh(skybox_t *sky, float sky_intensity);
API void skybox_sh_reset(skybox_t *sky);
API void skybox_sh_add_light(skybox_t *sky, vec3 light, vec3 dir, float strength);
API int skybox_push_state(skybox_t *sky, mat44 proj, mat44 view); // @to deprecate
API int skybox_pop_state(); // @to deprecate
// -----------------------------------------------------------------------------
// materials
@ -507,22 +543,24 @@ enum MODEL_FLAGS {
MODEL_RIMLIGHT = 32,
};
//@todo: make this data-driven
// enum SHADING_MODE {
// SHADING_NONE,
// SHADING_PHONG,
// SHADING_CARTOON,
// // SHADING_PBR,
// };
enum SHADING_MODE {
SHADING_NONE,
SHADING_PHONG,
SHADING_PBR,
};
typedef struct model_t {
struct iqm_t *iqm; // private
int shading; // based on SHADING_MODE
unsigned num_textures;
handle *textures;
char **texture_names;
array(material_t) materials;
pbr_material_t pbr_material;
texture_t sky_refl, sky_env;
texture_t lightmap;
float *lmdata;
@ -547,6 +585,8 @@ typedef struct model_t {
float *instanced_matrices;
unsigned num_instances;
int stored_flags;
} model_t;
enum BILLBOARD_MODE {
@ -564,6 +604,8 @@ API float model_animate(model_t, float curframe);
API float model_animate_clip(model_t, float curframe, int minframe, int maxframe, bool loop);
API float model_animate_blends(model_t m, anim_t *primary, anim_t *secondary, float delta);
API aabb model_aabb(model_t, mat44 transform);
API void model_shading(model_t*, int shading);
API void model_skybox(model_t*, skybox_t sky, bool load_sh);
API void model_render(model_t, mat44 proj, mat44 view, mat44 model, int shader);
API void model_render_skeleton(model_t, mat44 model);
API void model_render_instanced(model_t, mat44 proj, mat44 view, mat44 *models, int shader, unsigned count);
@ -603,31 +645,6 @@ API void lightmap_setup(lightmap_t *lm, int w, int h);
API void lightmap_bake(lightmap_t *lm, int bounces, void (*drawscene)(lightmap_t *lm, model_t *m, float *view, float *proj, void *userdata), void (*progressupdate)(float progress), void *userdata);
API void lightmap_destroy(lightmap_t *lm);
// -----------------------------------------------------------------------------
// skyboxes
typedef struct skybox_t {
handle program;
mesh_t geometry;
cubemap_t cubemap;
int flags;
// mie
int framebuffers[6];
int textures[6];
float *pixels;
} skybox_t;
API skybox_t skybox(const char *panorama_or_cubemap_folder, int flags);
API int skybox_render(skybox_t *sky, mat44 proj, mat44 view);
API void skybox_destroy(skybox_t *sky);
API void skybox_mie_calc_sh(skybox_t *sky, float sky_intensity);
API void skybox_sh_reset(skybox_t *sky);
API void skybox_sh_add_light(skybox_t *sky, vec3 light, vec3 dir, float strength);
API int skybox_push_state(skybox_t *sky, mat44 proj, mat44 view); // @to deprecate
API int skybox_pop_state(); // @to deprecate
// -----------------------------------------------------------------------------
// post-fxs

View File

@ -570,13 +570,16 @@ void scene_render(int flags) {
shader_vec3v("u_coefficients_sh", 9, last_scene->skybox.cubemap.sh);
}
model_skybox(model, last_scene->skybox, 0);
if (anim) {
float delta = window_delta() * obj->anim_speed;
model->curframe = model_animate_clip(*model, model->curframe + delta, anim->from, anim->to, anim->flags & ANIM_LOOP );
}
model->billboard = obj->billboard;
model_render(*model, cam->proj, cam->view, obj->transform, 0);
model_render(*model, cam->proj, cam->view, obj->transform, model->program);
if( do_retexturing ) {
for(int i = 0; i < model->iqm->nummeshes; ++i) {

View File

@ -35,9 +35,9 @@ static void mpeg_video_callback( plm_t* plm, plm_frame_t* frame, void* user ) {
if(v->paused) return;
if(v->has_ycbcr) {
mpeg_update_texture(GL_TEXTURE0, v->textureY.id, &frame->y);
mpeg_update_texture(GL_TEXTURE1, v->textureCb.id, &frame->cb);
mpeg_update_texture(GL_TEXTURE2, v->textureCr.id, &frame->cr);
mpeg_update_texture(GL_TEXTURE4, v->textureY.id, &frame->y);
mpeg_update_texture(GL_TEXTURE5, v->textureCb.id, &frame->cb);
mpeg_update_texture(GL_TEXTURE6, v->textureCr.id, &frame->cr);
} else {
plm_frame_to_rgb( frame, v->surface, v->texture.w * 3 );
texture_update( &v->texture, v->texture.w, v->texture.h, v->texture.n, v->surface, v->texture.flags );

View File

@ -17255,7 +17255,7 @@ GLuint shader_compile( GLenum type, const char *source ) {
// dump log with line numbers
shader_print( source );
PRINTF("!ERROR: shader_compile(): %s\n%s\n", type == GL_VERTEX_SHADER ? "Vertex" : "Fragment", buf);
PANIC("!ERROR: shader_compile(): %s\n%s\n", type == GL_VERTEX_SHADER ? "Vertex" : "Fragment", buf);
return 0;
}
@ -17276,7 +17276,7 @@ unsigned shader_geom(const char *gs, const char *vs, const char *fs, const char
}
}
const char *glsl_version = ifdef(ems, "300 es", "150");
const char *glsl_version = ifdef(ems, "300 es", "400");
if(gs)
gs = gs && gs[0] == '#' && gs[1] == 'v' ? gs : va("#version %s\n%s\n%s", glsl_version, glsl_defines, gs ? gs : "");
@ -17844,7 +17844,6 @@ static
int allocate_texture_unit() {
static int textureUnit = 0, totalTextureUnits = 0;
do_once glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &totalTextureUnits);
ASSERT(textureUnit < totalTextureUnits, "%d texture units exceeded", totalTextureUnits);
return textureUnit++;
}
@ -18785,7 +18784,7 @@ skybox_t skybox(const char *asset, int flags) {
mesh_update(&sky.geometry, "p3", 0,countof(vertices),vertices, countof(indices),indices, MESH_TRIANGLE_STRIP);
// sky program
sky.flags = flags ? flags : !!asset; // either cubemap or rayleigh
sky.flags = flags && flags != SKYBOX_PBR ? flags : !!asset ? SKYBOX_CUBEMAP : SKYBOX_RAYLEIGH; // either cubemap or rayleigh
sky.program = shader(vfs_read("shaders/vs_3_3_skybox.glsl"),
sky.flags ? vfs_read("fs_3_4_skybox.glsl") : vfs_read("shaders/fs_3_4_skybox_rayleigh.glsl"),
"att_position", "fragcolor", NULL);
@ -18828,6 +18827,77 @@ skybox_t skybox(const char *asset, int flags) {
return sky;
}
static inline
texture_t load_env_tex( const char *pathfile, unsigned flags ) {
int flags_hdr = strendi(pathfile, ".hdr") ? TEXTURE_FLOAT | TEXTURE_RGBA : 0;
texture_t t = texture(pathfile, flags | TEXTURE_LINEAR | TEXTURE_MIPMAPS | TEXTURE_REPEAT | flags_hdr);
glBindTexture( GL_TEXTURE_2D, t.id );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
return t;
}
skybox_t skybox_pbr(const char *refl_map, const char *env_map) {
skybox_t sky = {0};
// sky mesh
vec3 vertices[] = {{+1,-1,+1},{+1,+1,+1},{+1,+1,-1},{-1,+1,-1},{+1,-1,-1},{-1,-1,-1},{-1,-1,+1},{-1,+1,+1}};
unsigned indices[] = { 0, 1, 2, 3, 4, 5, 6, 3, 7, 1, 6, 0, 4, 2 };
mesh_update(&sky.geometry, "p3", 0,countof(vertices),vertices, countof(indices),indices, MESH_TRIANGLE_STRIP);
// sky program
sky.flags = SKYBOX_PBR;
sky.program = shader(vfs_read("shaders/vs_3_3_skybox.glsl"),
sky.flags ? vfs_read("fs_3_4_skybox.glsl") : vfs_read("shaders/fs_3_4_skybox_rayleigh.glsl"),
"att_position", "fragcolor", NULL);
// sky cubemap & SH
if( refl_map ) {
int is_panorama = vfs_size( refl_map );
if( is_panorama ) { // is file
stbi_hdr_to_ldr_gamma(1.2f);
image_t panorama = image( refl_map, IMAGE_RGBA );
sky.cubemap = cubemap( panorama, 0 ); // RGBA required
image_destroy(&panorama);
} else { // is folder
image_t images[6] = {0};
images[0] = image( va("%s/posx", refl_map), IMAGE_RGB ); // cubepx
images[1] = image( va("%s/negx", refl_map), IMAGE_RGB ); // cubenx
images[2] = image( va("%s/posy", refl_map), IMAGE_RGB ); // cubepy
images[3] = image( va("%s/negy", refl_map), IMAGE_RGB ); // cubeny
images[4] = image( va("%s/posz", refl_map), IMAGE_RGB ); // cubepz
images[5] = image( va("%s/negz", refl_map), IMAGE_RGB ); // cubenz
sky.cubemap = cubemap6( images, 0 );
for( int i = 0; i < countof(images); ++i ) image_destroy(&images[i]);
}
sky.refl = load_env_tex(refl_map, TEXTURE_SRGB);
}
if( env_map ) {
int is_panorama = vfs_size( env_map );
if( is_panorama ) { // is file
stbi_hdr_to_ldr_gamma(1.2f);
image_t panorama = image( env_map, IMAGE_RGBA );
sky.env_cubemap = cubemap( panorama, 0 ); // RGBA required
image_destroy(&panorama);
} else { // is folder
image_t images[6] = {0};
images[0] = image( va("%s/posx", env_map), IMAGE_RGB ); // cubepx
images[1] = image( va("%s/negx", env_map), IMAGE_RGB ); // cubenx
images[2] = image( va("%s/posy", env_map), IMAGE_RGB ); // cubepy
images[3] = image( va("%s/negy", env_map), IMAGE_RGB ); // cubeny
images[4] = image( va("%s/posz", env_map), IMAGE_RGB ); // cubepz
images[5] = image( va("%s/negz", env_map), IMAGE_RGB ); // cubenz
sky.env_cubemap = cubemap6( images, 0 );
for( int i = 0; i < countof(images); ++i ) image_destroy(&images[i]);
}
sky.env = load_env_tex(env_map, TEXTURE_SRGB);
}
return sky;
}
void skybox_mie_calc_sh(skybox_t *sky, float sky_intensity) {
unsigned WIDTH = 1024, HEIGHT = 1024;
int last_fb;
@ -20081,6 +20151,11 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view,
if ((loc = glGetUniformLocation(shader, "view")) >= 0) {
glUniformMatrix4fv(loc, 1, GL_FALSE, view);
}
if ((loc = glGetUniformLocation(shader, "inv_view")) >= 0) {
mat44 inv_view;
invert44( inv_view, view);
glUniformMatrix4fv(loc, 1, GL_FALSE, inv_view);
}
if ((loc = glGetUniformLocation(shader, "P")) >= 0) {
glUniformMatrix4fv(loc, 1, GL_FALSE, proj);
}
@ -20095,6 +20170,36 @@ void model_set_uniforms(model_t m, int shader, mat44 mv, mat44 proj, mat44 view,
if ((loc = glGetUniformLocation(shader, "u_matcaps")) >= 0) {
glUniform1i(loc, m.flags & MODEL_MATCAPS ? GL_TRUE:GL_FALSE);
}
if (m.shading == SHADING_PBR) {
const pbr_material_t *material = &m.pbr_material;
shader_colormap( "map_diffuse", material->diffuse );
shader_colormap( "map_normals", material->normals );
shader_colormap( "map_specular", material->specular );
shader_colormap( "map_albedo", material->albedo );
shader_colormap( "map_roughness", material->roughness );
shader_colormap( "map_metallic", material->metallic );
shader_colormap( "map_ao", material->ao );
shader_colormap( "map_ambient", material->ambient );
shader_colormap( "map_emissive", material->emissive );
shader_float( "specular_shininess", material->specular_shininess ); // unused, basic_specgloss.fs only
shader_vec2( "resolution", vec2(window_width(),window_height()));
bool has_tex_skysphere = m.sky_refl.id != texture_checker().id;
bool has_tex_skyenv = m.sky_env.id != texture_checker().id;
shader_bool( "has_tex_skysphere", has_tex_skysphere );
shader_bool( "has_tex_skyenv", has_tex_skyenv );
if( has_tex_skysphere ) {
float mipCount = floor( log2( m.sky_refl.h ) );
shader_texture("tex_skysphere", m.sky_refl);
shader_float( "skysphere_mip_count", mipCount );
}
if( has_tex_skyenv ) {
shader_texture( "tex_skyenv", m.sky_env );
}
shader_texture( "tex_brdf_lut", brdf_lut() );
}
}
static
void model_set_state(model_t m) {
@ -20532,6 +20637,9 @@ bool model_load_textures(iqm_t *q, const struct iqmheader *hdr, model_t *model,
model_t model_from_mem(const void *mem, int len, int flags) {
model_t m = {0};
m.stored_flags = flags;
m.shading = SHADING_PHONG;
const char *ptr = (const char *)mem;
// can't cache shader programs since we enable features via flags here
// static int shaderprog = -1;
@ -20812,6 +20920,7 @@ void model_draw_call(model_t m, int shader) {
for(int i = 0; i < q->nummeshes; i++) {
struct iqmmesh *im = &q->meshes[i];
if (m.shading != SHADING_PBR) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, q->textures[i] );
glUniform1i(glGetUniformLocation(shader, "u_texture2d"), 0 );
@ -20828,6 +20937,7 @@ void model_draw_call(model_t m, int shader) {
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m.lightmap.id);
glUniform1i(glGetUniformLocation(shader, "u_lightmap"), 1 );
}
glDrawElementsInstanced(GL_TRIANGLES, 3*im->num_triangles, GL_UNSIGNED_INT, &tris[im->first_triangle], m.num_instances);
profile_incstat("Render.num_drawcalls", +1);
@ -20857,6 +20967,33 @@ void model_render(model_t m, mat44 proj, mat44 view, mat44 model, int shader) {
model_render_instanced(m, proj, view, (mat44*)model, shader, 1);
}
void model_shading(model_t *m, int shading) {
m->shading = shading;
int flags = m->stored_flags;
// load pbr material if SHADING_PBR was selected
if (shading == SHADING_PBR && array_count(m->materials) > 0) {
pbr_material(&m->pbr_material, m->materials[0].name);
}
// rebind shader
// @fixme: destroy old shader program
const char *symbols[] = { "{{include-shadowmap}}", vfs_read("shaders/fs_0_0_shadowmap_lit.glsl") }; // #define RIM
int shaderprog = shader(strlerp(1,symbols,vfs_read("shaders/vs_323444143_16_3322_model.glsl")), strlerp(1,symbols,vfs_read("shaders/fs_32_4_model.glsl")), //fs,
"att_position,att_texcoord,att_normal,att_tangent,att_instanced_matrix,,,,att_indexes,att_weights,att_vertexindex,att_color,att_bitangent,att_texcoord2","fragColor",
va("%s,%s", shading == SHADING_PBR ? "SHADING_PBR" : "SHADING_PHONG", (flags&MODEL_RIMLIGHT)?"RIM":""));
m->program = shaderprog;
}
void model_skybox(model_t *mdl, skybox_t sky, bool load_sh) {
if (load_sh) {
shader_vec3v("u_coefficients_sh", 9, sky.cubemap.sh);
}
mdl->sky_refl = sky.refl;
mdl->sky_env = sky.env;
}
// static
aabb aabb_transform( aabb A, mat44 M ) {
// Based on "Transforming Axis-Aligned Bounding Boxes" by Jim Arvo, 1990
@ -22557,13 +22694,16 @@ void scene_render(int flags) {
shader_vec3v("u_coefficients_sh", 9, last_scene->skybox.cubemap.sh);
}
model_skybox(model, last_scene->skybox, 0);
if (anim) {
float delta = window_delta() * obj->anim_speed;
model->curframe = model_animate_clip(*model, model->curframe + delta, anim->from, anim->to, anim->flags & ANIM_LOOP );
}
model->billboard = obj->billboard;
model_render(*model, cam->proj, cam->view, obj->transform, 0);
model_render(*model, cam->proj, cam->view, obj->transform, model->program);
if( do_retexturing ) {
for(int i = 0; i < model->iqm->nummeshes; ++i) {
@ -26026,9 +26166,9 @@ static void mpeg_video_callback( plm_t* plm, plm_frame_t* frame, void* user ) {
if(v->paused) return;
if(v->has_ycbcr) {
mpeg_update_texture(GL_TEXTURE0, v->textureY.id, &frame->y);
mpeg_update_texture(GL_TEXTURE1, v->textureCb.id, &frame->cb);
mpeg_update_texture(GL_TEXTURE2, v->textureCr.id, &frame->cr);
mpeg_update_texture(GL_TEXTURE4, v->textureY.id, &frame->y);
mpeg_update_texture(GL_TEXTURE5, v->textureCb.id, &frame->cb);
mpeg_update_texture(GL_TEXTURE6, v->textureCr.id, &frame->cr);
} else {
plm_frame_to_rgb( frame, v->surface, v->texture.w * 3 );
texture_update( &v->texture, v->texture.w, v->texture.h, v->texture.n, v->surface, v->texture.flags );

View File

@ -3512,6 +3512,42 @@ API void mesh_render_prim(mesh_t *sm, unsigned prim);
API void mesh_destroy(mesh_t *m);
API aabb mesh_bounds(mesh_t *m);
// -----------------------------------------------------------------------------
// skyboxes
enum SKYBOX_FLAGS {
SKYBOX_RAYLEIGH,
SKYBOX_CUBEMAP,
SKYBOX_PBR,
};
typedef struct skybox_t {
handle program;
mesh_t geometry;
cubemap_t cubemap;
cubemap_t env_cubemap;
int flags;
// mie
int framebuffers[6];
int textures[6];
float *pixels;
// pbr
texture_t refl, env;
} skybox_t;
API skybox_t skybox(const char *panorama_or_cubemap_folder, int flags);
API skybox_t skybox_pbr(const char *refl_map, const char *env_map);
API int skybox_render(skybox_t *sky, mat44 proj, mat44 view);
API void skybox_destroy(skybox_t *sky);
API void skybox_mie_calc_sh(skybox_t *sky, float sky_intensity);
API void skybox_sh_reset(skybox_t *sky);
API void skybox_sh_add_light(skybox_t *sky, vec3 light, vec3 dir, float strength);
API int skybox_push_state(skybox_t *sky, mat44 proj, mat44 view); // @to deprecate
API int skybox_pop_state(); // @to deprecate
// -----------------------------------------------------------------------------
// materials
@ -3596,22 +3632,24 @@ enum MODEL_FLAGS {
MODEL_RIMLIGHT = 32,
};
//@todo: make this data-driven
// enum SHADING_MODE {
// SHADING_NONE,
// SHADING_PHONG,
// SHADING_CARTOON,
// // SHADING_PBR,
// };
enum SHADING_MODE {
SHADING_NONE,
SHADING_PHONG,
SHADING_PBR,
};
typedef struct model_t {
struct iqm_t *iqm; // private
int shading; // based on SHADING_MODE
unsigned num_textures;
handle *textures;
char **texture_names;
array(material_t) materials;
pbr_material_t pbr_material;
texture_t sky_refl, sky_env;
texture_t lightmap;
float *lmdata;
@ -3636,6 +3674,8 @@ typedef struct model_t {
float *instanced_matrices;
unsigned num_instances;
int stored_flags;
} model_t;
enum BILLBOARD_MODE {
@ -3653,6 +3693,8 @@ API float model_animate(model_t, float curframe);
API float model_animate_clip(model_t, float curframe, int minframe, int maxframe, bool loop);
API float model_animate_blends(model_t m, anim_t *primary, anim_t *secondary, float delta);
API aabb model_aabb(model_t, mat44 transform);
API void model_shading(model_t*, int shading);
API void model_skybox(model_t*, skybox_t sky, bool load_sh);
API void model_render(model_t, mat44 proj, mat44 view, mat44 model, int shader);
API void model_render_skeleton(model_t, mat44 model);
API void model_render_instanced(model_t, mat44 proj, mat44 view, mat44 *models, int shader, unsigned count);
@ -3692,31 +3734,6 @@ API void lightmap_setup(lightmap_t *lm, int w, int h);
API void lightmap_bake(lightmap_t *lm, int bounces, void (*drawscene)(lightmap_t *lm, model_t *m, float *view, float *proj, void *userdata), void (*progressupdate)(float progress), void *userdata);
API void lightmap_destroy(lightmap_t *lm);
// -----------------------------------------------------------------------------
// skyboxes
typedef struct skybox_t {
handle program;
mesh_t geometry;
cubemap_t cubemap;
int flags;
// mie
int framebuffers[6];
int textures[6];
float *pixels;
} skybox_t;
API skybox_t skybox(const char *panorama_or_cubemap_folder, int flags);
API int skybox_render(skybox_t *sky, mat44 proj, mat44 view);
API void skybox_destroy(skybox_t *sky);
API void skybox_mie_calc_sh(skybox_t *sky, float sky_intensity);
API void skybox_sh_reset(skybox_t *sky);
API void skybox_sh_add_light(skybox_t *sky, vec3 light, vec3 dir, float strength);
API int skybox_push_state(skybox_t *sky, mat44 proj, mat44 view); // @to deprecate
API int skybox_pop_state(); // @to deprecate
// -----------------------------------------------------------------------------
// post-fxs

2
list.bat 100644
View File

@ -0,0 +1,2 @@
@echo off
dir/w *.exe