Direct3d11 texture map of "Cold River Snow"

Source: Internet
Author: User

Today is New Year's day, first of all, I wish you a happy holiday. Due to the preparation to make a game, the teacher code to pull faster, so there are many omissions, but also please point out, from the mistakes can learn more things, know enough and improve.
Light enters the human eye and people can see the world. And the texture defines what the world looks like. In Direct3D, add texture maps to objects to make them colorful. Before describing how to add texture maps to a program, let's introduce the basics of texture mapping, and in the course of learning Direct3D9, I have a preliminary knowledge of some textures and summarize what I learned into the following article:

http://blog.csdn.net/lkysyzxz/article/details/53817343

The following highlights how the DIRECT3D11 programmable rendering pipeline adds texture maps to objects.

Loading map Files

In the DIRECT3D11 program, we have to define Id3d11shaderresourceview to save the mapping resource.
Next, we load the graphics file into memory through the D3dx11createshaderresourceviewfromfile function. of the function
The prototype is as follows:

HRESULT d3dx11createshaderresourceviewfromfile (
  _in_  id3d11device             *pdevice,
  _in_  LPCTSTR                  psrcfile,
  _in_  d3dx11_image_load_info   *ploadinfo,
  _in_  id3dx11threadpump        * Ppump,
  _out_ id3d11shaderresourceview **ppshaderresourceview,
  _out_ HRESULT                  *phresult
);
Id3d11device *pdevice: This is the device interface pointer, which is the LPCTSTR created at the time of initialization psrcfile: This is the file name, which is the filename of our decal file D3dx11_image_load_info *ploadinfo: Defines the information that loads the picture, basically is the width height, the depth and so on information, generally sets the null. Id3dx11threadpump *ppump: The thread that loads the texture, set to NULL indicates that no other thread is required to perform the operation. Id3d11shaderresourceview **ppshaderresourceview: Call this function to initialize this parameter, fill in the address of the Id3d11shaderresourceview interface pointer we define, Used to manage the loaded textures. HRESULT *phresult: If you use a different line loads to load the resource, and you need to return a value, the address of the return value is set to this parameter.

Here is an example invocation:

Hr=d3dx11createshaderresourceviewfromfile (D3dsys->d3ddevice (),
L ".//res//box.bmp", NULL, NULL, &M_ Ptexture, NULL);
        if (FAILED (HR)) {
            return hr;
        }
defining the vertex structure

In order to use textures, the structure of the vertices changes. Adds a texture coordinate more

struct Vertex {
    XMFLOAT3 Position;
    XMFLOAT3 Norm;
    XMFLOAT2 Tex;  The newly added texture coordinates
};

In the effect file also to make corresponding changes, the creation of input layout, the description of the parameters should also be modified accordingly.

The input structure of the vertex shader
struct vs_input
{
    float4 pos:position;   Vertex coordinates
    FLOAT3 norm:normal;    Normal vector
    float2 tex:texcoord0;   Texture coordinates
};

The input structure of the pixel shader is
struct ps_input
{
    float4 pos:sv_position;           Vertex coordinates
    FLOAT3 norm:texcoord0;            Normal vector
    float4 viewdirection:texcoord1;   Viewpoint direction
    Float4 Lightvector:texcoord2;     Valid for Point light and Spotlight,
                                        //First 3 components record "light vector"
                                        //Last component record illumination distance
    float2 Tex:texcoord3;   Texture coordinates
};

Adding texture variables and setting sampler states in the effect file allows you to increase the sampling accuracy by setting these parameters:

Texture2d Texture;
Samplerstate Sampler
{
    Filter = min_mag_mip_linear;
    Addressu = WRAP;
    ADDRESSV = WRAP;
};

The settings for these parameters can refer to MSDN's instructions, or take a look at one of my other notes, but the latter has little reference value for DX11.

http://blog.csdn.net/lkysyzxz/article/details/53831813

Add some code in the vertex shader and pixel shader.

The output at the end of the vertex shader
. Tex = input. Tex;

Pixel shader End
float4 texcolor = Float4 (1.0f, 1.0f, 1.0f, 1.0f);
Texcolor = texture.sample (Sampler, input. TEX);
Finalcolor = Texcolor*finalcolor;
FINALCOLOR.A = FINALCOLOR.A*TEXCOLOR.A;
Sample program Core code LightScene.h
#pragma once #include "dxUtil.h" #include "Light.h" struct Vertex {XMFLOAT3 Position;
    XMFLOAT3 Norm;
XMFLOAT2 Tex;
};
Class Scene;
Class Cube;
    Class Cube {private:material m_mat;
    Id3d11buffer *m_pvertexbuffer;//vertex cache UINT M_ustrid;
    UINT M_uoffset; Id3d11buffer *m_pindexbuffer;//Index cache xmfloat4x4 M_world; Position of the object Id3d11shaderresourceview *m_ptexture;//texture Public:cube (): M_pvertexbuffer (NULL), M_pindexbuffer (N
    ULL) {xmstorefloat4x4 (&m_world, xmmatrixidentity ());
    } id3d11buffer * * Getvertexbuffer () {return &m_pVertexBuffer;
    } id3d11buffer *getindexbuffer () {return m_pindexbuffer;
    } UINT Getstrid () {return m_ustrid;
    } UINT GetOffset () {return m_uoffset;
    } Material getmaterial () {return m_mat;
    } Xmmatrix Getworldposition () {return xmloadfloat4x4 (&m_world);

  }  HRESULT Init (directsystem* D3dsys) {HRESULT hr; Vertex vertices[] = {{XMFLOAT3 (1,1,1), XMFLOAT3 (1,1,1), XMFLOAT2 (0,0)}, {XMFLOAT3 ( -1,1,1), Xmfloa T3 ( -1,1,1), XMFLOAT2 (1,0)}, {XMFLOAT3 ( -1,-1,1), XMFLOAT3 ( -1,-1,1), XMFLOAT2 (All)}, {XMFLOAT3 (1,-1, 1), XMFLOAT3 (1,-1,1), XMFLOAT2 (0,1)}, {XMFLOAT3 (1,1,-1), XMFLOAT3 (1,1,-1), XMFLOAT2 (0,0)}, {Xmfloat
            3 ( -1,1,-1), XMFLOAT3 ( -1,1,-1), XMFLOAT2 (1,0)}, {XMFLOAT3 ( -1,-1,-1), XMFLOAT3 ( -1,-1,-1), XMFLOAT2 (All)}, {XMFLOAT3 (1,-1,-1), XMFLOAT3 (1,-1,-1), XMFLOAT2 (0,1)}, {XMFLOAT3 (1,1,1), XMFLOAT3 (1,1,1), XMFLOAT2 (0,0)} , {XMFLOAT3 ( -1,1,1), XMFLOAT3 ( -1,1,1), XMFLOAT2 (3,0)}, {XMFLOAT3 ( -1,1,-1), XMFLOAT3 ( -1,1,-1), Xmflo AT2 (3,3)}, {XMFLOAT3 (1,1,-1), XMFLOAT3 (1,1,-1), XMFLOAT2 (0,3)}, {XMFLOAT3 (1,-1,1), XMFLOAT3 (1,-1 , 1), XMFLOAT2 (0,0)}, {XMFLOAT3 ( -1,-1,1), XMFLOAT3 ( -1,-1,1), XMFLOAT2(1,0)}, {XMFLOAT3 ( -1,-1,-1), XMFLOAT3 ( -1,-1,-1), XMFLOAT2 (All)}, {XMFLOAT3 (1,-1,-1), XMFLOAT3 (1,- 1,-1), XMFLOAT2 (0,1)}, {XMFLOAT3 ( -1,1,1), XMFLOAT3 ( -1,1,1), XMFLOAT2 (0,0)}, {XMFLOAT3 ( -1,-1,1)}, X MFLOAT3 ( -1,-1,1), XMFLOAT2 (1,0)}, {XMFLOAT3 ( -1,1,-1), XMFLOAT3 ( -1,1,-1), XMFLOAT2 (0,1)}, {Xmfloat
            3 ( -1,-1,-1), XMFLOAT3 ( -1,-1,-1), XMFLOAT2 (), {XMFLOAT3 (1,1,1), XMFLOAT3 (1,1,1), XMFLOAT2 (0,0)},
            {XMFLOAT3 (1,-1,1), XMFLOAT3 (1,-1,1), XMFLOAT2 (1,0)}, {XMFLOAT3 (1,1,-1), XMFLOAT3 (1,1,-1), XMFLOAT2 (0,1)},
        {XMFLOAT3 (1,-1,-1), XMFLOAT3 (1,-1,-1), XMFLOAT2 ()}};
            WORD indexs[] = {0,1,2, 0,2,3, 4,6,5, 4,7,6, 8,10,9, 8,11,10, 12,13,14, 12,14,15, 16,18,19, 16,19,17, 2

        0,21,23, 20,23,22}; D3d11_buffer_DESC VertexBuffer;
        :: ZeroMemory (&vertexbuffer, sizeof (vertexbuffer));
        Vertexbuffer.bindflags = D3d11_bind_vertex_buffer;
        Vertexbuffer.bytewidth = sizeof (Vertex) * ARRAYSIZE (vertices);
        vertexbuffer.cpuaccessflags = 0;

        Vertexbuffer.usage = D3d11_usage_default;
        D3d11_subresource_data Initvertexdata;
        :: ZeroMemory (&initvertexdata, sizeof (Initvertexdata));

        Initvertexdata.psysmem = vertices;
        Hr=d3dsys->d3ddevice ()->createbuffer (&vertexbuffer, &initvertexdata,&m_pvertexbuffer);
        if (FAILED (HR)) {return hr;
        } M_ustrid = sizeof (Vertex);

        M_uoffset = 0;
        D3d11_buffer_desc IndexBuffer;
        :: ZeroMemory (&indexbuffer, sizeof (IndexBuffer));
        Indexbuffer.bindflags = D3d11_bind_index_buffer;
        Indexbuffer.bytewidth = sizeof (WORD) *arraysize (INDEXS);

        Indexbuffer.usage = D3d11_usage_default; D3d11_subresource_daTA Initindexdata;
        :: ZeroMemory (&initindexdata, sizeof (Initindexdata));
        Initindexdata.psysmem = Indexs;
        Hr=d3dsys->d3ddevice ()->createbuffer (&indexbuffer, &initindexdata, &m_pindexbuffer);
        if (FAILED (HR)) {return hr;
        } m_mat.ambient = XMFLOAT4 (1.0f, 1.0f, 1.0f, 1.0f);
        M_mat.diffuse = XMFLOAT4 (1.0f, 1.0f, 1.0f, 1.0f);
        M_mat.specular = XMFLOAT4 (1.0f, 1.0f, 1.0f,1.0f);

        M_mat.power = 5.0f; Hr=d3dx11createshaderresourceviewfromfile (D3dsys->d3ddevice (), L ".//res//box.bmp", NULL, NULL, &m_pTexture,
        NULL);
        if (FAILED (HR)) {return hr;
    } return S_OK; } HRESULT setcubeparm (Directsystem *d3dsys,id3dx11effect *effect) {d3dsys->d3ddevicecontext ()->IASetVer
        Texbuffers (0, 1, &m_pvertexbuffer, &m_ustrid, &m_uoffset); D3dsys->d3ddevicecontext ()->iasetindexbuffer (M_pindexbuffer, Dxgi_format_r16_uint, 0);

        D3dsys->d3ddevicecontext ()->iasetprimitivetopology (d3d11_primitive_topology_trianglelist);
        Effect->getvariablebyname ("World")->asmatrix ()->setmatrix ((float*) &m_world);
        Effect->getvariablebyname ("Matambient")->asvector ()->setfloatvector ((float*) &m_Mat.ambient);
        Effect->getvariablebyname ("Matdiffuse")->asvector ()->setfloatvector ((float*) &m_Mat.diffuse);
        Effect->getvariablebyname ("Matspecular")->asvector ()->setfloatvector ((float*) &m_Mat.specular);
        Effect->getvariablebyname ("Matpower")->asscalar ()->setfloat (m_mat.power);
        Effect->getvariablebyname ("Texture")->asshaderresource ()->setresource (m_ptexture);
    return S_OK;
        } VOID Update (float deltatime) {static float angle = 0;
        Angle + = Deltatime;
        if (angle > 6.28f) angle = 0.0f;
        Xmmatrix World;
        world=xmloadfloat4x4 (&m_world); WoRld=xmmatrixrotationy (angle);
    xmstorefloat4x4 (&m_world, World);
        } ~cube () {safe_release (m_pvertexbuffer);
        Safe_release (M_pindexbuffer);
    Safe_release (m_ptexture);




}
};
    Class Scene {Private:id3dx11effect *m_peffect;
    Id3dx11effecttechnique *m_ptechnique;  Id3d11inputlayout *m_pinputlayout;
    Input layout Cube *m_pcube;
Light m_pointlight; Public:scene (): M_peffect (null), M_ptechnique (null), m_pinputlayout (null), M_pcube (null) {} Hresu
        LT Draw (Directsystem *d3dsys) {Xmmatrix view;
        Xmmatrix projection;
        Xmvector eyeposition;
        view = D3dsys->getviewmatrix ();
        projection = D3dsys->getprojectionmatrix ();

        Eyeposition = D3dsys->geteyeposition ();
        M_pcube->setcubeparm (D3dsys,m_peffect);
        M_peffect->getvariablebyname ("View")->asmatrix ()->setmatrix ((float*) &view); M_peffect->getvariablebyname ("Projection")-&Gt
        Asmatrix ()->setmatrix ((float*) &projection);


        M_peffect->getvariablebyname ("Eyeposition")->asvector ()->setfloatvector ((float*) &eyePosition);
        M_peffect->getvariablebyname ("type")->asscalar ()->setint (M_pointlight.type); M_peffect->getvariablebyname ("Lightposition")->asvector ()->setfloatvector ((float*) &m_
        Pointlight.position); M_peffect->getvariablebyname ("Lightambient")->asvector ()->setfloatvector ((float*) &m_
        Pointlight.ambient); M_peffect->getvariablebyname ("Lightdiffuse")->asvector ()->setfloatvector ((float*) &m_
        Pointlight.diffuse); M_peffect->getvariablebyname ("Lightspecular")->asvector ()->setfloatvector ((float*) &m_

        Pointlight.specular);
        M_peffect->getvariablebyname ("LightAtt0")->asscalar ()->setfloat (M_POINTLIGHT.ATTENUATION0);
       M_peffect->getvariablebyname ("LightAtt1")->asscalar ()->setfloat (M_pointlight.attenuation1); M_peffect->getvariablebyname ("LightAtt2")->asscalar ()->setfloat (M_pointlight.attenuation2);
        D3dsys->d3ddevicecontext ()->iasetinputlayout (m_pinputlayout);
        M_ptechnique->getpassbyindex (0)->apply (0, D3dsys->d3ddevicecontext ());
        D3dsys->d3ddevicecontext ()->drawindexed (36, 0, 0);
    return S_OK;
    } VOID Update (float deltatime) {m_pcube->update (deltatime);
        } HRESULT Init (Directsystem *d3dsys) {HRESULT hr;
        Id3dblob *effectblob; Hr=d3dx11compilefromfile (L "lightshader.fx", NULL, NULL, NULL, "Fx_5_0", NULL, 0, NULL, &EFFECTBLOB, NULL,
        NULL);
        if (FAILED (HR)) {return hr; } hr=d3dx11createeffectfrommemory (Effectblob->getbufferpointer (), Effectblob->getbuffersize (), 0
        , D3dsys->d3ddevice (), &m_peffect, NULL);
        if (FAILED (HR)) {return hr; } M_ptechnique = M_peffect->gettechNiquebyname ("T0");
        D3dx11_pass_desc Passdesc;
        M_ptechnique->getpassbyindex (0)->getdesc (&PASSDESC); D3d11_input_element_desc inputdesc[] = {{"POSITION", 0, dxgi_format_r32g32b32_float, 0, 0, D3d11_input_per_
            Vertex_data, 0}, {"NORMAL", 0, dxgi_format_r32g32b32_float, 0, d3d11_input_per_vertex_data, 0},
        {"Texcoord", 0,dxgi_format_r32g32_float, 0, D3d11_input_per_vertex_data, 0}};
        UINT inputdescnum = ARRAYSIZE (INPUTDESC); hr = D3dsys->d3ddevice ()->createinputlayout (Inputdesc, Inputdescnum, Passdesc.piainputsignature, PassDes
        C.iainputsignaturesize, &m_pinputlayout);
        if (FAILED (HR)) {return hr;
        } m_pcube = new Cube ();


        M_pcube->init (D3dsys);
        M_pointlight.type = 1;
        M_pointlight.position = XMFLOAT4 ( -4.0f, 4.0f, -4.0f, 1.0f);
 M_pointlight.diffuse = XMFLOAT4 (0.5f, 0.5f, 0.5f, 1.0f);       M_pointlight.ambient = XMFLOAT4 (0.3f, 0.2f, 0.1f, 1.0f);

        M_pointlight.specular = XMFLOAT4 (0.5f, 0.5f, 0.9f, 1.0f);
        M_pointlight.attenuation0 = 0;
        M_pointlight.attenuation1 = 0.1f;
        M_pointlight.attenuation2 = 0;
    M_ptechnique = M_peffect->gettechniquebyname ("T_pointlight");
        } ~scene () {delete m_pcube;
        Safe_release (m_pinputlayout);
        Safe_release (M_ptechnique);

    Safe_release (M_peffect); }
};


Copyright©by Cold River Snow
date:2017.1.1

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.