Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CG Lab Exercise 5 Computer Graphics Lab Exercise 5: A Camera for Simple Parallel Projection This week we construct a module for the simplest kind of projection: orthographic parallel projection. This is the kind of projection used in engineering drawings, to produce "plan views". The projection is as follows: Given a point (x,y,z) the projection of that point is (x,y). (Alternatively, one could map to (x,z) or (y,z) depending on what kind of plan view is required). Write the following code: A simple camera module for managing point coordinates etc For rendering to screen For performing back face elimination Now load 3D objects from file Write a program that (a) loads objects from file and then (b) projects them to screen using the simple orthographic projection camera Your program will be assessed as part of your coursework (10%). Simple Camera Module Java Programming public class ParallelCamera{ public: int width, height; /*of the drawable in pixels*/ double xmin,xmax,ymin,ymax; /*a 2D window on the viewplane*/ double aX,bX,aY,bY; /*used to convert from 2D window -> display coordinates*/ /*assume that the viewport is whole of display area*/ Graphics myGraphics; /* used to store a Graphics Context for drawing purposes */ ParallelCamera(Graphics g, Component c); /* constructor for a new camera, with the given Graphics context g, this can be obtained by invoking getGraphics on your frame, however, your frame must already be visible on the screen. If you are using double buffering, g is the graphics context of your backBuffer. use setVisible(true) or show(). Component is the graphics component from which width and height can be retrieved Note that show() is deprecated from JDK 1.1 This function must assign the appropriate values to width and height, and appropriate defaults for the remaining parameters. */ void setWindowOnPCamera(double xmin, double xmax, double ymin, double ymax); /*sets the view plane window for this camera*/ void clickPCamera(); /*fixes the camera parameters according to whatever has been specified before, which remains in force until the next click, please recheck the boundaries of the clipping context to reset width and height*/} C Programming typedef struct{ DRAWABLE drawable; int width, height; /*of the drawable in pixels*/ double xmin,xmax,ymin,ymax; /*a 2D window on the viewplane*/ double aX,bX,aY,bY; /*used to convert from 2D window -> display coordinates*/ /*assume that the viewport is whole of display area*/ } ParallelCamera; /*function prototypes*/ ParallelCamera *newPCamera(DRAWABLE drawable); /* creates a new camera, with the given drawable, which must have been created. This function must assign the appropriate values to width and height, and appropriate defaults for the remaining parameters. */ void setWindowOnPCamera(ParallelCamera *camera, double xmin, double xmax, double ymin, double ymax); /*sets the view plane window for this camera*/ void clickPCamera(ParallelCamera *camera); /*fixes the camera parameters according to whatever has been specified before, which remains in force until the next click*/ Notes on Setting up the ParallelCamera The drawable is the physical medium on which the image is rendered, associated with the camera. It plays the role of the photographic film in a real camera. The boundary defined by [xmin, xmax, ymin, ymax] specifies a 2D window on the view plane. We will take the whole display area as the viewport, hence the viewport coordinates are (0,0) to (width-1, height -1). The values aX, bX, aY, bY are used to convert points on the 2D viewplane to the 2D display. In other words, if (x,y) is a point on the viewplane, and (x',y') the corresponding point on the display, then x' = aX + bX*x, and y' = aY + bY*y. These values are defined exactly as in the notes on 2D viewing. The function clickPCamera() simply computes the values of aX, bX, aY, bY given the values of the other parameters. Rendering Now we continue with the functions to actually render onto the drawable using this camera: Java programming /*function prototypes continued for ParallelCamera class*/ void displayFaceUsingPCamera(Face face); /*This displays the polygon associated with the face on the Graphics (context) of the camera*/ void displayObjectUsingPCamera(GObject object); /*This displays all the faces associated with the object on the Graphics (context) of the camera*/ c programming /*function prototypes continued*/ void displayFaceUsingPCamera(ParallelCamera *camera, Face *face); /*This displays the polygon associated with the face on the drawable of the camera*/ void displayObjectUsingPCamera(ParallelCamera *camera, GObject *object); /*This displays all the faces associated with the object on the DRAWABLE of the camera*/ Notes So the implementation of "displayFace" is on the following lines: Start off a new polygon on the drawable, using "newPolygonOnDrawable" or using the drawPolygon method of the Graphics context for Java programmers; Suppose (x,y,z) is a vertex of the face, then its projection to the view plane is (x,y); Convert this to display coordinates (x',y') = round(aX+bX*x, aY+bY*y); Use the "addToPolygonOnDrawable" function to send this vertex to the drawing area; When all the vertices of the polygon have been traversed in this way, use "displayPolygonOnDrawable". Problem is that this says nothing about the colour in which to draw the face. This can be easily rectified: Add a new entry to the Face data structure, and a corresponding function: typedef struct{ /*all the previous stuff, and ...*/ double red, green, blue; } Face; void setColourOfFace(Face *face, double red, double green, double blue); /*sets the colour of the face to be the (red, green, blue) mix*/ Remember to add a default colour setting in the newFace() function These class variables and methods can be easily added to the Face class for Java programmers. Before actually drawing the polygon call setColourOnDrawable(face->red, face->green, face->blue). To implement "displayObject", run through all faces in the object calling "displayFace". Back Face Elimination For any polyhedral object obviously not all of its faces can be simultaneously visible from any viewpoint. The kind of parallel projection we have defined is equivalent to looking on a line of sight down the positive Z axis, towards the origin (from infinitely far away). Back face elimination involves displaying only those polygons that are facing towards the viewpoint. Here is where we can make use of the plane equation of a face. Suppose the plane equation is l(x,y,z) = ax + by + cz -d = 0. Then we know that l(X,Y,Z) > 0 when (X,Y,Z) is "in front of" the polygon, ie, the polygon is facing towards (X,Y,Z). So suppose (0,0,Z) (Z>0) is a point on the Z axis. Then, l(0,0,Z) = cZ - d. We require this to be >0 for a visible face. Ie, cZ - d > 0, and hence d < cZ. Z is arbitrarily large for a parallel projection (it is at "infinity"). So the requirement d < cZ is equivalent to: if c > 0 then d can be anything (since d < cZ = +infinity is certainly true). if c < 0 then d must be < -infinity (impossible). if c = 0 then d must be negative. Hence the requirement is: display the face which has plane equation parameters (a,b,c,d) if and only if c > 0 or (c == 0 and d < 0). Loading objects from file You now need to write a new constructor for GObject which extends the present constructor to read in data from a file according to the specified format below. If you are not sure how to go about doing this, please ask the lab assistant GObject(String fileName, FaceArray farray, int maxChildren); /*Another constructor which reads in the object data from file "fileName"; */ The format is decribed here for a simple object. We are interested in loading a cube and a pyramid. cube.dat 8 number of vertices 1.0 0.0 1.0 vertex 0 2.0 0.0 1.0 vertex 1 2.0 0.0 0.0 vertex 2 1.0 0.0 0.0 vertex 3 1.0 1.0 1.0 vertex 4 2.0 1.0 1.0 vertex 5 2.0 1.0 0.0 vertex 6 1.0 1.0 0.0 vertex 7 6 number of faces 4 number of vertices in face 0 0 1 5 4 vertices in face 0 0.5 0.5 0.0 colour of face 4 1 2 6 5 0.5 0.0 0.0 colour of face 4 3 7 6 2 0.5 0.0 0.5 colour of face 4 0 4 7 3 0.0 0.5 0.5 colour of face 4 0 3 2 1 0.5 0.5 0.0 colour of face 4 4 5 6 7 1.0 0.0 0.0 colour of face pyramid.dat 5 number of vertices -2.0 0.0 1.0 vertex 0 -1.0 0.0 1.0 vertex 1 -1.0 0.0 0.0 vertex 2 -2.0 0.0 0.0 vertex 3 -1.5 2.0 0.5 vertex 4 5 number of faces 3 number of vertices in face 0 0 1 4 vertices in face 0 0.5 0.5 0.0 colour of face 3 1 2 4 0.5 0.0 0.0 colour of face 3 3 4 2 0.5 0.0 0.5 colour of face 3 0 4 3 0.0 0.5 0.5 colour of face 4 0 3 2 1 0.5 0.5 0.0 colour of face Note: colour information has been added, but assuming that you use a monochrome machine only, or only care about displaying as wire frame, this is not necessary. The colours given are suggested only, use whatever you like. Test Program A sample test program (ShowParallel.java) is available (and at /import/teaching/BSc/CG/lib-java). Please note that there are some small bugs in the code that will need fixing. If you can't fix it, please ask the TAs or your fellow students for advice. Display a parallel projection of the CUBE AND PYRAMID objects (assuming a plan view looking down the positive Z axis, towards the origin - hence (x,y,z) projects to (x,y). Such is the nature of orthographic parallel projection, this will be a rather boring view (you should see a square and an triangle). To make it more interesting, rotate the cube and pyramid about the Y axis (transformVerticesOfObject) eg, by 30 degrees (pi/6), clear the window and redisplay. Keep doing this, say 12 times, so that the objects should have rotated a full 360 degrees. Between each rotation, clear the display ("clearDrawable") . Important note: when you rotate the vertices of an object, obviously the old plane equations are no longer valid. So the transformation functions should take this into account. Click here to see an applet demonstrating what is expected A working implementation of test program represents a point of assessment in the coursework.