330 likes | 417 Views
COS 397 Computer Graphics Practical Session № 2. Points and Vectors. Outline. Task 1 : COS397 Library Task 2 : Particles inside in a Cube Task 3 : Particles on a cube surface Task 4: Rings Task 5: Particles on Sphere surface Task 6: Particles inside in a Sphere
E N D
COS 397 Computer Graphics Practical Session №2 Points and Vectors
Outline • Task 1:COS397Library • Task 2:Particles inside in a Cube • Task 3:Particles on a cube surface • Task 4:Rings • Task 5:Particles on Sphere surface • Task 6: Particles inside in a Sphere • Task 7: Vectors on Sphere surface • Task 8:Twisted Vectors on Sphere surface
Task №1 COS397 Library • 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>
Task №1 - COS397LIB.h • #ifndefCOS397LIB_H_INCLUDED • #define COS397LIB_H_INCLUDED • struct COS397_POINT • { • float x; • float y; • float z; • } ; • bool running(); • void init(); • void finit(); • float random(float a, float b); • void drawCube( float x, float y, float z, float a ); • void drawOxyz(); • COS397_POINT point( float x, float y, float z ); • float distance( COS397_POINT a, COS397_POINT b ); • #endif // COS397LIB_H_INCLUDED
Task №1 - COS397LIB.cpp • #include <cstdlib> • #include <math.h> • #include <GL/glfw.h> • #include "cos397lib.h" • float random(float a, float b) • { • return (rand()/(float)RAND_MAX)*(b-a)+a; • } • bool running() • { • return( !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED) ); • }
Task №1 - COS397LIB.cpp • void init() • { • int width, height; • glfwInit(); • if( !glfwOpenWindow( 640, 480, 0, 0, 0, 0, 0, 0, GLFW_WINDOW ) ) return; • glfwGetWindowSize( &width, &height ); • height = height > 0 ? height : 1; • glViewport( 0, 0, width, height ); • glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); • glMatrixMode( GL_PROJECTION ); • glLoadIdentity(); • gluPerspective( 65.0f, (GLfloat)width/(GLfloat)height, 1.0f, 100.0f ); • glMatrixMode( GL_MODELVIEW ); • glLoadIdentity(); • gluLookAt(0.0f, -10.0f, 0.0f, • 0.0f, 0.0f, 0.0f, • 0.0f, 0.0f, 1.0f ); • } • void finit() • { • glfwTerminate(); • }
Task №1 - COS397LIB.cpp • void drawCube (float x,floaty,floatz,float a) • { a = a/2; • glBegin( GL_LINE_LOOP ); • glVertex3f(x-a, y-a, z-a); • glVertex3f(x-a, y-a, z+a); • glVertex3f(x+a, y-a, z+a); • glVertex3f(x+a, y-a, z-a); • glEnd(); • glBegin( GL_LINE_LOOP ); • glVertex3f(x-a, y+a, z-a); • glVertex3f(x-a, y+a, z+a); • glVertex3f(x+a, y+a, z+a); • glVertex3f(x+a, y+a, z-a); • glEnd(); • glBegin( GL_LINES ); • glVertex3f(x-a, y-a, z-a); • glVertex3f(x-a, y+a, z-a); • glVertex3f(x-a, y-a, z+a); • glVertex3f(x-a, y+a, z+a); • glVertex3f(x+a, y-a, z+a); • glVertex3f(x+a, y+a, z+a); • glVertex3f(x+a, y-a, z-a); • glVertex3f(x+a, y+a, z-a); • glEnd(); • }
Task №1 - COS397LIB.cpp • void drawOxyz() • { • // Drawing axis • glBegin( GL_LINES ); • // OX • glVertex3f( 0.0, 0.0, 0.0 ); • glVertex3f( 5.0, 0.0, 0.0 ); • // OY • glVertex3f( 0.0, 5.0, 0.0 ); • glVertex3f( 0.0, 0.0, 0.0 ); • // OZ • glVertex3f( 0.0, 0.0, 5.0 ); • glVertex3f( 0.0, 0.0, 0.0 ); • glEnd(); • // Drawing arrows • glBegin( GL_TRIANGLES ); • // OX • glVertex3f( 5.0, 0.0, 0.0 ); • glVertex3f( 4.5, 0.2, 0.0 ); • glVertex3f( 4.5,-0.2, 0.0 ); • // OY • glVertex3f( 0.0, 5.0, 0.0 ); • glVertex3f( 0.2, 4.5, 0.0 ); • glVertex3f(-0.2, 4.5, 0.0 ); • // OZ • glVertex3f( 0.0, 0.0, 5.0 ); • glVertex3f(+0.14,-0.14, 4.5 ); • glVertex3f(-0.14,+0.14, 4.5 ); • glEnd(); • }
Task №1 - COS397LIB.cpp • COS397_POINT point( float x, float y, float z) • { • COS397_POINT p; • p.x = x; • p.y = y; • p.z = z; • return( p ); • } • float distance( COS397_POINT a, COS397_POINT b) • { • float dx = a.x-b.x; • float dy = a.y-b.y; • float dz = a.z-b.z; • return( sqrt(dx*dx+dy*dy+dz*dz) ); • }
Task №2 Cloud of points inside a cube • Drawn random points inside a cube • Implementation • Array of graphical objects
#include <cstdlib> • #include <GL/glfw.h> • #include “COS397lib.h“ • COS397_POINT* points; • intmain() • { • init(); • int n = 2013; • points = new COS397_POINT[n]; • for( inti=0; i<n; i++) • { • points[i].x = random( -3, +3 ); • points[i].y = random( -3, +3 ); • points[i].z = random( -3, +3 ); • } • while( running() ) • { • glClear( GL_COLOR_BUFFER_BIT ); • glRotatef( 0.1, 0.4, -0.2, 0.7); • drawOxyz(); • drawCube(0,0,0,6); • glBegin( GL_POINTS ); • for( inti=0; i<n; i++ ) • glVertex3f( points[i].x, points[i].y, points[i].z ); • glEnd(); • glfwSwapBuffers(); • } • delete[] points; • finit(); • return 0; • } Solution
Task № 3:Particles on a Cube Surface Drawn random points on cube surface
#include <cstdlib> • #include <GL/glfw.h> • #include "COS397lib.h" • COS397_POINT* points; • intmain() • { • init(); • int n = 2013*10; • points = new COS397_POINT[n]; • for( inti=0; i<n; i++) • { • points[i].x = random( -3, +3 ); • points[i].y = random( -3, +3 ); • points[i].z = random( -3, +3 ); • switch( rand()%6 ) • { • case 0: points[i].x = +3; break; • case 1: points[i].x = -3; break; • case 2: points[i].y = +3; break; • case 3: points[i].y = -3; break; • case 4: points[i].z = +3; break; • case 5: points[i].z = -3; break; • } • } Solution while( running() ) { glClear( GL_COLOR_BUFFER_BIT ); glRotatef( 0.1, 0.4, -0.2, 0.7); drawOxyz(); drawCube(0,0,0,6); glBegin( GL_POINTS ); for( inti=0; i<n; i++ ) glVertex3f( points[i].x, points[i].y, points[i].z ); glEnd(); glfwSwapBuffers(); } delete[] points; finit(); return 0; }
Task №4 Rings • Drawing rings • One ring for each plane in the Cartesian coordinate system • How many ring do we need to draw?
Solution • #include <cstdlib> • #include <math.h> • #include <GL/glfw.h> • #include "COS397lib.h" • COS397_POINT* points; • int main() • { • init(); • int n = 2013; • float r = 3; • points = new COS397_POINT[n]; • for( inti=0; i<n; i++) • { • float alpha = random( 0, 2*M_PI ); • float u = r*cos(alpha); • float v = r*sin(alpha); • switch( rand()%3 ) • { • case 0: points[i] = point( u, v, 0 ); break; • case 1: points[i] = point( u, 0, v ); break; • case 2: points[i] = point( 0, u, v ); break; • } • } while( running() ) { glClear( GL_COLOR_BUFFER_BIT ); glRotatef( 0.1, 0.4, -0.2, 0.7); drawOxyz(); glBegin( GL_LINE_LOOP ); glVertex3f( -3,-3,0 ); glVertex3f( -3,+3,0 ); glVertex3f( +3,+3,0 ); glVertex3f( +3,-3,0 ); glEnd(); glBegin( GL_LINE_LOOP ); glVertex3f( -3,0,-3 ); glVertex3f( -3,0,+3 ); glVertex3f( +3,0,+3 ); glVertex3f( +3,0,-3 ); glEnd(); glBegin( GL_LINE_LOOP ); glVertex3f( 0,-3,-3 ); glVertex3f( 0,-3,+3 ); glVertex3f( 0,+3,+3 ); glVertex3f( 0,+3,-3 ); glEnd(); glBegin( GL_POINTS ); for( inti=0; i<n; i++ ) glVertex3f( points[i].x, points[i].y, points[i].z ); glEnd(); glfwSwapBuffers(); } delete[] points; finit(); return 0; }
Task 5: Particles on a Sphere surface • Drawn random points on a Sphere surface
Z P r z O β y x X α Y Q
Solution • #include <cstdlib> • #include <math.h> • #include <GL/glfw.h> • #include "COS397lib.h" • COS397_POINT* points; • int main() • { • init(); • int n = 2013*10; • float r = 3; • points = new COS397_POINT[n]; • for( inti=0; i<n; i++) • { • float alpha = random( 0, 2*M_PI ); • float beta = random( -M_PI, M_PI ); • points[i].x = r*cos(alpha)*sin(beta); • points[i].y = r*sin(alpha)*sin(beta); • points[i].z = r*cos(beta); • } while( running() ) { glClear( GL_COLOR_BUFFER_BIT ); glRotatef( 0.1, 0.4, -0.2, 0.7); drawOxyz(); drawCube(0,0,0,2*r); glBegin( GL_POINTS ); for( inti=0; i<n; i++ ) glVertex3f( points[i].x, points[i].y, points[i].z ); glEnd(); glfwSwapBuffers(); } delete[] points; finit(); return 0; }
Task 6: Cloud inside in a Sphere Drawn random points inside in a Sphere
Solution • #include <cstdlib> • #include <iostream> • #include <math.h> • #include <time.h> • #include <GL/glfw.h> • #include "COS397lib.h" • COS397_POINT* points; • intmain() • { • init(); • int n = 2013*10; • int k = 0; • float r = 3; • COS397_POINT o = point(0,0,0); • srand ( time(NULL) ); • points = new COS397_POINT[n]; • for( inti=0; i<n; i++) • { • points[k] = point( random(-r,r), random(-r,r), random(-r,r) ); • if( distance(points[k],o)<=r ) • { • k++; • } • } • std::cout << "Total points " << n << "\n"; • std::cout << "Inside sphere " << k << "\n"; • std::cout << "Estimated pi = " << 6.0*k/n << "\n"; while( running() ) { glClear( GL_COLOR_BUFFER_BIT ); glRotatef( 0.1, 0.4, -0.2, 0.7); drawOxyz(); drawCube( 0, 0, 0, 2*r ); glBegin( GL_POINTS ); for( inti=0; i<k; i++ ) glVertex3f( points[i].x, points[i].y, points[i].z ); glEnd(); glfwSwapBuffers(); } delete[] points; finit(); return 0; }
Task 7: Vectors on a Sphere surface • Drawnvectors in random points on a Sphere surface
Solution • #include <cstdlib> • #include <math.h> • #include <GL/glfw.h> • #include "COS397lib.h" • COS397_POINT* points; • int main() • { • init(); • int n = 2013 • float r = 3.5; • float d = 0.5; • float k = (r+d)/r; • points = new COS397_POINT[n]; • for( inti=0; i<n; i++) • { • float alpha = random( 0, 2*M_PI ); • float beta = random( -M_PI, M_PI ); • points[i].x = r*cos(alpha)*sin(beta); • points[i].y = r*sin(alpha)*sin(beta); • points[i].z = r*cos(beta); • } while( running() ) { glClear( GL_COLOR_BUFFER_BIT ); glRotatef( 0.1, 0.4, -0.2, 0.7); drawOxyz(); glBegin( GL_LINES ); for( inti=0; i<n; i++ ) { glVertex3f( points[i].x, points[i].y, points[i].z ); glVertex3f( k*points[i].x, k*points[i].y, k*points[i].z ); } glEnd(); glfwSwapBuffers(); } delete[] points; finit(); return 0; }
Task 8: Twist Vectors on a Sphere surface • Drawntwist vectors in random points on a Sphere surface
Solution • #include <cstdlib> • #include <iostream> • #include <math.h> • #include <time.h> • #include <GL/glfw.h> • #include "COS397lib.h" • COS397_POINT* points; • intmain() • { • init(); • int n = 2000; • float r = 3.5; • float d = 0.5; • points = new COS397_POINT[2*n]; • for( inti=0; i<n; i++) • { • float alpha = random( 0, 2*M_PI ); • float beta = random( -M_PI, M_PI ); • points[i].x = r*cos(alpha)*sin(beta); • points[i].y = r*sin(alpha)*sin(beta); • points[i].z = r*cos(beta); • points[n+i].x = (r+d)*cos(alpha+0.3)*sin(beta); • points[n+i].y = (r+d)*sin(alpha+0.3)*sin(beta); • points[n+i].z = (r+d)*cos(beta); • } while( running() ) { glClear( GL_COLOR_BUFFER_BIT ); glRotatef( 0.1, 0.4, -0.2, 0.7); glBegin( GL_LINES ); for( inti=0; i<n; i++ ) { glVertex3f( points[i].x, points[i].y, points[i].z ); glVertex3f( points[n+i].x, points[n+i].y, points[n+i].z ); } glEnd(); glfwSwapBuffers(); } delete[] points; finit(); return 0; }