1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
| using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using D3D = Microsoft.DirectX.Direct3D;
namespace DirectX_Tutorial
{
public class WinForm : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private D3D.Device device;
private Material material;
private float scaling = 0.2f;
private Mesh spacemesh;
private float spacemeshradius;
private Material[] spacemeshmaterials;
private Texture[] spacemeshtextures;
private Texture gradientTexture = null;
private Effect effect = null;
private Matrix matView;
private Matrix matProjection;
private Matrix skyBoxWorldMat;
public WinForm()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}
public void InitializeDevice()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.AutoDepthStencilFormat = DepthFormat.D16;
presentParams.EnableAutoDepthStencil = true;
device = new D3D.Device(0, D3D.DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
device.RenderState.Lighting = false;
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
device.BeginScene();
device.Transform.World = Matrix.Identity;
skyBoxWorldMat = Matrix.Scaling(scaling, scaling, scaling);
DrawMesh(spacemesh, spacemeshmaterials, spacemeshtextures);
RenderSky(device);
device.EndScene();
device.Present();
this.Invalidate();
}
private void SetUpCamera()
{
matView = Matrix.LookAtLH(new Vector3(20, 5, 13), new Vector3(0, 0, -10), new Vector3(0, 1, 0));
matProjection = Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)this.Width / (float)this.Height, 0.3f, 500f);
device.Transform.Projection = matProjection;
device.Transform.View = matView;
}
public void RenderSky(Device device)
{
device.Transform.World = skyBoxWorldMat;
device.RenderState.CullMode = Cull.None;
if (effect == null)
{
effect = Effect.FromFile(device, "SkyHLSL.fx", null,null, ShaderFlags.None, null);
//effect. LoadSkyEffect(device, "SkyHLSL.fx");
}
gradientTexture = TextureLoader.FromFile(device, "ocean.bmp");
effect.SetValue("g_WorldViewProj", skyBoxWorldMat * matView * matProjection);
effect.SetValue("g_GradientTexture", gradientTexture);
effect.Technique = "RenderSky";
for (int i = 0; i < spacemeshmaterials.Length; i++)
{
device.Material = spacemeshmaterials[i];
effect.SetValue("g_BaseTexture", spacemeshtextures[i]);
// Apply the technique contained in the effect
int passes = effect.Begin(0);
for (int pass = 0; pass < passes; pass++)
{
effect.BeginPass(pass);
// Draw the mesh subset
spacemesh.DrawSubset(i);
effect.EndPass();
}
effect.End();
}
}
private void LoadTexturesAndMaterials()
{
material = new Material();
material.Diffuse = Color.White;
material.Ambient = Color.White;
device.Material = material;
}
private void LoadMesh(string filename, ref Mesh mesh, ref Material[] meshmaterials, ref Texture[] meshtextures, ref float meshradius)
{
ExtendedMaterial[] materialarray;
mesh = Mesh.FromFile(filename, MeshFlags.Managed, device, out materialarray);
if ((materialarray != null) && (materialarray.Length > 0))
{
meshmaterials = new Material[materialarray.Length];
meshtextures = new Texture[materialarray.Length];
for (int i = 0; i < materialarray.Length; i++)
{
meshmaterials[i] = materialarray[i].Material3D;
meshmaterials[i].Ambient = meshmaterials[i].Diffuse;
if ((materialarray[i].TextureFilename != null) && (materialarray[i].TextureFilename != string.Empty))
{
meshtextures[i] = TextureLoader.FromFile(device, materialarray[i].TextureFilename);
}
}
}
}
private void LoadMeshes()
{
LoadMesh("sphere.x", ref spacemesh, ref spacemeshmaterials, ref spacemeshtextures, ref spacemeshradius);
}
private void DrawMesh(Mesh mesh, Material[] meshmaterials, Texture[] meshtextures)
{
for (int i = 0; i < meshmaterials.Length; i++)
{
device.Material = meshmaterials[i];
device.SetTexture(0, meshtextures[i]);
mesh.DrawSubset(i);
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(500, 500);
this.Text = "Riemer's DirectX Tutorial using C# -- Season 2";
}
static void Main()
{
using (WinForm our_directx_form = new WinForm())
{
our_directx_form.InitializeDevice();
our_directx_form.SetUpCamera();
our_directx_form.LoadTexturesAndMaterials();
our_directx_form.LoadMeshes();
Application.Run(our_directx_form);
}
}
}
}[/B]
voila le code HLSL : : :
[B]// Global variables
//--------------------------------------------------------------------------------------
texture g_BaseTexture;
texture g_GradientTexture;
float4x4 g_WorldViewProj; // World * View * Projection matrix
float g_Movex = float( 75.00 );
float g_Movey = float( 75.00 );
//--------------------------------------------------------------------------------------
// Texture samplers
//--------------------------------------------------------------------------------------
sampler BaseTextureSampler =
sampler_state
{
Texture = <g_BaseTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
sampler GradientTextureSampler =
sampler_state
{
Texture = <g_GradientTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
//--------------------------------------------------------------------------------------
// Vertex shader output structure
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
float4 Position : POSITION; // vertex position
float2 TextureUV : TEXCOORD0; // vertex texture coords
};
//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSkyVS( float4 vPos : POSITION,
float3 vTexCoord0 : TEXCOORD0 )
{
VS_OUTPUT Output;
Output.Position = mul(vPos, g_WorldViewProj);
Output.TextureUV = vTexCoord0;
return Output;
}
//--------------------------------------------------------------------------------------
// Pixel shader output structure
//--------------------------------------------------------------------------------------
struct PS_OUTPUT
{
float4 RGBColor : COLOR0; // Pixel color
};
//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------
PS_OUTPUT RenderSkyPS( VS_OUTPUT In )
{
PS_OUTPUT Output;
float4 base, gradient;
base = tex2D(BaseTextureSampler, In.TextureUV + 0.3f *float2 (g_Movex, g_Movey));
gradient = tex2D(GradientTextureSampler, In.TextureUV);
Output.RGBColor = base * (1-gradient) + gradient;
return Output;
}
//--------------------------------------------------------------------------------------
// Renders scene to render target
//--------------------------------------------------------------------------------------
technique RenderSky
{
pass P0
{
VertexShader = compile vs_1_1 RenderSkyVS( );
PixelShader = compile ps_1_4 RenderSkyPS( );
}
} |
Partager