Changeset 1072


Ignore:
Timestamp:
Nov 16, 2011, 1:31:23 AM (12 years ago)
Author:
sam
Message:

tutorial: the Mandelbrot viewer now queries the nearest pixel of each
slice, and averages the results. This gives us almost free antialiasing,
since all these queries will need to be done anyway for raytracing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/tutorial/tut03.cpp

    r1071 r1072  
    270270
    271271                "#version 120\n"
    272                 "uniform vec4 in_PixelDelta;\n"
    273                 "uniform sampler2D in_Texture;\n"
     272                ""
     273                "uniform vec4 in_PixelDelta;"
     274                "uniform sampler2D in_Texture;"
     275                ""
     276                /* Get the coordinate of the nearest point in slice 0 in xy,
     277                 * and the squared distance to that point in z.
     278                 * p is in normalised [0,1] texture coordinates.
     279                 * return value has the 0.25 Y scaling. */
     280                "vec3 nearest0(vec2 p) {"
     281                "    vec2 q = p + 0.5 * in_PixelDelta.xy;"
     282                "    q -= mod(q, 2.0 * in_PixelDelta.xy);"
     283                "    q += 0.5 * in_PixelDelta.xy;"
     284                "    return vec3(q * vec2(1.0, 0.25),"
     285                "                length(q - p));"
     286                "}"
     287                ""
     288                "vec3 nearest1(vec2 p) {"
     289                "    vec2 q = p - 0.5 * in_PixelDelta.xy;"
     290                "    q -= mod(q, 2.0 * in_PixelDelta.xy);"
     291                "    q += 1.5 * in_PixelDelta.xy;"
     292                "    return vec3(q * vec2(1.0, 0.25) + vec2(0.0, 0.25),"
     293                "                length(q - p));"
     294                "}"
     295                ""
     296                "vec3 nearest2(vec2 p) {"
     297                "    vec2 q = p + vec2(0.5, -0.5) * in_PixelDelta.xy;"
     298                "    q -= mod(q, 2.0 * in_PixelDelta.xy);"
     299                "    q += vec2(0.5, 1.5) * in_PixelDelta.xy;"
     300                "    return vec3(q * vec2(1.0, 0.25) + vec2(0.0, 0.50),"
     301                "                length(q - p));"
     302                "}"
     303                ""
     304                "vec3 nearest3(vec2 p) {"
     305                "    vec2 q = p + vec2(-0.5, 0.5) * in_PixelDelta.xy;"
     306                "    q -= mod(q, 2.0 * in_PixelDelta.xy);"
     307                "    q += vec2(1.5, 0.5) * in_PixelDelta.xy;"
     308                "    return vec3(q * vec2(1.0, 0.25) + vec2(0.0, 0.75),"
     309                "                length(q - p));"
     310                "}"
     311                ""
    274312                "void main(void) {"
    275313                "    vec2 coord = gl_TexCoord[0].xy;"
    276                 /* i is 0 or 1, depending on the current X coordinate within
    277                  * the current 2×2 texel. j is 0 or 1, depending on the Y
    278                  * coordinate _and_ the value of i, in order to stay on the
    279                  * Bayer dithering pattern. */
    280                 "    int i = int(mod(coord.x, 2.0 * in_PixelDelta.x) * in_PixelDelta.z);"
    281                 "    int j = int(mod(coord.y + i * in_PixelDelta.y, 2.0 * in_PixelDelta.y) * in_PixelDelta.w);"
    282                 /* Choose the best slice depending on the value of i and j. */
    283                 "    coord.y += i + j * 2;"
    284                 "    coord.y *= 0.25;"
    285                 /* Get a pixel from the best slice */
    286                 "    vec4 p = texture2D(in_Texture, coord);"
    287                 "    gl_FragColor = p;"
     314                /* Slightly shift our pixel so that it does not lie at
     315                 * an exact texel boundary. This would lead to visual
     316                 * artifacts. */
     317                "coord -= 0.1 * in_PixelDelta.x;"
     318                /* Get a pixel from each slice */
     319                "    vec4 p0 = texture2D(in_Texture, nearest0(coord).xy);"
     320                "    vec4 p1 = texture2D(in_Texture, nearest1(coord).xy);"
     321                "    vec4 p2 = texture2D(in_Texture, nearest2(coord).xy);"
     322                "    vec4 p3 = texture2D(in_Texture, nearest3(coord).xy);"
     323                "    gl_FragColor = 0.25 * (p0 + p1 + p2 + p3);"
    288324                "}"
    289325#else
Note: See TracChangeset for help on using the changeset viewer.