OpenGL (I). What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D objects.

Slides:



Advertisements
Similar presentations
OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
Advertisements

OPEN GL. Install GLUT Download package di sini Dari devcpp, buka Tools->PackageManager-
Chapter 2: Graphics Programming
TOPIC 3 INTRODUCTION TO OPENGL CGMB214: Introduction to Computer Graphics.
CS 4731 Lecture 2: Intro to 2D, 3D, OpenGL and GLUT (Part I) Emmanuel Agu.
What is OpenGL? Low level 2D and 3D Graphics Library Competitor to Direct3D (the rendering part of DirectX) Used in: CAD, virtual reality, scientific.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
Chapter 3: Input and Interaction Instructor: Shih-Shinh Huang 1.
Device Independent Graphics and OpenGL
ITEPC 06 - Workshop on Fractal Creation Chiew-Lan Tai and Oscar Au.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 GLUT Callback Functions.
Based on slides created by Edward Angel
CSC461 Lecture 9: GLUT Callbacks Objectives Introduce double buffering for smooth animations Programming event input with GLUT.
CS 480/680 Computer Graphics Programming with Open GL Part 8: Working with Callbacks Dr. Frederick C Harris, Jr. Fall 2011.
Chapter 03: Graphics Primitives Course web page: Chapter #3.
Reference1. [OpenGL course slides by Rasmus Stenholt]
Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003.
Chi-Cheng Lin, Winona State University CS430 Computer Graphics Graphics Programming and OpenGL.
2003CS Hons RW778 Graphics1 Chapter 1: Introduction Admin: Admin: –Lecture slots: Monday 8-9:45, Tuesday 9:15-11:00. –Book: F. Hill, Computer Graphics.
Drawing Basic Graphics Primitives Lecture 4 Wed, Sep 3, 2003.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
1 Working with Callbacks Yuanfeng Zhou Shandong University.
WORKING WITH CALLBACKS Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Angel: Interactive.
Geometric Primitives Used in Open GL Polygons Polygon is : Flat shape with three or more straight sides. It could be either : convex or concave.
Computer Graphics using OpenGL, 3 rd Edition F. S. Hill, Jr. and S. Kelley Chapter 2 Initial Steps in Drawing Figures Ureerat Suksawatchon Faculty of Informatics.
Lecture 3 OpenGL.
Computer Graphics I, Fall 2010 Input and Interaction.
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 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
CD2012 Principles of Interactive Graphics Lecture 01 Introduction Abir Hussain (Rome: 6.33,Tel , Web:
Introduction to GL Geb Thomas. Example Code int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
Introduction to Computer Graphics 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
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.
1 Input and Interaction. 2 Objectives Introduce the basic input devices ­Physical Devices ­Logical Devices ­Input Modes Event-driven input Introduce double.
Computer Graphics I, Fall 2010 Working with Callbacks.
University of New Mexico
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
NoufNaief.net TA: Nouf Al-harbi.
NoufNaief.net 1 TA: Nouf Al-Harbi.
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.
Geometric Primitives Used in Open GL Drawing Triangles glBegin(GL_TRIANGELS); glVertex2i(p0); glVertex2i(p1); glVertex2i(p2); glVertex2i(p3); glVertex2i(p4);
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
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
Introduction to OpenGL Muhammad Aamir Khan Lecturer, DCS, UOP.
Introduction to Graphics Programming. Graphics API.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Working with Callbacks.
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.
Review GLUT Callback Functions
Working with Callbacks
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Lab 3 Geometric Drawing Lab 3 Geometric Drawing.
Starting to draw dealing with Windows which libraries? clipping
גרפיקה ממוחשבת: מבוא ל-OpenGL
Working with Callbacks
Drawing in the plane 455.
Introduction to OpenGL
Starting to draw dealing with Windows which libraries? clipping
University of North Carolina at Greensboro
Presentation transcript:

OpenGL (I)

What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D objects.

Interactive computer graphics system that allows us to access graphics hardware Easy to use Programs run efficiently Hardware-independent Graphics API (Application Programming Interface) A library of functions Others: DirectX (Microsoft), Java3D What is OpenGL (OGL)?

OGL is independent of any specific window system  basic window operations are not included Graphics user interface programming: GLUT = OGL Utility Toolkit Simple but easy to use What is OpenGL (OGL)?

Programming Environment OGL is usually installed on a MS Windows machine. Programming environment MS Visual Studio.Net 2003 Libraries we will use OGL (basic API tool) GLU (OGL Utility Library) GLUT (OGL Utility Toolkit, a windowing toolkit that handles window system operations)

OGL Data Types To make more portable, OGL provides internal data types

Basic OGL Syntax Functions are prefixed with gl, glBegin, glClear, glClearColor Constants in capital letters, and the underscore is used as a separator GL_2D, GL_LINES, GL_TRIANGLES Built-in data-type names begin with GL GLbyte, GLshort, GLint, GLboolean

First OGL Program: DrawDots

Our first program is not interactive. It consists of three functions: main, myDisplay, myInit. (see accompanying demo program) int main(int argc, char** argv) { glutInit(&argc, argv);// initialize the toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set the display mode glutInitWindowSize(640, 480); // set window size glutInitWindowPosition(100, 150); // set window position glutCreateWindow("my first attemp"); // open the screen window glutDisplayFunc(myDisplay); // register redraw function myInit();// additional initializations as necessary glutMainLoop();// go into a perpetual loop }

Creating Window for Drawing The first five function calls use GLUT to open a window for drawing Coordinate system in OGL sx sy (150, 100)

First OGL Program: DrawDots Draw primitives Display callback function myDisplay void myDisplay() { glClear(GL_COLOR_BUFFER_BIT); // clear the screen glBegin(GL_POINTS); glVertex2i(100, 50); // draw three dots glVertex2i(100, 130); glVertex2i(150, 130); glEnd(); glFlush();// send all output to display }

First OGL Program: DrawDots Draw primitives glBegin(GLenum mode) mode can be GL_POINTS, GL_LINES, GL_POLYGON, etc.

First OGL Program: DrawDots Initialization void myInit() { glClearColor(1.0, 0.0, 0.0, 0.0); // set red background color glColor3f(0.0, 1.0, 0.0); // set the drawing color glPointSize(10.0); // a 'dot' is 10 by 10 pixels // The following lines establish the coordinate system. // Details will be covered later. glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, 640, 0, 480); }

First OGL Program: DrawDots OpenGL is a state machine It keeps track of many state variables Current size of a point, current color of drawing, current background color, etc. Color of drawing is specified using glColor3f(red, green, blue) ; (range: [0, 1]) Background color is set with glClearColor(red, green, blue, alpha). glClear clears the entire window to the background color (cf. myDisplay ). The value of a state variable remains active until a new value is given.

Line Drawing glLineWidth(2.0);// set line thickness glBegin(GL_LINES); glVertex2i(10, 20);// first horizontal line glVertex2i(40, 20); glVertex2i(20, 10);// first vertical line glVertex2i(20, 40); // four more calls to glVertex here for the other two lines glEnd();

Drawing Modes glBegin(GLenum mode)

Skeleton of an interactive OGL program using GLUT int main(int argc, char** argv) { glutInit(&argc, argv);// initialize the toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set the display mode glutInitWindowSize(640, 480); // set window size glutInitWindowPosition(150, 100); // set window position glutCreateWindow("my first attempt"); // open the screen window // register the callback functions glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); glutMouseFunc(myMouse); glutKeyboardFunc(myKeyboard); myInit();// additional initializations as necessary glutMainLoop();// go into a perpetual loop }

Event-driven Programming & Callbacks Event-driven programming A property of most window-based programs The programs respond to various events Click of a mouse, resizing of a window, etc. The system automatically manages an event queue Deal with events first-come, first-served Programmers write functions (called callback functions) that will be called when events occur.

Event-driven Programming & Callbacks Callback functions in GLUT For example, glutMouseFunc(myMouse) Register function myMouse() as the function to be executed when a mouse event occurs. Prefix glut indicates that this function is part of GLUT If a particular program does not make use of a mouse, the corresponding callback function need not be registered.

Event-driven Programming & Callbacks glutDisplayFunc(myDisplay) Whenever the system determines that a window should be redrawn, it issues a “redraw” event. Redraw event happens When the window is first opened When the window is exposed by moving another window off it When your program explicitly requests that it be redrawn (through the call glutPostRedisplay ) (useful for animation) myDisplay() is registered as the callback function for a redraw event. Coordinate system in OpenGL

Event-driven Programming & Callbacks glutReshapeFunc(myReshape) Screen windows can be resized by the user, usually by dragging a corner of the window to a new position with the mouse. myReshape(int width, int height) is automatically passed arguments reporting the new width and height of the reshaped window. Coordinate system in OpenGL

Event-driven Programming & Callbacks glutMouseFunc(myMouse) When one of the mouse button is pressed or released, a mouse event is issued. myMouse(int button, int state, int x, int y) is automatically passed arguments describing the location of the mouse and the nature of the action initiated by pressing the button. Coordinate system in OpenGL

Event-driven Programming & Callbacks glutKeyboardFunc(myKeyboard) Keyboard event is issued when some key is pressed or released. myKeyboard(unsigned char key, int x, int y) is automatically passed arguments that tell which key was pressed. It is also passed the data indicating the location of the mouse at the time the key was pressed. Coordinate system in OpenGL

Summary of Common Callbacks

Mouse Interaction Let’s see a demo for mouse & keyboard callbacks glutMouseFunc(myMouse) Mouse button is pressed or released. glutMotionFunc(myMouse) Mouse is moved while one of the buttons is pressed. void myMouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { glBegin(GL_POINTS); glVertex2i(x, screenHeight - y); glEnd(); glFlush(); } Note the y value is the number of pixels down from the top of the window!

Mouse Interaction Mouse motion example “freehand” drawing with a fat brush See second demo program void myMovedMouse(int mouseX, int mouseY) { GLint x = mouseX; GLint y = screenHeight - mouseY;// flip it as usual GLint brushSize = 20; glRecti(x, y, x + brushSize, y + brushSize); glFlush(); }

Keyboard Interaction void myKeyboard(unsigned char key, int mouseX, int mouseY) { GLint x = mouseX; GLint y = screenHeight - mouseY;// flip it as usual switch(key) // value of key is the ASCII value of the key pressed { case 'p':// draw a dot at the mouse position glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); glFlush(); break; case 'E': exit(-1); // terminate the program }

Other GLUT Techniques Recommended GLUT tutorial Topics we didn’t cover but useful Animation: void glutIdleFunc(void (*func)(void)) Popup menus: int glutCreateMenu(void (*func)(int value)); Bitmap fonts: void glutBitmapCharacter(void *font, int character) Sub windows: int glutCreateSubWindow(int parentWindow, int x, int y, int width, int height); Recommended reading material Chapter 1 & 2 of OpenGL red bookOpenGL red book

Recommended Resources OpenGL official Website OpenGL Utility Toolkit (GLUT) (download GLUT) OpenGL Utility Toolkit (GLUT) Nice GLUT Tutorial NEHE: OpenGL tutorial NEHE OpenGL red book