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

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

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Syracuse University 3D Framework using GDI+.Net Carmen Vaca Ruiz Independent Study Fall 2004 Instructor: Dr. Jim Fawcett.

Syracuse University

3D Framework using 3D Framework using GDI+ GDI+ .Net.Net

Carmen Vaca Ruiz

Independent Study

Fall 2004

Instructor: Dr. Jim Fawcett

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

Syracuse University

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 pointsPerspectiveTransformationsLightingSphere construction using a triangle mesh

Implementation will use . . . GDI+

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

Syracuse University

.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:

LinesEllipsesRectanglesPollygons: filled with some given color

In addition to the Graphics class, we have PenBrush, and Font classes.

Each of these classes provides members that let you draw and fill shapes.

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

Syracuse University

.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

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

Syracuse University

.NET GDI+ : The Paint event

After we’ve got the Graphics resources managed properly, we have another issue

Cover and Uncover the formResizing the form

Windows asks a forms to redraw newly content via the Paint event, which provides a PaintEventArgs argumentprivate void Form_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

{

Graphics g = e.Graphics;

. . .

}

Demo ….

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

Syracuse University

.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);

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

Syracuse University

.NET GDI+ : Y coordinatesThe 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);

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

Syracuse University

.NET GDI+ : Y coordinates

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

Syracuse University

.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);

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

Syracuse University

3D Framework

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

Syracuse University

Module Diagram

TRANSFORMATIONModule

3DREPRESENTATIONSModule

INTERFACEModule

SORTINGModule

VECTOR_FUNCTIONSModule

ANGLE_FUNCTIONSModule

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

Syracuse University

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.

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

Syracuse University

Class Design

Object3D

Coordinates3D

UserInterface

Sphere Shape

TriangleShape

Translation RotationX RotationY RotationZ Scale

QuickSort

TriangleQuickSort

Transformation

Angle

Projection

VectorFunctions ColorFunctions

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

Syracuse University

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.

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

Syracuse University

Transformations

Transformations defined in 3D are:ProjectionRotationTranslationScale

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!!!

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

Syracuse University

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.

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

Syracuse University

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

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

Syracuse University

Projections : some math

Simple projection: Z=0

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

Syracuse University

Projections : some math

Project again from (x,y) to (xp, yp)

Is it z involved in the new projection?

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

Syracuse University

Projections : some math

z is there !!!

Connect original and projected point: Betha will help us!

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

Syracuse University

Projections : Cabinet Projection

Coordinates3d my3DPoint; . . .Projection project = new Projection();PointF pointProjected = project.transform(my3DPoint)

Cabinet Projection

Lambda= 0.5Alpha =30, 60

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

Syracuse University

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])));

}

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

Syracuse University

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.

x

y

z

x

y

z

x

y

z

+ve

+ve

+ve

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

Syracuse University

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

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

Syracuse University

Spheres

Parametric equations of the sphere are a useful representation for our purposes

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

Syracuse University

Spheres

Rotating phi and theta we can get the vertices for the triangles on the sphere surface

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

Syracuse University

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);

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

Syracuse University

Light

How can we add light effects

Triangles again!!!

Easy method:Obtain 2 vectors using the vertices of the triangleObtain the normal of the plane given by those vectorsObtain the center of the trianleDefine a line between the center of the triangle and the light source (L)Obtain the angle between the normal and LSet the brightness of the triangle color according to the angle

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

Syracuse University

Light

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

Syracuse University

Light

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

Syracuse University

Light

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

Syracuse University

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);

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

Syracuse University

Setting the color

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

Syracuse University

Setting the color

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

Syracuse University

Representing spheres, using light

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

Syracuse University

Representing spheres, using light

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

Syracuse University

Representing spheres, using light

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

Syracuse University

References

Dr. Fawcett guidancehttp://www.javaworld.com/javaworld/jw-06-1997/jw-06-howto.htmlhttp://www.javaworld.com/javaworld/jw-07-1997/jw-07-howto_p.htmlhttp://www.c-sharpcorner.com/Graphics/ThreeDRotationMG.asphttp://www.bobpowell.net/RGBHSB.htm