An Overview GLUT GLSL GLEW

Similar documents
GLUT. What is OpenGL? Introduction to OpenGL and GLUT

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

Introduction to Computer Graphics with OpenGL/GLUT

RECITATION - 1. Ceng477 Fall

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Computer Graphics. Bing-Yu Chen National Taiwan University

OpenGL refresher. Advanced Computer Graphics 2012

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

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

CS Computer Graphics: Intro to OpenGL

CS Computer Graphics: Intro to OpenGL

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

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

Exercise 1 Introduction to OpenGL

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

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

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

OPEN GL BACKGROUND. Objectives. Early History of APIs. SGI and GL. PHIGS and X. Modified from Angel 6e book slides

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

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

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

Programming with OpenGL Part 1: Background

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

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

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

CS450/550. Pipeline Architecture. Adapted From: Angel and Shreiner: Interactive Computer Graphics6E Addison-Wesley 2012

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

CS 432 Interactive Computer Graphics

Lecture 4 of 41. Lab 1a: OpenGL Basics

LECTURE 02 OPENGL API

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

Programming using OpenGL: A first Introduction

Computer Graphics 1 Computer Graphics 1

COMP 371/4 Computer Graphics Week 1

Basic Graphics Programming

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

Introduction to OpenGL Week 1

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

Drawing Primitives. OpenGL basics

OpenGL/GLUT Intro. Week 1, Fri Jan 12

COMP371 COMPUTER GRAPHICS

Introduction to Computer Graphics. Hardware Acceleration Review

Introduction to Shaders.

Introduction to OpenGL/GLSL and WebGL GLSL

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

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

Announcements OpenGL. Computer Graphics. Spring CS4815

Computer Graphics (Basic OpenGL)

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

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

CS 4204 Computer Graphics

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

INTRODUCTION TO OPENGL PIPELINE

Graphics Programming

Computer graphics MN1

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Programming shaders & GPUs Christian Miller CS Fall 2011

Programmable shader. Hanyang University

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Basic Graphics Programming

Supplement to Lecture 22

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

Computer Graphics, Chapt 08

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

12.2 Programmable Graphics Hardware

Computer Graphics. OpenGL

Introduction to the OpenGL Shading Language

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

Programmable Graphics Hardware

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

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

gvirtualxray Tutorial 01: Creating a Window and an OpenGL Context Using GLUT

Programmable Graphics Hardware

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

Programming with OpenGL Part 1: Background

Programming Graphics Hardware

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

Lecture 2 2D transformations Introduction to OpenGL

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

Graphics Hardware and OpenGL

CS 548: COMPUTER GRAPHICS INTRODUCTION TO OPENGL AND GLUT SPRING 2015 DR. MICHAEL J. REALE

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Lecture 3. Understanding of OPenGL programming

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

Lecture 13: OpenGL Shading Language (GLSL)

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

Precept 2 Aleksey Boyko February 18, 2011

Introduction to OpenGL

CS 450: COMPUTER GRAPHICS REVIEW: INTRODUCTION TO COMPUTER GRAPHICS SPRING 2016 DR. MICHAEL J. REALE

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

Lectures OpenGL Introduction

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

OpenGL Introduction Computer Graphics and Visualization

Real - Time Rendering. Graphics pipeline. Michal Červeňanský Juraj Starinský

Intro to OpenGL III. Don Fussell Computer Science Department The University of Texas at Austin

1.2 Basic Graphics Programming

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

Transcription:

OpenGL, GLUT, GLEW, GLSL An Overview GLUT GLEW GLSL

Objectives Give you an overview of the software that you will be using this semester OpenGL, GLUT, GLEW, GLSL What are they? How do you use them? What does the code look like? Evolution All of them are required to write modern All of them are required to write modern graphics code, although alternatives exist

What is OpenGL? An application programming interface (API) A(low (low-level) level) Graphics rendering API Generate high-quality images from geometric and image primitives

Maximal Portability Display device independent Window system independent Operating system independent Without a standard API (such as OpenGL) - impossible to port (100, 50) Line(100,50,150,80) 150 - device/lib 1 (150, 100) Moveto(100,50) - device/lib 2 Lineto(150,100)

Brief History of OpenGL Originated from a proprietary API called Iris GL from Silicon Graphics, Inc. Provide access to graphics hardware capabilities at the lowest possible level that still provides hardware independence The evolution is controlled by OpenGL Architecture Review Board, or ARB. OpenGL 1.0 API finalized in 1992, first implementation in 1993 In 2006, OpenGL ARB became a workgroup of the Khronos Group 10+ revisions since 1992

OpenGL Evolution 1.1 (1997): vertex arrays and texture objects 1.2 (1998): 3D textures 1.3 (2001): cubemap textures, compressed textures, multitextures 1.4 (2002): mipmap generation, shadow map textures, etc 1.5 (2003): vertex buffer object, shadow comparison functions, occlusion queries, nonpower-of-2 textures

OpenGL Evolution 2.0 (2004): vertex and fragment shading (GLSL 1.1), multiple render targets, etc 2.1 (2006): GLSL 1.2, pixel buffer objects, etc 3.0 (2008): GLSL 1.3, deprecation model, etc 3.1 (2009): GLSL 1.4, texture buffer objects, move much of deprecated functions to ARB compatible extension 3.2 (2009) 4.3 (August 2012)

OpenGL Extensions New features/functions are marked with prefix Supported only by one vendor NV_float_buffer (by nvidia) Supported by multiple vendors EXT_framebuffer_object Reviewed by ARB ARB_depth_texture Promoted to standard OpenGL API

Deprecation Model, Contexts, and Profiles Redundant and In-efficient functions are deprecated to be removed in the future glbegin(), glend() OpenGL Contexts data structures where OpenGL stores the state information used for rendering Textures, buffer objects, etc Profile A subset of OpenGL functionality used by an application program Core vs. Compatibility

OpenGL Basics OpenGL s primary function Rendering Rendering? converting geometric/mathematical object descriptions into frame buffer values OpenGL can render: Geometric primitives Bitmaps and Images (Raster primitives)

OpenGL Programming Main Steps Initialize OpenGL (using GLUT, discussed later) Define the geometry (points, lines, triangles/polygons) Define the vertex attributes (color, normal, etc) Transform the geometry (translate, rotate, scale) Set up the camera (position, direction, angle, etc) Set up lighting (light position/color etc) Set up textures Draw

Primitive Types

Specifying Geometric primitives OpenGL 1.x glbegin(primtype); //e.g. GL_TRIANGLES glvertex3f(x,y,z); glend(); OpenGL 3.x and up: use Vertex Buffer Objects (VBO) Much more efficient since it allows the geometry to be stored in the graphics card and reduce the number of function calls glgenbuffers(1, &vbohandle); // create a VBO handle glbindbuffer(gl_array_buffer, vbohandle); // bind the handle to the current VBO glbufferdata(gl_array_buffer, sizeof(vertices), vertices, GL_STATIC_DRAW); // allocate space and copy the data over

OpenGL 1.x Code Example void Display() { glclear(gl_color_buffer_bit); glcolor4f(1,1,0,1); 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(); } vertex attribute While glbegin/glend and glvertex*/glcolor* have been deprecated, it is still important to know them since there is a large amount of legacy code that uses them I will introduce OpenGL VBO in a separate lecture

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

Rest of an OpenGL program After the geometry is provided, OpenGL will control the GPUs to perform vertex and fragment processing Vertex processing: transformation and lighting (T&L) Fragment processing: shading and texture mapping Your job is to control the processing through OpenGL/GLSL API, the focus of this course OpenGL 1.x: use the fixed function pipeline OpenGL 3.x and up: provide more detailed d control to the shaders

Window-based programming Most of the modern graphics systems are window-based Non-window based environment Window based environment

Window system independent OpenGL is window system independent No window management functions create windows, resize windows, event handling, etc This is to ensure the application s portability Create some headache though just a pure OpenGL program won t work anywhere.

More APIs are needed Microsoft Windows: WGL Apple Macintosh: AGL X window system: GLX These libraries provide complete functionality to create the drawing area as well as graphics iser interface (GUI) such as sliders, buttons,, menus etc. Problem you need to learn them all to write a platform independent program

Use GLUT (OpenGL Utility Toolkit) For fast prototyping, you can use GLUT to interface with different window systems GLUT is a window independent API programs written using OpenGL and GLUT can be ported to X windows, MS windows, and Macintosh with no effort GLUT does not contain all the bells and whistles though (no sliders, no dialog boxes, no menu bar, etc) t)

GLUT Basics GLUT Program Structure 1. Configure OpenGL frame buffer 2. Create a drawing area (window) 3. Register input callback functions Render Resize Input: keyboard, mouse, etc 4. Enter event processing loop

Sample Program GLUT #include <GL/glut.h> #include <GL/gl.h> Void main(int argc, char** argv) { int mode = GLUT_RGB GLUT_SINGLE; _ glutinitdisplaymode(mode); glutinitwindowsize(500,500); glutcreatewindow(argv[0]); init(); glutdisplayfunc(display); glutkeyboardfunc(key); glutmainloop(); }

Sample Program GLUT #include <GL/glut.h> #include <GL/gl.h> Void main(int argc, char** argv) { int mode = GLUT_RGB GLUT_SINGLE; glutinitdisplaymode(mode); glutinitwindowsize(500,500); glutcreatewindow( Simple ); init(); glutdisplayfunc(display); glutkeyboardfunc(key); glutmainloop(); } Specify the display Mode RGB or color Index, single or double Buffer

Sample Program GLUT #include <GL/glut.h> #include <GL/gl.h> Void main(int argc, char** argv) { int mode = GLUT_RGB GLUT_SINGLE; glutinitdisplaymode(mode); glutinitwindowsize(500,500); glutcreatewindow( Simple ); init(); glutdisplayfunc(display); glutkeyboardfunc(key); glutmainloop(); } Create a window Named simple with resolution 500 x 500

Sample Program GLUT #include <GL/glut.h> #include <GL/gl.h> Void main(int argc, char** argv) { int mode = GLUT_RGB GLUT_SINGLE; glutinitdisplaymode(mode); glutinitwindowsize(500,500); glutcreatewindow( Simple ); glutdisplayfunc(display); glutkeyboardfunc(key); glutmainloop(); }

Sample Program GLUT #include <GL/glut.h> #include <GL/gl.h> Void main(int argc, char** argv) { int mode = GLUT_RGB GLUT_SINGLE; glutinitdisplaymode(mode); glutinitwindowsize(500,500); glutcreatewindow( Simple ); init(); glutdisplayfunc(display); glutmainloop(); } Register your call back functions

Callback functions? GLUT Most of window-based programs are event-driven which means do nothing until an event happens, and then execute some pre-defined functions Events key press, mouse button press and release, window resize, etc.

GLUT glutdisplayfunc(void (*func)(void) ) Void main(int argc, char** argv) { glutdisplayfunc(display); } void display() a function written by you. It contains all the OpenGL drawing function calls and will be called when pixels in the window need to be refreshed.

Event Queue GLUT MainLoop() Event queue Keyboard. Mouse Window Mouse_callback() {. { Keypress_callback() {. { window_callback() {. {

And many more GLUT glutkeyboardfunc() register the callback that will be called when a key is pressed glutmousefunc() register the callback that will be called when a mouse button is pressed glutmotionfunc() register the callback that will be called when the mouse is in motion while a buton is pressed glutidlefunc() register the callback that will be called when nothing is going on (no event) ent)

glutmainloop() GLUT #include <GL/glut.h> #include <GL/gl.h> Void main(int argc, char** argv) { int mode = GLUT_RGB GLUT_SINGLE; glutinitdisplaymode(mode); glutinitwindowsize(500,500); glutcreatewindow( Simple ); glutdisplayfunc(display); glutreshapefunc(resize); glutkeyboardfunc(key); glutmainloop(); } The program goes into a infinite loop waiting for events

GLEW: The OpenGL Extension Wrangler Library A cross-platform open-source C/C++ extension loading library (windows, OS X, Linux, FreeBSD) Why GLEW is needed? It is not possible to link directly to functions that are provided in newer version of OpenGL. In windows, this means OpenGL 1.2 and up GLEW does the tedious work to helps you find the function pointers (addresses of the functions) for OpenGL extensions GLEW can also help pyou to check if a particular OpenGL extension is available on your machine

GLEW usage Make sure you have GLEW on your machine Include the glew header file #include <GL/glew.h> Initialize glew before calling any opengl functions GLenum err = glewinit(); if ( err!= GLEW_OK) printf(" Error initializing GLEW! \n"); else printf("initializing GLEW succeeded!\n"); Check OpenGL features, for example, shaders if (! GLEW_ARB_vertex_program) printf(" ARB vertex program is not supported!!\n"); else printf(" ARB vertex program is supported!!\n");

OpenGL Shading Language g (GLSL) A C-like language and incorporated into OpenGL 2.0 Used to write vertex program and fragment program No distinction in the syntax between a vertex program and a fragment program Platform independent d compared to Cg

Shader Objects Shaders are defined as an array of strings Four steps to using a shader Send shader source to OpenGL Compile the shader Create an executable (i.e., link compiled shaders together) Install the executable as part of current state Goal was to mimic C/C++ source code development model

Sequence

The Programmable GPU Vertex Shader Fragment Shader vertices primitives Transform And Lighting Clipping Primitive Assembly And Rasterization Texture Stages Fragment Testing GPU = vertex shader (vertex program) + fragment shader (fragment program, pixel program) Vertex shader replaces per-vertex transform & lighting Fragment shader replaces texture stages Fragment testing after the fragment shader Flexibility to do framebuffer pixel blending

GPU programming model Stream programming Process each vertex or fragment independently Vertex v Shader 1 v 2 v n v 1 v 2 v n Fragment f 1 f 2 f n Shader f 1 f 2 f n

The idea You specify vertices as usual Vertex positions, texture coordinates, etc. And some user variables if you want The vertex shader modifies/calculates these variables. Each fragment gets the interpolated values, which might have been modified. d The fragment shader can now work on the interpolated values, including the user defined variables.

Vertex Program Replace the fixed-function operations performed by the vertex processor A vertex program is executed on each vertex Each vertex program must output the information that the rasterizer needs At a minimum transforms the vertex position The program can access all OpenGL states Current color, texture t coordinates, material properties, transformation matrices, etc The application can also supply additional input variables to the vertex program

A very simple vertex program void main(void) { gl_position = gl_projectionmatrix*gl_modelviewmatrix*gl_vertex; t i l M lvi M t i * l V t } Just a passing-through shader: convert a vertex from local space to clip space No color is assigned here, so the fragment program will need to decide the fragment colors All variables starts with gl _ are part of OpenGL state so no need to declare

GLSL Data Types Supported data types are very similar to C/C++: float, int, bool, etc Additional types examples: vec2, vec3, vec4, mat2, mat3, mat4 Can use C++ style constructor vec3 a = vec3(1.0, 2.1, -1.2); 12) vec3 b = vec2(a); //conversion

GLSL Qualifiers Three types of variables: Attributes, Uniform, Varying Attribute: used by vertex shaders for variables that can change once per vertex Build-in attributes: gl_vertex, gl_frontcolor User-definted attributes (example): temperature, velocity Uniform: variables set for the entire primitive, i.e., assigned outside glbegin()/glend(); le Also include build-in and user-definted

GLSL Qualifiers (cont d) Varying variables: the mechanism for conveying data from a vertex program to a fragment program Defined on a per vertex basis but interpolated over the primitive for the fragment program. Include build-in and user defined varying variables

Vertex Processor Input Vertex shader is executed once each time a vertex position is specified Via glvertex or gldrawarrays or other vertex array calls Per-vertex input values are called attributes Change every vertex Passed through normal OpenGL mechanisms (per-vertex API or vertex arrays) More persistent input values are called uniforms Can come from OpenGL state or from the application Constant across at least one primitive, typically constant for many primitives Passed through new OpenGL API calls

Vertex Processor Output Vertex shader uses input values to compute output values Vertex shader must compute gl_position Mandatory, needed by the rasterizer Can use built-in function ftransform() to get invariance with fixed functionality

Vertex Processor Output Other output values are called varying variables E.g., color, texture t coordinates, arbitrary data Will be interpolated in a perspective-correct fashion across the primitives Defined by the vertex shader Can be of type float, vec2, vec3, vec4, mat2, mat3, mat4, or arrays of these Output of vertex processor feeds into OpenGL fixed functionality If a fragment shader is active, output of vertex shader must match input of fragment shader If no fragment shader is active, output of vertex shader must match the needs of fixed functionality fragment processing

Fragment Program The fragment program is executed after rasterizer and operate on each fragment Vertex attributes (colors, positions, texture coordinates, etc) are interpolated across a primitive automatically as the input to the fragment program Fragment program can access OpenGL state, (interpolated) output from vertex program, and user defined variables

A very simple fragment program void main(void) { gl_fragcolor l = gl_frontcolor; l } Just a passing-through fragment shader

Fragment Program Input Output of vertex shader is the input to the fragment shader Compatibility is checked when linking occurs Compatibility between the two is based on varying variables that are defined in both shaders and that match in type and name Fragment shader is executed for each fragment pod produced edby rasterization tei tion For each fragment, fragment shader has access to the interpolated value for each varying variable ab Color, normal, texture coordinates, arbitrary values

Fragment Processor Input Fragment shader may access: gl_frontfacing contains facingness of primitive that produced the fragment gl_fragcoord contains computed window relative coordinates x, y, z, 1/w Uniform variables are also available OpenGL state or supplied by the application, same as for vertex shader If no vertex shader is active, fragment shader get the results of OpenGL fixed functionality

Fragment Processor Output Output of the fragment processor goes on to the fixed function fragment operations and frame buffer operations using built-in variables gl_fragcolor computed R, G, B, A for the fragment gl_fragdepth computed ddepth value for the fragment gl_fragdata[n] arbitrary data per fragment, stored in multiple render targets Values are destined for writing into the frame buffer if all back end tests (stencil, depth etc.) pass Clamping or format conversion to the target buffer is done automatically outside of the fragment shader