1 / 29

COS 397 Computer Graphics Practical Session № 3

COS 397 Computer Graphics Practical Session № 3. Solid Shapes . Outline. Task 1 : Updated Library for COS397 Task 2 : Prism Task 3 : Truncated Cone Task 4 : Pencil. Task № 1 . We are going to use user defined library COS397.cpp COS397.h

lamya
Download Presentation

COS 397 Computer Graphics Practical Session № 3

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. COS 397 Computer Graphics Practical Session №3 Solid Shapes

  2. Outline • Task 1:Updated Library for COS397 • Task 2:Prism • Task 3:Truncated Cone • Task 4:Pencil

  3. Task №1 • We are going to use user defined library • COS397.cpp • COS397.h • You need to copy these files in your project folder and to attach them to the project • In main file should be included by • #include <COS397.h>

  4. Modifications in void init() • glfwOpenWindow( 640, 480, 0, 0, 0, 0, 8, 0, GLFW_WINDOW) • This value sets the depth buffer. • It should be greater than 0, and depends on graphical card properties. • Some often used values: 8, 16, 32 …

  5. Cube • Side from X– • Has vertices glVertex(x–a,y±a,z±a)alternating signs ± (–,–) → (–,+) → (+,+) → (+,–) –,+ +,+ –,– +,–

  6. Cube’s sides: • fromX–glVertex(x–a,y±a,z±a) • fromX+glVertex(x+a,y±a,z±a) • fromY–glVertex(x±a,y–a,z±a) • fromY+glVertex(x±a,y+a,z±a) • fromZ–glVertex(x±a,y±a,z–a) • fromZ+glVertex(x±a,y±a,z+a)

  7. Normal Vectors • Normal vectors • Axis Oriented • For sideX+ • glNormal3f(1,0,0) • For sideZ– • glNormal3f(0,0,-1)

  8. Lightening • Enabling three settings • General lightening enabling • glEnable( GL_LIGHTING ) • Using the object color in lightening glEnable( GL_COLOR_MATERIAL ) • Turning on one light source (numbering starts from 0)glEnable( GL_LIGHT0 )

  9. Circle • Approximated by prism with N>30 • Vertex coordinates • Angle α, in range • Step • Vertex coordinates for polygon

  10. Circle • Parallel to OXY: • Parallel to OXZ: • Parallel to OYZ:

  11. Cone • For circle approximation of the base we use polygon with N sides • Generatenhorizontal andn vertical triangles (x,y,z+h) (0,0,h) Cone in point (0,0,0) Cone in point (x,y,z) (x,y,z) (0,0,0) (x+x2, y+y2, z) (x2, y2,0) (x+x1, y+y1, z) (x1, y1,0)

  12. Cylinder • Like cone with slight modifications (0,0,h) (x,y,z+h) (x+x2, y+y2, z+h) (x2, y2, h) (x1, y1, h) (x+x1, y+y1, z+h) Cylinder in point (0,0,0) Cylinder in point (x,y,z) (0,0,0) (x,y,z) (x+x2, y+y2, z) (x2, y2, 0) (x1, y1, 0) (x+x1, y+y1, z)

  13. Solid cylinder • Lightening • Normal vectors for sides • Normal vectors should be unit vectors, i.e. with length 1 • For bottom base • For tom base • For surface

  14. Surface normal vectors calculation α+α α+½α α

  15. Solid Cone • Lightening • Global problem – surfacenormal vectors calculation • We use a sphere with radius 1 and center (0,0,0)and calculate vectors from point (0,0,0) to the sphere surface points • We need to calculate two angles

  16. Horizontal angle αnorm • Like for cylinder surface • Vertical angle βnorm • More complicated • We use transformation from Spherical to Cartesian coordinate system • sin(βnorm)and cos(βnorm) should be calculated

  17. βnorm • Normal vector coordinates B βnorm h M A H βnorm r

  18. Result Normal vector

  19. Task № 2: Prism • Using functions: • void drawCylinder ( float x, float y, float z, float r, float h ); • Void drawSolidCylinder ( float x, float y, float z, float r, float h ); • Define functions • drawPrism and drawSolidPrism

  20. void drawSolidPrism( float x, float y, float z, float r, float h , int n ) • { float alpha = 0.0; • float dalpha = 2*M_PI/n; • for( inti=0; i<n; i++) • { • float dx1 = r*cos(alpha); • float dy1 = r*sin(alpha); • float dx2 = r*cos(alpha+dalpha); • float dy2 = r*sin(alpha+dalpha); • glBegin( GL_POLYGON ); • glNormal3f( cos(alpha+dalpha/2), sin(alpha+dalpha/2), 0 ); • glVertex3f( x+dx1, y+dy1, z ); • glVertex3f( x+dx2, y+dy2, z ); • glVertex3f( x+dx2, y+dy2, z+h ); • glVertex3f( x+dx1, y+dy1, z+h ); • glEnd(); • glBegin( GL_POLYGON ); • glNormal3f( 0, 0, -1 ); • glVertex3f( x, y, z ); • glVertex3f( x+dx1, y+dy1, z ); • glVertex3f( x+dx2, y+dy2, z ); • glEnd(); • glBegin( GL_POLYGON ); • glNormal3f( 0, 0, 1 ); • glVertex3f( x, y, z+h ); • glVertex3f( x+dx1, y+dy1, z+h ); • glVertex3f( x+dx2, y+dy2, z+h ); • glEnd(); • alpha += dalpha; • } • }

  21. int main() • { • init(); • glClearColor( 1.0f, 1.0f, 1.0f, 1.0f ); • glEnable( GL_DEPTH_TEST ); • glEnable( GL_LIGHTING ); • glEnable( GL_COLOR_MATERIAL ); • glEnable( GL_LIGHT0 ); • while( running() ) • { • glClear( GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT ); • glRotatef( 0.05, 0.4, -0.2, 0.7); • glColor3ub( 255, 255, 0 ); //yellow • drawSolidPrism( 0, 0, -3, 0.3, 6,6 ); • glfwSwapBuffers(); • } • finit(); • return 0; • }

  22. Task № 3: Truncated Cone • Using function: • Void drawSolidCone( float x, float y, float z, float r, float h ); • Define function • Void drawTruncatedCone( float x, float y, float z, • float r, float r1, float h ); h r1 r

  23. void drawTruncatedCone( float x, float y, float z, float r, float r1, float h ) • { int n = 32; • float h1 = h - h*r1/r; • float alpha = 0.0; • float dalpha = 2*M_PI/n; • for( inti=0; i<n; i++) • { • float dx1 = r*cos(alpha); • float dy1 = r*sin(alpha); • float dx2 = r*cos(alpha+dalpha); • float dy2 = r*sin(alpha+dalpha); • float dx3 = r1*cos(alpha); • float dy3 = r1*sin(alpha); • float dx4 = r1*cos(alpha+dalpha); • float dy4 = r1*sin(alpha+dalpha); • float cosbeta = h/sqrt(h*h+r*r); • float sinbeta = r/sqrt(h*h+r*r); • float nx = cos(alpha+dalpha/2)*cosbeta; • float ny = sin(alpha+dalpha/2)*cosbeta; • float nz = sinbeta; • // surface • glBegin( GL_POLYGON ); • glNormal3f( nx, ny, nz ); • glVertex3f( x+dx1, y+dy1, z ); • glVertex3f( x+dx2, y+dy2, z ); • glVertex3f( x+dx4, y+dy4, z+h1 ); • glVertex3f( x+dx3, y+dy3, z+h1 ); • glEnd(); • // bottom • glBegin( GL_POLYGON ); • glNormal3f( 0, 0, -1 ); • glVertex3f( x, y, z ); • glVertex3f( x+dx1, y+dy1, z ); • glVertex3f( x+dx2, y+dy2, z ); • glEnd(); • //top • glBegin( GL_POLYGON ); • glNormal3f( 0, 0, 1 ); • glVertex3f( x, y, z+h1 ); • glVertex3f( x+dx3, y+dy3, z+h1 ); • glVertex3f( x+dx4, y+dy4, z+h1 ); • glEnd(); • alpha += dalpha; • } }

  24. Task № 4: Pencil • Using functions: • drawSolidCylinder , drawSolidPrism, drawTruncatedCone • and drawSolidCone • Define class for compound object Pencil

  25. class Pencil{ • private: float s, x,y,z,t; • public: • Pencil(float x1, float y1, float z1, float s1, float t1) • { s = s1; //length • x=x1; • y=y1; • z=z1; • t=t1; //thin • } • void drawPencil() • { • glColor3ub( 225, 200, 0 ); //yellow • drawSolidPrism(x,y,z,t,s,6); • glColor3ub(200, 200, 200 ); //gray • float metal=s/10.0; • drawSolidPrism(x,y,z-metal,t*1.1,metal,18); • glColor3ub(225, 125, 225 ); //pink • float rubber=s/12.0; • drawSolidPrism(x,y,z-metal-rubber,t*0.9,rubber,35); • glColor3ub(250, 215, 145 ); //beige • float wood=s/5.0; • drawTruncatedCone(x,y,z+s,t*0.95,t/3,wood); • glColor3ub(5, 5, 5); //black • float graphite=s/15.0; • drawSolidCone(x,y,z+s+wood-graphite,t/3,graphite); • } • };

  26. int main() • { • init(); • Pencil p1 = Pencil(0,0,-3,6,0.3); • glClearColor( 1.0f, 1.0f, 1.0f, 1.0f ); • glEnable( GL_DEPTH_TEST ); • glEnable( GL_LIGHTING ); • glEnable( GL_COLOR_MATERIAL ); • glEnable( GL_LIGHT0 ); • while( running() ) • { • glClear( GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT ); • glRotatef( 0.05, 0.4, -0.2, 0.7); • p1.drawPencil(); • glfwSwapBuffers(); • } • finit(); • return 0; • }

  27. Questions?

More Related