Programming using OpenGL: A first Introduction

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

Introduction to OpenGL Week 1

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

Programming with OpenGL Part 1: Background

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

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

Programming with OpenGL Part 1: Background

Computer Graphics. Bing-Yu Chen National Taiwan University

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

Graphics Programming

Introduction to Computer Graphics with OpenGL/GLUT

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

Drawing Primitives. OpenGL basics

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

COMPUTER GRAPHICS LAB # 3

Computer Graphics (Basic OpenGL)

Assignment 1. Simple Graphics program using OpenGL

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

Computer Graphics. OpenGL

RECITATION - 1. Ceng477 Fall

Precept 2 Aleksey Boyko February 18, 2011

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

Lecture 4 of 41. Lab 1a: OpenGL Basics

Lecture 3 Advanced Computer Graphics (CS & SE )

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

CS 4204 Computer Graphics

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

Announcements OpenGL. Computer Graphics. Spring CS4815

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

Lectures OpenGL Introduction

Computer graphics MN1

Introduction to OpenGL

Computer Graphics, Chapt 08

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

COMP 371/4 Computer Graphics Week 1

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

OpenGL Introduction Computer Graphics and Visualization

LECTURE 02 OPENGL API

CS Computer Graphics: Intro to OpenGL

CS Computer Graphics: Intro to OpenGL

Basic Graphics Programming

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

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

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

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

Programming with OpenGL Part 3: Three Dimensions

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

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

CSC 8470 Computer Graphics. What is Computer Graphics?

Basic Graphics Programming

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

OpenGL refresher. Advanced Computer Graphics 2012

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

Today s Agenda. Basic design of a graphics system. 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

Using OpenGL with CUDA

CSE4030 Introduction to Computer Graphics

Computer Graphics 1 Computer Graphics 1

CS 432 Interactive Computer Graphics

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

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

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

OpenGL/GLUT Intro. Week 1, Fri Jan 12

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

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

1.2 Basic Graphics Programming

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

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

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

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

Draw the basic Geometry Objects. Hanyang University

Lecture 3. Understanding of OPenGL programming

Computer Graphics Primitive Attributes

Computer Graphics Introduction to OpenGL

Computer graphic -- Programming with OpenGL I


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

Rendering. Part 1 An introduction to OpenGL

Introduction to OpenGL: Part 2

Lecture 2 2D transformations Introduction to OpenGL

Introduction to MS Visual C/C++

An Interactive Introduction to OpenGL Programming

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

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

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

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

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

SOURCES AND URLS BIBLIOGRAPHY AND REFERENCES

GLUT. What is OpenGL? Introduction to OpenGL and GLUT

OpenGL. Toolkits.

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

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

Graphics Pipeline & APIs

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Bob s Concise Introduction to Doxygen

to OpenGL Introduction Pipeline Graphics pipeline OpenGL pipeline OpenGL syntax Modeling Arrays Conclusion 1 Introduction Introduction to OpenGL

Graphics Pipeline & APIs

Programming of Graphics

Transcription:

Programming using OpenGL: A first Introduction CMPT 361 Introduction to Computer Graphics Torsten Möller Machiraju/Zhang/Möller 1

Today Overview GL, GLU, GLUT, and GLUI First example OpenGL functions and states complete example: callbacks, simple viewing transformation, etc. overview of OpenGL primitives and attributes Readings: Sections 2.1-2.5 Machiraju/Zhang/Möller 2 2

OpenGL is an API Application programmers interface: link between low-level: graphics hardware high-level: application program you write OpenGL ES (for embedded systems): a subset of desktop OpenGL, providing lightweight interface for 2D/3D graphics on mobile and hand-held devices, etc. Machiraju/Zhang/Möller 3 3

OpenGL is a Library for 2D and 3D graphics programming 200+ functions for building application programs Portable to many platforms (Win, Mac, Unix, Linux) Callable from many programming languages (C, Java, Perl, Python) Primarily concerned with modeling and rendering operations Specify geometric primitives (lines, pixels, polygons...) Apply geometric transformations Specify camera, light, color, texture information, etc. No windowing or (platform-specific) input/interaction functions these are Machiraju/Zhang/Möller the jobs of GLUT 4 4

OpenGL libraries OpenGL: GL (libgl) and GLU (libglu) Windows native implementations (opengl32) Mesa 3D: freeware implementation for Linux GLUT: OpenGL Utility Toolkit (libglut) Window and menu management Mouse and keyboard interactions Other nice utilities, e.g., glutsolidteapot, etc. Machiraju/Zhang/Möller 5 5

Graphics libraries GLUI: GLUT-based user interface library (libglui) Offer controls to OpenGL applications, e.g., buttons, checkboxes, radio buttons, etc. Machiraju/Zhang/Möller 6 6

First example Display 2D points drawing the Sierpinski gasket 1.Choose p0 randomly inside the triangle 2.Choose a triangle vertex randomly 3.p1 is the midpoint 4.Repeat from step 2, replacing p0 by p1 An example of a fractal: self-similar geometric structure Machiraju/Zhang/Möller 7 7

void display() { Display routine GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}}; /* A triangle */ #define GLfloat float int j, k, rand(); /* standard random number generator */ GLfloat p[2] = {7.5, 5.0}; /* arbitrary initial point inside triangle */ glclear(gl_color_buffer_bit); Clear the window to a color specified by a call to glclearcolor() glbegin(gl_points); /* compute and plot 5000 new points */ for( k=0; k<5000; k++) { j=rand()%3; /* pick a vertex at random */ /* compute point halfway between selected vertex and old point */ p[0] = (p[0]+vertices[j][0])/2.0; p[1] = (p[1]+vertices[j][1])/2.0; GL_POINTS specify what geometric primitives the vertex calls define. Other primitives: GL_LINES, GL_POLYGON, etc. /* plot new point */ Draw a 2D vertex given as a vector glvertex2fv(p); Also possible, glvertex2i(x, y), } glvertex3f(x, y, z), etc. glend(); glflush(); /* clear buffers */ Machiraju/Zhang/Möller Force points to be rendered 8 as soon } as possible 8

The Sierpinski gasket Machiraju/Zhang/Möller 9 9

OpenGL function format function name dimensions glvertex3f(x,y,z) belongs to GL library x, y, z are floats glvertex3fv(p) p is a pointer to an array Machiraju/Zhang/Möller 10 10

Unresolved issues In what colors are we drawing? Where on the screen does the 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? Where is the camera? Machiraju/Zhang/Möller 11 11

Answers In what colors are we drawing? glcolor3f(1.0, 0.0, 0.0); /* draw in red */ Where on the screen does the image appear? glutinitwindowposition(0,0); /* place window top left on display */ How large will the image be? glutinitwindowsize(500,500); /* 500 x 500 pixel window */ How do we create an area of the screen a window for our image? glutcreatewindow("sierpinski Gasket"); /* window title */ How long will the image remain on the screen? glutmainloop(); /* enter (infinite) event loop */ Machiraju/Zhang/Möller 12 12

OpenGL functions Primitive functions: e.g., points, lines or polygons Attribute functions: e.g., color, light, material Viewing functions: e.g., camera position Input functions (GLUT): e.g., mouse, keyboard Control functions: e.g., OpenGL features, start/end Query functions, to acquire OpenGL states Read text or OpenGL primer and online OpenGL and glut function references (from course web page) Machiraju/Zhang/Möller 13 13

OpenGL header files Most OpenGL function definitions are in the include files gl.h, glu.h (typically found in /usr/include) and glut.h (typically found in /usr/local/include/gl) Note that #include <GL/glut.h> should automatically include the others Include files also define OpenGL data types: GLfloat, GLdouble,. Machiraju/Zhang/Möller 14 14

View OpenGL as a state machine Alternative classification of OpenGL functions Primitive/scene generating Can cause output if primitive is visible How vertices are processed and appearance of primitive are controlled by the various OpenGL states State changing Transformation functions Attribute functions, etc. Machiraju/Zhang/Möller 15 15

OpenGL as a state machine OpenGL Input from user program Description of geometric primitives scene-changing functions Camera positions and orientations state-changing functions Light, surface material, color, etc. state-changing functions Output to display: image pixels Machiraju/Zhang/Möller 16 16

Partial list of OpenGL states Color: glcolor3f() Line width: gllinewidth() Light source parameters: gllight() Camera position and orientation: glulookat() Transformation matrix: glloadidentity(), glrotate() Etc. see OpenGL manual State variables have default values State values are applied to all functions that follow until the next change of state Machiraju/Zhang/Möller 17 17

A simple program Generate a square on a solid background Machiraju/Zhang/Möller 18 18

simple.c #include <GL/glut.h> void mydisplay(){ glclear(gl_color_buffer_bit); glbegin(gl_polygon); glvertex2f(-0.5, -0.5); glvertex2f(-0.5, 0.5); glvertex2f(0.5, 0.5); glvertex2f(0.5, -0.5); glend(); glflush(); } int main(int argc, char** argv){ glutinit(&argc, argv); glutcreatewindow( Simple ); glutdisplayfunc(mydisplay); glutmainloop(); } Machiraju/Zhang/Möller 19 19

Display callback and event loop Main program defines a display callback Every glut program must have a display callback Display callback (mydisplay) is executed whenever OpenGL decides the display must be refreshed, e.g., when the window is opened or resized or moved Main ends by entering an infinite, interactive event loop Read mouse & keyboard from GLUT/GLUI Use input to change something in the scene Redisplay the scene with OpenGL (clear window & redraw) Machiraju/Zhang/Möller 20 20

Compilation Unix/linux Compile with lglut lglu lgl loader flags May have to add L flag for X libraries Mesa implementation part of most linux distributions Check web for latest on Mesa 3D and glut Visual C++ under windows Get glut.h, glut32.lib and glut32.dll from web Create a console application Add opengl32.lib, glu32.lib, glut32.lib to project Machiraju/Zhang/Möller 21 settings (under link tab) 21

Programming structure Most OpenGL programs have a similar structure that consists of the following functions main(): Opens one or more windows with the required properties Defines the callback functions Enters event loop (last executable statement) init(): sets the state variables Viewing Attributes Callbacks : Display function Input and window functions Machiraju/Zhang/Möller 22 22

simple.c revisited In this version, we shall see the same output but we define all the relevant state values through appropriate function calls In particular, we set Colors Viewing conditions Window properties Machiraju/Zhang/Möller 23 23

Main function #include <GL/glut.h> int main(int argc, char **argv) { glutinit(&argc, argv); /* init GLUT, call before anything */ glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowsize(250, 250); glutinitwindowposition(100, 100); glutcreatewindow( Simple ); init(); /* initialize display parameters */ glutdisplayfunc(mydisplay); /* register display callback */ glutmainloop(); } Machiraju/Zhang/Möller 24 24

GLUT functions glutinit allows application to get command line arguments and initializes system glutinitdisplaymode requests properties for the window (the rendering context) RGB color Single buffering Properties logically OR-ed together glutwindowsize in pixels glutwindowposition from top-left corner of display glutcreatewindow create window with title simple glutdisplayfunc display callback Machiraju/Zhang/Möller 25 25

GLUT functions glutmainloop enter infinite event loop Events include operations such as keyboard press mouse movement mouse button press mouse button release reshape window expose window, etc. Event-based programming method Machiraju/Zhang/Möller 26 26

Callback functions What happens when some event occurs? A callback function is called Programmer can define (register) own callback functions rely on default callback functions do nothing events ignored GLUT handles definition of callbacks For display: glutdisplayfunc() For mouse movement: glutmousefunc(), glutmotionfunc() For reshaping of window: glutreshapefunc() Machiraju/Zhang/Möller 27 For keyboard press: glutkeyboardfunc() 27

init() function void init() { /* select clearing (background) color */ glclearcolor(0.0, 0.0, 0.0, 1.0); /* RGBA (alpha for transparency) */ /* A = 1: fully opaque */ glmatrixmode(gl_projection); glloadidentity(); glortho(-1, 1, -1, 1, -1, 1); /* left, right, bottom, top, znear, zfar */ /* orthographic projection for viewing */ /* for 2D, gluortho2d(x_min, x_max, y_min, y_max); */ } Machiraju/Zhang/Möller 28 28

Projection In OpenGL, the projection is carried out by a projection matrix Need to set the matrix mode first to know which transformation matrix is being modified (there are others, e.g., GL_MODELVIEW) glmatrixmode(gl_projection) Transformation functions are incremental so we start with an identity matrix and alter it with a projection matrix that gives the view volume glloadidentity(); glortho(-1.0,1.0, -1.0,1.0, -1.0,1.0); Machiraju/Zhang/Möller 29 29

View volume In glortho(left, right, bottom, top, near, far), Left,, top specify left,, top clipping planes near and far specify distances from the camera (center of projection) 1 indicates that the near clipping plane is behind the camera Two-dimensional vertex commands, e.g., glvertex2i(), place all vertices in the plane z = 0 If the application is in two dimensions, we can use the function gluortho2d(left, right, bottom, top) In two dimensions, the view or clipping volume becomes a 2D clipping window Machiraju/Zhang/Möller 30 30

mydisplay() function void mydisplay() { glcolor3f(1.0, 1.0, 1.0); /* draw primitives in white */ glbegin(gl_polygon); /* or GL_LINES or GL_POINTS... */ glvertex2f(-.5, -.5); glvertex2f(.5, -.5); glvertex2f(.5,.5); glvertex2f(-.5,.5); glend(); } glflush(); /* do not wait, draw it now */ Machiraju/Zhang/Möller 31 31

Output Machiraju/Zhang/Möller 32 32

OpenGL Primitives GL_POINTS GL_LINES GL_LINE_STRIP GL_POLYGON GL_LINE_LOOP GL_TRIANGLES GL_QUAD_STRIP GL_TRIANGLE_STRIP GL_TRIANGLE_FAN Machiraju/Zhang/Möller 33 33

Polygon issues OpenGL will only display polygons correctly if simple: edges cannot cross convex flat: all vertices are in the same plane User program can check if above true OpenGL will produce output if these conditions are violated but it may not be what is desired But note that triangles satisfy all conditions Machiraju/Zhang/Möller 34 34

Attributes Attributes are part of the OpenGL state and determine the appearance of objects Color (points, lines, polygons) Size and width (points, lines) Stipple pattern (lines, polygons) Polygon mode Display as filled: solid color or stipple pattern Light and material properties for shading Machiraju/Zhang/Möller 35 35

RGB color and indexed color Each color component is stored separately in the frame buffer Usually 8 bits per component in buffer (bit-planes) Note in glcolor3f the color values range from 0.0 (none) to 1.0 (all), whereas in glcolor3ub (unsigned bytes) the values range from 0 to 255 Indexed colors: Colors are indices into tables of RGB values Requires less memory (indices typically 8 bits ) but not as important now memory is cheap Machiraju/Zhang/Möller 36 36

Smooth color Default is smooth shading OpenGL interpolates vertex colors across visible polygons Alternative is flat shading Color of first vertex determines fill color OpenGL function: glshademodel(gl_smooth) or GL_FLAT Machiraju/Zhang/Möller 37 37