Drawing Primitives. OpenGL basics

Similar documents
Computer Graphics. Making Pictures. Computer Graphics CSC470 1

COMPUTER GRAPHICS LAB # 3

Announcements OpenGL. Computer Graphics. Spring CS4815

Assignment 1. Simple Graphics program using OpenGL

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

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

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

Lecture 3. Understanding of OPenGL programming

Early History of APIs. PHIGS and X. SGI and GL. Programming with OpenGL Part 1: Background. Objectives

Programming with OpenGL Part 1: Background

Programming using OpenGL: A first Introduction

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

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

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

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

Graphics Programming

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

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

OpenGL Introduction Computer Graphics and Visualization

Lectures OpenGL Introduction

Programming with OpenGL Part 1: Background

Computer Graphics Anatomy of GUI. Computer Graphics CSC470 1

Duc Nguyen CSE 420 Computer Graphics 10/10/2018 Homework 1

CS 4204 Computer Graphics

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

Computer Graphics. Bing-Yu Chen National Taiwan University

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

Introduction to Computer Graphics with OpenGL/GLUT

Introduction to OpenGL Week 1

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

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

Computer Graphics. OpenGL

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

CSE4030 Introduction to Computer Graphics

Computer Graphics Course 2005

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

Computer Graphics, Chapt 08

Using OpenGL with CUDA

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

CS621 Lab 1 Name: Ihab Zbib

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

Computer Graphics (Basic OpenGL)

Basic Graphics Programming

Computer Graphics 1 Computer Graphics 1

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

6. Make use of glviewport() to display two sine curves on the same screen, one on the

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

CS Computer Graphics: Intro to OpenGL

CS Computer Graphics: Intro to OpenGL

LECTURE 02 OPENGL API

Programming with OpenGL Part 3: Three Dimensions

Precept 2 Aleksey Boyko February 18, 2011

Bob s Concise Introduction to Doxygen

OpenGL and GLUT. COMP413 Computer Graphics. School of Computer Sci. & Eng. Kyungpook National University, Korea. Spring Semester, 2016

Introduction to OpenGL: Part 2

Introduction to OpenGL

Computer Graphics (CS 543) Lecture 1 (Part 2): Introduction to OpenGL/GLUT (Part 1)

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

Lecture 4 of 41. Lab 1a: OpenGL Basics

Draw the basic Geometry Objects. Hanyang University

SOURCES AND URLS BIBLIOGRAPHY AND REFERENCES

Lecture 3 Advanced Computer Graphics (CS & SE )

OpenGL JOGL. OpenGL & JOGL. Shaoting Zhang, or Tony. September 12, 2007

OpenGL/GLUT Intro. Week 1, Fri Jan 12

CIS 441/541: Introduction to Computer Graphics Lecture 14: OpenGL Basics

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

Windows and Viewports. Windows and Viewports. Windows and Viewports. Windows and Viewports. CSC 706 Computer Graphics

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

API Background. Prof. George Wolberg Dept. of Computer Science City College of New York

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

Exercise 1 Introduction to OpenGL

3D computer graphics: geometric modeling of objects in the computer and rendering them

Project Sketchpad. Ivan Sutherland (MIT 1963) established the basic interactive paradigm that characterizes interactive computer graphics:

Computer Graphics: Line Drawing Algorithms

Computer Graphics (4731) Lecture 4: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

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

Lecture 2 2D transformations Introduction to OpenGL

RECITATION - 1. Ceng477 Fall

CS 432 Interactive Computer Graphics

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


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

GLUT. What is OpenGL? Introduction to OpenGL and GLUT

Computer Graphics. Downloaded from: LectureNotes 1 CSC-254

PART-I: Lab for MCS-051 (Advanced Internet Technologies)

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

Rendering. Part 1 An introduction to OpenGL

Input and Interaction. 1. Interaction. Chapter 3. Introduction: - We now turn to the development of interactive graphics programs.

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

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

Basic Graphics Programming

COMP 371/4 Computer Graphics Week 1

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

COS340A Assignment 1 I Pillemer Student# March 25 th 2007 p1/15

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

CSC 8470 Computer Graphics. What is Computer Graphics?

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

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

Programming of Graphics

Transcription:

CSC 706 Computer Graphics / Dr. N. Gueorguieva 1 OpenGL Libraries Drawing Primitives OpenGL basics OpenGL core library OpenGL32 on Windows GL on most unix/linux systems (libgl.a) OpenGL Utility Library (GLU) Provides functionality in OpenGL core but avoids having to rewrite code Links with window system GLX for X window systems WGL for Windows AGL for Macintosh CSC 706 Computer Graphics / Dr. N. Gueorguieva 2 GLUT OpenGL Utility Toolkit (GLUT) Provides functionality common to all window systems Open a window Get input from mouse and keyboard Menus Event-driven Code is portable but GLUT lacks the functionality of a good toolkit for a specific platform No slide bars Primitives Points Line Segments Polygons Attributes Transformations Viewing Modeling Control (GLUT) Input (GLUT) Query OpenGL Functions CSC 706 Computer Graphics / Dr. N. Gueorguieva 3 CSC 706 Computer Graphics / Dr. N. Gueorguieva 4 Lack of Object Orientation Example OpenGL is not object oriented so that there are multiple functions for a given logical function glvertex3f glvertex2i glvertex3dv Underlying storage mode is the same Easy to create overloaded functions in C++ but issue is efficiency type of object location of vertex glbegin(gl_polygon) glvertex3f(0.0, 0.0, 0.0); glvertex3f(0.0, 1.0, 0.0); glvertex3f(0.0, 0.0, 1.0); glend( ); end of object definition CSC 706 Computer Graphics / Dr. N. Gueorguieva 5 CSC 706 Computer Graphics / Dr. N. Gueorguieva 6

CSC 706 Computer Graphics / Dr. N. Gueorguieva 7 OpenGL function format OpenGL #defines function name dimensions glvertex3f(x,y,z) x,y,z are floats belongs to GL library glvertex3fv(p) p is a pointer to an array Most constants are defined in the include files gl.h, glu.h and glut.h Note #include <GL/glut.h> should automatically include the others Examples glbegin(gl_polygon) glclear(gl_color_buffer_bit) include files also define OpenGL data types: GLfloat, GLdouble,. CSC 706 Computer Graphics / Dr. N. Gueorguieva 8 Event driven programming Event driven programming Almost all window based programs follow the event driven paradigm program waits for events to occur takes appropriate action Events are typically stored in an event queue Application programs specify the types of events that are of interest The window system passes events to apps. Appropriate action is implemented by the application calling a unique, user defined function for each event of interest these functions are referred to as callback functions Associating callback functions with events is not a rendering issue, OpenGL does not provide for this GLUT (OpenGL Utility Toolkit) does CSC 706 Computer Graphics / Dr. N. Gueorguieva 9 CSC 706 Computer Graphics / Dr. N. Gueorguieva 10 Registering Events The function call: glutmousefunc(mouse) associates the callback function mouse with all mouse events button press/release When a mouse button is pressed or released in the graphics application window, the user defined function mouse is called Application ignores all mouse events without this function call Callback Functions (Re)draws screen when window opened or another window moved off it. Reports new window width and height for reshaped window. (Moving a window does not produce a reshape event.) glutidlefunc(myidle); when nothing else is going on, simply redraws display using void myidle() glutpostredisplay(); CSC 706 Computer Graphics / Dr. N. Gueorguieva 11 CSC 706 Computer Graphics / Dr. N. Gueorguieva 12

CSC 706 Computer Graphics / Dr. N. Gueorguieva 13 Callback Functions (2) Handles mouse button presses. Knows mouse location and nature of button (up or down and which button). glutmotionfunc(mymotionfunc); Handles case when the mouse is moved with one or more mouse buttons pressed. Callback Functions (3) glutpassivemotionfunc(mypassivemotionfunc) Handles case where mouse enters the window with no buttons pressed. glutkeyboardfunc(mykeyboardfunc); Handles key presses and releases. Knows which key was pressed and mouse location. glutmainloop() Runs forever waiting for an event. When one occurs, it is handled by the appropriate callback function. CSC 706 Computer Graphics / Dr. N. Gueorguieva 14 Event driven programming main() Opening a window for drawing main() glutinit(&argc, argv); glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowsize(640, 480); glutinitwindowposition(100, 150); glutcreatewindow("my first attempt"); Establishing coordinate system void myinit(void) glclearcolor(0.0,1.0,1.0,0.0); glcolor3f(1.0,0.0f,1.0f); glpointsize(4.0); glmatrixmode(gl_projec TION); glloadidentity(); gluortho2d(0.0,440.0,0.0,4 80.0); void myinit(void) glclearcolor(0.0,1.0,1.0,0.0); glcolor3f(1.0,0.0f,1.0f); glpointsize(9.0); glmatrixmode(gl_projection); glloadidentity(); gluortho2d(0.0,440.0,0.0,480.0); void mydisplay(void) glclear(gl_color_buffer_bit); glbegin(gl_points); glvertex2i(100,50); glvertex2i(100,130); glvertex2i(150, 130); glvertex2i(230, 100); glend(); glflush(); void main(int argc, char **argv) glutinit(&argc, argv); glutinitdisplaymode(glut_sing LE GLUT_RGB); glutinitwindowsize(640, 480); glutinitwindowposition(100, 150); glutcreatewindow("my first attempt"); // // // myinit(); glutmainloop(); CSC 706 Computer Graphics / Dr. N. Gueorguieva 15 CSC 706 Computer Graphics / Dr. N. Gueorguieva 16 A GL Program to Open a Window void main(int argc, char** argv) glutinit(&argc, argv); // initialize the Open-GL toolkit glutinitdisplaymode(glut_single GLUT_RGB); // set the display mode: single display buffer and colors glutinitwindowsize(640,480); // set window size glutinitwindowposition(100, 150); // set window upper left corner position on screen glutcreatewindow("my first attempt"); // open the screen window (Title: my first attempt) Window Program cont d // register the callback functions myinit(); // additional initializations as necessary glutmainloop(); // go into a perpetual loop CSC 706 Computer Graphics / Dr. N. Gueorguieva 17 CSC 706 Computer Graphics / Dr. N. Gueorguieva 18

CSC 706 Computer Graphics / Dr. N. Gueorguieva 19 Effect of Program First program - putting it together Vertices - instead of points we work with vertex and create objects from vertices. glbegin(gl_points); glvertex2i(100,50); glvertex2i(100,130); glvertex2i(150, 130); glvertex2i(230, 100); glend(); CSC 706 Computer Graphics / Dr. N. Gueorguieva 20 void mykeyboard(unsigned char key, int mousex, int mousey) Runs whenever a keyboard event occurs (e.g. user presses a key) void mymouse(int button, int state, int x, int y) Runs whenever a mouse event occurs (e.g. user presses a mouse button, mouse moves) void mydisplay(void) Runs whenever the system determines that the window must be redrawn (e.g. window comes to the front, window has been moved) void myreshape(void) Runs whenever a window changes size void mykeyboard(unsigned char key, int mousex, int mousey) if (key == f key == F ) glutfullscreen(); else if(key == w key == W ) glutreshapewindow(640,480); CSC 706 Computer Graphics / Dr. N. Gueorguieva 21 CSC 706 Computer Graphics / Dr. N. Gueorguieva 22 int main() //initialise things //create a screen window //initialise other things glutmainloop(); Opening a Window int main(int argc, char **argv) glutinit(&argc, argv); glutinitdisplaymode(glut_single GLUT RGB); glutinitwindowsize(640,480); glutinitwindowposition(100,100); glutcreatewindow( My OpenGL Window ); myinit(); glutmainloop(); CSC 706 Computer Graphics / Dr. N. Gueorguieva 23 CSC 706 Computer Graphics / Dr. N. Gueorguieva 24

CSC 706 Computer Graphics / Dr. N. Gueorguieva 25 Drawing Dots in OpenGL Display for Dots glbegin (GL_POINTS); glvertex2i (100, 50); glvertex2i (100, 130); glvertex2i (150, 130); glend(); GL_POINTS is constant built-into Open-GL (also GL_LINES, GL_POLYGON, ) CSC 706 Computer Graphics / Dr. N. Gueorguieva 26 Setting Drawing Colors in GL glcolor3f(red, green, blue); // set drawing color glcolor3f(1.0, 0.0, 0.0); // red glcolor3f(0.0, 1.0, 0.0); // green glcolor3f(0.0, 0.0, 1.0); // blue glcolor3f(0.0, 0.0, 0.0); // black glcolor3f(1.0, 1.0, 1.0); // bright white glcolor3f(1.0, 1.0, 0.0); // bright yellow glcolor3f(1.0, 0.0, 1.0); // magenta Setting Background Color in GL glclearcolor (red, green, blue, alpha); Sets background color. Alpha is degree of transparency; use 0.0 for now. glclear(gl_color_buffer_bit); clears window to background color Setting the Background Colour glclearcolour(r, G, B, α); glclearcolour(1,0,0,0); glclearcolour(0,1,0,0); glclearcolour(0,0,1,0); Setting Up a Coordinate System void myinit(void) glmatrixmode(gl_projection); glloadidentity(); gluortho2d(0, 640.0, 0, 480.0); // sets up coordinate system for window from (0,0) to (679, 479)

Drawing Lines glbegin (GL_LINES); //draws one line glvertex2i (40, 100); // between 2 vertices glvertex2i (202, 96); glend (); glflush(); If more than two vertices are specified between glbegin(gl_lines) and glend() they are taken in pairs, and a separate line is drawn between each pair. Line Attributes Color, thickness, stippling. glcolor3f() sets color. gllinewidth(4.0) sets thickness. The default thickness is 1.0. a). thin lines b). thick lines c). stippled lines Setting Line Parameters Polylines and Polygons: lists of vertices. Polygons are closed (right); polylines need not be closed (left). Rectangles A special type of polygon with four sides and with aligned with the coordinate axis is called an aligned rectangle. Aligned Rectangles glrecti(glint x1, GLint y1, GLint x2, GLint y2); (x1,y1) (x2,y2) Drawing rectangles void mydisplay(void) glclear(gl_color_buffer_bit); glclearcolor(1.0,1.0,1.0,0.0); // white background glcolor3f(0.6,0.6,0.6); // bright gray glrecti(20,20,100,70); glcolor3f(0.2,0.2,0.2); // dark gray glrecti(70, 50, 150, 130); glflush(); by the opposite corners by the center point, height and width by the upper left corner, width and aspect ratio

Aligned Rectangles Aligned Rectangles glrecti(glint x1, GLint y1, GLint x2, GLint y2); The rectangle will be drawn, filled with the current foreground colour. Aspect Ratio The shape of a rectangle can be described as it s aspect ratio. The aspect ratio is calculated thus: AR = Width/Height Landscape 29.5/21.2 Portrait 21.2/29.5 Golden Rectangle ɸ = 1.61803399