290 likes | 412 Vues
This practical session for COS 397 focuses on implementing advanced solid shapes using OpenGL. Students will work on four tasks: updating their graphics library, creating a prism, constructing a truncated cone, and completing a pencil task. Key functions involve defining and drawing solid shapes, setting up lighting, and ensuring depth buffer functionality. Code examples and snippets are provided, highlighting vertex coordinates and normal vectors necessary for rendering accurate 3D objects with proper lighting effects. Students must copy and integrate library files into their project folders for successful implementation.
E N D
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 • 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>
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 …
Cube • Side from X– • Has vertices glVertex(x–a,y±a,z±a)alternating signs ± (–,–) → (–,+) → (+,+) → (+,–) –,+ +,+ –,– +,–
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)
Normal Vectors • Normal vectors • Axis Oriented • For sideX+ • glNormal3f(1,0,0) • For sideZ– • glNormal3f(0,0,-1)
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 )
Circle • Approximated by prism with N>30 • Vertex coordinates • Angle α, in range • Step • Vertex coordinates for polygon
Circle • Parallel to OXY: • Parallel to OXZ: • Parallel to OYZ:
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)
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)
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
Surface normal vectors calculation α+α α+½α α
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
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
βnorm • Normal vector coordinates B βnorm h M A H βnorm r
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
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; • } • }
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; • }
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
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; • } }
Task № 4: Pencil • Using functions: • drawSolidCylinder , drawSolidPrism, drawTruncatedCone • and drawSolidCone • Define class for compound object Pencil
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); • } • };
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; • }