Skip to content

Commit

Permalink
Use normalized texture coordinates with SFML 3
Browse files Browse the repository at this point in the history
  • Loading branch information
texus committed Jan 27, 2024
1 parent aab7753 commit a8e13f0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
24 changes: 22 additions & 2 deletions src/Backend/Renderer/SFML-Graphics/BackendRenderTargetSFML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,17 @@ namespace tgui
sfStates.texture = &std::static_pointer_cast<BackendTextureSFML>(sprite.getTexture().getData()->backendTexture)->getInternalTexture();
sfStates.shader = sprite.getTexture().getShader();

#if SFML_VERSION_MAJOR < 3
const Vector2f textureSize = texture ? Vector2f{texture->getSize()} : Vector2f{1,1};
#endif
const std::vector<Vertex>& vertices = sprite.getVertices();
const std::vector<unsigned int>& indices = sprite.getIndices();
std::vector<Vertex> triangleVertices(indices.size());
auto triangleVertices = MakeUniqueForOverwrite<Vertex[]>(indices.size());
for (unsigned int i = 0; i < indices.size(); ++i)
{
#if SFML_VERSION_MAJOR >= 3
triangleVertices[i] = vertices[indices[i]];
#else
triangleVertices[i].position.x = vertices[indices[i]].position.x;
triangleVertices[i].position.y = vertices[indices[i]].position.y;
triangleVertices[i].color.red = vertices[indices[i]].color.red;
Expand All @@ -136,10 +141,11 @@ namespace tgui
triangleVertices[i].color.alpha = vertices[indices[i]].color.alpha;
triangleVertices[i].texCoords.x = vertices[indices[i]].texCoords.x * textureSize.x;
triangleVertices[i].texCoords.y = vertices[indices[i]].texCoords.y * textureSize.y;
#endif
}

static_assert(sizeof(Vertex) == sizeof(sf::Vertex), "Size of sf::Vertex has to match with tgui::Vertex for optimization to work");
const sf::Vertex* sfmlVertices = reinterpret_cast<const sf::Vertex*>(triangleVertices.data());
const sf::Vertex* sfmlVertices = reinterpret_cast<const sf::Vertex*>(triangleVertices.get());
m_target->draw(sfmlVertices, indices.size(), sf::PrimitiveType::Triangles, sfStates);

if (clippingRequired)
Expand All @@ -155,13 +161,18 @@ namespace tgui
// we will create an array of our own Vertex objects and then use a reinterpret_cast to turn them into sf::Vertex.
static_assert(sizeof(Vertex) == sizeof(sf::Vertex), "Size of sf::Vertex has to match with tgui::Vertex for optimization to work");

#if SFML_VERSION_MAJOR < 3
const Vector2f textureSize = texture ? Vector2f{texture->getSize()} : Vector2f{1,1};
#endif

if (indices)
{
auto verticesSFML = MakeUniqueForOverwrite<Vertex[]>(indexCount);
for (std::size_t i = 0; i < indexCount; ++i)
{
#if SFML_VERSION_MAJOR >= 3
verticesSFML[i] = vertices[indices[i]];
#else
verticesSFML[i].position.x = vertices[indices[i]].position.x;
verticesSFML[i].position.y = vertices[indices[i]].position.y;
verticesSFML[i].color.red = vertices[indices[i]].color.red;
Expand All @@ -170,12 +181,16 @@ namespace tgui
verticesSFML[i].color.alpha = vertices[indices[i]].color.alpha;
verticesSFML[i].texCoords.x = vertices[indices[i]].texCoords.x * textureSize.x;
verticesSFML[i].texCoords.y = vertices[indices[i]].texCoords.y * textureSize.y;
#endif
}

m_target->draw(reinterpret_cast<const sf::Vertex*>(verticesSFML.get()), indexCount, sf::PrimitiveType::Triangles, convertRenderStates(states, texture));
}
else // There are no indices
{
#if SFML_VERSION_MAJOR >= 3
m_target->draw(reinterpret_cast<const sf::Vertex*>(vertices), vertexCount, sf::PrimitiveType::Triangles, convertRenderStates(states, texture));
#else
auto verticesSFML = std::vector<Vertex>(vertices, vertices + vertexCount);
for (std::size_t i = 0; i < vertexCount; ++i)
{
Expand All @@ -184,6 +199,7 @@ namespace tgui
}

m_target->draw(reinterpret_cast<const sf::Vertex*>(verticesSFML.data()), vertexCount, sf::PrimitiveType::Triangles, convertRenderStates(states, texture));
#endif
}
}

Expand Down Expand Up @@ -224,6 +240,10 @@ namespace tgui
transformMatrix[1], transformMatrix[5], transformMatrix[13],
transformMatrix[3], transformMatrix[7], transformMatrix[15]);

#if SFML_VERSION_MAJOR >= 3
statesSFML.coordinateType = sf::CoordinateType::Normalized;
#endif

if (texture)
{
TGUI_ASSERT(std::dynamic_pointer_cast<BackendTextureSFML>(texture), "BackendRenderTargetSFML requires textures of type BackendTextureSFML");
Expand Down
20 changes: 18 additions & 2 deletions src/Backend/Renderer/SFML-Graphics/CanvasSFML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,26 @@ namespace tgui
"CanvasSFML::draw requires sprite to have a backend texture of type BackendTextureSFML");
statesSFML.texture = &std::static_pointer_cast<BackendTextureSFML>(sprite.getTexture().getData()->backendTexture)->getInternalTexture();

#if SFML_VERSION_MAJOR >= 3
statesSFML.coordinateType = sf::CoordinateType::Normalized;
#else
const sf::Vector2u textureSize{statesSFML.texture->getSize()};
#endif

const std::vector<Vertex>& vertices = sprite.getVertices();
const std::vector<unsigned int>& indices = sprite.getIndices();
std::vector<Vertex> triangleVertices(indices.size());
auto triangleVertices = MakeUniqueForOverwrite<Vertex[]>(indices.size());
for (std::size_t i = 0; i < indices.size(); ++i)
{
triangleVertices[i] = vertices[indices[i]];
#if SFML_VERSION_MAJOR < 3
triangleVertices[i].texCoords.x *= textureSize.x;
triangleVertices[i].texCoords.y *= textureSize.y;
#endif
}

static_assert(sizeof(Vertex) == sizeof(sf::Vertex), "Size of sf::Vertex has to match with tgui::Vertex for optimization to work");
const sf::Vertex* sfmlVertices = reinterpret_cast<const sf::Vertex*>(triangleVertices.data());
const sf::Vertex* sfmlVertices = reinterpret_cast<const sf::Vertex*>(triangleVertices.get());
m_renderTexture.draw(sfmlVertices, indices.size(), sf::PrimitiveType::Triangles, statesSFML);
}

Expand Down Expand Up @@ -255,10 +262,15 @@ namespace tgui
static_assert(sizeof(Vertex) == sizeof(sf::Vertex), "Size of sf::Vertex has to match with tgui::Vertex for optimization to work");

const sf::Texture& texture = m_renderTexture.getTexture();
#if SFML_VERSION_MAJOR < 3
const Vector2f textureSize = Vector2f{static_cast<float>(texture.getSize().x), static_cast<float>(texture.getSize().y)};
#endif
auto verticesSFML = MakeUniqueForOverwrite<Vertex[]>(indices.size());
for (std::size_t i = 0; i < indices.size(); ++i)
{
#if SFML_VERSION_MAJOR >= 3
verticesSFML[i] = vertices[indices[i]];
#else
verticesSFML[i].position.x = vertices[indices[i]].position.x;
verticesSFML[i].position.y = vertices[indices[i]].position.y;
verticesSFML[i].color.red = vertices[indices[i]].color.red;
Expand All @@ -267,6 +279,7 @@ namespace tgui
verticesSFML[i].color.alpha = vertices[indices[i]].color.alpha;
verticesSFML[i].texCoords.x = vertices[indices[i]].texCoords.x * textureSize.x;
verticesSFML[i].texCoords.y = vertices[indices[i]].texCoords.y * textureSize.y;
#endif
}

sf::RenderStates statesSFML;
Expand All @@ -276,6 +289,9 @@ namespace tgui
transformMatrix[0], transformMatrix[4], transformMatrix[12],
transformMatrix[1], transformMatrix[5], transformMatrix[13],
transformMatrix[3], transformMatrix[7], transformMatrix[15]);
#if SFML_VERSION_MAJOR >= 3
statesSFML.coordinateType = sf::CoordinateType::Normalized;
#endif

TGUI_ASSERT(dynamic_cast<BackendRenderTargetSFML*>(&target), "CanvasSFML requires a render target of type BackendRenderTargetSFML");
static_cast<BackendRenderTargetSFML&>(target).getTarget()->draw(reinterpret_cast<const sf::Vertex*>(verticesSFML.get()), indices.size(), sf::PrimitiveType::Triangles, statesSFML);
Expand Down

0 comments on commit a8e13f0

Please sign in to comment.