OpenGL and GLUT Review Daniel Ritchie Monday, October 3 rd, 2011.

Slides:



Advertisements
Similar presentations
Computer Graphics - Graphics Programming -
Advertisements

OpenGL Open a Win32 Console Application in Microsoft Visual C++.
OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
Chapter 2: Graphics Programming
#include int line_width = 1; void Display( void ) { glEnable( GL_LINE_STIPPLE ); glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT);
© 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.
OpenGL (I). What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D objects.
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
Computer Graphics CS 385 February 7, Fundamentals of OpenGl and Glut Today we will go through the basics of a minimal OpenGl Glut project, explaining.
Write a Simple Program with OpenGL & GLUT. Books and Web Books OpenGL Programming Guide (Red-book) OpenGL Reference Manual (Blue-book) OpenGL Super Bible.
Reference1. [OpenGL course slides by Rasmus Stenholt]
CS380 LAB I OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003.
COS 397 Computer Graphics Assoc. Prof. Svetla Boytcheva AUBG 2013 COS 397 Computer Graphics Practical Session №1 Introduction to OpenGL, GLFW and CG.
Drawing Basic Graphics Primitives Lecture 4 Wed, Sep 3, 2003.
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
Lecture 3 OpenGL.
Write a Simple Program with OpenGL & GLUT. Books OpenGL Programming Guide (Red-book) OpenGL Reference Manual (Blue-book) OpenGL Super Bible
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
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,
Modeling with OpenGL Practice with OpenGL transformations.
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.
Intro to OpenGL (Version 2) Geb Thomas. Setting Up GLUT You will need GLUT for opening windows We can use the version made by Nate Robins: –
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor.
GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window.
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
Lecture 2 Review OpenGL Libraries Graphics Overview Rendering Pipeline OpenGL command structure.
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.
CS559: Computer Graphics Lecture 12: OpenGL: ModelView Li Zhang Spring 2010.
OpenGL Basic Drawing Jian-Liang Lin A Smidgen of OpenGL Code #include main() { InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear.
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.
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.
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.
CS 480/680 Computer Graphics Programming with Open GL Part 2: Complete Programs Dr. Frederick C Harris, Jr. Fall 2011.
Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran
Basic Program with OpenGL and GLUT
CS380 Lab Spring Myungbae Son.
Computer Graphics Lecture 33
Reference1. [OpenGL course slides by Rasmus Stenholt]
Programming with OpenGL Part 2: Complete Programs
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
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.
Lab 3 Geometric Drawing Lab 3 Geometric Drawing.
Starting to draw dealing with Windows which libraries? clipping
גרפיקה ממוחשבת: מבוא ל-OpenGL
Rendering Pipeline, OpenGL/GLUT
Coordinate Systems and Transforming the Coordinates
Drawing in the plane 455.
Introduction to OpenGL
OpenGL program.
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Starting to draw dealing with Windows which libraries? clipping
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

OpenGL and GLUT Review Daniel Ritchie Monday, October 3 rd, 2011

Outline Build Environment – Visual Studio – Xcode Structure of a GLUT application – Initialization – Callbacks Simple OpenGL drawing – Clearing – Specifying Vertices – Drawing Primitives

Getting Started with Visual Studio Get Visual Studio 2010 Professional – Students: full version at – Express Edition also okay Might also need to install Windows SDK Download and install GLUT – Details on wiki – GLUT_HowTo

Getting Started with Xcode developer.apple.com/tools/Xcode – (or App Store, if you run Lion) – Includes developer tools (gcc, gdb, etc.) and IDE Using Xcode IDE – Add reference to GLUT.framework Using Makefiles g++ -o myApp -framework GLUT -framework OpenGL myApp.cpp

Structure of a GLUT Application

GLUT Application Structure Typical #include s GLUT application main() function Callbacks – Display – Reshape – Mouse – Keyboard

Typical #include s // General: #include...

Typical #include s // General: #include...

Typical #include s - Windows // Windows users: #include... If using Windows headers, include them before GL, GLUT

Typical #include s - Windows // Windows users: #include... If using Windows headers, include them before GL, GLUT

Typical #include s - MacOS // MacOS X users: #include... GL, GLUT headers live in different directories

Typical #include s - MacOS // MacOS X users: #include... GL, GLUT headers live in different directories

GLUT main() function - initialization int main(int argc, char *argv[]) { // Allow GLUT to “intercept” command-line arguments: glutInit( &argc, argv ); // Read your own command-line arguments... // Set GLUT options and create window: glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH ); glutInitWindowPosition(20, 20); glutInitWindowSize(640, 480); glutCreateWindow(“Window Title”);... }

glutInitDisplayMode(unsigned int mode) “or” together various flags for mode – GLUT_RGBA Can also use GLUT_INDEX – GLUT_ALPHA Request an alpha channel in frame buffer – GLUT_DEPTH, GLUT_STENCIL, GLUT_ACCUM Request additional buffers Depth buffer very useful for 3D drawing – GLUT_SINGLE, GLUT_DOUBLE Choose single (default) or double-buffering – GLUT_MULTISAMPLE, GLUT_STEREO Even more advanced options!

GLUT main() – register callbacks int main(int argc, char *arv[]) { // Initialize //you define these callbacks above in main.cpp glutDisplayFunc( &MyDisplayCallback ); glutReshapeFunc( &MyReshapeCallback ); glutKeyboardFunc( &MyKeyboardCallback ); glutMouseFunc( &MyMouseCallback );... }

GLUT main() – enter main loop int main(int argc, char *arv[]) { // Initialize... // Register callbacks glutMainLoop(); // Finalize... return 0; } Runs “forever” … until Close window Call exit() Crash

GLUT callbacks Allow your code to respond to events – Keyboard, mouse, etc. Ordinary C functions, just need correct signature Called from “inside” glutMainLoop() You will need some globals… – Textures, resources, animation parameters, etc. – Globals are Evil, but *some* unavoidable with GLUT – IDEAL: Package up in a C++ object/struct (i.e. “State” class or “Controller” class)

Display Callback void MyDisplayCallback(void){…} – Called to redraw contents of window Only called when window system decides – Need to force redraw? glutPostRedisplay() Steps: 1) Clear window 2) Perform any updates on state before drawing 3) Use GL to draw 4) Make drawing visible to user – glFlush() if using GLUT_SINGLE – glutSwapBuffers() if using GLUT_DOUBLE

Reshape Callback void MyReshapeCallback(int w, int h) { // Save width and height to global variables gWidth = w; gHeight = h; // Update OpenGL viewport to cover whole window: glViewport( 0, 0, w, h ); // Update OpenGL projection matrix: glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluOrtho2D(0, w, 0, h); // more on next slide... // Tell GLUT to draw the window: glutPostRedisplay(); }

Setting the projection matrix gluOrtho2D(-1, 1, -1, 1) – Drawing in 2D – (0,0) is at center of screen gluOrtho2D(0, w, 0, h) – 2D drawing with exact pixel coordinates gluPerspective(fov, (float) w / h, near, far) – 3D drawing with pinhole camera – fov is field of view in degrees (try 60) – (float) w / h is aspect ratio – near, far distances determine visible range Precision increases when range is smaller

Keyboard Callback void MyKeyboardCallback(unsigned char key, int x, int y) { // (x,y) tells where mouse when when key pressed switch(key) { case ‘a’:... case ‘q’: exit(0); break; default: } // If state was updated, tell GLUT to re-draw the window glutPostRedisplay(); } Arrow, Function keys handled by glutSpecialFunc Shift, Ctrl, etc. can be determined by glutGetModifiers

Mouse Button Callback void MyMouseCallback(int button, int state, int x, int y){…} button is one of GLUT_{LEFT, RIGHT, MIDDLE}_BUTTON state is one of GLUT_UP, GLUT_DOWN Beware: mouse coordinates might not match your GL coordinates Assignment 1 Default Setup (0,0) (0,1) (1,0) GLUT mouse (0,0) (0,h) (0,w) (w,h) (1,1)

Simple OpenGL Drawing

Clearing Simplest case: Clear window to a solid color Do this before rendering anything else If doing 3D with GLUT_DEPTH, probably want: glClearColor(r, g, b, a); glClear(GL_COLOR_BUFFER_BIT); glClearColor(r, g, b, a); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Drawing Primitives Pick type of primitive you want to draw, and begin: Submit zero or more vertices: End the primitive: glBegin(GL_LINES); glVertex2f(0, 0); glVertex2f(1, 1); // First line. glVertex2f(0, 0); glVertex2f(1, 1); // Second line. glEnd();

Submitting Vertices Each call to glVertex submits a new vertex Actually a family of functions The main ones you will need are: Position of new vertex specified with arguments Other attributes must be set separately… glVertex2f(float x, float y); glVertex3f(float x, float y, float z);

Vertex Attributes Every vertex can have many attributes – Position and color are enough for assignment 1 Position is specified with glVertex arguments Color and other attributes specified separately Color of new vertex comes from most recent glColor glColor4f(float r, float g, float b, float a);

Primitive Types glBegin(???); glVertex2f(0.0, 0.0); glVertex2f(0.5, 0.0); glVertex2f(0.75, 0.5); glVertex2f(1.0, 1.0); glVertex2f(0.5, 1.0); glVertex2f(0.0, 0.5); glEnd(); (0,0) (0,1) (1,0) (1,1)

Primitive Types - Points glBegin(GL_POINTS); glVertex2f(0.0, 0.0); glVertex2f(0.5, 0.0); glVertex2f(0.75, 0.5); glVertex2f(1.0, 1.0); glVertex2f(0.5, 1.0); glVertex2f(0.0, 0.5); glEnd(); (0,0) (0,1) (1,0) (1,1)

Primitive Types - Lines glBegin(GL_LINES); glVertex2f(0.0, 0.0); glVertex2f(0.5, 0.0); glVertex2f(0.75, 0.5); glVertex2f(1.0, 1.0); glVertex2f(0.5, 1.0); glVertex2f(0.0, 0.5); glEnd(); (0,0) (0,1) (1,0) (1,1)

Primitive Types – Line Strip glBegin(GL_LINE_STRIP); glVertex2f(0.0, 0.0); glVertex2f(0.5, 0.0); glVertex2f(0.75, 0.5); glVertex2f(1.0, 1.0); glVertex2f(0.5, 1.0); glVertex2f(0.0, 0.5); glEnd(); (0,0) (0,1) (1,0) (1,1)

Primitive Types – Line Loop glBegin(GL_LINE_LOOP); glVertex2f(0.0, 0.0); glVertex2f(0.5, 0.0); glVertex2f(0.75, 0.5); glVertex2f(1.0, 1.0); glVertex2f(0.5, 1.0); glVertex2f(0.0, 0.5); glEnd(); (0,0) (0,1) (1,0) (1,1)

Primitive Types – Triangles glBegin(GL_TRIANGLES); glVertex2f(0.0, 0.0); glVertex2f(0.5, 0.0); glVertex2f(0.75, 0.5); glVertex2f(1.0, 1.0); glVertex2f(0.5, 1.0); glVertex2f(0.0, 0.5); glEnd(); (0,0) (0,1) (1,0) (1,1)

Primitive Types – Polygon glBegin(GL_POLYGON); glVertex2f(0.0, 0.0); glVertex2f(0.5, 0.0); glVertex2f(0.75, 0.5); glVertex2f(1.0, 1.0); glVertex2f(0.5, 1.0); glVertex2f(0.0, 0.5); glEnd(); (0,0) (0,1) (1,0) (1,1)

Working with the OpenGL Machine OpenGL is a state machine – GL commands trigger state transitions, actions Your program is generating the input string – Effectively constructing GL program on the fly This means conditionals or loops in your program are not seen by OpenGL. You can use this to your advantage to structure your program

Examples Put a loop inside a glBegin / glEnd pair: Conditionally set pieces of state: // Draw a circle. glBegin(GL_LINE_LOOP); for(int i = 0; i < 100; ++i) { float theta = 2*PI*(i / 100.0f); glVertex2f(cosf(theta), sinf(theta)); } glEnd(); // Draw selected shape in red. if(shape == gSelectedShape) { glColor3f(1, 0, 0); } glBegin(...);

Questions?