Forum
Contact





DirectX using C#
DirectX using C++
DirectX using Visual Basic



Latest Forum posts

 Backup DVD Escape Plan
  Posted by: Applefly
  When: 17/04/2014 at 06:00:12

 Disney DVDs to MP4
  Posted by: Applefly
  When: 17/04/2014 at 05:56:07

 DVD movies through media streamer
  Posted by: VIKIVannessa
  When: 16/04/2014 at 02:25:09

 DVD movies to play on Galaxy Tab 3 10.1
  Posted by: VIKIVannessa
  When: 16/04/2014 at 02:20:51

 Blu-ray to Apple ProRes
  Posted by: Applefly
  When: 12/04/2014 at 00:47:46

 HD H.264 MP4 format
  Posted by: Applefly
  When: 12/04/2014 at 00:43:23

 DVD collection to computer
  Posted by: VIKIVannessa
  When: 04/04/2014 at 07:13:33

 DVD movies to play on Galaxy Tab 3 10.1
  Posted by: VIKIVannessa
  When: 04/04/2014 at 07:11:27

 DVD movies to play on Galaxy Tab 3 10.1
  Posted by: VIKIVannessa
  When: 04/04/2014 at 07:11:27

 DVD Ripper to populate
  Posted by: Applefly
  When: 04/04/2014 at 05:01:35

 kids and family watch DVD
  Posted by: Applefly
  When: 04/04/2014 at 04:59:21

 Blu-ray movie for your lovely kids
  Posted by: VIKIVannessa
  When: 02/04/2014 at 05:47:49

 DVD to DNxHD Converter for Mac
  Posted by: VIKIVannessa
  When: 02/04/2014 at 05:46:04

 play HD Videos on Galaxy
  Posted by: Applefly
  When: 01/04/2014 at 03:16:39

 video on iPhone 6
  Posted by: Applefly
  When: 01/04/2014 at 03:15:18

 MessageBox
  Posted by: net777
  When: 31/03/2014 at 03:34:55

 Play MVI/MKV/AVI on Galaxy NotePro 12.2
  Posted by: VIKIVannessa
  When: 31/03/2014 at 01:30:21

 Create family videos with iMovie on Mac
  Posted by: VIKIVannessa
  When: 31/03/2014 at 01:29:25

 WMV format from DVD
  Posted by: Applefly
  When: 28/03/2014 at 02:55:09

 BD/DVD to FCP X
  Posted by: Applefly
  When: 28/03/2014 at 02:53:35




Topic: Multitextureshader selfmade



  
Goto parent category
  
Create a new user account


   Multitextureshader selfmade
 Poster : plucked
 Posts: 3
 Country : Germany
 City: Berlin

  
Posted by plucked on 03/03/2007 at 07:59:51
Hi there,

hi riemer! I love your Tutorials. You give me the first understanding of xna and shaderprogramming. Thank you!


Because I cant wait for the next step in the 4th tutorial, I've build a shader to do the multitexturepart. It was a interesting excerice and I can recommend for every beginner.


I work with ATI RenderMonkey. I don't want to say which of these is better. I think this is more suited for me (o:

Okay here is a screenshot of the multitextures terrain in rendermonkey (it's another terrain as in the tutorial):




And here is my commented source of the shader for interessted:



//**************************************************************//
//  Effect File exported by RenderMonkey 1.6
//
//  - Although many improvements were made to RenderMonkey FX  
//    file export, there are still situations that may cause  
//    compilation problems once the file is exported, such as  
//    occasional naming conflicts for methods, since FX format
//    does not support any notions of name spaces. You need to
//    try to create workspaces in such a way as to minimize    
//    potential naming conflicts on export.                    
//    
//  - Note that to minimize resulting name collisions in the FX
//    file, RenderMonkey will mangle names for passes, shaders  
//    and function names as necessary to reduce name conflicts.
//**************************************************************//

//--------------------------------------------------------------//
// TerrainMultiTexture
//--------------------------------------------------------------//
//--------------------------------------------------------------//
// Pass 0
//--------------------------------------------------------------//

float4x4 matWorld : World;
float upaxis
<
   string UIName = "upaxis";
   string UIWidget = "Numeric";
   bool UIVisible =  false;
   float UIMin = -1.00;
   float UIMax = 0.00;
> = float( 0.00 );
float4 weights
<
   string UIName = "weights";
   string UIWidget = "Direction";
   bool UIVisible =  false;
   float4 UIMin = float4( -10.00, -10.00, -10.00, -10.00 );
   float4 UIMax = float4( 10.00, 10.00, 10.00, 10.00 );
   bool Normalize =  false;
> = float4( 4.80, -33.88, -75.40, 14.80 );

float4x4 matViewProjection : ViewProjection;

struct VS_INPUT
{
   float4 Position : POSITION0;
   float2 Texcoord : TEXCOORD0;
   float3 Normal : NORMAL0;  
};

struct VS_OUTPUT
{
   float4 Position : POSITION0;
   float2 Texcoord : TEXCOORD0;
   float4 TexWeight : TEXCOORD1;
   float3 Normal : TEXCOORD2;
};

VS_OUTPUT Effect_Group_1_TerrainMultiTexture_Pass_0_Vertex_Shader_vs_main( VS_INPUT Input )
{
   VS_OUTPUT Output;
   float4x4 matWorldViewProjection = mul( matWorld, matViewProjection );
  
   // get the Position by the worldviewprojection
   Output.Position = mul( Input.Position, matWorldViewProjection );
   // get the texturecoordinates
   Output.Texcoord = Input.Texcoord;
   // initialize a variable to store the y (height) of the vertices
   float posy = (float)0;
   // store the height by the axisorientation
   if (upaxis < 0) posy = Input.Position.x;
   if (upaxis == 0) posy = Input.Position.y;
   if (upaxis > 0) posy = Input.Position.z;
  
   // create a float4 to store the textureweight
   // x : intensity of the snow texture
   // y : intensity of the rock
   // z : intensity of the grass
   // w : intensity of the sand
   float4 TexWeight = (float4)0;
  
   // given is the float4 weight
   // it contains following data:
   // x : start of the snow
   // y : start of the rock
   // z : start of the grass
   // w : width of the border to have a smooth fade between the textures
   // First we want to determine the 'fadingblock' between the snow and the rock
   // borderSnowRock.x = the start of the snow with full intensity
   // borderSnowRock.y = the End of the rock with full intensity
   // between borderSnowRock.x and borderSnowRock.y is a fade between the
   // intensity of the snow and rock
   float2 borderSnowRock = float2(weights.x + weights.w, weights.x - weights.w);
   // same for rock and gras
   float2 borderRockGras = float2(weights.y + weights.w, weights.y - weights.w);
   // and for gras and sand
   float2 borderGrasSand = float2(weights.z + weights.w, weights.z - weights.w);
  
   // if the height is greater then the top of the fadingblock between snow and rock
   if (posy > borderSnowRock.x)
   {
      // only snow get a intensity of 1
      TexWeight.x = 1;
   }
   // if the height is inside of the first fadingblock
   if (posy <= borderSnowRock.x && posy > borderSnowRock.y)
   {
      // get a fadein from the snow texture inside of the range
      TexWeight.x = smoothstep(borderSnowRock.y,borderSnowRock.x,posy);
      // get a fadeout from the rock texture
      TexWeight.y = smoothstep(borderSnowRock.x,borderSnowRock.y,posy);
   }
   // if height is smaller then the first fadingblock and greater then the
   // start of the second block
   if (posy <= borderSnowRock.y && posy > borderRockGras.x)
   {
      // only rock get intensity of 1
      TexWeight.y = 1;
   }
   // same as below ...
   if (posy <= borderRockGras.x && posy > borderRockGras.y)
   {
      TexWeight.y = smoothstep(borderRockGras.y,borderRockGras.x,posy);
      TexWeight.z = smoothstep(borderRockGras.x,borderRockGras.y,posy);
   }
   if (posy <= borderRockGras.y && posy > borderGrasSand.x)
   {
      TexWeight.z = 1;
   }
   if (posy <= borderGrasSand.x && posy > borderGrasSand.y)
   {
      TexWeight.z = smoothstep(borderGrasSand.y,borderGrasSand.x,posy);
      TexWeight.w = smoothstep(borderGrasSand.x,borderGrasSand.y,posy);
   }
   // if the height is smaller then the last block, the texture
   // will be gras
   if (posy <=borderGrasSand.y)
   {
      TexWeight.w = 1;
   }
  
   // store the weight for the PS
   Output.TexWeight = TexWeight;
   // normals for lightingeffects, etc.
   Output.Normal = Input.Normal;
  
   return( Output );
  
}




float textureComplexity
<
   string UIName = "textureComplexity";
   string UIWidget = "Numeric";
   bool UIVisible =  false;
   float UIMin = -1.00;
   float UIMax = 0.00;
> = float( 7.81 );
bool colorMode
<
   string UIName = "colorMode";
   string UIWidget = "Numeric";
   bool UIVisible =  false;
> = bool( false );
texture snowTex_Tex;

sampler2D snow = sampler_state
{
   Texture = (snowTex_Tex);
   ADDRESSU = MIRROR;
   ADDRESSV = MIRROR;
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
   MIPFILTER = LINEAR;
   ADDRESSW = MIRROR;
};
texture sandTex_Tex;

sampler2D sand = sampler_state
{
   Texture = (sandTex_Tex);
   ADDRESSU = MIRROR;
   ADDRESSV = MIRROR;
   ADDRESSW = MIRROR;
   MINFILTER = LINEAR;
   MIPFILTER = LINEAR;
   MAGFILTER = LINEAR;
};

texture rockTex_Tex;

sampler2D rock = sampler_state
{
   Texture = (rockTex_Tex);
   ADDRESSU = MIRROR;
   ADDRESSV = MIRROR;
   ADDRESSW = MIRROR;
   MIPFILTER = LINEAR;
   MINFILTER = LINEAR;
   MAGFILTER = LINEAR;
};
texture grassTex_Tex;

sampler2D grass = sampler_state
{
   Texture = (grassTex_Tex);
   ADDRESSU = MIRROR;
   ADDRESSV = MIRROR;
   ADDRESSW = MIRROR;
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
   MIPFILTER = LINEAR;
};

struct PS_INPUT
{
   float4 Position : POSITION0;
   float2 Texcoord : TEXCOORD0;
   float4 TexWeight : TEXCOORD1;
   float3 Normal : TEXCOORD2;

};

float4 Effect_Group_1_TerrainMultiTexture_Pass_0_Pixel_Shader_ps_main( PS_INPUT Input ) : COLOR0
{
   float4 color = 0;
  
   // prepare the textures
   // textureComplexity is to have an additional loop in the texture
   // I need this, because it looks better in RenderMonkey (o:
   float4 grassColor = tex2D( grass, Input.Texcoord * textureComplexity );
   float4 sandColor = tex2D( sand, Input.Texcoord * textureComplexity );
   float4 rockColor = tex2D( rock, Input.Texcoord * textureComplexity );
   float4 snowColor = tex2D( snow, Input.Texcoord * textureComplexity );
  
   // for debugging, we implement a "colormode"
   if (colorMode == true)
   {
      color = float4(1,0,0,1) * Input.TexWeight.x +
              float4(0,1,0,1) * Input.TexWeight.y +
              float4(0,0,1,1) * Input.TexWeight.z +
              float4(1,1,0,1) * Input.TexWeight.w;
   }
   else
   {
      // set the color with a intensity of each
      // texture
      color = snowColor * Input.TexWeight.x +
           rockColor * Input.TexWeight.y +
           grassColor * Input.TexWeight.z +
           sandColor * Input.TexWeight.w;
   }
  
   // basic light illusion
   color.rgb = Input.Normal.x * 0.4 + color.rgb * (0.6);
    
   return color;
}




//--------------------------------------------------------------//
// Technique Section for Effect Workspace.Effect Group 1.TerrainMultiTexture
//--------------------------------------------------------------//
technique TerrainMultiTexture
{
   pass Pass_0
   {

      VertexShader = compile vs_2_0 Effect_Group_1_TerrainMultiTexture_Pass_0_Vertex_Shader_vs_main();
      PixelShader = compile ps_2_0 Effect_Group_1_TerrainMultiTexture_Pass_0_Pixel_Shader_ps_main();
   }

}

 Poster : plucked
 Posts: 3
 Country : Germany
 City: Berlin

  
Posted by plucked on 03/03/2007 at 08:01:56
Oh the imagepath was to long:

 Poster : Anonymous
 Posts:
 Country :
 City:

  
Posted by Anonymous on 03/03/2007 at 10:53:59
http://img376.imageshack.us/img376/8421/mtrmft0.jpg
 Poster : Lordikon
 Posts: 57
 Country : USA
 City: Denver

  
Posted by Lordikon on 14/03/2007 at 00:06:56
I used this in place of the original that comes in the Series 4 tutorials and it compiles fine, however when I run it, it breaks immediately. I noticed there are different names for your techniques.

Can you show any changes you made to your code to get this to work? Thanks in advance.
 Poster : Anonymous
 Posts:
 Country :
 City:

  
Posted by Anonymous on 14/03/2007 at 07:08:08
Here is the full code:



using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.IO;

namespace Riemers_Tutorial_4
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public struct VertexMultitextured
        {
            public Vector3 Position;
            public Vector3 Normal;
            public Vector4 TextureCoordinate;
            public Vector4 TexWeights;

            public static int SizeInBytes = (3 + 3 + 4 + 4) * 4;
            public static VertexElement[] VertexElements = new VertexElement[]
     {
         new VertexElement( 0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0 ),
         new VertexElement( 0, sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Normal, 0 ),
         new VertexElement( 0, sizeof(float) * 6, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 0 ),
         new VertexElement( 0, sizeof(float) * 10, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 1 ),
     };
        }

        public struct VertexPositionNormalColored
        {
            public Vector3 Position;
            public Color Color;
            public Vector3 Normal;

            public static int SizeInBytes = 7 * 4;
            public static VertexElement[] VertexElements = new VertexElement[]
              {
                  new VertexElement( 0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0 ),
                  new VertexElement( 0, sizeof(float) * 3, VertexElementFormat.Color, VertexElementMethod.Default, VertexElementUsage.Color, 0 ),
                  new VertexElement( 0, sizeof(float) * 4, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Normal, 0 ),
              };
        }

        private GraphicsDeviceManager graphics;
        private ContentManager content;
        private GraphicsDevice device;
        private Effect effect;
        private VertexBuffer terrainVertexBuffer;
        private IndexBuffer terrainIndexBuffer;
        Texture2D heightMap;
        private int WIDTH;
        private int HEIGHT;
        private float[,] heightData;
        private Matrix viewMatrix;
        private Matrix projectionMatrix;

        private Vector3 cameraPosition = new Vector3(130, 50, 30);
        private Vector3 cameraAngles = new Vector3(-MathHelper.Pi / 10.0f, 0, MathHelper.PiOver2);

        private MouseState previousMouseState;

        private Texture2D sandTexture;
        private Texture2D grassTexture;
        private Texture2D rockTexture;
        private Texture2D snowTexture;

        private Vector4 weights = new Vector4(23, 14, 4, 7);
        private float upaxis = 1.0f;
        private float start = 0.41f;
        private float textureComplexity = 1;
        private bool colorMode = false;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }

        protected override void Initialize()
        {
            base.Initialize();
            LoadHeightData();

            SetUpTerrainterrainVertices();
            SetUpTerrainterrainIndices();
            SetInitialValues();
        }

        private void SetUpXNADevice()
        {
            device = graphics.GraphicsDevice;

            graphics.PreferredBackBufferWidth = 500;
            graphics.PreferredBackBufferHeight = 500;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();
            Window.Title = "Riemer's XNA Tutorials -- Series 4: Advanced terrain";


            effect = content.Load<Effect>("TerrainMultiTexture");
        }

        

        private void SetUpTerrainterrainVertices()
        {
            VertexMultitextured[] terrainVertices = new VertexMultitextured[WIDTH * HEIGHT];

             for (int x = 0; x < WIDTH; x++)
                 for (int y = 0; y < HEIGHT; y++)
                 {
                     terrainVertices[x + y * WIDTH].Position = new Vector3(x, y, heightData[x, y]);
                     terrainVertices[x + y * WIDTH].Normal = new Vector3(0, 0, 1);
                     terrainVertices[x + y * WIDTH].TextureCoordinate.X = (float)x / 30.0f;
                     terrainVertices[x + y * WIDTH].TextureCoordinate.Y = (float)y / 30.0f;
            
                     terrainVertices[x + y * WIDTH].TexWeights.X = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 0) / 8.0f, 0, 1);
                     terrainVertices[x + y * WIDTH].TexWeights.Y = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 12) / 6.0f, 0, 1);
                     terrainVertices[x + y * WIDTH].TexWeights.Z = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 20) / 6.0f, 0, 1);
                     terrainVertices[x + y * WIDTH].TexWeights.W = MathHelper.Clamp(1.0f - Math.Abs(heightData[x, y] - 30) / 6.0f, 0, 1);

                     float totalWeight = terrainVertices[x + y * WIDTH].TexWeights.X;
                     totalWeight += terrainVertices[x + y * WIDTH].TexWeights.Y;
                     totalWeight += terrainVertices[x + y * WIDTH].TexWeights.Z;
                     totalWeight += terrainVertices[x + y * WIDTH].TexWeights.W;
                     terrainVertices[x + y * WIDTH].TexWeights.X /= totalWeight;
                     terrainVertices[x + y * WIDTH].TexWeights.Y /= totalWeight;
                     terrainVertices[x + y * WIDTH].TexWeights.Z /= totalWeight;
                     terrainVertices[x + y * WIDTH].TexWeights.W /= totalWeight;
                 }
                

            for (int x = 1; x < WIDTH - 1; x++)
            {
                for (int y = 1; y < HEIGHT - 1; y++)
                {
                    Vector3 normX = new Vector3((terrainVertices[x - 1 + y * WIDTH].Position.Z - terrainVertices[x + 1 + y * WIDTH].Position.Z) / 2, 0, 1);
                    Vector3 normY = new Vector3(0, (terrainVertices[x + (y - 1) * WIDTH].Position.Z - terrainVertices[x + (y + 1) * WIDTH].Position.Z) / 2, 1);
                    terrainVertices[x + y * WIDTH].Normal = normX + normY;
                    terrainVertices[x + y * WIDTH].Normal.Normalize();

                    
                }
            }



            terrainVertexBuffer = new VertexBuffer(device, VertexMultitextured.SizeInBytes * WIDTH * HEIGHT, ResourceUsage.WriteOnly, ResourceManagementMode.Automatic);
            terrainVertexBuffer.SetData(terrainVertices);
        }

        private void SetUpTerrainterrainIndices()
        {
            int[] terrainIndices = new int[(WIDTH - 1) * (HEIGHT - 1) * 6];
            for (int x = 0; x < WIDTH - 1; x++)
            {
                for (int y = 0; y < HEIGHT - 1; y++)
                {
                    terrainIndices[(x + y * (WIDTH - 1)) * 6] = (x + 1) + (y + 1) * WIDTH;
                    terrainIndices[(x + y * (WIDTH - 1)) * 6 + 1] = (x + 1) + y * WIDTH;
                    terrainIndices[(x + y * (WIDTH - 1)) * 6 + 2] = x + y * WIDTH;

                    terrainIndices[(x + y * (WIDTH - 1)) * 6 + 3] = (x + 1) + (y + 1) * WIDTH;
                    terrainIndices[(x + y * (WIDTH - 1)) * 6 + 4] = x + y * WIDTH;
                    terrainIndices[(x + y * (WIDTH - 1)) * 6 + 5] = x + (y + 1) * WIDTH;
                }
            }

            terrainIndexBuffer = new IndexBuffer(device, typeof(int), (WIDTH - 1) * (HEIGHT - 1) * 6, ResourceUsage.WriteOnly, ResourceManagementMode.Automatic);
            terrainIndexBuffer.SetData(terrainIndices);
        }

        private void LoadHeightData()
        {
            float minimumHeight = 255;
            float maximumHeight = 0;

            WIDTH = heightMap.Width;
            HEIGHT = heightMap.Height;
            Color[] heightMapColors = new Color[WIDTH * HEIGHT];
            heightMap.GetData(heightMapColors);

            heightData = new float[WIDTH, HEIGHT];
            for (int x = 0; x < WIDTH; x++)
                for (int y = 0; y < HEIGHT; y++)
                {
                    heightData[x, y] = heightMapColors[y + x * HEIGHT].R;
                    if (heightData[x, y] < minimumHeight) minimumHeight = heightData[x, y];
                    if (heightData[x, y] > maximumHeight) maximumHeight = heightData[x, y];
                }

            for (int x = 0; x < WIDTH; x++)
                for (int y = 0; y < HEIGHT; y++)
                    heightData[x, y] = (heightData[x, y] - minimumHeight) / (maximumHeight - minimumHeight) * 30;
        }

        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                SetUpXNADevice();


                heightMap = content.Load<Texture2D>("heightmap");

                sandTexture = content.Load<Texture2D>("sand");
                grassTexture = content.Load<Texture2D>("grass");
                rockTexture = content.Load<Texture2D>("rock");
                snowTexture = content.Load<Texture2D>("snow");
            }
        }

        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent == true)
            {
                content.Unload();
            }
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            base.Update(gameTime);

            UpdateCameraMatrices();
            ProcessInput((float)gameTime.ElapsedGameTime.Milliseconds / 30.0f);
        }

        private void UpdateCameraMatrices()
        {
            Matrix cameraRotation = Matrix.CreateRotationX(cameraAngles.X) * Matrix.CreateRotationZ(cameraAngles.Z);

            Vector3 targetPos = cameraPosition + Vector3.Transform(new Vector3(0, 1, 0), cameraRotation);
            Vector3 upVector = Vector3.Transform(new Vector3(0, 0, 1), cameraRotation);

            viewMatrix = Matrix.CreateLookAt(cameraPosition, targetPos, upVector);
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)this.Window.ClientBounds.Width / (float)this.Window.ClientBounds.Height, 1.0f, 1000.0f);
        }

        protected override void Draw(GameTime gameTime)
        {
            device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DarkBlue, 1.0f, 0);

            effect.CurrentTechnique = effect.Techniques["TerrainMultiTexture"];
            effect.Parameters["sandTex_Tex"].SetValue(sandTexture);
            effect.Parameters["grassTex_Tex"].SetValue(grassTexture);
            effect.Parameters["rockTex_Tex"].SetValue(rockTexture);
            effect.Parameters["snowTex_Tex"].SetValue(snowTexture);

            Matrix worldMatrix = Matrix.Identity;
            effect.Parameters["matWorld"].SetValue(worldMatrix);
            effect.Parameters["matViewProjection"].SetValue(viewMatrix * projectionMatrix);

            effect.Parameters["start"].SetValue(start);
            effect.Parameters["upaxis"].SetValue(upaxis);
            effect.Parameters["textureComplexity"].SetValue(textureComplexity);
            effect.Parameters["weights"].SetValue(weights);
            effect.Parameters["colorMode"].SetValue(colorMode);

            effect.Begin();
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Begin();

                device.Vertices[0].SetSource(terrainVertexBuffer, 0, VertexMultitextured.SizeInBytes);
                device.Indices = terrainIndexBuffer;
                device.VertexDeclaration = new VertexDeclaration(device, VertexMultitextured.VertexElements);
                device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, WIDTH * HEIGHT, 0, (WIDTH - 1) * (HEIGHT - 1) * 2);

                pass.End();
            }
            effect.End();

            base.Draw(gameTime);
        }

        private void ProcessInput(float amountOfMovement)
        {
            Vector3 moveVector = new Vector3();

            KeyboardState keys = Keyboard.GetState();
            GamePadState gpad = GamePad.GetState(PlayerIndex.One);

            //if (keys.IsKeyDown(Keys.Right))
            moveVector.X += amountOfMovement * gpad.ThumbSticks.Right.X;
            //if (keys.IsKeyDown(Keys.Left))
            //    moveVector.X -= amountOfMovement;
            //if (keys.IsKeyDown(Keys.Down))
            //    moveVector.Y -= amountOfMovement;
            //if (keys.IsKeyDown(Keys.Up))
            moveVector.Y += amountOfMovement * gpad.ThumbSticks.Right.Y;

            Matrix cameraRotation = Matrix.CreateRotationX(cameraAngles.X) * Matrix.CreateRotationZ(cameraAngles.Z);
            cameraPosition += Vector3.Transform(moveVector, cameraRotation);

            MouseState currentMouseState = Mouse.GetState();
            //if (currentMouseState.X != previousMouseState.X)
            cameraAngles.Z -= amountOfMovement / 20.0f * gpad.ThumbSticks.Left.X;//(currentMouseState.X - previousMouseState.X);
            //if (currentMouseState.Y != previousMouseState.Y)
            cameraAngles.X += amountOfMovement / 20 * gpad.ThumbSticks.Left.Y; //(currentMouseState.Y - previousMouseState.Y);

            Mouse.SetPosition(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);
        }

        private void SetInitialValues()
        {
            Mouse.SetPosition(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);
            previousMouseState = Mouse.GetState();
        }
    }
}

  
Post a new reply
 





Google
 
Web www.riemers.net
If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !
- Website design & DirectX code : Riemer Grootjans -
©2006 Riemer Grootjans


News
Home
Forum
XNA 2.0 Recipes Book (8)
XNA 3.0 Recipes Book (8)
Downloads
Extra Reading (3)
Matrices: geometrical
Matrix Mathematics
Homogenous matrices
Community Projects (1)
Tutorials (160)
XNA 4.0 using C# (89)
DirectX using C# (54)
Series 1:Terrain (14)
Opening a window
Linking to the Device
Drawing a triangle
Camera
Rotation - Translation
Indices
Terrain creation
Terrain from file
DirectInput
Importing bmp files
Colored vertices
DirectX Light basics
Mesh creation
Mesh lighting
Series 2: Flightsim (19)
Series 3: HLSL (19)
Short Tuts (2)
Resizing problem
Checking Device caps
DirectX using C++ (15)
DirectX using VB (2)