Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

CGLabLec6.pptx

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Chargement dans…3
×

Consultez-les par la suite

1 sur 25 Publicité

Plus De Contenu Connexe

Similaire à CGLabLec6.pptx (20)

Plus récents (20)

Publicité

CGLabLec6.pptx

  1. 1. 2/3/2023 1 Three Dimensional Programs Using OpenGL Computer Graphics OpenGL – Lecture 6
  2. 2. Content 1. The Three Dimension 2. OpenGL 3D Objects 3. Some Built-In Functions 4. Transformations in Open GL 5. Writing your First 3D Program 6. Another 3D Program 7. Projection Transformation Program 2/3/2023 2
  3. 3. • In OpenGL: 2-dimensional applications are a special case of 3 dimensional graphics. (glVertex3f(X,Y,Z) (in 2D Z = zero) ). • In 3D:  Not much changes.  For 3D, we use a vector graphics representation.  When we make 3D objects we will make them out of triangles (or other polygons). By placing the triangles side by side, we can create complex shapes.  3D graphics take lots of processing power to do things like vector calculations, shading, lighting and texture mapping and we can use them to make objects look more real. The Three Dimensions 2/3/2023 3
  4. 4. • There are many ways to draw 3D shapes:  We can simply connect some 2D faces together.  Example: a cube (3D) is made of 6 square (2D) faces. 1. Use glVertex3*(X,Y,Z). 2. Use built in functions that draw 3D openGL objects. The Three Dimensions 2/3/2023 4
  5. 5. • GLUT provides the follow objects: (we can draw it by built in functions)  Cube.  Sphere.  Cylinder.  Cone.  Tea pot.  CD shaped disc.  Torus.  icosahedrons,  Ocrtahedron  Tetrahedron….etc. OpenGL 3D Objects 2/3/2023 5
  6. 6. Icosahedrons Ocrtahedron Tetrahedron OpenGL 3D Objects 2/3/2023 6 Icosahedrons: a solid figure with twenty plane faces, especially equilateral triangular ones. Ocrtahedron: a three-dimensional shape having eight plane faces, especially a regular solid figure with eight equal triangular faces. Tetrahedron: a solid having four plane triangular faces; a triangular pyramid.
  7. 7. • Both wireframe and solid :  glutSolidSphere(1.0, 24, 24) or:  glutWireCube(1.0) • Note: Most of 3D objects looks like 2D object until we apply lighting on it. OpenGL 3D Objects 2/3/2023 7
  8. 8. • There are several built in functions in openGL to draw 3D objects as the following: Some Built In Functions 2/3/2023 8
  9. 9. Transformations in OpenGL 1) Projection Transformation • Refer to the transformation from scene to image. • Adjust the lens of camera. 2) Viewing Transformation • Refer to the transformation on the camera coordinates. 3) Modeling Transformation • Refer to the transformation of models (objects). 4) Viewport Transformations • Enlarge or reduce the physical photograph (screen coordinates). • Viewport aspect ratio should be same as projection transformation. 2/3/2023 9
  10. 10. Projections Transformation • Specifying the projection transformation is like choosing a lens for a camera. • You can think of this transformation as determining the field of view and which objects are inside it and, how they should look. • There are 2 basic types of projections provided for you by OpenGL: 1) Orthographic Projection:  glOrtho(left, right, bottom, top, near, far) -> to 3D graphics.  gluOrtho2D( left, right, bottom, top) -> to 2D graphics. 2) Perspective Projection:  gluPerspective (double angle, double aspect, double near, double far)  glFrustum( xleft, xright, ybottom, ytop, zNear, zFar ) 2/3/2023 10
  11. 11. Projections Transformation 2/3/2023 11
  12. 12. Projections Transformation 2/3/2023 12
  13. 13. Projections Transformation 2/3/2023 13
  14. 14. Viewing Transformation 2/3/2023 14
  15. 15. Setting up the Camera 2/3/2023 15
  16. 16. Writing your First 3D Program #include<windows.h> #include<GL/glut.h> void Display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1.0, 0.2, 0.2); glLineWidth(6); //gluLookAt(eyex, eyey, eyez, cx, cy, cz, upx, upy, upz) gluLookAt(0, 2, 5, 0, 1, 1, 0, 1, 2); glutWireCube(2); //Size = 2 glFlush(); } void init() { glClearColor(1.0, 0.8, 0.8, 0.0); glShadeModel(GL_FLAT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //Clear the Matrix //gluPerspective(angle, aspect(width/hight), near, far) gluPerspective(60, 1, 1, 20); } int main() { glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(300, 300); glutInitWindowPosition(100, 100); glutCreateWindow("The Cube"); init(); glutDisplayFunc(Display); glutMainLoop(); return 0; } 2/3/2023 16
  17. 17. Writing your First 3D Program 2/3/2023 17
  18. 18. Writing your First 3D Program 2/3/2023 18
  19. 19. Another 3D Program #include<windows.h> #include<GL/glut.h> void Display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1.0, 0.2, 0.2); glLineWidth(6); //gluLookAt(eyex, eyey, eyez, cx, cy, cz, upx, upy, upz) gluLookAt(0, 2, 5, 0, 1, 1, 0, 1, 2); // A Cone with (Bottom Radius, Height, Slice, Stack) glutWireCone(1.8, 3.5, 35, 35); glFlush(); } void init() { glClearColor(1.0, 0.8, 0.8, 0.0); glShadeModel(GL_FLAT); //or GL_SMOOTH glMatrixMode(GL_PROJECTION); //Load the Matrix glLoadIdentity(); //Clear the Matrix //glFrustum(left, right, bottom, top, near, far) glFrustum(-2, 2, -2, 2, 2, 20); } int main() { glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(300, 300); glutInitWindowPosition(0, 0); glutCreateWindow("Draw a Cone"); init(); glutDisplayFunc(Display); glutMainLoop(); return 0; } 2/3/2023 19
  20. 20. Another 3D Program 2/3/2023 20
  21. 21. Another 3D Program 2/3/2023 21
  22. 22. Projection Transformation Program #include<windows.h> #include<GL/glut.h> #include<math.h> static int W=0, H=0; static int Window1, Window2, Window3, Window4; void Draw() { glColor3f(0, 0, 0); glTranslatef(0.0, 0.0, -100.0); glScalef(20, 20, 20); glutWireIcosahedron(); } void render_1() { //‫األولى‬ ‫النافذة‬ glutSetWindow(Window1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45, 1.0, 10.0, 200.0); Draw(); glutSwapBuffers(); } void render_2() { //‫الثانية‬ ‫النافذة‬ glutSetWindow(Window2); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(30, 3.0, 10.0, 200.0); Draw(); glutSwapBuffers(); } void render_3() { //‫الثالثة‬ ‫النافذة‬ glutSetWindow(Window3); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(20, 1.0, 10.0, 200.0); Draw(); glutSwapBuffers(); } void render_4() { //‫الرابعة‬ ‫النافذة‬ glutSetWindow(Window4); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(75, 1.0, 10.0, 200.0); Draw(); glutSwapBuffers(); } 2/3/2023 22
  23. 23. Projection Transformation Program int main() { glutInitDisplayMode (GLUT_DEPTH| GLUT_DOUBLE); glutInitWindowPosition(230, 40); //‫األولى‬ ‫النافذة‬ glutInitWindowSize(180, 180); Window1 = glutCreateWindow("Perspective 1"); glutSetWindow(Window1); glClearColor(1.0, 0.5, 0.5, 0); glutDisplayFunc(render_1); glutInitWindowPosition(40, 40); //‫الثانية‬ ‫النافذة‬ glutInitWindowSize(180, 180); Window2 = glutCreateWindow("Perspective 2"); glutSetWindow(Window2); glClearColor(1.0, 0.5, 0.5, 0); glutDisplayFunc(render_2); glutInitWindowPosition(230, 260); //‫الثالثة‬ ‫النافذة‬ glutInitWindowSize(180, 180); Window3 = glutCreateWindow("Perspective 3"); glutSetWindow(Window3); glClearColor(1.0, 0.5, 0.5, 0); glutDisplayFunc(render_3); glutInitWindowPosition(40, 260); //‫الرابعة‬ ‫النافذة‬ glutInitWindowSize(180, 180); Window4 = glutCreateWindow("Perspective 4"); glutSetWindow(Window4); glClearColor(1.0, 0.5, 0.5, 0); glutDisplayFunc(render_4); glutMainLoop(); return 0;} 2/3/2023 23
  24. 24. Projection Transformation Program 2/3/2023 24
  25. 25. Projection Transformation Program 2/3/2023 25

×