Skip to content

Instantly share code, notes, and snippets.

@f-space
Created Sep 26, 2016
Embed
What would you like to do?
DXライブラリで三角形を回転させるサンプル (C#)
using DxLibDLL;
using System;
using System.Diagnostics;
namespace DxLibTest
{
public static class Program
{
private static readonly float DegToRad = (float)(Math.PI / 180.0);
private static readonly float Sqrt3 = (float)Math.Sqrt(3.0);
private static readonly Vector[] Triangle =
{
new Vector(0.0f, 1.0f / Sqrt3, 0.0f),
new Vector(0.5f, -0.5f / Sqrt3, 0.0f),
new Vector(-0.5f, -0.5f / Sqrt3, 0.0f),
};
private static Stopwatch stopwatch;
[STAThread]
public static void Main()
{
DX.ChangeWindowMode(DX.TRUE);
if (DX.DxLib_Init() == -1)
{
throw new InvalidOperationException("Failed to initialize DxLib.");
}
DX.SetDrawScreen(DX.DX_SCREEN_BACK);
Initialize();
try
{
while (DX.ProcessMessage() == 0)
{
if (DX.CheckHitKey(DX.KEY_INPUT_ESCAPE) != 0) break;
DX.ClearDrawScreen();
Update();
DX.ScreenFlip();
}
}
finally
{
DX.DxLib_End();
}
}
private static void Initialize()
{
Vector cameraPosition = new Vector(0.0f, 0.25f / Sqrt3, -1.0f);
Vector cameraTarget = new Vector(0.0f, 0.25f / Sqrt3, 0.0f);
DX.SetupCamera_Ortho(2.0f);
DX.SetCameraNearFar(0.01f, 100.0f);
DX.SetCameraPositionAndTarget_UpVecY(cameraPosition, cameraTarget);
stopwatch = new Stopwatch();
stopwatch.Start();
}
private static void Update()
{
float time = (float)stopwatch.Elapsed.TotalSeconds;
Vector angle;
angle.X = 17.0f * DegToRad * time;
angle.Y = 29.0f * DegToRad * time;
angle.Z = 37.0f * DegToRad * time;
Vector pos0 = Triangle[0].Rotate(angle);
Vector pos1 = Triangle[1].Rotate(angle);
Vector pos2 = Triangle[2].Rotate(angle);
DX.DrawTriangle3D(pos0, pos1, pos2, DX.GetColor(255, 0, 0), DX.TRUE);
}
}
}
using DxLibDLL;
using DxLibVector = DxLibDLL.DX.VECTOR;
using DxLibMatrix = DxLibDLL.DX.MATRIX;
namespace DxLibTest
{
public struct Vector
{
#region Fields
public float X;
public float Y;
public float Z;
#endregion
#region Constructors
public Vector(float x, float y, float z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public Vector(DxLibVector v)
{
this.X = v.x;
this.Y = v.y;
this.Z = v.z;
}
#endregion
#region Methods
public Vector Rotate(Vector angle)
{
DxLibMatrix rotX = DX.MGetRotX(angle.X);
DxLibMatrix rotY = DX.MGetRotY(angle.Y);
DxLibMatrix rotZ = DX.MGetRotZ(angle.Z);
DxLibMatrix rotation = DX.MMult(DX.MMult(rotX, rotY), rotZ);
return DX.VTransform(this, rotation);
}
public DxLibVector ToDxLibVector()
{
DxLibVector result;
result.x = this.X;
result.y = this.Y;
result.z = this.Z;
return result;
}
#endregion
#region Operators
public static Vector operator +(Vector v)
{
return v;
}
public static Vector operator -(Vector v)
{
Vector result;
result.X = -v.X;
result.Y = -v.Y;
result.Z = -v.Z;
return result;
}
public static Vector operator +(Vector v1, Vector v2)
{
Vector result;
result.X = v1.X + v2.X;
result.Y = v1.Y + v2.Y;
result.Z = v1.Z + v2.Z;
return result;
}
public static Vector operator -(Vector v1, Vector v2)
{
Vector result;
result.X = v1.X - v2.X;
result.Y = v1.Y - v2.Y;
result.Z = v1.Z - v2.Z;
return result;
}
public static Vector operator *(Vector v, float scalar)
{
Vector result;
result.X = v.X * scalar;
result.Y = v.Y * scalar;
result.Z = v.Z * scalar;
return result;
}
public static Vector operator *(float scalar, Vector v)
{
Vector result;
result.X = v.X * scalar;
result.Y = v.Y * scalar;
result.Z = v.Z * scalar;
return result;
}
public static Vector operator /(Vector v, float scalar)
{
Vector result;
result.X = v.X / scalar;
result.Y = v.Y / scalar;
result.Z = v.Z / scalar;
return result;
}
public static implicit operator DxLibVector(Vector v)
{
DxLibVector result;
result.x = v.X;
result.y = v.Y;
result.z = v.Z;
return result;
}
public static implicit operator Vector(DxLibVector v)
{
Vector result;
result.X = v.x;
result.Y = v.y;
result.Z = v.z;
return result;
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment