Dragon Book D3d11 chapter exercise Answer (eighth)

Source: Internet
Author: User
Tags mul

The following answers are for reference only, please leave a message.


Chapter 8:texturing


1. Experiment with the ' Crate ' demo by changing the texture coordinates and using different address mode combinations and filtering options. In particular, reproduce the images in figures 8.7, 8.9, 8.10, 8.11, 8.12, and 8.13.


The effects of Figures 8.7, 8.9, 8.10, 8.11, 8.12, and 8.13 are re-implemented by modifying the UV texture coordinates of box in Cratedemo and switching between the different address mode and filtering options.


8.7: is two contrast magification case constant interpolating and linear interpolating difference diagram, the former pixel is too much than the latter smooth. When we have changed the fileter in the samplestate structure in basic.fx, we will find that we can never get the magnification when we pull the lens closer, because we set the perspective The angle of view in the Y direction of the matrix is too large and should be modified to:

void Crateapp::onresize () {d3dapp::onresize (); Xmmatrix P = XMMATRIXPERSPECTIVEFOVLH (0.01f*mathhelper::P i, Aspectratio (), 1.0f, 1000.0f); xmstorefloat4x4 (&mproj, P);}
Mag_point:mag_linear:



8.9: Here the filter setting is independent of Mag, related to Min and MIP.

Point : LINEAR:

ANISOTROPIC:


8.10,11,12,13:

Add a member variable about the box texture Extrude in the class       xmfloat4x4 mboxtexscale;//init when assigned Xmmatrix Boxtexscale = xmmatrixscaling (4.0f,4.0f,0.0f ); xmstorefloat4x4 (&mboxtexscale, Boxtexscale);//Drawscene with EFFECTS11 settings to the FX file effects::basicfx-> Settextransform (xmloadfloat4x4 (&mboxtexscale));</span>
basic.fx: Modify the Samplestate in Addressu, ADDRESSV can, note that the use of border when you need to specify BorderColor, the format is FLOAT4 Rgba.

Wrap:mirror:

Clamp:border:


Addressu and ADDRESSV can set different address mode to achieve the blending effect:

such as Addressu:wrap + Address:border = ... etc ...




2. Using the DirectX Texture Tool, we can manually specify each mipmap level (File->open Onto this Surface). Create a DDS file with a mipmap chain like the one in Figure 8.19, with a different textual description or color on each L Evel so, can easily distinguish between each mipmap level.

Modify the Crate demo by using this texture and has the camera zoom in and out so the can explicitly see the mipmap Levels changing. Try both point and linear mipmap filtering.


Open the DX SDK folder directory under the Utilities\bin Dxtex.exe (source in the source folder, visual is written by MFC), use it very simple, first file->open a picture, or mouse dragged in, and then format- >generate MIP Maps, automatically generate a series of MIP Chains, press PgUp and PGDN to switch to the different level of MIP Chains, this time File->open Onto this Surface to select a different image to modify this level of Texture~,save,ok.


。。 Recorded several times GIF, either the file is too large to upload, or the beginning of the Nico is ignored by the software. 2333




3.3. Given-textures of the same size, we can combine them via different operations to obtain a new image. More generally, which is called multitexturing, where multiple textures was used to achieve a result. For example, we can add, subtract,or (component-wise) multiply the corresponding texels of the textures.

Figure 8.20 shows the result of component-wise multiplying, textures to get a fireball-like result. For this exercise, modify the ' Crate ' demo by combining the ' source textures in ' 8.20 in a pixel shader to Produc E The fireball texture over each of the cube face. (The image files for this exercise is downloaded from the book ' s website.) Note that you'll have to modify the basic.fx-to-support more than one texture.



Simple but there are a lot of steps, you can find all the references of Mdiffusemap, and then change them in turn, I do the following:

BASIC.FX:

1. Create a new two global variable texture2d gdiffusemapflare, Gdiffusemapflarealpha; the non-basic data type cannot be placed inside the cbuffer.

2. Modify pixel Shader: Inside the PS function:

    Default to multiplicative identity.    FLOAT4 Texcolor = FLOAT4 (1, 1, 1, 1); Float4 Texflarecolor = FLOAT4 (1, 1, 1, 1); Float4 Texflarealphacolor = FLOAT4 (1, 1, 1, 1);    if (gusetexure) {//Sample texture.//Texcolor = gdiffusemap.sample (Samanisotropic, PIN. TEX); Texflarecolor = Gdiffusemapflare.sample (Samanisotropic, PIN. TEX); Texflarealphacolor = Gdiffusemapflarealpha.sample (Samanisotropic, PIN. TEX); texcolor = Texflarecolor*texflarealphacolor;}


CrateDemo.cpp:

1. Add a new member variable id3d11shaderresourceview *mdiffusemapflare, *mdiffusemapflarealpha.

2. The constructor is initialized to 0, and the destructor releasecom releases the interface.

3. Call D3dx11createshaderresourceviewfromfile to import the corresponding DDS file from the file into the two variables above, the HR macro acts as an exception handler.

4. In this chapter of the code, the original BUILDFX function calls related variables and operations encapsulated in the effect class, so bound to the Gdiffusemapflare in Basic.fx , The Gdiffusemapflarealpha process is done in the effects class:

Add two new public methods to the Effects.h:

void Setdiffusemapflare (id3d11shaderresourceview* Tex)   {Diffusemapflare->setresource (tex);} void Setdiffusemapflarealpha (id3d11shaderresourceview* Tex)   {Diffusemapflarealpha->setresource (tex);}
In-class definition, which is inline by default, so this encapsulation efficiency is OK.

Within Effects.cpp, in the constructor of the effects class:

Diffusemap        = Mfx->getvariablebyname ("Gdiffusemap")->asshaderresource ();D iffusemapflare   = mFX- >getvariablebyname ("Gdiffusemapflare")->asshaderresource ();D iffusemapflarealpha   = mFX-> Getvariablebyname ("Gdiffusemapflarealpha")->asshaderresource ();

5. Finally return to Drawscene:

Effects::basicfx->setdiffusemap (MDIFFUSEMAPSRV); Effects::basicfx->setdiffusemapflare (Mdiffusemapsrvflare); Effects::basicfx->setdiffusemapflarealpha (Mdiffusemapsrvflarealpha);
This way, the binding of the two global variables in the. fx file completes and ends.



If you need to load a lot of textures, you can use the DxTex tool to make a larger texture map, so that in the drawscene time will be less calls to repeat the function, the book refers to the term texture Atlas.






4. Modify the solution to Exercise 3 by rotating the fireball texture as a function of time over each cube face.


Add the new member variable Xmmatrix mboxtexrotate; and add the code in Updatescene:

Modifying the U and V components at the same time is equivalent to modifying X and Y at the same time (x, Y, z), which rotates around the z axis.

static float Z = 0.0f; z + = Dt;if (Z > 6.28f) z = 0.0f;mballtexrotate = Xmmatrixrotationz (z);
The matrix of scaling, rotation, and other processing of textures is then multiplied in drawscene into the mtextransform of the float4x4 type:

xmstorefloat4x4 (&mtextransform, mballtexscale*mballtexrotate);
and passed to shader:

Effects::basicfx->settextransform (xmloadfloat4x4 (&mtextransform));


Obviously, there is a problem: the rotation center point should be set at the texture coordinates (0.5, 0.5) rather than (0,0) to achieve the normal flame rotation effect.

=.=, my plan is to work with the UV coordinates of the vertex coordinates in the vertex shader, to establish a UV coordinate system for the origin (0.5, 0.5), when the original (0, 0) is now ( -0.5, -0.5), and the original (1.0, 1.0) is now (0.5, 0.5)  , then multiply the rotate matrix, and then restore the coordinate system to the UV coordinate system with the origin (0, 0), which is the Tex coordinate of each vertex +=0.5; The code is as follows:

Vertexout VS (Vertexin vin) {vertexout vout;//Transform to World space Space.vout.PosW    = Mul (vin. POSL, 1.0f), Gworld). Xyz;vout. NORMALW = Mul (vin. Normall, (float3x3) gworldinvtranspose);//Transform to homogeneous clip Space.vout.PosH = Mul (vin. POSL, 1.0f), gworldviewproj);//Output vertex attributes for interpolation across Triangle.vout.Tex = Mul (FLOAT4 (Vin.tex). x-0.5f, Vin. tex.y-0.5f, 0.0f, 1.0f), Gtextransform). Xy;vout. Tex + = 0.5f;return vout;}
Note:
My gtextransform Here only contains the rotation matrix, if the gtextransform is also offset, scaling, then note: These transformations will be (0,5, 0.5) as the center.


( small fireball with no fluctuation)



5. This chapter's downloadable directory contains a folder with the frames of a fire animation designed to being played over 4 seconds (frames per second). Figure 8.21 shows the first frames.

Modify the "Crate" demo by playing this animation the cube. (Hint:load the images to an array of texture objects. Start by using the first frame texture, and every 1/30th of a second, increment to the next frame texture. After the 120th frame texture, roll back to the first texture and repeat the process.)

This is sometimes called page flipping, because it's reminiscent of flipping the pages in a flip book.

Note:working with individual texture animation frames One-by-one like this is inefficient. It would is better to put all the frames in one texture Atlas, and then offset the texture coordinates every 1/30th of a s Econd to the next frame of the animation. But for the sake of the exercise, the inefficient method is fine.

1. Add a new member variable id3d11shaderresourceview* mdiffusemapsrvarray[120]; , the constructor initializes, and the destructor releases the interface.

2. In init Nega, load 120 different BMP textures into the Mdiffusemapsrvarray array:

for (int i=0; i<120; ++i) {wchar_t bmppath[50];if (i+1<10) swprintf_s (Bmppath, L "Fireanim/fire00%u.bmp", i+1); else if (i+1<100) swprintf_s (Bmppath, L "fireanim/fire0%u.bmp", i+1); elseswprintf_s (Bmppath, L "fireanim/fire%u.bmp", i+ 1);//outputdebugstringw (Bmppath); HR (D3dx11createshaderresourceviewfromfile (md3ddevice, BmpPath, 0, 0, & Mdiffusemapsrvarray[i], 0));}
I didn't notice here at first, fire001.bmp---> fire010.bmp---> fire120.bmp, note the naming format of BMP files, the number of digits in the first digit index is single digit, 10 digits, and the number of 0 in front of the hundred is different. Of course.. I think it would be convenient to use the batch command to process the next file named 1-120.

3.UpdateScene:

static float T = 0.0f;t + = dt;if (T > 0.03f) {++mindex;t = 0.0f;} if (mindex >=) mindex = 0;
4.DrawScene:

Effects::basicfx->setdiffusemap (Mdiffusemapsrvarray[mindex]);





6. Let P0, p1, and P2 are the vertices of a 3D triangle with respective texture coordinates q0, Q1, and Q2. Recall from§8.2 A arbitrary point on a 3D triangle P (s, t) = P0 + S (p1? p0) + t (P2? p0) where S≥0, t≥0, S + t≤1, its texture
coordinates (u, v) is found by linearly interpolating the vertex texture coordinates across the 3D triangle by the SA Me s, t parameters:
(U, v) = q0 + S (Q1? q0) + t (Q2? q0)
(a) Given (U, v) and q0, Q1, and Q2, solve for (s, T) in terms of U and v.
consider the vector equation (u, v)? q0 = S (Q1? q0) + t (Q2? q0).
(b) Express p as a function of u and V; that's, find a formula P = P (U, v).
(c) Compute P/?u and p/?v and give a geometric interpretation of what these vectors mean.

... It took a long time to think out, ashamed.

Note: The symbols in the following q0u, q0v, respectively, represent the q0 point's U, v coordinates, q1u...q2v similar.


(a)

move Q0 to the left of the equation to (U, v)? q0 = S (Q1? q0) + t (Q2? q0) after, written in matrix form:

, the symbols q0u, q0v respectively represent the U, v coordinates of q0 points, and the other symbols are the same. In fact, is the 2D coordinate transformation form:, so to find the inverse, so that both sides at the same time right multiply:



(b)

P (s, t) means that the arguments are S and T, and the dependent variable is P:

In order to get P (U, v), we are going to convert s and T to U,v, so the conclusion of substituting (a) can be slightly.


(c) To obtain the derivative of (b) on the deviation of U and V and give a geometrical interpretation of the meaning, unsure whether or not it is correct, left to think.




7. Modify the "Lit Skull" Demo from the previous chapter by adding textures to the ground, columns, and spheres (Figure 8. The textures can found in this chapters code directory.

simple but need to be careful, here I use Chapter7 's LitSkullDemo.cpp to begin to modify (note: vertex and Effect class use Chapter8 code):

1. Look for the posnormal that appear in the code, modify it to BASIC32, and before you create VertexBuffer, Vertices[i]. Tex assignment value.

2. Add Mdiffusemapsrvbox\grid\sphere\cylinder of the id3d11shaderresourceview* type, and xmfloat4x4 Mtextrans( Although you do not need to transform the texture coordinates, but basic.fx in vertex shader calculation Vout.tex need to multiply gtextransform, of course you can also directly change shader code, will write a few lines) member variables.

3. After initializing these variables, use the effect class set all the required parameters to shader to draw.



The process is similar to p.319 content (for example, suppose we use the texture atlas as in Figure 8.3 ... Errata: Figure 8.3 should be changed to Figure 8.4), and you can use texture Atlas to improve performance.






















Dragon Book D3d11 chapter exercise Answer (eighth)

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.