概要
cscの作法、調べてみた。
OpenRCF v2.8、見つけたので、windows11で、MSbuildしてみた。
練習問題やってみた。
練習問題
objの文字列から、プリミティブを作れ。
写真
サンプルコード
class Obj {
private float radius = 0.5f;
private float[][] Vertex = new float[12][];
private static class BaseVertex {
public static float[][] Vertex = new float[12][];
static BaseVertex() {
string code = @"
v -1 -1 0
v 1 -1 0
v 1 1 0
v -1 1 0
v 0 0 1
f 5 1 2
f 5 2 3
f 5 3 4
f 5 4 1
";
code = code.Replace(" ", " ");
string[] lines = code.Split(new string[] {
"\n"
}, StringSplitOptions.None);
int n = 0;
int t = 0;
float[,] v = new float[5, 3];
float[,] f = new float[12, 3];
for (int i = 0; i < lines.Length; i++)
{
string[] spline = lines[i].Split(new string[] {
" "
}, StringSplitOptions.None);
switch (spline[0])
{
case "v":
v[n, 0] = float.Parse(spline[1]);
v[n, 1] = float.Parse(spline[2]);
v[n, 2] = float.Parse(spline[3]);
n++;
break;
case "f":
f[t, 0] = v[int.Parse(spline[1]) - 1, 0];
f[t, 1] = v[int.Parse(spline[1]) - 1, 1];
f[t, 2] = v[int.Parse(spline[1]) - 1, 2];
f[t + 1, 0] = v[int.Parse(spline[2]) - 1, 0];
f[t + 1, 1] = v[int.Parse(spline[2]) - 1, 1];
f[t + 1, 2] = v[int.Parse(spline[2]) - 1, 2];
f[t + 2, 0] = v[int.Parse(spline[3]) - 1, 0];
f[t + 2, 1] = v[int.Parse(spline[3]) - 1, 1];
f[t + 2, 2] = v[int.Parse(spline[3]) - 1, 2];
t = t + 3;
break;
default:
break;
}
}
for (int i = 0; i < 12; i++)
{
Vertex[i] = new float[3] { f[i, 2], f[i, 1], f[i, 0] };
}
}
}
public Vector Position = new Vector(3);
public RotationMatrix Rotate = new RotationMatrix();
public Color Color = new Color();
public float Size = 0.5f;
public float Radius {
get {
return radius;
}
set {
radius = value;
}
}
public Obj(float size = 0.5f) {
Size = size;
}
public void Draw() {
VertexUpdate();
Color.EnableBodyColor();
DrawVertex(PrimitiveType.Polygon);
}
private void DrawVertex(PrimitiveType primitiveType) {
GL.Begin(primitiveType);
GL.Normal3(Rotate[0, 2], Rotate[1, 2], Rotate[2, 2]);
for (int i = 0; i < Vertex.GetLength(0); i++)
{
GL.Vertex3(Vertex[i]);
}
GL.End();
}
private void VertexUpdate() {
float[] vector = new float[3];
for (int i = 0; i < Vertex.GetLength(0); i++)
{
vector[0] = Size * BaseVertex.Vertex[i][0];
vector[1] = Size * BaseVertex.Vertex[i][1];
vector[2] = Size * BaseVertex.Vertex[i][2];
Vertex[i] = Position.Plus(Rotate * vector);
}
}
}
以上。