怎样用directx 画线在地图上画房子

1917人阅读
1. Beginning DirectX 11学习笔记(25)
DirectX11 绘制字体
1. DirectX中如何实现绘制字体?
由于Direct3D11中微软移除了ID3DXFont这个在Direct3D 9中非常好用的字体接口,这样导致了目前的Direct3D11中竟然没有一个官方的字体解决方案。所以如果要绘制字体,必须要采取手动绘制文本的方式。
我们这里介绍一种图形贴图的方式来绘制字体,即将游戏中要绘制的字体做成一副图片(见下图),然后在游戏里面切开一个个字符图片,按字符串所需字符来绘制。
2. 如何将一幅字体纹理切开每一个字符图片?
其实我们并没有真正的切开一幅字体纹理,而是通过修改UV来选中不同的字符。实际上,我们只需要增加一些代码来将之前 Demo 中的顶点缓存修改为动态缓存,和增加一个函数来将我们的贴图精灵填充到缓存中即可。 动态缓存对于我们需要修改一块缓存中的内容的这种情况来说是很合适的。 不推荐多次创建和销毁静态缓存块,特别是逐帧这样做,你应该使用动态缓存来做这样的任务(上一节已经介绍过如何创建动态缓存)。
也就是说我们就修改动态顶点缓存中的顶点位置(字符的位置)和UV(字符的纹理)就可以了。
3. 如何修改动态顶点缓存的数据?
想修改动态缓存,首先要调用D3D设备的Map函数,来获得子资源的指针(ID3D11Buffer继承自 ID3D11Resource,所以缓存也是一种资源)。
Map函数的原型如下:
HRESULT Map(
ID3D11Resource
*pResource,
Subresource,
[out, optional] D3D11_MAPPED_SUBRESOURCE *pMappedResource
第一个参数pResource,是映射的源头。
第二个参数Subresource,是子资源的索引(设置为 0,因为我们没有多个子资源)。
第三个参数D3D11_MAP,在本 Demo 中映射类型是 D3D11_MAP_WRITE_DISCARD,它指示 Direct3D 将缓存中的之前的值作为未定义考虑。
第四个参数MapFlags,映射标识。对于其它的映射类型,映射标识可以是 D3D11_MAP_FLAG_DO_NOT_WAIT。但是当使用映射类型D3D11_MAP_WIRTE_DISCARD,则映射标识必须是 0,因为D3D11_MAP_FLAG_DO_NOT_WAIT标识不能用于此种映射类型。
第五个参数pMappedResource,用一个D3D11_MAPPED_SUBRESOURCE结构体类型的指针来保存该映射子资源。(我们通过它来修改动态缓存。为了更新缓存,我们只需要简单的拷贝任何数据给 D3D11_MAPPED_SUBRESOURCE 结构的 pData 成员。 )
4. 绘制字体示例代码
(完整代码太多就不贴了,请自行查看该书附带源码)
bool D3DTextDemo::DrawString( char* message, float startX, float startY )
const int sizeOfSprite = sizeof( VertexPos ) * 6;
const int maxLetters = 24;
int length = strlen( message );
if( length & maxLetters )
length = maxL
float charWidth = 32.0f / 800.0f;
float charHeight = 32.0f / 640.0f;
float texelWidth = 32.0f / 864.0f;
const int verticesPerLetter = 6;
D3D11_MAPPED_SUBRESOURCE mapR
HRESULT d3dResult = d3dContext_-&Map( vertexBuffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapResource );
if( FAILED( d3dResult ) )
DXTRACE_MSG( "Failed to map resource!" );
return false;
VertexPos *spritePtr = ( VertexPos* )mapResource.pD
const int indexA = static_cast&char&( 'A' );
const int indexZ = static_cast&char&( 'Z' );
for( int i = 0; i & ++i )
float thisStartX = startX + ( charWidth * static_cast&float&( i ) );
float thisEndX = thisStartX + charW
float thisEndY = startY + charH
spritePtr[0].pos = XMFLOAT3( thisEndX,
thisEndY, 1.0f );
spritePtr[1].pos = XMFLOAT3( thisEndX,
spritePtr[2].pos = XMFLOAT3( thisStartX, startY,
spritePtr[3].pos = XMFLOAT3( thisStartX, startY,
spritePtr[4].pos = XMFLOAT3( thisStartX, thisEndY, 1.0f );
spritePtr[5].pos = XMFLOAT3( thisEndX,
thisEndY, 1.0f );
int texLookup = 0;
int letter = static_cast&char&( message[i] );
if( letter & indexA || letter & indexZ )
texLookup = ( indexZ - indexA ) + 1;
texLookup = ( letter - indexA );
float tuStart = 0.0f + ( texelWidth * static_cast&float&( texLookup ) );
float tuEnd = tuStart + texelW
spritePtr[0].tex0 = XMFLOAT2( tuEnd, 0.0f );
spritePtr[1].tex0 = XMFLOAT2( tuEnd, 1.0f );
spritePtr[2].tex0 = XMFLOAT2( tuStart, 1.0f );
spritePtr[3].tex0 = XMFLOAT2( tuStart, 1.0f );
spritePtr[4].tex0 = XMFLOAT2( tuStart, 0.0f );
spritePtr[5].tex0 = XMFLOAT2( tuEnd, 0.0f );
spritePtr += 6;
d3dContext_-&Unmap( vertexBuffer_, 0 );
d3dContext_-&Draw( 6 * length, 0 );
return true;
文章:90篇
阅读:109408This page contains DirectX 9.0 Software Development Kit Update (Summer 2003).
Move to desired DirectX SDK part:
- for backward compatibility
Info field below contains Name / Date / Size with links to file and
description field contains some notes about sample and screenshot from run-time.
Description
(WinRAR SFX archive)
28-Jul-2004
Delphi headers adaptation followed by Borland and JEDI standarts for all (up to 9.0) versions of
DirectDraw, Direct3D, DirectInput, DirectSound, DirectM DirectPlay8 (updated to DirectX9);
D3DX 9.0 headers
dxerr9 (DirectX 9.x error reporting) headers with support library.
In this file you'll find JEDI compliant version and preprocessed
versions of headers for Delphi 4/5 and Delphi 6/7. Also this archive contains simple example of
Direct3D8 program.
This package DOES NOT include any helper libraries like D3DX (D3DX9Sab.DLL) or DXErr -
error reporting (DXErr9ab.DLL).
28-Jul-2004
Subset of previous translation. Includes only PAS files compatible with JEDI standarts from
25-Dec-2003
DLLs needed for both Delphi and C++ Builder, to use D3DX 9.0 functionality and/or DXErr (DirectX error
reporting) in your projects or compile Microsoft demos: D3DX9Sab.dll, DXErr9ab.DLL.
you can find DirectX libraries required for
compiling Direct3D 9.0 applications with C++&Builder.
Notes: All examples can be compiled either in Delphi or C++&Builder.
Please read "" page containing instructions how to compile examples in either Delphi or
C++Builder with additional details.
Description
30-Dec-2003
6 "step by step" DirectX tutorials. Great tutorials to start learning
Direct3D. From initializing D3D device renderer to loading meshes.
These tutorial do not need any additional files to compile and run. Only headers and D3DX helper dll for some of tutorials.
26-Jan-2003
Free Pascal compatible
Translated Sample DirectX Framework - used by all Direct3D/DirectSound examples.
Includes: D3DApp, D3DEnumeration, D3DFile, D3DFont, D3DRes, D3DSettings, D3DUtil, DSUtil, DXUtil files.
There are Direct3D files (D3DSaver) what still need Delphi conversion...
Also DDUtil unit (DirectDraw utilities) is included - initially converted by Dominique Louis.
NOTE: All Direct3D samples require )
to compile!
28-Jan-2004
Free Pascal compatible
The BasicHLSL sampleshows a simple example of the High-Level Shader Language (HLSL) using an effect
interface smoke, explosions, and more. This sample simply loads a mesh, creates an effect from a file, and then uses
the effect to render the mesh. The effect that is used is a simple vertex shader that animates the vertices based
30-Dec-2003
The Billboard sample illustrates the billboarding technique. Rather than
rendering complex 3D models (such as a high-polygon tree model),
billboarding renders a 2D image of the model and rotates it to always face
the eyepoint. This technique is commonly used to render trees, clouds,
smoke, explosions, and more.
09-Feb-2004
Free Pascal compatible
The Blobs sample mimics a metaball effect in screen space using a pixel shader. True metaball techniques deform
surfaces according to pushing or pulling modifiers, and are commonly used to model liquid effects like the merging of
however, metaball effects can be computationally expensive, and this sample shows how to fake a 3D
metaball effect in 2D image space using a pixel shader.
More extensive documentation can be found in Readme.html in sample archive.
28-Jul-2004
Free Pascal compatible
Bumpmapping folder contains 4 demos (and lacks 3 others). These demos demostrate various
techniques of applying bump-mapping on objects: EMBP, emboss, Dot3.
The BumpEarth sample demonstrates the bump mapping capabilities of Direct3D.
Bumpmapping is a texture blending technique used to render the appearance of
rough, bumpy surfaces. This sample renders a rotating, bumpmapped planet Earth.
The BumpLens sample demonstrates a lens effect that can be acheived using
bumpmapping. Bumpmapping is a texture blending technique used to render the
appearance of rough, bumpy surfaces, but can also be used for other effects
as shown here.
This BumpSelfshadow sample includes all source and content for the self shadowing
bump map algorithm presented at the 2001 GDC lecture by Dan Baker and Chas Boyd.
Presentatin itself can be found .
This app will run without pixel shaders, as long as the hardware has rendertargets and DOT3.
However, it runs much more efficently with pixel shaders and with better visual results.
The BumpUnderwater sample demonstrates an underwater effect that can be
acheived using bumpmapping. Bumpmapping is a texture blending technique used
to render the appearance of rough, bumpy surfaces, but can also be used for
other effects as shown here.
30-Dec-2003
The ClipMirror sample demonstrates the use of custom-defined clip planes.
A 3D scene is rendered normally, and then again in a 2nd pass as if reflected
in a planar mirror. Clip planes are used to clip the reflected scene to the
edges of the mirror.
NOTE: This sample is no longer included in original MS DirectX SDK.
30-Dec-2003
The ClipVolume sample demonstrates a technique for using shaders to
"subtract" a sphere from an arbitrary model when rendering. This technique
could be useful in visualization applications to see inside complex objects.
The sample also shows how to perform two-sided lighting and pixel level clipping via texkill.
09-Feb-2004
Free Pascal compatible
The CompiledEffect sample shows how an ID3DXEffect object can be compiled when the project is built and loaded
directly as a binary file at run time. This sample can be treaded like BasicHLSL modified to use precompiled EffectFile.
30-Dec-2003
The Cull sample illustrates how to cull objects whose object bounding box (OBB) does
not intersect the viewing frustum. By not passing these objects to Microsoft&
Direct3D&, you save the time that would be spent by Direct3D transforming and lighting
these objects, which will never be visible. The time saved could be significant if there
are many such objects or if the objects contain many vertices.
More elaborate and efficient culling can be done by creating hierarchies of objects, with
bounding boxes around groups of objects, so that not every object's OBB has to be compared
to the view frustum.
It is more efficient to do this OBB/frustum intersection calculation in your own code than
to use Direct3D to transform the OBB and check the resulting clip flags.
You can adapt the culling routines in this sample meet the needs of programs that you write.
30-Dec-2003
The DepthOfField sample shows a technique for creating a depth-of-field effect with
Direct3D, in which objects are only in focus at a given distance from the
camera, and are out of focus at other distances.
Sample uses FX effect-file to drive rendering for different hardware / techniques. Techniques include PixelShader 1.1,
PixelShader 1.4 and PixelShader 2.0 variations with HLSL (High Level Shading Language) and assembler implementations.
24-Jan-2003
Free Pascal compatible
The DolphinVS sample shows an underwater scene of a dolphin swimming, with
caustic effects on the dolphin and seafloor. The dolphin is animated using
a technique called &tweening&. The underwater effect simply uses fog, and
the water caustics use an animated set of textures. These effects are
achieved using vertex shaders
30-Dec-2003
The CubeMap sample demonstrates an environment mapping technique called cube mapping.
Environment mapping is a technique in which the environment surrounding a 3-D object, such
as the lights, is put into a texture map, so that the object can have complex lighting
effects without expensive lighting calculations.
The FishEye sample shows a fish-eye lens effect that can be achieved using cube maps.
The SphereMap sample demonstrates an environment mapping technique called sphere
mapping. Environment mapping is a technique in which the environment surrounding a 3-D
object, such as the lights, are put into a texture map, so that the object can have complex
lighting effects without expensive lighting calculations.
NOTE: This sample is no longer included in original MS DirectX SDK.
30-Dec-2003
The Fur sample shows how to do real-time fur rendering. This sample implementation is tied to cube
but contains all the code and media files to render any other fur meshes. Sample do not require hi-end hardware and should be
run even on TNT-class video cards - only with vertex shader code processed by CPU.
25-Apr-2004
Free Pascal compatible
The HDRCubeMap sample demonstrates cubic environment-mapping with floating-point textures and high
dynamic range lighting. In DirectX 9.0, floating-point formats have become available for textures. These formats can
store color values higher than 1.0, which can make lighting effects more realistic on the environment-mapped mesh when
the material absorbs part of the light.
Note that not all cards support all features for the environment-mapping and high dynamic range lighting techniques.
28-Jan-2004
Free Pascal compatible
This HLSLWithoutEffects sample shows an example of how to use Microsoft Direct3D's High-Level Shader
Language (HLSL), without the use of the Direct3D Extension (D3DX) effect interfaces. Not using the ID3DXEffect
interface is a more difficult method of using HLSL. See the BasicHLSL sample for a simpler method of using HLSL.
30-Dec-2003
The Lighting samples shows how to use D3D lights when rendering.
It shows the difference between
the various types of lights (ambient, point, directional, spot), how to configure these lights, and
how to enable and disable them.
NOTE: This sample is no longer included in original MS DirectX SDK.
30-Dec-2003
The LightingVS sample is an extension of the Lighting sample that adds
vertex shader implementations of lighting.
It lets you add multiple lights
and compare the output and performance between the vertex shader lights
and the fixed-function pipeline lights.
30-Dec-2003
The EnhancedMesh sample shows how to use D3DX to load and enhance a mesh.
The mesh is enhanced by increasing the vertex count using N-Patch tesselation algorithm.
The OptimizedMesh sample illustrates how to load and optimize a file-based mesh
using the Microsoft}

我要回帖

更多关于 directx 画圆 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信