CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 2.

Slides:



Advertisements
Similar presentations
Chapter 2: Graphics Programming
Advertisements

Pemrograman OpenGL Dasar
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Programming with OpenGL Part 3: Three Dimensions.
Informationsteknologi Monday, October 29, 2007Computer Graphics - Class 21 Today’s class Graphics programming Color.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D 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 CSC461 Lecture 7: 3D Programming in OpenGL Objectives: Develop 2D and 3D examples -- Sierpinski gasket: a fractal Develop 2D and 3D examples -- Sierpinski.
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]
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
Basic OpenGL Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, September 10, 2003.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
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.
Graphics Systems and OpenGL. Business of Generating Images Images are made up of pixels.
Computer Graphics CS 385 January 31, Fractals Some definitions Object which is self-similar at all scales. Regardless of scale the same level of.
Graphics Programming Chapter 2. CS 480/680 2Chapter 2 -- Graphics Programming Introduction: Introduction: Our approach is programming oriented. Our approach.
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,
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
1 Programming with OpenGL Part 3: Three Dimensions Yuanfeng Zhou Shandong University.
Computer Graphics I, Fall 2010 Programming with OpenGL Part 3: Three Dimensions.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Programming with OpenGL Review.
CS 480/680 Computer Graphics Programming with Open GL Part 6: Three Dimensions Dr. Frederick C Harris, Jr. Fall 2011.
1 Chapter 2 Graphics Programming. 2 Using OpenGL in Visual C++ – 1/3 Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib and glu32.lib.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor.
Graphics: Conceptual Model
CS552: Computer Graphics Lecture 6: Viewing in 2D.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 3: Three Dimensions Ed Angel Professor of Computer Science,
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 15 Creating 3D Models.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 6: Three Dimensions Ed Angel Professor.
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
Introduction to Graphics Programming. Graphics API.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
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.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Chapter (2)1 Computer Graphics - Chapter 2 Graphics Programming Objectives are to learn about: An example: The Sierpinski Gasket The OpenGL API Primitives.
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
The Human Visual System vs The Pinhole camera
Programming with OpenGL Part 2: Complete Programs
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
Programming with OpenGL Part 3: Three Dimensions
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
Graphics Programming (I)
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Lab 3 Geometric Drawing Lab 3 Geometric Drawing.
Drawing in the plane 455.
Programming with OpenGL Part 3: Three Dimensions
Introduction to OpenGL
Interactive Computer Graphics Graphics Programming
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 3: Three Dimensions
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Presentation transcript:

CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 2

2-Dimensional Drawing with OpenGL  Two-dimensional objects are a special case of three-dimensional figures  The drawing is limited (by the programmer) to a plane  Viewing is normally an orthogonal view, perpendicular to the drawing plane

Sierpinski Gasket  A simple but interesting example  Start with any triangle 1. Pick an internal point at random 2. Pick a vertex at random 3. Find the midpoint between 1 and 2 4. Display this point 5. Replace initial point with this one 6. Repeat from step 2

Sierpinski Gasket Construction

OpenGL Program for Sierpinski Gasket main() { initialize_the_system(); for (some_number_of_points) { pt = generate_a_point(); display_the_point(pt); } cleanup(); return 0; }

Points or Vertices  Points are represented by vectors with an entry for each coordinate  p = (x, y, z) in 3 dimensions  p = (x, y, 0) gives 2 dimensions by always setting z to 0  OpenGL allows up to 4 dimensions  Internal representation is always the same

OpenGL Vertices  Vertex creating functions have general name glVertex*  The suffix is 2 or 3 characters –Number of dimensions: 2, 3, or 4 –Data type: i = integer, f = float, d = double –Optional v if pointer

Underlying Representation  OpenGL data types are defined in header file #define GLfloat float so a header might look like glVertex2i(GLint xi, GLint yi) or glVertex3f(GLfloat xf, GLfloat yf, GLfloat zf)

Representation (cont.)  For the vector form GLfloat vertex[3]; and then use glVertex3fv(vertex);

Defining Geometric Objects  Objects are defined by collections of point constructors bounded by calls to glBegin and glEnd  A line is defined by glBegin(GL_LINES); glVertex2f(x1, y1); glVertex2f(x2, y2); glEnd();

OpenGL Code for Sierpinski Gasket  See p. 41 of text  Code leaves many open questions by using default values 1.colors 2.image position 3.size 4.clipping? 5.persistence

A Resulting Image

Coordinate System  Early systems depended on specific device mapping  Device-independent graphics broke link  Use application or problem coordinate system to define image  Use device coordinates, raster coordinates, screen coordinates for device

Coordinates  Application coordinates can be integer or real and multi-dimensional  Screen or raster coordinates are always integer and essentially 2- dimensional  Graphics program maps application coordinates onto device coordinates

App to Device Mapping

Classes for OpenGL Functions 1. Primitives – draw points, line segments, polygons, text, curves, surfaces 2. Attributes – specify display characteristics of objects: color, fill, line width, font 3. Viewing – determine aspects of view: position and angle of camera, view port size, …

Function Classes (cont) 4. Transformations – change appearance or characteristics of objects: rotate, scale, translate 5. Input – handle keyboard, mouse, etc. 6. Control – communicate with window system 7. Inquiry – get display information: size, raster value, …

OpenGL Interface  Graphics Utility Interface (GLU) –Creates common objects like spheres  GL Utility Toolkit (GLUT) –Provides generic interface to window system  GLX for Unix/Linux and wgl for Microsoft Windows –provide low-level glue to window system

Library Organization

Using Libraries  Header files –#include –#include  On some systems the GL/ is not used

Primitives  OpenGL supports both geometric primitives and raster primitives

Geometric Primitives  Points = GL_POINTS –vertex displayed with size >= 1 pixel  Line segments = GL_LINES –defined by pairs of vertices as endpoints of segments  Polygons = GL_LINE_STRIPE or GL_LINE_LOOP –Loop is closed, stripe is not

Primitive Examples

Properties of Polygons  Defined by line loop border  Simple if no edges cross  Convex if every line segment connecting pair of points on boundary or inside lies completely inside

Polygon Types in OpenGL  Polygons are either filled regions (default) or boundaries  Set with glPolygonMode  To get polygon with boundary must draw twice, once as filled and once as boundary or line loop

Special Polygons  GL_TRIANGLES, GL_QUADS –Groups of 3 or 4 points are grouped as triangles or quadrilaterals

Special Polygons  GL_TRIANGLE_STRIPE, GL_QUAD_STRIPE, GL_TRIANGEL_FAN –Contiguous stripe or fan of triangles or quadrilaterals

Drawing a Sphere  Draw great circles  Fill between latitudes with quad strips  Fill caps with triangle fans  Code on p.52

Text  May be raster – from bit map –Fast –Does not scale well –Poor in rotation other than 90 o  Vector – from drawn curves –Slow to draw –Scales, rotates, etc. well

Raster text

Color  The physiology of vision leads to 3- color theory  Any color can be produced by a combination of red, green, and blue at intensities that produce the same response in the cones as the true color

Colors in OpenGL  Colors are stored using 4 attributes, RGBA  A = Alpha channel –Controls opacity or transparency  Color values can be integers in range from 0 to max component value –0 – 255 for 24-bit color  Real numbers between 0.0 and 1.0

Colors in OpenGL (cont)  Colors are set with glColor* functions –* is two characters, nt  n = 3 or 4 color values  t = date type: i, f, etc.

Clearing Frame Buffer  To get predictable results a program must first clear the frame buffer glClearColor(1.0, 1.0, 1.0, 1.0) –sets color to white

Indexed Color  OpenGL also supports indexed color –Saves space when only a limited number of distince colors are used

Indexed Color (cont)  Set color in table with glutSetColor(int color, GLfloat red, GLfloat blue, GLfloat green) GLfloat blue, GLfloat green)  Access color in table with glIndexi(element)

Viewing  Based on synthetic camera model  If nothing is specified, there are default viewing parameters –Rarely used –Would force us to fit model world to camera  Prefer flexibility of setting viewing parameters

Two-Dimensional Viewing  Selected rectangle from 2- dimensional world is displayed  Called viewing rectangle or clipping rectangle

Viewing Volume  2-dimensional viewing is special case of 3-dimensional viewing –viewing volume  Default is 2 x 2 x 2 cube centered cube centered at (0,0,0)

Orthographic Projection  Projects point (x,y,z) onto (x,y,0)  View is perpendicular to plane x=0  Set viewing rectagle with void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top) GLdouble top)

Matrix Modes  Graphic pipelines perform matrix transformations on images at each stage  Most important matrices are –model-view –projection  State includes both

Manipulating Mode Matrices  Matrix mode operations operate on matrix for currently selected mode –Model-view is default  Mode is set with glMatrixMode(mode) mode = GL_PROJECTION, GL_MODELVIEW, etc.  Always return to model-view to insure consistency

Control Functions  Depend on particular window system  GLUT provides standard set of basic operations –We will consider only these

Window Control  Operations only on display window for the program  Initialization – glutInit  Creation – glutCreateWindow  Display mode –glutInitDisplayMode –RGB or indexed –hidden-surface removal –singer or double buffering

Example Code glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(480, 480); glutWindowPosition(0, 0); glutCreateWindow(“sample");

Aspect Ratio and Viewports  Ratio of width to length is aspect ratio  If aspect ratio of viewing rectangle and window differ, image will be stretched and distorted  Viewport defines region of screen in which to display image –Can eliminate distortion

Distortion

Viewport  Set viewport with void glViewport(GLint x, GLint y, GLsizei w, GLsizei h)

GLUT Main Loop  If we simply run an OpenGL program it will display the image and exit –may not allow time to see it –could sleep program to keep window open but this is limited solution  GLUT provides controls to avoid this  Keep program running waiting for event void glutMainLoop(void)

Glut Display  To display an image, code a function to create the image and have GLUT call it  image drawing function takes no arguments and returns no result –Any parameters must be passed through global variables void glutDisplayFunc(void (*func)(void))

Simple Main Program #include void main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutWindowPosition(0, 0);

Simple Main Program (cont) glutCreateWindow(“My Image"); glutDisplayFunc(display); myinit(); glutMainLoop(); }

Program Structure  Key components –initialization –display callback function –main  Gasket Program..\code\gasket.c..\code\gasket.c – 2-d..\code\gasket.c..\code\gasket2.c..\code\gasket2.c – 3-d with colors..\code\gasket2.c../code/gasket3.c../code/gasket3.c – 3-d with polygons../code/gasket3.c

Hidden Surface Removal  Z-buffer algorithm  Z coordinate determines depth  Point nearer camera obscures one with greater depth  Steps –Init with GLUT_DEPTH –glEnable(GL_DEPTH_TEST  can also disable –clear before redrawing

Sample HSR Program void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) tetrahedron(n) glFlush(); }