Skip to content

Instantly share code, notes, and snippets.

@TheSpydog
Created April 26, 2020 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheSpydog/9960f485884a87c9c1cb5e51815b8252 to your computer and use it in GitHub Desktop.
Save TheSpydog/9960f485884a87c9c1cb5e51815b8252 to your computer and use it in GitHub Desktop.
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace VertexSamplerTest
{
public class Program
{
public static void Main(string[] args)
{
using (MyGame g = new MyGame())
{
g.Run();
}
}
}
struct VertexPosition2 : IVertexType
{
public Vector2 Position;
public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration(
new VertexElement(0, VertexElementFormat.Vector2, VertexElementUsage.Position, 0));
VertexDeclaration IVertexType.VertexDeclaration
{
get { return VertexDeclaration; }
}
}
public class MyGame : Game
{
GraphicsDeviceManager gdm;
Texture2D heightMapTexture;
Matrix viewMatrix;
Matrix projectionMatrix;
Effect effect;
VertexBuffer vertexBuffer;
IndexBuffer indexBuffer;
int numVertices;
int numIndices;
public MyGame()
{
gdm = new GraphicsDeviceManager(this);
}
override protected void Initialize()
{
const int heightMapSize = 64;
heightMapTexture = new Texture2D(GraphicsDevice, heightMapSize, heightMapSize, false, SurfaceFormat.Single);
var heightMapData = new float[heightMapSize * heightMapSize];
for (var y = 0; y < heightMapSize; y++)
for (var x = 0; x < heightMapSize; x++)
heightMapData[(y * heightMapSize) + x] = (float) Math.Sin(x / 2.0f) + (float) Math.Sin(y / 3.0f);
heightMapTexture.SetData(heightMapData);
viewMatrix = Matrix.CreateLookAt(new Vector3(32, 10, 60), new Vector3(32, 0, 30), Vector3.Up);
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
GraphicsDevice.Viewport.AspectRatio, 1.0f, 100.0f);
effect = Content.Load<Effect>("VertexTextureEffect");
effect.Parameters["WorldViewProj"].SetValue(viewMatrix * projectionMatrix);
effect.Parameters["HeightMapTexture"].SetValue(heightMapTexture);
effect.Parameters["HeightMapSize"].SetValue((float) heightMapSize);
numVertices = heightMapSize * heightMapSize;
vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPosition2), numVertices, BufferUsage.WriteOnly);
var vertices = new VertexPosition2[numVertices];
for (var y = 0; y < heightMapSize; y++)
for (var x = 0; x < heightMapSize; x++)
vertices[(y * heightMapSize) + x] = new VertexPosition2 { Position = new Vector2(x, y) };
vertexBuffer.SetData(vertices);
GraphicsDevice.SetVertexBuffer(vertexBuffer);
numIndices = (heightMapSize - 1) * (heightMapSize - 1) * 2 * 3;
indexBuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, numIndices, BufferUsage.WriteOnly);
var indexData = new short[numIndices];
var indexIndex = 0;
for (short y = 0; y < heightMapSize - 1; y++)
for (short x = 0; x < heightMapSize - 1; x++)
{
var baseIndex = (short) ((y * heightMapSize) + x);
indexData[indexIndex++] = baseIndex;
indexData[indexIndex++] = (short) (baseIndex + heightMapSize);
indexData[indexIndex++] = (short) (baseIndex + 1);
indexData[indexIndex++] = (short) (baseIndex + 1);
indexData[indexIndex++] = (short) (baseIndex + heightMapSize);
indexData[indexIndex++] = (short) (baseIndex + heightMapSize + 1);
}
indexBuffer.SetData(indexData);
GraphicsDevice.Indices = indexBuffer;
GraphicsDevice.RasterizerState = RasterizerState.CullNone;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
effect.CurrentTechnique.Passes[0].Apply();
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
0, 0, numVertices, 0, numIndices / 3);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment