OpenGL CS418 Computer Graphics John C. Hart. OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when.

Slides:



Advertisements
Similar presentations
OpenGL Open a Win32 Console Application in Microsoft Visual C++.
Advertisements

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
Computer Graphics CSCE 441
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 Basics Donghui Han. Assignment Grading Visual Studio Glut Files of four types needed: – Source code:.cpp,.h – Executable file:.exe (build in release.
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,
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
ITEPC 06 - Workshop on Fractal Creation Chiew-Lan Tai and Oscar Au.
Programming with OpenGL Part 1: Background Mohan Sridharan Based on slides created by Edward Angel CS4395: Computer Graphics 1.
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.
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.
Chi-Cheng Lin, Winona State University CS430 Computer Graphics Graphics Programming and OpenGL.
Computer Graphics Bing-Yu Chen National Taiwan University.
COS 397 Computer Graphics Assoc. Prof. Svetla Boytcheva AUBG 2013 COS 397 Computer Graphics Practical Session №1 Introduction to OpenGL, GLFW and CG.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
Graphics Architectures & OpenGL API Introduction Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
OpenGL A Brief Overview. What is OpenGL? It is NOT a programming language. It is a Graphics Rendering API consisting of a set of functions with a well.
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.
Programming with OpenGL Part 1: Background
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
 “OpenGL (Open Graphics Library) is a standard specification defining a cross- language cross-platform API for writing applications that produce 2D and.
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 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);
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.
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
NoufNaief.net TA: Nouf Al-harbi.
GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window.
CSCE 441: Computer Graphics
Introduction to OpenGL Programming
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 Based on GL (graphics library) by Silicon Graphics Inc. (SGI) Advantages: Runs on everything, including.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
The Human Visual System vs The Pinhole camera
Programming with OpenGL Part 1: Background
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.
OpenGL A Brief Overview.
Lab 3 Geometric Drawing Lab 3 Geometric Drawing.
Starting to draw dealing with Windows which libraries? clipping
גרפיקה ממוחשבת: מבוא ל-OpenGL
Coordinate Systems and Transforming the Coordinates
Programming with OpenGL Part 1: Background
Drawing in the plane 455.
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 1: Background
Starting to draw dealing with Windows which libraries? clipping
Programming with OpenGL Part 2: Complete Programs
OpenGL A Brief Overview.
Presentation transcript:

OpenGL CS418 Computer Graphics John C. Hart

OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when event occurs Example: Declare a function myMouse to respond to mouse click Register it: Tell OpenGL to call it when mouse clicked Code? glutMouseFunc(myMouse);

GL Utility Toolkit (GLUT) OpenGL is window system independent Concerned only with drawing No window management functions (create, resize, etc) Very portable GLUT: Minimal window management: fast prototyping Interfaces with different windowing systems Allows easy porting between windowing systems

Program Structure Configure and open window (GLUT) Initialize OpenGL state Register input callback functions (GLUT) Render Resize Input: keyboard, mouse, etc My initialization Set background color, clear color, drawing color, point size, establish coordinate system, etc. glutMainLoop( ) Waits here infinitely till action is selected

GLUT: Opening a window GLUT used to open window glutInit(&argc, argv); initializes glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); sets display mode (e.g. single buffer with RGB) glutInitWindowSize(640,480); sets window size (WxH) glutInitPosition(100,150); sets upper left corner of window glutCreateWindow(“my first attempt”); open window with title “my first attempt”

OpenGL Skeleton void main(int argc, char** argv){ // First initialize toolkit, set display mode and create window glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); glutCreateWindow(“my first attempt”); // … then register callback functions, // … do my initialization //.. wait in glutMainLoop for events }

GLUT Callback Functions Register all events your program will react to Event occurs => system generates callback Callback: routine system calls when event occurs No registered callback = no action

GLUT Callback Functions GLUT Callback functions in skeleton glutDisplayFunc(myDisplay): window contents need to be redrawn glutReshapeFunc(myReshape): called when window is reshaped glutMouseFunc(myMouse): called when mouse button is pressed glutKeyboardFunc(mykeyboard): called when keyboard is pressed or released glutMainLoop( ): program draws initial picture and enters infinite loop till event

Example: Rendering Callback Do all your drawing in the display function Called initially and when picture changes (e.g.resize) First, register callback in main( ) function glutDisplayFunc( display ); Then, implement display function void display( void ) { // put drawing stuff here ……. glBegin( GL_LINES ); glVertex3fv( v[0] ); glVertex3fv( v[1] ); …………… glEnd(); }

OpenGL Skeleton void main(int argc, char** argv){ // First initialize toolkit, set display mode and create window glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(100, 150); glutCreateWindow(“my first attempt”); // … now register callback functions glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); glutMouseFunc(myMouse); glutKeyboardFunc(myKeyboard); myInit( ); glutMainLoop( ); }

OpenGL Based on GL (graphics library) by Silicon Graphics Inc. (SGI) Advantages: Runs on everything, including cell phones (OpenGL/ES) Alternatives: Microsoft’s Direct3D – limited to MSWindows Sun’s Java3D – slower, implemented on top of OpenGL

APP GLUT Library Layers OpenGL = GL + GLU –Basic low-level GL routines implemented using OS graphics routines –Timesaving higher-level GLU routines implemented using GL routines GLUT opens and manages OpenGL windows and adds helper functions OpenGL Extensions provide direct device-dependent access to hardware GL GLU OS Driver HW EXT

Vertex Pipeline Homogeneous Divide Model Coords Model Xform World Coords Viewing Xform Still Clip Coords Clipping Window Coordinates Window Coordinates Window to Viewport Window to Viewport Coordinates Viewport Coordinates Clip Coords Viewing Coords Perspective Distortion

Viewport Coordinates Physical per-pixel integer coordinates Also called screen or device coordinates glViewport(x,y,w,h) x,y – lower left pixel w – width h – height Sometimes (0,0) is in the upper left corner (e.g. for mouse input) (0,0)(HRES-1,0) (HRES-1, VRES-1) (0,VRES-1)

Window Coordinates Logical, mathematical floating-point coordinates glOrtho(l,r,b,t,n,f) left, right, bottom, top near, far: limits depth gluOrtho2D(l,r,b,t) calls glOrtho(l,r,b,t,-1,1) To use per-pixel coordinates, call: gluOrtho2D(-.5,-.5,w-.5,h-.5); (-1,-1)(1,-1) (1,1) (-1,1)

OpenGL State OpenGL commands change its internal state variables which control how it turns geometric models into pictures E.g. the window-to-viewport transformation glViewport(x,y,w,h) glOrtho(l,r,b,t,n,f) Can query this state: GLfloat buf[4]; glGetFloatv(GL_VIEWPORT,buf); x = buf[0]; y = buf[1]; w = buf[2]; h = buf[3]; (l,b)(r,b) (r,t)(l,t) (x,y)(x+w-1,y) (x+w-1,y+h-1) (x,y+h-1) W2V OpenGL State: x,y,w,h,l,r,b,t,n,f,…

OpenGL Commands gl[u]Fubar[234][dfis][v] [u]: GLU v. GL command [234]: dimension [dfisb]: data type –d: GLdouble (double) –f: GLfloat (float) –i: GLint (int) –s: string [v]: vector (reference v. value) Examples: glVertex3f(-0.5, , 2); glVertex2i(200, 350); GLdouble point[4]; glVertex4dv(point); Examples: glVertex3f(-0.5, , 2); glVertex2i(200, 350); GLdouble point[4]; glVertex4dv(point);

Points glBegin(GL_POINTS); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

Lines glBegin(GL_LINES); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

Line Strip glBegin(GL_LINE_STRIP); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

Line Loop glBegin(GL_LINE_LOOP); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

Polygon glBegin(GL_POLYGON); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1) OpenGL only supports convex polygons (and really only triangles)

Quads glBegin(GL_QUADS); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

Quads glBegin(GL_QUADS); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glEnd(); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.);

Quads glBegin(GL_QUADS); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glEnd(); (-1,1) (-1,-1)(1,-1) (1,1) (-1,1) Lines should never pass through a vertex.

Triangles glBegin(GL_TRIANGLES); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

Triangles glBegin(GL_TRIANGLES); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.6,1.); glVertex2f(-.2,.6); glVertex2f(.6,1.); glVertex2f(-.2,.6); glVertex2f(.2,.6); glVertex2f(.6,1.); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); … glEnd(); (-1,-1)(1,-1) (1,1) (-1,1) Lines should never pass through a vertex.

Triangle Strip glBegin(GL_TRIANGLE_STRIP); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1)(1,-1) (1,1) (-1,1)

Triangle Strip glBegin(GL_TRIANGLE_STRIP); glVertex2f(-.6,1.); glVertex2f(.6,1.); glVertex2f(-.2,.6); glVertex2f(.2,.6); glVertex2f(-.2,-.6); glVertex2f(.2,-.6); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glEnd(); … (-1,-1)(1,-1) (1,1) (-1,1) First two vertices prime the pump, then every new vertex creates a triangle connecting it to the previous two vertices

Triangle Fan glBegin(GL_TRIANGLE_FAN); glVertex2f(-.2,.6); glVertex2f(-.6,.6); glVertex2f(-.6,1.); glVertex2f(.6,1.); glVertex2f(.2,.6); glVertex2f(.2,-.6); glVertex2f(-.2,-.6); glEnd(); … (-1,-1)(1,-1) (1,1) (-1,1) First two vertices prime the pump, then every new vertex creates a triangle connecting it to the previous vertex and the first vertex

Assigning Color glColor3f(0,0,1); glBegin(GL_POLYGON); glVertex2f(-1,1); glVertex2f(-1,-1); glVertex2f(1,-1); glEnd(); glColor3f(1,0,0); glBegin(GL_POLYGON); glVertex2f(-1,1); glVertex2f(-1,-1); glVertex2f(1,-1); glEnd(); glColor3f(0,0,0); glBegin(GL_LINE_LOOP); glVertex2f(-1,1); glVertex2f(-1,-1); glVertex2f(1,-1); glEnd(); glBegin(GL_POLYGON); glColor3f(0,1,0); glVertex2f(-1,1); glColor3f(0,0,1); glVertex2f(-1,-1); glColor3f(1,0,0); glVertex2f(1,-1); glEnd();

Indexed Face Set Popular file format –VRML, Wavefront “.obj”, etc. Ordered list of vertices –Prefaced by “v” (Wavefront) –Spatial coordinates x,y,z –Index given by order List of polygons –Prefaced by “f” (Wavefront) –Ordered list of vertex indices –Length = # of sides –Orientation given by order v x 1 y 1 z 1 v x 2 y 2 z 2 v x 3 y 3 z 3 v x 4 y 4 z 4 f f (x1,y1,z1)(x1,y1,z1) (x2,y2,z2)(x2,y2,z2) (x3,y3,z3)(x3,y3,z3) (x4,y4,z4)(x4,y4,z4)

Other Attributes Vertex normals –Prefixed w/ “vn” (Wavefront) –Contains x,y,z of normal –Not necessarily unit length –Not necessarily in vertex order –Indexed as with vertices Texture coordinates –Prefixed with “vt” (Wavefront) –Not necessarily in vertex order –Contains u,v surface parameters Faces –Uses “/” to separate indices –Vertex “/” normal “/” texture –Normal and texture optional –Can eliminate normal with “//” v x 0 y 0 z 0 v x 1 y 1 z 1 v x 2 y 2 z 2 vn a 0 b 0 c 0 vn a 1 b 1 c 1 vn a 2 b 2 c 2 vt u 0 v 0 vt u 1 v 1 vt u 2 v 2 (x 0,y 0,z 0 ) (a 0,b 0,c 0 ) (u 0,v 0 ) (x1,y1,z1)(a1,b1,c1)(u1,v1)(x1,y1,z1)(a1,b1,c1)(u1,v1) (x2,y2,z2)(a2,b2,c2)(u2,v2)(x2,y2,z2)(a2,b2,c2)(u2,v2) f 0/0/0 1/1/1 2/2/2f 0/0/0 1/0/1 2/0/2