Results 1 to 5 of 5

Thread: The Texture Filtering Problem

  1. #1
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default The Texture Filtering Problem

    Introduction:
    CL_Sprite contains set_linear_filter(), that determines if CL_Texture uses linear filtering or nearest filtering.

    This is used to control how the source texture pixels are stretced.
    Linear Filtering - Uses the average of neighbouring pixels
    Nearest Filtering - Uses the nearest pixel

    Usage:
    Lets say we have an Image somewhere within the CL_Texture

    For linear filtering, we want to ensure that the Image coordinates denote the center of the pixel:
    For example: coordinate_x = (image_position_x + cl_pixelcenter_constant) / texture_width

    The cl_pixelcenter_constant is 0.375, that is the "magic" number that looks best in most situations.

    This has a side effect; the pixel position of the image may be a pixel outside of the image (depending on the destination scale/rotation)

    This is solved by giving the image within the texture a 1 pixel outline in the same color as the inner pixels

    For nearest filtering, since we do not care about the neighbouring pixels, we do not not need to add the cl_pixelcenter_constant or require the pixel outline

    Problem

    CL_SpriteRenderBatch::to_sprite_vertex() and CL_SpriteRenderBatch::draw_image()

    Contains: coordinate_x = (image_position_x + cl_pixelcenter_constant) / texture_width

    - Even when the CL_Texture filtering is set to nearest filtering

    Thus, it is a requirement to always have a pixel outline.

    Solution 1

    Modify CL_Sprite to automatically add a pixel outline (in the same color as the inner pixel) if scaling is used

    (Ideally, this should also be required for CL_Font_Texture, as it compacts font glyphs within the same texture)

    Make it a requirement for CL_Image's to have a pixel outline. The Utilities/TexturePacker already adds this extra pixel

    Solution 2

    If filtering is "linear", use Solution 1

    If filtering is "nearest", modify CL_SpriteRenderBatch::to_sprite_vertex() and CL_SpriteRenderBatch::draw_image() to not use the cl_pixelcenter_constant (thus negating the need to have a 1 pixel outline)

    Notes

    The calculation for the texture position in CL_SpriteRenderBatch::draw_image() can be precalculated (in the CL_Image constructor)

    How to determine the filtering used is not straightforward:
    We could use... if (texture.get_mag_filter() == cl_filter_nearest)
    But there is also texure_get_min_filter() and mipmap filters.

    Proof

    See Tests/Display/TexelCenter

  2. #2
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    Fixed.

    I modified CL_Sprite to only add the pixel center contant (0.375) when linear filtering is used.

  3. #3
    Lesser Wizard
    Join Date
    Jun 2007
    Posts
    109

    Default

    Excellent!

    Is this contained in the 2.03 release version, or shall I continue to update from SVN?

  4. #4
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    Yes, it's in the release.

    At the time of writing this message, the only SVN change since the release is "Changed visual margins" to the CTalk example

  5. #5
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    ooops, apparently my CL_Sprite patch did not work correctly.

    It incorrectly sets the default internal linear_filter flag to false, when CL_Texture's default to true.

    Code:
    Index: Display/2D/sprite_impl.cpp
    ===================================================================
    --- Display/2D/sprite_impl.cpp	(revision 3542)
    +++ Display/2D/sprite_impl.cpp	(working copy)
    @@ -57,7 +57,7 @@
     	scale_x(1.0f),
     	scale_y(1.0f),
     	color(1.0f, 1.0f, 1.0f, 1.0f),
    -	linear_filter(false),
    +	linear_filter(true),
     	translation_hotspot(0,0),
     	rotation_hotspot(0,0),
     	translation_origin(origin_top_left),
    Fixed in svn

Similar Threads

  1. texture drawing method questions
    By ppheathead in forum Official ClanLib SDK Forums
    Replies: 5
    Last Post: 06-10-2009, 07:19 AM
  2. [driAllocateTexture:636] unable to allocate texture
    By jmguy02 in forum Official ClanLib SDK Forums
    Replies: 4
    Last Post: 05-27-2009, 12:30 PM
  3. ClanLib 0.9 Texture Packer
    By sphair in forum Official ClanLib SDK Forums
    Replies: 4
    Last Post: 05-15-2009, 05:45 AM
  4. relationship between GC and Texture?
    By logixoul in forum Official ClanLib SDK Forums
    Replies: 4
    Last Post: 07-07-2008, 07:17 AM
  5. linear filtering
    By logixoul in forum Official ClanLib SDK Forums
    Replies: 1
    Last Post: 07-01-2008, 01:28 PM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •