A texture map object is created using the function XintView3DCreateTextureMap.
XintView3DTextureMap *XintView3DCreateTextureMap( char *name, int type, int format, int blend, int width, int height, float *image, float **mipmaps, Boolean build_mipmaps, int mag_filter, int min_filter, Boolean clamp_s, Boolean clamp_t, XintView3DAutoTexture *auto_data, Boolean conserve_memory);
Arguments type, format and blend are used to define the style of texture map to generate.
The texture map type can be set to one of the following values:
format defines the format of the data stored in the texture map image:
blend defines the method that the texture map is blended with the underlying color of the object:
width and height define the dimensions of the texture map. Internally, the texture map must have dimensions that are a power of 2. You are not required to create a texture map with this limitation. View3D will convert the texture map internally. If you choose not to use dimensions of a power of 2, pay attention to the conserve_memory flag. If set to True, and the texture map dimensions are not a power of 2, then the next smaller image size will be used. This will cause a loss of some data, but will conserve_memory. If conserve_memory is set to False, and the texture map dimensions are not a power of 2, then the next larger image size will be used. This will preserve all of the original image data, but will require more memory.
For example:
| Original Image Size | conserve_memory False | conserve_memory True |
|---|---|---|
| 28x43 (1204 texels) | 32x64 (2048 texels) | 16x32 (512 texels) |
| 1025x1025 (1,050,625 texels) | 2048x2048 (4,194,304 texels) | 1024x1024 (1,048,576 texels) |
| 1023x1023 (1,046,529 texels) | 1024x1024 (1,048,576 texels) | 512x512 (262,144 texels) |
clamp_s and clamp_t define the behavior of the texture map when the texture map is mapped to coordinates outside its range of [0,1]. If clamp_* is set to True, then any coordinate outside of the range will be clamped to the range. This can cause streaking effects on the object. If set to False, the texture map will be repeated across the object. Repeating a texture map is not always desirable. If a single image should be mapped to fit exactly onto another object, then clamp_* should be set to True, and any artifacts noticed will be attributed to an incorrect evaluation of the texture coordinates in the application.
The texture coordinates are defined during the call to XintView3DAssignTexture.
void XintView3DAssignTexture(
widget view3d,
XintView3DObject *object,
XintVector2f *tcoords,
XintView3DTextureMap *front_texture,
XintView3DTextureMap *back_texture);
The call requires a View3D widget, an object, an optional list of texture coordinates and at least one texture map. The second texture map is only useful for two-sided surfaces. The texture coordinates are required unless automatic texture coordinate generation has been enabled. See Advanced Topics.
mag_filter defines the method that the texturing process uses when the texture map is smaller than the actual screen space (the texture map must be made larger):
min_filter defines the method that the texturing process uses when the texture map is larger than the actual screen space (the texture map must be made smaller):
For example: An original image size of 32x32 will require 5 mipmaps of size 16x16, 8x8, 4x4, 2x2 and 1x1. An original image size of 64x32 will also require only 5 mipmaps of size 32x16, 16x8, 8x4, 4x2, and 2x1.
mipmaps is an array of images (float **). In the example above, memory would be allocated as such:
mipmaps = (float **)malloc(5*sizeof(float *));
mipmaps[0]=(float *)malloc(16*16*num_components*sizeof(float));
mipmaps[1]=(float *)malloc(8*8*num_components*sizeof(float));
mipmaps[2]=(float *)malloc(4*4*num_components*sizeof(float));
mipmaps[3]=(float *)malloc(2*2*num_components*sizeof(float));
mipmaps[4]=(float *)malloc(1*1*num_components*sizeof(float));
where num_components is based on the format of the data and will either be 1 (XintTFORMAT_RED, XintTFORMAT_GREEN, XintTFORMAT_BLUE or XintTFORMAT_LUMINANCE), 2 (XintTFORMAT_LUMINANCE_ALPHA), 3 (XintTFORMAT_RGB) or 4 (XintTFORMAT_RGBA).
If you want to use mipmaps, but do not want to take the time to generate them yourself, you can use the flag build_mipmaps. This will generate all the required mipmaps.
Mipmaps do require significantly more memory than a single texture image. In most cases, using a mag_filter = XintTFILTER_LINEAR and a min_filter = XintTFILTER_LINEAR will produce a high enough quality image.
auto_data is a data structure that is used for the automatic generation of texture coordinates of type XintView3DAutoTexture defined below:
typedef struct {
int mode[2];
float parameters[2][4];
} XintView3DAutoTexture;
Each element of this structure contains 2 elements. The first "[0]" is for 1D textures, or the S coordinate of a 2D texture. The second element "[1]" is only used for 2D textures, and represents the T coordinate. mode must be:
| Defined Constant | Description |
|---|---|
| XintTGEN_OBJECT_LINEAR | Linear mapping across the object. The texture map is fixed to the object. |
| XintTGEN_EYE_LINEAR | Mapping relative to eye coordinates. The texture map is fixed relative to the eye. The object will move through the texture. |
| XintTGEN_SPHERE | Spherical mapping. Useful for environment mapping. |
If mode is set to XintTGEN_OBJECT_LINEAR then parameters specifies a linear mapping using the function:
If mode is set to XintTGEN_EYE_LINEAR then parameters specifies a linear mapping in eye coordinates using the function:
Where M is the viewing transformation matrix.
If mode is set to XintTGEN_SPHERE, then parameters does not need to be defined.
Automatic generation of texture coordinates can be very useful. Here are two suggested uses:
Color Coding Elevations: Create a 1D texture map with the colors ranging from low elevation to high. Enable automatic texture generation with the following values in auto_data:
auto_data.mode[0] = XintTGEN_OBJECT_LINEAR; auto_data.parameters[0][0] = 0.0; auto_data.parameters[0][1] = 0.0; auto_data.parameters[0][2] = 1.0; auto_data.parameters[0][3] = 0.0;
Environment Mapping: This technique will produce object with a mirrored finish. The texture map image needs to be a "fish eye" or "wide-angle" picture within a circle:
Enable automatic texture generation with the following values in auto_data:
auto_data.mode[0] = XintTGEN_SPHERE; auto_data.mode[1] = XintTGEN_SPHERE;