本文整理汇总了C#中VertexBuffer.BufferData方法的典型用法代码示例。如果您正苦于以下问题:C# VertexBuffer.BufferData方法的具体用法?C# VertexBuffer.BufferData怎么用?C# VertexBuffer.BufferData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类VertexBuffer
的用法示例。
在下文中一共展示了VertexBuffer.BufferData方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InitializeGraphics
public virtual void InitializeGraphics()
{
if (heightmap == null && string.IsNullOrEmpty(Heightmap))
return;
if (heightmap == null)
owner.Resources.Load(Heightmap, out heightmap);
//if (heightmap.Width != heightmap.Height)
//throw new NotSupportedException("Must be rectangular heightmap");
int num_vertices = (heightmap.Width) * (heightmap.Height);
if (vb != null)
DoDispose();
vb = new VertexBuffer<VertexPositionTexCoordNormal>(VertexPositionTexCoordNormal.Descriptor);
vb.Allocate(num_vertices);
if (mat_name != null)
owner.Resources.Load(mat_name, out mat);
size = CellSize * heightmap.Width;
float s2 = size / 2;
int vi = 0;
float xx = 0;
float yy = -s2;
Vector3[,] norms = new Vector3[heightmap.Height, heightmap.Width];
for (int i = 0; i < heightmap.Height; i++)
{
for (int j = 0; j < heightmap.Width; j++)
{
Vector3 n = new Vector3();
n.x = (heightmap[i - 1, j] - heightmap[i + 1, j]) * Height;
n.y = (heightmap[i, j - 1] - heightmap[i, j + 1]) * Height;
n.z = 2.0f / (heightmap.Width + 1) / CellSize + 2.0f / (heightmap.Height + 1) / CellSize;
norms[i, j] = n.Normalize();
}
}
for (int i = 0; i < heightmap.Height; i++)
{
xx = -s2;
for (int j = 0; j < heightmap.Width; j++, vi++)
{
vb[vi] = new VertexPositionTexCoordNormal()
{
Position = new Vector3(xx, heightmap[i, j] * Height, yy),
Normal = norms[i, j]
};
xx += CellSize;
}
yy += CellSize;
}
float offset = s2 / 2;
int x_count = heightmap.Width / 2;
int y_count = heightmap.Height / 2;
var new_size = new Vector3(s2, size / 8, s2);
blocks = new TerrainBlock[4];
blocks[0] = new TerrainBlock(0, 0, x_count, y_count, new BoundingBox()
{
Size = new_size,
Position = new Vector3(-offset, 0, offset)
}, 0, this);
blocks[1] = new TerrainBlock(x_count-1, 0, x_count, y_count, new BoundingBox()
{
Size = new_size,
Position = new Vector3(offset, 0, offset)
}, 0, this);
blocks[2] = new TerrainBlock(x_count-1, y_count-1, x_count, y_count, new BoundingBox()
{
Size = new_size,
Position = new Vector3(offset, 0, -offset)
}, 0, this);
blocks[3] = new TerrainBlock(0, y_count-1, x_count, y_count, new BoundingBox()
{
Size = new_size,
Position = new Vector3(-offset, 0, -offset)
}, 0, this);
vb.BufferData(VboUsage.GL_STATIC_DRAW);
vb.FreeClientData();
init_finished = true;
}
示例2: Build
public override Model Build()
{
Model ret = new Model();
VertexBuffer<VertexPositionTexCoordNormal> vb = new VertexBuffer<VertexPositionTexCoordNormal>(VertexPositionTexCoordNormal.Descriptor);
//IndexBuffer<uint> ib = new IndexBuffer<uint>();
float ang_inc = (float)(2 * Math.PI) / sides;
int vsides = sides / 2 - 2;
float ang = 0;
vb.Allocate(sides * (sides / 2 - 2) + 2);
int index = 0;
for (int i = 0; i < sides; i++, ang += ang_inc)
{
float cs = (float)Math.Cos(ang);
float ss = (float)Math.Sin(ang);
float sang_inc = (float)Math.PI / vsides;
float sang = -((float)Math.PI / 2) + sang_inc;
for (int j = 0; j < vsides; j++, sang += sang_inc, ++index)
{
float vcs = (float)Math.Cos(sang);
float vss = (float)Math.Sin(sang);
Vector3 pos = new Vector3(
cs * radius * vcs,
vss * radius,
ss * radius * vss);
vb[index] = new VertexPositionTexCoordNormal()
{
Position = pos,
TexCoord = new Vector2(),
Normal = new Vector3(cs * vcs, vss, ss * vss)
};
}
vb[index] = new VertexPositionTexCoordNormal()
{
Position = new Vector3(0, -radius, 0),
Normal = new Vector3(0, -1, 0)
};
vb[index + 1] = new VertexPositionTexCoordNormal()
{
Position = new Vector3(0, radius, 0),
Normal = new Vector3(0, radius, 0)
};
}
ret.VertexBuffer = vb;
vb.BufferData(Glorg2.Graphics.OpenGL.VboUsage.GL_STATIC_DRAW);
return ret;
}
示例3: CreateBuffer
private IVertexBuffer CreateBuffer()
{
var result = new VertexBuffer<Vertex>(36, gl);
// Set up every face omn the cube
// Top
result[0] = new Vertex { Position = new Vector3f(-1, 1, 1), Normal = new Vector3f(0, 1, 0)};
result[1] = new Vertex { Position = new Vector3f( 1, 1, 1), Normal = new Vector3f(0, 1, 0) };
result[2] = new Vertex { Position = new Vector3f(-1, 1, -1), Normal = new Vector3f(0, 1, 0) };
result[3] = new Vertex { Position = new Vector3f(-1, 1, -1), Normal = new Vector3f(0, 1, 0) };
result[4] = new Vertex { Position = new Vector3f( 1, 1, 1), Normal = new Vector3f(0, 1, 0) };
result[5] = new Vertex { Position = new Vector3f( 1, 1, -1), Normal = new Vector3f(0, 1, 0) };
// Bottom
result[6] = new Vertex { Position = new Vector3f(-1, -1, 1), Normal = new Vector3f(0, -1, 0) };
result[7] = new Vertex { Position = new Vector3f(-1, -1, -1), Normal = new Vector3f(0, -1, 0) };
result[8] = new Vertex { Position = new Vector3f( 1, -1, 1), Normal = new Vector3f(0, -1, 0) };
result[9] = new Vertex { Position = new Vector3f(-1, -1, -1), Normal = new Vector3f(0, -1, 0) };
result[10] = new Vertex { Position = new Vector3f(1, -1, -1), Normal = new Vector3f(0, -1, 0) };
result[11] = new Vertex { Position = new Vector3f( 1, -1, 1), Normal = new Vector3f(0, -1, 0) };
// Left
result[12] = new Vertex { Position = new Vector3f(-1, 1, 1), Normal = new Vector3f(-1, 0, 0)};
result[13] = new Vertex { Position = new Vector3f(-1, 1, -1), Normal = new Vector3f(-1, 0, 0) };
result[14] = new Vertex { Position = new Vector3f(-1, -1, -1), Normal = new Vector3f(-1, 0, 0) };
result[15] = new Vertex { Position = new Vector3f(-1, 1, 1), Normal = new Vector3f(-1, 0, 0) };
result[16] = new Vertex { Position = new Vector3f(-1, -1, -1), Normal = new Vector3f(-1, 0, 0) };
result[17] = new Vertex { Position = new Vector3f(-1, -1, 1), Normal = new Vector3f(-1, 0, 0) };
// Right
result[18] = new Vertex { Position = new Vector3f(1, 1, 1), Normal = new Vector3f(1, 0, 0) };
result[19] = new Vertex { Position = new Vector3f(1, -1, -1), Normal = new Vector3f(1, 0, 0) };
result[20] = new Vertex { Position = new Vector3f(1, 1, -1), Normal = new Vector3f(1, 0, 0) };
result[21] = new Vertex { Position = new Vector3f(1, 1, 1), Normal = new Vector3f(1, 0, 0) };
result[22] = new Vertex { Position = new Vector3f(1, -1, 1), Normal = new Vector3f(1, 0, 0) };
result[23] = new Vertex { Position = new Vector3f(1, -1, -1), Normal = new Vector3f(1, 0, 0) };
// Front
result[24] = new Vertex {Position = new Vector3f(-1, 1, 1), Normal = new Vector3f(0, 0, 1)};
result[25] = new Vertex { Position = new Vector3f(1, -1, 1), Normal = new Vector3f(0, 0, 1) };
result[26] = new Vertex { Position = new Vector3f(1, 1, 1), Normal = new Vector3f(0, 0, 1) };
result[27] = new Vertex { Position = new Vector3f(-1, 1, 1), Normal = new Vector3f(0, 0, 1) };
result[29] = new Vertex { Position = new Vector3f(1, -1, 1), Normal = new Vector3f(0, 0, 1) };
result[28] = new Vertex { Position = new Vector3f(-1, -1, 1), Normal = new Vector3f(0, 0, 1) };
// Back
result[30] = new Vertex { Position = new Vector3f(-1, 1, -1), Normal = new Vector3f(0, 0, -1) };
result[32] = new Vertex { Position = new Vector3f(1, -1, -1), Normal = new Vector3f(0, 0, -1) };
result[31] = new Vertex { Position = new Vector3f(1, 1, -1), Normal = new Vector3f(0, 0, -1) };
result[33] = new Vertex { Position = new Vector3f(-1, 1, -1), Normal = new Vector3f(0, 0, -1) };
result[34] = new Vertex { Position = new Vector3f(1, -1, -1), Normal = new Vector3f(0, 0, -1) };
result[35] = new Vertex { Position = new Vector3f(-1, -1, -1), Normal = new Vector3f(0, 0, -1) };
using (result.Bind())
{
result.BufferData(BufferUsage.StaticDraw);
}
return result;
}
示例4: InitializeGraphics
public void InitializeGraphics()
{
if (vb != null)
vb.Dispose();
if (mat == null)
{
Glorg2.Resource.MaterialImporter imp = new Glorg2.Resource.MaterialImporter();
using(var stream = System.IO.File.OpenRead(".\\shaders\\Grid.mxl"))
{
mat = imp.Import<StdMaterial>(stream, "grid", null);
}
}
vb = new VertexBuffer<WireframeVertex>(WireframeVertex.Description);
vb.Allocate(Columns * 2 + Rows * 2 + 4);
float tot_w = Size * Columns;
float tot_h = Size * Rows;
float fi = -tot_w / 2;
int i;
for (i = 0; i <= Columns; i++, fi += Size)
{
Vector4 color;
if(Major == 0 || (i % Major) == 0)
color = MajorColor;
else
color = MinorColor;
vb[i * 2] = new WireframeVertex() { Position = new Vector3(fi, 0, -tot_h / 2), Color = color };
vb[i * 2 + 1] = new WireframeVertex() { Position = new Vector3(fi, 0, tot_h / 2), Color = color };
}
int start = i * 2;
fi = -tot_h / 2;
for (int j = 0; j <= Rows; j++, fi += Size)
{
Vector4 color;
if (Major == 0 || (j % Major) == 0)
color = MajorColor;
else
color = MinorColor;
vb[start + j * 2] = new WireframeVertex() { Position = new Vector3(-tot_h / 2, 0, fi), Color = color };
vb[start + j * 2 + 1] = new WireframeVertex() { Position = new Vector3( tot_h / 2, 0, fi), Color = color };
}
vb.BufferData(VboUsage.GL_STATIC_DRAW);
vb.FreeClientData();
}
示例5: Build
protected override void Build()
{
vb = new VertexBuffer<Vector3>(Vector3.Descriptor);
vb.Allocate(8);
ib = new IndexBuffer<ushort>();
ib.Allocate(4 * 6);
vb[0] = new Vector3(-0.5f, -0.5f, -0.5f);
vb[1] = new Vector3(0.5f, -0.5f, -0.5f);
vb[2] = new Vector3(0.5f, 0.5f, -0.5f);
vb[3] = new Vector3(-0.5f, 0.5f, -0.5f);
vb[4] = new Vector3(-0.5f, -0.5f, 0.5f);
vb[5] = new Vector3(0.5f, -0.5f, 0.5f);
vb[6] = new Vector3(0.5f, 0.5f, 0.5f);
vb[7] = new Vector3(-0.5f, 0.5f, 0.5f);
ib[0] = 0;
ib[1] = 1;
ib[2] = 2;
ib[3] = 3;
ib[4] = 4;
ib[5] = 0;
ib[6] = 3;
ib[7] = 7;
ib[8] = 5;
ib[9] = 4;
ib[10] = 7;
ib[11] = 6;
ib[12] = 1;
ib[13] = 5;
ib[14] = 6;
ib[15] = 2;
ib[16] = 6;
ib[17] = 7;
ib[18] = 3;
ib[19] = 2;
ib[20] = 0;
ib[21] = 4;
ib[22] = 5;
ib[23] = 1;
vb.BufferData(VboUsage.GL_STATIC_DRAW);
ib.BufferData(VboUsage.GL_STATIC_DRAW);
// Client data is no longer needed. Free it!
vb.FreeClientData();
ib.FreeClientData();
}
示例6: Build
public static Terrain Build(IOpenGL33 gl, int columns, int rows, float width, float depth)
{
var noise = new PerlinNoise();
var noise2 = new PerlinNoise();
var vertexBuffer = new VertexBuffer<PositionNormalTexCoord>((columns + 1) * (rows + 1), gl);
var indexBuffer = new ElementBuffer<uint>(columns * rows * 6 , gl);
float fx = - width / 2;
float fy = -depth / 2;
float dx = width / columns;
float dy = depth / rows;
int i = 0;
for (int y = 0; y < (rows + 1); y++)
{
for (int x = 0; x < (columns + 1); x++, i++)
{
float ix = fx + x * dx;
float iy = fy + y * dy;
const float detail = 0.25f;
const float detailScale = 0.2f;
const float macro = 0.008f;
const float macroScale = 4f;
var fUp = (float)((noise.Noise(x * detail, 0, (y + 1) * detail) - noise2.Noise(x * detail, 0, (y + 1) * detail) * 0.25f) * detailScale + noise.Noise(x * macro, 0, (y + 1) * macro) * macroScale) ;
var fDown = (float)((noise.Noise(x * detail, 0, (y - 1) * detail) - noise2.Noise(x * detail, 0, (y - 1) * detail) * 0.25f) * detailScale + noise.Noise(x * macro, 0, (y - 1) * macro) * macroScale) ;
var fLeft = (float)((noise.Noise((x - 1) * detail, 0, y * detail) - noise2.Noise((x - 1) * detail, 0, y * detail) * 0.25f) * detailScale + noise.Noise((x - 1) * macro, 0, y * macro) * macroScale);
var fRight = (float)((noise.Noise((x + 1) * detail, 0, y * detail) - noise2.Noise((x + 1) * detail, 0, y * detail) * 0.25f) * detailScale + noise.Noise((x + 1) * macro, 0, y * macro) * macroScale);
var fThis = (float)((noise.Noise(x * detail, 0, y * detail) - noise2.Noise(x * detail, 0, y * detail) * 0.25f) * detailScale + noise.Noise(x * macro, 0, y * macro) * macroScale);
var vUp = new Vector3f(ix, fUp, iy - dy);
var vDown = new Vector3f(ix, fDown, iy + dy);
var vLeft = new Vector3f(ix - 1, fLeft, iy);
var vRight = new Vector3f(ix + 1, fRight, iy);
var vThis = new Vector3f(ix, fThis, iy);
var upperLeft = CalculateNormal(vLeft, vUp, vThis);
var upperRight = CalculateNormal(vUp, vRight, vThis);
var lowerRight = CalculateNormal(vRight, vDown, vThis);
var lowerLeft = CalculateNormal(vDown, vLeft, vThis);
var normal = upperLeft + upperRight + lowerLeft + lowerRight;
normal = new Vector3f(normal.X * 0.25f, normal.Y * 0.25f, normal.Z * 0.25f);
vertexBuffer[i] = new PositionNormalTexCoord { Normal = normal.Normalize(), Position = vThis, TexCoord = new Vector2f(vThis.X, vThis.Z)};
}
}
int offset = 0;
foreach(var index in Enumerable.Range(0, columns * rows + columns).Where(index => (index % (columns + 1)) != columns))
{
indexBuffer[offset] = (uint)index;
indexBuffer[offset + 1] = (uint)(index + (columns + 1) + 1);
indexBuffer[offset + 2] = (uint)index + 1;
indexBuffer[offset + 3] = (uint)index;
indexBuffer[offset + 4] = (uint)(index + (columns + 1));
indexBuffer[offset + 5] = (uint)(index + (columns + 1) + 1);
offset += 6;
}
using(vertexBuffer.Bind())
vertexBuffer.BufferData(BufferUsage.StaticDraw);
using(indexBuffer.Bind())
indexBuffer.BufferData(BufferUsage.StaticDraw);
var vertexArray = new VertexArray(gl, new[] {vertexBuffer}, new[] {PositionNormalTexCoord.Descriptor});
var shader = new TerrainShader(gl);
return new Terrain(gl, vertexArray, vertexBuffer, indexBuffer, shader);
}