The IoT market is growing at a rapid pace and manufacturers are under pressure to quickly provide new products. Can you be sure that your devices do what they're supposed to do, while still being secure?
Become a Premium Member and unlock a new, free course in leading technologies each month.
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
/// <summary>
/// Calculate and Initialize Vertex Data
/// </summary>
private void InitVertex()
{
Cnt = FX0.Count;
VertexData = new List<CustomVertex.PositionNormalTextured>();
for (i = 0; i < Cnt - 1; i++)
{
//Calculate and fill into Veretex Data
}
Type VBType = typeof(CustomVertex.PositionNormalTextured);
VertexFormats VBFormats = CustomVertex.PositionNormalTextured.Format;
Usage VBUsage = Usage.WriteOnly | Usage.Dynamic; //Dynamic Vertex Buffer
Pool Pl = Pool.Default;
VBuffer = new VertexBuffer(VBType,
VertexData.Length,
GDevice,
VBUsage,
VBFormats,
Pl);
VBuffer.SetData(VertexData.ToArray(), 0, LockFlags.None);
}
//Here I`m just duplicating the entries. The appended entries will be offset in the final code
private void Add_Click(object sender, EventArgs e)
{
//How do I Lock, Copy and Unlock. I`m lost here.
VertexData.AddRange(VertexData);
Render();
}
/// <summary>
/// Render
/// </summary>
private void Render()
{
GDevice.Clear(ClearFlags.Target, System.Drawing.Color.FromArgb(0, 0, 0).ToArgb(), 1.0f, 0);
GDevice.BeginScene();
InitDevice();
GDevice.VertexFormat = CustomVertex.PositionNormalTextured.Format;
GDevice.SetStreamSource(0, VFiberBuffer, 0);
GDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, (VertexData.Count / 3));
GDevice.EndScene();
GDevice.Present();
}
Assuming vertices 0-99 describe a model, and vertices 100-199 would describe the same model, but rotated 5 degrees. The fastest way would normally be to draw vertices 0-99, set the world transform to rotate 5 degrees and draw vertices 0-99 again. You don't need vertices 100-199 at all.
GDevice.Clear(ClearFlags.Target, System.Drawing.Color.FromArgb(0, 0, 0).ToArgb(), 1.0f, 0);
GDevice.BeginScene();
GDevice.VertexFormat = CustomVertex.PositionNormalTextured.Format;
GDevice.SetStreamSource(0, VFiberBuffer, 0);
GDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, (VertexData.Count / 3));
GDevice.SetTransform(D3DTS_WORLD, &mtxObject);
GDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, (VertexData.Count / 3));
GDevice.EndScene();
GDevice.Present();
mtxObject is a D3DMATRIX that transforms the model in some way; rotate, translate or scale. You can repeat the set matrix/draw model process as often as you like. The matrix can be obtained by any means, for example you could put that code in a loop and recalculate a rotation matrix each time around the loop. VertexBuffer.SetData(NewVertexData.ToArray(), 0, LockFlags.NoOverwrite);
/// <summary>
/// Initialize
/// Set the VetrexBuffer and the IndexBuffer
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Initialize_Click(object sender, EventArgs e)
{
Type VBType = typeof(CustomVertex.PositionNormalTextured);
VertexFormats VBFormats = CustomVertex.PositionNormalTextured.Format;
Usage VBUsage = Usage.WriteOnly | Usage.SoftwareProcessing;
Pool Pl = Pool.Managed;
//Usage VBUsage = Usage.WriteOnly | Usage.Dynamic;
//Pool Pl = Pool.Default;
CVLength = FX0.Count * 3 * 2 * 2; //FX$, FY$ and FZ$ contain the X, Y, Z Values. CVLength will be the number of vertices for a single pass.
VXtrBuffer = new VertexBuffer(VBType,
CVLength * 50, //Have enough for 50 passes, same with index buffer
GDevice,
VBUsage,
VBFormats,
Pl);
IXtrBuffer = new IndexBuffer(GDevice, sizeof(ushort) * CVLength * 50, VBUsage, Pl, true);
InitXtr(); //Xtr
}
/// <summary>
/// Add Pass
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Add_Click(object sender, EventArgs e)
{
AddPass();
}
/// <summary>
/// Add Pass.. See comments within code
/// This works for Pass 1, subsequent passes don`t seem to work.
/// What I`m trying to do, is draw subsequent passes offset by an angle.
/// </summary>
private void AddPass()
{
int Cnt = 0;
int i = 0;
int Ind = 0;
Vector3 V0, V1, V2, V3;
Vector3 N1, N2;
float VertX0 = 0, VertY0 = 0, VertZ0 = 0;
float VertX1 = 0, VertY1 = 0, VertZ1 = 0;
float VertX2 = 0, VertY2 = 0, VertZ2 = 0;
float VertX3 = 0, VertY3 = 0, VertZ3 = 0;
//FX$,FY$, FZ$ are the X, Y, Z coordinates
Cnt = FX0.Count;
//An array to store the vertices for a single Pass
XtrTexture = new CustomVertex.PositionNormalTextured[CVLength];
//For every call of the function, the angle is incremented by 5.
//This means the same pattern is offset by Ang everytime the function is called i,e, every Pass
Ang = Ang + 5;
//This is the main code which generates the pattern to be dispalyed.
//This works good. This is not the most efficient way to do this, but for the sake of simplicity, I`ll go with this for now.
for (i = 0; i < Cnt - 1; i++)
{
//Triangle 1 and 2 [Top]
VertX0 = (float)(FX0[i] * Math.Cos(Ang) - FY0[i] * Math.Sin(Ang));
VertY0 = (float)(FX0[i] * Math.Sin(Ang) + FY0[i] * Math.Cos(Ang));
VertZ0 = (float)(FZ0[i]);
V0 = new Vector3(VertX0, VertY0, VertZ0);
VertX1 = (float)(FX0[i + 1] * Math.Cos(Ang) - FY0[i + 1] * Math.Sin(Ang));
VertY1 = (float)(FX0[i + 1] * Math.Sin(Ang) + FY0[i + 1] * Math.Cos(Ang));
VertZ1 = (float)(FZ0[i + 1]);
V1 = new Vector3(VertX1, VertY1, VertZ1);
VertX2 = (float)(FX1[i + 1] * Math.Cos(Ang) - FY1[i + 1] * Math.Sin(Ang));
VertY2 = (float)(FX1[i + 1] * Math.Sin(Ang) + FY1[i + 1] * Math.Cos(Ang));
VertZ2 = (float)(FZ1[i + 1]);
V2 = new Vector3(VertX2, VertY2, VertZ2);
VertX3 = (float)(FX1[i] * Math.Cos(Ang) - FY1[i] * Math.Sin(Ang));
VertY3 = (float)(FX1[i] * Math.Sin(Ang) + FY1[i] * Math.Cos(Ang));
VertZ3 = (float)(FZ1[i]);
V3 = new Vector3(VertX3, VertY3, VertZ3);
N1 = CalcNormal(V0, V1, V2);
N2 = CalcNormal(V2, V3, V0);
XtrTexture[Ind + 0] = new CustomVertex.PositionNormalTextured(V0, N1, 0f, .5f);
XtrTexture[Ind + 1] = new CustomVertex.PositionNormalTextured(V1, N1, 1.0f, 0.5f);
XtrTexture[Ind + 2] = new CustomVertex.PositionNormalTextured(V2, N1, 1.0f, 1.0f);
XtrTexture[Ind + 3] = new CustomVertex.PositionNormalTextured(V2, N2, 1.0f, 1.0f);
XtrTexture[Ind + 4] = new CustomVertex.PositionNormalTextured(V3, N2, 0.0f, 1.0f);
XtrTexture[Ind + 5] = new CustomVertex.PositionNormalTextured(V0, N2, 0f, 0.5f);
//Triangle 3 and 4 [Bottom]
VertX0 = (float)(FX2[i] * Math.Cos(Ang) - FY2[i] * Math.Sin(Ang));
VertY0 = (float)(FX2[i] * Math.Sin(Ang) + FY2[i] * Math.Cos(Ang));
VertZ0 = (float)(FZ2[i]);
V0 = new Vector3(VertX0, VertY0, VertZ0);
VertX1 = (float)(FX2[i + 1] * Math.Cos(Ang) - FY2[i + 1] * Math.Sin(Ang));
VertY1 = (float)(FX2[i + 1] * Math.Sin(Ang) + FY2[i + 1] * Math.Cos(Ang));
VertZ1 = (float)(FZ2[i + 1]);
V1 = new Vector3(VertX1, VertY1, VertZ1);
VertX2 = (float)(FX0[i + 1] * Math.Cos(Ang) - FY0[i + 1] * Math.Sin(Ang));
VertY2 = (float)(FX0[i + 1] * Math.Sin(Ang) + FY0[i + 1] * Math.Cos(Ang));
VertZ2 = (float)(FZ0[i + 1]);
V2 = new Vector3(VertX2, VertY2, VertZ2);
VertX3 = (float)(FX0[i] * Math.Cos(Ang) - FY0[i] * Math.Sin(Ang));
VertY3 = (float)(FX0[i] * Math.Sin(Ang) + FY0[i] * Math.Cos(Ang));
VertZ3 = (float)(FZ0[i]);
V3 = new Vector3(VertX3, VertY3, VertZ3);
N1 = CalcNormal(V0, V1, V2);
N2 = CalcNormal(V2, V3, V0);
XtrTexture[Ind + 6] = new CustomVertex.PositionNormalTextured(V0, N1, 0.0f, 0.0f);
XtrTexture[Ind + 7] = new CustomVertex.PositionNormalTextured(V1, N1, 1.0f, 0f);
XtrTexture[Ind + 8] = new CustomVertex.PositionNormalTextured(V2, N1, 1.0f, 0.5f);
XtrTexture[Ind + 9] = new CustomVertex.PositionNormalTextured(V2, N2, 1.0f, 0.5f);
XtrTexture[Ind + 10] = new CustomVertex.PositionNormalTextured(V3, N2, 0.0f, 0.5f);
XtrTexture[Ind + 11] = new CustomVertex.PositionNormalTextured(V0, N2, 0.0f, 0.0f);
Ind += 12;
}
//Question
//I want to update the index buffer, I`m not sure if this is the correct way of doing this.
//1st Pass I`m locking and updating from 0 to CVLength
//2nd Pass I`m locking from the CVLength to 2 * CVLength [since the number of indices are doubled, only update the new vertices?]
//3rd Pass.....nth Pass
using (GStream = IXtrBuffer.Lock((Pass * CVLength), ((Pass * CVLength) + CVLength) * sizeof(ushort), LockFlags.None))
{
for (int j = (Pass * CVLength); j < (Pass * CVLength) + CVLength; j += 12)
{
ushort[] Indices = {
(ushort)(j + 0),
(ushort)(j + 1 ),
(ushort)(j + 2),
(ushort)(j + 3),
(ushort)(j + 4),
(ushort)(j + 5),
(ushort)(j + 6),
(ushort)(j + 7),
(ushort)(j + 8),
(ushort)(j + 9),
(ushort)(j + 10),
(ushort)(j + 11)
};
GStream.Write(Indices);
}
IXtrBuffer.Unlock();
}
//Question
//Just like the index buffer, updating the new Vertices here
//Is VXtrBuffer appended with new vertices for each pass (what I`m trying to do) or VXtrbuffer gets reset and get new set of vertices?
GStream = VXtrBuffer.Lock((Pass * CVLength), ((Pass * CVLength) + CVLength) * 32, LockFlags.None);
GStream.Write(XtrTexture);
VXtrBuffer.Unlock();
GDevice.Clear(ClearFlags.Target, System.Drawing.Color.FromArgb(0, 0, 0).ToArgb(), 1.0f, 0);
GDevice.BeginScene();
InitDeviceXtr();
GDevice.VertexFormat = CustomVertex.PositionNormalTextured.Format;
//Question
//The function Setstream, comments in the parameter
GDevice.SetStreamSource(0,
VXtrBuffer, //If the Vertex Buffer is appended, I should offset it for each pass
(Pass * CVLength) * 32); //Offset for each pass
GDevice.Indices = IXtrBuffer;
//Question
//The function DrawIndexPrimitives, comments in the paramter
GDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, //Primitive Type
Pass * CVLength, //Base Vertex - Offset applied?
0, //MinIntVertex - Not sure? Should this point to the index of the next batch of vertices
CVLength, //Number of Vertices
(Pass * CVLength), //Start Index
((Pass * CVLength) + CVLength) / 3); //Primitive Count
//Increment the Pass Number
Pass++;
GDevice.EndScene();
GDevice.Present();
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.