Chapter 2 Using OpenGL Chih-Kuo Yeh.  Addison Wesley OpenGL SuperBible 4 th Edition Jun 2007 Author: Richard S. Wright, Jr. Benjamin Lipchak Nicholas.

Slides:



Advertisements
Similar presentations
Programming with OpenGL - Getting started - Hanyang University Han Jae-Hyek.
Advertisements

Chapter 2: Graphics Programming
Computer Graphics CSCE 441
Pemrograman OpenGL Dasar
CS 4731 Lecture 2: Intro to 2D, 3D, OpenGL and GLUT (Part I) Emmanuel Agu.
6/10/2015 3:41:00 AM 1 CSE 307 COMPUTER GRAPHICS  OVERVIEW OF GRAPHICS SYSTEMS  GRAPHICS OUTPUT PRIMITIVES  GEOMETRICAL TRANSFORMATIONS  TWO DIMENSIONAL.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
30/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 1: Introduction.
Programming with OpenGL Part 0: 3D API March 1, 2007.
Interactive 3D Graphics and Virtual Reality Introduction to OpenGL concepts Session 2.
CSE 494/598 Intro to Applied Computer Graphics Anshuman Razdan DCST AR's Web Page AR's Web Page
Programming with OpenGL Part 1: Background Mohan Sridharan Based on slides created by Edward Angel CS4395: Computer Graphics 1.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 GLUT Callback Functions.
Based on slides created by Edward Angel
Today … The rendering pipeline in detail What is OpenGL
Introduction to OpenGL M. Ramanathan STTP CAD 2011Introduction to OpenGL.
Introduction to OpenGL Keng Shih-Ling 2003 Spring.
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.
Chapter 03: Graphics Primitives Course web page: Chapter #3.
Reference1. [OpenGL course slides by Rasmus Stenholt]
CS380 LAB I OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003.
Computer Graphics Bing-Yu Chen National Taiwan University.
1 Computer Graphics Dr. Amy Zhang Assistant Professor of Computer Science United International College.
Using OpenGL. 2 What is OpenGL? A software interface to graphics hardware It is a Graphics Rendering API (Application Programmer’s Interface) that is.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
Using OpenGL in Visual C++ Opengl32.dll and glu32.dll should be in the system folder Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib.
CSC 461: Lecture 41 CSC461: Lecture 4 Introduction to OpenGL Objectives: Development of the OpenGL API OpenGL Architecture -- OpenGL as a state machine.
Graphics Architectures & OpenGL API Introduction Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
CS 480/680 Computer Graphics Programming with Open GL Part 1: Background Dr. Frederick C Harris, Jr. Fall 2011.
2 COEN Computer Graphics I Introductions n Brad Grantham lecturer lab dude n Dave Shreiner lecturer slave driver.
Programming with OpenGL Part 1: Background
Lecture 3 OpenGL.
Write a Simple Program with OpenGL & GLUT. Books OpenGL Programming Guide (Red-book) OpenGL Reference Manual (Blue-book) OpenGL Super Bible
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
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 OpenGL Programming Jian-Liang Lin 2002.
Interactive Computer Graphics CS 418 MP1: Dancing I TA: Zhicheng Yan Sushma S Kini Mary Pietrowicz Slides Taken from: “An Interactive Introduction to OpenGL.
Computing & Information Sciences Kansas State University CIS 536/636 Introduction to Computer Graphics Lecture 4 of 41 William H. Hsu Department of Computing.
Introduction to OpenGL Week 1 David Breen Department of Computer Science Drexel University Based on material from Ed Angel, University of New Mexico CS.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 1: Background Ed Angel Professor Emeritus.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Programming with OpenGL Review.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 43 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 43 Computer Graphics Programming with OpenGL I.
Draw a Simple Object. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending.
NoufNaief.net TA: Nouf Al-harbi.
CGGM Lab. Tan-Chi Ho Introduction to OpenGL.
GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window.
Doç. Dr. Cemil Öz SAÜ Bilgisayar Mühendisliği Dr. Cemil Öz.
Introduction to OpenGL Programming
Chap 2 Write a Simple OpenGL Program. Preparing 1/2 environment : Microsoft Visual C 、 Microsoft Visual C++.Net Also need : GLUT
Introduction to Graphics Programming. Graphics API.
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
CSC Graphics Programming Budditha Hettige Department of Statistics and Computer Science.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Computer Graphics -practical- Lecture 6. (visual c++) open gl library To use open GL with VC++ we add these files:- 1)Glut.h C:\program files\ Microsoft.
Basic Program with OpenGL and GLUT
The Human Visual System vs The Pinhole camera
Programming with OpenGL Part 1: Background
CS380 Lab Spring Myungbae Son.
Programming with OpenGL Part 1: Background
גרפיקה ממוחשבת: מבוא ל-OpenGL
Programming with OpenGL Part 1: Background
Introduction to OpenGL
Programming with OpenGL Part 1: Background
Programming with OpenGL Part 1: Background
Presentation transcript:

Chapter 2 Using OpenGL Chih-Kuo Yeh

 Addison Wesley OpenGL SuperBible 4 th Edition Jun 2007 Author: Richard S. Wright, Jr. Benjamin Lipchak Nicholas Haemel

3  What is OpenGL & what can it do for me?  OpenGL in windowing systems  Why GLUT  A GLUT program template

4  Graphics rendering API  high-quality color images composed of geometric and image primitives  window system independent  operating system independent

5 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009  Silicon Graphics (SGI) revolutionized the graphics workstation by implementing the pipeline in hardware (1982)  To access the system, application programmers used a library called GL  With GL, it was relatively simple to program three dimensional interactive applications  1.0 (1992)

6 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 The success of GL lead to OpenGL (1992), a platform-independent API that was  Easy to use  Close enough to the hardware to get excellent performance  Focus on rendering  Omitted windowing and input to avoid window system dependencies

7 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009  OpenGL core library  OpenGL32 on Windows  GL on most unix/linux systems (libGL.a)  OpenGL Utility Library (GLU)  Provides functionality in OpenGL core but avoids having to rewrite code  Links with window system  GLX for X window systems  WGL for Windows  AGL for Macintosh

8  Geometric primitives  points, lines and polygons  Image Primitives  images and bitmaps  separate pipeline for images and geometry ▪ linked through texture mapping  Rendering depends on state  colors, materials, light sources, etc.

9  AGL, GLX, WGL  glue between OpenGL and windowing systems  GLU (OpenGL Utility Library)  part of OpenGL  NURBS, tessellators, quadric shapes, etc.  GLUT (OpenGL Utility Toolkit)  portable windowing API  not officially part of OpenGL

10 GLUT GLU GL GLX, AGL or WGL X, Win32, Mac O/S software and/or hardware application program OpenGL Motif widget or similar

11 Display List Polynomial Evaluator Per Vertex Operations & Primitive Assembly Rasterization Per Fragment Operations Frame Buffer Texture Memory CPU Pixel Operations

12  Headers Files  #ifdef WIN32  #include // Must have for Windows platform builds  #include "glee.h"// OpenGL Extension "autoloader"  #include // Microsoft OpenGL headers (version 1.1 by themselves)  #include // OpenGL Utilities  #include "glut.h"// Glut (Free-Glut on Windows)  #endif  Libraries  Enumerated Types  OpenGL defines numerous types for compatibility ▪ GLfloat, GLint, GLenum, etc.

13 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009  See website and ftp for examples  Unix/linux  Include files usually in …/include/GL  Compile with –lglut –lglu –lgl loader flags  May have to add –L flag for X libraries  Mesa implementation included with most linux distributions  Check web for latest versions of Mesa and glut

14 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009  Visual C++  Get glut.h, glut32.lib and glut32.dll from web  Create a console application  Add opengl32.lib, glu32.lib, glut32.lib to project settings (under link tab)  Borland C similar  Cygwin (linux under Windows)  Can use gcc and similar makefile to linux  Use –lopengl32 –lglu32 –lglut32 flags

15  Application Structure  Configure and open window  Initialize OpenGL state  Register input callback functions ▪ render ▪ resize ▪ input: keyboard, mouse, etc.  Enter event processing loop

17 1. #include “../../shared/gltools.h” // OpenGL toolkit 2. void RenderScene(void) 3. { 4. glClear(GL_COLOR_BUFFER_BIT); 5. glFlush(); 6. } 7. void SetupRC(void) 8. { 9. glClearColor(0.0f, 0.0f, 1.0f, 1.0f); 10. } 11. int main(int argc, char* argv[]) 12. { 13. glutInit(&argc, argv); 14. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); 15. glutCreateWindow(“Simple”); 16. glutDisplayFunc(RenderScene); 17. SetupRC(); 18. glutMainLoop(); 19. return 0; 20. }

21  Routine to call when something happens  window resize or redraw  user input  animation  “Register” callbacks with GLUT glutDisplayFunc( display ); glutIdleFunc( idle ); glutKeyboardFunc( keyboard );

 void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);

 glClear(GL_COLOR_BUFFER_BIT);  glFlush();

int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutCreateWindow("GLRect"); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); SetupRC(); glutMainLoop(); return 0; }

 Do all of your drawing here void RenderScene(void) {glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 0.0f, 0.0f); glRectf(-25.0f, 25.0f, 25.0f, -25.0f) glFlush();}

void ChangeSize(int w, int h) { GLfloat aspectRatio; if(h == 0) h = 1; glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); aspectRatio = (GLfloat)w / (GLfloat)h; if (w <= h) glOrtho (-100.0, 100.0, -100 / aspectRatio, / aspectRatio, 1.0, -1.0); else glOrtho ( * aspectRatio, * aspectRatio, , 100.0, 1.0, -1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }

 void glColor3f(GLfloat red, GLfloat green, GLfloat blue);  void glRectf(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2);

31 glVertex3fv( v ) Number of components 2 - (x,y) 3 - (x,y,z) 4 - (x,y,z,w) Data Type b - byte ub - unsigned byte s - short us - unsigned short i - int ui - unsigned int f - float d - double Vector omit “v” for scalar form glVertex2f( x, y )

void glViewport(GLint x, GLint y, GLsizei width, GLsizei height);

 void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far );

aspectRatio = (GLfloat)w / (GLfloat)h; if (w <= h) glOrtho (-100.0, 100.0, -100 / aspectRatio, / aspectRatio, 1.0, -1.0); else glOrtho ( * aspectRatio, * aspectRatio, , 100.0, 1.0, -1.0);

int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(800,600); glutCreateWindow("Bounce"); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); glutTimerFunc(33, TimerFunction, 1); SetupRC(); glutMainLoop(); return 0; }

GLfloat x = 0.0f; GLfloat y = 0.0f; GLfloat rsize = 25; GLfloat xstep = 1.0f; GLfloat ystep = 1.0f; GLfloat windowWidth; GLfloat windowHeight; void RenderScene(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 0.0f, 0.0f); glRectf(x, y, x + rsize, y - rsize); glutSwapBuffers(); }

void TimerFunction(int value) { if(x > windowWidth-rsize || x < -windowWidth) xstep = -xstep; if(y > windowHeight || y < -windowHeight + rsize) ystep = -ystep; x += xstep; y += ystep; if(x > (windowWidth-rsize + xstep)) x = windowWidth-rsize-1; else if(x < -(windowWidth + xstep)) x = -windowWidth -1; if(y > (windowHeight + ystep)) y = windowHeight-1; else if(y < -(windowHeight - rsize + ystep)) y = -windowHeight + rsize - 1; glutPostRedisplay(); glutTimerFunc(33,TimerFunction, 1); }

 glutTimerFunc(unsigned int millis, void (GLUTCALLBACK *func)(int value), int value);  Registers a timer callback to be triggered in a specified number of milliseconds.

 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);  glutSwapBuffers();

 Process user input  glutKeyboardFunc( keyboard );  void keyboard( unsigned char key, int x, int y )  {  switch( key ) {  case ‘q’ : case ‘Q’ :  exit( EXIT_SUCCESS );  break;  case ‘r’ : case ‘R’ :  rotate = GL_TRUE;  glutPostRedisplay();  break;  }

45 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 glutMouseFunc(mymouse)  void mymouse(GLint button, GLint state, GLint x, GLint y)  Returns  which button ( GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON ) caused event  state of that button ( GLUT_UP, GLUT_DOWN )  Position in window