1 / 18

COS 397 Computer Graphics Practical Session 1

COS 397 Computer Graphics Practical Session 1. Solutions. Task 3:. #include <GL/ glfw.h > int main() { int width, height; bool running = true; // // GLFW initialization // glfwInit (); // // Graphical Window opening and initialization //

yitro
Télécharger la présentation

COS 397 Computer Graphics Practical Session 1

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 1 Solutions

  2. Task 3: • #include <GL/glfw.h> • int main() • { • int width, height; • bool running = true; • // • // GLFW initialization • // • glfwInit(); • // • // Graphical Window opening and initialization • // • if( !glfwOpenWindow( 512, 512, 0, 0, 0, 0, 0, 0, GLFW_WINDOW ) ) • { • glfwTerminate(); • return 0; • }

  3. Task 3 (cont) • glfwSetWindowTitle("GLFW Application"); • glfwGetWindowSize( &width, &height ); • height = height > 0 ? height : 1; • glViewport( 0, 0, width, height ); • glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); • // // Projection Type Setting • // • glMatrixMode( GL_PROJECTION ); • glLoadIdentity(); • gluPerspective( 65.0f, (GLfloat)width/(GLfloat)height, 1.0f, 100.0f ); • // • // View Point Setting • // • glMatrixMode( GL_MODELVIEW ); • glLoadIdentity(); • gluLookAt(0.0f, -10.0f, 0.0f, • 0.0f, 0.0f, 0.0f, • 0.0f, 0.0f, 1.0f );

  4. Task 3 (cont) • // Main loop for frame by frame Animation generation • while(running) • { • // Buffer cleaning • glClear( GL_COLOR_BUFFER_BIT ); • // Drawing a frame in the hidden buffer • glRotatef(1, 0.25f, 1.0f, 0.75f); • glBegin( GL_TRIANGLES ); • glColor3f(0.1f, 0.0f, 0.0f ); • glVertex3f(0.0f, 3.0f, -4.0f); • glColor3f(0.0f, 1.0f, 0.0f ); • glVertex3f(3.0f, -2.0f, -4.0f); • glColor3f(0.0f, 0.0f, 1.0f ); • glVertex3f(-3.0f, -2.0f, -4.0f); • glEnd(); • glBegin( GL_TRIANGLES ); • glColor3f(0.0f, 0.1f, 0.0f ); • glVertex3f(0.0f, 3.0f, -3.0f); • glColor3f(0.0f, 0.0f, 1.0f ); • glVertex3f(3.0f, -2.0f, -2.0f); • glColor3f(1.0f, 0.0f, 0.0f ); • glVertex3f(-3.0f, -2.0f, 2.0f); • glEnd(); • // Swapping the hidden buffer with the visible one • glfwSwapBuffers(); • // exit if ESC was pressed or window was closed • running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED); • }

  5. Task 3 (cont) • // • // Termination • // • glfwTerminate(); • return 0; • }

  6. Task 4 • bool running() • { • return( !glfwGetKey(GLFW_KEY_ESC) && • glfwGetWindowParam( GLFW_OPENED) ); • }

  7. Task 4 (cont) • 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 ); • }

  8. Task 4 (cont) • void finit() • { • glfwTerminate(); • }

  9. Task 6 • glBegin( GL_POINTS ); • glVertex3f(-1, -1, -1); • glVertex3f(-1, -1, +1); • glVertex3f(-1, +1, -1); • glVertex3f(-1, +1, +1); • glVertex3f(+1, -1, -1); • glVertex3f(+1, -1, +1); • glVertex3f(+1, +1, -1); • glVertex3f(+1, +1, +1); • glEnd();

  10. Task 7 • glBegin( GL_LINES ); • // Four vertical segments • glColor3f(1,0,0); • glVertex3f(-1, -1, -1); • glVertex3f(-1, -1, +1); • glVertex3f(-1, +1, -1); • glVertex3f(-1, +1, +1); • glVertex3f(+1, -1, -1); • glVertex3f(+1, -1, +1); • glVertex3f(+1, +1, -1); • glVertex3f(+1, +1, +1); • // Four horizontal segments at the bottom • glColor3f(0,1,0); • glVertex3f(-1, -1, -1); • glVertex3f(-1, +1, -1); • glVertex3f(-1, +1, -1); • glVertex3f(+1, +1, -1); • glVertex3f(+1, +1, -1); • glVertex3f(+1, -1, -1); • glVertex3f(+1, -1, -1); • glVertex3f(-1, -1, -1); • // Four horizontal segments on the top • glColor3f(0,0,1); • glVertex3f(-1, -1, +1); • glVertex3f(-1, +1, +1); • glVertex3f(-1, +1, +1); • glVertex3f(+1, +1, +1); • glVertex3f(+1, +1, +1); • glVertex3f(+1, -1, +1); • glVertex3f(+1, -1, +1); • glVertex3f(-1, -1, +1); • glEnd();

  11. Task 7 v2 • glBegin( GL_LINE_LOOP ); • glVertex3f(-1, -1, -1); • glVertex3f(-1, -1, +1); • glVertex3f(+1, -1, +1); • glVertex3f(+1, -1, -1); • glEnd(); • glBegin( GL_LINE_LOOP ); • glVertex3f(-1, +1, -1); • glVertex3f(-1, +1, +1); • glVertex3f(+1, +1, +1); • glVertex3f(+1, +1, -1); • glEnd(); • glBegin( GL_LINES ); • glVertex3f(-1, -1, -1); • glVertex3f(-1, +1, -1); • glVertex3f(-1, -1, +1); • glVertex3f(-1, +1, +1); • glVertex3f(+1, -1, +1); • glVertex3f(+1, +1, +1); • glVertex3f(+1, -1, -1); • glVertex3f(+1, +1, -1); • glEnd();

  12. Task 8 • void drawCube (float a) • { • a = a/2; • // because the center is (0,0,0) • // half of the size is on the left hand side and the other half is on the right hand side • glBegin( GL_LINE_LOOP ); • glVertex3f(-a, -a, -a); • glVertex3f(-a, -a, +a); • glVertex3f(+a, -a, +a); • glVertex3f(+a, -a, -a); • glEnd(); • glBegin( GL_LINE_LOOP ); • glVertex3f(-a, +a, -a); • glVertex3f(-a, +a, +a); • glVertex3f(+a, +a, +a); • glVertex3f(+a, +a, -a); • glEnd(); • glBegin( GL_LINES ); • glVertex3f(-a, -a, -a); • glVertex3f(-a, +a, -a); • glVertex3f(-a, -a, +a); • glVertex3f(-a, +a, +a); • glVertex3f(+a, -a, +a); • glVertex3f(+a, +a, +a); • glVertex3f(+a, -a, -a); • glVertex3f(+a, +a, -a); • glEnd(); • }

  13. Task 8 (cont) • #include <ctime> • #include <cstdlib> • ……. • intmain() • { srand(unsigned(time(0))); • init(); • while( running() ) • { • glClear( GL_COLOR_BUFFER_BIT ); • glRotatef( 0.1, 0.4, -0.2, 0.7); • drawCube(2.0+(rand() % 600)/100.0); • glfwSwapBuffers(); • } • finit(); • return 0; • }

  14. Task 9 • int main() • { • init(); • while( running() ) • { • glClear( GL_COLOR_BUFFER_BIT ); • glRotatef( 0.1, 0.4, -0.2, 0.7); • for( inti=1; i<11; i++) drawCube(i); • glfwSwapBuffers(); • } • finit(); • return 0; • }

  15. Task 10 • 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(); • }

  16. Task 10 (cont) • int main() • { • init(); • while( running() ) • { • glClear( GL_COLOR_BUFFER_BIT ); • glRotatef( 0.1, 0.4, -0.2, 0.7); • drawCube(-3.2,0,0,3); • drawCube( 0.0,0,0,3); • drawCube(+3.2,0,0,3); • glfwSwapBuffers(); • } • finit(); • return 0; • }

  17. Task 10 v2 (cont) • int main() • { • init(); • while( running() ) • { • glClear( GL_COLOR_BUFFER_BIT ); • glRotatef( 0.1, 0.4, -0.2, 0.7); • float a=3.0; // Central cube size • float d=0.0; // Distance from its center to (0,0,0) • float k=0.6; // Step for small cubes size decrement • drawCube(0,0,0,a); • for( inti=0; i<10; i++ ) • { • d = d+(k+1)*a/2.0; • a = a*k; • drawCube(+d,0,0,a); • drawCube(-d,0,0,a); • drawCube(0,+d,0,a); • drawCube(0,-d,0,a); • drawCube(0,0,+d,a); • drawCube(0,0,-d,a); • } • glfwSwapBuffers(); • } • finit(); • return 0; • }

More Related