본문 바로가기

프로그래밍/공부관련

* code sampler


* code sampler

 

Initialization
 

Demonstrates how to initialize Direct3D. This sample is by far the simplest of all Direct3D samples on this page. If you know nothing about Direct3D, you probably need to start here. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant C++ Keywords: Direct3DCreate9, GetAdapterDisplayMode, CheckDeviceFormat, GetDeviceCaps, CreateDevice, Clear, BeginScene, EndScene, Present, Release, D3D_SDK_VERSION, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, D3DFMT_D16, D3DCREATE_HARDWARE_VERTEXPROCESSING, D3DSWAPEFFECT_DISCARD, D3DCLEAR_TARGET, D3DCLEAR_ZBUFFER, D3DCOLOR_COLORVALUE, BackBufferFormat, SwapEffect, Windowed, EnableAutoDepthStencil, and AutoDepthStencilFormat.

  • Download (Last Updated: 05/27/05 - C++)

 

 

<?XML:NAMESPACE PREFIX = O />

Full-Screen Initialization
 

This sample demonstrates how to probe the hardware for specific Display or Adaptor Modes and hardware support suitable for a full-screen application with Direct3D. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant C++ Keywords: GetAdapterModeCount, EnumAdapterModes, CheckDeviceType, CheckDeviceFormat, GetDeviceCaps, D3DFMT_X8R8G8B8, D3DDISPLAYMODE, Width, Height, Format, RefreshRate, and D3DUSAGE_DEPTHSTENCIL.

 

  • Download (Last Updated: 05/27/05 - C++)

 

 

View Ports
 

This sample demonstrates how to set multiple view ports using DirectX 9.0.

Relevant C++ Keywords: SetViewport, Clear, BeginScene, EndScene, Present, Y, X, Width, Height, MinZ, MaxZ, and D3DVIEWPORT9.

  • Download (Last Updated: 06/02/05 - C++)

 

 

Multiple Devices
 

This sample demonstrates how to render to multiple windows using multiple devices under DirectX 9.0. You should note that this is not considered the most optimal way to manage two windows from one application. The preferred method is to create one Direct3D device and use CreateAdditionalSwapChain() to create additional front and back buffer pairs for rendering in multiple windows. This allows two windows to share resources like meshes and textures and keeps the driver from performing a costly context switch between multiple devices. The usage of CreateAdditionalSwapChain() is covered in the sample below.

Relevant Keywords: CreateDevice, Present, BeginScene, EndScene, and D3DPRESENT_PARAMETERS.

 

Swap Chains
 

This sample demonstrates how to render to multiple windows using swap chains under DirectX 9.0. This is far more efficient than creating multiple Direct3D devices, which is covered in the above sample.

Relevant Keywords: GetSwapChain, CreateAdditionalSwapChain, CreateDevice, GetBackBuffer, SetRenderTarget, Present, BeginScene, EndScene, D3DPRESENT_PARAMETERS, and LPDIRECT3DSWAPCHAIN9.

Download 146 KB (Last Updated: 10/14/04)

Texture Mapping

Demonstrates basic texture mapping using a textured quad and a bitmap of some wooden floor tiles. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: D3DXCreateTextureFromFile, SetSamplerState, SetTexture, LPDIRECT3DTEXTURE9, D3DSAMP_MINFILTER, D3DSAMP_MAGFILTER, and D3DTEXF_LINEAR.

  • Download 234.9 KB (Last Updated: 10/14/04)

Texture Filtering

Demonstrates how to filter out unwanted graphical artifacts from Direct3D textures. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: D3DXCreateTextureFromFile, SetSamplerState, SetTexture, LPDIRECT3DTEXTURE9, D3DSAMP_MINFILTER, D3DSAMP_MAGFILTER, D3DTEXF_NONE, D3DTEXF_POINT, D3DTEXF_LINEAR, D3DTEXF_ANISOTROPIC, D3DTEXF_FLATCUBIC, and D3DTEXF_GAUSSIANCUBIC,

  • Download 201 KB (Last Updated: 10/14/04)

Texture Blending

Demonstrates how to alpha-blend Direct3D textures. The sample creates a textured cube as an example whose sides are alpha-blended with one another in such a way as to create a translucent effect. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: SetRenderState, D3DRS_ALPHABLENDENABLE, D3DRS_ZENABLE, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA, D3DRS_DESTBLEND, D3DBLEND_ONE, D3DRS_CULLMODE, and D3DCULL_NONE.

  • Download 219 KB (Last Updated: 10/14/04)

Texture Addressing

Demonstrates how texture addressing works under Direct3D by allowing the texture addressing mode to be changed at run-time with a textured quad whose texture coordinates range from 0.0 to 3.0. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: SetSamplerState, D3DSAMP_BORDERCOLOR, D3DSAMP_ADDRESSU, D3DSAMP_ADDRESSV, D3DSAMP_ADDRESSW, D3DTADDRESS_WRAP, D3DTADDRESS_CLAMP, D3DTADDRESS_MIRROR, D3DTADDRESS_BORDER, and D3DTADDRESS_MIRRORONCE.

  • Download 198 KB (Last Updated: 10/14/04)

Texture Sub-loading

Demonstrates how texture sub-loading works under Direct3D by copying a smaller texture over the texel data of a larger texture. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: D3DXCreateTextureFromFile, D3DXLoadSurfaceFromSurface, LPDIRECT3DSURFACE9, LPDIRECT3DTEXTURE9, and D3DX_DEFAULT

  • Download 190.6 KB (Last Updated: 10/14/04)

Texture Mip-mapping

This sample demonstrates how mip-mapping works under Direct3D by tiling a single textured quad across a 25x25 unit area. The original texture is simply white with a thin black border, but in a effort to show mip-mapping in action, several mip-map levels have been altered by replacing the auto generated mip-map texture for that level with a color-coded version. This causes mip-map filtering to produce rainbow colored bands which radiate across the surface as the eye point moves about. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: SetSamplerState, D3DXCreateTextureFromFile, D3DXLoadSurfaceFromSurface, LPDIRECT3DSURFACE9, LPDIRECT3DTEXTURE9, D3DX_DEFAULT, D3DSAMP_MINFILTER, D3DSAMP_MAGFILTER, D3DSAMP_MIPFILTER, D3DTEXF_NONE, D3DTEXF_POINT, D3DTEXF_LINEAR, and D3DTEXF_ANISOTROPIC.

  • Download 197.5 KB (Last Updated: 10/14/04)

Multi-texture

This sample demonstrates how to perform multi-texturing under Direct3D by either modulating or adding two textures together in one rendering pass. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: SetTextureStageState, SetSamplerState, D3DTSS_TEXCOORDINDEX, D3DTSS_COLOROP, D3DTSS_COLORARG1, D3DTSS_COLORARG2, D3DTOP_MODULATE, D3DTA_TEXTURE, D3DTA_DIFFUSE, and D3DPT_TRIANGLESTRIP.

  • Download 190.9 KB (Last Updated: 10/14/04)

Depth Bias

This sample demonstrates how to eliminate z-fighting when rendering polygons directly on top of other polygons  by using Direct3D's D3DRS_SLOPESCALEDEPTHBIAS and D3DRS_DEPTHBIAS render states.

Relevant Keywords: SetRenderState, D3DRS_SLOPESCALEDEPTHBIAS, and D3DRS_DEPTHBIAS.

  • Download 198 KB (Last Updated: 10/14/04)

Fonts

This sample demonstrates how to render text to the screen using Direct3D's ID3DXFont class.

Relevant Keywords: GetDC, GetDeviceCaps, ReleaseDC, CreateFont, D3DXCreateFont, DeleteObject, SetRect, Begin, End, DrawText, RECT, DT_SINGLELINE, FW_DONTCARE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, and FF_DONTCARE.

  • Download 140 KB (Last Updated: 10/14/04)

View Matrix

Demonstrates how to manually modify the view matrix using mouse and keyboard input to create a game style motion control. This sample is very similar to the "First-Person-Shooter View Control" sample except this one uses no DirectInput. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: SetTransform, D3DXMatrixPerspectiveFovLH, D3DXMatrixRotationAxis, D3DXVec3TransformCoord, D3DXMatrixScaling, D3DXMatrixIdentity, D3DXVec3Normalize, D3DXVec3Dot,  D3DXVec3Cross, D3DXLoadMeshFromX, GetBufferPointer, DrawSubset, D3DXMATERIAL, D3DXMESH_SYSTEMMEM, D3DTS_VIEW, D3DTS_WORLD, D3DTS_PROJECTION, LPD3DXMESH, MatD3D, pTextureFilename, Ambient, and Diffuse.

  • Download 280.7 KB (Last Updated: 10/14/04)

Lighting

This sample demonstrates the three basic types of lights that are available in Direct3D: directional, spot, and point. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: SetLight, SetRenderState, LightEnable, D3DXMatrixTranslation, D3DXMatrixRotationZ, D3DXToRadian, D3DXMatrixLookAtLH, D3DXMatrixInverse, D3DXCreateSphere, D3DXCreateCylinder, D3DRS_LIGHTING, D3DLIGHT_DIRECTIONAL, D3DLIGHT_SPOT, D3DLIGHT_POINT, D3DRS_AMBIENT, D3DLIGHT9, D3DMATERIAL9, Diffuse, Ambient, Type, Direction, Position, Theta, Phi, Falloff, Attenuation0, and Attenuation1.

  • Download 150 KB (Last Updated: 10/14/04)

Materials

Demonstrates how to use materials with lighting to produce different surface effects. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: SetMaterial, SetRenderState, D3DLIGHT_DIRECTIONAL, SetLight, LightEnable, D3DMATERIAL9, D3DRS_DIFFUSEMATERIALSOURCE, D3DRS_SPECULARMATERIALSOURCE, D3DRS_AMBIENTMATERIALSOURCE, D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL, D3DLIGHT9, Diffuse, Ambient, Specular, Power, D3DRS_COLORVERTEX, D3DRS_AMBIENT, and D3DCOLOR_COLORVALUE.

  • Download 183 KB (Last Updated: 10/14/04)

Lost Device Recovery

This sample demonstrates how to recover from a lost device. A device can become "lost" when an event, such as the loss of keyboard focus in a full-screen application, causes rendering to become impossible. The lost state is characterized by the silent failure of all rendering operations, which means that the rendering methods can return success codes even though the rendering operations fail. In this situation, the error code D3DERR_DEVICELOST is returned by IDirect3DDevice9::Present.

Relevant Keywords: Present, TestCooperativeLevel, D3DERR_DEVICELOST, D3DERR_DEVICENOTRESET, Reset, BackBufferWidth, BackBufferHeight, and Release.

  • Download 341 KB (Last Updated: 10/14/04)

Resizing DirectX 9.0 Window

This sample demonstrates how to respond to the app's window getting resized by resizing the front and back buffers of the Direct3D device to match it. If you don't do this, Direct3D will be forced to perform a stretch blit when the window is enlarged and everything rendered will appearcourse and blocky. For example, if the initial window size and Direct3D device are set to 640x480 and you enlarge the window to be 1024x768, the Direct3D device will continue to render at 640x480 unless its front back buffers are resized accordingly.

Relevant Keywords: WM_SIZE, Reset, BackBufferWidth, BackBufferHeight, and Release.

  • Download 341 KB (Last Updated: 10/14/04)

Point Sprites

This sample demonstrates how to use point sprites with Direct3D. Point sprites are hardware-accelerated billboards, which are capable of being textured. Point sprites are ideal for creating high-performance particle systems because you only have to send a single vertex point or point sprite for each particle instead of four vertices for a regular billboarded quad. Point sprites also do all the math involved in view aligning the final quad on the GPU instead of the CPU. See the Particle System sample below for a more complex example of their usage. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: SetRenderState, D3DUSAGE_DYNAMIC, D3DUSAGE_POINTS, D3DUSAGE_WRITEONLY, D3DFVFCAPS_PSIZE, D3DRS_POINTSPRITEENABLE, D3DRS_POINTSIZE, D3DRS_POINTSIZE_MIN, D3DRS_POINTSCALE_A, D3DRS_POINTSCALE_B, D3DRS_POINTSCALE_C, D3DLOCK_DISCARD, D3DPT_POINTLIST, D3DRS_ALPHABLENDENABLE, D3DBLEND_ONE, D3DRS_ZWRITEENABLE, D3DLOCK_DISCARD, and FVFCaps.

Note: Requires a video card that supports Direct3D Point Sprites.

  • Download 193.4 KB (Last Updated: 10/14/04)

Particle System (Using Point Sprites)

Adding a particle system to your game engine has become all the rage, but where do you start? Here of course! This sample demonstrates how to create a particle system class which uses Direct3D's hardware accelerated point sprites. To learn more about point sprites, check out the Point Sprites sample above. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Note: Requires a video card that supports Direct3D Point Sprites.

  • Download 231.5 KB (Last Updated: 10/14/04)

Off-screen Rendering

This code sample demonstrate how to create dynamic textures through the use of an off-screen rendering surface under Direct3D. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: D3DXCreateRenderToSurface, D3DXMatrixRotationYawPitchRoll, LPD3DXRENDERTOSURFACE, LPDIRECT3DTEXTURE9, and LPDIRECT3DSURFACE9.

  • Download 193.5 KB (Last Updated: 10/14/04)

Occlusion Query

This sample demonstrates how to use Direct3D new occlusion query feature to find out how many pixels of an object are actually visible. The sample is basically a simplified port of the nVIDIA's OpenGL sample, which covers the same subject. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: CreateQuery, GetData, D3DXCreateSphere, D3DXCreateBox, CloneMeshFVF, GetVertexBuffer, GetNumVertices, D3DQUERYTYPE_OCCLUSION, Issue, D3DISSUE_BEGIN, D3DISSUE_END, and LPDIRECT3DQUERY9.

 

Primitive Types

This sample demonstrates how to use the 6 primitive types available under Direct3D: point list, line list, line strip, triangle list, triangle strip, and triangle fan.

Relevant Keywords: CreateVertexBuffer, Lock, Unlock, SetStreamSource, SetTexture, SetFVF, DrawPrimitive, D3DPT_POINTLIST, D3DPT_LINELIST, D3DPT_LINESTRIP, D3DPT_TRIANGLELIST, D3DPT_TRIANGLESTRIP, D3DPT_TRIANGLEFAN, LPDIRECT3DVERTEXBUFFER9, LPDIRECT3DTEXTURE9, and D3DPOOL_DEFAULT.

  • Download 84.8 KB (Last Updated: 10/14/04)

Vertex Data (Vertex Buffers)

Demonstrates how to use Vertex Buffers for the storage of model data. The sample creates a textured cube as an example. Check out the OpenGL source code page for a Vertex Array sample which is OpenGL's equivalent of Direct3D's Vertex Buffers.

Relevant Keywords: CreateVertexBuffer, Lock, Unlock, SetStreamSource, SetTexture, SetFVF, DrawPrimitive, LPDIRECT3DVERTEXBUFFER9, LPDIRECT3DTEXTURE9, D3DPT_TRIANGLESTRIP, and D3DPOOL_DEFAULT.

  • Download 189.8 KB (Last Updated: 10/14/04)

Multiple Vertex Buffers

This sample demonstrates how to create 3D geometry with Direct3D by loading vertex data into multiple Vertex Buffers. Check out the OpenGL source code page for the OpenGL equivalent of this sample, which uses multiple Vertex Arrays.

Relevant Keywords: CreateVertexBuffer, Lock, Unlock, SetStreamSource, SetTexture, SetFVF, DrawPrimitive, CreateVertexDeclaration, SetVertexDeclaration, SetStreamSource, D3DDECL_END, LPDIRECT3DVERTEXDECLARATION9, D3DVERTEXELEMENT9, D3DDECLTYPE_FLOAT3, D3DDECLTYPE_D3DCOLOR, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, D3DDECLUSAGE_COLOR, D3DDECLUSAGE_TEXCOORD LPDIRECT3DVERTEXBUFFER9, LPDIRECT3DTEXTURE9, D3DPT_TRIANGLESTRIP, and D3DPOOL_DEFAULT.

  • Download 192.8 KB (Last Updated: 10/14/04)

Indexed Geometry

This sample demonstrates how to optimize performance by using indexed geometry. As a demonstration, the sample reduces the vertex count of a simple cube from 24 to 8 by redefining the cube’s geometry using an indices array. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: CreateVertexBuffer, CreateVertexBuffer, Lock, Unlock, DrawIndexedPrimitive, DrawPrimitive, D3DPT_TRIANGLESTRIP, SetStreamSource, SetIndices, SetFVF, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, , and D3DPOOL_DEFAULT.

  • Download 92 KB (Last Updated: 10/14/04)

Transforms

Demonstrates how to use concatenated translation, rotation, and scaling matrices to create a simulated solar system. And as a small bonus, the sample also demonstrates how to optimize Direct3D applications using a matrix stack to prevent continuous changes to the view matrix, which can kill your apps draw time.

On an unrelated side-note, the sample also shows how to clone a mesh so you can add additional properties to its FVF code or vertex layout. The sample uses D3DXCreateSphere to create test spheres, which originally have no color. I then clone the mesh and add colors to them for demo purposes.

Relevant Keywords: SetTransform, D3DXMatrixTranslation, D3DXMatrixRotationY, D3DXMatrixScaling, D3DXCreateSphere, CloneMeshFVF, GetVertexBuffer, GetNumVertices, D3DXCreateMatrixStack, LoadIdentity, LoadMatrix, Push, Pop, MultMatrixLocal, LPD3DXMATRIXSTACK, and LPD3DXMESH.

  • Download 203 KB (Last Updated: 10/14/04)

Emboss Bump Mapping

These samples demonstrate how to perform one-pass and two-pass emboss bump mapping with Direct3D. I've also included code, which demonstrates how to calculate tangent and binormal vectors on a per-vertex basis. These special vectors define the texture space for each vertex, which allows for accurate texture coordinate shifting during the bump mapping process. Check out the OpenGL source code page for the OpenGL equivalent of these samples.

Relevant Keywords: SetTextureStageState, SetRenderState, SetTexture, D3DXVec3Dot, D3DXVec3Normalize, D3DXMatrixInverse, GetTransform, D3DXCreateSphere, D3DTSS_TEXCOORDINDEX, D3DTSS_COLOROP, D3DTSS_COLORARG1, D3DTSS_COLORARG2, D3DTSS_ALPHAOP, D3DTSS_ALPHAARG1, D3DTSS_ALPHAARG2, D3DTOP_MODULATE, D3DTA_TEXTURE, D3DTA_DIFFUSE, D3DTOP_SELECTARG1, D3DTA_CURRENT, D3DTOP_ADDSIGNED, D3DTA_COMPLEMENT, D3DTOP_MODULATE2X, D3DTA_ALPHAREPLICATE, D3DTOP_DISABLE, and D3DRS_ALPHABLENDENABLE.

Note: If you have an older video card and the one-pass sample fails to work, try the "One-pass Fake Emboss Bump Mapping" code, which I left in the sample.

Shadow Volume

This code sample demonstrates how to create dynamic shadows with Direct3D using a stencil shadow volume. The sample is basically a simplified re-write of the ShadowVolume sample that ships with the DirectX 9 SDK.

Relevant Keywords: GetBackBuffer, SetRenderState, D3DXLoadMeshFromX, GetDeviceCaps, Lock, Unlock,  D3DSTENCILCAPS_TWOSIDED, D3DBACKBUFFER_TYPE_MONO, D3DRS_STENCILENABLE, D3DRS_SHADEMODE, D3DRS_STENCILENABLE, D3DRS_STENCILFUNC, D3DRS_STENCILZFAIL, D3DRS_STENCILFAIL, D3DRS_STENCILREF, D3DRS_STENCILMASK, D3DRS_STENCILWRITEMASK, D3DCMP_LESSEQUAL, D3DRS_STENCILPASS, D3DSTENCILOP_KEEP, D3DRS_ZWRITEENABLE, D3DRS_ALPHABLENDENABLE, D3DRS_SRCBLEND, D3DRS_DESTBLEND, D3DBLEND_ZERO, D3DBLEND_ONE, D3DSTENCILOP_KEEP, D3DCMP_ALWAYS, D3DSHADE_FLAT, D3DSTENCILOP_DECR, D3DRS_CULLMODE, D3DCULL_NONE, D3DCULL_CCW, D3DSHADE_GOURAUD, D3DRS_FILLMODE, D3DFILL_WIREFRAME, D3DFILL_SOLID, and StencilCaps.

  • Download 287.9 KB (Last Updated: 10/14/04)
  • Learn More 336 KB
  • Learn More (Gamasutra.com - Real-Time Shadow Casting Using Shadow Volumes)
  • Learn More (GameDev.net - The Theory of Stencil Shadow Volumes)

Creating 2D Sprites with D3DXSprite

This sample demonstrates how to create a animated 2D sprite using D3DXSprite which is hardware accelerated and fully compatible with 3D generated content.

  • Download 229 KB (Last Updated: 10/14/04)

Simple Effect Using .fx Files

This sample demonstrates how to write and use Effect files (.fx) under DirectX 8.1. The sample Effect is very straightforward. It defines a technique called "TwoPassTextureBlend", which has two separate rendering passes, called "Pass0" and "Pass1". The two passes are then used to basically blend two textures together through the simple addition of the texel data.

Relevant Keywords: D3DXCreateEffectFromFile, GetBufferPointer, SetTexture, SetTechnique, Begin, End, texture, technique, pass, AlphaBlendEnable, Texture, ColorOp, ColorArg1, SelectArg1, Disable, and LPD3DXEFFECT.

  • Download 445.8 KB (Last Updated: 10/14/04)

Simple Vertex & Pixel Shader (HLSL/Fx Effect File)

This code sample demonstrates how to write simple vertex and pixel shaders using Direct3D's new High-Level Shading Language. The two shaders reside within a Direct3D Fx Effect File and have matching inputs and outputs, which allow them to be used simultaneously on the same piece of geometry.

Relevant Keywords: D3DXCreateEffectFromFile, SetTechnique, SetMatrix, SetTexture, Begin, End, float4x4, texture, sampler, technique, pass, LPD3DXEFFECT, and LPD3DXBUFFER.

  • Download 445.4 KB (Last Updated: 10/14/04)

Simple Vertex Shader (vs.2.0)

Demonstrates how to write an assembly-level vertex shader using the vs2.0 instruction set defined in DirectX 9.0.

Relevant Keywords: CreateVertexDeclaration, D3DXAssembleShaderFromFile, CreateVertexShader, LPDIRECT3DVERTEXDECLARATION9, LPDIRECT3DVERTEXSHADER9, D3DVERTEXELEMENT9, ID3DXBuffer, D3DDECLTYPE_FLOAT3, D3DDECLUSAGE_COLOR, D3DDECLUSAGE_POSITION, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECL_END, vs.2.0, dcl_position, dcl_color, m4x4, oPos, oD0, and mov.

  • Download 338 KB (Last Updated: 10/14/04)

Simple Vertex Shader (Cg)

Demonstrates how to write a simple vertex shader, or vertex program using Cg. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: CGcontext, CGprogram, CGparameter, cgCreateContext, cgD3D9GetLatestVertexProfile, cgCreateProgramFromFile, cgD3D9ValidateVertexDeclaration, cgD3D9LoadProgram, cgGetNamedParameter, cgGetParameterType, cgD3D9TypeToSize, cgD3D9SetDevice, cgDestroyProgram, cgDestroyContext, cgD3D9SetUniformMatrix, cgD3D9SetUniform, cgD3D9BindProgram, CreateVertexDeclaration, LPDIRECT3DVERTEXDECLARATION9, LPDIRECT3DVERTEXDECLARATION9, POSITION, COLOR0, uniform, float4x4, float4, mul, and return.

  • Download 69.3 KB (Last Updated: 10/14/04)

Simple Vertex & Pixel Shader (Cg)

Demonstrates how to write both a simple vertex and pixel shader using Cg. The two shaders have matching connectors and can be used simultaneously on the same piece of geometry. The shaders in this sample don't really do anything useful or neat, it's meant to be used as a framework or starting place for experimenting with shaders. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: cgD3D9GetLatestPixelProfile, cgD3D9GetOptimalOptions, cgD3D9SetTextureWrapMode, cgD3D9SetSamplerState, cgD3D9BindProgram, D3DXMatrixTranspose, D3DXMatrixRotationYawPitchRoll, D3DVERTEXELEMENT9, D3DDECLTYPE_FLOAT3, D3DDECLUSAGE_POSITION, D3DDECLTYPE_D3DCOLOR, D3DDECLUSAGE_COLOR, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, D3DSAMP_ADDRESSUD3DSAMP_ADDRESSV, D3DTADDRESS_WRAP, D3DTEXF_POINT, D3DTEXF_NONE, D3DSAMP_MINFILTER, D3DSAMP_MAGFILTER, D3DSAMP_MIPFILTER, float2, float3, float4, uniform, float4x4, sampler2D, and tex2D.

  • Download 194 KB (Last Updated: 10/14/04)

Lighting Shader (Cg)

Demonstrates how to write a vertex shader using Cg, which calculates simple diffuse lighting for a single light source. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: cgD3D9SetUniformMatrix, cgD3D9SetUniform, SetLight, SetRenderState, LightEnable, SetMaterial, D3DLIGHT9, D3DMATERIAL9, D3DRS_LIGHTING, LPDIRECT3DVERTEXDECLARATION9, POSITION, NORMAL, COLOR0, float4, float4x4, uniform, mul, normalize, dot, and pow.

  • Download 87.2 KB (Last Updated: 10/14/04)

Anisotropic Lighting Shader (Cg)

Demonstrates how to write a vertex shader using Cg, which calculates Anisotropic Lighting for a single light source. The shader uses a texture as a look-up table for the correct Anisotropic Lighting values by storing the pre-calculated diffuse values in the texture's RGB components and specular values in its alpha component. This style of lighting is very useful for rendering surfaces like brushed steel where the surface is composed of micro facets or microscopic scratches that tend to lay parallel or run in the same direction. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: D3DXLoadMeshFromX, GetVertexBuffer, GetIndexBuffer, CloneMeshFVF, Optimize, GetAttributeTable, LPDIRECT3DVERTEXDECLARATION9, LPDIRECT3DINDEXBUFFER9, D3DXATTRIBUTERANGE, D3DXMESH_SYSTEMMEM, D3DXMESHOPT_ATTRSORT, D3DXMESH_WRITEONLY, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR, D3DTOP_MODULATE4X, D3DTA_ALPHAREPLICATE, D3DTSS_COLOROP, D3DTSS_COLORARG1, D3DTSS_COLORARG2, D3DTA_TEXTURE, POSITION, NORMAL, TEXCOORD0, float3x3, float3x4, normalize, mul, and max.

Vertex Displacement or Mesh Deformation Shader (Cg)

10.0pt; color: black"> Demonstrates how to performs vertex displacement on a simple mesh to create a flag waving animation. 10.0pt; color: black">Check out the OpenGL source code 10.0pt; color: black">page for the OpenGL equivalent of this sample.

Relevant Keywords: CGcontext, CGprogram, CGparameter, cgCreateContext, cgD3D9GetLatestVertexProfile, cgCreateProgramFromFile, cgD3D9ValidateVertexDeclaration, cgD3D9LoadProgram, cgGetNamedParameter, cgGetParameterType, cgD3D9TypeToSize, cgD3D9SetDevice, cgDestroyProgram, cgDestroyContext, cgD3D9SetUniformMatrix, cgD3D9SetUniform, cgD3D9BindProgram, CreateVertexDeclaration, SetRenderState, D3DRS_FILLMODE, D3DFILL_WIREFRAME, D3DFILL_SOLID, LPDIRECT3DVERTEXDECLARATION9, LPDIRECT3DVERTEXDECLARATION9, POSITION, COLOR0, uniform, float4x4, float4, mul, sin, and return.

  • Download 198 KB (Last Updated: 10/14/04 )

Solid Node BSP Tree Compiler

One method of speeding up a game engine is to use a BSP-Tree (Binary-Space Partitioning Tree) to subdivide and sort your game's world geometry.

This is done by dividing up, sorting, and storing polys in a BSP Tree in a such as way as to denote their relationship to one another (i.e. being in front of or in back of another poly). Once the BSP tree is compiled or built, the engine can quickly decide whether or not it should render certain polys based on the view’s current location within the world.

For example, the BSP compiler in this sample demonstrates how early game programmers, who didn't have hardware accelerated z-buffers, used a simple solid-node style BSP tree to sort their scene polys into a back-to-front order for proper rendering. The sample also demonstrates how a BSP Tree can be used for collision detection by identifying whether or not a unobstructed line-of sight can be established between two 3D points.

Unfortunately, the code doesn't go into a lot of BSP Tree theory or teach the basics, so if you're new to the concept of a BSP Tree, check out this cool little Java Applet for a real-time demonstration of how they work.

This sample is based on one of the BSP demos that came with the "Advanced 3D BSP, PVS and CSG Techniques" course offered at GameInstitute.com. If you're truly interested in learning about BSP trees and how they're used in games, this course is the best by far and I highly recommend it.

Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: CreateDevice, SetSamplerState, SetDataFormat, GUID_SysMouse, SetCooperativeLevel, Acquire, Unacquire, GetDeviceState, DrawIndexedPrimitiveUP, D3DXVec3Dot, DISCL_NONEXCLUSIVE, DISCL_FOREGROUND, LPDIRECTINPUT8, LPDIRECTINPUTDEVICE8, D3DXVECTOR3, DIERR_INPUTLOST, DIMOUSESTATE2, D3DSAMP_MIPMAPLODBIAS, D3DSAMP_MAGFILTER, D3DSAMP_MINFILTER, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR, D3DFMT_INDEX16 and DIRECTINPUT_VERSION.

2D Demo Game of a Side-Scrolling Shooter

Re-live such classic side-scrolling shooters as Gradius and R-Type by writing your own. Of course, this sample is a far cry from a fully featured side-scroller, but it will definitely put you on the right track. Check out the DirectX 8.1 source code page for the DirectX 8.1 code equivalent of this sample.

  • Download 378 KB (Last Updated: 05/11/04)

How to use the Tokamak SDK to Create a Rope or Chain using Ball-Joints

This sample demonstrates how to use the Tokamak Physics SDK to create a chain or rope like object by using small collision cubes joined by ball-joints. For demonstration purposes, the chain is dropped onto a platform, which doesn’t quite catch all of it. This allows the chain to hang off and swing about. For fun, you can use the F1 key to lift up on the chain's last segment by applying some velocity to it. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: neSimulator, neRigidBody, neAnimatedBody, neSimulatorSizeInfo, neBoxInertiaTensor,  neJoint, neGeometry, CreateSimulator, CreateRigidBody, AddGeometry, SetBoxSize, CollideConnected, UpdateBoundingInfo, SetInertiaTensor, SetMass, SetJointFrameWorld, SetType, SetEpsilon, SetIteration, Enable, and GetTransform.

  • Download 161 KB (Last Updated: 10/14/04)

How to use the Tokamak SDK to Create a Stack of Blocks

This sample demonstrates how to use the Tokamak Physics SDK to create a stack of blocks which can be knocked over. For demonstration purposes, you can use the F1 key to lift up and drop the bottom block, which will obviously disturb the stack. You can also use the F2 key to tilt the blue platform and dump the blocks over the side. Check out the OpenGL source code page for the OpenGL equivalent of this sample.

Relevant Keywords: neSimulator, neRigidBody, neAnimatedBody, neSimulatorSizeInfo, neBoxInertiaTensor,  neJoint, neGeometry, CreateSimulator, CreateRigidBody, AddGeometry, SetBoxSize, UpdateBoundingInfo, SetInertiaTensor, SetMass, Enable, SetRotation and GetTransform.

  • Download 161 KB (Last Updated: 10/14/04)

How to use Tokamak's Different Geometry Types or Collision Primitives

This sample demonstrates how to use the box, sphere, and cylinder geometry types of the Tokamak Physics SDK. The sample is basically a variation of the stacking sample above.

Relevant Keywords: neRigidBody, neAnimatedBody, SetBoxSize, neBoxInertiaTensor, SetCylinder, neCylinderInertiaTensor, SetSphereDiameter, and neSphereInertiaTensor.

  • Download 226 KB (Last Updated: 10/14/04)
code sampler 예제