this.scrollYaw = new System.Windows.Forms.HScrollBar();
this.scrollZoom = new System.Windows.Forms.HScrollBar();
this.gbZoom = new System.Windows.Forms.GroupBox();
+ this.btnResetView = new System.Windows.Forms.Button();
this.chkWireFrame = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
+ this.label2 = new System.Windows.Forms.Label();
this.gbZoom.SuspendLayout();
this.SuspendLayout();
//
this.glControl.Dock = System.Windows.Forms.DockStyle.Fill;
this.glControl.Location = new System.Drawing.Point(0, 0);
this.glControl.Name = "glControl";
- this.glControl.Size = new System.Drawing.Size(644, 549);
+ this.glControl.Size = new System.Drawing.Size(644, 524);
this.glControl.TabIndex = 5;
this.glControl.VSync = false;
this.glControl.Paint += new System.Windows.Forms.PaintEventHandler(this.glControl_Paint);
//
// gbZoom
//
+ this.gbZoom.Controls.Add(this.label2);
+ this.gbZoom.Controls.Add(this.btnResetView);
this.gbZoom.Controls.Add(this.chkWireFrame);
this.gbZoom.Controls.Add(this.label1);
this.gbZoom.Controls.Add(this.scrollZoom);
this.gbZoom.Controls.Add(this.scrollPitch);
this.gbZoom.Controls.Add(this.scrollRoll);
this.gbZoom.Dock = System.Windows.Forms.DockStyle.Bottom;
- this.gbZoom.Location = new System.Drawing.Point(0, 549);
+ this.gbZoom.Location = new System.Drawing.Point(0, 524);
this.gbZoom.Name = "gbZoom";
- this.gbZoom.Size = new System.Drawing.Size(644, 56);
+ this.gbZoom.Size = new System.Drawing.Size(644, 81);
this.gbZoom.TabIndex = 8;
this.gbZoom.TabStop = false;
//
+ // btnResetView
+ //
+ this.btnResetView.Location = new System.Drawing.Point(12, 52);
+ this.btnResetView.Name = "btnResetView";
+ this.btnResetView.Size = new System.Drawing.Size(94, 23);
+ this.btnResetView.TabIndex = 22;
+ this.btnResetView.Text = "Reset View";
+ this.btnResetView.UseVisualStyleBackColor = true;
+ this.btnResetView.Click += new System.EventHandler(this.btnReset_Click);
+ //
// chkWireFrame
//
this.chkWireFrame.AutoSize = true;
// label1
//
this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(293, 32);
+ this.label1.Location = new System.Drawing.Point(385, 29);
this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(132, 13);
+ this.label1.Size = new System.Drawing.Size(34, 13);
this.label1.TabIndex = 20;
- this.label1.Text = "Zoom (mouse scroll wheel)";
+ this.label1.Text = "Zoom";
+ //
+ // label2
+ //
+ this.label2.AutoSize = true;
+ this.label2.Location = new System.Drawing.Point(140, 57);
+ this.label2.Name = "label2";
+ this.label2.Size = new System.Drawing.Size(391, 13);
+ this.label2.TabIndex = 23;
+ this.label2.Text = "Drag to rotate object, ALT-Drag for Zoom, Alt-Ctrl-Drag for rotate, Ctrl-Drag to" +
+ " pan";
//
// frmPrimWorkshop
//
public System.Windows.Forms.GroupBox gbZoom;
private System.Windows.Forms.CheckBox chkWireFrame;
private System.Windows.Forms.Label label1;
+ private System.Windows.Forms.Button btnResetView;
+ private System.Windows.Forms.Label label2;
}
}
List<FacetedMesh> Prims = null;
- bool DraggingTexture = false;
bool Wireframe = false;
bool RenderingEnabled = false;
#region GLControl Callbacks
- private void glControl_Paint(object sender, PaintEventArgs e)
- {
- if (!RenderingEnabled) return;
+ Vector3 Center = Vector3.Zero;
+ private void Render()
+ {
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.LoadIdentity();
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
}
- Vector3 center = Vector3.Zero;
-
//var mLookAt = OpenTK.Matrix4d.LookAt(
// center.X, (double)scrollZoom.Value * 0.1d + center.Y, center.Z,
// center.X, center.Y, center.Z,
// 0d, 0d, 1d);
//GL.MultMatrix(ref mLookAt);
OpenTK.Graphics.Glu.LookAt(
- center.X, (double)scrollZoom.Value * 0.1d + center.Y, center.Z,
- center.X, center.Y, center.Z,
+ Center.X, (double)scrollZoom.Value * 0.1d + Center.Y, Center.Z,
+ Center.X, Center.Y, Center.Z,
0d, 0d, 1d);
//GL.Light(LightName.Light0, LightParameter.Position, lightPos);
break;
}
- GL.Color4(teFace.RGBA.R, teFace.RGBA.G, teFace.RGBA.B, teFace.RGBA.A);
- GL.Material(MaterialFace.Front, MaterialParameter.AmbientAndDiffuse, new float[] { teFace.RGBA.R, teFace.RGBA.G, teFace.RGBA.B, teFace.RGBA.A });
- GL.Material(MaterialFace.Front, MaterialParameter.Specular, new float[4] { teFace.RGBA.R, teFace.RGBA.G, teFace.RGBA.B, teFace.RGBA.A });
+ var faceColor = new float[] { teFace.RGBA.R, teFace.RGBA.G, teFace.RGBA.B, teFace.RGBA.A };
+
+ GL.Color4(faceColor);
+ GL.Material(MaterialFace.Front, MaterialParameter.AmbientAndDiffuse, faceColor);
+ GL.Material(MaterialFace.Front, MaterialParameter.Specular, faceColor);
#region Texturing
Face face = Prims[i].Faces[j];
GL.DisableClientState(ArrayCap.NormalArray);
GL.Flush();
+ }
+
+ private void glControl_Paint(object sender, PaintEventArgs e)
+ {
+ if (!RenderingEnabled) return;
+
+ Render();
+
glControl.SwapBuffers();
}
data.TexCoords[k * 2 + 1] = face.Vertices[k].TexCoord.Y;
}
- // Texture for this face
- if (LoadTexture(teFace.TextureID, ref data.Texture, false))
- {
- if (IsHandleCreated)
+ ThreadPool.QueueUserWorkItem(sync =>
{
- Invoke(new MethodInvoker(() =>
+ // Texture for this face
+ if (LoadTexture(teFace.TextureID, ref data.Texture, false))
{
- GL.GenTextures(1, out data.TexturePointer);
- GL.BindTexture(TextureTarget.Texture2D, data.TexturePointer);
-
- Bitmap bitmap = new Bitmap(data.Texture);
- bool hasAlpha;
- if (data.Texture.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb)
- {
- hasAlpha = true;
- }
- else
+ if (IsHandleCreated)
{
- hasAlpha = false;
+ Invoke(new MethodInvoker(() =>
+ {
+ GL.GenTextures(1, out data.TexturePointer);
+ GL.BindTexture(TextureTarget.Texture2D, data.TexturePointer);
+
+ Bitmap bitmap = new Bitmap(data.Texture);
+ bool hasAlpha;
+ if (data.Texture.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb)
+ {
+ hasAlpha = true;
+ }
+ else
+ {
+ hasAlpha = false;
+ }
+
+ bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
+ Rectangle rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
+
+
+ BitmapData bitmapData =
+ bitmap.LockBits(
+ rectangle,
+ ImageLockMode.ReadOnly,
+ hasAlpha ? System.Drawing.Imaging.PixelFormat.Format32bppArgb : System.Drawing.Imaging.PixelFormat.Format24bppRgb);
+
+ GL.TexImage2D(
+ TextureTarget.Texture2D,
+ 0,
+ hasAlpha ? PixelInternalFormat.Rgba : PixelInternalFormat.Rgb8,
+ bitmap.Width,
+ bitmap.Height,
+ 0,
+ hasAlpha ? OpenTK.Graphics.OpenGL.PixelFormat.Bgra : OpenTK.Graphics.OpenGL.PixelFormat.Bgr,
+ PixelType.UnsignedByte,
+ bitmapData.Scan0);
+
+ GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
+ //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
+ GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
+ GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
+ GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
+ GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1);
+ GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
+ bitmap.UnlockBits(bitmapData);
+ bitmap.Dispose();
+
+ SafeInvalidate();
+ }
+ ));
}
-
- bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
- Rectangle rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
-
-
- BitmapData bitmapData =
- bitmap.LockBits(
- rectangle,
- ImageLockMode.ReadOnly,
- hasAlpha ? System.Drawing.Imaging.PixelFormat.Format32bppArgb : System.Drawing.Imaging.PixelFormat.Format24bppRgb);
-
- GL.TexImage2D(
- TextureTarget.Texture2D,
- 0,
- hasAlpha ? PixelInternalFormat.Rgba : PixelInternalFormat.Rgb8,
- bitmap.Width,
- bitmap.Height,
- 0,
- hasAlpha ? OpenTK.Graphics.OpenGL.PixelFormat.Bgra : OpenTK.Graphics.OpenGL.PixelFormat.Bgr,
- PixelType.UnsignedByte,
- bitmapData.Scan0);
-
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
- //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1);
- GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
- bitmap.UnlockBits(bitmapData);
- bitmap.Dispose();
}
- ));
- }
- }
+ });
// Set the UserData for this face to our FaceData struct
face.UserData = data;
#endregion Scrollbar Callbacks
- #region PictureBox Callbacks
-
- private void picTexture_MouseDown(object sender, MouseEventArgs e)
- {
- DraggingTexture = true;
- }
-
- private void picTexture_MouseUp(object sender, MouseEventArgs e)
- {
- DraggingTexture = false;
- }
-
- private void picTexture_MouseLeave(object sender, EventArgs e)
- {
- DraggingTexture = false;
- }
-
- private void picTexture_MouseMove(object sender, MouseEventArgs e)
- {
- if (DraggingTexture)
- {
- // What is the current action?
- // None, DraggingEdge, DraggingCorner, DraggingWhole
- }
- else
- {
- // Check if the mouse is close to the edge or corner of a selection
- // rectangle
-
- // If so, change the cursor accordingly
- }
- }
-
- private void picTexture_Paint(object sender, PaintEventArgs e)
- {
- // Draw the current selection rectangles
- }
-
- #endregion PictureBox Callbacks
private void chkWireFrame_CheckedChanged(object sender, EventArgs e)
{
int deltaX = e.X - dragX;
int deltaY = e.Y - dragY;
- int newRoll = scrollRoll.Value + deltaY;
- if (newRoll < 0) newRoll += 360;
- if (newRoll > 360) newRoll -= 360;
+ if (ModifierKeys == Keys.Control || ModifierKeys == (Keys.Alt | Keys.Control | Keys.Shift))
+ {
+ Center.X -= deltaX / 100f;
+ Center.Z += deltaY / 100f;
+ }
- scrollRoll.Value = newRoll;
+ if (ModifierKeys == Keys.Alt)
+ {
+ Center.Y -= deltaY / 25f;
+ int newYaw = scrollYaw.Value + deltaX;
+ if (newYaw < 0) newYaw += 360;
+ if (newYaw > 360) newYaw -= 360;
- int newYaw = scrollYaw.Value + deltaX;
- if (newYaw < 0) newYaw += 360;
- if (newYaw > 360) newYaw -= 360;
+ scrollYaw.Value = newYaw;
- scrollYaw.Value = newYaw;
+ }
+
+ if (ModifierKeys == Keys.None || ModifierKeys == (Keys.Alt | Keys.Control))
+ {
+ int newRoll = scrollRoll.Value + deltaY;
+ if (newRoll < 0) newRoll += 360;
+ if (newRoll > 360) newRoll -= 360;
+
+ scrollRoll.Value = newRoll;
+
+
+ int newYaw = scrollYaw.Value + deltaX;
+ if (newYaw < 0) newYaw += 360;
+ if (newYaw > 360) newYaw -= 360;
+
+ scrollYaw.Value = newYaw;
+
+ }
dragX = e.X;
dragY = e.Y;
+
SafeInvalidate();
}
}
GL.Enable(EnableCap.CullFace);
GL.ColorMaterial(MaterialFace.Front, ColorMaterialParameter.AmbientAndDiffuse);
GL.ColorMaterial(MaterialFace.Front, ColorMaterialParameter.Specular);
-
+
GL.DepthMask(true);
GL.DepthFunc(DepthFunction.Lequal);
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
// and will also invalidate the GL control
glControl_Resize(null, null);
}
+
+ private void btnReset_Click(object sender, EventArgs e)
+ {
+ scrollYaw.Value = 0;
+ scrollPitch.Value = 0;
+ scrollRoll.Value = 0;
+ scrollZoom.Value = -50;
+ Center = Vector3.Zero;
+
+ SafeInvalidate();
+ }
+
}
- public struct FaceData
+ public class FaceData
{
public float[] Vertices;
public ushort[] Indices;