|
Dieses Beispielzeigt wie ein Shader aufgebaut ist.
Er besteht aus einem Fragmentshader und einem Vertexshader. ( Es gibt noch andere Shader, wie der Geometryshader, TessControlShader, tessEvaluationShader).
Schreiben Sie Ihren eigenen Fragmentshader und Vertexshader und klicken Sie auf den Move to OpenGlDevice Button. Dann wird Ihr Shader compiliert und der output entsprechend dargestellt.
Zum Beenden müssen Sie das Outputfenster schließen.
// Das outputfenster
using System;
using System.Drawing;
using System.Windows.Forms;
using Drawing3d;
namespace Shaders
{
public partial class Form1 : Form
{
MyDevice Device = new MyDevice();
FrmShader ShaderForm = new FrmShader();
public Form1()
{
InitializeComponent();
Device.WinControl = this;
ShaderForm.Device = Device;
ShaderForm.Show();
}
}
public class MyDevice:OpenGlDevice
{
Texture EarthTexture = new Texture();
GLShader SaveShader = null;
Sphere S = new Sphere(3);
protected override void OnCreated()
{
base.OnCreated();
SaveShader = Shader;
EarthTexture.LoadFromFile("earthmap1k.jpg");
Shadow = true;
}
GLShader ExternShader = null;
internal void ActivateShader(string Fragment, string Vertex)
{
if ((ExternShader != null) && (ExternShader.Handle > 0))
ExternShader.Dispose();
try
{
ExternShader = new GLShader(Fragment, Vertex,this);
Shader = ExternShader;
}
catch (Exception E)
{
Shader = SaveShader;
}
Shader.UpDateAllVars();
Refresh();
}
public override void OnPaint()
{
base.OnPaint();
Color SaveAmient = Ambient;
Ambient = Color.FromArgb(255,50,50,50);
drawBox(new xyz(-10, -10, -4), new xyz( 20, 20, 1));
Ambient = SaveAmient;
texture = EarthTexture;
S.Paint(this);
texture = null;
}
}
}
//-------------------------------------
// Das Shader Fenster
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Shaders
{
private void tbFragment_MouseMove(object sender, MouseEventArgs e)
{
Point P = tbFragment.PointToClient(MousePosition);
int k = tbFragment.GetCharIndexFromPosition(P);
int line = tbFragment.GetLineFromCharIndex(k)+1;
lbLineFragment.Text = line.ToString();
}
private void tbVertex_MouseMove(object sender, MouseEventArgs e)
{
Point P = tbVertex.PointToClient(MousePosition);
int k = tbVertex.GetCharIndexFromPosition(P);
int line = tbVertex.GetLineFromCharIndex(k)+1;
lbLineVertex.Text = line.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
Device.ActivateShader(tbFragment.Text, tbVertex.Text);
}
string ShaderName;
string PathName;
string FragmentShader;
string VertexShader;
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.FileName = "";
if (saveFileDialog1.ShowDialog()== DialogResult.OK)
{
string _ShaderName = System.IO.Path.GetFileNameWithoutExtension(saveFileDialog1.FileName);
string _PathName = System.IO.Directory.GetParent(saveFileDialog1.FileName).FullName;
string _FragmentShader = _PathName + "\\" + _ShaderName + ".frag";
string _VertexShader = _PathName + "\\" + _ShaderName + ".vert";
{
try
{
System.IO.File.WriteAllText(_FragmentShader, tbFragment.Text);
System.IO.File.WriteAllText(_VertexShader, tbVertex.Text);
ShaderName = _ShaderName;
PathName = _PathName;
FragmentShader = _FragmentShader;
VertexShader = _VertexShader;
this.Text = ShaderName;
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
}
}
}
void saveShader()
{
if ((FragmentShader != null) && (FragmentShader != ""))
try
{
System.IO.File.WriteAllText(FragmentShader, tbFragment.Text);
System.IO.File.WriteAllText(VertexShader, tbVertex.Text);
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
tbFragment.Modified = false;
tbVertex.Modified = false;
}
private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
if (tbVertex.Focused)
tbVertex.Undo();
if (tbFragment.Focused)
tbFragment.Undo();
}
private void FrmShader_Load(object sender, EventArgs e)
{
undoToolStripMenuItem.ShortcutKeys = Keys.Back | Keys.Alt;
undoToolStripMenuItem.Visible = false;
}
private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
{
saveShader();
}
}
}
|
|