Directx10 longshu notes-a color cube Program

Source: Internet
Author: User
Tags pixel coloring

This document is used as a learning note. The detailed code is displayed on the detail page of Introduction to 3D game programming with directx10. You can download it from www.d3dcoder.net.

 

The effect of this program is:

 

This demo shows the basic DirectX rendering process.

 

First, the rendering pipeline: it is used to generate 2D images from the virtual camera's perspective.

 

The main category is coloredcubeapp.

Class coloredcubeapp: Public d3dapp {public: coloredcubeapp (hinstance );~ Coloredcubeapp (); void initapp (); // initialize void onresize (); // call void updatescene (float DT) when the window size changes; // call each frame, update the parameter void drawscene (); // call each frame and draw PRIVATE: void buildfx (); // The Void buildvertexlayouts () of the colorator. // input the assembler, construct metadata PRIVATE: Box mbox; id3d10effect * mfx; Comment * mtech; Comment * mvertexlayout; Comment * mfxwvpvar; d3dxmatrix mview; d3dxmatrix mproj; d3dxmatrix mwvp; float mtheta; float mphi ;};


Initapp () in this class prepares for initialization Rendering

Void coloredcubeapp: initapp () {d3dapp: initapp (); mbox. INIT (md3ddevice, 0.3f); // This is the initialization of the cube to be created. Provides the vertex buffer and the index buffer buildfx (); // This is the configuration buildvertexlayouts () of the vertex coloring machine, geometric coloring machine, and pixel coloring machine (); // construct the Element Based on the above information}


Next let's take a look at the specific mbox. how does init initialize the void box of the cube: Init (id3d10device * device, float scale) {md3ddevice = device; mnumvertices = 8; mnumfaces = 12; // 2 per quad // create vertex buffer vertex vertices [] = {d3dxvector3 (-1.0f,-1.0f,-1.0f), white}, {d3dxvector3 (-1.0f, + 1.0f,-1.0f), black}, {d3dxvector3 (+ 1.0f, + 1.0f,-1.0f), red}, {d3dxvector3 (+ 1.0f,-1.0f,-1.0f ), green}, {d3dxvector3 (-1.0f,-1.0f, + 1.0f), blue}, {d3dxvector3 (-1.0f, + 1.0f, + 1.0f), yellow }, {d3dxvector3 (+ 1.0f, + 1.0f, + 1.0f), cyan}, {d3dxvector3 (+ 1.0f,-1.0f, + 1.0f), Magenta},}; // scale the box. for (DWORD I = 0; I <mnumvertices; ++ I) vertices [I]. pos * = scale; // vertex buffer d3d10_buffer_desc vbd; vbd. usage = d3d10_usage_immutable; vbd. bytewidth = sizeof (vertex) * mnumvertices; vbd. bindflags = d3d10_bind_vertex_buffer; vbd. cpuaccessflags = 0; vbd. miscflags = 0; d3d10_subresource_data vinitdata; vinitdata. psysmem = vertices; HR (md3ddevice-> createbuffer (& vbd, & vinitdata, & MVB )); // create the index buffer // index buffer DWORD indices [] = {// front face0, 1, 2, 0, 2, 3, // back face4, 6, 5, 4, 7, 6, // left face4, 5, 1, 0, // right face3, 2, 6, 3, 6, 7, // top face1, 5, 6, 6, 2, // bottom face4, 0, 3, 4, 3, 7}; d3d10_buffer_desc IBD; IBD. usage = d3d10_usage_immutable; IBD. bytewidth = sizeof (DWORD) * mnumfaces * 3; IBD. bindflags = d3d10_bind_index_buffer; IBD. cpuaccessflags = 0; IBD. miscflags = 0; d3d10_subresource_data iinitdata; iinitdata. psysmem = indices; HR (md3ddevice-> createbuffer (& IBD, & iinitdata, & MIB ));}

We can see from the above that the main function is to first build the required non-repeated points, and then build a cube index point (the index is to save resources, do not have to repeat the build point ).

 

The following describes the content in buildfx ().

HR = d3dx10createeffectfromfile (L "color. FX", 0, 0,

"Fx_4_0", shaderflags, 0, md3ddevice, 0, 0, & mfx, & compilationerrors, 0 );

This is the main sentence.

Color. FX is a text file.

//=============================================================================// color.fx by Frank Luna (C) 2008 All Rights Reserved.//// Transforms and colors geometry.//=============================================================================cbuffer cbPerObject{float4x4 gWVP; };void VS(float3 iPosL  : POSITION,        float4 iColor : COLOR,        out float4 oPosH  : SV_POSITION,        out float4 oColor : COLOR){// Transform to homogeneous clip space.oPosH = mul(float4(iPosL, 1.0f), gWVP);// Just pass vertex color into the pixel shader.    oColor = iColor;}float4 PS(float4 posH  : SV_POSITION,          float4 color : COLOR) : SV_Target{    return color;}technique10 ColorTech{    pass P0    {        SetVertexShader( CompileShader( vs_4_0, VS() ) );        SetGeometryShader( NULL );        SetPixelShader( CompileShader( ps_4_0, PS() ) );    }}


The above can be seen as a C ++-like file, that is, defining a group of operations. Then, use void coloredcubeapp: buildvertexlayouts () {// create the vertex input layout. d3d10_input_element_desc vertexdesc [] = {"position", 0, average, 0, 0, d3d10_input_per_vertex_data, 0 },{ "color", 0, minimum, 0, 12, minimum, 0 }}; // create the input layout d3d10_pass_desc passdesc; mtech-> getpassbyindex (0)-> getdesc (& passdesc); HR (md3ddevice-> createinputlayout (vertexdesc, 2, passdesc. piainputsignature, passdesc. iainputsignaturesize, & mvertexlayout ));}

The above function calls the corresponding operation functions in color. FX to construct the elements.

The preparation is complete. Now is the drawing process.

 

Void coloredcubeapp: drawscene () {d3dapp: drawscene (); // Restore Default states, input layout and primitive topology // because mfont-> drawtext changes them. note that we can // restore the default states by passing null. // md3ddevice-> memory (0, 0); float blendfactors [] = {0.0f, 0.0f, 0.0f, 0.0f}; // md3ddevice-> omsetblendstate (0, blendfactors, 0 xffffffff); md3ddevice-> iasetinputlayout (M Vertexlayout); // figure scheme? In the meta-a column, the D table is named? Shape? Md3ddevice-> iasetprimitivetopology (topology); // set constantsmwvp = mview * mproj; mfxwvpvar-> setmatrix (float *) & mwvp); Notify techdesc; mtech-> getdesc (& techdesc); For (uint p = 0; P <techdesc. passes; ++ p) {mtech-> getpassbyindex (P)-> apply (0); // updated? How many GPUs are frequently used? Slow o rush? Zone ?, Why? Yes? Dá? Colors? ¡Â bind a token®Set limit § to limit? What are the pipe wires ?, Why? Qi? Dynamic transfer ¥ pass mbox. draw ();} mswapchain-> present (0, 0);} void coloredcubeapp: updatescene (float DT) {d3dapp: updatescene (DT ); // update Angles Based on Input to orbit camera around box. if (getasynckeystate ('A') & 0x8000) mtheta-= 2.0f * DT; If (getasynckeystate ('D') & 0x8000) mtheta + = 2.0f * DT; if (getasynckeystate ('W') & 0x8000) mphi-= 2.0f * DT; If (getasynckeystate ('s') & 0x8000) mphi + = 2.0f * DT; // restrict the angle Mphi. if (mphi <0.1f) mphi = 0.1f; If (mphi> PI-0.1f) mphi = PI-0.1f; // convert spherical to Cartesian coordinates: mphi measured from + y // and mtheta measured counterclockwise from-Z. float x = 5.0f * sinf (mphi) * sinf (mtheta); float Z =-5.0f * sinf (mphi) * cosf (mtheta ); float y = 5.0f * cosf (mphi); // build the view matrix.? Who did? To snapshot? World °?? Moment? Tsung®D3dxvector3 pos (x, y, z); d3dxvector3 target (0.0f, 0.0f, 0.0f); d3dxvector3 up (0.0f, 1.0f, 0.0f); d3dxmatrixlookatlh (& mview, & Pos, & target, & UP );}

Currently, this is only the first complete DEMO code that has not been fully understood. We can understand the general process. I will learn more and understand later


Directx10 longshu notes-a color cube Program

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.