Skip to content

Commit

Permalink
Got the grass shader workin B)
Browse files Browse the repository at this point in the history
  • Loading branch information
EPICGameGuy committed Dec 13, 2023
1 parent d571f28 commit 5b1a6a2
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 39 deletions.
5 changes: 4 additions & 1 deletion assets/models/kettle.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"mesh_file": "kettle.fbx",
"primitive_type": 2147483649
"primitive_type": 2147483649,
"modifiers": [
"gamma_correct_alpha"
]
}
5 changes: 4 additions & 1 deletion assets/models/plane.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"mesh_file": "plane.fbx",
"primitive_type": 2147483649
"primitive_type": 2147483649,
"modifiers": [
"gamma_correct_alpha"
]
}
Binary file modified assets/models/plant.fbx
Binary file not shown.
6 changes: 5 additions & 1 deletion assets/models/plant.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"mesh_file": "plant.fbx",
"primitive_type": 2147483650
"primitive_type": 2147483650,
"modifiers": [
"vegetation",
"gamma_correct_alpha"
]
}
5 changes: 4 additions & 1 deletion assets/models/testmap.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"mesh_file": "testmap.fbx",
"primitive_type": 2147483649
"primitive_type": 2147483649,
"modifiers": [
"gamma_correct_alpha"
]
}
4 changes: 2 additions & 2 deletions src/renderer/ps2gl_renderers/vertex_color_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ void CVertexColorRenderer::InitContext(GLenum primType, tU32 rcChanges, bool use

packet.Pad96();

packet.OpenUnpack(Vifs::UnpackModes::s_32, kTime, Packet::kSingleBuff);
packet += 0.f;
packet.OpenUnpack(Vifs::UnpackModes::s_32, kIsVegetation, Packet::kSingleBuff);
packet += (s32)0;
packet.CloseUnpack(1);

packet.Mscal(0);
Expand Down
18 changes: 16 additions & 2 deletions src/renderer/ps2gl_renderers/vertex_color_vegetation_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ CVertexColorVegetationRenderer* CVertexColorVegetationRenderer::Register()
return renderer;
}

static float get_veg_p(float offset)
{
return sinf(Engine::get_game_time() + offset);
}

void CVertexColorVegetationRenderer::InitContext(GLenum primType, tU32 rcChanges, bool userRcChanged)
{
primType = GL_TRIANGLE_STRIP;
Expand All @@ -79,10 +84,19 @@ void CVertexColorVegetationRenderer::InitContext(GLenum primType, tU32 rcChanges

packet.Pad96();

packet.OpenUnpack(Vifs::UnpackModes::s_32, kTime, Packet::kSingleBuff);
packet += (float)(fmodf(Engine::get_game_time(), (M_PI * 2)) - M_PI);
packet.OpenUnpack(Vifs::UnpackModes::s_32, kIsVegetation, Packet::kSingleBuff);
packet += (s32)1;
packet.CloseUnpack(1);

packet.OpenUnpack(Vifs::UnpackModes::s_32, kVegetationParams, Packet::kSingleBuff);
packet += get_veg_p(0.f);
packet += get_veg_p(M_PI / 6.f);
packet += get_veg_p((2.f * M_PI) / 6.f);
packet += get_veg_p((3.f * M_PI) / 6.f);
packet += get_veg_p((4.f * M_PI) / 6.f);
packet += get_veg_p((5.f * M_PI) / 6.f);
packet.CloseUnpack(6);

packet.Mscal(0);
packet.Flushe();

Expand Down
2 changes: 1 addition & 1 deletion tools/ps2-mesh-converter/include/model_modifiers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
#include <vector>
#include "types.hpp"

void apply_gamma_correction(std::vector<Mesh>& meshes);
bool apply_modification(const std::string& mod, Mesh& mesh);
2 changes: 2 additions & 0 deletions tools/ps2-mesh-converter/include/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ struct Mesh
uint32_t primitive_type;
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;

std::vector<std::string> modifiers;
};

static bool isMeshValid(const Mesh& mesh)
Expand Down
9 changes: 9 additions & 0 deletions tools/ps2-mesh-converter/src/json_importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,14 @@ bool parseJson(std::string_view path, std::vector<Mesh>& meshes)
meshes[0].primitive_type = obj["primitive_type"].asUInt();
}

meshes[0].modifiers.clear();
if (obj.isMember("modifiers"))
{
for (auto val : obj["modifiers"])
{
meshes[0].modifiers.push_back(val.asString());
}
}

return true;
}
8 changes: 4 additions & 4 deletions tools/ps2-mesh-converter/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ static void process()

printf("Loaded %lu meshes from file\n", meshes.size());

printf(ANSI_COLOR_MAGENTA "[PS2-Mesh-Converter]: Applying modifications\n" ANSI_COLOR_RESET);
apply_gamma_correction(meshes);

std::vector<MeshStrip> strips;
for (size_t i = 0; i < meshes.size(); ++i)
{
//printf("Mesh %lu: Num verts (before stripping): %lu, num tris: %lu\n", i, meshes[i].vertices.size(), meshes[i].indices.size() / 3);
for (const std::string& modifier : meshes[i].modifiers)
{
apply_modification(modifier, meshes[i]);
}

// Stripify it
std::vector<unsigned int> strip = stripify(meshes[i], false, 'S');
Expand Down
47 changes: 38 additions & 9 deletions tools/ps2-mesh-converter/src/model_modifiers.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
#include "model_modifiers.hpp"
#include "utils.hpp"

void apply_gamma_correction(std::vector<Mesh>& meshes)
#include <cmath>

static void apply_gamma_correction(Mesh& mesh)
{
for (Vertex& vertex : mesh.vertices)
{
//vertex.r = pow(vertex.r, 1.0 / 2.2);
//vertex.g = pow(vertex.g, 1.0 / 2.2);
//vertex.b = pow(vertex.b, 1.0 / 2.2);
vertex.a = pow(vertex.a, 1.0 / 2.2);
}
}

// Converts the red channel to an integer
static void apply_vegetation_mod(Mesh& mesh)
{
for (Vertex& vertex : mesh.vertices)
{
int32_t red_value = (int)std::round(vertex.r * 255.f);
vertex.r = *(reinterpret_cast<float*>(&red_value));
}
}

bool apply_modification(const std::string& mod, Mesh& mesh)
{
for (Mesh& mesh : meshes)
if (mod == "vegetation")
{
for (Vertex& vertex : mesh.vertices)
{
//vertex.r = pow(vertex.r, 1.0 / 2.2);
//vertex.g = pow(vertex.g, 1.0 / 2.2);
//vertex.b = pow(vertex.b, 1.0 / 2.2);
vertex.a = pow(vertex.a, 1.0 / 2.2);
}
printf(ANSI_COLOR_MAGENTA "[PS2-Mesh-Converter]: Applying mesh modification: %s\n" ANSI_COLOR_RESET, "vegetation");
apply_vegetation_mod(mesh);
return true;
}
else if (mod == "gamma_correct_alpha")
{
printf(ANSI_COLOR_MAGENTA "[PS2-Mesh-Converter]: Applying mesh modification: %s\n" ANSI_COLOR_RESET, "gamma correction");
apply_gamma_correction(mesh);
return true;
}

printf(ANSI_COLOR_RED "[PS2-Mesh-Converter]: Unable to find mesh mod: %s\n" ANSI_COLOR_RESET, mod.c_str());
return false;
}
27 changes: 12 additions & 15 deletions vu1/vertex_color_renderer.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ main_loop_lid:

set_strip_adcs

; const_color = material emissive + global ambient
get_cnst_color const_color

init_bfc_strip

xform_loop_lid: --LoopCS 1,3
Expand All @@ -57,7 +54,7 @@ xform_loop_lid: --LoopCS 1,3

; Vert colors
load_pvcolor vert_color
move.xyzw vert_color_old, vert_color
move.xyzw vert_color_old, vert_color
;move.xyzw vert_color, vf00
;maxw.xyzw vert_color, vf00, vert_color_old[w] ; w is AO

Expand All @@ -67,24 +64,24 @@ xform_loop_lid: --LoopCS 1,3
muli.xyzw vert_color, vert_color, i
store_rgba vert_color

; Wind
move.xyzw time, vf00
lq.xyzw time, kTime(vi00)
; Skip the vegetation stuff if this isn't rendering vegetation
ilw.x is_vegetation, kIsVegetation(vi00)
ibeq is_vegetation, vi00, project_lid
; Add the time offset from the vert color
; and calculate the sin value
loi 3.14159
muli.x vert_color_old, vert_color_old, i
; Wind
; Lookup the leaf index from the vertex color
ilw.x leaf_index, 3(next_input)
; Add a per-leaf time offset from the red channel
addx.xyzw time, time, vert_color_old[x]
esin p, time.y
mfp.xyzw time, p
; Lookup the sine(time) from leaf index
move.xyzw time, vf00
lq.xyzw time, kVegetationParams(leaf_index)
; Modulate leaf wind by the blue channel of the vertex color
mulz.xyzw time, time, vert_color_old[z]
add.y vert, vert, time
project_lid:
xform_vert xformed_vert, vert_xform, vert
vert_to_gs gs_vert, xformed_vert
Expand Down
Binary file modified vu1/vertex_color_renderer.vo
Binary file not shown.
6 changes: 4 additions & 2 deletions vu1/vertex_color_renderer_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ typedef struct

#define kClipInfo (kGifTag + 1)

#define kTime (kClipInfo + 1)
#define kIsVegetation (kClipInfo + 1)

#define kContextLength (kTime - kContextStart + 1)
#define kVegetationParams (kIsVegetation + 1)

#define kContextLength (kVegetationParams - kContextStart + 6)

0 comments on commit 5b1a6a2

Please sign in to comment.