Computer Graphics. OpenGL

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

Introduction to Computer Graphics with OpenGL/GLUT

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

COMPUTER GRAPHICS LAB # 3

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

Assignment 1. Simple Graphics program using OpenGL

RECITATION - 1. Ceng477 Fall

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

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

Programming using OpenGL: A first Introduction

Lecture 4 of 41. Lab 1a: OpenGL Basics

CS 4204 Computer Graphics

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Abel J. P. Gomes LAB. 1. INTRODUCTION TO OpenGL

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

CSE4030 Introduction to Computer Graphics

Computer Graphics. Bing-Yu Chen National Taiwan University

Drawing Primitives. OpenGL basics

Computer Graphics 1 Computer Graphics 1

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

Computer Graphics, Chapt 08

Computer graphics MN1

Precept 2 Aleksey Boyko February 18, 2011

COMP 371/4 Computer Graphics Week 1

Programming with OpenGL Part 1: Background

Rendering. Part 1 An introduction to OpenGL

Computer Graphics Introduction to OpenGL

Basic Graphics Programming

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

Lecture 2 2D transformations Introduction to OpenGL

Announcements OpenGL. Computer Graphics. Spring CS4815

Lecture 3 Advanced Computer Graphics (CS & SE )

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

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

Computer Graphics Primitive Attributes

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

Graphics Programming

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

Introduction to OpenGL Week 1

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

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

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

Lectures OpenGL Introduction

Computer Graphics Course 2005

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

CS 543 Lecture 1 (Part 3) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

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

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

OpenGL refresher. Advanced Computer Graphics 2012

Computer Graphics (CS 4731) OpenGL/GLUT(Part 1)

Overview of Graphics Systems Hearn & Baker Chapter 2. Some slides are taken from Robert Thomsons notes.

Programming of Graphics

CS 548: COMPUTER GRAPHICS GLUT INPUT FUNCTIONS SPRING 2015 DR. MICHAEL J. REALE

Draw the basic Geometry Objects. Hanyang University

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

CSC 8470 Computer Graphics. What is Computer Graphics?

Basic Graphics Programming

Lecture 11: Callbacks. CITS 3003 Graphics & Animation

Introduction to OpenGL

OpenGL/GLUT Intro. Week 1, Fri Jan 12

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

Computer Graphics (CS 543) Lecture 2b: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

Graphics Hardware and OpenGL

Exercise 1 Introduction to OpenGL

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

Computer Graphics (Basic OpenGL)

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

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

This library uses only GL functions but contains code for creating common objects and simplifying viewing.

Computer graphic -- Programming with OpenGL I

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

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

CS621 Lab 1 Name: Ihab Zbib

Lecture 3. Understanding of OPenGL programming

Announcement. Homework 1 has been posted in dropbox and course website. Due: 1:15 pm, Monday, September 12

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

C OMPUTER G RAPHICS Thursday

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

Computer Graphics Introduction to OpenGL

An Interactive Introduction to OpenGL Programming

CMSC 425: Lecture 4 More about OpenGL and GLUT Tuesday, Feb 5, 2013

Programming with OpenGL Part 3: Three Dimensions

Computer Graphics. Chapter 3 Computer Graphics Software

Introduction to MS Visual C/C++

Introduction to OpenGL: Part 2

Cameras (and eye) Ideal Pinhole. Real Pinhole. Real + lens. Depth of field

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

LECTURE 02 OPENGL API

Comp 410/510 Computer Graphics Spring Input & Interaction

VTU EDUSAT PROGRAMME. Computer Graphics & Visualization


Graphics Programming. 1. The Sierpinski Gasket. Chapter 2. Introduction:

Computer Graphics Introduction to OpenGL

Input and Interaction

Working with Callbacks

CS Computer Graphics: Intro to OpenGL

CS Computer Graphics: Intro to OpenGL

Introduction to OpenGL

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

Transcription:

Computer Graphics OpenGL

What is OpenGL? OpenGL (Open Graphics Library) is a library for computer graphics It consists of several procedures and functions that allow a programmer to specify the objects and operations involved in producing high-quality graphical images, specifically colour images of three-dimensional objects.

OpenGL Features Used to draw graphics primitives in 2D & 3D Points Lines Polygons Also used in Lighting. Shading. Texture mapping. Animation. Other special effects.

Installation Install Microsoft Visual C++ You need to get glut.h glut32.dll glut32.lib Copy (glut.h) file into C:\Program Files\Microsoft Visual Studio\VC98\include\gl Put the DLL file (glut32.dll) in C:\WINDOWS\SYSTEM Put the library file (glut32.lib) into C:\Program Files\Microsoft Visual Studio\VC98\lib

How it works Start MS Visual Studio Select FILE NEW to create a new project Select Win32 Console Application, choose a project name, directory location, make sure create new workspace is checked then click OK. When asked what type of application to build select An Empty Project, then click Finish.

How it works Some settings should be changed so the project is linked to the OpenGL library files (Done only once in your PC) Select PROJECT SETTINGS from the main menu bar Change the Settings For option to All Configurations. Select the LINK tab Make sure category is General In the Object/library modules textbox add: glut32.lib as the first entry with a space separating it from the next entry. Then click OK Select FILE NEW from the main menu bar Select C++ Source File

Sample code What is the output? #include <GL/glut.h> // (or others, depending on the system in use) void init (void) { glclearcolor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white. glmatrixmode (GL_PROJECTION); // Set projection parameters. gluortho2d (0.0, 200.0, 0.0, 150.0); } void linesegment (void) { glclear (GL_COLOR_BUFFER_BIT); // Clear display window. glcolor3f (0.0, 0.0, 1.0); // Set line segment color to blue. glbegin (GL_LINES); glvertex2i (180, 15); // Specify line-segment geometry. glvertex2i (10, 145); glend ( ); glflush ( ); // Process all OpenGL routines as quickly as possible. } void main (int argc, char** argv) { glutinit (&argc, argv); // Initialize GLUT. glutinitdisplaymode (GLUT_SINGLE GLUT_RGB); // Set display mode. glutinitwindowposition (50, 100); // Set top-left display-window position. glutinitwindowsize (400, 300); // Set display-window width and height. glutcreatewindow ("An Example OpenGL Program"); // Create display window. init ( ); // Execute initialization procedure. glutdisplayfunc (linesegment); // Send graphics to display window. glutmainloop ( ); // Display everything and wait. }

Syntax Header Files: Describes all function calls # include <GL/gl.h> (the core Library) # include <GL/glu.h> (the utility Library) # include <GL/glut.h> ( win. Toolkit Lib) Libraries You can call other libraries. Data Types variables for your program. float GLfloat int GLint

Syntax Function names glxxxyy() XXX - root command YY arguments type: a number and a data type For example: glcolor3f - used to specify a color with 3 float values glvertex2d - used to specify a vertex with 2 double values

glvertex To send vertices to the GL, call glvertex() glvertex3f( float x, float y, float z ); The number will represent how many dimensions you are drawing in, so you will use 2 or 3 The type of data you send is simply the format. You can send floats, (f), doubles (d), ints (i), shorts (s)

General Program structure void display(){ } void main(){ }

main function structure void main( ) { glutinitwindowsize(int width, int height); glutinitwindowposition(int x, int y); glutcreatewindow("opengl First Example"); glutdisplayfunc(display); glutmainloop(); }

glutinitwindowsize (int width, int height) width, height - the size of the window in pixels glutinitwindowposition (int x, int y) defines the position of the window on the screen X,Y - the top left corner. (0, 0) is the upper left corner of the display.

glutcreatewindow( Window Name ) Creates a window with the specified title. Note: the window is NOT displayed until glutmainloop() is called. Example: glutcreatewindow( Sphere");

Callback function glutdisplayfunc(displayfunc_name) The argument is a pointer to a callback function that takes no argument. Whenever the window needs refreshing this displayfunc is called. Therefore, you should put all the routines you need to redraw the scene in this function. Example: glutdisplayfunc(display);

glutmainloop( ) is the starting point of GLUT, windows are displayed and the event processing is started. glutdisplayfunc and glutmainloop are used to display a scene.

#include <GL/glut.h> void draw(void) { glclear(gl_color_buffer_bit); glbegin(gl_triangles); glcolor3f(1, 0, 0); glvertex2i(25, 25); glvertex2i(375, 25); glvertex2i(200, 375); glend(); glflush(); } void main(){ glutinitwindowsize(400,400); glutinitdisplaymode(glut_rgb); glutcreatewindow("opengl Set Up Test"); glutdisplayfunc(draw); glclearcolor(0.0, 0.0, 0.0, 0.0); gluortho2d(0.0, 400.0, 0.0, 400.0); glutmainloop(); } Sample Program

Graphics primitives

Points and Lines GL_POINTS GL_LINES Line segments GL_LINE_STRIP A series of one or more connected line segments the first vertex specifies the first segment s start point while the second vertex specifies the first segment s endpoint and the second segment s start point. GL_LINE_LOOP Same as GL_LINE_STRIP, in addition to connecting the last vertex with the first one.

What is the output? glbegin( GL_POINTS ); glvertex2i(25, 25); glvertex2i(375, 25); glvertex2i(200, 375); glvertex2i(375, 375); glend(); glbegin( GL_LINES ); glvertex2i(25, 25); glvertex2i(375, 25); glvertex2i(200, 375); glvertex2i(375, 375); glend(); glbegin( GL_LINE_STRIP ); glvertex2i(25, 25); glvertex2i(375, 25); glvertex2i(200, 375); glvertex2i(375, 375); glend(); glbegin( GL_LINE_LOOP ); glvertex2i(25, 25); glvertex2i(375, 25); glvertex2i(200, 375); glvertex2i(375, 375); glend();

Graphics primitives Cont. GL_POLYGON GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP

GL_POLYGON To display a single polygon fill area At least 3 vertices Example: glbegin(gl_polygon); glvertex2i(25, 200); glvertex2i(125, 25); glvertex2i(275, 25); glvertex2i(375, 200); glvertex2i(275, 375); glvertex2i(125, 375); glend(); p6 p5 (125, 375) (275, 375) p1 p4 (25, 200) (375, 200) p2 p3 (125, 25) (275, 25)

GL_TRIANGLES To display Triangles Example: glbegin(gl_triangles); glvertex2i(25, 200); glvertex2i(125, 25); glvertex2i(275, 25); glvertex2i(375, 200); glvertex2i(275, 375); glvertex2i(125, 375); glend(); Output?

GL_TRIANGLE_STRIP A triangle strip is a series of triangles connected along shared edges. In this case, the first three vertices define the first triangle. Each subsequent vertex defines a new triangle using that point along with two vertices from the previous triangle. What is the necessary code to output the following shape using GL_TRIANGLE_STRIP? How many triangles do you need? (125, 375) p6 p5 (275, 375) p1 (25, 200) p4 (375, 200) p2 p3 (125, 25) (275, 25)

GL_TRIANGLE_FAN A triangle fan is the same as a triangle strip with one exception: each vertex after the first triangle always replaces the 2 nd vertex of the previous triangle. All triangles share a common vertex. What is the necessary code to output the following shape using GL_TRIANGLE_FAN? (125, 375) p6 p5 (275, 375) p1 (25, 200) p4 (375, 200) p2 p3 (125, 25) (275, 25)

GL_QUADS To display four-sided polygons At least four vertices Example glbegin(gl_quads); glvertex2i(25, 200); glvertex2i(125, 25); glvertex2i(275, 25); glvertex2i(375, 200); glvertex2i(275, 375); glvertex2i(125, 375); glend(); GL_QUAD_STRIP To display set of connected quads For n vertices (n>=4), we obtain n/2 1 quads

2D Viewing Viewport glviewport(xvmin, yvmin, vpwidth, vpheight); Clipping gluortho2d(xwmin, xwmax, ywmin, ywmax); Or glortho

Attributes: color, width and size Color: glcolor3f(r, G, B); Point size: glpointsize(size); Line width: gllinewidth(width);

Geometric Transformations For 2D transformations, we set t z =0, (v x, v y, v z )=(0,0,1), and s z =1 Suffix: f (float), d (double) Translation gltranslate(t x, t y, t z ); specifies a translation vector Example: gltranslatef(10.0, 20.0, 0.0); Rotation glrotate(theta, v x, v y, v z ); specifies a rotation angle about a rotation axis vector (v x, v y, v z ), with the axis passing through the origin. Example glrotatef(90.0, 0.0, 0.0, 1.0);

Geometric Transformations Scaling glscale(s x, s y, s z ); specifies multiplicative factors relative to the origin. Example glscalef(2.0, 1.0, 1.0); Reflection operation can be generated by setting scaling factors to negative Example glscalef(-1.0, 1.0, 1.0);

GLUT menus OpenGL supports menus so that the user can select multiple options. To use a menu in your program, you have to do: Define the entries in the menu. Link the menu to a particular mouse button. Define the callback function corresponding to each menu.

Menus To create a pop-up menu glutcreatemenu(menufcn); Where menufcn is the name of the procedure to be invoked when a menu entry is selected, This procedure has one argument, which is the integer value corresponding to the position of a selected menu void menufcn (GLint menuitemnumber);

Menus To add a menu entry, use glutaddmenuentry(charstring, menuitemnumber); charstring is the text to be displayed in the menu entry, and menuitemnumber gives the location of that entry, the following statements create a menu with two options: glutcreatemenu(menufcn); glutaddmenuentry( New, 1); glutaddmenuentry( Open, 2); We must specify a mouse button to be used to select a menu option: glutattachmenu(button);

Submenus submenuid = glutcreatemenu(submenufcn); glutaddmenuentry( First submenu item, 1);... glutcreatemenu(menufcn); glutaddmenuentry( First menu item, 1);... glutaddsubmenu( Submenu option, submenuid);

Example Let us define the following pop-up menu:

Example Define Right Button Menu glutcreatemenu(right_menu); glutaddmenuentry( Quit, 1); glutaddmenuentry( Clear, 2); glutattachmenu(glut_right_button); Define Color Menu C_menu = glutcreatemenu(color_menu); glutaddmenuentry( RED, 1); glutaddmenuentry( BLUE, 2); glutaddmenuentry( BLACK, 3); glutaddmenuentry( GREEN, 4); glutaddmenuentry( WHITE, 5);

Example Define Pixel Size Menu P_menu = glutcreatemenu(pixel_menu); glutaddmenuentry( Increase Size, 1); glutaddmenuentry( Decrease Size, 2); Define Left Menu glutcreatemenu(left_menu); glutaddsubmenu( COLOR, C_menu); glutaddsubmenu ( PIXEL SIZE, P_menu); glutattachmenu(glut_left_button);

Example Define all functions to a menu response: void Color_menu (GLint id) { if (id==1) glcolor3f(1.0,0.0,0.0); if (id==2) glcolor3f(0.0,0.0,1.0); if (id==3) glcolor3f(0.0,0.0,0.0); if (id==4) glcolor3f(0.0,1.0,0.0); if (id==5) glcolor3f(1.0,1.0,1.0); }

OpenGL Interactive Input-Device Functions GLUT Mouse Functions When a mouse button is pressed or released. glutmousefunc(mousefcn); void mousefcn(glint button, GLint action, GLint xmouse, GLint ymouse) button: GLUT_LEFT_BUTTON GLUT_MIDDLE_BUTTON GLUT_RIGHT_BUTTON action: GLUT_DOWN GLUT_UP //press //release (xmouse, ymouse): The coordinate position, This location is relative to the top-left corner of the display window

Mouse Functions When a mouse is moved within the display window with one or more buttons activated. glutmotionfunc(mousefcn); void mousefcn(glint xmouse,glint ymouse) When a mouse is moved within the display window without pressing a button. glutpassivemotionfunc(mousefcn);

GLUT Keyboard Functions When a key is pressed glutkeyboardfunc(keyfcn); void keyfcn(glubyte key, Glint xmouse, Glint ymouse) key is assigned a character value or the corresponding ASCII code.

Keyboard Functions For function keys and other special-purpose keys. glutspecialfunc(keyfcn); void keyfcn(glint specialkey, Glint xmouse, Glint ymouse) specialkey: GLUT_KEY_F1... GLUT_KEY_F12 GLUT_KEY_UP... GLUT_KEY_RIGHT GLUT_KEY_PAGE_DOWN GLUT_KEY_HOME Backspace, delete, and escape keys can be designated with glutkeyboardfunc using their ASCII code 8, 127 and 27 respectively.

OpenGL Codes Find the output of the attached C++ programs