DIRECTX11 Learn notes Create two different Elf elves

Source: Internet
Author: User
Tags mixed

First configure the DIRECTX11 basic environment to create the DIRECTX11 render base class.

Then create an Elf class to generate the sprite

The header file is defined as follows:

#pragma once
#ifndef _gamespirt_h_
#define _gamespirt_h_
#include <xnamath.h>

class Gamespirt
{public
:
	gamespirt ();
	Virtual ~gamespirt ();

	Xmmatrix Getworldmatrix ();     
	
	void SetPosition (xmfloat2& position);  Position interface function
	void setrotation (float rotation);  Rotation interface function
	void Setscale (xmfloat2& scale);    Scale interface Function

private:
	XMFLOAT2 position_;  The position of the elf
	float Rotaion_;      The sprite rotates
	XMFLOAT2 scale_;     Elf's visual Range
};

#endif

The source file is defined as follows:

#include <d3d11.h>
#include <d3dx11.h>
#include "GameSpirt.h"

gamespirt::gamespirt ()
{
	rotaion_ = 0;
	scale_.x = Scale_.y = 1.0f;    The initial value is set to 1.0f
}

void Gamespirt::setposition (xmfloat2& position) {
	position_ = position;
}

void Gamespirt::setrotation (float rotation) {
	rotaion_ = rotation;
}

void Gamespirt::setscale (xmfloat2& scale) {
	scale_ = scale;
}

Xmmatrix Gamespirt::getworldmatrix () {
	Xmmatrix translation = Xmmatrixtranslation (position_.x, POSITION_.Y, 0.0f );  Generate Shift Matrix
	Xmmatrix RotationZ = Xmmatrixrotationz (rotaion_);             Generates a matrix about the z-axis
	Xmmatrix scale = xmmatrixscaling (scale_.x, Scale_.y, 1.0f);    Generate visual range

	return translation * RotationZ * scale;         Return matrix
}


gamespirt::~gamespirt ()
{
}

Then render the scene:

The header file is defined as follows:

#pragma once #ifndef _scenedemo_h_ #define _scenedemo_h_ #include "Direct3DBase.h" #include "GameSpirt.h" class Scenedemo
	:p ublic direct3dbase {public:scenedemo ();

	Virtual ~scenedemo ();
	BOOL Loadcontent ();

	void Unloadcontent ();
	void Update (float dt);
	void Render ();

	Create vertex render function HRESULT createvertices (float width, float height); Create a Sprite function Xmmatrix scenedemo::createsprite (gamespirt *spritesa_, const int spritesnums, Id3d11shaderresourceview *
	
	Colormapforspirt, XMFLOAT2 spritespos[], const int spriteposnums, Xmmatrix vpmatrix); Show Wizard function void Showspirte (Gamespirt *sprites, const int Spritesnums,xmmatrix Vpmatrix, id3d11shaderresourceview*
Colormapforspirt);  Private:/* Basic section */id3d11vertexshader* Solidcolorvs_;  Vertex shader id3d11pixelshader* Solidcolorps_;  Pixel shader id3d11inputlayout* Inputlayout_;  Vertex layout id3d11buffer* vertexbuffer_;

	Buffer */* Sprite part */const static int spritesnum = 2;    id3d11shaderresourceview* Colormapforspirta_; Set the color table for the sprite a map Id3d11shaderresourceview* colormapforspirtb_;  Set the sprite B map color table id3d11samplerstate* colormapsampler_;

	Sampling state variable id3d11blendstate* alphablendstate_;    Gamespirt Spritesa_[spritesnum];    The array used to store sprite a gamespirt spritesb_[spritesnum];      Used to store the array of sprite B id3d11buffer* mvpcb_;   
	Constant cache of vertex shaders Xmmatrix Vpmatrixforsprita_;

Xmmatrix vpmatrixforspritb_;

}; #endif


The load content function is set as follows

Set the vertex shader first

/* Vertex shader settings */
	id3dblob* vsbuffer = 0;
        Compile HLSL standard created text file
	bool Compileresult = Compiled3dshader ("texturemap.fx", "Vs_main", "Vs_4_0", &vsbuffer);
	if (Compileresult = = False) {
		dxtrace_msg ("Error loading vertex shader!!");
		return false;
	}
       
	HRESULT D3dresult;      Create a vertex shader and load device
	D3dresult = D3ddevice_->createvertexshader (Vsbuffer->getbufferpointer (), vsbuffer-> GetBufferSize (), 0, &solidcolorvs_);
	if (FAILED (D3dresult)) {  
		dxtrace_msg ("CreateVertexShader failed!!");
		if (vsbuffer)
			vsbuffer->release ();
		return false;
	}

The FX file is a text file created using the high Order Shader language (HLSL) standard. Enter the appearance settings:
	The input appearance settings
	//inputlayout used to shader the data in the data portion of the final entry into shader  
	d3d11_input_element_desc solidcolorlayout[] = {   
		{"POSITION", 0, dxgi_format_r32g32b32_float, 0, 0, d3d11_input_per_vertex_data, 0},
		{"Texcoord", 0, dxgi_format_r32g32_float, 0, d3d11_input_per_vertex_data, 0}    Coordinates of the map
	};

	unsigned int totalcolorlayouts = ARRAYSIZE (solidcolorlayout);

	Create an input skin and import
	the device D3dresult = D3ddevice_->createinputlayout (Solidcolorlayout, Totalcolorlayouts, vsbuffer-> GetBufferPointer (), Vsbuffer->getbuffersize (), &inputlayout_);

	Vsbuffer->release ();
	if (FAILED (D3dresult)) {
		dxtrace_msg ("Createlayout failed!!");
		return false;
	}


Pixel Shader Settings:
/* Pixel shader Settings */
	id3dblob* psbuffer = 0;
	Compiling HLSL standard files
	compileresult = Compiled3dshader ("texturemap.fx", "Ps_main", "Ps_4_0", &psbuffer);
	if (Compileresult = = False) {
		dxtrace_msg ("Error loading pixel shader!");
		return false;
	}

Create a pixel shader and import the device:
D3dresult = D3ddevice_->createpixelshader (Psbuffer->getbufferpointer (), Psbuffer->getbuffersize (), 0, &SOLIDCOLORPS_);

	Psbuffer->release ();
	if (FAILED (D3dresult)) {
		return false;
	}

D3dresult = D3ddevice_->createpixelshader (Psbuffer->getbufferpointer (), Psbuffer->getbuffersize (), 0, &SOLIDCOLORPS_);

	Psbuffer->release ();
	if (FAILED (D3dresult)) {
		return false;
	}


Create a resource View file and import a resource map
HRESULT d3dresult1, D3DRESULT2;
	D3DRESULT1 = D3dx11createshaderresourceviewfromfile (D3ddevice_, "Rbar.dds", 0, 0, &colormapforspirtb_, 0);  Import map from text create Sprite a
	d3dresult2 = D3dx11createshaderresourceviewfromfile (D3ddevice_, "Decal.dds", 0, 0, & Colormapforspirta_, 0); Import a map from text create Sprite B
	if (FAILED (D3DRESULT1) | | FAILED (D3DRESULT2)) {
		dxtrace_msg ("FAILED to Create shader from File");
		return false;
	}

Sampling settings:
D3d11_sampler_desc Colormapdesc;
	ZeroMemory (&colormapdesc, sizeof (COLORMAPDESC));  Empty memory
	
	colormapdesc.addressu = d3d11_texture_address_wrap;
	COLORMAPDESC.ADDRESSV = D3d11_texture_address_wrap;
	COLORMAPDESC.ADDRESSW = D3d11_texture_address_wrap;
	Colormapdesc.comparisonfunc = D3d11_comparison_never;
	Colormapdesc.filter = d3d11_filter_min_mag_point_mip_linear;
	Colormapdesc.maxlod = D3d11_float32_max;
	
	D3dresult = D3ddevice_->createsamplerstate (&colormapdesc, &colormapsampler_);

	if (FAILED (D3dresult)) {
		dxtrace_msg ("ERROR Loading Sampler State"); 
		return false;
	}


And then it's the wizard. When creating a sprite, we define the creation of vertex functions and the creation of sprite functions.

To create a vertex function:

HRESULT Scenedemo::createvertices (float width, float height) {HRESULT d3dresult; Vertexpos vertices[] =//coordinates of each vertex {{XMFLOAT3 (width, height, 1.0f), XMFLOAT2 (1.0f, 0.0f)}, {XMFLOAT3 (width,-h Eight, 1.0f), XMFLOAT2 (1.0f, 1.0f)}, {XMFLOAT3 (-width,-height, 1.0f), XMFLOAT2 (0.0f, 1.0f)}, {XMFLOAT3 (-width,-H Eight, 1.0f), XMFLOAT2 (0.0f, 1.0f)}, {XMFLOAT3 (-width, height, 1.0f), XMFLOAT2 (0.0f, 0.0f)}, {XMFLOAT3 (width, he
	
	ight, 1.0f), XMFLOAT2 (1.0f, 0.0f)},};  D3d11_buffer_desc Vertexdesc;
	Create a vertex description ZeroMemory (&vertexdesc, sizeof (VERTEXDESC));
	Vertexdesc.usage = D3d11_usage_default;
	Vertexdesc.bindflags = D3d11_bind_vertex_buffer;  Vertexdesc.bytewidth = sizeof (vertexpos) * 6;  Several vertices are multiplied by several vertices d3d11_subresource_data resourcedata;
	Resource Data ZeroMemory (&resourcedata, sizeof (Resourcedata));

	Resourcedata.psysmem = vertices;
	
	D3dresult = D3ddevice_->createbuffer (&vertexdesc, &resourcedata, &vertexbuffer_);
return d3dresult; }


Sprite creation Function:

Xmmatrix scenedemo::createsprite (gamespirt *sprites,const int Spritesnums,id3d11shaderresourceview *   
	COLORMAPFORSPIRT,XMFLOAT2 spritespos[], const int spriteposnums, Xmmatrix vpmatrix) {//set map id3d11resource* ColorTex;
	HRESULT D3dresult; Colormapforspirt->getresource (&colortex);
	Assign the decal to Colortext D3d11_texture2d_desc Colortexdesc; 
	((id3d11texture2d*) Colortex)->getdesc (&AMP;COLORTEXDESC);   Colortex->release ();
	Free content Float Halfwidth = (float) colortexdesc.width/2.0f;

	float halfheight = (float) colortexdesc.height/2.0f;   
	Create vertex cache D3dresult = createvertices (Halfwidth, halfheight);
		if (FAILED (D3dresult)) {dxtrace_msg ("FAILED to create vertex buffer!");
	return false;
	}//Cache description D3d11_buffer_desc Constdesc;
	ZeroMemory (&constdesc, sizeof (CONSTDESC));
	Constdesc.bindflags = D3d11_bind_constant_buffer;
	Constdesc.bytewidth = sizeof (Xmmatrix);


	Constdesc.usage = D3d11_usage_default; D3dresult = D3ddevice_->createbuffer (&constdesc, 0,&AMP;MVPCB_);
	Create Cache if (FAILED (D3dresult)) {return false;  }/* Elf position operation */for (int i = 0; i < spriteposnums; ++i) {sprites->setposition (spritespos[i]);
	Set the location of each sprite sprites = sprites + 1;
	} Xmmatrix view = Xmmatrixidentity ();

	Xmmatrix projection = XMMATRIXORTHOGRAPHICOFFCENTERLH (0.0f, 800.0f, 0.0f, 600.0f, 0.1f, 100.0f);

	Vpmatrix = xmmatrixmultiply (view, projection);
return Vpmatrix; }



You can then create the wizard:

	/* Create Sprite section */
	XMFLOAT2 pos[2] = {{300.0f, 200.0f}, {200.0f, 100.0f}};    Set the coordinates of the two sprites
	vpmatrixforspritb_ = Createsprite (Spritesa_,arraysize (Spritesa_), Colormapforspirtb_, POS, ARRAYSIZE (POS), vpmatrixforspritb_);   Create Sprite Success  
	XMFLOAT2 Pos2[2] = {{200.0f, 300.0f}, {400.0f, 400.0f}};  The coordinates of the two elves of evil
	Vpmatrixforsprita_ = Createsprite (spritesb_, ARRAYSIZE (spritesb_), Colormapforspirta_, Pos2, ARRAYSIZE (POS2), vpmatrixforsprita_);  Create Wizard Success

Mixed Description section:

	/* Mixed Description */
	d3d11_blend_desc Blenddesc;
	ZeroMemory (&blenddesc, sizeof (BLENDDESC));
	Blenddesc.rendertarget[0]. blendenable = TRUE;
	Blenddesc.rendertarget[0]. Blendop = D3d11_blend_op_add;
	Blenddesc.rendertarget[0]. Srcblend = D3d11_blend_src_alpha;
	Blenddesc.rendertarget[0]. Destblend = D3d11_blend_one;
	Blenddesc.rendertarget[0]. Blendopalpha = D3d11_blend_op_add;
	Blenddesc.rendertarget[0]. Srcblendalpha = D3d11_blend_zero;
	Blenddesc.rendertarget[0]. Destblendalpha = D3d11_blend_zero;
	Blenddesc.rendertarget[0]. Rendertargetwritemask = 0x0F;

	Float Blendfactor[4] = {0.0f, 0.0f, 0.0f, 0.0f};

	D3ddevice_->createblendstate (&blenddesc, &alphablendstate_);  Alphablendstate Set Transparency
	d3dcontext_->omsetblendstate (Alphablendstate_, Blendfactor, 0xFFFFFFFF);

	return true;


The sprite is then displayed on the form by a render function

Show Wizard functions:

void Scenedemo::showspirte (Gamespirt *sprites, const int spritesnums, Xmmatrix Vpmatrix, id3d11shaderresourceview* Colormapforspirt) {

	d3dcontext_->pssetshaderresources (0, 1, &colormapforspirt);  Pixel shader setting Sprite's decal
	d3dcontext_->pssetsamplers (0, 1, &colormapsampler_);   Sample shader for

	(int i = 0; i < spritesnums; ++i)
	{
		Xmmatrix world = sprites[i]. Getworldmatrix ();  
		Xmmatrix MVP = xmmatrixmultiply (World, Vpmatrix);
		MVP = Xmmatrixtranspose (MVP);   

		D3dcontext_->updatesubresource (mvpcb_, 0, 0, &MVP, 0, 0);
		D3dcontext_->vssetconstantbuffers (0, 1, &mvpcb_);

		D3dcontext_->draw (6, 0);
	}
}


Final Render function

/* For rendering *
/void Scenedemo::render () {/
	* Empty cache */
	float clearcolor[4] = {0.0f, 0.0f, 0.25f, 1.0f};
	D3dcontext_->clearrendertargetview (backbuffertarget_, clearcolor);

	if (D3dcontext_ = = 0)
		return;

	unsigned int stride = sizeof (VERTEXPOS);
	unsigned int offset = 0;

	D3dcontext_->iasetinputlayout (inputlayout_);
	D3dcontext_->iasetvertexbuffers (0, 1, &vertexbuffer_, &stride, &offset);
	D3dcontext_->iasetprimitivetopology (d3d11_primitive_topology_trianglelist);

	D3dcontext_->vssetshader (solidcolorvs_, 0, 0);
	D3dcontext_->pssetshader (solidcolorps_, 0, 0);

	Show Sprite
	showspirte (Spritesa_, ARRAYSIZE (Spritesa_), Vpmatrixforsprita_, colormapforspirta_);
	Showspirte (Spritesb_, ARRAYSIZE (spritesb_), vpmatrixforspritb_, colormapforspirtb_);
	Swapchain_->present (0, 0);








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.