160 likes | 170 Views
Learn about scan conversion, Bresenham's algorithm, flood fill, and OpenGL concepts. Implement recursive flood fill in C#.
E N D
Line Drawing • Algorithm: • Draw a straight line between two points • Set all pixels along line to foreground color • Set all other pixels to background color
Line Drawing • Algorithm (Bresenham):
Fill Algorithms • Goal: • Fill all pixels inside contourwith foreground color Seed point
Fill Algorithms • Flood Fill: • Start from seed point • Recursively fill neighboring pixels if not contour pixels
Fill Algorithms • Flood Fill: • Start from seed point • Recursively fill 4 neighboring pixels if not contour pixels Flood_fill (x, y, bg, fg) if (get_pixel(x, y) == bg) put_pixel(x, y, fg); Flood_fill (x+1, y, bg, fg); Flood_fill (x-1, y, bg, fg); Flood_fill (x, y+1, bg, fg); Flood_fill (x, y-1, bg, fg);
Fill Algorithms • Goal: • Fill all pixels inside contourwith foreground color
Fill Algorithms • Goal: • Fill all pixels inside contourwith foreground color Problem: • Narrow areas Solution: • Include diagonally connected pixels(8 neighbors)
Fill Algorithms • Goal: • Fill all pixels inside contourwith foreground color
Fill Algorithms • Goal: • Fill all pixels inside contourwith foreground color Flood_fill (x, y, bg, fg) if (get_pixel(x, y) == bg) put_pixel(x, y, fg); Flood_fill (x+1, y, bg, fg); Flood_fill (x-1, y, bg, fg); Flood_fill (x, y+1, bg, fg); Flood_fill (x, y-1, bg, fg); Flood_fill (x+1, y+1, bg, fg); Flood_fill (x+1, y-1, bg, fg); Flood_fill (x-1, y+1, bg, fg); Flood_fill (x-1, y-1, bg, fg);
Task 5.1 • Goal: Implement recursive flood fill • Use Program.cs from Homework 2 • Add functionColor get_pixel (int x, int y, intsize_x, intsize_y, byte[] array); • Add functionvoid flood_fill (int x, int y, intsize_x, intsize_y, Color bg, Color fg, byte[] array); • In Main function: • Comment out draw_line statements • Use same color for both gradient colors • Call: flood_fill (size_x/2, size_y/2, size_x, size_y,your_frame_color, your_fill_color, pixels);
Recap: OpenGL • Concepts • GL.Clear (ClearBufferMask mask)clears frame buffer and selects type, e.g., ColorBufferBit • GL.MatrixMode(MatrixMode.Modelview);GL.LoadMatrix(ref modelview);selects and loads transformation matrix • GL.Begin(PrimitiveType.Triangles); GL.Color3(1.0f, 1.0f, 0.0f); GL.Vertex3(-1.0f, -1.0f, 4.0f); GL.Color3(1.0f, 0.0f, 0.0f); GL.Vertex3(1.0f, -1.0f, 4.0f); GL.Color3(0.2f, 0.9f, 1.0f); GL.Vertex3(0.0f, 1.0f, 4.0f);GL.End();creates a triangle • SwapBuffers();
Task 4 • Goal: Create OpenGL program • In Visual Studio, open "New Project" dialog and select "Open Visual Studio Installer". • Under "Individual Components",find "NuGet Package Manager”. • Create new project called "OpenGL" • Open in Tools->NuGet Package Manager:NuGet Package Manager Console • At the prompt, type:Install-Package OpenTK -Version 2.0.0 • Overwrite “Program.cs” with “opengl.txt” from http://tinyurl.com/dig1705
Task 5.2 • Goal: Add rotation to OpenGL program • Add the following declarations to the beginning of the Game class:float angle = 0.0f; static Vector3 axis; • Add the following line to Main: axis = new Vector3(0.0f, 1.0f, 0.0f); // y axis • Add the following lines to OnRenderFramebelowGL.LoadMatrix(ref modelview);:GL.Translate(0.0f, 0.0f, 4.0f); GL.Rotate(angle, axis); GL.Translate(0.0f, 0.0f, -4.0f); angle += 1.0f; if (angle >= 360.0f) angle = 0.0f;
Questions? Joerg Meyer, Ph.D. Miami Dade College Wolfson Campus jmeyer2@mdc.edu http://joergmeyer.ddns.net/COURSES/DIG1705