Graphics Programming (I)

Slides:



Advertisements
Similar presentations
Programming with OpenGL - Getting started - Hanyang University Han Jae-Hyek.
Advertisements

OpenGL Open a Win32 Console Application in Microsoft Visual C++.
Chapter 2: Graphics Programming
Computer Graphics CSCE 441
Pemrograman OpenGL Dasar
© 2004, Tom Duff and George Ledin Jr1 Lectures OpenGL Introduction By Tom Duff Pixar Animation Studios Emeryville, California and George Ledin Jr Sonoma.
CS 4731 Lecture 2: Intro to 2D, 3D, OpenGL and GLUT (Part I) Emmanuel Agu.
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 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.
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 2.
Using OpenGL in Visual C++ Opengl32.dll and glu32.dll should be in the system folder Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib.
2 COEN Computer Graphics I Introductions n Brad Grantham lecturer lab dude n Dave Shreiner lecturer slave driver.
Programming with OpenGL Part 1: Background
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
Computer Graphics CS 385 January 31, Fractals Some definitions Object which is self-similar at all scales. Regardless of scale the same level of.
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
Introduction to GL Geb Thomas. Example Code int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
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.
CA 302 Computer Graphics and Visual Programming Lecture 2: Introduction to OpenGL Aydın Öztürk
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
NoufNaief.net TA: Nouf Al-harbi.
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
OpenGL: Introduction #include main() { OpenWindow() ; glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0,
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 고려대학교 컴퓨터 그래픽스 연구실.
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.
Graphics Programming. Graphics Functions We can think of the graphics system as a black box whose inputs are function calls from an application program;
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
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
Introduction to OpenGL (IDE: Eclipse)
The Human Visual System vs The Pinhole camera
Programming with OpenGL Part 1: Background
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
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Lab 3 Geometric Drawing Lab 3 Geometric Drawing.
גרפיקה ממוחשבת: מבוא ל-OpenGL
Programming with OpenGL Part 1: Background
Drawing in the plane 455.
Programming with OpenGL Part 3: Three Dimensions
Introduction to OpenGL
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 1: Background
Programming with OpenGL Part 3: Three Dimensions
Programming with OpenGL Part 2: Complete Programs
CS297 Graphics with Java and OpenGL
Programming with OpenGL Part 2: Complete Programs
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Programming with OpenGL Part 3: Three Dimensions
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Presentation transcript:

Graphics Programming (I) 姜明 北京大学数学科学学院 Based on [EA], Chapter 2. 更新时间2018年9月22日星期六4时43分5秒

Outline The Sierpinski Gasket Programming 2D Applications The OpenGL API

The Sierpinski Gasket The Sierpinski gasket is the intersection of all the sets in this sequence, that is, the set of points that remain after this construction is repeated infinitely often. http://ecademy.agnesscott.edu/~lriddle/ifs/siertri/siertri.htm http://astronomy.swin.edu.au/~pbourke/fractals/gasket/ Sierpinski, Warclaw. "Sur une courbe dont tout point est un point de ramification," Compt. Rendus Acad. Sci. Paris 160 (1915), 302-305.

The Sierpinski gasket is an object that can be defined recursively or randomly. However, it has properties that are not at all random.

Chaos game Consider 3 vertices P1, P2, and P3 of a triangle as shown on the left. Starting at any point on the plane, point 0, choose a vertex randomly of the three vertices. Draw a point on the plane half way between the current point and the vertex chosen, point 1, this now becomes the current point. Repeat this process indefinitely, each time drawing a point halfway between the current point and a randomly chosen vertex. http://ecademy.agnesscott.edu/~lriddle/ifs/siertri/siertri.htm http://astronomy.swin.edu.au/~pbourke/fractals/gasket/

Result of the Game http://ecademy.agnesscott.edu/~lriddle/ifs/siertri/siertri.htm http://astronomy.swin.edu.au/~pbourke/fractals/gasket/

Real World Gaskets http://ecademy.agnesscott.edu/~lriddle/ifs/siertri/siertri.htm http://astronomy.swin.edu.au/~pbourke/fractals/gasket/

Sierpinski Algorithm Given 3 vertices of a triangle Starting at any point P on the plane, initial point, choose a vertex V randomly. Draw a point M half way P and V. M now becomes the current point, P. Repeat this process, each time drawing a point halfway between the current point and a randomly chosen vertex.

Pseudo-code We use the drawing of the Sierpinski gasket as a sample problem. A possible form for our 1st program might be as the left. Although our final OpenGL program will have a different organization, it will be almost that simple. main () { initialize_the_system (); for (some_number_of_points) pt = generate_a_point (); display_the_point (pt); } clean_up ();

Sierpinski carpet Instead of removing the central third of a triangle, the central square piece is removed from a square sliced into thirds horizontally and vertically. http://astronomy.swin.edu.au/~pbourke/fractals/gasket/

Fractal Antenna Fractal antenna based upon the carpet replaces the usual rubbery stalk. A fractal arrangement can combine the robustness of a random array and the efficiency of a regular array with a much lower number of elements. http://astronomy.swin.edu.au/~pbourke/fractals/gasket/ http://www.hbci.com/~wenonah/cfa/fractal.htm http://www.fractenna.com/

Core of the Implementation Core: generating and displaying points. How do we represent points in space? Should we use a 2D, 3D, or other representation?

Programming 2D Applications We often use the term vertex, rather than point. We represent a point as [x y z] in the 3D world. We regard 2D plane as a subspace of 3D space. We can represent a point in the 2D plane as [x y 0], or [x y]. OpenGL allows us to use either representations.

OpenGL has multiple forms for many functions. If the user wants to work in 2D with integer coordinates, the following form is appropriate glvertex2i (GLint xi, GLint yi); The following form specifies a vertex in 3D space glvertex3f (GLfloat x, GLfloat y, GLfloat z); If we use an array to store the information for the 3D vertex GLfloat p[3]; then we can use glvertex3fv (p). In OpenGL, we often use basic OpenGL types, such as GLfloat and GLint. Use of the OpenGL types allows flexibility for implementation and portability.

Vertices can defined a variety of geometric objects; different numbers of vertices are required depending on the object. We can group as many vertices as we wish, using the functions glBegin and glEnd. The argument of glBegin specifies the geometric type that we want our vertices to define.

Now we can implement the display for the Sierpinski gasket program. A line segment can be defined by glBegin (GL_LINES) glvertex2f (x1, y1); glvertex2f (x2, y2); glEnd; A pair of points can be defined by glBegin (GL_POINTS) glvertex2f (x1, y1); glvertex2f (x2, y2); glEnd; Now we can implement the display for the Sierpinski gasket program.

void display( void ) { typedef GLfloat point2[2]; /* define a point data type */ point2 vertices [3] = {{0.0,0.0}, {250.0,500.0}, {500.0,0.0}}; /* a triangle */ int i, j, k; int rand(); /* standard random number generator */ point2 p = {75.0, 50.0}; /* an arbitrary initial point inside triangle */ glClear (GL_COLOR_BUFFER_BIT); /*clear the window */ /* compute and plots 5000 new points */ for ( k=0; k<5000; k++) j = rand () % 3; /* pick a vertex at random */ /* compute point halfway between selected vertex and old point */ p[0] = (p[0] + vertices[j][0]) / 2.0; p[1] = (p[1] + vertices[j][1]) / 2.0; /* plot new point */ glBegin (GL_POINTS); glVertex2fv (p); glEnd(); } glFlush(); /* clear buffers */

The call to glFlush ensures that points are rendered to the screen as soon as possible. If we leave it out, the program should work correctly, but you may notice a delay in a busy or networked environment.

Other Issues In what colors are we drawing? Where does our image appear on the screen? How large will the image be? How do we create an area of the screen – a window – for our image? How much of the 2D plane will appear on the screen? How long will the image remain on the screen?

Coordinate Systems How to interpret the values of x, y and z in the specification of vertices? In what units are they? Where is the origin? Answer: it is up to you!

Originally, graphics systems required the specification directly in units of the display device. Many problems with this method. The most critical one: using the distance on the computer screen to describe natural objects. The advent of device-independent graphics freed programmers from worrying about the details of input and output devices. The user’s coordinate system became known as the world coordinate system or the application or problem coordinate system. We can use any (float) numbers that fit our application.

Units on the display device were first called physical-device coordinantes or just device coordinates. For raster devices, such as most CRT displays, we use the term raster coordinates or screen coordinates. Screen coordinates are always expressed in some integer type, because pixels are inherently discrete and we can address them using integers.

At some point, the values in world coordinates must be mapped into device coordinates. The graphics system is responsible for this task and the mapping is performed automatically as part of the rendering process. The user only needs to specify only a few parameters – the area of the world to see and the size of the display – to define this mapping.

The OpenGL API Graphics Functions The Graphics State Machines The OpenGL Interface

We use OpenGL API for those purposes. We want to gain control how our objects appear on the display the flow of the program interaction with the window system We use OpenGL API for those purposes.

Graphics Functions Our basic model of a graphics package is a black box. We can take the simplified view of inputs as function call and outputs as primitives displayed on our CRT screen. We describe an API through the functions in its library. It is helpful to divide them into seven groups by their functionality.

The primitive functions define the low-level objects or atomic entities that can be displayed. Attribute functions govern the way that a primitive appears on the display. Viewing functions allow us to specify various views. Transformation functions allow us to carry out transformations of objects. Input functions allow us to deal with diverse forms of input. The control functions enable us to communicate with the windows system and/or operating system. Inquiry functions enable us to use information within API, including camera parameters or values in the frame buffer.

The Graphics State Machines We can think of the entire graphics system as a state machine, a black box that contains a finite state machine. It has inputs from the application program that may change the state of the machine or cause the machine to produce a visible output. From the perspective of the API, graphics functions are of two types: those that define primitives that flow through a pipeline inside the state machine: e.g., glVertex, etc. those that change the state of the machine: the others in OpenGL.

In OpenGL, most parameters are persistent; their values remains unchanged until we explicitly change them through functions that change the state. E.g., once we set a color, that color remains the present color until it is changed through a color-altering function. There can be annoying consequences if we neglect to make state changes when needed or lose track of the state.

The OpenGL Interface High performance, window independent 3D graphics API. Designed by SGI, 1982. No commands for windowing tasks and user interaction. Portable, device independent. C library of about 350 functions. All function names begin with gl. All constant names begin with GL. There a few related libraries that we also use.

GLU: graphics utility library; use only GL functions; create common objects; available in all OpenGL implementation. GLUT: GL Utility Toolkit; minimal functionality in windowing systems. GLX for X Window; WGL for Microsoft Windows.

OpenGL makes heavy use of macros to increase code readability and to avoid the use of magic numbers. Strings such as GL_LINES and GL_POINTS are defined in header files. Using #include <GL/glut.h> or #include <glut.h> is sufficient to read in glut.h, gl.h and glu.h.

OpenGL命名规范 函数名以gl, glu, glut开始,其中每一成份的首写字母大写,如glPolygonMode (); 常数名以GL, GLU, GLUT开始,中间以下划线_分离,如GLUT_RGBA.

[gl,glu,glut]<Command>[234][sifd][v](args) 函数名的一般结构 [gl,glu,glut]<Command>[234][sifd][v](args) [234]表示参数个数 [sifd]表示参数类型 [v]使用指针参数

OpenGL Data Type Suffix Number of bits C- type OpenGL-type b 8 char GLbyte s 16 short int GLshort i 32 long int GLint f d 64 float double Glfloat GLdouble ub unsigned char GLubyte us unsigned short GLushort ui unsigned long GLuint

The Gasket Program The C file is here. The Program is here. To compile it, issue cl gasket.c in a command console.

Implementation #include <GL/glut.h> void main(int argc, char** argv) { /* Standard GLUT initialization */ glutInit (&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default */ glutInitWindowSize (500,500); /* 500 x 500 pixel window */ glutInitWindowPosition (0,0); /* place window top left corner */ glutCreateWindow ("Sierpinski Gasket"); /* window title */ glutDisplayFunc(display); /* display callback invoked when window opened */ myinit(); /* set attributes (state variables) */ glutMainLoop(); /* enter event loop */ }

void myinit(void) { /* attributes */ glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */ glColor3f(1.0, 0.0, 0.0); /* draw in red */ /* set up viewing, camera */ /* 500 x 500 clipping window with lower left coner at (0.0,0.0), world coordinates */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 500.0, 0.0, 500.0); glMatrixMode(GL_MODELVIEW); }

Window Management Five routines necessary for initializing a window glutInit (&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default */ glutInitWindowSize (500,500); /* 500 x 500 pixel window */ glutInitWindowPosition (0,0); /* place window top left on display */ glutCreateWindow ("Sierpinski Gasket"); /* window title */ Five routines necessary for initializing a window