Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
seto77 committed May 9, 2020
1 parent 120c3f9 commit 255c976
Show file tree
Hide file tree
Showing 18 changed files with 1,754 additions and 677 deletions.
74 changes: 37 additions & 37 deletions Crystallography.Controls/Crystal/AtomControl.resx

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Crystallography.Controls/Crystal/CrystalControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ private void checkSpecialNumber()
}
}
}

#region Crystalクラスを画面下部 から生成/にセット


Expand Down
4 changes: 2 additions & 2 deletions Crystallography.Controls/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// すべての値を指定するか、下のように '*' を使ってビルドおよびリビジョン番号を
// 既定値にすることができます:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2020.5.6.0925")]
[assembly: AssemblyFileVersion("2020.5.6.0925")]
[assembly: AssemblyVersion("2020.5.9.1049")]
[assembly: AssemblyFileVersion("2020.5.9.1049")]
1 change: 1 addition & 0 deletions Crystallography.OpenGL/Crystallography.OpenGL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Windows.Forms" />
Expand Down
133 changes: 98 additions & 35 deletions Crystallography.OpenGL/GLControlAlpha.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Vec2d = OpenTK.Vector2d;
using Vec3d = OpenTK.Vector3d;
using Vec3f = OpenTK.Vector3;
using System.Management;

namespace Crystallography.OpenGL
{
Expand All @@ -26,6 +27,7 @@ unsafe public partial class GLControlAlpha : UserControl

private Clip Clip = null;
private List<GLObject> glObjects = new List<GLObject>();
private ParallelQuery<GLObject> glObjectsP;
private GLObject quad = null;

private int eyePositionIndex = 0;
Expand All @@ -36,6 +38,10 @@ unsafe public partial class GLControlAlpha : UserControl
private int passOIT1Index = 0;
private int passOIT2Index = 0;
private int passNormalIndex = 0;

private int depthCueingNearIndex = 0;
private int depthCueingFarIndex = 0;
private int depthCueingEnabledIndex = 0;
#endregion フィールド

#region Enum
Expand All @@ -46,18 +52,18 @@ public enum RotationModes { Object, View, Light }

public enum TranslatingModes { Object, View }

public enum RenderingTransparencyModes { Always, NotAlways, Never }
public enum RenderingTransparencyModes { OIT, ZSORT}

#endregion Enum

#region イベント
public new MouseEventHandler MouseMove;
public new MouseEventHandler MouseDown;
public new MouseEventHandler MouseUp;

public new PaintEventHandler Paint;


/// <summary>
/// WorldMatrixが変化したときに発生するイベント.
/// </summary>
Expand Down Expand Up @@ -169,7 +175,7 @@ public string GLVersionCurrent
/// Order Independent Transparency modeを有効にするかどうか
/// </summary>
[Category("Rendering properties")]
public RenderingTransparencyModes RenderingTransparency { get; set; } = RenderingTransparencyModes.NotAlways;
public RenderingTransparencyModes RenderingTransparency { get; set; } = RenderingTransparencyModes.ZSORT;

/// <summary>
/// バックグラウンドカラー
Expand Down Expand Up @@ -235,6 +241,8 @@ public Matrix3D WorldMatrixEx
}
}

public List<(string Product, string Version)> GraphicsInfo { get; set; } = new List<(string Product, string Version)>();

/// <summary>
/// カメラの位置
/// </summary>
Expand Down Expand Up @@ -324,7 +332,6 @@ private void setProjMatrix()
#endregion プロパティ

#region ロード関連
private System.Timers.Timer timer = new System.Timers.Timer(100);
private GLControl glControl = new GLControl();
private Graphics glControlGraphics;

Expand All @@ -337,14 +344,17 @@ public GLControlAlpha()

if (DesignMode) return;

// glControlのコンストラクタで、GraphicsModeを指定する必要があるが、これをするとデザイナが壊れるので、ここに書く。
glControl = new OpenTK.GLControl(new GraphicsMode(GraphicsMode.Default.ColorFormat, GraphicsMode.Default.Depth, 8))
glObjectsP = glObjects.AsParallel();

// glControlのコンストラクタで、GraphicsModeを指定する必要があるが、これをするとデザイナが壊れるので、ここに書く。
var gMode = new GraphicsMode(GraphicsMode.Default.ColorFormat, GraphicsMode.Default.Depth, 8, 1);
glControl = new GLControl(gMode)
{
AutoScaleMode = AutoScaleMode.Dpi,
BackColor = Color.White,
Location = new Point(0, 0),
Name = "glControl",
Size = new Size(this.Size.Width, this.Size.Height),
Size = new Size(Size.Width, Size.Height),
Dock = DockStyle.Fill,
TabIndex = 1,
VSync = false,
Expand All @@ -360,7 +370,23 @@ public GLControlAlpha()

Controls.Add(glControl);

//ビデオカード検索
var searcher = new ManagementObjectSearcher(new SelectQuery("Win32_VideoController"));
foreach (var envVar in searcher.Get())
GraphicsInfo.Add((envVar["name"].ToString(), envVar["DriverVersion"].ToString()));

if (GraphicsInfo.Select(g => g.Product.ToLower()).Any(p => p.Contains("nvidia") || p.Contains("amd")))
{
Cone.Default = (1, 16);
Pipe.Default = (1, 16);
Sphere.DefaultSlices = 3;
}
else
{
Cone.Default = (1, 8);
Pipe.Default = (1, 8);
Sphere.DefaultSlices = 2;
}
}


Expand Down Expand Up @@ -392,6 +418,10 @@ private void glControl_Load(object sender, EventArgs e)
projMatrixIndex = GL.GetUniformLocation(Program, "ProjMatrix");
worldMatrixIndex = GL.GetUniformLocation(Program, "WorldMatrix");

depthCueingEnabledIndex = GL.GetUniformLocation(Program, "DepthCueing");
depthCueingFarIndex = GL.GetUniformLocation(Program, "Far");
depthCueingNearIndex = GL.GetUniformLocation(Program, "Near");

passOIT1Index = GL.GetSubroutineIndex(Program, ShaderType.FragmentShader, "passOIT1");
passOIT2Index = GL.GetSubroutineIndex(Program, ShaderType.FragmentShader, "passOIT2");
passNormalIndex = GL.GetSubroutineIndex(Program, ShaderType.FragmentShader, "passNormal");
Expand All @@ -404,14 +434,8 @@ private void glControl_Load(object sender, EventArgs e)
quad = new Quads(new Vec3d(-1, -1, 1), new Vec3d(1, -1, 1), new Vec3d(1, 1, 1), new Vec3d(-1, 1, 1), new Material(1, 1, 1, 1, 1, 1, 1, 1, 1), DrawingMode.Surfaces);
quad.Generate(Program);

timer.AutoReset = false;
timer.Elapsed += Timer_Elapsed;
}

private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Render(true);
timer.Stop();
GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);
GL.Hint(HintTarget.PolygonSmoothHint, HintMode.Nicest);
}

#region Shaderの作成
Expand Down Expand Up @@ -521,6 +545,14 @@ public void AddObjects(IEnumerable<GLObject> objs)
foreach (var obj in objs)
obj.Generate(Program);
glObjects.AddRange(objs);

/* foreach (var obj in objs)
{
obj.Indices = null;
obj.Vertices = null;
}
GC.Collect();
*/
}

public void DeleteObjects(GLObject obj)
Expand Down Expand Up @@ -562,26 +594,20 @@ public void SetClip(Clip clip)
/// <summary>
/// レンダリング
/// </summary>
public void Render(bool oit = false)
public void Render()
{
if (InvokeRequired)//別スレッドから呼び出されたとき Invokeして呼びなおす
{
Invoke(new Action(() => Render(oit)), null);
Invoke(new Action(() => Render()), null);
return;
}
timer.Stop();

if (SkipRendering || Program < 1)
return;

if (RenderingTransparency == RenderingTransparencyModes.Always)
oit = true;
else if (RenderingTransparency == RenderingTransparencyModes.Never)
oit = false;

glControl.MakeCurrent();

if (glObjects == null || glObjects.Count == 0 || glObjects.All(obj => obj.Rendered == false))
if (glObjects == null || glObjects.Count == 0 || glObjectsP.All(obj => obj.Rendered == false))
{
GL.UniformSubroutines(ShaderType.FragmentShader, 1, ref passNormalIndex);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
Expand All @@ -599,8 +625,12 @@ public void Render(bool oit = false)
GL.UniformMatrix4(projMatrixIndex, false, ref projMatrixF);
GL.UniformMatrix4(worldMatrixIndex, false, ref worldMatrixF);

if (oit)//oitモードの時、 CullFace無効、DepthTest無効、
if (RenderingTransparency == RenderingTransparencyModes.OIT)//oitモードの時、 CullFace無効、DepthTest無効、
{
GL.Disable(EnableCap.LineSmooth);
GL.Disable(EnableCap.PolygonSmooth);
GL.Disable(EnableCap.Blend);

GL.Disable(EnableCap.CullFace);
GL.Disable(EnableCap.DepthTest);
var bgcolor = BackgroundColor.ToV4f();
Expand Down Expand Up @@ -630,6 +660,11 @@ public void Render(bool oit = false)
}
else//通常モードの時、 CullFace有効、DepthTest有効
{
GL.Enable(EnableCap.LineSmooth);
GL.Enable(EnableCap.PolygonSmooth);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);

GL.Enable(EnableCap.CullFace);
GL.CullFace(CullFaceMode.Back);
GL.Enable(EnableCap.DepthTest);
Expand All @@ -638,14 +673,22 @@ public void Render(bool oit = false)
GL.UniformSubroutines(ShaderType.FragmentShader, 1, ref passNormalIndex);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.ClearColor(BackgroundColor);

//描画対称に透明なものが一つでもあるとき
if (glObjectsP.Any(o => o.Rendered && o.Material.ColorV.W != 1))
{
var rot = worldMatrix.Inverted();
glObjectsP.ForAll(o => o.Z = !o.Rendered || o.Material.ColorV.W == 1 ? double.NegativeInfinity : rot.Mult(o.CircumscribedSphereCenter).Z);
glObjects.Sort((o1, o2) => o1.Z.CompareTo(o2.Z));
}

glObjects.ForEach(o => o.Render(Clip));// draw scene
}

glControl.SwapBuffers();//swap
GL.Finish();

Paint?.Invoke(this, new PaintEventArgs(glControlGraphics, glControl.ClientRectangle));

timer.Start();
}

#endregion
Expand Down Expand Up @@ -685,13 +728,7 @@ private void glControl_MouseMove(object sender, MouseEventArgs e)
else if (e.Button == MouseButtons.Right && AllowMouseScaling)
{
if ((ModifierKeys & Keys.Control) == Keys.Control && ProjectionMode == ProjectionModes.Perspective)
{
SkipRendering = true;
viewFrom *= (float)(1 + dy * 0.005);
setViewMatrix();
SkipRendering = false;
setProjMatrix();
}
SetPerspectiveDistance(viewFrom.Length * (float)(1 + dy * 0.005));
else
{
var coeff = 1 + dy * 0.005;
Expand All @@ -703,6 +740,8 @@ private void glControl_MouseMove(object sender, MouseEventArgs e)
lastMousePosition = new Point(e.X, e.Y);
}



private void GlControl_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.None && AllowMouseScaling)
Expand Down Expand Up @@ -730,6 +769,16 @@ private void glControl_MouseUp(object sender, MouseEventArgs e)

#endregion

public void SetPerspectiveDistance(double distance)
{
SkipRendering = true;
viewFrom = viewFrom.Normalized() * distance;
setViewMatrix();
SkipRendering = false;
setProjMatrix();

}

#region GLControlのイベント

private void glControl_Paint(object sender, PaintEventArgs e)
Expand Down Expand Up @@ -758,6 +807,20 @@ public Bitmap GenerateBitmap()
return BitmapConverter.FlipVertically(bmp);
}

#endregion

#region Depth Cueingの設定

public void SetDepthCueing(bool enabled, double near, double far)
{
GL.Uniform1(depthCueingEnabledIndex, enabled ? 1 : 0);
GL.Uniform1(depthCueingNearIndex, (float)near);
GL.Uniform1(depthCueingFarIndex, (float)far);
GL.Finish();
Render();
}


#endregion
}
}
Loading

0 comments on commit 255c976

Please sign in to comment.