directx 9 draw 3d line
DirectX9: Texture Basics
I. Introduction
Texture mapping is a technique for imparting the image data is triangular
In Direct3D texture throughIDirect3DTexture9 Represents interface, a textured surface is a matrix of pixels is mapped on the triangle
II. Texture coordinates
Direct3D texture coordinate using a arrangement, which is the horizontal direction ofuAxis (to the right as positive) and the vertical managementv Axis (positive downward) configuration
// texture coordinate system representation
struct Vertex
{
float _x, _y, _z;
float _nx, _ny, _nz;
float _u, _v;
static const DWORD FVF;
};const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
III. Creating Textures
1.D3DXCreateTextureFromFile()
Texture information can exist read from the prototype file stored on disk, insertIDirect3DTexture9 Abstruse Textures type
Supported image formats: BMP DDS DIB JPG PNG TGA
HRESULT D3DXCreateTextureFromFile(
LPDIRECT3DDEVICE9 pDevice,
LPCSTR pSrcFile,
LPDIRECT3DTEXTURE9* ppTexture
);
HRESULT IDirect3DDevice9::SetTexture(
DWORD Phase,
IDirect3DBaseTexture9* pTexture
);
// create a texture IDirect3Dtexture9* _stonewall; D3DXCreateTextureFromFile(_device, "stonewall.bmp", &_stonewall); // ready the texture Device->SetTexture(0, _stonewall); IV. Filter
Textures are mapped onto the screen triangle, and the triangle is not possible as big as texture, when the need arises to deformation texture, it is necessary withfilter(Filtering) to make a smooth deformation technology
Direct3D provides three different filters, each level of quality and speed vary
1.Nearest point sampling
The default filtering method, the worst quality, fastest
Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); ii.Linear filtering
Recommended, good filtering consequence generated
Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); three.Anisotropic filtering
Filtration yielded the best quality, the longest processing fourth dimension
Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC); Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC); 4.Mipmaps filter
If the video card supports Mipmaps, Direct3D volition automatically select the best match of the triangular Mipmap
Device-> SetSamplerState (0, D3DSAMP_MIPFILTER, D3DTEXF_NONE); // do not use mipmap
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); V. Addressing Modes
Between the texture coordinates must be specified in [0, 1]
Direct3D There are four addressing modes: Surround texture addressing manner edge color texture addressing mode taken texture image texture addressing way addressing fashion
1. environment texture addressing way (wrap address style)
if (::GetAsyncKeyState('W') & 0x8000f) { Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); } 2. Edge Colour texture addressing mode (border color accost mode)
if (::GetAsyncKeyState('B') & 0x8000f) { Device>SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER); Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER); Device->SetSamplerState(0, D3DSAMP_BORDERCOLOR, 0x000000ff); } iii. Intercept texture addressing mode (clamp address mode)
if (::GetAsyncKeyState('C') & 0x8000f) { Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); } 4. Mirror texture addressing manner (mirror address way)
if (::GetAsyncKeyState('Thou') & 0x8000f) { Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR); Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_MIRROR); } VI. Examples
i. Do non filter textures
#include "d3dUtility.h" IDirect3DDevice9* Device = 0; const int Width = 640; const int Elevation = 480; IDirect3DVertexBuffer9* Quad = 0; IDirect3DTexture9* Tex = 0; struct Vertex { Vertex(){} float _x, _y, _z; bladder _nx, _ny, _nz; float _u, _v; Vertex(bladder 10, float y, float z, bladder nx, float ny, bladder nz, float u, float v) { _x = x; _y = y; _z = z; _nx = nx; _ny = ny; _nz = nz; _u = u; _v = 5; } static const DWORD FVF; }; const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1; bool Setup() { Device->CreateVertexBuffer( vi * sizeof(Vertex), D3DUSAGE_WRITEONLY, Vertex::FVF, D3DPOOL_MANAGED, &Quad, 0); Vertex* v; Quad->Lock(0, 0, (void**)&v, 0); five[0] = Vertex(-1.0f, -i.0f, 1.25f, 0.0f, 0.0f, -ane.0f, 0.0f, 1.0f); five[1] = Vertex(-1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -one.0f, 0.0f, 0.0f); five[two] = Vertex(i.0f, one.0f, i.25f, 0.0f, 0.0f, -1.0f, i.0f, 0.0f); 5[iii] = Vertex(-1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -i.0f, 0.0f, 1.0f); 5[iv] = Vertex(1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -ane.0f, 1.0f, 0.0f); v[v] = Vertex(1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, i.0f, one.0f); Quad->Unlock(); // Load the texture file D3DXCreateTextureFromFile(Device, "a.bmp", &Tex); Device->SetTexture(0, Tex); Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT); Device->SetRenderState(D3DRS_LIGHTING, fake); D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.5f, (float)Width / (float)Height, 1.0f, g.0f); Device->SetTransform(D3DTS_PROJECTION, &proj); render truthful; } void Cleanup() { d3d::Release<IDirect3DVertexBuffer9*>(Quad); d3d::Release<IDirect3DTexture9*>(Tex); } bool Display(float timeDelta) { if (Device) { Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0); Device->BeginScene(); Device->SetStreamSource(0, Quad, 0, sizeof(Vertex)); Device->SetFVF(Vertex::FVF); Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2); Device->EndScene(); Device->Present(0, 0, 0, 0); } render true; } 2. texture filtering
#include "d3dUtility.h" // // Globals // IDirect3DDevice9* Device = 0; const int Width = 640; const int Height = 480; IDirect3DVertexBuffer9* Quad = 0; IDirect3DTexture9* Tex = 0; // vertex format struct Vertex { Vertex(){} Vertex( float ten, float y, float z, float nx, float ny, float nz, float u, float v) { _x = x; _y = y; _z = z; _nx = nx; _ny = ny; _nz = nz; _u = u; _v = v; } bladder _x, _y, _z; float _nx, _ny, _nz; bladder _u, _v; static const DWORD FVF; }; const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1; // // Framework Functions // bool Setup() { // Create the vertex buffer Device->CreateVertexBuffer( six * sizeof(Vertex), D3DUSAGE_WRITEONLY, Vertex::FVF, D3DPOOL_MANAGED, &Quad, 0); Vertex* v; Quad->Lock(0, 0, (void**)&5, 0); v[0] = Vertex(-1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -one.0f, 0.0f, 3.0f); v[one] = Vertex(-ane.0f, 1.0f, 1.25f, 0.0f, 0.0f, -one.0f, 0.0f, 0.0f); v[ii] = Vertex( 1.0f, one.0f, i.25f, 0.0f, 0.0f, -1.0f, three.0f, 0.0f); five[three] = Vertex(-i.0f, -1.0f, ane.25f, 0.0f, 0.0f, -1.0f, 0.0f, 3.0f); v[4] = Vertex( ane.0f, ane.0f, one.25f, 0.0f, 0.0f, -one.0f, 3.0f, 0.0f); v[5] = Vertex( 1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -ane.0f, 3.0f, iii.0f); Quad->Unlock(); // // Create the texture and set texture // D3DXCreateTextureFromFile( Device, "a.bmp", &Tex); Device->SetTexture(0, Tex); Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT); // // Don't use lighting for this sample // Device->SetRenderState(D3DRS_LIGHTING, fake); // // Prepare the projection matrix // D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.5f, (float)Width / (float)Height, 1.0f, g.0f); Device->SetTransform(D3DTS_PROJECTION, &proj); render true; } void Cleanup() { d3d::Release<IDirect3DVertexBuffer9*>(Quad); d3d::Release<IDirect3DTexture9*>(Tex); } bool Display(bladder timeDelta) { if (Device) { // // Update the scene // // set wrap address manner if (::GetAsyncKeyState('W') & 0x8000f) { Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); } // set border color address way if (::GetAsyncKeyState('B') & 0x8000f) { Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER); Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER); Device->SetSamplerState(0, D3DSAMP_BORDERCOLOR, 0x000000ff); } // set clamp address mode if (::GetAsyncKeyState('C') & 0x8000f) { Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); } // ready mirror accost mode if (::GetAsyncKeyState('K') & 0x8000f) { Device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR); Device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_MIRROR); } // // Describe the scene // Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0); Device->BeginScene(); Device->SetStreamSource(0, Quad, 0, sizeof(Vertex)); Device->SetFVF(Vertex::FVF); Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, ii); Device->EndScene(); Device->Present(0, 0, 0, 0); } return true; } Intelligent Recommendation
Shader learning basics (fifteen) program texture
Plan texture refers to images that are generated by calculations, and some specific algorithms are ordinarily used to create personalized patterns. In fact, the paradigm is calculated and and then assigned t...
Covering and alternative with DirectX9
I saw an article about apoplexy culling, which was well written.http://bbs.gameres.com/showthread.asp?threadid=11409 Object alternative is an important attribute of graphical programming. Rendering objects ...
Win32+DirectX9 game
A pocket-size game of drawing, originally a work problem of someone, the title requirement is to make a small system that tin draw a circle and write elementary words. I have get like this after all one thousand...
More Recommendation
Directx9 play video
brandish issue Source address: https://github.com/KaiWenK/d3d9player.git...
Use of D3DLOCKED_RECT in DirectX9
1. Definition In d3d9types.h, D3DLOCKED_RECT is defined as follows: Amid them, pBits is a pointer to the rectangle,Pitch is in bytes, which represents the length of each row of the rectangle. Note: P...
Directx9 learning (5) input
2017.08.25 Today, learn about DX to get input from the keyboard and mouse Here you demand to add together header files and libraries The kickoff is keyboard input (scan the keyboard to see if there is input) Two k...
DirectX9 learning (two)
2017.08.23 Learned how to create a D3D variable, how to apply this variable to create a device, configure this variable and draw a blue background D3D9 should be directly usable (VS2015) direct add he...
DirectX9 learning (i)
2017.8.22 Write to yourself, start to arrive touch with DirectX, this is a backup for your ain learning. I started to make it touch with WinMain and WinProc, most of which are based on the book, but al...
Copyright DMCA © 2018-2022 - All Rights Reserved - www.programmersought.com User Discover
Source: https://programmersought.com/article/82872712180/
0 Response to "directx 9 draw 3d line"
Post a Comment