[C#, OpenGL] 3D coordinates to 2D coordinates
This code will convert 3D coordinates to 2D coordinates. It is taken from around the web and put together to work with C# and OpenGL (OpenTK actually). If you use something else you will probably have to change the code somewhat, but that should not be a huge problem I think.
private Point WorldToScreen(Vector3 p) { Matrix4 model, proj; int[] view = new int[4]; GL.GetFloat(GetPName.ModelviewMatrix, out model); GL.GetFloat(GetPName.ProjectionMatrix, out proj); GL.GetInteger(GetPName.Viewport, view); double wx = 0, wy = 0, wz = 0; int d = Glu.gluProject ( p.X, p.Y, p.Z, model, proj, view, ref wx, ref wy, ref wz ); return new Point((int)wx, (int)wy); }
int gluProject ( float objx, float objy, float objz, Matrix4 modelMatrix, Matrix4 projMatrix, int[] viewport, ref double winx, ref double winy, ref double winz ) { Vector4 _in; Vector4 _out; _in.X = objx; _in.Y = objy; _in.Z = objz; _in.W = 1.0f; //__gluMultMatrixVecd(modelMatrix, in, out); // Commented out by code author //__gluMultMatrixVecd(projMatrix, out, in); // Commented out by code author //TODO: check if multiplication is in right order _out = Vector4.Transform(_in, modelMatrix); _in = Vector4.Transform(_out, projMatrix); if (_in.W == 0.0) return (0); _in.X /= _in.W; _in.Y /= _in.W; _in.Z /= _in.W; /* Map x, y and z to range 0-1 */ _in.X = _in.X * 0.5f + 0.5f; _in.Y = _in.Y * 0.5f + 0.5f; _in.Z = _in.Z * 0.5f + 0.5f; /* Map x,y to viewport */ _in.X = _in.X * viewport[2] + viewport[0]; _in.Y = _in.Y * viewport[3] + viewport[1]; winx = _in.X; winy = _in.Y; winz = _in.Z; return (1); }
[C#] Console output in a form / textbox
http://sourceforge.net/projects/consolewidget/
Setup:
- Download the files.
- Go to our project and find the references
- Add the .dll
- Go to the form-designer and create a new textbox
- Go to the xx.Designer.cs ***
- Find :
this.textBox1 = new System.Windows.Forms.TextBox();
and
private System.Windows.Forms.TextBox textBox1; - Replace them with :
private ConsoleWidget.ConsoleTextBox textBox1;
and
this.textBox1 = new ConsoleWidget.ConsoleTextBox();
Bonus: If you want to make you WHOLE form a console-output-form you can do like this:
Change Form1 : Form into Form1 : ConsoleWidget.ConsoleForm[C#] Struct to Byte-array and back
Setup / Code:
The struct we will send.
[StructLayout(LayoutKind.Sequential, Pack = 1)] public struct datastruct { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)] // Max length of string public string str; public float x; public float y; }
This converts the struct into an array of bytes.
public static Byte[] SerializeMessage<T>(T msg) where T : struct { int objsize = Marshal.SizeOf(typeof(T)); Byte[] ret = new Byte[objsize]; IntPtr buff = Marshal.AllocHGlobal(objsize); Marshal.StructureToPtr(msg, buff, true); Marshal.Copy(buff, ret, 0, objsize); Marshal.FreeHGlobal(buff); return ret; }
This converts it back into a struct.
public static T DeserializeMsg(Byte[] data) where T : struct { int objsize = Marshal.SizeOf(typeof(T)); IntPtr buff = Marshal.AllocHGlobal(objsize); Marshal.Copy(data, 0, buff, objsize); T retStruct = (T)Marshal.PtrToStructure(buff, typeof(T)); Marshal.FreeHGlobal(buff); return retStruct; }
Usage:
Struct to byte-array.
yourstruct info = new yourstruct(); info.str= "hej"; info.x = 2; info.y = 4; byte[] bytearray = MarshalHelper.MarshalHelper.SerializeMessage<datastruct>(info);
Byte-array to struct.
byte[] data = bytearray; // from above code object d = MarshalHelper.MarshalHelper.DeserializeMsg<datastruct>(data); datastruct ds = (datastruct)d;
"Output"
ds.str = hej ds.x = 2 ds.y = 4
Syntax highlighting in blogger.
http://alexgorbatchev.com/SyntaxHighlighter/manual/api/autoloader.html
Usage:
<pre class="brush: csharp">
Code goes here.
</pre>
Result:
public class SyntaxHighlight { public Testing() { } public void Method() { int x = 9; } }