1 / 38

3D Framework using GDI+ .Net

3D Framework using GDI+ .Net. Carmen Vaca Ruiz Independent Study Fall 2004 Instructor: Dr. Jim Fawcett. Objective. The implementation of a 3D framework using C# gives us a combination for a promising application There are 4 main topics to cover: 3D Objects as a collection of points

tavi
Download Presentation

3D Framework using GDI+ .Net

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3D Framework using GDI+ .Net Carmen Vaca Ruiz Independent Study Fall 2004 Instructor: Dr. Jim Fawcett Syracuse University

  2. Objective • The implementation of a 3D framework using C# gives us a combination for a promising application • There are 4 main topics to cover: • 3D Objects as a collection of points • Perspective • Transformations • Lighting • Sphere construction using a triangle mesh • Implementation will use . . . GDI+ Syracuse University

  3. .NET GDI+ • GDI+ provides .NET developers with a simple-to-use wrapper around the graphics services provided by Windows. • The Graphics class provides the most important object you'll encounter when working with GDI+. With it you can draw: • Lines • Ellipses • Rectangles • Pollygons: filled with some given color • In addition to the Graphics class, we have • Pen • Brush, and • Font classes. Each of these classes provides members that let you draw and fill shapes. Syracuse University

  4. .NET GDI+ • Where do we get a graphics object? Graphics g = this.CreateGraphics(); • The graphics object holds a resource managed by Windows. We are responsible for releasing the resource (even when an exception ocurrs) try{ g.DrawEllipse( ... } finally{ g.Dispose(); } • Let’s use C# facilities for this: using( Graphics g = this.CreateGraphics() ) { g.FillEllipse(Brushes.DarkKhaki, 30, 30, 130,90); } // g.Dispose called automatically here Syracuse University

  5. .NET GDI+ : The Paint event • After we’ve got the Graphics resources managed properly, we have another issue • Cover and Uncover the form • Resizing the form • Windows asks a forms to redraw newly content via the Paint event, which provides a PaintEventArgs argument private void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; . . . } • Demo …. Syracuse University

  6. .NET GDI+ : Resizing • What happens when we resize the form? • Demo …. • Luckily, you can set a style to request that Windows redraw the entire form during a resize: this.SetStyle(ControlStyles.ResizeRedraw, true); Syracuse University

  7. .NET GDI+ : Y coordinates • The origin of the coordinates in GDI+ is located in the left upper corner of the screen. Thus, any y-coordinate is measured from top to bottom. • GDI+ provides transformations in 2D that we can use to avoid changing the sign of y-coordinates. g.Transform = new Matrix(1, 0, 0, -1, 0, 0); g.TranslateTransform(0, ClientRectangle.Height, MatrixOrder.Append); Syracuse University

  8. .NET GDI+ : Y coordinates Syracuse University

  9. .NET GDI+ : Origin position • The position of the origin should be in the center of the screen • We can use GDI+ transformations float width = this.ClientRectangle.Width/4; float heigth = this.ClientRectangle.Height/2; g.TranslateTransform(width, heigth); Syracuse University

  10. 3D Framework Syracuse University

  11. Module Diagram Syracuse University

  12. Important Modules Description • 3DRepresentation Module • This module provides the definition of classes that represent points, shapes and objects in three dimensions. • The classes will be defined in an incremental way. This approach will be useful when we apply transformations • Transformation Module • The transformation module contains most of the math involved for 3D • Projection • Translation • Scale • Rotation • Sorting Module The 3D objects will be rendered using Painter’s algorithm. We need a module to sort the objects. The closer objects will be drawn first. Syracuse University

  13. Class Design Syracuse University

  14. Class Design: Triangle Shape • TriangleShape derives from Shape. The triangle class will be useful in building a sphere. • This class override can override the method Draw of the base class by defining a particular way of rendering the triangle. • Light effects can be obtained by getting the angle between the triangle normal and the Light coordinates. The color is determined according to the angle. Syracuse University

  15. Transformations • Transformations defined in 3D are: • Projection • Rotation • Translation • Scale • The transformations allow us to move, rotate, scale objects in 3D. • Transformations are propagated from objects to shapes to coordinates3D • We’ll see this again later!!! Syracuse University

  16. Projections • Once we define a class to represent an object in 3D, we need to draw it in the screen , but • The screen is a 2D device. How do we get a 3D object represented in a 2D screen? • Here we need the first transformation defined in the 3D world: Projections. • If we project all the vertices of an object, we get the 3D representation we are looking for. Syracuse University

  17. Projections • How does a point in 3D look like in our program? square = new Shape(new Coordinates3D[]{ new Coordinates3D(30.0f, 0.0f, 0.0f), new Coordinates3D(30.0f, 0.0f, 30.0f), new Coordinates3D(60.0f, 0.0f, 30.0f), new Coordinates3D(60.0f, 0.0f, 0.0f), new Coordinates3D(30.0f, 0.0f, 0.0f) }); • Projection transformation takes a (x, y, z) point and returns its representation in 2D Syracuse University

  18. Projections : some math • Simple projection: Z=0 Syracuse University

  19. Projections : some math • Project again from (x,y) to (xp, yp) • Is it z involved in the new projection? Syracuse University

  20. Projections : some math • z is there !!! • Connect original and projected point: Betha will help us! Syracuse University

  21. Projections : Cabinet Projection Cabinet Projection Lambda= 0.5 Alpha =30, 60 Coordinates3d my3DPoint; . . . Projection project = new Projection(); PointF pointProjected = project.transform(my3DPoint) Syracuse University

  22. When do we apply the projection? • Remember that transformations are propagated from objects to shapes to points: Pen aPen = new Pen(linesColor, 1); Projection project = new Projection(); for (int i = 0; i < Corners.Count-1; i++) { g.DrawLine(aPen, project.transform(((Coordinates3D)Corners[i])), project.transform(((Coordinates3D)Corners[i+1]))); } Syracuse University

  23. Rotation • We can rotate objects by transforming all the points belonging to it. • We can derive the equations to rotate objects around any of the axis. Syracuse University

  24. Spheres • GDI+ provides a library for drawing in 2D • There are not primitives to draw a 3D object ( this includes spheres!) • Our sphere object will be built using triangles. • To get a good approximation the vertices of the triangles should be points in the sphere surface Syracuse University

  25. Spheres • Parametric equations of the sphere are a useful representation for our purposes Syracuse University

  26. Spheres • Rotating phi and theta we can get the vertices for the triangles on the sphere surface Syracuse University

  27. Again: Triangle Shape • TriangleShape derives from Shape. The triangle class will be useful in building a sphere. • Using the Draw method provided by Shape we will get a wireframe figure. We can, instead draw the triangle by using the FillPolygon function provided by GDI+. Projection project = new Projection(); PointF[] vertex = { project.transform(p1),project.transform(p2), project.transform(p3),project.transform(p1) }; Brush br = Get a new brush g.FillPolygon(br, vertex); Syracuse University

  28. Light • How can we add light effects • Triangles again!!! • Easy method: • Obtain 2 vectors using the vertices of the triangle • Obtain the normal of the plane given by those vectors • Obtain the center of the trianle • Define a line between the center of the triangle and the light source (L) • Obtain the angle between the normal and L • Set the brightness of the triangle color according to the angle Syracuse University

  29. Light Syracuse University

  30. Light Syracuse University

  31. Light Syracuse University

  32. Setting the color • There is an issue with setting the color according to the light in GDI+ • GDI+ provides functions to generate colors using RGB scheme. However, this scheme will give us different scales of gray. • We need to use a piece of code to setup the brightness of the color instead: Color myColor = Color.FromArgb((int)dot1,(int)dot1,(int)dot1); Color myColor = HSL.SetBrightness(Color.FromArgb(_r,_g,_b), dot0); Syracuse University

  33. Setting the color Syracuse University

  34. Setting the color Syracuse University

  35. Representing spheres, using light Syracuse University

  36. Representing spheres, using light Syracuse University

  37. Representing spheres, using light Syracuse University

  38. References • Dr. Fawcett guidance • http://www.javaworld.com/javaworld/jw-06-1997/jw-06-howto.html • http://www.javaworld.com/javaworld/jw-07-1997/jw-07-howto_p.html • http://www.c-sharpcorner.com/Graphics/ThreeDRotationMG.asp • http://www.bobpowell.net/RGBHSB.htm Syracuse University

More Related