Skip to content

Commit

Permalink
update finalpass to use new vertex format
Browse files Browse the repository at this point in the history
  • Loading branch information
meetric1 committed Jul 30, 2024
1 parent f04316e commit 9d2345b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
51 changes: 27 additions & 24 deletions binary/src/flex_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern IVEngineClient* engine = NULL;
//extern IMaterialSystem* materials = NULL; // stops main branch compile from bitching

#define min(a, b) a < b ? a : b
#define max(a, b) a > b ? a : b

float u[3] = { 0.5 - SQRT3 / 2, 0.5, 0.5 + SQRT3 / 2 };
float v[3] = { 1, -0.5, 1 };
Expand Down Expand Up @@ -97,22 +98,18 @@ IMesh* _build_diffuse(int id, FlexRendererThreadData data) {

// calculate triangle rotation
//Vector forward = (eye_pos - particle_pos).Normalized();
Vector forward = (particle_pos - data.eye_pos).Normalized();
Vector right = forward.Cross(Vector(0, 0, 1)).Normalized();
Vector up = right.Cross(forward);
Vector local_pos[3] = { (-up - right * SQRT3), up * 2.0, (-up + right * SQRT3) };

Vector4D ani0 = data.particle_ani0[particle_index];
float scalar = data.radius * data.particle_positions[particle_index].w;

for (int i = 0; i < 3; i++) {
Vector pos_ani = local_pos[i]; // Warp based on velocity
pos_ani = pos_ani + (data.particle_ani0[particle_index].AsVector3D() * pos_ani.Dot(data.particle_ani0[particle_index].AsVector3D()) * 0.0016).Min(Vector(3, 3, 3)).Max(Vector(-3, -3, -3));
//Vector pos_ani = local_pos[i]; // Warp based on velocity
//pos_ani = pos_ani + (data.particle_ani0[particle_index].AsVector3D() * pos_ani.Dot(data.particle_ani0[particle_index].AsVector3D()) * 0.0016).Min(Vector(3, 3, 3)).Max(Vector(-3, -3, -3));

Vector world_pos = particle_pos + pos_ani * scalar;
//Vector world_pos = particle_pos + pos_ani * scalar;
mesh_builder.TexCoord2f(0, u[i], v[i]);
mesh_builder.Position3f(world_pos.x, world_pos.y, world_pos.z);
mesh_builder.Normal3f(-forward.x, -forward.y, -forward.z);
//mesh_builder.Position3f(world_pos.x, world_pos.y, world_pos.z);
//mesh_builder.Normal3f(-forward.x, -forward.y, -forward.z);
mesh_builder.AdvanceVertex();
}
}
Expand All @@ -125,8 +122,9 @@ IMesh* _build_diffuse(int id, FlexRendererThreadData data) {

// Launches 1 thread for each mesh. particles are split into meshes with MAX_PRIMATIVES number of primatives
void FlexRenderer::build_meshes(FlexSolver* flex, float diffuse_radius) {
update_meshes();
// Clear previous imeshes since they are being rebuilt
destroy_meshes();
//destroy_meshes();

int max_particles = flex->get_active_particles();
if (max_particles == 0) return;
Expand All @@ -139,28 +137,24 @@ void FlexRenderer::build_meshes(FlexSolver* flex, float diffuse_radius) {
render_context->GetMatrix(MATERIAL_PROJECTION, &projection_matrix);
MatrixMultiply(projection_matrix, view_matrix, view_projection_matrix);

// Get eye position for sprite calculations
Vector eye_pos; render_context->GetWorldSpaceCameraPosition(&eye_pos);

Vector4D* particle_positions = flex->get_parameter("smoothing") != 0 ? (Vector4D*)flex->get_host("particle_smooth") : (Vector4D*)flex->get_host("particle_pos");
Vector4D* particle_ani0 = (Vector4D*)flex->get_host("particle_ani0");
Vector4D* particle_ani1 = (Vector4D*)flex->get_host("particle_ani1");
Vector4D* particle_ani2 = (Vector4D*)flex->get_host("particle_ani2");
bool particle_ani = flex->get_parameter("anisotropy_scale") != 0; // Should we do anisotropy calculations?
float radius = flex->get_parameter("radius");

// thread data
FlexRendererThreadData data;
data.eye_pos = eye_pos;
data.view_projection_matrix = view_projection_matrix;
data.particle_positions = particle_positions;
data.max_particles = max_particles;
data.radius = radius;
data.render_buffer = water_buffer;
if (flex->get_parameter("anisotropy_scale") != 0) { // Should we do anisotropy calculations?
data.particle_ani0 = particle_ani0;
data.particle_ani1 = particle_ani1;
data.particle_ani2 = particle_ani2;
data.particle_ani0 = (Vector4D*)flex->get_host("particle_ani0");
data.particle_ani1 = (Vector4D*)flex->get_host("particle_ani1");
data.particle_ani2 = (Vector4D*)flex->get_host("particle_ani2");;
} else {
data.particle_ani0 = nullptr;
data.particle_ani1 = nullptr;
data.particle_ani2 = nullptr;
}

int max_meshes = min(ceil(max_particles / (float)MAX_PRIMATIVES), allocated);
Expand All @@ -169,6 +163,14 @@ void FlexRenderer::build_meshes(FlexSolver* flex, float diffuse_radius) {
queue[mesh_index] = threads->enqueue(_build_water_anisotropy, mesh_index, data);
}

// Remove meshes which wont be built
for (int mesh = max_meshes; mesh < allocated; mesh++) {
if (meshes[mesh] == nullptr) continue;

render_context->DestroyStaticMesh(meshes[mesh]);
meshes[mesh] = nullptr;
}
/*
// Diffuse particles
max_particles = flex->get_active_diffuse();
if (max_particles == 0) return;
Expand All @@ -191,7 +193,7 @@ void FlexRenderer::build_meshes(FlexSolver* flex, float diffuse_radius) {
// Launch thread
queue[mesh_index + allocated] = threads->enqueue(_build_diffuse, mesh_index, data);
}
}*/
};

void FlexRenderer::update_meshes() {
Expand All @@ -205,7 +207,7 @@ void FlexRenderer::update_meshes() {
}

void FlexRenderer::draw_water() {
update_meshes(); // Update status of water meshes (join threads)
//update_meshes(); // Update status of water meshes (join threads)

IMatRenderContext* render_context = materials->GetRenderContext();
for (int mesh = 0; mesh < allocated; mesh++) {
Expand All @@ -216,14 +218,15 @@ void FlexRenderer::draw_water() {
};

void FlexRenderer::draw_diffuse() {
/*
update_meshes();
IMatRenderContext* render_context = materials->GetRenderContext();
for (int mesh = allocated; mesh < allocated * 2; mesh++) {
if (meshes[mesh] == nullptr) continue;
meshes[mesh]->Draw();
}
}*/
};

void FlexRenderer::destroy_meshes() {
Expand Down
1 change: 0 additions & 1 deletion binary/src/flex_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

struct FlexRendererThreadData {
//IMesh*& water;
Vector eye_pos;
VMatrix view_projection_matrix;
Vector4D* particle_positions;
Vector4D* particle_ani0;
Expand Down
4 changes: 2 additions & 2 deletions binary/src/shaders/GWaterFinalpass.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ SHADER_DRAW {
bool bHasFlashlight = UsingFlashlight(params);
SHADOW_STATE {
// Note: Removing VERTEX_COLOR makes the shader work on all objects (Like props)
unsigned int flags = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_TEXCOORD0_2D;
pShaderShadow->VertexShaderVertexFormat(flags, 1, 0, 0);
pShaderShadow->VertexShaderVertexFormat(VERTEX_GWATER2, 1, 0, 0);
pShaderShadow->EnableTexture(SHADER_SAMPLER0, true); // Smoothed normals texture
pShaderShadow->EnableTexture(SHADER_SAMPLER1, true); // Screen texture
pShaderShadow->EnableTexture(SHADER_SAMPLER2, true); // Cubemap
Expand Down Expand Up @@ -132,6 +131,7 @@ SHADER_DRAW {

pShaderAPI->SetPixelShaderConstant(0, scr_s);
pShaderAPI->SetPixelShaderConstant(1, &radius);
pShaderAPI->SetVertexShaderConstant(5, &radius);
pShaderAPI->SetPixelShaderConstant(2, &ior);
pShaderAPI->SetPixelShaderConstant(3, &reflectance);
pShaderAPI->SetPixelShaderConstant(12, color2_normalized); // used to be 4, but that was overlapping with ambient cube.
Expand Down
8 changes: 2 additions & 6 deletions binary/src/shaders/GWaterNormals.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ SHADER_FALLBACK{

SHADER_DRAW {
SHADOW_STATE {

// Note: Removing VERTEX_COLOR makes the shader work on all objects (Like props)
unsigned int flags = VERTEX_GWATER2;

pShaderShadow->VertexShaderVertexFormat(flags, 1, 0, 0);
pShaderShadow->VertexShaderVertexFormat(VERTEX_GWATER2, 1, 0, 0);
//pShaderShadow->EnableAlphaWrites(true);

DECLARE_STATIC_VERTEX_SHADER(GWaterNormals_vs30);
SET_STATIC_VERTEX_SHADER(GWaterNormals_vs30);
Expand All @@ -53,7 +50,6 @@ SHADER_DRAW {
DECLARE_DYNAMIC_PIXEL_SHADER(GWaterNormals_ps30);
SET_DYNAMIC_PIXEL_SHADER_COMBO(DEPTH, depthfix);
SET_DYNAMIC_PIXEL_SHADER(GWaterNormals_ps30);


}

Expand Down
8 changes: 5 additions & 3 deletions lua/autorun/gwater2_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,10 @@ local function gwater_tick2()

gwater2.solver:IterateMeshes(gwater2.update_meshes)
hook.Run("gwater2_pretick")
if gwater2.solver:Tick(FrameTime(), 0) then
gwater2.renderer:BuildMeshes(gwater2.solver, 0.15)

gwater2.renderer:BuildMeshes(gwater2.solver, 0.15)
if gwater2.solver:Tick(limit_fps, 0) then

end
end

Expand All @@ -186,7 +188,7 @@ hook.Add("Think", "gwater_tick", function()
gwater2.solver:IterateMeshes(gwater2.update_meshes)
end)

timer.Create("gwater2_tick", 0, 0, function()
timer.Create("gwater2_tick", limit_fps, 0, function()
if !gwater2.new_ticker then return end
gwater_tick2()
end)
Expand Down
10 changes: 5 additions & 5 deletions lua/gwater2_shaders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ hook.Add("PostDrawOpaqueRenderables", "gwater2_render", function(depth, sky, sky
end

-- grab normals
water_normals:SetFloat("$radius", radius * 0.5)
water_normals:SetFloat("$radius", radius)
render.SetMaterial(water_normals)
--render.PushRenderTarget(cache_normals)
render.PushRenderTarget(cache_normals)
render.SetRenderTargetEx(1, cache_depth)
render.ClearDepth()
gwater2.renderer:DrawWater()
--render.PopRenderTarget()
render.PopRenderTarget()
render.SetRenderTargetEx(1, nil)
/*

-- Blur normals
water_blur:SetFloat("$radius", radius)
water_blur:SetTexture("$depthtexture", cache_depth)
Expand Down Expand Up @@ -165,7 +165,7 @@ hook.Add("PostDrawOpaqueRenderables", "gwater2_render", function(depth, sky, sky
render.OverrideAlphaWriteEnable(false, false)

render.SetMaterial(water_mist)
gwater2.renderer:DrawDiffuse()*/
--gwater2.renderer:DrawDiffuse()

-- Debug Draw
local dbg = 0
Expand Down
2 changes: 1 addition & 1 deletion materials/gwater2/finalpass.vmt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ GWaterFinalpass {
$envmap "env_cubemap"
$radius 10
$ior 1.333
$reflectance 0.63
$reflectance 0.8
$screentexture "_rt_fullframefb"
$color2 "[209 237 255 25]"
$flashlight 1
Expand Down

0 comments on commit 9d2345b

Please sign in to comment.