際際滷

際際滷Share a Scribd company logo
2D To 3D Terrain GeneratorMehmet Ayd脹n Bahad脹rMuhammed Fatih PolatAhmet Ta巽脹
XNA Nedir?2004  San Joserenciler, Hobi.NET altyap脹s脹C# dili DirectXWindows, Xbox 360
XNA Nedir? - 2http://creators.xna.com/en-US/Raven XNA http://www.codeplex.com/RavenXNAReactor 3D http://www.reactor3d.com/site/Carot http://www.carotengine.com/Physic http://www.codeplex.com/FarseerPhysics
Hadi Balayal脹mMicrosoft XNA Game Studio 3.1Visual Studio 2008 SP1.NET Framework 2.0 RuntimeXNA Framework RuntimeDirectx SDKDirectx Runtime
2D to 3D
Effect Dosyas脹 ?XNA  巽genler, 端巽genler, 端巽genler巽geni nas脹l g旦stereceim?.fxEffect effecteffect = Content.Load<Effect> ("effects");
Indisleme
Basit bir terrain?4x3l端k bir matris.private float[,] heightData;private void LoadHeightData()private void SetUpVertices()private void SetUpIndices()vertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x,y], -y);
Basit bir Terrain?
Dosyadan Terrain (?)Grayscale resimlerEn y端ksee beyazlar! Siyahlar inin aa脹ya!Oku bakal脹m: Texture2D heightMap = Content.Load<Texture2D> ("heightmap");LoadHeightData(heightMap);Content  bmp, jpg, png ...
Okudum!private void LoadHeightData(Texture2D heightMap){terrainWidth = heightMap.Width;terrainHeight = heightMap.Height;Color[] heightMapColors = new Color[terrainWidth * terrainHeight];heightMap.GetData(heightMapColors);heightData = new float[terrainWidth, terrainHeight];for (int x = 0; x < terrainWidth; x++)for (int y = 0; y < terrainHeight; y++)heightData[x, y] = heightMapColors[x + y * terrainWidth].R / 5.0f;}
Ne oldu imdi?Her bir nokta i巽in y端kseklik bilgisi var.SetUpIndice  her noktan脹n array脹 i巽in vertex.SetUpIndices  Terraini kurmak i巽in her 端巽gene 3 indis.Draw  indisler dizileri izin verdik巽e 端巽genleri renderlemeye devam et.
Ne oldu imdi? -2
Renksiz ?
Kar脹t脹r Kar脹t脹r Kar脹t脹rvertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x, y], -y);if (heightData[x, y] < minHeight + (maxHeight - minHeight) / 4)vertices[x + y * terrainWidth].Color = Color.Blue;else if (heightData[x, y] < minHeight + (maxHeight - minHeight) * 2 / 4)vertices[x + y * terrainWidth].Color = Color.Green;else if (heightData[x, y] < minHeight + (maxHeight - minHeight) * 3 / 4)vertices[x + y * terrainWidth].Color = Color.Brown;elsevertices[x + y * terrainWidth].Color = Color.White;
Kar脹t脹r ?
K旦r oldum I脹k verelim:
I脹k?T端m vertexlerin normalizesini istiyorum.Bu verileri vertex datas脹na iletmeliyimBir normalize, bir renk ve bir pozisyon XNA i巽in fazla!Fakat bu sorun olmasa gerek?
I脹k - 2public structVertexPositionNormalColored{public Vector3 Position;public ColorColor;public Vector3 Normal;public static intSizeInBytes = 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 ),};}
I脹k  Sonu巽?
Terrainim I脹k 聴ster!private void CalculateNormals(){for (inti = 0; i < vertices.Length; i++)vertices[i].Normal = new Vector3(0, 0, 0);}for (inti = 0; i < indices.Length / 3; i++){int index1 = indices[i * 3];int index2 = indices[i * 3 + 1];int index3 = indices[i * 3 + 2];Vector3 side1 = vertices[index1].Position - vertices[index3].Position;Vector3 side2 = vertices[index1].Position - vertices[index2].Position;Vector3 normal = Vector3.Cross(side1, side2);vertices[index1].Normal += normal;vertices[index2].Normal += normal;vertices[index3].Normal += normal;}
Terrainim I脹k 聴ster - 2for (inti = 0; i < vertices.Length; i++)vertices[i].Normal.Normalize();Load Contentte unutmay脹ver unu yazmay脹:CalculateNormals();Okus pokus desin 脹脹klar terrainime!Vector3 lightDirection = new Vector3(1.0f, -1.0f, -1.0f);lightDirection.Normalize();effect.Parameters["xLightDirection"].SetValue(lightDirection);effect.Parameters["xAmbient"].SetValue(0.1f);
Terrainim I脹k 聴ster - 3
Eee Optimizasyonu Nerde Bunun?Optimizasyona birebir: Vertexbuffer, Indexbuffer! 聴la巽 gibi keser.Neden? B脹rakal脹m da ekran kart脹 biraz ekerleme yapabilsin!Vertexlerin IndexBuffera gidecei yok ya!
Neymi kodlar脹?VertexBuffermyVertexBuffer;IndexBuffermyIndexBuffer;private void CopyToBuffers(){myVertexBuffer = new VertexBuffer(device, vertices.Length * VertexPositionNormalColored.SizeInBytes, BufferUsage.WriteOnly);myVertexBuffer.SetData(vertices);}Yazmay脹 unutmamal脹 Load Content al脹n脹r sonra: CopyToBuffers();
Ne ekledik?Biraz ho g旦r端ns端n istedik, g端ne ekledik, hava ekledik, su ve sis eklemeye 巽al脹t脹k.Heightmap d端zenleme ara巽lar脹 ekledik, colormap eklemeye 巽al脹t脹k pek de baar脹l脹 olamad脹k.Kamera kontrol kodlar脹 巽arpt脹k sadan soldan hemen ekledik
Ne ekledik? - 2Y端kseklii, 脹脹脹, g端nei, g旦ky端z端n端 kullan脹c脹n脹n emrine verdik!Boyama arac脹 yapmaya 巽al脹t脹k elimize y端z端m端ze bulat脹rd脹k Kullan脹c脹ya farkl脹 doku g旦r端n端mleri sunduk (Geometrik ve Doku olarak)Tasar脹m脹 kaydetme, tasar脹m a巽ma se巽enekleri ekledik!Bu kadar yeter dedik
Demo
Teekk端rlerMehmet Ayd脹n Bahad脹rMuhammed Fatih PolatAhmet Ta巽脹

More Related Content

2 D To 3 D Terrain Generator

  • 1. 2D To 3D Terrain GeneratorMehmet Ayd脹n Bahad脹rMuhammed Fatih PolatAhmet Ta巽脹
  • 2. XNA Nedir?2004 San Joserenciler, Hobi.NET altyap脹s脹C# dili DirectXWindows, Xbox 360
  • 3. XNA Nedir? - 2http://creators.xna.com/en-US/Raven XNA http://www.codeplex.com/RavenXNAReactor 3D http://www.reactor3d.com/site/Carot http://www.carotengine.com/Physic http://www.codeplex.com/FarseerPhysics
  • 4. Hadi Balayal脹mMicrosoft XNA Game Studio 3.1Visual Studio 2008 SP1.NET Framework 2.0 RuntimeXNA Framework RuntimeDirectx SDKDirectx Runtime
  • 6. Effect Dosyas脹 ?XNA 巽genler, 端巽genler, 端巽genler巽geni nas脹l g旦stereceim?.fxEffect effecteffect = Content.Load<Effect> ("effects");
  • 8. Basit bir terrain?4x3l端k bir matris.private float[,] heightData;private void LoadHeightData()private void SetUpVertices()private void SetUpIndices()vertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x,y], -y);
  • 10. Dosyadan Terrain (?)Grayscale resimlerEn y端ksee beyazlar! Siyahlar inin aa脹ya!Oku bakal脹m: Texture2D heightMap = Content.Load<Texture2D> ("heightmap");LoadHeightData(heightMap);Content bmp, jpg, png ...
  • 11. Okudum!private void LoadHeightData(Texture2D heightMap){terrainWidth = heightMap.Width;terrainHeight = heightMap.Height;Color[] heightMapColors = new Color[terrainWidth * terrainHeight];heightMap.GetData(heightMapColors);heightData = new float[terrainWidth, terrainHeight];for (int x = 0; x < terrainWidth; x++)for (int y = 0; y < terrainHeight; y++)heightData[x, y] = heightMapColors[x + y * terrainWidth].R / 5.0f;}
  • 12. Ne oldu imdi?Her bir nokta i巽in y端kseklik bilgisi var.SetUpIndice her noktan脹n array脹 i巽in vertex.SetUpIndices Terraini kurmak i巽in her 端巽gene 3 indis.Draw indisler dizileri izin verdik巽e 端巽genleri renderlemeye devam et.
  • 15. Kar脹t脹r Kar脹t脹r Kar脹t脹rvertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x, y], -y);if (heightData[x, y] < minHeight + (maxHeight - minHeight) / 4)vertices[x + y * terrainWidth].Color = Color.Blue;else if (heightData[x, y] < minHeight + (maxHeight - minHeight) * 2 / 4)vertices[x + y * terrainWidth].Color = Color.Green;else if (heightData[x, y] < minHeight + (maxHeight - minHeight) * 3 / 4)vertices[x + y * terrainWidth].Color = Color.Brown;elsevertices[x + y * terrainWidth].Color = Color.White;
  • 17. K旦r oldum I脹k verelim:
  • 18. I脹k?T端m vertexlerin normalizesini istiyorum.Bu verileri vertex datas脹na iletmeliyimBir normalize, bir renk ve bir pozisyon XNA i巽in fazla!Fakat bu sorun olmasa gerek?
  • 19. I脹k - 2public structVertexPositionNormalColored{public Vector3 Position;public ColorColor;public Vector3 Normal;public static intSizeInBytes = 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 ),};}
  • 21. Terrainim I脹k 聴ster!private void CalculateNormals(){for (inti = 0; i < vertices.Length; i++)vertices[i].Normal = new Vector3(0, 0, 0);}for (inti = 0; i < indices.Length / 3; i++){int index1 = indices[i * 3];int index2 = indices[i * 3 + 1];int index3 = indices[i * 3 + 2];Vector3 side1 = vertices[index1].Position - vertices[index3].Position;Vector3 side2 = vertices[index1].Position - vertices[index2].Position;Vector3 normal = Vector3.Cross(side1, side2);vertices[index1].Normal += normal;vertices[index2].Normal += normal;vertices[index3].Normal += normal;}
  • 22. Terrainim I脹k 聴ster - 2for (inti = 0; i < vertices.Length; i++)vertices[i].Normal.Normalize();Load Contentte unutmay脹ver unu yazmay脹:CalculateNormals();Okus pokus desin 脹脹klar terrainime!Vector3 lightDirection = new Vector3(1.0f, -1.0f, -1.0f);lightDirection.Normalize();effect.Parameters["xLightDirection"].SetValue(lightDirection);effect.Parameters["xAmbient"].SetValue(0.1f);
  • 24. Eee Optimizasyonu Nerde Bunun?Optimizasyona birebir: Vertexbuffer, Indexbuffer! 聴la巽 gibi keser.Neden? B脹rakal脹m da ekran kart脹 biraz ekerleme yapabilsin!Vertexlerin IndexBuffera gidecei yok ya!
  • 25. Neymi kodlar脹?VertexBuffermyVertexBuffer;IndexBuffermyIndexBuffer;private void CopyToBuffers(){myVertexBuffer = new VertexBuffer(device, vertices.Length * VertexPositionNormalColored.SizeInBytes, BufferUsage.WriteOnly);myVertexBuffer.SetData(vertices);}Yazmay脹 unutmamal脹 Load Content al脹n脹r sonra: CopyToBuffers();
  • 26. Ne ekledik?Biraz ho g旦r端ns端n istedik, g端ne ekledik, hava ekledik, su ve sis eklemeye 巽al脹t脹k.Heightmap d端zenleme ara巽lar脹 ekledik, colormap eklemeye 巽al脹t脹k pek de baar脹l脹 olamad脹k.Kamera kontrol kodlar脹 巽arpt脹k sadan soldan hemen ekledik
  • 27. Ne ekledik? - 2Y端kseklii, 脹脹脹, g端nei, g旦ky端z端n端 kullan脹c脹n脹n emrine verdik!Boyama arac脹 yapmaya 巽al脹t脹k elimize y端z端m端ze bulat脹rd脹k Kullan脹c脹ya farkl脹 doku g旦r端n端mleri sunduk (Geometrik ve Doku olarak)Tasar脹m脹 kaydetme, tasar脹m a巽ma se巽enekleri ekledik!Bu kadar yeter dedik
  • 28. Demo