From 409edea2cf6271956137918e4e0b4f1c2addf620 Mon Sep 17 00:00:00 2001 From: hgn Date: Sat, 25 Feb 2023 07:00:26 +0000 Subject: [PATCH] now we're doing a bunch of them --- maps_src/mp_mtzero.mdl | Bin 12900752 -> 12905088 bytes player.c | 4 +- scene.h | 6 +- shaders/common_scene.glsl | 48 ++++++-- shaders/common_world.glsl | 22 +--- shaders/model_character_view.fs | 2 - shaders/model_character_view.h | 38 ++----- shaders/model_sky.fs | 31 ++++-- shaders/model_sky.h | 165 +++++++++++++++++++++++++--- shaders/scene.vs | 13 ++- shaders/scene_depth.fs | 6 - shaders/scene_depth.h | 91 +++++++++------ shaders/scene_position.fs | 6 - shaders/scene_position.h | 91 +++++++++------ shaders/scene_route.fs | 6 - shaders/scene_route.h | 93 ++++++++++------ shaders/scene_standard.fs | 6 - shaders/scene_standard.h | 93 ++++++++++------ shaders/scene_standard_alphatest.fs | 6 - shaders/scene_standard_alphatest.h | 93 ++++++++++------ shaders/scene_terrain.fs | 6 - shaders/scene_terrain.h | 93 ++++++++++------ shaders/scene_vertex_blend.fs | 6 - shaders/scene_vertex_blend.h | 93 ++++++++++------ shaders/scene_water.fs | 6 - shaders/scene_water.h | 93 ++++++++++------ shaders/scene_water_fast.fs | 6 - shaders/scene_water_fast.h | 93 ++++++++++------ world.h | 35 +++--- world_gen.h | 34 ++++-- world_render.h | 56 ++++++++-- world_routes.h | 10 +- world_water.h | 14 ++- 33 files changed, 856 insertions(+), 509 deletions(-) diff --git a/maps_src/mp_mtzero.mdl b/maps_src/mp_mtzero.mdl index 4175d691890dfcadcab6c39c660520dc045a574d..f3a6aee96559aa9f4de948db4bdafbbe1d7b91fe 100644 GIT binary patch delta 3454 zcmc)KYfw~W9LMnk2;zc-y1Imf7>j~(mA#;;a1LZ%PzgyTO(i!4ykVhYDZ7G#C?T@k zyky8F?}0*qh=|XcIF6Jrh#aSBFfQK8VT>jt(|-H#pijMSh?$S)(XY?`&)IVh*_B2c z!*@oT)q>D`%0dtX!-FgMQxNj31R(*(8{pU@2t#pnQ&3fUz!1!L_j+|TYwyK67*(h*tR5y|0tG|BnN z+o{Kvex|U(TM*jZvq)t60V%6Ig&5sBd=x|a+FG<#k4i}6KBLrFkVp5)SxZ9mKXHyu)Y zH%U$|&S)>j-9Y+vWD!vu+#p@wtDxI**|u9~=RC@ZQ@|=|k-Z&lrL1+J6}DEB&2~$q z1>zs{G^<$aM*Bg2p4{IYDJ6Hc5u;=Ki+NAc#`KzqWka~ME3}F3Yhn9reFPz8+buH8 zYp!IuPQaC2L`5%KR=;ROThw7ZT(1J3dY-)*{dvsvnYXGd!iF zYR}8`_OZ4U&FSuQ+6-mO8_k~?#ab*{Q<}_l6Z@{sm}ve?g^smEwBA|vrZX}R>F}in zIxmqm16t}#TT{XVxui)Rh4b`Lu#}0kOc-o>m~SI}-7$||UM_2eXw&XknYi&9=y-S=LS4>+KYZYj{#a5=)zrGQLCenBNSgS|NiT{)2EI2Pl|K3XX zwXk*#&HS~v96KUrrah*ugS8g4@%TDD9V*0mbu#)W^|024=6A1)gf!QPzW1F?^zFkd zZvOchEiCgcNe-?No#Q9ceR9^GpvfP1;_<8!$?3_oDOnqYUkx`a+lfnhxj1{vd~;K% zSaU?Hi@i;fVo!?k7h;TloxTc|T#$Afn#fF9saQXH9sS5C);!V7Z;ETuA+h)SEwt%a zQ=yq(+M?V0#YLJ_+7ej{L3{Qzm?-P^h&IX$G@t%);Yd5D*b)m%y{Ie5rrUDaw#8^` zYVTdX)4pB28dykMDQh~k825{`wH-TZZlkJLOF(=1Q|WMTv7g=$JZod&^hU4Y(*?u! zF#N(D*uU!sdf>n2=?DCZ|9JxXf&a??aRU1P;hys+5c%IfbTum#7VEtA5gX#xC;Iva zYVpMPuQl8{h+9v7xmNje>i}*&iCYJ9YZbRvzg+88+%Oe)5-M)6iW{uv2CKQjYHqNa z8?5FAtGU7IzPWmO^=fXgnj5U<25Y#%8g8(L8?505Yq-H0Zm@>CV2u_pR{z(o;Rb8C z!CG#xmK&_)25Y&&T5hnG8?5C9Yx^&FEbhIQ8?5C92bl*8>CK@QU9pCwRdG zmw%012=WK7@}T5jMeQ*aAtg6+VVfU>j_QWYB{Fc0dZG!cN!)X^;-PAp`cnUdV(` zAq)0FHtdHSFhVZmK|T~fAsm2%a0m{=XK(~Qhaxx%#ZUsJPzGPXF*pt<;3S-a({Kj9 rgmS2Wvv3Y7p$fi&YN&x)I1hDj0qWr*G{7Zjgv&s{RMQ-K#gz9qpQe{_ delta 918 zcmXBOMN<|~6oBFTfgp+~CU%R3t=Ng(f!&SR-HL^+C?dAlohaC1EOo|>3x9zw9mgNw z!?<|PJ9Exm+^p}pu{njgv5Q0adM^?}$ocWv3?U;rgjM>5#9tvKXv6qOh|%^HOJBO{ z-?;yslcGug_utYYVeX%(P}*i)%ZE^OEc_mv5DHEf=Qm1&(BSaBR8DfZ~EOC^hJn>W@fr?Zjk;+t|D%Ge?4U(uyEoxJTy40gS4QNOs8q?Dg_>}C&p*+(|}Ilw^mtx.pv ); world_instance *world = get_active_world(); - world_link_lighting_ub( world, _shader_model_character_view.id, 2 ); + world_link_lighting_ub( world, _shader_model_character_view.id ); + world_bind_position_texture( world, _shader_model_character_view.id, + _uniform_model_character_view_g_world_depth, 2 ); glUniformMatrix4x3fv( _uniform_model_character_view_uTransforms, player->playeravatar->sk.bone_count, diff --git a/scene.h b/scene.h index 63b351d..fa1ce4d 100644 --- a/scene.h +++ b/scene.h @@ -10,14 +10,14 @@ typedef struct scene_vert scene_vert; #pragma pack(push,1) -/* 28 byte vertexs, we don't care about the normals too much, +/* 32 byte vertexs, we don't care about the normals too much, * maybe possible to bring down uv to i16s too */ struct scene_vert { v3f co; /* 3*32 */ v2f uv; /* 2*32 */ i8 norm[4]; /* 4*8 */ - u8 lights[4]; /* 4*8 */ + u16 lights[4]; /* 4*16 */ }; #pragma pack(pop) @@ -260,7 +260,7 @@ VG_STATIC void scene_upload( scene *pscene, glmesh *mesh ) glEnableVertexAttribArray( 2 ); /* 3: light cluster */ - glVertexAttribIPointer( 3, 4, GL_UNSIGNED_BYTE, + glVertexAttribIPointer( 3, 4, GL_UNSIGNED_SHORT, stride, (void *)offsetof(scene_vert, lights) ); glEnableVertexAttribArray( 3 ); diff --git a/shaders/common_scene.glsl b/shaders/common_scene.glsl index 4a78fbc..c71f5e2 100644 --- a/shaders/common_scene.glsl +++ b/shaders/common_scene.glsl @@ -1,7 +1,33 @@ // :D +in vec2 aUv; +in vec4 aNorm; +in vec3 aCo; +in vec3 aWorldCo; +flat in vec4 light_colours[3]; +flat in vec4 light_positions[3]; + #include "common_world.glsl" +float sdLine( vec3 p, vec3 a, vec3 b ) +{ + vec3 pa = p - a; + vec3 ba = b - a; + + float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); + return length( pa - ba*h ); +} + +float compute_board_shadow() +{ + // player shadow + float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 ); + float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 ); + player_shadow *= player_shadow*player_shadow*player_shadow; + + return 1.0 - player_shadow*0.8; +} + vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal ) { // Lighting @@ -18,29 +44,31 @@ vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal ) world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 ); float world_shadow = newlight_compute_sun_shadow(); + float board_shadow = compute_board_shadow(); - total_light += world_light * world_shadow; + total_light += world_light * min( board_shadow, world_shadow ); // Compute the other lights that exist in the map, not effected by the sun // shadow + total_light += newlight_compute_quadratic ( wnormal, halfview, - g_point_light_positions[ aLights.x ].xyz, - g_point_light_colours[ aLights.x ].rgb - ); + light_positions[0].xyz, + light_colours[0].rgb + ) * board_shadow; total_light += newlight_compute_quadratic ( wnormal, halfview, - g_point_light_positions[ aLights.y ].xyz, - g_point_light_colours[ aLights.y ].rgb - ); + light_positions[1].xyz, + light_colours[1].rgb + ) * board_shadow; total_light += newlight_compute_quadratic ( wnormal, halfview, - g_point_light_positions[ aLights.z ].xyz, - g_point_light_colours[ aLights.z ].rgb - ); + light_positions[2].xyz, + light_colours[2].rgb + ) * board_shadow; return apply_fog( diffuse * total_light, fdist ); } diff --git a/shaders/common_world.glsl b/shaders/common_world.glsl index 45e12df..043cf26 100644 --- a/shaders/common_world.glsl +++ b/shaders/common_world.glsl @@ -13,8 +13,10 @@ layout (std140) uniform ub_world_lighting int g_light_preview; int g_shadow_samples; - vec4 g_point_light_positions[32]; - vec4 g_point_light_colours[32]; + // g_time ? + + //vec4 g_point_light_positions[32]; + //vec4 g_point_light_colours[32]; }; uniform sampler2D g_world_depth; @@ -41,15 +43,6 @@ float shadow_sample( vec3 vdir ) return clamp( fdelta, 0.1, 0.2 )-0.1; } -float sdLine( vec3 p, vec3 a, vec3 b ) -{ - vec3 pa = p - a; - vec3 ba = b - a; - - float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); - return length( pa - ba*h ); -} - vec3 apply_fog( vec3 vfrag, float fdist ) { float dist = pow(fdist*0.0008,1.2); @@ -85,12 +78,7 @@ float newlight_compute_sun_shadow() famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7); famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8); - // player shadow - float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 ); - float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 ); - player_shadow *= player_shadow*player_shadow*player_shadow; - - return 1.0 - max( player_shadow*0.8, famt ); + return 1.0 - famt; } vec3 newlight_compute_world_diffuse( vec3 wnormal ) diff --git a/shaders/model_character_view.fs b/shaders/model_character_view.fs index 8bf29bc..a85e124 100644 --- a/shaders/model_character_view.fs +++ b/shaders/model_character_view.fs @@ -1,7 +1,5 @@ uniform sampler2D uTexMain; uniform vec3 uCamera; -uniform vec3 uBoard0; -uniform vec3 uBoard1; in vec4 aColour; in vec2 aUv; diff --git a/shaders/model_character_view.h b/shaders/model_character_view.h index c527d90..18d5c19 100644 --- a/shaders/model_character_view.h +++ b/shaders/model_character_view.h @@ -74,8 +74,6 @@ static struct vg_shader _shader_model_character_view = { .static_src = "uniform sampler2D uTexMain;\n" "uniform vec3 uCamera;\n" -"uniform vec3 uBoard0;\n" -"uniform vec3 uBoard1;\n" "\n" "in vec4 aColour;\n" "in vec2 aUv;\n" @@ -99,8 +97,10 @@ static struct vg_shader _shader_model_character_view = { " int g_light_preview;\n" " int g_shadow_samples;\n" "\n" -" vec4 g_point_light_positions[32];\n" -" vec4 g_point_light_colours[32];\n" +" // g_time ?\n" +"\n" +" //vec4 g_point_light_positions[32];\n" +" //vec4 g_point_light_colours[32];\n" "};\n" "\n" "uniform sampler2D g_world_depth;\n" @@ -127,15 +127,6 @@ static struct vg_shader _shader_model_character_view = { " return clamp( fdelta, 0.1, 0.2 )-0.1;\n" "}\n" "\n" -"float sdLine( vec3 p, vec3 a, vec3 b )\n" -"{\n" -" vec3 pa = p - a;\n" -" vec3 ba = b - a;\n" -"\n" -" float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );\n" -" return length( pa - ba*h );\n" -"}\n" -"\n" "vec3 apply_fog( vec3 vfrag, float fdist )\n" "{\n" " float dist = pow(fdist*0.0008,1.2);\n" @@ -171,12 +162,7 @@ static struct vg_shader _shader_model_character_view = { " famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n" " famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n" "\n" -" // player shadow\n" -" float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );\n" -" float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );\n" -" player_shadow *= player_shadow*player_shadow*player_shadow;\n" -"\n" -" return 1.0 - max( player_shadow*0.8, famt );\n" +" return 1.0 - famt;\n" "}\n" "\n" "vec3 newlight_compute_world_diffuse( vec3 wnormal )\n" @@ -217,7 +203,7 @@ static struct vg_shader _shader_model_character_view = { " return light_colour*attenuation;\n" "}\n" "\n" -"#line 13 0 \n" +"#line 11 0 \n" "#line 1 2 \n" "const float k_motion_lerp_amount = 0.01;\n" "\n" @@ -237,7 +223,7 @@ static struct vg_shader _shader_model_character_view = { " oMotionVec = (vmotion1-vmotion0) * (1.0/k_motion_lerp_amount);\n" "}\n" "\n" -"#line 14 0 \n" +"#line 12 0 \n" "\n" "void main()\n" "{\n" @@ -271,8 +257,6 @@ static GLuint _uniform_model_character_view_uPv; static GLuint _uniform_model_character_view_uTransforms; static GLuint _uniform_model_character_view_uTexMain; static GLuint _uniform_model_character_view_uCamera; -static GLuint _uniform_model_character_view_uBoard0; -static GLuint _uniform_model_character_view_uBoard1; static GLuint _uniform_model_character_view_g_world_depth; static void shader_model_character_view_uPv(m4x4f m){ glUniformMatrix4fv(_uniform_model_character_view_uPv,1,GL_FALSE,(float*)m); @@ -283,12 +267,6 @@ static void shader_model_character_view_uTexMain(int i){ static void shader_model_character_view_uCamera(v3f v){ glUniform3fv(_uniform_model_character_view_uCamera,1,v); } -static void shader_model_character_view_uBoard0(v3f v){ - glUniform3fv(_uniform_model_character_view_uBoard0,1,v); -} -static void shader_model_character_view_uBoard1(v3f v){ - glUniform3fv(_uniform_model_character_view_uBoard1,1,v); -} static void shader_model_character_view_g_world_depth(int i){ glUniform1i(_uniform_model_character_view_g_world_depth,i); } @@ -301,8 +279,6 @@ static void shader_model_character_view_link(void){ _uniform_model_character_view_uTransforms = glGetUniformLocation( _shader_model_character_view.id, "uTransforms" ); _uniform_model_character_view_uTexMain = glGetUniformLocation( _shader_model_character_view.id, "uTexMain" ); _uniform_model_character_view_uCamera = glGetUniformLocation( _shader_model_character_view.id, "uCamera" ); - _uniform_model_character_view_uBoard0 = glGetUniformLocation( _shader_model_character_view.id, "uBoard0" ); - _uniform_model_character_view_uBoard1 = glGetUniformLocation( _shader_model_character_view.id, "uBoard1" ); _uniform_model_character_view_g_world_depth = glGetUniformLocation( _shader_model_character_view.id, "g_world_depth" ); } #endif /* SHADER_model_character_view_H */ diff --git a/shaders/model_sky.fs b/shaders/model_sky.fs index 036f244..e893799 100644 --- a/shaders/model_sky.fs +++ b/shaders/model_sky.fs @@ -1,6 +1,3 @@ -layout (location = 0) out vec4 oColour; - -uniform vec4 uColour; uniform sampler2D uTexGarbage; uniform float uTime; @@ -8,30 +5,42 @@ in vec4 aColour; in vec2 aUv; in vec3 aNorm; in vec3 aCo; +in vec3 aWorldCo; +#include "common_world.glsl" #include "motion_vectors_fs.glsl" void main() { compute_motion_vectors(); - float fintensity = 1.0-(abs(aNorm.y)*0.7); + vec3 rd = normalize(aNorm); + + float fintensity = 1.0-(abs(rd.y)*0.7); float fblend = pow(fintensity,4.0); - vec3 horizon = vec3( 0.8, 0.9, 0.9 ); - vec3 skycolour = vec3( 0.5, 0.6, 0.9 ); + vec3 horizon = vec3( 0.87, 0.93, 0.98 ); + vec3 skycolour = vec3( 0.16, 0.58, 0.95 ) - rd.y*rd.y*0.5; vec3 diffuse = mix( skycolour, horizon, fblend ); float fmove = uTime * 0.004; - vec2 cloudplane = (aNorm.xz / (aNorm.y*sign(aNorm.y))) * 0.05; + vec2 cloudplane = (rd.xz / (rd.y*sign(rd.y))) * 0.05; vec4 clouds1 = texture( uTexGarbage, cloudplane + vec2(0.1,0.4)*fmove*2.0 ); - vec4 clouds2 = texture( uTexGarbage, cloudplane + vec2(0.3,0.1)*fmove ); + vec4 clouds2 = texture( uTexGarbage, cloudplane*2.0 + vec2(0.3,0.1)*fmove ); float cloud_d = max(clouds1.b*clouds2.r -0.2 - clouds2.g*0.4,0.0); - float cloud_e = pow(cloud_d,1.5)*pow(abs(aNorm.y),0.3)*2.0; + float cloud_e = pow(cloud_d,1.5)*pow(abs(rd.y),0.3)*2.0; vec3 colour_ocean = vec3( 0.61, 0.84, 0.9 ); - float fhorizon = step( aNorm.y * 0.5 + 0.5, 0.5 ); + float fhorizon = step( rd.y * 0.5 + 0.5, 0.5 ); vec3 skycomp = mix(diffuse, vec3(1.0,1.0,1.0), cloud_e); - oColour = vec4(pow(skycomp, vec3(1.5)),1.0); + + + float sundot = clamp(dot(rd, -g_light_directions[0].xyz), 0.0, 1.0); + vec3 sun = 0.25 * vec3(1.0,0.7,0.4) * pow( sundot,5.0 ); + sun += 0.25 * vec3(1.0,0.8,0.6) * pow( sundot,64.0 ); + sun += 0.2 * vec3(1.0,0.8,0.6) * pow( sundot,512.0 ); + skycomp += sun * g_light_colours[0].rgb; + + oColour = vec4(skycomp,1.0); } diff --git a/shaders/model_sky.h b/shaders/model_sky.h index 1559bcf..2da4c32 100644 --- a/shaders/model_sky.h +++ b/shaders/model_sky.h @@ -64,9 +64,6 @@ static struct vg_shader _shader_model_sky = { .fs = { .static_src = -"layout (location = 0) out vec4 oColour;\n" -"\n" -"uniform vec4 uColour;\n" "uniform sampler2D uTexGarbage;\n" "uniform float uTime;\n" "\n" @@ -74,8 +71,132 @@ static struct vg_shader _shader_model_sky = { "in vec2 aUv;\n" "in vec3 aNorm;\n" "in vec3 aCo;\n" +"in vec3 aWorldCo;\n" "\n" "#line 1 1 \n" +"layout (location = 0) out vec4 oColour;\n" +"\n" +"layout (std140) uniform ub_world_lighting\n" +"{\n" +" vec4 g_light_colours[3];\n" +" vec4 g_light_directions[3];\n" +" vec4 g_ambient_colour;\n" +"\n" +" vec4 g_water_plane;\n" +" vec4 g_depth_bounds;\n" +" float g_water_fog;\n" +" int g_light_count;\n" +" int g_light_preview;\n" +" int g_shadow_samples;\n" +"\n" +" // g_time ?\n" +"\n" +" //vec4 g_point_light_positions[32];\n" +" //vec4 g_point_light_colours[32];\n" +"};\n" +"\n" +"uniform sampler2D g_world_depth;\n" +"\n" +"float world_depth_sample( vec3 pos )\n" +"{\n" +" vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n" +" return texture( g_world_depth, depth_coord ).r;\n" +"}\n" +"\n" +"float world_water_depth( vec3 pos )\n" +"{\n" +" vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; \n" +" float ref_depth = g_water_plane.y*g_water_plane.w;\n" +" return texture( g_world_depth, depth_coord ).g - ref_depth;\n" +"}\n" +"\n" +"float shadow_sample( vec3 vdir )\n" +"{\n" +" vec3 sample_pos = aWorldCo + vdir;\n" +" float height_sample = world_depth_sample( sample_pos );\n" +"\n" +" float fdelta = height_sample - sample_pos.y;\n" +" return clamp( fdelta, 0.1, 0.2 )-0.1;\n" +"}\n" +"\n" +"vec3 apply_fog( vec3 vfrag, float fdist )\n" +"{\n" +" float dist = pow(fdist*0.0008,1.2);\n" +" return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );\n" +"}\n" +"\n" +"\n" +"// New lighting model\n" +"\n" +"vec3 newlight_compute_ambient()\n" +"{\n" +" return g_ambient_colour.rgb;\n" +"}\n" +"\n" +"float newlight_compute_sun_shadow()\n" +"{\n" +" if( g_shadow_samples == 0 )\n" +" {\n" +" return 1.0;\n" +" }\n" +"\n" +" float fspread = g_light_colours[0].w;\n" +" vec3 vdir = g_light_directions[0].xyz;\n" +" float flength = g_light_directions[0].w;\n" +"\n" +" float famt = 0.0;\n" +" famt+=shadow_sample((vdir+vec3(-0.563, 0.550, 0.307)*fspread)*flength*0.1);\n" +" famt+=shadow_sample((vdir+vec3( 0.808, 0.686, 0.346)*fspread)*flength*0.2);\n" +" famt+=shadow_sample((vdir+vec3( 0.787, 0.074,-0.065)*fspread)*flength*0.3);\n" +" famt+=shadow_sample((vdir+vec3(-0.593, 0.071,-0.425)*fspread)*flength*0.4);\n" +" famt+=shadow_sample((vdir+vec3(-0.790,-0.933,-0.875)*fspread)*flength*0.5);\n" +" famt+=shadow_sample((vdir+vec3( 0.807,-0.690, 0.472)*fspread)*flength*0.6);\n" +" famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);\n" +" famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);\n" +"\n" +" return 1.0 - famt;\n" +"}\n" +"\n" +"vec3 newlight_compute_world_diffuse( vec3 wnormal )\n" +"{\n" +" vec3 vtotal = g_ambient_colour.rgb;\n" +"\n" +" for( int i=0; ilight_count; i++ ) + if( 0 ) { - struct world_light *light = &world->lights[i]; + for( int i=0; ilight_count; i++ ) + { + struct world_light *light = &world->lights[i]; - u32 colour = 0xff000000; - u8 r = light->colour[0] * 255.0f, - g = light->colour[1] * 255.0f, - b = light->colour[2] * 255.0f; + u32 colour = 0xff000000; + u8 r = light->colour[0] * 255.0f, + g = light->colour[1] * 255.0f, + b = light->colour[2] * 255.0f; - colour |= r; - colour |= g << 8; - colour |= b << 16; + colour |= r; + colour |= g << 8; + colour |= b << 16; - vg_line_pt3( light->co, 0.25f, colour ); + vg_line_pt3( light->co, 0.25f, colour ); + } } world_global.in_trigger = in_trigger; diff --git a/world_gen.h b/world_gen.h index 5cfc9d1..59bed34 100644 --- a/world_gen.h +++ b/world_gen.h @@ -596,6 +596,29 @@ VG_STATIC void world_post_process( world_instance *world ) vg_acquire_thread_sync(); { + /* create scene lighting buffer */ + glGenBuffers( 1, &world->tbo_light_entities ); + glBindBuffer( GL_TEXTURE_BUFFER, world->tbo_light_entities ); + glBufferData( GL_TEXTURE_BUFFER, world->light_count*sizeof(float)*8, + NULL, GL_DYNAMIC_DRAW ); + + v4f *light_dst = glMapBuffer( GL_TEXTURE_BUFFER, GL_WRITE_ONLY ); + + for( int i=0; ilight_count; i++ ) + { + struct world_light *light = &world->lights[i]; + + v3_muls( light->colour, light->colour[3] * 2.0f, light_dst[i*2+0] ); + v3_copy( light->co, light_dst[i*2+1] ); + } + + glUnmapBuffer( GL_TEXTURE_BUFFER ); + + glGenTextures( 1, &world->tex_light_entities ); + glBindTexture( GL_TEXTURE_BUFFER, world->tex_light_entities ); + glTexBuffer( GL_TEXTURE_BUFFER, GL_RGBA32F, world->tbo_light_entities ); + + /* Upload lighting uniform buffer */ if( world->water.enabled ) v4_copy( world->water.plane, world->ub_lighting.g_water_plane ); @@ -609,17 +632,6 @@ VG_STATIC void world_post_process( world_instance *world ) info_vec[3] = 1.0f/ (bounds[1][2]-bounds[0][2]); v4_copy( info_vec, world->ub_lighting.g_depth_bounds ); - /* add scene lights */ - for( int i=0; ilight_count; i++ ) - { - struct world_light *light = &world->lights[i]; - - v3_muls( light->colour, light->colour[3] * 2.0f, - world->ub_lighting.g_point_light_colours[i] ); - v3_copy( light->co, - world->ub_lighting.g_point_light_positions[i] ); - } - /* upload full buffer */ glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting ); glBufferSubData( GL_UNIFORM_BUFFER, 0, diff --git a/world_render.h b/world_render.h index 54c58a5..1926364 100644 --- a/world_render.h +++ b/world_render.h @@ -69,14 +69,27 @@ VG_STATIC void world_render_init(void) vg_release_thread_sync(); } -VG_STATIC void world_link_lighting_ub( world_instance *world, - GLuint shader, int texture_id ) +VG_STATIC void world_link_lighting_ub( world_instance *world, GLuint shader ) { GLuint idx = glGetUniformBlockIndex( shader, "ub_world_lighting" ); glUniformBlockBinding( shader, idx, world->ubo_bind_point ); +} + +VG_STATIC void world_bind_position_texture( world_instance *world, + GLuint shader, GLuint location, + int slot ) +{ + render_fb_bind_texture( &world->heightmap, 0, slot ); + glUniform1i( location, slot ); +} - render_fb_bind_texture( &world->heightmap, 0, texture_id ); - glUniform1i( glGetUniformLocation( shader, "g_world_depth" ), texture_id ); +VG_STATIC void world_bind_light_array( world_instance *world, + GLuint shader, GLuint location, + int slot ) +{ + glActiveTexture( GL_TEXTURE0 + slot ); + glBindTexture( GL_TEXTURE_BUFFER, world->tex_light_entities ); + glUniform1i( location, slot ); } VG_STATIC void render_world_depth( world_instance *world, camera *cam ); @@ -150,7 +163,12 @@ VG_STATIC void render_world_vb( world_instance *world, camera *cam ) shader_scene_vertex_blend_use(); shader_scene_vertex_blend_uTexGarbage(0); shader_scene_vertex_blend_uTexGradients(1); - world_link_lighting_ub( world, _shader_scene_vertex_blend.id, 2 ); + world_link_lighting_ub( world, _shader_scene_vertex_blend.id ); + world_bind_position_texture( world, _shader_scene_vertex_blend.id, + _uniform_scene_vertex_blend_g_world_depth, 2 ); + world_bind_light_array( world, _shader_scene_vertex_blend.id, + _uniform_scene_vertex_blend_uLightsArray, 3 ); + vg_tex2d_bind( &tex_terrain_noise, 0 ); shader_scene_vertex_blend_uPv( cam->mtx.pv ); @@ -174,7 +192,13 @@ VG_STATIC void render_world_standard( world_instance *world, camera *cam ) shader_scene_standard_uTexMain(1); shader_scene_standard_uPv( cam->mtx.pv ); shader_scene_standard_uPvmPrev( cam->mtx_prev.pv ); - world_link_lighting_ub( world, _shader_scene_standard.id, 2 ); + + world_link_lighting_ub( world, _shader_scene_standard.id ); + world_bind_position_texture( world, _shader_scene_standard.id, + _uniform_scene_standard_g_world_depth, 2 ); + world_bind_light_array( world, _shader_scene_standard.id, + _uniform_scene_standard_uLightsArray, 3 ); + bind_terrain_noise(); shader_scene_standard_uMdl( identity_matrix ); @@ -196,7 +220,14 @@ VG_STATIC void render_world_alphatest( world_instance *world, camera *cam ) shader_scene_standard_alphatest_uTexMain(1); shader_scene_standard_alphatest_uPv( cam->mtx.pv ); shader_scene_standard_alphatest_uPvmPrev( cam->mtx_prev.pv ); - world_link_lighting_ub( world, _shader_scene_standard_alphatest.id, 2 ); + + world_link_lighting_ub( world, _shader_scene_standard_alphatest.id ); + world_bind_position_texture( world, _shader_scene_standard_alphatest.id, + _uniform_scene_standard_alphatest_g_world_depth, 2 ); + world_bind_light_array( world, _shader_scene_standard_alphatest.id, + _uniform_scene_standard_alphatest_uLightsArray, 3 ); + + bind_terrain_noise(); shader_scene_standard_alphatest_uMdl( identity_matrix ); @@ -230,7 +261,12 @@ VG_STATIC void render_terrain( world_instance *world, camera *cam ) shader_scene_terrain_use(); shader_scene_terrain_uTexGarbage(0); shader_scene_terrain_uTexGradients(1); - world_link_lighting_ub( world, _shader_scene_terrain.id, 2 ); + + world_link_lighting_ub( world, _shader_scene_terrain.id ); + world_bind_position_texture( world, _shader_scene_terrain.id, + _uniform_scene_terrain_g_world_depth, 2 ); + world_bind_light_array( world, _shader_scene_terrain.id, + _uniform_scene_terrain_uLightsArray, 3 ); vg_tex2d_bind( &tex_terrain_noise, 0 ); @@ -382,7 +418,7 @@ VG_STATIC void render_world_depth( world_instance *world, camera *cam ) shader_scene_depth_uPv( cam->mtx.pv ); shader_scene_depth_uPvmPrev( cam->mtx_prev.pv ); shader_scene_depth_uMdl( identity_matrix ); - world_link_lighting_ub( world, _shader_scene_depth.id, 2 ); + world_link_lighting_ub( world, _shader_scene_depth.id ); mesh_bind( &world->mesh_geo ); mesh_draw( &world->mesh_geo ); @@ -398,7 +434,7 @@ VG_STATIC void render_world_position( world_instance *world, camera *cam ) shader_scene_position_uPv( cam->mtx.pv ); shader_scene_position_uPvmPrev( cam->mtx_prev.pv ); shader_scene_position_uMdl( identity_matrix ); - world_link_lighting_ub( world, _shader_scene_position.id, 2 ); + world_link_lighting_ub( world, _shader_scene_position.id ); mesh_bind( &world->mesh_geo ); mesh_draw( &world->mesh_geo ); diff --git a/world_routes.h b/world_routes.h index 121f447..bd4ce0c 100644 --- a/world_routes.h +++ b/world_routes.h @@ -1142,6 +1142,10 @@ VG_STATIC void world_routes_update( world_instance *world ) } VG_STATIC void bind_terrain_noise(void); +VG_STATIC void world_bind_light_array( world_instance *world, + GLuint shader, GLuint location, + int slot ); + VG_STATIC void render_world_routes( world_instance *world, camera *cam ) { m4x3f identity_matrix; @@ -1149,7 +1153,11 @@ VG_STATIC void render_world_routes( world_instance *world, camera *cam ) shader_scene_route_use(); shader_scene_route_uTexGarbage(0); - world_link_lighting_ub( world, _shader_scene_route.id, 2 ); + world_link_lighting_ub( world, _shader_scene_route.id ); + world_bind_position_texture( world, _shader_scene_route.id, + _uniform_scene_route_g_world_depth, 2 ); + world_bind_light_array( world, _shader_scene_route.id, + _uniform_scene_route_uLightsArray, 3 ); bind_terrain_noise(); shader_scene_route_uPv( cam->mtx.pv ); diff --git a/world_water.h b/world_water.h index a86d4ee..c16e2c5 100644 --- a/world_water.h +++ b/world_water.h @@ -34,8 +34,10 @@ VG_STATIC void water_set_surface( world_instance *world, float height ) v4_copy( (v4f){ 0.0f, 1.0f, 0.0f, height }, world->water.plane ); } -VG_STATIC void world_link_lighting_ub( world_instance *world, - GLuint shader, int texture_id ); +VG_STATIC void world_link_lighting_ub( world_instance *world, GLuint shader ); +VG_STATIC void world_bind_position_texture( world_instance *world, + GLuint shader, GLuint location, + int slot ); /* * Does not write motion vectors @@ -130,7 +132,9 @@ VG_STATIC void render_water_surface( world_instance *world, camera *cam ) 1.0f / (float)vg.window_x, 1.0f / (float)vg.window_y }); - world_link_lighting_ub( world, _shader_scene_water.id, 2 ); + world_link_lighting_ub( world, _shader_scene_water.id ); + world_bind_position_texture( world, _shader_scene_water.id, + _uniform_scene_water_g_world_depth, 2 ); render_fb_bind_texture( gpipeline.fb_water_beneath, 0, 3 ); shader_scene_water_uTexBack( 3 ); @@ -175,7 +179,9 @@ VG_STATIC void render_water_surface( world_instance *world, camera *cam ) shader_scene_water_fast_uTime( world_global.time ); shader_scene_water_fast_uCamera( cam->transform[3] ); shader_scene_water_fast_uSurfaceY( world->water.height ); - world_link_lighting_ub( world, _shader_scene_water_fast.id, 2 ); + world_link_lighting_ub( world, _shader_scene_water_fast.id ); + world_bind_position_texture( world, _shader_scene_water_fast.id, + _uniform_scene_water_fast_g_world_depth, 2 ); m4x3f full; m4x3_identity( full ); -- 2.25.1