670 likes | 891 Views
Chapter 2 Graphics Programming. Using OpenGL in Visual C++ – 1/3. Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib and glu32.lib should be in the lib folder for VC++ gl.h and glu.h should be in a folder called GL under the include folder for VC++
E N D
Using OpenGL in Visual C++ – 1/3 Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib and glu32.lib should be in the lib folder for VC++ gl.h and glu.h should be in a folder called GL under the include folder for VC++ Get glut32.lib, glut32.dll and glut.h from the course homepage and put them in the same places as the other files
Using OpenGL in Visual C++ – 2/3 Fire up visual studio Create a new project as the following:File New Project (input your project name, then a directory (workspace) with the same name will be built) Win32 Console Application An Empty Application
Using OpenGL in Visual C++ – 3/3 In the workspace click on “File View” to access (expand) the source code tree In “Source Files”, add in the source code (*.c files) Select Project Settings Link andin “Object/library modules” add “Opengl32.lib glu32.lib glut32.lib” Press “F7” to build “your_project.exe”
Sierpinski Gasket – 1/2 Pick an initial point at random inside the triangle Select one of the three vertices at random Find the point halfway between the initial point and the randomly selected vertex Display the new point Replace the initial point with this new point Return to step 2
Sierpinski Gasket – 2/2 main() { initialize_the_system(); for(some_number_of_points) { pt = generate_a_point(); display_the_point(pt); } cleanup(); }
Programming 2D Applications • A vertex is a location in space • glVertex* • * is in the form of nt or ntv • n is the number of dimensions • t denotes the data type:integer (i), float (f), or double (d); pointer to an array (v) • #define GLfloat float
Examples glVertex2i(GLint xi, GLint yi) glVertex3f(GLfloat x, GLfloat y, GLfloat z) GLfloat vertex[3]glVertex3fv(vertex)
OpenGL Object Examples glBegin(GL_LINES); glVertex2f(x1, y1); glVertex2f(x2, y2);glEnd(); glBegin(GL_POINTS); glVertex2f(x1, y1); glVertex2f(x2, y2);glEnd();
Code of Sierpinski Gasket – 1/3 Typedef GLfloat point2[2]; void display(void) { point2 vertices[3]={{0,0},{250,500},{500,0}}; /* an arbitrary triangle */ static point2 p = {75,50}; /* any desired initial point */ int j, k; int rand(); /* standard random-number generator */
Code of Sierpinski Gasket – 2/3 for(k=0;k<5000;k++) { j=rand()%3; /* pick a random vertex from 0, 1, 2 */ p[0]=(p[0]+vertices[j][0])/2; /* compute new point */ p[1]=(p[1]+vertices[j][1])/2; glBegin(GL_POINTS); glVertex2fv(p); /* display new point */ glEnd(); } glFlush(); }
Questions In what colors are we drawing Where on the screen does our image appear How large will the image be How do we create an area of the screen – a window – for our image? How long will the image remain on the screen?
Graphics Functions – 2/3 Primitive functions: points, line segments, polygons, pixels, text, curves, surfaces Attributes functions:color, pattern, typeface Viewing functions:position, orientation, clipping
Graphics Functions – 3/3 Transformation functions:rotation, translation, scaling Input functions:keyboards, mice, data tablets Control functions:communicate with windows, initialization, error handling Inquiry functions: number of colors, camera parameters/values
Graphics Pipeline and State Machine • Functionalities of graphics functions: • Define primitive: glvertex* • Change opengl state (most of them!) • Present color, current matrix
OpenGL Interface GL (OpenGL in Windows) GLU (graphics utility library)uses only GL functions, creates common objects (such as spheres) GLUT (GL Utility Toolkit)interfaces with the window system GLX: glue between OpenGL and Xwindow, used by GLUT
API Dilemma for Primitives • Minimal or maximal? • Convenience versus portability • OpenGL’s intermediate approach: • GL contains a small set of basic primitives • GLU contains richer set of objects
OpenGL Primitives Geometric primitives and raster primitives
Polygon Basics – 1/3 Filled objects Methods of displaying a polygon
Polygon Basics – 2/3 Simple Nonsimple Simple, convex, and flat
Polygon Basics – 3/3 Convexity Convex objects
Polygon Types in OpenGL – 1/2 In OpenGL, edges and interior must be drawn separately!
Polygon Types in OpenGL – 2/2 Use triangles if possible because of efficiency!
Drawing a Sphere – 2/5 z x y
Drawing a Sphere – 3/5 Drawing the portion of lower latitudes c=M_PI/180.0; // degrees to radians, M_PI=3.14159… for(phi=-80.0; phi<=80.0; phi+=20.0) { glBegin(GL_QUAD_STRIP); for(theta=-180.0; theta<=180.0; theta+=20.0) { x=sin(c*theta)*cos(c*phi); y=cos(c*theta)*cos(c*phi); z=sin(c*phi); glVertex3d(x, y, z); x=sin(c*theta)*cos(c*(phi+20.0)); y=cos(c*theta)*sin(c*(phi+20.0)); z=sin(c*(phi+20.0)); glVertex3d(x, y, z); } glEnd(); }
Drawing a Sphere – 4/5 Drawing the portion around the north pole x=y=0; z=1; glBegin(GL_TRIANGLE_FAN); glVertex3d(x, y, z); c=M_PI/180.0; z=sin(c*80.0); for(theta=-180.0; theta<=180.0; theta+=20.0) { x=sin(c*theta)*cos(c*80.0); y=cos(c*theta)*sin(c*80.0); glVertex3d(x, y, z); } glEnd();
Drawing a Sphere – 5/5 Drawing the portion around the south pole x=y=0; z=-1; glBegin(GL_TRIANGLE_FAN); glVertex3d(x, y, z); z=-sin(c*80.0); for(theta=-180.0; theta<=180.0; theta+=20.0) { x=sin(c*theta)*cos(c*80.0); y=cos(c*theta)*sin(c*80.0); glVertex3d(x, y, z); } glEnd();
Color – 1/4 C=T1R+T2G+T3B, T1, T2,T3 are the tristimulus values Additive color matching
Color – 2/4 Basic tenet of three-color theory:If two colors produce the same tristimulus values, then they are visually indistinguishable The range of colors that we can produce on a given system is called that system’s color gamut
Color – 3/4 Color Solid
Color – 4/4 Subtractive Color Additive Color
RGB Color • glColor3f(1.0, 0.0, 0.0):Can be used to specify 24 bits true colors • Alpha channel:Used in OpenGL as an opacity or transparency value • glClearColor(1.0, 1.0,1.0,1.0) • glPointSize(2.0)
Indexed Color Color-lookup table Example: k=m=8: pick 256 out of 16M colors
Two-dimensional Viewing Objects before clipping Image after clipping
Viewing Volume OpenGL default: 222 cube
Orthographic View void glOrtho(GLdouble left, GLdouble right, …)
Matrix Mode • There are two matrices in OpenGL: • Model-view: defines COP and orientation • Projection: defines the projection matrix • glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0, 500.0, 0.0, 500.0);glMatrixMode(GL_MODELVIEW);
Control Functions OpenGL assumes origin is bottom left glutInit(int *argcp, char **argv); glutCreateWindow(char *title); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowSize(480,640); glutInitWindowPosition(0,0); OpenGL default: RGB color, no hidden-surface removal, single buffering
Aspect Ratio Viewing rectangle glOrtho(…) Display window glutInitWindowSize(…)
Viewports void glViewport(GLint x, GLint y, GLsizei w, GLsizei h) Viewport is part of the state.
main, display, myinit functions … void glutMainLoop(void)Why do we need this? void glutDisplayFunc(void (*func)(void))When is the function invoked?