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

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

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

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

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

CS Computer Graphics: Intro to OpenGL

CS Computer Graphics: Intro to OpenGL

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

CS 432 Interactive Computer Graphics

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

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

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

COMPUTER GRAPHICS LAB # 3

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

Announcements OpenGL. Computer Graphics. Spring CS4815

Drawing Primitives. OpenGL basics

Using OpenGL with CUDA

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

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

LECTURE 02 OPENGL API

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

An Overview GLUT GLSL GLEW

Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2)

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

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

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

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

CS 432 Interactive Computer Graphics

C++ - OpenGL - Setting up Visual Studio

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

Programming using OpenGL: A first Introduction

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

Computer Graphics. Bing-Yu Chen National Taiwan University

Objectives. Topic 4: OpenGL An Example Program. simple.cpp revisited. Defining Objects in OpenGL Programs. Build a complete OpenGL program

Computer Graphics (CS 543) Lecture 1 (Part 1): Introduction to Computer Graphics

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

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

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

Lecture 3. Understanding of OPenGL programming

Computer Graphics (CS 543) Lecture 4a: Linear Algebra for Graphics (Points, Scalars, Vectors)

Input and Interaction. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Graphics Programming

CSE4030 Introduction to Computer Graphics

Assignment 1. Simple Graphics program using OpenGL

Programming with OpenGL Part 1: Background

Lecture 3. Programming with OpenGL GLUT + GLEW. CS 354 Computer Graphics Sunday, January 20, 13

Computer Graphics CS 543 Lecture 5 (Part 2) Implementing Transformations

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

OpenGL refresher. Advanced Computer Graphics 2012

CS 548: COMPUTER GRAPHICS PORTRAIT OF AN OPENGL PROGRAM SPRING 2015 DR. MICHAEL J. REALE

Bob s Concise Introduction to Doxygen

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

Rendering Objects. Need to transform all geometry then

Introduction to Computer Graphics with OpenGL/GLUT

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

Introduction to MS Visual C/C++

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

Computer Graphics Anatomy of GUI. Computer Graphics CSC470 1

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

Comp 410/510 Computer Graphics Spring Input & Interaction

Introduction to OpenGL: Part 2

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

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

RECITATION - 1. Ceng477 Fall

Introduction to OpenGL Week 1

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

Computer Graphics: Line Drawing Algorithms

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

Interaction. CSCI 420 Computer Graphics Lecture 3

Programming with WebGL Part 1: Background. CS 432 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Computer Graphics. OpenGL

Programming with OpenGL Part 3: Three Dimensions

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

CS475/CS675 - Computer Graphics. OpenGL Drawing

CS380: Computer Graphics Basic OpenGL Structure. Sung-Eui Yoon ( 윤성의 ) Course URL:

Introduction to OpenGL

CS 450: COMPUTER GRAPHICS REVIEW: STATE, ATTRIBUTES, AND OBJECTS SPRING 2015 DR. MICHAEL J. REALE

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

GLUT Tutorial. John Tsiombikas (Nuclear / The Lab Demos) May 6, Introduction to GLUT (OpenGL Utility Toolkit)

Introduction to Computer Graphics. Hardware Acceleration Review

Lecture 11: Callbacks. CITS 3003 Graphics & Animation

CS 4204 Computer Graphics

Computação Gráfica. Computer Graphics Engenharia Informática (11569) 3º ano, 2º semestre. Chap. 4 Windows and Viewports

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

Objectives. Programming with OpenGL Part 5: More GLSL. Program Object. Reading a Shader. Shader Reader. Linking Shaders with Application

From system point of view, a graphics application handles the user input, changes the internal state, called the virtual world by modeling or

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

CS 380 Introduction to Computer Graphics. LAB (1) : OpenGL Tutorial Reference : Foundations of 3D Computer Graphics, Steven J.

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

CS 432 Interactive Computer Graphics

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Programming with OpenGL Part 1: Background

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

Basic Graphics Programming

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

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

Objectives. Programming with WebGL Part 1: Background. Retained vs. Immediate Mode Graphics. Early History of APIs. PHIGS and X.

Transcription:

Computer Graphics (CS 543) Lecture 1 (Part 2): Introduction to OpenGL/GLUT (Part 1) Prof Emmanuel Agu Computer Science Dept. Worcester Polytechnic Institute (WPI)

OpenGL/GLUT Installation OpenGL: Specific version (e.g. 4.3) on your graphics card hardware Just check OpenGL version on your graphics card GLUT: software that needs to be installed already installed in zoolab machines GLUT: install it! OpenGL: already on graphics card

glinfo: Finding out about your Graphics Card Software tool to find out OpenGL version and extensions your graphics card supports This class? Need graphics card with OpenGL 4.3 or later OpenGL extensions on Graphics card OpenGL version on Graphics card

OpenGL Extension Wrangler Library (GLEW) OpenGL extensions: card manufacturers may implement new proprietary features after latest OpenGL version released Published, made available as extension to latest OpenGL GLEW: library to access OpenGL extensions on a graphics card We will install GLEW as well

Windows Installation of GLUT, GLEW Note: GLUT, GLEW already installed in zoolab. Can just go there The following instructions only useful if you want to install on your home machine 1. Install Visual Studio (e.g 2017) 2. Download freeglut 32-bit (GLUT implementation) http://freeglut.sourceforge.net/ Check graphics card 3. Download 32-bit GLEW http://glew.sourceforge.net/ Install GLUT, GLEW 4. Unzip GLUT, GLEW =>.lib,.h,.dll files E.g. freeglut 3.0.0, files: freeglut.dll, glut.h, freeglut.lib

Windows Installation of GLUT, GLEW Check graphics card Install GLUT, GLEW Install.lib,.h,.dll files (for GLUT and GLEW) from zip files. Put.dll files in C:\windows\system.h files in c:\visual Studio \include\ directory.lib files in c:\visual Studio. \lib\ directory

Getting Started: Writing.cpp In Visual studio 1. Create empty project, 2. Create blank console application (C program) 3. Include glew.h and glut.h at top of your program #include <glew.h> #include <GL/glut.h> Create VS Solution GLUT, GLEW includes Note: GL/ is sub-directory of compiler include/ directory OpenGL drawing functions in gl.h glut.h contains GLUT functions, also includes gl.h

Getting Started: More #includes Most OpenGL applications use standard C library (e.g printf), so #include <glew.h> #include <GL/glut.h> #include <stdlib.h> #include <stdio.h>

OpenGL/GLUT Program Structure Open window (GLUT) Configure display mode, window position/size Register GLUT callback functions (GLUT) Render, resize, input: keyboard, mouse, etc Custom initialization Set background color, clear color, etc Generate points to be drawn Initialize shaders Initialize GLEW glutmainloop( ) Waits here infinitely till event Open GLUT Window Register callback fns My Initializations Initialize GLEW GLUT main loop

OpenGL Drawing Window?

Opening a GLUT window GLUT Commands used glutinit(&argc, argv); Initializes GLUT glutinitdisplaymode(glut_single GLUT_RGB); sets display mode (e.g. single framebuffer with RGB colors) glutinitwindowsize(640,480); sets window size (Width x Height) in pixels glutinitposition(100,150); sets location of upper left corner of window glutcreatewindow( Hello world ); open window with title Hello world Then initialize GLEW glewinit( ); 100 150 Hello world 480 640

OpenGL Skeleton void main(int argc, char** argv){ // First initialize toolkit, set display mode and create window glutinit(&argc, argv); // initialize toolkit glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowsize(640, 480); glutinitwindowposition(100, 150); glutcreatewindow( my first attempt ); glewinit( ); 150 // then register callback functions, // do my initialization //.. wait in glutmainloop for events 100 my first attempt 480 } 640

Sequential Vs Event-driven OpenGL programs are event-driven Sequential program Start at main( ) Perform actions 1, 2, 3. N End Event-driven program Start at main( ) Initialize Wait in infinite loop Wait till defined event occurs Event occurs => Take defined actions What is world s most widely used event-driven program?

OpenGL: Event-driven Program only responds to events Do nothing until event occurs Example Events: Redraw scene in OpenGL window mouse clicks, keyboard stroke Programmer defines: Events that program should respond to Actions to be taken when event occurs Operating system (e.g. Windows): Receives event, maintains event queue Left mouse click Keyboard h key Calls functions defined by programmer for specific event

OpenGL: Event-driven How in OpenGL? Programmer declares, registers callback functions (event handler) Callback function called when event occurs Example: Programmer 1. Declare function mymouse, to be called on mouse click 2. Register it: glutmousefunc(mymouse); When OS receives mouse click, calls callback function mymouse Mouse click Event mymouse( ){ }. Callback function

GLUT Callback Functions Register callbacks for all events your program will react to No registered callback = no action Example: if no registered keyboard callback function, hitting keyboard keys generates NO RESPONSE!!

GLUT Callback Functions GLUT Callback functions in skeleton glutdisplayfunc(mydisplay): Initial drawing put here glutreshapefunc(myreshape): called when window is reshaped glutmousefunc(mymouse): called when mouse button pressed glutkeyboardfunc(mykeyboard): called when keyboard is pressed or released glutmainloop( ): program draws initial picture (by calling mydisplay function once) Enters infinite loop till event occurs

OpenGL Skeleton void main(int argc, char** argv){ // First initialize toolkit, set display mode and create window glutinit(&argc, argv); // initialize toolkit glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowsize(640, 480); glutinitwindowposition(100, 150); glutcreatewindow( my first attempt ); glewinit( ); // now register callback functions glutdisplayfunc(mydisplay); --Next how to draw in mydisplay glutreshapefunc(myreshape); glutmousefunc(mymouse); glutkeyboardfunc(mykeyboard); } myinit( ); glutmainloop( );

Example: Draw in function mydisplay Task: Draw red triangle on white background Rendering steps: 1. Generate triangle corners (3 vertices) 2. Create GPU buffer for vertices 3. Move array of 3 vertices from CPU to GPU buffer 4. Draw 3 points from array on GPU using gldrawarray

Example: Retained Mode Graphics Rendering steps: 1. Generate triangle corners (3 vertices) 2. Create GPU buffer for vertices 3. Move array of 3 vertices from CPU to GPU buffer 4. Draw 3 points from array on GPU using gldrawarray Simplified Execution model: 1. Generate 3 triangle corners 3. Move array of 3 vertices from CPU to GPU buffer 2. Create GPU buffers for vertices Application Program (on CPU) GPU 4. Draw points using gldrawarrays Rendered vertices

1. Generate triangle corners (3 vertices) // declare array of 3 points point2 points[3]; // generate 3 triangle vertices + store in points array void generategeometry( void ){ points[0] = point2( -0.5, -0.5 ); points[1] = point2( 0.0, 0.5 ); points[2] = point2( 0.5, -0.5 ); } (0.0, 0.5) x y (-0.5, -0.5) (0.5, -0.5)

Declare some Types for Points, vectors Useful declarations (homegrown) in header file vec.h from book point2 for (x,y) locations vec3 for (x,y,z) vector coordinates Need to include header vec.h Example usage: #include vec.h vec3 vector1; Declares (x, y, z) coordinates of a vector Note: You will be given file Angel.h from book. It includes vec.h

OpenGL Skeleton: Where are we? void main(int argc, char** argv){ glutinit(&argc, argv); // initialize toolkit glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowsize(640, 480); glutinitwindowposition(100, 150); glutcreatewindow( my first attempt ); glewinit( ); } // now register callback functions glutdisplayfunc(mydisplay); glutreshapefunc(myreshape); glutmousefunc(mymouse); glutkeyboardfunc(mykeyboard); glewinit( ); generategeometry( ); glutmainloop( ); // generate 3 triangle vertices + store in array void generategeometry( void ){ points[0] = point2( -0.5, -0.5 ); points[1] = point2( 0.0, 0.5 ); points[2] = point2( 0.5, -0.5 ); }

2. Create GPU Buffer for Vertices Rendering from GPU memory significantly faster. Move data there Fast GPU (off-screen) memory for data called Vertex Buffer Objects (VBO) Array of VBOs (called Vertex Array Object (VAO)) usually created Example use: vertex positions in VBO 1, color info in VBO 2, etc So, first create the VAO GLuint vao; VAO VBO VBO VBO glgenvertexarrays( 1, &vao ); // create 1 VAO glbindvertexarray( vao ); // make VAO active

2. Create GPU Buffer for Vertices Next, create a buffer object in two steps 1. Create VBO and give it name (unique ID number) GLuint buffer; glgenbuffers(1, &buffer); // create one VBO Number of Buffer Objects to return VAO VBO 2. Make created VBO currently active one glbindbuffer(gl_array_buffer, buffer); Data is array of values

3. Move points GPU memory 3. Move points to VBO glbufferdata(gl_array_buffer, buffer, sizeof(points), points, GL_STATIC_DRAW ); //data is array Data to be transferred to GPU memory (generated earlier) GL_STATIC_DRAW: buffer object data will not be changed. Specified once by application and used many times to draw GL_DYNAMIC_DRAW: buffer object data will be changed. Specified repeatedly and used many times to draw

Put it Together: 2. Create GPU Buffer for Vertices 3. Move points GPU memory void initgpubuffers( void ) { // Create a vertex array object GLuint vao; glgenvertexarrays( 1, &vao ); glbindvertexarray( vao ); VAO VBO } // Create and initialize a buffer object GLuint buffer; glgenbuffers( 1, &buffer ); glbindbuffer( GL_ARRAY_BUFFER, buffer ); glbufferdata( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW );

OpenGL Skeleton: Where are we? void main(int argc, char** argv){ glutinit(&argc, argv); // initialize toolkit glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowsize(640, 480); glutinitwindowposition(100, 150); glutcreatewindow( my first attempt ); glewinit( ); } // now register callback functions glutdisplayfunc(mydisplay); glutreshapefunc(myreshape); glutmousefunc(mymouse); glutkeyboardfunc(mykeyboard); glewinit( ); generategeometry( ); initgpubuffers( ); glutmainloop( ); void initgpubuffers( void ) { // Create a vertex array object GLuint vao; glgenvertexarrays( 1, &vao ); glbindvertexarray( vao ); } // Create and initialize a buffer object GLuint buffer; glgenbuffers( 1, &buffer ); glbindbuffer( GL_ARRAY_BUFFER, buffer ); glbufferdata( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW );

4. Draw points (from VBO) gldrawarrays(gl_points, 0, N); Render buffered data as points Starting index Number of points to be rendered Display function using gldrawarrays: void mydisplay(void){ glclear(gl_color_buffer_bit); gldrawarrays(gl_line_loop, 0, 3); glflush( ); } // clear screen // draw the points // force rendering

References Angel and Shreiner, Interactive Computer Graphics, 6 th edition, Chapter 2 Hill and Kelley, Computer Graphics using OpenGL, 3 rd edition, Chapter 2