Skip to content

Commit

Permalink
Merge pull request #10 from Tokeiburu/lub-rsm-fix
Browse files Browse the repository at this point in the history
More fixes for lub rendering
  • Loading branch information
Borf committed Dec 6, 2023
2 parents 8b08e52 + be896d8 commit f08e433
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 66 deletions.
41 changes: 25 additions & 16 deletions browedit/components/LubRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ void LubRenderer::render()
modelMatrix = glm::rotate(modelMatrix, -glm::radians(lubEffect->rotate_angle.x), glm::vec3(1, 0, 0));
}

shader->setUniform(LubShader::Uniforms::modelMatrix, modelMatrix);
shader->setUniform(LubShader::Uniforms::billboard_off, (bool)lubEffect->billboard_off);
shader->setUniform(LubShader::Uniforms::color, lubEffect->color);
shader->setUniform(LubShader::Uniforms::color, glm::vec4(lubEffect->color.r, lubEffect->color.g, lubEffect->color.b, 1.0f));

glBindBuffer(GL_ARRAY_BUFFER, 0);
if (texture)
Expand All @@ -87,17 +86,17 @@ void LubRenderer::render()
p.position += (p.dir * lubEffect->speed + p.speed) * elapsedTime;
p.life -= elapsedTime;
p.speed += lubEffect->speed * lubEffect->gravity * glm::vec3(1, -1, 1) * elapsedTime;

if (p.start_life > 1) {
if (p.life < p.start_life / 2)
if (p.life < glm::min(1.0f, p.start_life / 2.0f))
p.alpha -= elapsedTime;
else
p.alpha += elapsedTime;

p.alpha = glm::clamp(p.alpha, 0.0f, 1.0f);
}
}
if(particles.size() > 0)
if (particles.size() > 0)
particles.erase(std::remove_if(particles.begin(), particles.end(), [](const Particle& p) { return p.life < 0; }), particles.end());
emitTime -= elapsedTime;
if (emitTime < 0 && particles.size() < lubEffect->maxcount)
Expand All @@ -108,6 +107,7 @@ void LubRenderer::render()
-lubEffect->radius.y + (rand() / (float)RAND_MAX) * (-lubEffect->radius.y + 2 * abs(lubEffect->radius.y) + lubEffect->radius.y),
-lubEffect->radius.z + (rand() / (float)RAND_MAX) * (-lubEffect->radius.z + 2 * abs(lubEffect->radius.z) + lubEffect->radius.z)
);
p.speed = glm::vec3(0);
p.dir = glm::vec3(
lubEffect->dir1.x + (rand() / (float)RAND_MAX) * (lubEffect->dir2.x - lubEffect->dir1.x),
-lubEffect->dir1.y + (rand() / (float)RAND_MAX) * (-lubEffect->dir2.y + lubEffect->dir1.y),
Expand All @@ -122,36 +122,45 @@ void LubRenderer::render()
emitTime = 1.0f / (lubEffect->rate.x + (rand() / (float)RAND_MAX) * (lubEffect->rate.y - lubEffect->rate.x));
}


if (lubEffect->zenable)
glEnable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
int src = d3dToOpenGlBlend(lubEffect->srcmode);
int dst = d3dToOpenGlBlend(lubEffect->destmode);
glBlendFuncSeparate(src, dst, GL_ONE, GL_ONE);
glBlendFunc(src, dst);
glDepthMask(0);

std::vector<VertexP3T2A1> verts;
for (const auto& p : particles)
{
float alpha = p.alpha;
//float alpha = 1.0;
verts.push_back(VertexP3T2A1(p.position + glm::vec3(-p.size, -p.size, 0), glm::vec2(0, 0), alpha));
verts.push_back(VertexP3T2A1(p.position + glm::vec3(-p.size, p.size, 0), glm::vec2(0, 1), alpha));
verts.push_back(VertexP3T2A1(p.position + glm::vec3(p.size, p.size, 0), glm::vec2(1, 1), alpha));
verts.push_back(VertexP3T2A1(p.position + glm::vec3(p.size, -p.size, 0), glm::vec2(1, 0), alpha));
verts.push_back(VertexP3T2A1(glm::vec3(-p.size, -p.size, 0), glm::vec2(0, 0), alpha));
verts.push_back(VertexP3T2A1(glm::vec3(-p.size, p.size, 0), glm::vec2(0, 1), alpha));
verts.push_back(VertexP3T2A1(glm::vec3(p.size, p.size, 0), glm::vec2(1, 1), alpha));
verts.push_back(VertexP3T2A1(glm::vec3(p.size, -p.size, 0), glm::vec2(1, 0), alpha));
}


if (verts.size() > 0)
{

glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(VertexP3T2A1), verts[0].data);
glVertexAttribPointer(1, 2, GL_FLOAT, false, sizeof(VertexP3T2A1), verts[0].data + 3);
glVertexAttribPointer(2, 1, GL_FLOAT, false, sizeof(VertexP3T2A1), verts[0].data + 5);
glDrawArrays(GL_QUADS, 0, (int)verts.size());
}

for (int i = 0; i < particles.size(); i++)
{
Particle p = particles[i];
glm::mat4 modelMatrixSub = modelMatrix;
modelMatrixSub[3] += glm::vec4(p.position.x, p.position.y, -p.position.z, 0.0f);
shader->setUniform(LubShader::Uniforms::modelMatrix, modelMatrixSub);
glDrawArrays(GL_QUADS, 4 * i, 4);
}

glDepthMask(1);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_DEPTH_TEST);
}


Expand Down
43 changes: 14 additions & 29 deletions browedit/components/Rsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,18 +584,13 @@ void Rsm::Mesh::calcMatrix1(int time)
if (scaleFrames.size() > 0) {
int tick = time % glm::max(1, model->animLen);
int prevIndex = -1;
int nextIndex = -1;
int nextIndex = 0;

while (true) {
nextIndex++;

if (nextIndex == scaleFrames.size() || tick < scaleFrames[nextIndex].time)
break;

prevIndex++;
for (; nextIndex < scaleFrames.size() && tick >= scaleFrames[nextIndex].time; nextIndex++) {
}

float prevTick = (float)(prevIndex < 0 ? 0 : scaleFrames[prevIndex].time); //TODO: this part is repeated for rot,scale,pos... dedupe code
prevIndex = nextIndex - 1;
float prevTick = (float)(prevIndex < 0 ? 0 : scaleFrames[prevIndex].time);
float nextTick = (float)(nextIndex == scaleFrames.size() ? model->animLen : scaleFrames[nextIndex].time);
glm::vec3 prev = prevIndex < 0 ? glm::vec3(1) : scaleFrames[prevIndex].scale;
glm::vec3 next = nextIndex == scaleFrames.size() ? scaleFrames[nextIndex - 1].scale : scaleFrames[nextIndex].scale;
Expand All @@ -608,22 +603,17 @@ void Rsm::Mesh::calcMatrix1(int time)
{
int tick = time % glm::max(1, model->animLen);
int prevIndex = -1;
int nextIndex = -1;

while (true) {
nextIndex++;

if (nextIndex == rotFrames.size() || tick < rotFrames[nextIndex].time)
break;

prevIndex++;
int nextIndex = 0;

for (; nextIndex < rotFrames.size() && tick >= rotFrames[nextIndex].time; nextIndex++) {
}


prevIndex = nextIndex - 1;
float prevTick = (float)(prevIndex < 0 ? 0 : rotFrames[prevIndex].time);
float nextTick = (float)(nextIndex == rotFrames.size() ? model->animLen : rotFrames[nextIndex].time);
glm::quat prev = prevIndex < 0 ? glm::quat() : rotFrames[prevIndex].quaternion;
glm::quat prev = prevIndex < 0 ? glm::quat(1, 0, 0, 0) : rotFrames[prevIndex].quaternion;
glm::quat next = nextIndex == rotFrames.size() ? rotFrames[nextIndex - 1].quaternion : rotFrames[nextIndex].quaternion;

float mult = (tick - prevTick) / (nextTick - prevTick);
glm::quat quat = glm::slerp(prev, next, mult);
matrix1 = glm::toMat4(quat) * matrix1;
Expand All @@ -644,17 +634,12 @@ void Rsm::Mesh::calcMatrix1(int time)
if (posFrames.size() > 0) {
int tick = time % glm::max(1, model->animLen);
int prevIndex = -1;
int nextIndex = -1;

while (true) {
nextIndex++;

if (nextIndex == posFrames.size() || tick < posFrames[nextIndex].time)
break;
int nextIndex = 0;

prevIndex++;
for (; nextIndex < posFrames.size() && tick >= posFrames[nextIndex].time; nextIndex++) {
}

prevIndex = nextIndex - 1;
float prevTick = (float)(prevIndex < 0 ? 0 : posFrames[prevIndex].time);
float nextTick = (float)(nextIndex == posFrames.size() ? model->animLen : posFrames[nextIndex].time);
glm::vec3 prev = prevIndex < 0 ? pos_ : posFrames[prevIndex].position;
Expand Down
2 changes: 1 addition & 1 deletion browedit/components/RsmRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void RsmRenderer::renderMesh(Rsm::Mesh* mesh, const glm::mat4& matrix, bool sele
textures.push_back(util::ResourceManager<gl::Texture>::load("data\\texture\\" + textureFilename));
}

if (mesh && (!mesh->rotFrames.empty() || mesh->matrixDirty || !mesh->scaleFrames.empty() || !mesh->posFrames.empty()))
if (mesh && (!mesh->rotFrames.empty() || mesh->matrixDirty || mesh->model->version >= 0x202))
{
mesh->matrixDirty = false;

Expand Down
1 change: 1 addition & 0 deletions browedit/components/Rsw.Effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ void LubEffect::load(const json& data)
srcmode = data["srcmode"][0];
destmode = data["destmode"][0];
maxcount = data["maxcount"][0];
zenable = data["zenable"][0];

if (data.find("billboard_off") != data.end())
billboard_off = data["billboard_off"][0];
Expand Down
25 changes: 11 additions & 14 deletions browedit/components/Rsw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void Rsw::load(const std::string& fileName, Map* map, BrowEdit* browEdit, bool l
quadtree = nullptr;

json lubInfo = json::array();
std::map<int, json> lubInfoMap;

std::string mapName = fileName;
mapName = mapName.substr(0, mapName.size() - 4);
Expand Down Expand Up @@ -139,20 +140,12 @@ void Rsw::load(const std::string& fileName, Map* map, BrowEdit* browEdit, bool l
std::replace(data.begin(), data.end(), ']', '\"');
data = util::replace(data, "\"\"", "");
std::replace(data.begin(), data.end(), '=', ':');
data[0] = '[';
auto lines = util::split(data, "\n");
bool done = false;
for (auto i = 0; i < lines.size(); i++)
{
if (lines[i].find("--") != std::string::npos)
lines[i] = util::rtrim(lines[i].substr(0, lines[i].find("--")));//remove comments
if (lines[i].size() > 2 && lines[i][0] == '\t' && lines[i][1] == '\"')
{
if (lines[i].find("{") != std::string::npos)
lines[i] = lines[i].substr(lines[i].find(":") + 2);
else
lines[i] = "";
}
if (lines[i].size() > 3 && lines[i][0] == '\t' && lines[i][1] == '\t' && lines[i][2] != '\t')
{
lines[i] = "\t\t\"" + lines[i].substr(2, lines[i].find(" ") - 2) + "\"" + lines[i].substr(lines[i].find(" ") + 1);
Expand All @@ -174,11 +167,14 @@ void Rsw::load(const std::string& fileName, Map* map, BrowEdit* browEdit, bool l
jsondata = util::replace(jsondata, "\n\n", "\n");

jsondata = util::trim(jsondata);
jsondata[jsondata.length() - 1] = ']';

try
{
lubInfo = json::parse(jsondata);

for (auto& it : lubInfo.items()) {
lubInfoMap[atoi(it.key().c_str())] = it.value();
}
}
catch (const std::exception& e)
{
Expand Down Expand Up @@ -384,8 +380,8 @@ void Rsw::load(const std::string& fileName, Map* map, BrowEdit* browEdit, bool l
if (rswEffect && rswEffect->id == 974)
{
auto lubEffect = new LubEffect();
if (lubIndex <= lubInfo.size())
lubEffect->load(lubInfo[lubIndex]);
if (lubInfoMap.find(lubIndex) != lubInfoMap.end())
lubEffect->load(lubInfoMap[lubIndex]);
object->addComponent(lubEffect);
object->addComponent(new LubRenderer());
lubIndex++;
Expand Down Expand Up @@ -596,15 +592,15 @@ void Rsw::save(const std::string& fileName, BrowEdit* browEdit)
{
std::cout << "Lub effects found, saving to " << browEdit->config.ropath << "data\\luafiles514\\lua files\\effecttool\\" << mapName << ".lub" << std::endl;
std::ofstream lubFile((browEdit->config.ropath + "data\\luafiles514\\lua files\\effecttool\\" + mapName + ".lub").c_str(), std::ios_base::out | std::ios_base::binary);
lubFile << "_" << luaMapName << "_emitterInfo_version = "<<lubVersion<<".0" << std::endl;
lubFile << "_" << luaMapName << "_effect_version = "<<lubVersion<<".0" << std::endl;
lubFile << "_" << luaMapName << "_emitterInfo =" << std::endl;
lubFile << "{" << std::endl;

#define SAVEPROP0(x,y) lubFile<<"\t\t[\""<<x<<"\"] = { "<<y<<" }"
#define SAVEPROP2(x,y) lubFile<<"\t\t[\""<<x<<"\"] = { "<<y[0]<<", "<<y[1]<<" }"
#define SAVEPROP3(x,y) lubFile<<"\t\t[\""<<x<<"\"] = { "<<y[0]<<", "<<y[1]<<", "<<y[2]<<" }"
#define SAVEPROP4(x,y) lubFile<<"\t\t[\""<<x<<"\"] = { "<<y[0]<<", "<<y[1]<<", "<<y[2]<<", "<<y[3]<<" }"
#define SAVEPROPS(x,y) lubFile<<"\t\t[\""<<x<<"\"] = [["<<y<<"]]"
#define SAVEPROPS(x,y) lubFile<<"\t\t[\""<<x<<"\"] = \""<<y<<"\""

for (auto i = 0; i < lubEffects.size(); i++)
{
Expand All @@ -624,7 +620,8 @@ void Rsw::save(const std::string& fileName, BrowEdit* browEdit)
SAVEPROP0("srcmode", lubEffects[i]->srcmode) << "," << std::endl;
SAVEPROP0("destmode", lubEffects[i]->destmode) << "," << std::endl;
SAVEPROP0("maxcount", lubEffects[i]->maxcount) << "," << std::endl;
SAVEPROP0("zenable", lubEffects[i]->zenable) << "," <<std::endl;
SAVEPROP0("zenable", lubEffects[i]->zenable) << "," << std::endl;
SAVEPROP0("billboard_off", lubEffects[i]->billboard_off) << "," << std::endl;
SAVEPROP3("rotate_angle", lubEffects[i]->rotate_angle) << std::endl;


Expand Down
3 changes: 2 additions & 1 deletion data/shaders/gnd.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ void main()
if(texture.a < 0.1)
discard;

texture.rgb *= max((max(0.0, dot(normal, vec3(1,-1,1)*lightDirection)) * lightDiffuse + lightIntensity * lightAmbient), lightToggle);
float NL = clamp(dot(normalize(normal), vec3(1,-1,1)*lightDirection),0.0,1.0);
texture.rgb *= max((NL * min(lightDiffuse, 1.0 - lightAmbient) * lightDiffuse + lightAmbient), lightToggle);
texture.rgb *= max(color, colorToggle).rgb;
texture.rgb *= max(texture2D(s_lighting, texCoord2).a, shadowMapToggle);
texture.rgb += clamp(texture2D(s_lighting, texCoord2).rgb, 0.0, 1.0) * lightColorToggle;
Expand Down
2 changes: 0 additions & 2 deletions data/shaders/lub.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ void main()
vec4 outColor = texture2D(s_texture, texCoord);
outColor *= color;
outColor.a *= alpha;
//if(outColor.a < 0.1)
// discard;
fragColor = outColor;
}
7 changes: 4 additions & 3 deletions data/shaders/rsm.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ void main()
discard;

if (lightToggle) {
// lightIntensity is ignored by the client for whatever reason
if (shadeType == 0)
color.rgb *= min(lightDiffuse, 1.0 - lightAmbient) * lightDiffuse + lightAmbient;
else if (shadeType == 1 || shadeType == 2)
color.rgb *= clamp(dot(normalize(normal), lightDirection),0.0,1.0) * lightDiffuse + lightAmbient;
else if (shadeType == 1 || shadeType == 2) {
float NL = clamp(dot(normalize(normal), lightDirection),0.0,1.0);
color.rgb *= NL * min(lightDiffuse, 1.0 - lightAmbient) * lightDiffuse + lightAmbient;
}
else if (shadeType == 4) // only for editor
color.rgb *= lightDiffuse;
}
Expand Down

0 comments on commit f08e433

Please sign in to comment.