OpenGL Basics OpenGL’s primary function – Rendering

Slides:



Advertisements
Similar presentations
Coordinate System.
Advertisements

Computer Graphics CSCE 441
CS 4731 Lecture 2: Intro to 2D, 3D, OpenGL and GLUT (Part I) Emmanuel Agu.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
OpenGL (I). What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D objects.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
Computer Graphics CS 385 February 7, Fundamentals of OpenGl and Glut Today we will go through the basics of a minimal OpenGl Glut project, explaining.
Chapter 03: Graphics Primitives Course web page: Chapter #3.
Reference1. [OpenGL course slides by Rasmus Stenholt]
CS380 LAB I OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
COS 397 Computer Graphics Assoc. Prof. Svetla Boytcheva AUBG 2013 COS 397 Computer Graphics Practical Session №1 Introduction to OpenGL, GLFW and CG.
Basic OpenGL Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, September 10, 2003.
1 GLUT Callback functions Event-driven: Programs that use windows  Input/Output  Wait until an event happens and then execute some pre-defined functions.
Using OpenGL. 2 What is OpenGL? A software interface to graphics hardware It is a Graphics Rendering API (Application Programmer’s Interface) that is.
Drawing Basic Graphics Primitives Lecture 4 Wed, Sep 3, 2003.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
1 Working with Callbacks Yuanfeng Zhou Shandong University.
2 COEN Computer Graphics I Introductions n Brad Grantham lecturer lab dude n Dave Shreiner lecturer slave driver.
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
1. OpenGL/GLU/GLUT  OpenGL v4.0 (latest) is the “core” library that is platform independent  GLUT v3.7 is an auxiliary library that handles window creation,
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
Computer Graphics Bing-Yu Chen National Taiwan University.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
Computer Graphics I, Fall 2010 Working with Callbacks.
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
Pop-Up Menus Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Friday, September 26, 2003.
NoufNaief.net TA: Nouf Al-harbi.
NoufNaief.net 1 TA: Nouf Al-Harbi.
CSCE 441: Computer Graphics
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
OpenGL: Introduction #include main() { OpenWindow() ; glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0,
OpenGL Basic Drawing Jian-Liang Lin A Smidgen of OpenGL Code #include main() { InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear.
Introduction to Graphics Programming. Graphics API.
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL Based on GL (graphics library) by Silicon Graphics Inc. (SGI) Advantages: Runs on everything, including.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Working with Callbacks.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Lecture 8: Discussion of papers OpenGL programming Lecturer: Simon Winberg Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
Administrivia Back on track: canceling OpenGL lecture 2 Assignment 1
The Human Visual System vs The Pinhole camera
Working with Callbacks
Computer Graphics Lecture 33
Programming with OpenGL Part 2: Complete Programs
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
The User Interface Lecture 2 Mon, Aug 27, 2007.
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Lab 3 Geometric Drawing Lab 3 Geometric Drawing.
Advanced Menuing, Introduction to Picking
Chapter 3 arrays of vertices vertex arrays display lists drawing text
Working with Callbacks
Coordinate Systems and Transforming the Coordinates
Chapter 3 arrays of vertices vertex arrays display lists drawing text
Drawing in the plane 455.
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
Chapter 3 arrays of vertices vertex arrays display lists drawing text
Programming with OpenGL Part 2: Complete Programs
CS297 Graphics with Java and OpenGL
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
Chapter 3 arrays of vertices vertex arrays display lists drawing text
University of North Carolina at Greensboro
Presentation transcript:

OpenGL Basics OpenGL’s primary function – Rendering Rendering? – converting geometric/mathematical descriptions of objects into frame buffer values OpenGL can render: Geometric primitives Bitmaps and Images

Specifying Geometric primitives Primitives are specified using glBegin(primType); // define your primitive here … glEnd(); primType: GL_POINTS, GL_LINES, GL_TRIANGLES, GL_QUADS, …

Primitive Types

Sample Example Void DrawQuad(Glfloat color[]) { glBegin(GL_QUADS); glColor3fv(color); glVertex2f(0,0); glVertex2f(1.0, 0,0); glVertex2f(1.0, 1.0); glVertex2f(0.0, 1.0); glEnd(); }

OpenGL Command Formats glVertex2f(x, y) number of Components/ Dimensions 2 – (x,y) 3 – (x,y,z) 4 – (x,y,z,w) or (r,g,b,a) Add ‘v’ for vector form glVertex2fv(v) B – byte ub – unsigned byte s – short us – unsigned short i – int ui – unsigned int f – float d – double

Shape Example

OpenGL state OpenGL is a state machine can only be in one of a finite number of states states are sticky once a state is set it is in effect until it is set again OpenGL states are maintained in state variables States are read/write OpenGL defines default states usually to minimize rendering time

OpenGL State Variables All rendering attributes are encapsulated in the OpenGL variables Rendering styles (color, point size, etc) Shading Lighting Texture mapping

OpenGL State Variables Appearance is controlled by current state A typical manipulation of OpenGL states for each (primitive to render) { update OpenGL state (if necessary) render primitive }

Example Void DrawQuad(Glfloat color[]) { glBegin(GL_QUADS); glColor3f(1,0,0); glVertex2f(0,0); glVertex2f(1.0, 0,0); glVertex2f(1.0, 1.0); glVertex2f(0.0, 1.0); glEnd(); }

Controlling current state Setting State glColor3f(…) glPointSize(3) glLineStipple(repeat, pattern) glShadeModel(GL_SMOOTH) Enable Features glEnable(GL_LIGHTING) glEnable(GL_TEXGTURE_2D) …

OpenGL Interface between the users application and the underlying hardware

OpenGL Rendering Pipeline

Your First Program 1BlankWindow.exe Does nothing but open a 256 X 256 pixel screen window Clear the screen in the display() Set the clear color in the init()

OpenGL Primitives OpenGL coordinate system: x increases to the right y increases from bottom to top glutInitWindowSize(256, 256); open a window 256 pixels wide by 256 pixels high visible pixels 0 <= x <= 255, 0 <= y <= 255

GLUT Primitives

OpenGL state We modified several OpenGL state variables already glClearColor(0.3f, 0.0f, 0.0f, 1.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0f, 256.0f, 0.0f, 256.0f); glColor3f(1.0f, 1.0f, 1.0f);

More Simple Programs 2ThreePoints.exe 3RandomPoints Draws three white points 3RandomPoints Draws 1000 points in random sizes, locations, and colors.

Data Types Fortunately, the size of OpenGL’s data types corresponds with SUN’s, g++, and Microsoft Truly portable code does not make this assumption Glbyte, Glshort, Glint, Glfloat, GLdouble

Coordinate System Examples have opened a screen window with specified dimensions (in pixels) Changed the color of some of the pixels in the screen window So far, we have just been dealing with pixel coordinates

Coordinate System Write a program to plot the function 4FlatLine.exe for 4FlatLine.exe Is there a problem with the program? Print out the values and see if they make sense

Coordinate System The values look reasonable The problem is: We want to plot points in pixel coordinates but are evaluating the function in fractional pixel coordinates The x value used to evaluate the function is from 0 to 100, which is okay The y value of the function goes from 1 to about 0, which is about 1 pixel!

Coordinate System We need to expand the function values so they correspond reasonably to available pixels in our window plot a scaled version of y, where the scale factor is 200 5ProperCurve.exe we are plotting the function: for

Coordinate System Now let’s plot a similar function: for 6YFixed.exe

Coordinate System We have a similar problem as before, the values of x are given in a fractions of pixels but we want to plot x over an integer range of pixels scale x by 50 before plotting its value 7ProperCurve.exe

Coordinate System Now let’s plot a similar function: for What will the problem be? How is it fixed? 8ProperCurve.exe

Coordinate System There are two choices: shift x by 2, scale x by 50 scale x by 50, shift x by 100

Coordinate System We need to expand the function values so they reasonably correspond to the available pixels in our window shift x so only positive values are plotted scale x so we use 256 (or some reasonable fraction of 256) scale y so we use 256

Coordinate System -1 <= x <= 1 scale x by 1/2: -0.5 <= x1 <= 0.5 shift x1 by 0.5: 0.0 <= x2 <= 1.0 scale x2 by 175: 0.0 <= x3 <= 175 shift x3 by 25: 25 <= x4 <= 200

Coordinate System In equation form: x varies from -1.0 to 1.0 x1 = x * 0.5 x2 = x1 + 0.5 == x * 0.5 + 0.5 x3 = x2 * 175 == (x * 0.5 + 0.5) * 175 x4 = x3 + 25 == (x * 0.5 + 0.5) * 175 + 25 x = -1, x4 = 0 x = 1, x4 = 200

Coordinate System Or, flipping the last equation around: x4 = (x * 0.5 + 0.5) * 175 + 25 x4 = (((x + 1) / 2) * 175) + 25 Or, flipping the last equation around: x = (((x4 - 25) / 175) * 2) - 1 where 25 <= x4 <= 200 x4 = 25, x = -1 x4 = 200, x = 1

Coordinate System That takes care of scaling and shifting x, how about doing the same thing for y y ranges from 0 to 1, and we want to plot y for values between 25 and 200 0 <= y <= 1 scale by 175: 0 <= y1 <= 175 shift y1 by 25: 25 <= y2 <= 200 y2 = y * 175 + 25 y = (y2 - 25) / 175, for 25 <= y2 <= 200

OpenGL Primitives Lines are communicated to OpenGL: Using the enumerated type: GL_LINES as the argument to glBegin() specifying the two end points of the line as vertices between the glBegin() and glEnd() function calls

OpenGL Primitives GL_LINES GL_LINE_STRIP Treats each pair of vertices as an independent line segment. Vertices 2n – 1 and 2n define line n N/2 lines are drawn. GL_LINE_STRIP Draws a connected group of line segments from the first vertex to the last. Vertices n and n+1 define line n N – 1 lines are drawn.

OpenGL Primitives 9Lines.exe GL_LINE_LOOP Draws a connected group of line segments from the first vertex to the last, then back to the first. Vertices n and n+1 define line n. The last line, however, is defined by vertices N and 1. N lines are drawn. 9Lines.exe

OpenGL Primitives Author provides a 2D line drawing of a dinosaur self contained file the 2D points points are vertices in polylines

OpenGL Primitives file format: number of polylines number of vertices in polyline 1 vertex x1 y1 vertex x2 y2 … number of vertices in polyline 2

OpenGL Primitives Developed a class to represent a polyline file in memory PolyLineObject class contains data members number of polylines in the object an array of pointers to polylines member functions read a polyline file write a polyline file draw a PolyLineObject using OpenGL

OpenGL Primitives Developed a class to represent a single polyline in memory PolyLine class contains data members number of vertices in the polyline an array of vertices, represented as generic Points

OpenGL Primitives Generic Point class (class template) template parameters data type size or the number of dimensions data members number of dimensions array of values member functions constructors, destructor, operator=, operator[] various “shortcut” functions: X(), X(T val), ...

OpenGL Primitives number of polygons polyline pointers array number of vertices vertex array pointer polyline pointer 1 polyline pointer 2 polyline pointer 3 polyline pointer n Point 1: x1, y1 Point 2: x2, y2 Point 3: x3, y3 Point m: xm, ym

OpenGL Primitives Write and draw are very similar to each other, and inverses of read each polyline is drawn using: glBegin(GL_LINE_STRIP); animation: move some of the vertices of the polylines and redraw

KeyPress Events Used because of limited GUI features (no menubar, sliders, dials, buttons, …) in GLUT simply add a callback to handleKeyPress events big switch statement to determine which key was pressed

KeyPress Events 10LineWidth.exe Draws a line of varying width + add one to the current line width - subtract one from the current line width Esc to quit the program (traditional in GL programs) three arguments to the keyboard callback ASCII code for the key x and y location of the mouse when the key was pressed

KeyPress Events the keyboard callback needs to call the display function let GLUT do it: glutPostRedisplay marks the current window for redisplay glutMainLoop calls the display callback on the next iteration through the main loop no enumerated binding for the Ecs key, we need to switch on 27

Menu CallBacks GLUT provides cascading pop-up menus popup menus can be arranged in a hierarchy of menus, sub-menus, sub-sub-menus, … identifiers for all sub-menus must be passed as an argument to the GLUT functions that create the parent menu 11LineColorMenu.exe 12LineColorMenuAndMouse.exe

Mouse Events GLUT can be informed of mouse events that occur in your window Button identifiers GLUT_LEFT_BUTTON GLUT_MIDDLE_BUTTON GLUT_RIGHT_BUTTON Buttons have state GLUT_DOWN GLUT_UP

Mouse Events The mouse callback takes 4 arguments int button: which button caused the event int state: GLUT_UP/GLUT_DOWN int x, int y: location in the window where the event occurred The mouse callback responds to mouse events only, not motion events mouse events are good for identifying locations, start/stop drawing (paint prog)

Mouse Events 13DrawLineWithMouse1.exe 14DrawLineWithMouse2.exe Notice that height - mouse_y is used because the mouse_y is from the top of the screen rather than the bottom. glutPostRedisplay() is used to redraw the window. 14DrawLineWithMouse2.exe

Motion Event Mouse movement when any button is pressed application parses the button 15DrawLineWithMouse3.exe Generally used for panning rotating closer/further

Joystick Events GLUT has two callbacks for joystick input: glutJoystickFunc( functionName, pollInterval ); Registers a function with GLUT to call at the pollInterval  if the joystick has changed state. glutForceJoystickFunc() Forces glut to call the function registered with glutJoystickFunc immediately. This method should be placed in a timer to call the joystickFunc at regular intervals. If the joystick is held in a constant state, the joystickFunc is not invoked.

Joystick Events The joystick event handler function should have the following signature: joystickEventHandler(unsigned int buttonMask, int x, int y, int z) The joystick buttons are reported by the buttonMask parameter: GLUT_JOYSTICK_BUTTON_A (0x1), GLUT_JOYSTICK_BUTTON_B (0x2), GLUT_JOYSTICK_BUTTON_C (0x4), and GLUT_JOYSTICK_BUTTON_D (0x8)