Example a/project_texture: Texture projection.

This example shows how to use cmgui to create texture maps from actual photographs and geometrical models. It shows three different texture parameterisations.

The image is a picture taken from Ponui Island near Auckland city.

Screenshot of example 
			 project_texture showing image and mesh

Screenshot of example a/project_texture


The comfile run by this example is as follows:

#Example project_texture.com:  Creating textured objects.
#
# To add realism to models we can use textures.  Often these are created
# by mapping photographs onto the object.  This example shows how an image
# in the background can be captured as a texture on the object.
#
# Test on small textures, when you do this yourself you will probably choose
# larger values.  These examples will run much faster on displays with hardware
# OpenGL 1.3 and above as they make use of offscreen display buffers where available.
$texture_width = 64;
$texture_height = 64;
#----------
#
# Use reduced tessellation quality to agree with test output
gfx define tessellation default minimum_divisions "1" refinement_factors "1";
#
# Read in the nodes for the femur
gfx read nodes $example/femur_exnode.cmiss
gfx read elements $example/femur_exelem.cmiss
#
# Create initial graphics:
gfx modify g_element "/" general clear;
gfx modify g_element femur_left lines coordinate coordinates material default;

# Open a graphics window.
gfx create window 1
#
# The femur is automatically shown at the centre of the window.
# Display it with a nice material
gfx create material bone ambient 0.7 0.7 0.3 diffuse 0.9 0.9 0.8 emission 0 0 0 specular 0.05 0.05 0.05 alpha 1 shininess 0.1
gfx modify window 1 set perturb_lines
gfx modify g_element femur_left surfaces material bone
#
# Next we read in a image that we are going to map onto our object.
#
gfx create texture ponui image $example/ponui02.jpg
# Linearly interpolating our texture means that the texture doesn't appear so pixelated.
gfx modify texture ponui linear edge_clamp;
gfx mod win 1 layout width 500 height 500
gfx mod win 1 back texture ponui tex_placement 20 -20 460 460
#
# ---------- Interactive texturing ----------
#
# We can view the surfaces textured interactively by transforming the bone
# coordinates into 2D coordinates in the background of the window,
# and then using these coordinates to sample the texture. This field now
# contains the colour as data and we can render it with a spectrum
# that maps the three data components directly to rgb colour.
#gfx define field projection_matrix window_projection field coordinates window 1 texture_projection
gfx define field projection_matrix window_projection window 1 from world to window_pixel_top_left;
gfx define field pixel_projection projection field coordinates projection_matrix projection_matrix;
gfx define field offset_pixel_projection offset field pixel_projection offsets -20.0 480.0 0.0;
gfx define field projection scale field offset_pixel_projection scale_factors (1.0/460.0) (1.0/460.0) 1.0;
gfx define field mapped_texture sample_texture coordinate projection field ponui;
gfx create spectrum rgb_spectrum clear overwrite_colour
gfx modify spectrum rgb_spectrum linear range 0 1 extend_above extend_below red colour_range 0 1 ambient diffuse component 1
gfx modify spectrum rgb_spectrum linear range 0 1 extend_above extend_below green colour_range 0 1 ambient diffuse component 2
gfx modify spectrum rgb_spectrum linear range 0 1 extend_above extend_below blue colour_range 0 1 ambient diffuse component 3
gfx modify g_element femur_left surfaces material bone data mapped_texture spectrum rgb_spectrum;
#
# As you move the object you are displaying around the window the mapped
# colour should update automatically.  The resolution is low, controlled by
# the graphical_element sampling, typically 4x4 on each element.  Between the
# sample points OpenGL Gouraud interpolates the colour smoothing the image.
# You can improve the resolution by increasing the discretisation of your
# elements.  This works but is very demanding on your graphics as you are
# increasing the number of polygons.
gfx define tessellation default minimum_divisions "1" refinement_factors "12";
gfx define tessellation fine minimum_divisions "12" refinement_factors "1";
gfx modify g_element femur_left lines delete;
#
# Use reduced tessellation quality to agree with test output
gfx define tessellation default minimum_divisions "1" refinement_factors "1";
#
# -------------- Texture coordinate space --------------
#
# Instead of Gouroud Shading we can make a texture which gives the high resolution colour
# without the high resolution geometry.
#
# Firstly you need to choose some texture coordinates.  These should
# evenly spread your texture over your object.  The integration field
# called texture_coordinates uses the element mesh of your model to create
# texture coordinates.  Starting at the seed_element it spreads out.
# We want to offset and scale the texture coordinates so they lie from
# 0 to 1 in each direction.
#
gfx define field one composite 1.0;
gfx define field xi_texture_coordinatesA integration coordinate xi integrand one seed_element 3160009;
gfx define field xi_texture_coordinatesB scale field xi_texture_coordinatesA scale_factors 0.0625 0.038461538;
gfx define field xi_texture_coordinates offset field xi_texture_coordinatesB offsets 0.5 0;
#
# To visualise the texture space we will create a scene and window
# that shows the object with the texture_coordinates chosen as the
# actual coordinates.  As above this approximation of the texture will
# update automatically as you rotate the femur in graphics window 1.
#
gfx define graphics_filter texture_projection_lines_on match_graphics_name texture_projection_lines;
gfx define graphics_filter texture_projection_surfaces_on match_graphics_name texture_projection_surfaces;
gfx define graphics_filter texture_projection_on operator_or add_filters texture_projection_lines_on texture_projection_surfaces_on;
#gfx define scene texture_projection filter texture_projection_on;
gfx define graphics_filter texture_projection_lines_off match_graphics_name texture_projection_lines inverse;
gfx define graphics_filter texture_projection_surfaces_off match_graphics_name texture_projection_surfaces inverse;
gfx define graphics_filter texture_projection_off operator_or add_filters default texture_projection_lines_off texture_projection_surfaces_off;
#gfx define scene default filter texture_projection_off;


#gfx define scene default filter match_graphics_name texture_projection_lines inverse;
#gfx define scene default filter match_graphics_name texture_projection_surfaces inverse;

gfx modify g_element femur_left lines coordinate xi_texture_coordinates tessellation fine as texture_projection_lines;
gfx modify g_element femur_left surfaces coordinate xi_texture_coordinates tessellation fine material bone as texture_projection_surfaces data mapped_texture spectrum rgb_spectrum;
gfx create window texture_projection;
#gfx modify window texture_projection image scene texture_projection;
gfx modify window texture_projection image filter texture_projection_on;
gfx modify window texture_projection layout 2d ortho_axes z -y eye_spacing 0.25 width 400 height 400;
gfx modify window texture_projection view parallel eye_point 0.495756 0.498507 974.064 interest_point 0.495756 0.498507 28.2347 up_vector -0 1 0 view_angle 0.089045 near_clipping_plane 9.4583 far_clipping_plane 3380.07 relative_viewport ndc_placement -1 1 2 2 viewport_coordinates 0 0 1 1;
#
# -------------- High resolution texture generation --------------
#
# Now we will create a high resolution texture representation of the same data.
# This will take a while to calculate initially but once done renders almost instantly,
# however it doesn't update when you transform the window.  The maximum size of the texture
# that you can use is limited by the hardware, although you should be able to calculate one
# of an arbitrary size here.  Also OpenGL restricts you to dimensions that are multiples of 2.
#
gfx create texture ponui_mapped width 1 height 1 specify_width $texture_width specify_height $texture_height evaluate_image field mapped_texture spectrum rgb_spectrum texture_coord xi_texture_coordinates element_group femur_left;
#
# Show the texture on the bone in both windows.  The bone becomes basically invisible in 
# graphics_window 1 showing that the texturing operation was correct.  Rotate the bone to
# prove that it is actually still there.
#
gfx create material ponui_mapped texture ponui_mapped diffuse 1 1 1 ambient 0.3 0.3 0.3;
gfx modify g_element femur_left surfaces tessellation fine material ponui_mapped texture xi_texture_coordinates as texture_projection_surfaces;
#
gfx write texture ponui_mapped file ponui_mapped.sgi;
#
# A linear texture mapping is slower but is less obviously pixelated
gfx modify texture ponui_mapped linear;
# You can change the rendition of the texture so that the bone is still shaded.
gfx modify texture ponui_mapped modulate;
#
# ------------ Forward projection texture generation -------------
#
# Another way of creating the texture image is to print the texture projection window.
# To make this work the scene must be exactly placed in the window, the original image is
# used as a texture and the texture coordinates are calculated from the 2D window_projection of
# the objects 3D coordinates.  This suffers limitations of texturing approximations but is
# very fast to calculate.  Finally the maximum sized texture you can make this way will be
# limited by the ability of your hardware.
#
# Make a material which contains the original picture as it's texture
gfx create material ponui texture ponui diffuse 1 1 1 ambient 1 1 1;
# Transform the texture_projection window so that it is placed exactly
#gfx modify window texture_projection view parallel eye_point 0.5 0.5 1000 interest_point 0.5 0.5 0 up_vector 0 1 0 view_angle 0.0810284144 near_clipping_plane 9.4583 far_clipping_plane 3380.07;
# Remove the lines and apply the texture
gfx modify g_element femur_left lines tessellation fine as texture_projection_lines delete;
gfx modify g_element femur_left surfaces tessellation fine material ponui texture projection as texture_projection_surfaces;
# Print the resulting window, it should be very similar to the texture we saved previosly.
# Set the window size to the $texture_width and $texture_height so that 
# onscreen and offscreen printing yield the same size file
gfx modify window texture_projection layout width $texture_width height $texture_height;
gfx print window texture_projection width $texture_width height $texture_height file ponui_mapped2.sgi;
gfx modify window texture_projection layout width 400 height 400;
#
# ------------ Other texture coordinate spaces ------------
#
# Using computed fields there are many other texture coordinate parameterisations that you
# may choose to use.  The idea is to use the texture space to use the detail in the places
# that you need it, generally you aim to have a pretty even distribution.  The xi texture
# coordinate projection that we have been using so far is appropriate when the elements all
# have roughly the same size and the there is a regular number of elements in two directions.
# The question you need to consider is, when you lay out the elements in the texture projection
# window do they each have a reasonable coverage of texture space.
# For this part we will set the system back to the interactive mode that we looked at first.
gfx modify g_element femur_left surfaces tessellation fine material bone data mapped_texture spectrum rgb_spectrum;
gfx modify g_element femur_left lines coordinate xi_texture_coordinates tessellation fine as texture_projection_lines;
gfx modify g_element femur_left surfaces tessellation fine material bone as texture_projection_surfaces data mapped_texture spectrum rgb_spectrum;
#
# Spherical texture coordinates - appropriate for a spherical object (unlike our femur).
# Transform to spherical coordinates, and ignore the radius.  The origin should be near
# the centre of your object.  The reason this coordinate projection is not sensible here
# is that very little of the texture space is used for the ends of the bone, most of it
# is used up on the long main part of the bone.  Furthermore some of the ends of the bone
# will have the same coordinates as the shape is not spherical and so cannot be mapped correctly.
# Finally there are problems where the elements wrap around from one edge of the texture to the
# other as they stretch right across our texture space, recking the image.
gfx define field spherical_texture_coordinatesA coordinate_system spherical coordinate_transformation field coordinates;
# Swap the radius to be last
gfx define field spherical_texture_coordinatesB composite spherical_texture_coordinatesA.2 spherical_texture_coordinatesA.3 spherical_texture_coordinatesA.1
# Scale from (-pi to pi, and pi to zero) to (zero to one) coordinates.
gfx define field spherical_texture_coordinatesC scale field spherical_texture_coordinatesB scale_factors 0.159154943 0.318309886 0.003921569 ;
gfx define field spherical_texture_coordinates offset field spherical_texture_coordinatesC offsets 0.5 0.5 0.5;

# Show the new texture coordinate system.
gfx modify g_element femur_left surfaces tessellation fine as texture_projection_surfaces coordinate spherical_texture_coordinates;
#
#
# Cylindrical texture coordinates - more sensible for our femur.
# Transform to cylindrical coordinates, and ignore the radius.  The origin should be near
# the radius of your object and the z axis along the long axis of your object.
gfx define field cylindrical_texture_coordinatesA coordinate_system cylindrical coordinate_transformation field coordinates;
# Swap the radius to be last
gfx define field cylindrical_texture_coordinatesB composite cylindrical_texture_coordinatesA.2 cylindrical_texture_coordinatesA.3 cylindrical_texture_coordinatesA.1
# Scale from (-pi to pi, and pi to zero) to (zero to one) coordinates.
gfx define field cylindrical_texture_coordinatesC coordinate_system rectangular_cartesian scale field cylindrical_texture_coordinatesB scale_factors 0.159155 0.0021 0.00392157;
gfx define field cylindrical_texture_coordinates coordinate_system rectangular_cartesian offset field cylindrical_texture_coordinatesC offsets 0.5 0.45 0.5;

# Show the new texture coordinate system.
# Unfortunately the z axis of the original coordinate system did not lie exactly along the axis of
# the bone and so the top part of the bone is not correctly unrolled.  We would need to do further
# transformations to the bone prior to the coordinate conversion if we were to get a better projection.
gfx modify g_element femur_left surfaces tessellation fine as texture_projection_surfaces coordinate cylindrical_texture_coordinates;
gfx modify g_element femur_left lines tessellation fine as texture_projection_lines coordinate cylindrical_texture_coordinates;
#
# Restore normal tessellation quality
gfx define tessellation default minimum_divisions "1" refinement_factors "4";

#
gfx;

Files used by this example are:

Name                 Modified     Size

project_texture.com 17-Mar-2014 14k COPYRIGHT 17-Mar-2014 504 femur_exelem.cmiss 17-Mar-2014 170k femur_exnode.cmiss 17-Mar-2014 140k ponui02.jpg 17-Mar-2014 28k

Download the entire example:

Name                               Modified     Size

examples_a_project_texture.tar.gz 09-Mar-2016 869k

Testing status by version:

StatusTestedReal time (s)
i686-linux
cmgui-wxFailureSun Mar 6 00:18:34 20163
last breakTue Feb 24 03:19:00 20154
cmgui-wx-debugFailureSun Mar 6 00:19:38 20166
last breakTue Feb 24 03:22:00 20156
cmgui-wx-debug-memorycheckFailureSun Mar 6 00:24:41 20167
last breakTue Feb 24 03:25:00 20159
cmgui-wx-debug-valgrindFailureSun Mar 6 01:51:33 2016223
last breakSun Mar 6 01:47:00 2016223
x86_64-linux
cmgui-wxFailureSun Mar 6 00:01:32 20160
last breakSun Mar 6 00:01:00 20160
cmgui-wx-debugFailureSun Mar 6 00:01:33 20161
last breakSun Mar 6 00:01:00 20161
cmgui-wx-debug-memorycheckFailureSun Mar 6 00:01:32 20160
last breakSun Mar 6 00:01:00 20160
cmgui-wx-debug-valgrindFailureSun Mar 6 00:02:56 201610
last breakSun Mar 6 00:02:00 201610
cmgui-wx-gcc-cad-debug-valgrindSuccessThu Jan 7 00:02:29 20167

Testing status by file:


Html last generated: Wed Mar 9 16:02:31 2016

Input last modified: Wed Mar 9 15:49:39 2016


CMISS Help / Examples / a / project_texture