Computer graphics MN1

Similar documents
Computer graphics MN1

OpenGL. Toolkits.

Computer graphics MN1

Precept 2 Aleksey Boyko February 18, 2011

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

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

Lecture 4 of 41. Lab 1a: OpenGL Basics

Programming using OpenGL: A first Introduction

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

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

Introduction to Computer Graphics with OpenGL/GLUT

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

Introduction to OpenGL

Introduction to OpenGL

Basic Graphics Programming

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

Computer Graphics. OpenGL

Order of Transformations

Graphics Pipeline & APIs

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

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

Graphics and Computation Introduction to OpenGL

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

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

Graphics Pipeline & APIs

Graphics Programming

RECITATION - 1. Ceng477 Fall

C OMPUTER G RAPHICS Thursday

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


Computer Graphics 1 Computer Graphics 1

Computer Graphics. Bing-Yu Chen National Taiwan University

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

Basic Graphics Programming

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Computer Graphics Introduction to OpenGL

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

Programming with OpenGL Part 1: Background

Computer Graphics Primitive Attributes

Interaction Computer Graphics I Lecture 3

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

CS 4204 Computer Graphics

Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

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

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Introduction to OpenGL Week 1

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

Lecture 2 2D transformations Introduction to OpenGL

2 Transformations and Homogeneous Coordinates

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

OpenGL refresher. Advanced Computer Graphics 2012

Computer Graphics, Chapt 08

An Interactive Introduction to OpenGL Programming

Lectures OpenGL Introduction

COMP 371/4 Computer Graphics Week 1

Lecture 3. Understanding of OPenGL programming

Lecture 3 Advanced Computer Graphics (CS & SE )

Exercise 1 Introduction to OpenGL

Interaction. CSCI 480 Computer Graphics Lecture 3

COMPUTER GRAPHICS LAB # 3

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

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

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

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

CS418 OpenGL & GLUT Programming Tutorial (I) Presented by : Wei-Wen Feng 1/30/2008

Computer Graphics. Chapter 10 Three-Dimensional Viewing

Programming with OpenGL Part 3: Three Dimensions

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

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

Transformation Pipeline

CS Computer Graphics: Intro to OpenGL

CS Computer Graphics: Intro to OpenGL

Transformation, Input and Interaction. Hanyang University

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

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

SOURCES AND URLS BIBLIOGRAPHY AND REFERENCES

Interaction. CSCI 420 Computer Graphics Lecture 3

Computer Graphics Programming

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

1 (Practice 1) Introduction to OpenGL

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

Source code: #include <iostream>

CSC 470 Computer Graphics

Chapter 13 Selection and Feedback

Computer Graphics Introduction to OpenGL

Introduction to OpenGL

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

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

Introduction to OpenGL: Part 2

Computer Graphics Course 2005

2. OpenGL -I. 2.1 What is OpenGL? Things OpenGL can do: -23-

Drawing Primitives. OpenGL basics

Image Processing. Geometry Processing. Reading: (Not really covered in our text. See Sects 18.1, 18.2.) Overview: Display

CSE4030 Introduction to Computer Graphics

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

Computer Graphics (Basic OpenGL)

CS 543 Lecture 1 (Part II): Intro to OpenGL and GLUT (Part I) Emmanuel Agu

Transcription:

Computer graphics MN1 http://www.opengl.org Todays lecture What is OpenGL? How do I use it? Rendering pipeline Points, vertices, lines,, polygons Matrices and transformations Lighting and shading Code examples Open OpenGL Open Graphics raphics Libraryibrary Graphics API similar to DirectX,, Java3D OpenGL 2.0 released September 2004 Delivered with UNIX, Win9x/2000/Me/Nt/Xp 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 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. ( OpenGL is the assembler of computer graphics ) Application program Pipeline Transform Project Clip Pixel operations Frame buffer 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 Primitives in OpenGL Some primitive types GL_POINTS GL_LINES GL_LINESTRIP GL_TRIANGLES GL_QUADS GL_POLYGON 2

Example 1 glbegin(gl_line_loop); glvertex2f(-0.5,-0.5); // 1 glvertex2f( 0.5,-0.5); // 2 glvertex2f( 0.5, 0.5); // 3 glvertex2f(-0.5, 0.5); // 4 4 1 3 2 Example 2 Example 3 4 5 glbegin(gl_line_strip); glvertex2f(-0.5,-0.5); // 1 glvertex2f( 0.5,-0.5); // 2 glvertex2f( 0.5, 0.5); // 3 glvertex2f(-0.5, 0.5); // 4 4 1 3 glbegin(gl_triangles); glvertex3f(-0.5,-0.5,0.0); // 1 glvertex3f(0.5,-0.5,0.0); // 2 glvertex3f(0.25, 0.5,0.0); // 3 glvertex3f(-0.5, 1.25, 0.0); // 4 glvertex3f(0.5, 1.25, 0.0); // 5 2 glvertex3f(0.25, 0.75, 0.0); // 6 6 3 1 2 3

Example 3 Color glbegin(gl_triangle_strip); glvertex3f(-0.5,-0.5,0.0); // 1 glvertex3f(0.5,-0.5,0.0); // 2 glvertex3f(0.25, 0.5,0.0); // 3 glvertex3f(0.5, 0.75, 0.0); // 4 3 4 glcolor3f(1.0,0.0,0.0); // red glbegin(gl_triangles); 1 2 glbegin(gl_triangles); glcolor3f(); glcolor3f(); Color per vertex Normals glbegin(gl_triangles); glcolor3f(); glnormal3f(); glcolor3f(); glnormal3f(); Transformations OpenGL provides matrix stacks Two stacks: GL_MODELVIEW GL_PROJECTION 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); 4

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); Projections Orthographic projection glortho(left, right, bottom, top, near, far); Projections Perspective projection glfrustum(xmin, xmax, ymin, ymax, near, far); gluperspective(fovy,aspect,near,far); 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 OpenGL should call when the corresponding event occurs GLUT provides an easy-to-use interface 5

Callback functions glutmousefunc(function); // click glutmotionfunc(function); // drag glutreshapefunc(function); // window resize glutkeyboardfunc(function); // keyboard glutidlefunc(function); // animation glutdisplayfunc(function); // draw primitives A very simple program (program1.cpp) #ifdef WIN32 #include <GL/glaux.h> #endif #include <GL/gl.h> #include <GL/glut.h> using namespace std; // Title. const char* TITLE = "Program 1"; void initglut( int &argc, char **argv) glutinit( &argc,argv ); glutinitdisplaymode( GLUT_SINGLE GLUT_RGB ); glutinitwindowsize( 700, 700 ); glutinitwindowposition( 100, 100 ); glutcreatewindow( TITLE ); 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 ); 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; 6

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 ); glflush(); program6.cpp void initgl() glclearcolor( 0.0, 0.0, 0.0, 0.0 ); 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); glenable(gl_depth_test); glshademodel( GL_SMOOTH ); glenable(gl_texture_2d); // Load the textures LoadGLTextures("textures/metal3.bmp", &ourtexture); cubefaces[0] = ourtexture; cubefaces[1] = ourtexture; cubefaces[2] = ourtexture; cubefaces[3] = ourtexture; cubefaces[4] = ourtexture; cubefaces[5] = ourtexture; // 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); 7

void polygon(int a, int b, int c, int d, GLuint texture) glbindtexture(gl_texture_2d, texture); glbegin(gl_polygon); gltexcoord2f(0.0, 0.0); glnormal3fv(normals[a]); glvertex3fv(vertices[a]); void texturecube(gluint *textures) // No error checking of the array... // Draw the different sides. polygon(0,3,2,1, textures[0]); polygon(2,3,7,6, textures[1]); polygon(0,4,7,3, textures[2]); polygon(1,2,6,5, textures[3]); polygon(4,5,6,7, textures[4]); polygon(0,1,5,4, textures[5]); void draw() glclear( GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT ); glcolor3f(1.0, 1.0, 1.0); glrotatef(anglex,0,1,0); glrotatef(-angley,1,0,0); texturecube(cubefaces); glflush(); glutswapbuffers(); void mouse(int btn, int state, int x, int y) grab_pos_x = x; grab_pos_y = y; 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; 8

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