Create a concave-convex texture

Source: Internet
Author: User
[1] What is concave-convex ing
The process of convex/concave ing involves extracting the texture and illuminating the surface with the internal information of the texture, so that the surface shows more details than it actually has. This process is done by using the normal value stored in the texture image rather than the surface normal value when the ry is illuminated. You can obtain the appearance details of an object by changing the normal value or pixel in the image. For example, you can use a convex/concave ing to create a cement crack or crawler skin scale. The difference between texture ing and convex/concave ing is that texture is used for shadow to reach the surface, while convex/concave map is used for lighting to reach the surface. For example, you can use a convex/concave ing to create a cement crack or crawler skin scale. The difference between texture ing and convex/concave ing is that texture is used for shadow to reach the surface, while convex/concave map is used for lighting to reach the surface.
Note]
Do not confuse the normal ing and convex/concave ing technologies. Although both technologies use an image named "convex or concave texture", there are still differences. The difference lies in how images are created. The image used by the convex/concave ing technology is created in a gray (black to white) 2D texture image. Sometimes this image is also called a "height image ". Normal ing technology uses the image to create details in a high-resolution model (role, object, and so on) and save it to the texture, so that when used in Convex/concave ing, the low-resolution model looks like or similar to the high-resolution model. These two topics are beyond the scope of this book, but note that normal ing is a convex/concave ing that is used to make the low-resolution model look like a high-resolution model, the convex/concave ing extracts all 2D images and creates the normal from them.
With convex/concave ing, you can use an image to extract data and use it for illumination, instead of rendering the surface with a texture.
[2] creating convex and concave textures
Sometimes a convex or concave texture refers to a normal texture, which can be created using several tools. First, you can select Photoshopcs Here, you can use a plug-in to convert an image into a convex or concave texture. The second method is to use Direct3d Function d3dxcomputenormalmap (). The third method is CompiledCodeMedium costAlgorithmLine textures. For the first method, you need to check your favorite image editing software based on the plug-in to see if it is available. Such as Adobe photoshopcs with NVIDIA normal map plug-in tools.
Create with direct3d
Hresult winapi d3dxcomputenormalmap (
Lpdirect3dtexture9 ptexture,
Lpdirect3dtexture9 psrctexture,
Const paletteentry * psrcpalette,
DWORD flags,
DWORD channel,
Float Amplitude
);
The d3dxcomputenormalmap () function prototype is very simple. The ptexture parameter is a direct3d texture object that saves the normal map. The psrctexture parameter is the original image to be converted to a normal texture. The parameter psrcpalette is the color palette of the original resource texture, and the parameter amplits is the added value or reduced value of the normal map. You may not need to use the color palette, but at least need to know the options to choose from.

D3dxcomputenormalmap () function can be used as an identifier parameter value table

D3dx_normal_0000_u  
D3dx_normal_1__v  
D3dx_normal_mirror  
D3dx_normal_invertsign  
D3dx_normal_compute_occlusion  

Channel Parameter Value List of d3dxcomputenormalmap () function

D3dx_channel_red Use the red channel when calculating the normal map
D3dx_channel_green Use the green channel when calculating the normal map
D3dx_channel_blue Use the blue channel when calculating the normal map
D3dx_channel_alpha Use alpha channel when calculating normal maps
D3dx_channel_luminance Calculates the brightness values of the red, green, and blue channels when the normal map is used.

[3] exampleProgram
/*
Demo name: d3d normal maps
Author: Allen Sherrod
Chapter: CH 4
*/

# Include <d3d9. h>
# Include <d3dx9. h>

# Define window_class "ugpdx"
# Define window_name "creating d3d normal maps"
# Define windows _ width 640
# Define window_height 480

// Function prototypes...
Bool initialized3d (hwnd, bool fullscreen );
Bool initializeobjects ();
Void renderscene ();
Void Shutdown ();

// Direct3d object and device.
Lpdirect3d9 g_d3d = NULL;
Lpdirect3ddevice9 g_d3ddevice = NULL;

// Matrices.
D3dxmatrix g_projection;
D3dxmatrix g_viewmatrix;

// Vertex buffer to hold the geometry.
Lpdirect3dvertexbuffer9 g_vertexbuffer = NULL;

// Holds a texture image.
Lpdirect3dtexture9 g_texture = NULL, g_normalmap = NULL;

// A structure for our custom vertex type
Struct std3dvertex
{
Float x, y, z;
Unsigned long color;
Float tu, TV;
};

// Our custom fvf, which describes our custom vertex Structure
# Define d3dfvf_vertex (d3dfvf_xyz | d3dfvf_diffuse | d3dfvf_tex1)

Lresult winapi msgproc (hwnd, uint MSG, wparam, lparam)
{
Switch (MSG)
{
Case wm_destroy:
Postquitmessage (0 );
Return 0;
Break;

Case wm_keyup:
If (wparam = vk_escape) postquitmessage (0 );
Break;
}

Return defwindowproc (hwnd, MSG, wparam, lparam );
}

Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr using line, int show)
{
// Register the window class
Wndclassex WC = {sizeof (wndclassex), cs_classdc, msgproc, 0l, 0l,
Getmodulehandle (null), null,
Window_class, null };
Registerclassex (& WC );

// Create the application's window
Hwnd = createwindow (window_class, window_name, ws_overlappedwindow,
100,100, window_width, window_height,
Getasktopwindow (), null, WC. hinstance, null );

// Initialize direct3d
If (initialized3d (hwnd, false ))
{
// Show the window
Showwindow (hwnd, sw_showdefault );
Updatewindow (hwnd );

// Enter the message loop
MSG;
Zeromemory (& MSG, sizeof (MSG ));

While (msg. message! = Wm_quit)
{
If (peekmessage (& MSG, null, 0u, 0u, pm_remove ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Else
Renderscene ();
}
}

// Release any and all resources.
Shutdown ();

// Unregister our window.
Unregisterclass (window_class, WC. hinstance );
Return 0;
}

Bool initialized3d (hwnd, bool fullscreen)
{
D3ddisplaymode displaymode;

// Create the d3d object.
G_d3d = direct3dcreate9 (d3d_sdk_version );
If (g_d3d = NULL) return false;

// Get the desktop display mode.
If (failed (g_d3d-> getadapterdisplaymode (d3dadapter_default, & displaymode )))
Return false;

// Set up the structure used to create the d3ddevice
D3dpresent_parameters d3dpp;
Zeromemory (& d3dpp, sizeof (d3dpp ));

If (fullscreen)
{
D3dpp. incluwed = false;
D3dpp. backbufferwidth = window_width;
D3dpp. backbufferheight = window_height;
}
Else
D3dpp. paiwed = true;
D3dpp. swapeffect = d3dswapeffect_discard;
D3dpp. backbufferformat = displaymode. format;

// Create the d3ddevice
If (failed (g_d3d-> createdevice (d3dadapter_default, d3ddevtype_hal, hwnd,
D3dcreate_software_vertexprocessing, & d3dpp, & g_d3ddevice )))
{
Return false;
}

// Initialize any objects we will be displaying.
If (! Initializeobjects () return false;

Return true;
}

Bool initializeobjects ()
{
// Fill in our structure to draw an object.
// X, Y, Z, color, texture coords.
Std3dvertex objdata [] =
{
{-0.3f,-0.4f, 0.0f, d3dcolor_xrgb (255,255,255), 0.0f, 1.0f },
{0.3f,-0.4f, 0.0f, d3dcolor_xrgb (255,255,255), 1.0f, 1.0f },
{0.3f, 0.4f, 0.0f, d3dcolor_xrgb (255,255,255), 1.0f, 0.0f },

{0.3f, 0.4f, 0.0f, d3dcolor_xrgb (255,255,255), 1.0f, 0.0f },
{-0.3f, 0.4f, 0.0f, d3dcolor_xrgb (255,255,255), 0.0f, 0.0f },
{-0.3f,-0.4f, 0.0f, d3dcolor_xrgb (255,255,255), 0.0f, 1.0f}
};

// Create the vertex buffer.
If (failed (g_d3ddevice-> createvertexbuffer (sizeof (objdata), 0,
D3dfvf_vertex, d3dpool_default, & g_vertexbuffer, null) return false;

// Fill the vertex buffer.
Void * PTR;
If (failed (g_vertexbuffer-> lock (0, sizeof (objdata), (void **) & PTR, 0) return false;
Memcpy (PTR, objdata, sizeof (objdata ));
G_vertexbuffer-> unlock ();

// Load the texture image from file.
If (d3dxcreatetexturefromfile (g_d3ddevice, "heightmap. TGA", & g_texture )! = D3d_ OK)
Return false;

D3dsurface_desc DESC;
G_texture-> getleveldesc (0, & DESC );

// Create normal map texture the size of the original.
If (d3dxcreatetexture (g_d3ddevice, DESC. Width, DESC. Height, 0, 0, d3dfmt_a8r8g8b8,
D3dpool_managed, & g_normalmap )! = D3d_ OK) return false;

// Compute the normal map.
If (d3dxcomputenormalmap (g_normalmap, g_texture, 0, d3dx_normalmap_mirror,
D3dx_channel_green, 10 )! = D3d_ OK) return false;

// Set the image states to get a good quality image.
G_d3ddevice-> setsamplerstate (0, d3dsamp_minfilter, d3dtexf_linear );
G_d3ddevice-> setsamplerstate (0, d3dsamp_magfilter, d3dtexf_linear );

// Set default rendering states.
G_d3ddevice-> setrenderstate (d3drs_lighting, false );
G_d3ddevice-> setrenderstate (d3drs_cullmode, d3dcull_none );

// Set the projection matrix.
D3dxmatrixperspectivefovlh (& g_projection, 45.0f, window_width/window_height,
0.1f, 1000.0f );
G_d3ddevice-> settransform (d3dts_projection, & g_projection );

// Define camera information.
D3dxvector3 camerapos (0.0f, 0.0f,-1.0f );
D3dxvector3 lookatpos (0.0f, 0.0f, 0.0f );
D3dxvector3 updir (0.0f, 1.0f, 0.0f );

// Build view matrix.
D3dxmatrixlookatlh (& g_viewmatrix, & camerapos, & lookatpos, & updir );

Return true;
}

Void renderscene ()
{
// Clear the backbuffer.
G_d3ddevice-> clear (0, null, d3dclear_target,
D3dcolor_xrgb (0, 0), 1.0f, 0 );

// Begin the scene. Start rendering.
G_d3ddevice-> beginscene ();

// Apply the view (CAMERA ).
G_d3ddevice-> settransform (d3dts_view, & g_viewmatrix );

// Draw square.
G_d3ddevice-> settexture (0, g_normalmap );
G_d3ddevice-> setstreamsource (0, g_vertexbuffer,
0, sizeof (std3dvertex ));
G_d3ddevice-> setfvf (d3dfvf_vertex );
G_d3ddevice-> drawprimitive (d3dpt_trianglelist, 0, 2 );

// End the scene. Stop rendering.
G_d3ddevice-> endscene ();

// Display the scene.
G_d3ddevice-> present (null, null );
}

Void Shutdown ()
{
If (g_d3ddevice! = NULL) g_d3ddevice-> release ();
G_d3ddevice = NULL;

If (g_d3d! = NULL) g_d3d-> release ();
G_d3d = NULL;

If (g_vertexbuffer! = NULL) g_vertexbuffer-> release ();
G_vertexbuffer = NULL;

If (g_texture! = NULL) g_texture-> release ();
G_texture = NULL;

If (g_normalmap! = NULL) g_normalmap-> release ();
G_normalmap = NULL;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.