Computer graphics MN1

Similar documents
Computer graphics MN1

OpenGL. Toolkits.

Precept 2 Aleksey Boyko February 18, 2011

Books, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, PointClouds, and G3D

Computer graphics MN1

Books, OpenGL, GLUT, CUDA, OpenCL, OpenCV, PointClouds, G3D, and Qt

Outline. Other Graphics Technology. OpenGL Background and History. Platform Specifics. The Drawing Process

Graphics and Computation Introduction to OpenGL

API for creating a display window and using keyboard/mouse interations. See RayWindow.cpp to see how these are used for Assignment3

Lecture 4 of 41. Lab 1a: OpenGL Basics

UNIT 7 LIGHTING AND SHADING. 1. Explain phong lighting model. Indicate the advantages and disadvantages. (Jun2012) 10M

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

Graphics Pipeline & APIs

Graphics Pipeline & APIs

Introduction to OpenGL

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

Basic Graphics Programming

Introduction to OpenGL

Introduction to Computer Graphics with OpenGL/GLUT

Programming with OpenGL Part 2: Complete Programs Computer Graphics I, Fall

Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

Programming using OpenGL: A first Introduction

Order of Transformations

Modeling Transform. Chapter 4 Geometric Transformations. Overview. Instancing. Specify transformation for objects 李同益


Computer Graphics. Transformations. CSC 470 Computer Graphics 1

Basic Graphics Programming

Computer Graphics. OpenGL

RECITATION - 1. Ceng477 Fall

C OMPUTER G RAPHICS Thursday

Interaction Computer Graphics I Lecture 3

CS559: Computer Graphics. Lecture 12: OpenGL Transformation Li Zhang Spring 2008

11/1/13. Basic Graphics Programming. Teaching Assistant. What is OpenGL. Course Producer. Where is OpenGL used. Graphics library (API)

Lecture 2 CISC440/640 Spring Department of Computer and Information Science

CS559: Computer Graphics. Lecture 12: OpenGL Li Zhang Spring 2008

Teacher Assistant : Tamir Grossinger Reception hours: by - Building 37 / office -102 Assignments: 4 programing using

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

2 Transformations and Homogeneous Coordinates

CS 4204 Computer Graphics

Computer Graphics 1 Computer Graphics 1

Graphics Programming

Lecture 2 2D transformations Introduction to OpenGL

OpenGL/GLUT Intro. Week 1, Fri Jan 12

An Interactive Introduction to OpenGL and OpenGL ES Programming. Ed Angel Dave Shreiner

Computer Graphics Introduction to OpenGL

Understand how real-world lighting conditions are approximated by OpenGL

Lecture 3. Understanding of OPenGL programming

An Interactive Introduction to OpenGL Programming

Computer Graphics Primitive Attributes

Computer Graphics. Bing-Yu Chen National Taiwan University

Computer Graphics, Chapt 08

Programming with OpenGL Part 1: Background

Introduction to OpenGL Week 1

Objectives. Image Formation Revisited. Physical Approaches. The Programmer s Interface. Practical Approach. Introduction to OpenGL Week 1

OpenGL refresher. Advanced Computer Graphics 2012

Lectures OpenGL Introduction

Visualizing Molecular Dynamics

Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Exercise 1 Introduction to OpenGL

CSC 470 Computer Graphics

Interaction. CSCI 480 Computer Graphics Lecture 3

Source code: #include <iostream>

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

1 /* 4 C:\opencv\build\include. 6 C:\opencv\build\x86\vc10\lib

RASTERISED RENDERING

Computer Graphics. Chapter 10 Three-Dimensional Viewing

COMP 371/4 Computer Graphics Week 1

OpenGL. Jimmy Johansson Norrköping Visualization and Interaction Studio Linköping University

Computer Graphics Programming

Transformation, Input and Interaction. Hanyang University

OpenGL. 1 OpenGL OpenGL 1.2 3D. (euske) 1. Client-Server Model OpenGL

C++ is Fun Part 13 at Turbine/Warner Bros.! Russell Hanson

Scientific Visualization Basics

Computer Graphics. Chapter 7 2D Geometric Transformations

Pearce Outline. OpenGL Background and History. Other Graphics Technology. Platform Specifics. The Drawing Process

Display Lists in OpenGL

Transformation Pipeline

COMPUTER GRAPHICS LAB # 3

Shading in OpenGL. Outline. Defining and Maintaining Normals. Normalization. Enabling Lighting and Lights. Outline

1 (Practice 1) Introduction to OpenGL

VR-programming tools (procedural) More VRML later in this course! (declarative)

5.2 Shading in OpenGL

Lecture 3 Advanced Computer Graphics (CS & SE )

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

Interaction. CSCI 420 Computer Graphics Lecture 3

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

Introduction to OpenGL

by modifying the glutinitwindowsize() function you can change the screen size to whatever you please.

Rendering Pipeline/ OpenGL

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

Introduction to OpenGL. CSCI 4229/5229 Computer Graphics Fall 2012

Graphics Programming. August 31, Programming of the Sierpinski gasket. Programming with OpenGL and C/C++

CS 4731 Lecture 3: Introduction to OpenGL and GLUT: Part II. Emmanuel Agu

20 GLuint objects; 36 Scale += 0.1; 37 break; 38 case GLUT_KEY_DOWN:

Computer Graphics CS 543 Lecture 5 (Part 2) Implementing Transformations

Illumination Model. The governing principles for computing the. Apply the lighting model at a set of points across the entire surface.

Computer Graphics Course 2005

CS Computer Graphics: Intro to OpenGL

Transcription:

Computer graphics MN1 http://www.opengl.org Todays lecture What is OpenGL? HowdoI useit? Rendering pipeline Points, vertices, lines, polygons Matrices and transformations Lighting and shading Code examples OpenGL Open Graphics Library Graphics API similar to DirectX, Java3D OpenGL 2.0 released September 2004 Delivered with UNIX, Win9x/2000/Me/Nt/Xp, Mac OS Utilizes the window system and event handling of the OS Often hardware supported in graphics cards Language bindings to C, C++, Java, Fortran, Python, Perl, Toolkits GLU (OpenGL Utility Library) Support for NURBS, Quadrics, etc. Delivered with OpenGL GLUT (OpenGL Utility Toolkit) Simplifies window handling User interface functions Can be downloaded from opengl.org OpenGL Extensions allows new hardware innovations to be accessible through OpenGL MESA http://www.mesa3d.org/ Mesa is a 3-D graphics library with an API which is very similar to that of OpenGL. Mesa is not a licensed implementation of OpenGL and has not been tested by the OpenGL conformance tests. You can consider Mesa to be OpenGL ; you can use OpenGL documentation, for example. Goals for OpenGL Industry-wide acceptance Consistent implementations Innovative implementations Long life High quality 1

Why/Why not OpenGL + Supporting OS + Open (kind of) + Free versions (Mesa) + Widely used - Can be hard to use in structured programming. State machine. A good idea is to wrap OpenGL into objects. Application program Pipeline Transform Project Clip Pixel operations Frame buffer Pipeline State machine OpenGL is designed as a state machine Input and outputs (geometric primitives, bitmaps) The state machine converts the inputs to an output image The resulting image depends on the current state Example: colors, shading, texture, Primitives in OpenGL OpenGL is using a right hand system Vertices are defined using glvertex*() where * can be 2f, 2d, 3f, 3d,... Ex.: void glvertex2f( GLfloat vx, GLfloat vy ); GLfloat typedefined as float Primitive defining statements all starts with glbegin(<primitive_type>) and ends with glend() Colors, normals, texture coordinates can be specified for each vertex 2

Primitives in OpenGL Some primitive types GL_POINTS GL_LINES GL_LINESTRIP GL_TRIANGLES GL_QUADS GL_POLYGON Example 1 Example 2 glbegin(gl_line_loop); 4 3 glbegin(gl_line_strip); 4 3 glvertex2f(-0.5,-0.5); // 1 glvertex2f(-0.5,-0.5); // 1 glvertex2f( 0.5,-0.5); // 2 glvertex2f( 0.5,-0.5); // 2 glvertex2f( 0.5, 0.5); // 3 glvertex2f( 0.5, 0.5); // 3 glvertex2f(-0.5, 0.5); // 4 1 2 glvertex2f(-0.5, 0.5); // 4 1 2 3

Example 3 4 5 Example 3 glbegin(gl_triangle_strip); glvertex3f(-0.5,-0.5,0.0); // 1 glvertex3f(0.5,-0.5,0.0); // 2 6 glvertex3f(-0.5,-0.5,0.0); // 1 glvertex3f(0.5,-0.5,0.0); // 2 4 glvertex3f(0.25, 0.5,0.0); // 3 glvertex3f(-0.5, 1.25, 0.0); // 4 3 glvertex3f(0.25, 0.5,0.0); // 3 glvertex3f(0.5, 0.75, 0.0); // 4 3 glvertex3f(0.5, 1.25, 0.0); // 5 glvertex3f(0.25, 0.75, 0.0); // 6 1 2 1 2 Color Color per vertex glcolor3f(1.0,0.0,0.0); // red glvertex3f(); glcolor3f(); glvertex3f(); glcolor3f(); glvertex3f(); glcolor3f(); glnormal3f(); glvertex3f(); glcolor3f(); glnormal3f(); glvertex3f(); Normals glcolor3f(); gltexcoord2f(); glnormal3f(); glvertex3f(); Texture coordinates 4

Transformations OpenGL provides matrix stacks Two most important stacks: GL_MODELVIEW GL_PROJECTION We also have GL_TEXTURE and GL_COLOR Have to select which transformation matrix to modify: glmatrixmode( mode ) The current transformation matrix is the product of the above The matrices are 4x4 Transformations To set the matrix we can use or glloadmatrixf( GLfloat *ptr ); Rotation, translation, and scaling glrotatef(angle, vx, vy, vz); gltranslatef(dx, dy, dz); glscalef(sx, sy, sz); Your own matrix glmultmatrixf( GLfloat *ptr ); Example: Rotation about a line glmatrixmode(gl_modelview); // identity matrix gltranslatef(4.0, 5.0, 6.0); glrotatef(45.0, 1.0, 2.0, 3.0); gltranslatef(-4.0, -5.0, -6.0); C=T(4.0,5.0,6.0) R(45,1.0,2.0,3.0) T(-4.0,-5.0,-6.0) q=cp The transformation specified last is the one applied first Camera model Position Direction (look at) View up vector glulookat(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz); eye up at Projections Projections Orthographic projection glortho(left, right, bottom, top, near, far); Perspective projection glfrustum(xmin, xmax, ymin, ymax, near, far); gluperspective(fovy,aspect,near,far); 5

Example glmatrixmode( GL_PROJECTION ); gluperspective(65.0, 1.0, 1.0, 20.0 ); glmatrixmode (GL_MODELVIEW ); glulookat(1,-4,-4,0,0,0,0,1,0); Callback functions Used for input and interaction The user submits a pointer to a function that should be called when the corresponding event occurs GLUT provides an easy-to-use interface Callback functions A very simple program (program1.cpp) glutmousefunc(function); // click mouse glutmotionfunc(function); // move mouse (button pressed) glutpassivemotionfunc(function); // (no button pressed) glutreshapefunc(function); // window resize glutkeyboardfunc(function); // keyboard glutspecialfunc(function); // arrows, pgup, pgdwn, etc. glutidlefunc(function); // animation glutdisplayfunc(function); // draw primitives #ifdef WIN32 #include <windows.h> #endif #include <GL/gl.h> #include <GL/glut.h> using namespace std; void initglut( int &argc, char **argv) glutinit( &argc,argv ); glutinitdisplaymode( GLUT_SINGLE GLUT_RGB ); glutinitwindowsize( 700, 700 ); glutinitwindowposition( 100, 100 ); glutcreatewindow( Program1 ); void initgl() glclearcolor( 0.0, 0.0, 0.0, 0.0 ); glmatrixmode( GL_PROJECTION ); glortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0 ); glmatrixmode (GL_MODELVIEW ); 6

void draw() float point2[2] = 0.5, 0.75; glclear( GL_COLOR_BUFFER_BIT ); glcolor3f( 1.0, 1.0, 1.0 ); glbegin( GL_POLYGON ); glvertex2f( 0.25, 0.25 ); glvertex2fv( point2 ); glvertex2f( 0.75, 0.5 ); glvertex2f( 0.5, 0.5 ); glflush(); int main(int argc, char **argv) initglut(argc, argv); initgl(); // Set the display callback function to draw(). glutdisplayfunc( draw ); //Start the GLUT main Loop. glutmainloop(); return 0; Z-Buffering void initglut( int &argc, char **argv) glutinit( &argc,argv ); glutinitdisplaymode( GLUT_SINGLE GLUT_RGB GLUT_DEPTH); // z-buffer glutinitwindowsize( 700, 700 ); glutinitwindowposition( 100, 100 ); glutcreatewindow( TITLE ); Z-Buffering Z-Buffering void initgl() glclearcolor( 0.0, 0.0, 0.0, 0.0 ); glmatrixmode( GL_PROJECTION ); glortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0 ); glmatrixmode (GL_MODELVIEW ); // Enable z-buffering glenable(gl_depth_test); void draw() glclear( GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT ); glvertex3f(); glflush(); 7

Lightsources and shading // lighting glenable(gl_lighting); glenable(gl_light0); // 0-GL_MAX_LIGHTS (3377) // set position, colors, direction, distance attenuation gllightfv(gl_light0, ); // shading glshademodel(gl_smooth); // Gouraud shading //glshademodel(gl_flat); Material properties Use material instead of color per face or vertex Set ambient, diffuse, specular, emission and shininess You can use color per face or vertex with glenable(gl_color_material); glmaterialfv(gl_front, GL_DIFFUSE, diff_ptr); glmaterialfv(gl_front, GL_SPECULAR, spec_ptr); glmateriali(gl_front, GL_SHININESS, 100); // culling glenable(gl_cull_face); glcullface(gl_back); Display lists in OpenGL Allows to store OpenGL commands in a list to be called later. Example: generation of a sphere Assume we have the function generatesphere() that computes coordinates of a unit sphere using sines and cosines. Display lists in OpenGL void generatesphere() for (j=0;j<n/2;j++) t1 = phi1 + j * (phi2 - phi1) / (n/2); t2 = phi1 + (j + 1) * (phi2 - phi1) / (n/2); for (i=0;i<=n;i++) t3 = theta1 + i * (theta2 - theta1) / n; e.x = cos(t1) * cos(t3); e.y = sin(t1); e.z = cos(t1) * sin(t3); p.x = c.x + r * e.x; p.y= c.y+ r * e.y; p.z= c.z+ r * e.z; glnormal3f(e.x,e.y,e.z); gltexcoord2f(i/(double)n,2*j/(double)n); glvertex3f(p.x,p.y,p.z); Display lists in OpenGL void initgl() list = glgenlists(1); glnewlist(list, GL_COMPILE); generatesphere(); glendlist(); void display() glcalllist(list); teapot.cpp void initgl() glclearcolor( 0.0, 0.0, 0.0, 0.0 ); // Setting up camera intrinsic parameters. glmatrixmode( GL_PROJECTION ); gluperspective(65.0, 1.0, 1.0, 20.0 ); // Setting up camera extrinsic parameters. glmatrixmode (GL_MODELVIEW ); glulookat(1,-4,-4,0,0,0,0,1,0); glenable(gl_depth_test); glshademodel( GL_SMOOTH ); // set up texture mapping glenable(gl_texture_2d); // load the texture LoadGLTextures("textures/copper.bmp", &ourtexture); 8

// set up lighting glenable(gl_lighting); glenable(gl_light1); gllightfv(gl_light1, GL_AMBIENT, light1ambient); gllightfv(gl_light1, GL_DIFFUSE, light1diffuse); gllightfv(gl_light1, GL_SPECULAR, light1specular); gllightfv(gl_light1, GL_POSITION, light1position); // set up material properties // (in this demo we have only one object, otherwise // you should specify material properties for each object) glmaterialfv(gl_front, GL_AMBIENT, mat_ambient); glmaterialfv(gl_front, GL_DIFFUSE, mat_diffuse); glmaterialfv(gl_front, GL_SPECULAR, mat_specular); glmaterialfv(gl_front, GL_EMISSION, mat_emission); glmateriali(gl_front, GL_SHININESS, mat_shininess); // display function void draw() glclear( GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT ); glpushmatrix(); glrotatef(anglex,0,1,0); glrotatef(-angley,1,0,0); // draw teapot glutsolidteapot(1.0); glpopmatrix(); glflush(); glutswapbuffers(); // mouse callback // btn = mouse button // state = up or down // x,y = window coords void mouse(int btn, int state, int x, int y) grab_pos_x = x; grab_pos_y = y; // Mouse motion, use mouse movements to rotate the object // OBS! This is a SIMPLE test function. // In the ideal case the rotation would be view dependent. void mousemotion(int x, int y) anglex += (x-grab_pos_x)/2.0; if( anglex > 360.0 ) anglex -= 360.0; else if( anglex < -360.0 ) anglex += 360.0; angley += (y-grab_pos_y)/2.0; if( angley > 360.0 ) angley -= 360.0; else if( angley < -360.0 ) angley += 360.0; grab_pos_x = x; grab_pos_y = y; glutpostredisplay(); int main(int argc, char **argv) initglut(argc, argv); initgl(); // Set the callback functions. glutdisplayfunc( draw ); glutmousefunc(mouse); glutmotionfunc(mousemotion); //Start the GLUT main Loop. glutmainloop(); return 0; 9

Links The main page for reference, documentation, news, downloads, tutorials, etc. www.opengl.org Nice tutorials (beginner to expert) http://nehe.gamedev.net/ See links on course homepage Books The standard reference books OpenGL Programming Guide `Red Book OpenGL Rererence Manual `Blue Book Available online (HTML,PDF) See links on course homepage Course book Interactive Computer Graphics: A Top-Down Approach with OpenGL - 3rd/4th Edition http://www.cs.unm.edu/~angel/ Hierarchical modeling Hierarchical modeling 10