Gray-scale to Surface Generator

1

Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Public Class Form1
Dim Drawn As Boolean = False
Dim pointList As List(Of String) = New List(Of String)
Dim picWidth As Integer
Dim picHieght As Integer
Dim BGcolor As Color = Color.LightGray
Dim FGColor As Color
‘ These will be our switches
Dim Go As Boolean
Dim LeftSet As Boolean
Dim TopSet As Boolean
‘ These will hold the mouse position
Dim HoldLeft As Integer
Dim HoldTop As Integer
‘ These will hold the offset of the mouse in the element
Dim OffLeft As Integer
Dim OffTop As Integer
Dim EyeX As Integer
Dim EyeY As Integer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Drawn = False
OpenFileDialog1.Title = “Please Select a File”
OpenFileDialog1.InitialDirectory = “C:\Users\Tom\Pictures\Achitecture\Year Two\Site Vist\Photos”
OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim bm As New Bitmap(OpenFileDialog1.FileName)
PictureBox1.Image = bm
PictureBox1.SizeMode = PictureBoxSizeMode.Normal
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bitMap As System.Drawing.Bitmap = New Bitmap(PictureBox1.Image)
Dim newImage As System.Drawing.Bitmap = New Bitmap(bitMap.Width, bitMap.Height)
Dim i As Integer = 0
Dim j As Integer = 0
Dim Red As Integer
Dim Blue As Integer
Dim Green As Integer
Dim greyscale As Integer
picWidth = bitMap.Width – 1
picHieght = bitMap.Height – 1
ProgressBar1.Maximum = (bitMap.Height + 1)
Do Until j = (bitMap.Height)
i = 0
Do
‘test
Red = bitMap.GetPixel(i, j).R
Blue = bitMap.GetPixel(i, j).B
Green = bitMap.GetPixel(i, j).G
greyscale = Int((Red + Blue + Green) / 3)
If RadioButton1.Checked = True Then
greyscale = Int(greyscale / 10)
End If
newImage.SetPixel(i, j, Color.FromArgb(greyscale, greyscale, greyscale))
pointList.Add(greyscale.ToString)
RichTextBox1.Text += i.ToString + “:” + j.ToString + “:” + greyscale.ToString + “|”
‘next i or loop to next j
If i = (bitMap.Width – 1) Then
Exit Do
Else
i += 1
End If
Loop
j += 1
ProgressBar1.Value += 1
ProgressBar1.Update()
Loop
PictureBox2.Image = newImage
Drawn = True
ProgressBar1.Value = ProgressBar1.Maximum
ProgressBar1.Update()
EyeX = 300
EyeY = 50
Button1.Enabled = False
Button2.Enabled = False
End Sub
Private Sub GlControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GlControl1.Load
‘Control is loaded, set back color
GL.ClearColor(BGcolor)
End Sub
Private Sub GlControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GlControl1.Paint
GL.ClearColor(BGcolor)
‘First Clear Buffers
GL.Clear(ClearBufferMask.ColorBufferBit)
GL.Clear(ClearBufferMask.DepthBufferBit)
‘Basic Setup for viewing
Dim perspective As Matrix4 = Matrix4.CreatePerspectiveFieldOfView(1.04, 4 / 3, 1, 10000) ‘Setup Perspective
Dim lookat As Matrix4 = Matrix4.LookAt(EyeX, EyeY, 0, 0, 0, 0, 0, 1, 0) ‘Setup camera
GL.MatrixMode(MatrixMode.Projection) ‘Load Perspective
GL.LoadIdentity()
GL.LoadMatrix(perspective)
GL.MatrixMode(MatrixMode.Modelview) ‘Load Camera
GL.LoadIdentity()
GL.LoadMatrix(lookat)
GL.Viewport(0, 0, GlControl1.Width, GlControl1.Height) ‘Size of window
GL.Enable(EnableCap.DepthTest) ‘Enable correct Z Drawings
GL.DepthFunc(DepthFunction.Less) ‘Enable correct Z Drawings
‘Rotating
GL.Rotate(NumericUpDown1.Value, 0, 0, 1)
GL.Rotate(NumericUpDown2.Value, 0, 1, 0)
GL.Rotate(HoldLeft – OffLeft, 0, 1, 0)
GL.Rotate(HoldTop – OffTop, 0, 0, 1)
‘Draw pyramid, Y is up, Z is twards you, X is left and right
‘Vertex goes (X,Y,Z)
Dim greyHeight As Integer
Dim counterI As Integer = -(Int((picWidth + 1) / 2))
Dim counterJ As Integer = -(Int((picHieght + 1) / 2))
If Drawn = True Then
GL.Color3(FGColor)
If ComboBox1.Text = “Points” Then
GL.Begin(BeginMode.Points)
For Each part In pointList
greyHeight = 128 – part
GL.Vertex3(counterI, greyHeight, counterJ)
If counterI = Int(picWidth / 2) Then
counterJ += 1
counterI = -(Int(picWidth / 2))
Else
counterI += 1
End If
Next
GL.End()
End If
If ComboBox1.Text = “Lines” Then
GL.Begin(BeginMode.Lines)
For Each part In pointList
greyHeight = 128 – part
GL.Vertex3(counterI, greyHeight, counterJ)
If counterI = Int(picWidth / 2) Then
counterJ += 1
counterI = -(Int(picWidth / 2))
Else
counterI += 1
End If
Next
GL.End()
End If
If ComboBox1.Text = “Blocks” Then
For Each part In pointList
greyHeight = 128 – part
GL.Begin(BeginMode.Lines)
GL.Vertex3(counterI, 0, counterJ)
GL.Vertex3(counterI, greyHeight, counterJ)
If counterI = Int(picWidth / 2) Then
counterJ += 1
counterI = -(Int(picWidth / 2))
Else
counterI += 1
End If
GL.End()
Next
End If
If ComboBox1.Text = “Polygons” Then
GL.Begin(BeginMode.Polygon)
For Each part In pointList
greyHeight = 128 – part
GL.Vertex3(counterI, greyHeight, counterJ)
If counterI = Int(picWidth / 2) Then
counterJ += 1
counterI = -(Int(picWidth / 2))
Else
counterI += 1
End If
Next
GL.End()
Else
GL.Begin(BeginMode.Points)
For Each part In pointList
greyHeight = 128 – part
GL.Vertex3(counterI, greyHeight, counterJ)
If counterI = Int(picWidth / 2) Then
counterJ += 1
counterI = -(Int(picWidth / 2))
Else
counterI += 1
End If
Next
GL.End()
End If
Else
GL.Begin(BeginMode.Polygon)
GL.Color3(Color.Red)
GL.Vertex3(25, -25, 25)
GL.Vertex3(-25, -25, 25)
GL.Vertex3(-25, -25, -25)
GL.Vertex3(25, -25, -25)
GL.Vertex3(25, -25, 25)
GL.End()
End If
‘Finally…
GraphicsContext.CurrentContext.VSync = True ‘Caps frame rate as to not over run GPU
GlControl1.SwapBuffers() ‘Takes from the ‘GL’ and puts into control
End Sub
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
‘Must be called to update the control window!
GlControl1.Invalidate()
If NumericUpDown1.Value = 360 Then
NumericUpDown1.Value = 0
End If
End Sub
Private Sub NumericUpDown2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown2.ValueChanged
‘Must be called to update the control window!
GlControl1.Invalidate()
If NumericUpDown2.Value = 360 Then
NumericUpDown2.Value = 0
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
GlControl1.Invalidate()
End Sub
Private Sub GlControl1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseUp
If (e.Button = Windows.Forms.MouseButtons.Right) Then
Go = False
LeftSet = False
TopSet = False
End If
End Sub
Private Sub GlControl1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Right) Then
Go = True
End If
End Sub
Private Sub GlControl1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseMove
GlControl1.Invalidate()
‘ Check if the mouse is down
If Go = True Then
‘ Set the mouse position
HoldLeft = (Control.MousePosition.X – Me.Left)
HoldTop = (Control.MousePosition.Y – Me.Top)
‘ Find where the mouse was clicked ONE TIME
If TopSet = False Then
OffTop = HoldTop – sender.Top
‘ Once the position is held, flip the switch
‘ so that it doesn’t keep trying to find the position
TopSet = True
End If
If LeftSet = False Then
OffLeft = HoldLeft – sender.Left
‘ Once the position is held, flip the switch
‘ so that it doesn’t keep trying to find the position
LeftSet = True
End If
End If
End Sub
Private Sub glcontrol_mousewheel(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseWheel
If e.Delta > 0 Then
EyeX += e.Delta
EyeY += e.Delta
Else
EyeX += e.Delta
EyeY += e.Delta
End If
Label3.Text = EyeX
GlControl1.Invalidate()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ProgressBar1.Value = 0
Drawn = False
GlControl1.Invalidate()
pointList.Clear()
RichTextBox1.Text = “”
picWidth = 0
picHieght = 0
EyeX = 300
EyeY = 50
Button1.Enabled = True
Button2.Enabled = True
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Dim MyDialog As New ColorDialog()
‘ Keeps the user from selecting a custom color.
MyDialog.AllowFullOpen = True
‘ Allows the user to get help. (The default is false.)
MyDialog.ShowHelp = True
‘ Sets the initial color select to the current text color,
MyDialog.Color = BGcolor
‘ Update the text box color if the user clicks OK
If (MyDialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
BGcolor = MyDialog.Color
GlControl1.Invalidate()
End If
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim MyDialog As New ColorDialog()
‘ Keeps the user from selecting a custom color.
MyDialog.AllowFullOpen = True
‘ Allows the user to get help. (The default is false.)
MyDialog.ShowHelp = True
‘ Sets the initial color select to the current text color,
MyDialog.Color = FGColor
‘ Update the text box color if the user clicks OK
If (MyDialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
FGColor = MyDialog.Color
GlControl1.Invalidate()
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim currentVeiw As System.Drawing.Bitmap = New Bitmap(716, 442)
Dim regtangle1 As System.Drawing.Rectangle = New Rectangle(0, 0, 716, 442)
GlControl1.DrawToBitmap(currentVeiw, regtangle1)
Dim myStream As System.IO.Stream
Dim saveFileDialog1 As New SaveFileDialog()
‘ERROR SAVEING
Dim bmp As New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
Dim data As System.Drawing.Imaging.BitmapData = bmp.LockBits(Me.ClientRectangle, System.Drawing.Imaging.ImageLockMode.[WriteOnly], System.Drawing.Imaging.PixelFormat.
Format24bppRgb)
GL.ReadPixels(0, 0, Me.ClientSize.Width, Me.ClientSize.Height, OpenTK.Graphics.OpenGL.PixelFormat.Bgr, PixelType.UnsignedByte, _
data.Scan0)
bmp.UnlockBits(data)
bmp.RotateFlip(RotateFlipType.RotateNoneFlipY)
bmp.Dispose()
saveFileDialog1.Filter = “Image Files(*.JPG)|*.JPG|All files (*.*)|*.*”
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = saveFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
bmp.Save(saveFileDialog1.FileName)
myStream.Close()
End If
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim saveFileDialog2 As New SaveFileDialog()
saveFileDialog2.Filter = “Text files (*.txt)|*.txt|All files (*.*)|*.*”
saveFileDialog2.RestoreDirectory = True
Try
If saveFileDialog2.ShowDialog() = Windows.Forms.DialogResult.OK Then
Me.RichTextBox1.SaveFile(saveFileDialog2.FileName, RichTextBoxStreamType.PlainText)
End If
Catch em As Exception
MsgBox(“An Exception has occured while saving file “, MsgBoxStyle.Critical, “Error”)
End Try
MessageBox.Show(“Saved”)
End Sub
End

Advertisements
Gray-scale to Surface Generator

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s