Example a_backup/texture_tiling: Texture tiling.

This example shows how cmgui can break up a large 2D texture into tiles and render these seemlessly.

The image is a picture taken at the Blue Pools on the Haast Rd.

The image shown here is of the underlying geometry, showing the diagonal cuts made to the geometry to so that there are geometrical boundaries matching the tiles made of the texture.

Screenshot of example a_backup/texture_tiling


The comfile run by this example is as follows:

# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is CMISS Multipass Render Example.
#
# The Initial Developer of the Original Code is
# Spark Dental Technology Ltd.s
# Portions created by the Initial Developer are Copyright (C) 2008
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#    Shane Blackett <shane@blackett.co.nz>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****

#Example texture_tiling.com:  Showing how texture tiling works.
#
# Graphics card implementations place limits on the largest texture that
# can be displayed as a single image.  To work around this limitation 
# texture_tiling automatically breaks a texture into smaller pieces and
# divides geometry into matching pieces so that they join back together,
# hopefully seamlessly.

# In this example the graphics card limitation is overridden by this
# environment variable, to force texture tiling to activate at a much 
# smaller resolution.  Current graphics cards actually have limits in the
# order of 4096.
$ENV{CMISS_MAX_TEXTURE_SIZE} = 1024;

# Read our texture and enable texture_tiling.
gfx create texture bluepools image $example/bluepools.jpg texture_tiling decal;
# Use a decal texture with pink so that glitches are obvious
gfx create material bluepools texture bluepools diffuse 1 0 0.5;

# First show on a square
gfx create region small;
gfx read elements $example/square.exelem region small;
gfx read elements $example/square.exelem;

gfx modify g_element square general clear circle_discretization 6 default_coordinate coordinates element_discretization "1*1*1" native_discretization none;
gfx modify g_element square surfaces select_on material bluepools texture_coordinates xi selected_material default_selected render_shaded;

gfx define graphics_filter small_only match_region_path small;
gfx define scene small filter small_only;
gfx set transformation name small 0.2 0 0 0 -0 0.2 0 0 0 -0 0.2 0 -4 -4 0 1;
gfx modify g_element small/square general clear;
gfx modify g_element small/square surfaces coordinate coordinates select_on material bluepools texture_coordinates xi selected_material default_selected render_wireframe;

gfx create window 1 double_buffer;
gfx modify window 1 layout 2d ortho_axes z x width 512 height 512;
gfx modify window 1 background colour 0.8 0 0;

# Show the dividing up of the geometry to match the texture tiles
gfx modify g_element square surfaces select_on material bluepools texture_coordinates xi selected_material default_selected render_wireframe;

# Rotate the texture coordinates so they are not orthogonal to the texture
gfx define field rotation constant 0.5 0.5 0.5 -0.5;
gfx define field small/rotation constant 0.5 0.5 0.5 -0.5;
#xi field is always 3 coordinates, we only want 2
gfx define field xi2 composite xi.1 xi.2;
gfx define field small/xi2 composite xi.1 xi.2;
gfx define field rotate_xi matrix_multiply number_of_rows 2 fields rotation xi2;
gfx define field small/rotate_xi matrix_multiply number_of_rows 2 fields rotation xi2;
gfx define field offset_xi add fields rotate_xi [0 0.5];
gfx define field small/offset_xi add fields rotate_xi [0 0.5];

gfx modify g_element square surfaces select_on material bluepools texture_coordinates offset_xi selected_material default_selected render_wireframe;
gfx modify g_element small/square surfaces coordinate coordinates select_on material bluepools texture_coordinates offset_xi selected_material default_selected render_wireframe;

if ($TESTING)
{
   # Don't show the inset geometry as we are displaying the mesh anyway
   gfx set visibility small off;
   gfx modify window 1 view parallel eye_point 0 0 19.5447 interest_point 0 0 0.072 up_vector -1 -0 -0 view_angle 40 near_clipping_plane 0.194727 far_clipping_plane 69.5889 relative_viewport ndc_placement -1 1 2 2 viewport_coordinates 0 0 1 1;
   gfx print win 1 file diagonal_meshing.png;
   gfx set visibility small on;
}

# The texture should still look seamless
gfx modify g_element square surfaces select_on material bluepools texture_coordinates offset_xi selected_material default_selected render_shaded;

# Check the texture is displayed at full resolution by zooming right in
# There is not filtering activated so the rectangular pixels of the texture should 
# be visible.
gfx modify window 1 view parallel eye_point -1.06777 -1.70898 19.5447 interest_point -1.06777 -1.70898 0.072 up_vector -1 -0 -0 view_angle 0.199361 near_clipping_plane 0.194727 far_clipping_plane 69.5889 relative_viewport ndc_placement -1 1 2 2 viewport_coordinates 0 0 1 1;

if ($TESTING)
{
   gfx print win 1 file zoomed_texels.png;
}

#Show how large the texels become without texture tiling
gfx create texture bluepool_no_tiling image $example/bluepools.jpg no_texture_tiling decal;
# Use a decal texture with pink so that glitches are obvious
gfx create material bluepool_no_tiling texture bluepool_no_tiling diffuse 1 0 0.5;

gfx create region no_tiling;
gfx read elements $example/square.exelem region no_tiling;
gfx define graphics_filter no_tiling_only match_region_path no_tiling;
gfx define scene no_tiling filter no_tiling_only;
gfx define field no_tiling/rotation constant 0.5 0.5 0.5 -0.5;
gfx define field no_tiling/xi2 composite xi.1 xi.2;
gfx define field no_tiling/rotate_xi matrix_multiply number_of_rows 2 fields rotation xi2;
gfx define field no_tiling/offset_xi add fields rotate_xi [0 0.5];
gfx modify g_element no_tiling/square general clear;
gfx modify g_element no_tiling/square surfaces coordinate coordinates select_on material bluepool_no_tiling texture_coordinates offset_xi selected_material default_selected render_shaded;

gfx create window no_texture_tiling;
gfx modify window no_texture_tiling image scene no_tiling;
gfx modify window no_texture_tiling layout 2d ortho_axes z x width 512 height 512;
gfx modify window no_texture_tiling view parallel eye_point -1.06777 -1.70898 19.5447 interest_point -1.06777 -1.70898 0.072 up_vector -1 -0 -0 view_angle 0.199361 near_clipping_plane 0.194727 far_clipping_plane 69.5889 relative_viewport ndc_placement -1 1 2 2 viewport_coordinates 0 0 1 1;

# Finally lets do it on a more complicated mesh
sub show_femur
{
gfx set vis square off

# Read in the nodes for the femur
gfx read nodes $example/femur_exnode.cmiss
gfx read elements $example/femur_exelem.cmiss

# Define some continuous texture coordinates
gfx define field xi_texture_coordinatesA integration coordinate xi integrand [1.0] seed_element 3160009;
gfx define field xi_texture_coordinatesB multiply fields xi_texture_coordinatesA [0.0625 0.038461538];
gfx define field xi_texture_coordinates add fields xi_texture_coordinatesB [0.5 0];

gfx modify g_element femur_left general clear element_discretization "4*4*4";
gfx modify g_element femur_left surfaces coordinate coordinates select_on material bluepools texture_coordinates xi_texture_coordinates selected_material default_selected render_shaded;

gfx modify window 1 layout simple;
gfx modify window 1 view parallel eye_point -734.147 -587.894 14.6424 interest_point -9.41066 19.6251 28.2347 up_vector -0.439582 0.540448 -0.717415 view_angle 40 near_clipping_plane 9.45784 far_clipping_plane 3379.91 relative_viewport ndc_placement -1 1 2 2 viewport_coordinates 0 0 1 1;
}

sub test_sizes
{
	my $xsizes = shift;
	if (! defined $xsizes)
	{
		my @xsize_array = map $_ * 99 + 1001, 1..22;
		$xsizes = \@xsize_array;
	}
	my $ysizes = shift;
	if (! defined $ysizes)
	{
		my @ysize_array = map $_ * 97 + 1001, 1..14;
		$ysizes = \@ysize_array;
	}
	my $discretizations = shift;
	if (! defined $discretizations)
	{
		$discretizations = [ 1, 2, 5, 9, 31, 32 ];
	}

    gfx modify g_element square general clear circle_discretization 6 default_coordinate coordinates element_discretization "1*1*1" native_discretization none;
    gfx modify g_element square surfaces select_on material bluepools texture_coordinates xi selected_material default_selected render_shaded;

    gfx modify g_element small/square general;
    gfx modify g_element small/square surfaces coordinate coordinates select_on material bluepools texture_coordinates xi selected_material default_selected render_wireframe;

    for my $i (@$xsizes)
    {
        for my $j (@$ysizes)
        {
             for my $element_discretization (@$discretizations)
             {
                print ("width $i height $j discretization $element_discretization\n");
                # BEWARE: these no longer modify existing graphics, so example is broken
                gfx modify g_element square general element_discretization "$element_discretization";
                gfx modify g_element small/square general element_discretization "$element_discretization";

                gfx modify texture bluepools image crop 0 0 $i $j $example/bluepools.jpg texture_tiling decal;
                gfx modify window 1 image update;
            }
        }
    }
}

sub test_stripesmall
{
    #These images are useful for ensuring that the borders are displayed correctly.
    gfx modify texture bluepools image $example/yellowstripesmaller.png texture_tiling decal;
}

sub test_stripe
{
    gfx modify texture bluepools image $example/yellowstripe.png texture_tiling decal;
}

sub test_monostripe
{
    gfx modify texture bluepools image $example/monostripe.png texture_tiling decal;
}

$PI = 3.14159;

sub test_rotations
{
    my $rotation_step = 2 * $PI / 20;
    my $centrex = 0.5;
    my $centrey = 0.5;

	my $rotations = shift;
	if (! defined $rotations)
	{
		my @rotation_array = map $_ * $rotation_step, 1..20;
		$rotations = \@rotation_array;
	}
	my $discretizations = shift;
	if (! defined $discretizations)
	{
		$discretizations = { 1, 31, 32 };
	}

    gfx modify g_element square general clear circle_discretization 6 default_coordinate coordinates element_discretization "1*1*1" native_discretization none;
    gfx modify g_element square surfaces select_on material bluepools texture_coordinates offset_xi selected_material default_selected render_shaded;

    gfx modify g_element small/square general clear;
    gfx modify g_element small/square surfaces coordinate coordinates select_on material bluepools texture_coordinates offset_xi selected_material default_selected render_wireframe;

    for my $rotation ( @$rotations )
    {
        gfx define field rotation constant cos($rotation) sin($rotation) -sin($rotation) cos($rotation);
        #xi field is always 3 coordinates, we only want 2
        gfx define field xi2 composite xi.1 xi.2;
        gfx define field rotate_xi matrix_multiply number_of_rows 2 fields rotation xi2;
        my $offset_x = $centrex * (1-cos($rotation)) - $centrey * sin($rotation);
        my $offset_y = $centrey * (1-cos($rotation)) + $centrex * sin($rotation);
        gfx define field offset_xi add fields rotate_xi [ $offset_x $offset_y ];
    
             for my $element_discretization (@$discretizations)
             {
                print ("rotation $rotation discretization $element_discretization\n");

				# BEWARE: these no longer update existing graphics, so example is broken
                gfx modify g_element square general element_discretization "$element_discretization";
                gfx modify g_element small/square general element_discretization "$element_discretization";
              
                gfx modify texture bluepools image $example/bluepools.jpg texture_tiling decal;
                gfx modify window 1 image update;
            }
    }
}

gfx modify window 1 layout 2d ortho_axes z x width 1024 height 1024;
gfx modify window 1 view parallel eye_point -0.65 -0.65 23.2666 interest_point -0.65 -0.65 0.0432 up_vector -1 -0 -0 view_angle 28.0051 near_clipping_plane 0.232234 far_clipping_plane 82.9923 relative_viewport ndc_placement -1 1 2 2 viewport_coordinates 0 0 1 1;

gfx mod texture bluepools filter_linear_mipmap_linear;

if ($TESTING)
{
	test_rotations( [ 0, $PI / 10.0, $PI / 2.0, $PI ], [1, 31] ); 

	test_sizes( [ 1001, 2000 ], [ 1001, 2000 ], [1, 31] ); 
}

if (0)
{
    #Check winding (use along with modulate).
    gfx modify lmodel default one;
    
    test_sizes;
    test_rotations;
    show_femur;
    test_stripe;
    test_stripesmall;
    test_monostripe;
}

Files used by this example are:

Name                     Modified     Size

texture_tiling.com 20-Apr-2012 13k COPYRIGHT 20-Apr-2012 504 bluepools.jpg 20-Apr-2012 3.3M femur_exelem.cmiss 20-Apr-2012 170k femur_exnode.cmiss 20-Apr-2012 140k monostripe.png 20-Apr-2012 4.0k square.exelem 20-Apr-2012 1.9k yellowstripe.png 20-Apr-2012 9.3k yellowstripesmaller.png 20-Apr-2012 9.5k

Download the entire example:

Name                                     Modified     Size

examples_a_backup_texture_tiling.tar.gz 12-Aug-2014 6.8M

Testing status by version:

StatusTestedReal time (s)
i686-linux
cmgui-wxSuccessSun Mar 6 00:35:00 201619
cmgui-wx-debugSuccessSun Mar 6 00:35:16 201619
cmgui-wx-debug-memorycheckSuccessSun Mar 6 00:34:02 201619
cmgui-wx-debug-valgrindFailureSun Mar 6 02:30:32 2016462
last breakSun Mar 6 02:22:00 2016462
last successTue Feb 10 02:44:00 2015453
x86_64-linux
cmgui-wxFailureSun Mar 6 00:01:49 20160
last breakSun Mar 6 00:01:00 20160
last successWed Jun 3 00:15:00 201515
cmgui-wx-debugFailureSun Mar 6 00:01:38 20161
last breakSun Mar 6 00:01:00 20161
last successWed Jun 3 00:15:00 201515
cmgui-wx-debug-memorycheckFailureSun Mar 6 00:01:38 20160
last breakSun Mar 6 00:01:00 20160
last successWed Jun 3 00:15:00 201515
cmgui-wx-debug-valgrindFailureSun Mar 6 00:03:13 20169
last breakSun Mar 6 00:03:00 20169
last successWed Jun 3 01:49:00 2015388
cmgui-wx-gcc-cad-debug-valgrindSuccessThu Jan 7 00:02:36 20167

Testing status by file:


Html last generated: Sun Mar 6 05:51:03 2016

Input last modified: Fri Apr 20 16:02:36 2012


CMISS Help / Examples / a_backup / texture_tiling