Skip to content

Commit

Permalink
SoftPoly: Fixed the md3 models, you can now correctly display the fra…
Browse files Browse the repository at this point in the history
…me of the model, in addition, interpolation is added to the model.

This error is mentioned by drfrag in the following link https://forum.zdoom.org/viewtopic.php?f=336&t=71228
  • Loading branch information
Erick194 authored and coelckers committed Feb 20, 2021
1 parent d98b401 commit 65b0047
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 14 deletions.
29 changes: 25 additions & 4 deletions src/common/rendering/polyrenderer/backend/poly_buffers.cpp
Expand Up @@ -105,16 +105,37 @@ void PolyVertexBuffer::SetFormat(int numBindingPoints, int numAttributes, size_t

/////////////////////////////////////////////////////////////////////////////

void PolyVertexInputAssembly::Load(PolyTriangleThreadData *thread, const void *vertices, int index)
void PolyVertexInputAssembly::Load(PolyTriangleThreadData *thread, const void *vertices, int frame0, int frame1, int index)
{
const uint8_t *vertex = static_cast<const uint8_t*>(vertices) + mStride * index;
const float *attrVertex = reinterpret_cast<const float*>(vertex + mOffsets[VATTR_VERTEX]);
uint8_t* buff = (uint8_t*)vertices;

// [GEC] finds the right frame.
uint32_t offsets[2] = { static_cast<uint32_t>(frame0 * Stride), static_cast<uint32_t>(frame1 * Stride) };
uint8_t* vertexBuffers[2] = { buff + offsets[0], buff + offsets[1] };

const uint8_t* vertex = static_cast<const uint8_t*>(vertexBuffers[0]) + mStride * index;
const float* attrVertex = reinterpret_cast<const float*>(vertex + mOffsets[VATTR_VERTEX]);

const uint8_t* vertex2 = static_cast<const uint8_t*>(vertexBuffers[1]) + mStride * index;
const float* attrVertex2 = reinterpret_cast<const float*>(vertex2 + mOffsets[VATTR_VERTEX]);

const float *attrTexcoord = reinterpret_cast<const float*>(vertex + mOffsets[VATTR_TEXCOORD]);
const uint8_t *attrColor = reinterpret_cast<const uint8_t*>(vertex + mOffsets[VATTR_COLOR]);
const uint32_t* attrNormal = reinterpret_cast<const uint32_t*>(vertex + mOffsets[VATTR_NORMAL]);
const uint32_t* attrNormal2 = reinterpret_cast<const uint32_t*>(vertex + mOffsets[VATTR_NORMAL2]);

thread->mainVertexShader.aPosition = { attrVertex[0], attrVertex[1], attrVertex[2], 1.0f };
// [GEC] Apply the formula for model interpolation

float newVertex[3];

float t = thread->mainVertexShader.Data.uInterpolationFactor;
float invt = 1.0f - t;

newVertex[0] = (invt * attrVertex[0]) + (t * attrVertex2[0]);
newVertex[1] = (invt * attrVertex[1]) + (t * attrVertex2[1]);
newVertex[2] = (invt * attrVertex[2]) + (t * attrVertex2[2]);

thread->mainVertexShader.aPosition = { newVertex[0], newVertex[1], newVertex[2], 1.0f };
thread->mainVertexShader.aTexCoord = { attrTexcoord[0], attrTexcoord[1] };

if ((UseVertexData & 1) == 0)
Expand Down
2 changes: 1 addition & 1 deletion src/common/rendering/polyrenderer/backend/poly_buffers.h
Expand Up @@ -48,7 +48,7 @@ class PolyVertexInputAssembly final : public PolyInputAssembly
std::vector<FVertexBufferAttribute> Attrs;
int UseVertexData;

void Load(PolyTriangleThreadData *thread, const void *vertices, int index) override;
void Load(PolyTriangleThreadData *thread, const void *vertices, int frame0, int frame1, int index) override;
};

class PolyVertexBuffer : public IVertexBuffer, public PolyBuffer
Expand Down
Expand Up @@ -258,7 +258,10 @@ void PolyRenderState::Apply()

ApplyMaterial();

if (mVertexBuffer) mDrawCommands->SetVertexBuffer(mVertexBuffer->Memory());
if (mVertexBuffer)
{
mDrawCommands->SetVertexBuffer(mVertexBuffer->Memory(), mVertexOffsets[0], mVertexOffsets[1]); // [GEC] Add offset params
}
if (mIndexBuffer) mDrawCommands->SetIndexBuffer(mIndexBuffer->Memory());
mDrawCommands->SetInputAssembly(static_cast<PolyVertexBuffer*>(mVertexBuffer)->VertexFormat);
mDrawCommands->SetRenderStyle(mRenderStyle);
Expand Down
2 changes: 1 addition & 1 deletion src/common/rendering/polyrenderer/drawers/poly_thread.cpp
Expand Up @@ -388,7 +388,7 @@ void PolyTriangleThreadData::Draw(int index, int vcount, PolyDrawMode drawmode)

ShadedTriVertex PolyTriangleThreadData::ShadeVertex(int index)
{
inputAssembly->Load(this, vertices, index);
inputAssembly->Load(this, vertices, frame0, frame1, index);
mainVertexShader.SIMPLE = (SpecialEffect == EFF_BURN) || (SpecialEffect == EFF_STENCIL);
mainVertexShader.SPHEREMAP = (SpecialEffect == EFF_SPHEREMAP);
mainVertexShader.main();
Expand Down
6 changes: 5 additions & 1 deletion src/common/rendering/polyrenderer/drawers/poly_thread.h
Expand Up @@ -44,7 +44,7 @@ class PolyTriangleThreadData
void SetTwoSided(bool value) { twosided = value; }

void SetInputAssembly(PolyInputAssembly *input) { inputAssembly = input; }
void SetVertexBuffer(const void *data) { vertices = data; }
void SetVertexBuffer(const void *data, int offset0, int offset1) { vertices = data; frame0 = offset0; frame1 = offset1;} //[GEC] Save frame params
void SetIndexBuffer(const void *data) { elements = (const unsigned int *)data; }
void SetLightBuffer(const void *data) { lights = (const FVector4 *)data; }
void SetViewpointUniforms(const HWViewpointUniforms *uniforms);
Expand Down Expand Up @@ -145,6 +145,10 @@ class PolyTriangleThreadData
uint32_t AlphaThreshold = 0x7f000000;
const PolyPushConstants* PushConstants = nullptr;

// [GEC] Add frame params, necessary to project frames and model interpolation correctly
int frame0 = 0;
int frame1 = 0;

const void *vertices = nullptr;
const unsigned int *elements = nullptr;
const FVector4 *lights = nullptr;
Expand Down
11 changes: 7 additions & 4 deletions src/common/rendering/polyrenderer/drawers/poly_triangle.cpp
Expand Up @@ -186,11 +186,14 @@ class PolySetShaderCommand : public PolyDrawerCommand
class PolySetVertexBufferCommand : public PolyDrawerCommand
{
public:
PolySetVertexBufferCommand(const void* vertices) : vertices(vertices) { }
void Execute(DrawerThread* thread) override { PolyTriangleThreadData::Get(thread)->SetVertexBuffer(vertices); }
PolySetVertexBufferCommand(const void* vertices, int offset0, int offset1) : vertices(vertices), offset0(offset0), offset1(offset1){ }
void Execute(DrawerThread* thread) override { PolyTriangleThreadData::Get(thread)->SetVertexBuffer(vertices, offset0, offset1); }

private:
const void* vertices;
// [GEC] Add offset params, necessary to project frames and model interpolation correctly
int offset0;
int offset1;
};

class PolySetIndexBufferCommand : public PolyDrawerCommand
Expand Down Expand Up @@ -345,9 +348,9 @@ void PolyCommandBuffer::SetInputAssembly(PolyInputAssembly *input)
mQueue->Push<PolySetInputAssemblyCommand>(input);
}

void PolyCommandBuffer::SetVertexBuffer(const void *vertices)
void PolyCommandBuffer::SetVertexBuffer(const void *vertices, int offset0, int offset1)
{
mQueue->Push<PolySetVertexBufferCommand>(vertices);
mQueue->Push<PolySetVertexBufferCommand>(vertices, offset0, offset1);
}

void PolyCommandBuffer::SetIndexBuffer(const void *elements)
Expand Down
4 changes: 2 additions & 2 deletions src/common/rendering/polyrenderer/drawers/poly_triangle.h
Expand Up @@ -50,7 +50,7 @@ class PolyCommandBuffer

void SetViewport(int x, int y, int width, int height, DCanvas *canvas, PolyDepthStencil *depthStencil, bool topdown);
void SetInputAssembly(PolyInputAssembly *input);
void SetVertexBuffer(const void *vertices);
void SetVertexBuffer(const void *vertices, int offset0, int offset1); // [GEC] Add offset params
void SetIndexBuffer(const void *elements);
void SetLightBuffer(const void *lights);
void SetViewpointUniforms(const HWViewpointUniforms *uniforms);
Expand Down Expand Up @@ -117,5 +117,5 @@ struct PolyPushConstants
class PolyInputAssembly
{
public:
virtual void Load(PolyTriangleThreadData *thread, const void *vertices, int index) = 0;
virtual void Load(PolyTriangleThreadData *thread, const void *vertices, int frame0, int frame1, int index) = 0; // [GEC] Add frame params
};

0 comments on commit 65b0047

Please sign in to comment.