1 Understanding of OpenGL TA: Dong Hyun Jeong Instructor : Dr. Kalpathi Subramanian Transformations and Projections.

Slides:



Advertisements
Similar presentations
Chapter 2: Graphics Programming
Advertisements

© 2004, Tom Duff and George Ledin Jr1 Lectures OpenGL Introduction By Tom Duff Pixar Animation Studios Emeryville, California and George Ledin Jr Sonoma.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
Hidden Surfaces and Shading CS BSP Tree T1T1 T2T2 E if (f 1 (E) < 0) then draw T 1 draw T 2 else draw T 2 draw T 1 f 1 (p) = 0 is the.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
OpenGL and Projections
Computer Graphics (Fall 2005) COMS 4160, Lecture 10: OpenGL 1
Interactive 3D Graphics and Virtual Reality Introduction to OpenGL concepts Session 2.
OpenGL Tutorial Jason Lawrence COS426 Precept Notes 2/16/04.
Lighting & Material. Example 1/5 #include ” glut.h ” void display(); void reshape(int, int); void lighting(); int main(int argc, char** argv) { glutInit(&argc,
Development of Interactive 3D Virtual World Applications
3D Rendering with JOGL Introduction to Java OpenGL Graphic Library By Ricardo Veguilla
Introduction to OpenGL M. Ramanathan STTP CAD 2011Introduction to OpenGL.
Write a Simple Program with OpenGL & GLUT. Books and Web Books OpenGL Programming Guide (Red-book) OpenGL Reference Manual (Blue-book) OpenGL Super Bible.
Introduction to OpenGL Jian Huang This set of slides are extracted from the Interactive OpenGL Programming course given by Dave Shreine, Ed Angel and Vicki.
OpenGL A Brief Overview. What is OpenGL?  It is NOT a programming language.  It is a Graphics Rendering API consisting of a set of function with a well.
1 OpenGL Basics A Graphics Standard ©Mel Slater, Anthony Steed
Computer Graphics, KKU. Lecture 131 Transformation and Viewing in OpenGL.
The Viewing Pipeline (Chapter 4) 5/26/ Overview OpenGL viewing pipeline: OpenGL viewing pipeline: – Modelview matrix – Projection matrix Parallel.
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
Write a Simple Program with OpenGL & GLUT. Books OpenGL Programming Guide (Red-book) OpenGL Reference Manual (Blue-book) OpenGL Super Bible
Introduction to OpenGL 1. 2 OpenGL A Graphics rendering API introduced in 1992 by Silicon Graphics Inc Provide the low-level functions to access graphics.
1. OpenGL/GLU/GLUT  OpenGL v4.0 (latest) is the “core” library that is platform independent  GLUT v3.7 is an auxiliary library that handles window creation,
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
Introduction to GL Geb Thomas. Example Code int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Modeling with OpenGL Practice with OpenGL transformations.
Computing & Information Sciences Kansas State University CIS 536/636 Introduction to Computer Graphics Lecture 4 of 41 William H. Hsu Department of Computing.
OpenGL: Introduction Yanci Zhang Game Programming Practice.
OpenGL Color and Lighting 2003 Spring Keng Shih-Ling.
OpenGL Lighting Jian-Liang Lin 2002 Hidden-Surface Removal -1 Original Code: while (1) { get_viewing_point_from_mouse_position(); glClear(GL_COLOR_BUFFER_BIT);
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
Chapters 5 2 March Classical & Computer Viewing Same elements –objects –viewer –projectors –projection plane.
CGGM Lab. Tan-Chi Ho 2001 Viewing and Transformation.
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower.
Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall.
Doç. Dr. Cemil Öz SAÜ Bilgisayar Mühendisliği Dr. Cemil Öz.
Chap 3 Viewing and Transformation
Introduction to OpenGL Programming
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
CS559: Computer Graphics Lecture 12: OpenGL: ModelView Li Zhang Spring 2010.
Introduction to Graphics Programming. Graphics API.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
Implement of transformation,projection, viewing Hanyang University Jungsik Park.
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
CS559: Computer Graphics Lecture 16: Shading and OpenGL Li Zhang Spring 2008.
CS380 LAB II OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
CSC Graphic Programming Lecture 2 OpenGL Lightning.
CSC Graphics Programming
The Human Visual System vs The Pinhole camera
Jason Lawrence COS426 Precept Notes 2/16/04
Programming with OpenGL Part 2: Complete Programs
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Understanding of OpenGL
גרפיקה ממוחשבת: מבוא ל-OpenGL
Lighting and Shading Lab 8:.
Computer Graphics, KKU. Lecture 13
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Lighting and Shading Lab 8:.
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

1 Understanding of OpenGL TA: Dong Hyun Jeong Instructor : Dr. Kalpathi Subramanian Transformations and Projections

2 Purpose Short overview of OpenGL Understanding of basic knowledge about OpenGL Short discussion on how to use OpenGL including transformations and projections

3 What is OpenGL? Software Interface for 3D Graphic Hardware (Instruction sets) Interactive 2D and 3D graphics application programming interface (API) Support high visual quality and performance –CAD/CAM, entertainment, medical imaging, virtual reality, Etc. Developer-driven advantages –Industry standard, stable, reliable and portable, easy to use ……

4 Supports Operating systems –Mac OS, OS/2, UNIX, Windows 95/98, NT, 2000, Linux, OPENStep, BeOS Windowing systems –Win32, MacOS, Presentation Manager, X-Window System Callable from –C, C++, Ada, Fortran, Python, Perl, Java

5 Flexibility and Extensions Has flexibility of defining a particular OpenGL implementation depending on systems OpenGL has extensions (API) –GLU, GLX, WGL (defined by vendors) UNIX APPLICATIONWINDOWS APPLICATION GLU XlibGLXOpenGL GLU GDUWGLOpenGL OpenGL applications use the window system’s window, input, and event mechanism GLU supports quadrics (2D curve), NURBS, complex polygons, matrix utilities, and more

6 Several Advanced API Open Inventor –Supports cross-platform user interface and flexible scene graph IRIS Performer –Visual simulation and virtual sets (demanding high frame rate) OpenGL Optimizer –Real-time interaction, modification, and rendering of complex surface-based models (CAD/CAM, special effects)

7 Several Advanced API OpenGL Volumizer –High-level immediate mode volume rendering API (energy, medical and sciences) OpenGL Shader –Realistic visual effects, bump mapping, multiple textures, etc.

8 Viewport and Viewport Clipping Projecting 3D objects onto 2D screen (CG process pipeline) Viewport –A two-dimensional (2D) rectangle into which a 3D scene is projected Vecterize –Representing 3D objects as line or plane pieces of objects or elements Clipping (Clip) –Remove the not interested regions from the vecterized elements VectorizeClip Transform To viewport Draw

9 Viewport and Viewport Clipping Clipping Volume –Physical coordinate or Global coordinate to represent the 3D objects. glOrtho(), gluPerspective() Viewport –Plane regions to map the physical coordinate into windows’ pixel coordinate. glViewport() Viewing transform –Changing user’s viewing angle to represent the 3d objects. glTranslated(), glRoated(), glMatrixMode(), gluLookAt()

10 Displaying Objects

11 Drawing a triangle on screen 1.Clear the screen 2.Create a triangle 3.Determine the location and direction of the triangle 4.Change the viewing angle 5.Define a projection method

12 Clear the screen Remove the frame buffer –glClearColor(0.0,0.0,0.0,0.0); –glClear(GL_COLOR_BUFFER_BIT);

13 Create a triangle A triangle as a geometry object void DrawTriangle() { glColor3f(1.0f,0.0f,0.0f); glBegin(GL_TRIANGLES); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(1.0f,0.0f,0.0f); glVertex3f(0.0f,1.0f,0.0f); glEnd(); }

14 Positioning Determine the location and direction of the triangle –glTranslatef(1.0f,1.0f,0.0f); –glRotatef(45.0f,0.0f,1.0f,0.0f); –DrawTriangle(); X Z Y Translate Rotate

15 Viewing angle Change the viewing angle –glMatrixMode(GL_PROJECTION); –glLoadIdentity(); –glOrtho(-1.0f,1.0f,-1.0f,1.0f,- 1.0f,10.0f); –glMatrixMode(GL_MODELVIEW); –gluLookAt(1.5f,1.5f,1.0f,1.5f,1.5f,- 0.5f, 0.0f,1.0f,0.0f); X Z Y

16 Projection Determine the projection –Perspective Projection glFrustum(-1.0f,1.0f,- 1.0f,1.0f,1.0f,100.0f); –Orthographic Projection glOrtho(-1.0f,1.0f,- 1.0f,1.0f,0.0f,100.0f); X Z Y Perspective Projection Orthographic Projection

17 Points –GL_POINTS Lines –GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP Triangles –GL_TRIANGLES, GL_TRIANGLES_STRIP, GL_TRIANGLE_FAN Geometry elements I v0 v1 v2 v3 v4 v0 v1 v2 v3 v4 v5 v0 v1 v2 v3 v4 v5 v0 v1 v2 v3 v4 v5 v0 v1 v2 v3 v4 v5 v0 v1 v4 v5 v3 v2 v0 v1 v2 v3 v4 v5

18 Geometry elements II Quadric –GL_QUADS, GL_QUAD_STRIP Polygon –GL_POLYGON v0 v1 v2 v4 v5 v3 v7 v6 v0 v1 v5 v7 v3 v4 v6 v2 v0 v5 v2 v1 v4 v3

19 Basic restrictions All have to be plane objects No halls Edges do not have to be crossing ValidInvalid

20 Creating complex objects Complex objects using connected lines Complex objects using connected triangles

21 Vertex transformation

22 Viewing Positioning a object Change the viewing angle Projection and transformation Viewport and coordinate determination

23 Modeling Transformation Place object and change the viewing angle glMatrixMode(GL_MODELVIEW) glLoadIdentity() – Identify matrix is called –glTranslatef(x,y,z) –glRotatef(degree,x,y,z) –glScalef(x,y,z) Translate Rotate Scale

24 Viewing Transformation Changes the position and orientation of the viewpoint Generally it consists of translations and rotations. –gluLookAt()

25 Projection Transformations Defining a viewing volume –determines how an object is projected onto the screen –glMatrixMode(GL_PROJECTION); –glLoadIdentity(); –glFrustum(left,right, bottom,top, near, far); –glOrtho(left,right, bottom,top, near, far);

26 Perspective Projection I glFrustum(left, right, bottom, top, near, far); (left, right, bottom, top) - near clipping plane near and far give the distances from the viewpoint to the near and far clipping planes. Easy to understand, but not intuitive to use

27 Perspective Projection II gluPerspective(fovy, aspect, near, far); Creates a matrix for a symmetric perspective- view frustum

28 Orthographic Projection the viewing volume is a rectangular parallelepiped glOrtho(left, right, bottom, top, near, far);

29 Viewport Transformation The viewport is the rectangular region of the window where the image is drawn Measured in window coordinates, which reflect the position of pixels on the screen relative to the lower left corner of the window. –glViewport(x, y, width, height);

30 Aspect Ratio Mapping the Viewing Volume to the Viewport Displayed image can be distorted depending on the ratio (w/h)

31 Matrix Stacks A stack of matrices is useful for constructing hierarchical models, in which complicated objects are constructed from simpler ones. –glMatrixMode() –glLoadMatrix(), glMultMatrix() –glPushMatrix(), glPopMatrix() –glLoadIdentity() Pushing and Popping the Matrix Stack Modelview and Projection Matrix Stacks

32 Matrix Stacks Example glTranslatef() glPushMatrix() CreateTriangleCreateCircle glLoadIdentity() CreateCircle (0.0, 0.0, 0.0) CreateCircle (0.0, 0.0, -5.0)

33 How to program? Need OpenGL Extensions –Win32 OS: WGL. Prefix starting with wgl. (brief explanation at MSDN) –X Windows: GLX –Apple Macintosh: AGL –OS/2: PGL –Each extensions supports making windows-based applications depending on operating systems. –But, it has restrictions of broadly using in other operating systems.

34 What is GLUT? OpenGL Utility Toolkit –A window system independent toolkit for writing OpenGL programs. –Works across all PC and workstation OS platforms – –Better than AUX (Auxiliary) library –Need to locate the header and library files when using Windows Operating System (not obligation, but useful when making OpenGL applications) –(*.dll) C:\WINDOWS\system32 Vs6 : –(*.h) C:\Program Files\Microsoft Visual Studio\VC98\Include\GL –(*.lib) C:\Program Files\Microsoft Visual Studio\VC98\Lib.net –(*.h) C:\Program Files\Microsoft Visual Studio.NET 2003\Vc7\PlatformSDK\Include\gl –(*.lib) C:\Program Files\Microsoft Visual Studio.NET 2003\Vc7\PlatformSDK\Lib

35 How to use GLUT? Initialization functions –void glutInit(int *argc, char **argv); Initialize display mode –glutInitDisplayMode(unsigned int mode); GLUT_RGB, GLUT_SINGLE, GLUT_DOUBLE Set the initial size of the window –glutInitWindowSize(int width, int height); The initial position of the window –glutInitWindowPosition(int x, int y); Creates the window –int glutCreateWindow(char *name); Specifying the function that needs to be called (callback function) –void glutDisplayFunc(void (*func)(void)); Redraw the current window –void glutPostRedisplay(void); A callback function called when window size is changed –void glutReshapeFunc(void (*func)(int width, int height)); More info. at “An introduction to GLUT”,

36 A simple example #include void DrawTriangle() { glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); // red glVertex2f(0.0f, 0.0f); glColor3f(0.0f, 1.0f, 0.0f); // green glVertex2f(1.0f, 0.0f); glColor3f(0.0f, 0.0f, 1.0f); // blue glVertex2f(0.0f, 1.0f); glEnd(); } void display() { glClearColor(0.0, 0.0, 0.0, 0.0); // black glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); // push modelview matrix glTranslatef(1.0f, 1.0f, 0.0f); glRotatef(45.0f, 0.5f, 1.0f, 0.5f); DrawTriangle(); glPopMatrix(); // pop modelview matrix glFlush(); } void reshape(int w, int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, 0.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(1.5f, 1.5f, 1.0f, 1.5f, 1.5f, -0.5f, 0.0f, 1.0f, 0.0f); } void init() { // Gouraud Shading glShadeModel(GL_SMOOTH); } void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100,100); glutInitWindowSize(400,400); glutCreateWindow("OpenGL EXAMPLE"); init(); glutReshapeFunc(reshape); glutDisplayFunc(display); glutMainLoop(); }

37 Transformation Example I #include "glut.h" static int year = 0, day = 0; void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glPushMatrix(); glColor3f(1.0, 0.0, 0.0); glutSolidSphere(1.0, 10, 10);/* draw sun */ glRotatef((GLfloat) year, 0.0, 1.0, 0.0); glTranslatef (2.0, 0.0, 0.0); glRotatef((GLfloat) day, 0.0, 1.0, 0.0); glColor3f(0.0, 0.0, 1.0); glutSolidSphere(0.2, 10, 10);/* draw smaller planet */ glPopMatrix(); glFlush(); } void myinit(void) { glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); } void myReshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef (0.0, 0.0, -5.0); } void Key1(int key, int x, int y) { switch(key) { case GLUT_KEY_LEFT: year = (year - 5) % 360; break; case GLUT_KEY_RIGHT: year = (year + 5) % 360; break; case GLUT_KEY_UP: day = (day + 10) % 360; break; case GLUT_KEY_DOWN: day = (day - 10) % 360; break; } glutPostRedisplay(); } void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100,100); glutInitWindowSize(400,400); glutCreateWindow("OpenGL EXAMPLE"); myinit(); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutSpecialFunc(Key1); glutMainLoop(); }

38 Display List OpenGL display lists are designed to optimize performance drawCircle() { GLint i; GLfloat cosine, sine; glBegin(GL_POLYGON); for(i=0;i<100;i++){ cosine=cos(i*2*PI/100.0); sine=sin(i*2*PI/100.0); glVertex2f(cosine,sine); } glEnd(); } buildCircle() { GLint i; GLfloat cosine, sine; glNewList(MY_CIRCLE_LIST, GL_COMPILE); glBegin(GL_POLYGON); for(i=0;i<100;i++){ cosine=cos(i*2*PI/100.0); sine=sin(i*2*PI/100.0); glVertex2f(cosine,sine); } glEnd(); glEndList(); } glCallList(MY_CIRCLE_LIST);

39 Transformation Example II #include "glut.h" GLuint listName = 1; void display(void) { glClear(GL_COLOR_BUFFER_BIT); GLuint i; glColor3f(0.0, 1.0, 0.0); for (i = 0; i < 10; i++) glCallList (listName); glFlush (); } void myinit(void) { glShadeModel(GL_SMOOTH); glNewList (listName, GL_COMPILE); glColor3f(1.0, 0.0, 0.0); glBegin (GL_TRIANGLES); glVertex2f (0.0, 0.0); glVertex2f (1.0, 0.0); glVertex2f (0.0, 1.0); glEnd (); glTranslatef (1.2, 0.0, 0.0); glEndList (); } void myReshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) gluOrtho2D (0.0, 2.0, -0.5 * (GLfloat) h/(GLfloat) w,1.5 * (GLfloat) h/(GLfloat) w); else gluOrtho2D (0.0, 2.0 * (GLfloat) w/(GLfloat) h, -0.5, 1.5); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100,100); glutInitWindowSize(400,400); glutCreateWindow("OpenGL EXAMPLE 2"); myinit(); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutMainLoop(); }

40 Shading model Flat shading –glShadeModel(GL_FLAT) Smooth (Gouraud) shading –glShadeModel(GL_SMOOTH)

41 Light source Ambient (GL_AMBIENT) –The light from that source that's been scattered so much by the environment Diffuse (GL_DIFFUSE) –Comes from one direction Specular (GL_SPECULAR) –Comes from a particular direction GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position);

42 Material Properties Define the material properties of the objects in the scene: –GL_AMBIENT, GL_DIFFUSE, GL_AMBIENT_AND_DIFFUSE, GL_SPECULAR, Etc. GLfloat mat_amb_diff[] = { 0.1, 0.5, 0.8, 1.0 }; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);

43 Light Example I #include "glut.h" void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutSolidSphere(1.0, 50, 50); glFlush(); } void myinit(void) { GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 50.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); } void myReshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w, 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0); else glOrtho (-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h, - 1.5, 1.5, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100,100); glutInitWindowSize(500,500); glutCreateWindow("OpenGL EXAMPLE 3"); myinit(); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutMainLoop(); }

44 Light Example II #include "glut.h" static int spin = 0; void display(void) { GLfloat position[] = { 0.0, 0.0, 1.5, 1.0 }; glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix (); glTranslatef (0.0, 0.0, -5.0); glPushMatrix (); glRotated ((GLdouble) spin, 1.0, 0.0, 0.0); glRotated (0.0, 1.0, 0.0, 0.0); glLightfv (GL_LIGHT0, GL_POSITION, position); glTranslated (0.0, 0.0, 1.5); glDisable (GL_LIGHTING); glColor3f (0.0, 1.0, 1.0); glutWireCube(0.1); glEnable (GL_LIGHTING); glPopMatrix (); glutSolidSphere(0.9, 50, 50); glPopMatrix (); glFlush (); } void myinit(void) { glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); } void myReshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW);} void Key1(int key, int x, int y) { switch(key) { case GLUT_KEY_LEFT: spin = (spin + 30) % 360; break; } glutPostRedisplay(); } void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100,100); glutInitWindowSize(500,500); glutCreateWindow("OpenGL EXAMPLE 4"); myinit(); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutSpecialFunc(Key1); glutMainLoop(); }

45 Light Example III #include "glut.h" GLfloat diffuseMaterial[4] = { 0.5, 0.5, 0.5, 1.0 }; void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutSolidSphere(1.0, 50, 50); glFlush(); } void myinit(void) { GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialf(GL_FRONT, GL_SHININESS, 25.0); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); glColorMaterial(GL_FRONT, GL_DIFFUSE); glEnable(GL_COLOR_MATERIAL); } void myReshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho (-1.5, 1.5, - 1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0); else glOrtho (-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h, - 1.5, 1.5, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void Mouse(int button, int state, int x, int y) { if (state == GLUT_DOWN) { switch (button) { case GLUT_LEFT_BUTTON : diffuseMaterial[0] += 0.1; if (diffuseMaterial[0] > 1.0) diffuseMaterial[0] = 0.0; glColor4fv(diffuseMaterial); break; case GLUT_RIGHT_BUTTON : diffuseMaterial[1] += 0.1; if (diffuseMaterial[1] > 1.0) diffuseMaterial[1] = 0.0; glColor4fv(diffuseMaterial); break; case GLUT_MIDDLE_BUTTON : diffuseMaterial[2] += 0.1; if (diffuseMaterial[2] > 1.0) diffuseMaterial[2] = 0.0; glColor4fv(diffuseMaterial); break; } glutPostRedisplay(); } void main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100,100); glutInitWindowSize(500,500); glutCreateWindow("OpenGL EXAMPLE 5"); myinit(); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutMouseFunc(Mouse); glutMainLoop(); }

46 Complicated Examples

47 Complex Model I Does it look complicated? –Only use simple shape models such as sphere, rectangle boxes, octagon, etc. Configurations –Torso, Hip, Shoulder, RocketPod, UpperArm, ForeAm, UpperLeg, Foot, VulcanGun, Building models Smooth animation –Only transformation functions

48 Complex Model II Environment –Loading the tunnel model –Add texture images –For navigation pre-calculated camera route used Text –glfont library used Environmental map

49 Complex Model III Loading models –Use user-defined file format Additional Features –Collision detection –Fog effects (OpenGL support)

50 Complex Model IV Loading models –Importing Wavefront OBJ file to display –*.obj file includes vertex, texture coordinate, and face numbers Rendered using 3DS MAX without texture Rendered using 3DS MAX with texture

51 Useful Website for OpenGL OpenGL Official site: SGI website: OpenGL documentations: – Tutorials – GLUT –