OPENGL.

Slides:



Advertisements
Similar presentations
OPENGL.
Advertisements

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,
Computer Graphics (Fall 2003) COMS 4160, Lecture 6: OpenGL 2 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
CS 248 OpenGL Help Session CS248 Presented by Sean Walker
1 Lecture 5 Rendering 3D graphical primitives. 2 3D Rendering Example:
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
Introduction to OpenGL M. Ramanathan STTP CAD 2011Introduction to OpenGL.
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.
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 2.
Geometric Objects and Transformations Chapter 4. Points, Scalars and Vectors  Points - position in space  Scalars - real numbers, complex numbers obey.
Chapter 4 10 February Agenda Program 2 – Due 2/17 Chapter 4 – transformations GLUT solids.
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.
3D Viewing.
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 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
2 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Fall Computer Engineering METU.
Computing & Information Sciences Kansas State University CIS 536/636 Introduction to Computer Graphics Lecture 4 of 41 William H. Hsu Department of Computing.
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);
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: –
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
NoufNaief.net 1 TA: Nouf Al-Harbi.
Visible Surface Detection
Chap 3 Viewing and Transformation
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
CS552: Computer Graphics Lecture 6: Viewing in 2D.
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.
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.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
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.
3 DIMENSIONAL VIEWING Ceng 477 Introduction to Computer Graphics Computer Engineering METU.
Computer Graphics Lecture 34. OpenGL Programming II Taqdees A. Siddiqi
CSC Graphics Programming
Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran
The Human Visual System vs The Pinhole camera
LAB 5 Drawing Objects Lab 5 Drawing Objects.
Reference1. [OpenGL course slides by Rasmus Stenholt]
“Computer Science is no more about computers than astronomy is about telescopes.” Professor Edsger Dijkstra.
Programming with OpenGL Part 2: Complete Programs
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
OpenGL API 2D Graphic Primitives
Advanced Graphics Algorithms Ying Zhu Georgia State University
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.
גרפיקה ממוחשבת: מבוא ל-OpenGL
Computer Graphics, KKU. Lecture 13
Introduction to OpenGL
LAB 5 Drawing Objects Lab 5 Drawing Objects.
Line and Curve Drawing Algorithms
LAB 5 Drawing Objects Lab 5 Drawing Objects.
CSE 411 Computer Graphics Lecture #8 2D Viewing
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
3D Viewing.
Programming with OpenGL Part 2: Complete Programs
OpenGL A Brief Overview.
Presentation transcript:

OPENGL

OpenGL OpenGL resources: www.opengl.org The Red Book, www.glprogramming.com/red/ Manual pages, www.opengl.org/sdk/docs/man3/

OpenGL OpenGL core library OpenGL Utility (GLU) OpenGL Utility Toolkit (GLUT) Header files: #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h>

#include <GL/glut.h> // (or others, depending on the system in use) void init (void) { glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white. glMatrixMode (GL_PROJECTION); // Set projection parameters. gluOrtho2D (0.0, 200.0, 0.0, 150.0); } void lineSegment (void) { glClear (GL_COLOR_BUFFER_BIT); // Clear display window. glColor3f (0.0, 0.0, 1.0); // Set line segment color to blue. glBegin (GL_LINES); glVertex2i (180, 15); // Specify line-segment geometry. glVertex2i (10, 145); glEnd ( ); glFlush ( ); // Process all OpenGL routines as quickly as possible. void main (int argc, char** argv) { glutInit (&argc, argv); // Initialize GLUT. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode. glutInitWindowPosition (50, 100); // Set top-left display-window position. glutInitWindowSize (400, 300); // Set display-window width and height. glutCreateWindow ("An Example OpenGL Program"); // Create display window. init ( ); // Execute initialization procedure. glutDisplayFunc (lineSegment); // Send graphics to display window. glutMainLoop ( ); // Display everything and wait. OpenGL

OpenGL Point Functions glVertex*( ); * : 2, 3, 4 i (integer) s (short) f (float) d (double) Ex: glBegin(GL_POINTS); glVertex2i(50, 100); glEnd(); Ex: int p1[ ]={50, 100}; glBegin(GL_POINTS); glVertex2iv(p1); glEnd();

OpenGL Line Functions GL_LINES GL_LINE_STRIP GL_LINE_LOOP Ex: glBegin(GL_LINES); glVertex2iv(p1); glVertex2iv(p2); glEnd();

OpenGL glBegin(GL_LINES); GL_LINES GL_LINE_STRIP glVertex2iv(p1); glVertex2iv(p2); glVertex2iv(p3); glVertex2iv(p4); glVertex2iv(p5); glEnd(); GL_LINE_LOOP p3 p3 p5 p1 p1 p2 p4 p2 p4 p3 p5 p1 p2 p4

OpenGL Ex: glBegin(GL_POINTS); GL_POINTS glVertex2iv(p1); glEnd(); glPointSize(size); size: 1,2,… Ex: glPointSize(2.0); // 2x2 pixels p3 p5 p1 p2 p4

OpenGL Polygon Functions glRect*(x1, y1, x2, y2); Ex: Ex: i (integer) s (short) f (float) d (double) v (vector) Ex: glRecti(200, 100, 50, 250); Ex: int v1[ ]={200, 100}; int v2[ ]={50, 250}; glRectiv(v1, v2); (50, 250) (200, 100)

OpenGL Ex: GL_POLYGON glBegin (GL_POLYGON); glVertex2iv(p1); glEnd(); p6 p5 p1 p4 p2 p3 GL_POLYGON

OpenGL Ex: GL_TRIANGLES glBegin (GL_TRIANGLES); glVertex2iv(p1); glEnd(); p6 p5 p1 p4 p2 p3 GL_TRIANGLES

OpenGL Ex: GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN glBegin (GL_TRIANGLES); glVertex2iv(p1); glVertex2iv(p2); glVertex2iv(p6); glVertex2iv(p3); glVertex2iv(p4); glVertex2iv(p5); glEnd(); p6 p5 p1 p4 p2 p3 GL_TRIANGLES p6 p6 p5 p5 p1 p4 p1 p4 p2 p3 p2 p3 GL_TRIANGLE_STRIP GL_TRIANGLE_FAN

OpenGL Ex: GL_QUADS GL_QUAD_STRIP glBegin (GL_QUADS); glVertex2iv(p1); glEnd(); p8 p1 p4 p5 p7 p6 p3 p2 GL_QUADS GL_QUAD_STRIP

OpenGL RGB colors Red Green Blue Color Black 1 Cyan Magenta Yellow Black 1 Cyan Magenta Yellow White

OpenGL Color Functions glClearColor(r, g, b, a); 0.0 ≤ r, g, b, a ≤ 1.0 specifies the color for color buffers glClear(GL_COLOR_BUFFER_BIT); applies the clear color to the color buffers Ex: glClearColor(1.0, 1.0, 1.0, 0.0); // White glColor(GL_COLOR_BUFFER_BIT);

OpenGL Color Functions glColor3f(r, g, b); 0.0 ≤ r, g, b ≤ 1.0 Ex: glColor3f(1.0, 0.0, 0.0); // Red glColor3ub(r, g, b); 0 ≤ r, g, b ≤ 255 glColor3ub(255, 0, 0); // Red

OpenGL Reshape Function glutReshapeFunc(func); Ex: glutReshapeFunc(my_reshape_func);

OpenGL – Transformations Matrix Operations glMatrixMode routine: Projection mode (GL_PROJECTION) Modelview mode (GL_MODELVIEW) Texture mode Color mode

OpenGL – Transformations Modelview Mode glMatrixMode(GL_MODELVIEW); designates 4x4 modelview matrix as the current matrix. glLoadIdentity(); assigns the identity matrix to the current matrix.

OpenGL – Transformations Translate glTranslatef (tx, ty, tz); Scale glScalef (sx, sy, sz); Rotate glRotatef (theta, vx, vy, vz);

OpenGL I Modelview Matrix glColor3f (0.0, 0.0, 1.0); // blue glRecti(0,0,80,40); glFlush ( ); Modelview Matrix I

OpenGL I I.T Modelview Matrix glColor3f (0.0, 0.0, 1.0); // blue glRecti(0,0,80,40); glFlush ( ); Modelview Matrix I glColor3f (1.0, 0.0, 0.0); // red glTranslatef(10.0, 10.0, 0.0); glRecti(0,0,80,40); glFlush ( ); I.T

OpenGL I I.T I.T.S Modelview Matrix glColor3f (0.0, 0.0, 1.0); // blue glRecti(0,0,80,40); glFlush ( ); Modelview Matrix I glColor3f (1.0, 0.0, 0.0); // red glTranslatef(10.0, 10.0, 0.0); glRecti(0,0,80,40); glFlush ( ); I.T glColor3f (1.0, 0.0, 0.0); // red glScalef(2.0, 2.0, 0.0); glRecti(0,0,80,40); glFlush ( ); I.T.S

OpenGL I I.T I.T.S I.T.S.R Modelview Matrix glColor3f (0.0, 0.0, 1.0); // blue glRecti(0,0,80,40); glFlush ( ); Modelview Matrix I glColor3f (1.0, 0.0, 0.0); // red glTranslatef(10.0, 10.0, 0.0); glRecti(0,0,80,40); glFlush ( ); I.T glColor3f (1.0, 0.0, 0.0); // red glScalef(2.0, 2.0, 0.0); glRecti(0,0,80,40); glFlush ( ); I.T.S glColor3f (0.0, 1.0, 0.0); // green glRotatef(20.0, 0.0, 0.0, 1.0); glRecti(0,0,80,40); glFlush ( ); I.T.S.R

OpenGL – Transformations Matrix Stack glPushMatrix (); glPopMatrix ();

OpenGL I I.T I.T.S I.T.R Modelview Matrix Stack glColor3f (0.0, 0.0, 1.0); // blue glRecti(0,0,80,40); glFlush ( ); I glColor3f (1.0, 0.0, 0.0); // red glTranslatef(10.0, 10.0, 0.0); glRecti(0,0,80,40); glPushMatrix(); glFlush ( ); I.T I.T glColor3f (1.0, 0.0, 0.0); // red glScalef(2.0, 2.0, 0.0); glRecti(0,0,80,40); glFlush ( ); I.T.S I.T glColor3f (0.0, 1.0, 0.0); // green glPopMatrix(); glRotatef(20.0, 0.0, 0.0, 1.0); glRecti(0,0,80,40); glFlush ( ); I.T.R

OpenGL Ex: ROBOT transform robot glPushMatrix() transform head draw head glPopMatrix() transform body transform left_arm draw left_arm transform right_arm draw right_arm draw body Ex: ROBOT

OpenGL – 2D Viewing Clipping Window Viewport Function gluOrtho2D (xwmin, xwmax, ywmin, ywmax); float or double Viewport Function glViewport (xvmin, yvmin, vpWidth, vpHeight); integer Window (xwmax, ywmax) Viewport vpHeight vpWidth (xvmin, yvmin) (xwmin, ywmin)

OpenGL – 2D Viewing Create Display Window Display Window Mode glutInit (&argc, &argv) glutInitWindowPosition (xTopLeft, yTopLeft) glutInitWindowSize (width, height) windowID=glutCreateWindow (“title”) glutDestroyWindow (windowID) Display Window Mode glutInitDisplayMode (mode) Ex: glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

OpenGL – 2D Viewing Display Functions glutDisplayFunction (dFunc) dFunc (callback function) describes what is to be displayed in the current window glutPostRedisplay () Indicates that the contents of the current display window should be renewed glutIdleFunc (iFunc) iFunc is executed when there are no other events for the system to process glutMainLoop ()

OpenGL – 3D Viewing Projection glMatrixMode (GL_PROJECTION) Orthogonal Projection gluOrtho (xwmin, xwmax, ywmin, ywmax, dnear, dfar) Perspective Projection gluPerspective (theta, aspect, dnear, dfar) theta: field-of-view angle (00 – 1800) aspect: aspect ratio of the clipping window (width/height) dnear, dfar: positions of the near and far planes (must have positive values) glFrustum (xwmin, xwmax, ywmin, ywmax, dnear, dfar) if xwmin = -xwmax and ywmin = -ywmax then symmetric view volume

OpenGL – 3D Viewing glMatrixMode (GL_MODELVIEW) gluLookAt (x0, y0, z0, xref, yref, zref, Vx, Vy, Vz) Designates the origin of the viewing reference frame as, the world coordinate position P0=(x0, y0, z0) the reference position Pref=(xref, yref, zref) and the viewup vector V=(Vx, Vy, Vz) double-precision, floating-point values xw yw zw xv yv zv V P0 N Pref

OpenGL – 3D Viewing gluLookAt (x0, y0, z0, xref, yref, zref, Vx, Vy, Vz) Default parameters are: P0=(0,0,0) Pref=(0,0,-1) V=(0,1,0) xw yw zw xv yv zv V P0 Pref

OpenGL float eye_x, eye_y, eye_z; void init (void) { glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white. glMatrixMode (GL_PROJECTION); // Set projection parameters. glLoadIdentity(); gluPerspective(80.0, 1.0, 50.0, 500.0); // degree, aspect, near, far glMatrixMode (GL_MODELVIEW); eye_x = eye_y = eye_z = 60; gluLookAt(eye_x, eye_y, eye_z, 0,0,0, 0,1,0); // eye, look-at, view-up } void main (int argc, char** argv) { glutInit (&argc, argv); // Initialize GLUT. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode. glutInitWindowPosition (50, 500); // Set top-left display-window position. glutInitWindowSize (400, 300); // Set display-window width and height. glutCreateWindow ("An Example OpenGL Program"); // Create display window. glViewport (0, 0, 400, 300); init ( ); // Execute initialization procedure. glutDisplayFunc (my_objects); // Send graphics to display window. glutReshapeFunc(reshape); glutKeyboardFunc(keybrd); glutMainLoop ( ); // Display everything and wait.

OpenGL Functions Cube glutSolidCube (size) glutWireCube (size) Cone glutSolidCone (base_radius, height, slices, stacks) glutWireCone (base_radius, height, slices, stacks) Sphere glutSolidSphere (radius, slices, stacks) glutWireSphere (radius, slices, stacks) Teapot glutSolidTeapot (size) glutWireTeapot (size) Torus glutSolidTorus (inner_radius, outer_radius, nsides, rings) glutWireTorus (inner_radius, outer_radius, nsides, rings)

OpenGL Functions Tetrahedron glutSolidTetrahedron () glutWireTetrahedron () 4 sided Octahedron glutSolidOctahedron () glutWireOctahedron () 8 sided Dodecahedron glutSolidDodecahedron () glutWireDodecahedron () 12 sided Icosahedron glutSolidIcosahedron () glutWireIcosahedron () 20 sided

OpenGL glPolygonMode(GL_FRONT_AND BACK, GL_LINE) glEnable (GL_FOG) displays both visible and hidden edges glEnable (GL_FOG) glFogi (GL_FOG_MODE, GL_LINEAR) generates fog effect. Uses depth cueing. glEnable (GL_CULL_FACE) glDisable (GL_CULL_FACE) glCullFace (mode) used for backface removal GL_BACK (default), GL_FRONT (if inside a building), GL_FRONT_AND_BACK

OpenGL glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH) initializes GLUT, requests a depth buffer and a frame buffer glClear (GL_DEPTH_BUFFER_BIT) initially depth buffer has the background color. It must be cleared every time before displaying a new frame. glEnable (GL_DEPTH_TEST) glDisable (GL_DEPTH_TEST) to activate depth buffer routines glClearDepth (maxDept) to set maxDepth to another value (0.0 – 1.0) glDepthRange (nearNormDept, farNormDept) to adjust normalization values (defaults: near- 0.0, far- 1.0)

Diffuse and Specular Lightings I = Idiff + Ispec = ka Ia + kd Il (N.L) + ks Il (N.H)ns

OpenGL GLfloat pos[ ] = {x, y, z, w} (x, y, z): position w: type 1.0 – local (positional light) 0.0 – distant (directional light) glLightfv(GL_LIGHT1, GL_POSITION, pos) GL_POSITION sets the light position and type glEnable (GL_LIGHT1) glDisable (GL_LIGHT1)

OpenGL GLfloat color[ ] = {r, g, b, a} (r, g, b): RGB color (I) a: alpha (used only if color blending routines are active) glLightfv(GL_LIGHT1, GL_AMBIENT, color) glLightfv(GL_LIGHT1, GL_DIFFUSE, color) glLightfv(GL_LIGHT1, GL_SPECULAR, color) GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR set the light color

OpenGL GLfloat dir[ ] = {x, y, z} 1.0 if light source is not spotlight fl,angatten = 0.0 if Vobj.Vlight=cos a < cos ql (Vobj . Vlight )al otherwise OpenGL GLfloat dir[ ] = {x, y, z} (x, y, z): direction vector Vlight default direction (0.0, 0.0, -1.0) glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, dir) glLightfv(GL_LIGHT2, GL_SPOT_CUTOFF, c) c: cut off angle θ (0o-90o) glLightfv(GL_LIGHT2, GL_SPOT_EXPONENT, e) e: exponent value for angular intensity attenuation (0-128) Vlight Vobj ql a

OpenGL GLfloat ka[ ] = {r, g, b, a} GLfloat kd[ ] = {r, g, b, a} GLfloat ks[ ] = {r, g, b, a} (r, g, b): RGB color (k) a: alpha glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ka) glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, kd) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, ks) glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, s) s: specular reflection exponent (0-128) GLfloat emissioncolor[ ] = {r, g, b, a} glMaterialfv(GL_FRONT, GL_EMISSION, emissioncolor)

OpenGL glColor4f(R, G, B, A) glEnable (GL_BLEND) glDisable (GL_BLEND) A: alpha parameter 1.0 – transparent 0.0 - opaque glEnable (GL_BLEND) glDisable (GL_BLEND)