Interaction. CSCI 420 Computer Graphics Lecture 3

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

Interaction. CSCI 480 Computer Graphics Lecture 3

Interaction Computer Graphics I Lecture 3

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

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

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Basic Graphics Programming

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

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

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. 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

Basic Graphics Programming

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

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

Lecture 4 of 41. Lab 1a: OpenGL Basics

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

Order of Transformations

OpenGL refresher. Advanced Computer Graphics 2012

Lectures OpenGL Introduction

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

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

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

Computer graphics MN1

Introduction to Computer Graphics with OpenGL/GLUT

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

Modeling Transform. Chapter 4 Geometric Transformations. Overview. Instancing. Specify transformation for objects 李同益

CSC 470 Computer Graphics

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

Programming with OpenGL Part 3: Three Dimensions

Lecture 3 Advanced Computer Graphics (CS & SE )

Computer Graphics. Bing-Yu Chen National Taiwan University

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

1/12/11. Basic Graphics Programming. What is OpenGL. OpenGL is cross-platform. How does OpenGL work. How does OpenGL work (continued) The result

Class of Algorithms. Visible Surface Determination. Back Face Culling Test. Back Face Culling: Object Space v. Back Face Culling: Object Space.


Precept 2 Aleksey Boyko February 18, 2011

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

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

1 (Practice 1) Introduction to OpenGL

1.2 Basic Graphics Programming

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

C OMPUTER G RAPHICS Thursday

Using OpenGL with CUDA

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

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

CSE 167: Introduction to Computer Graphics Lecture #5: Visibility, OpenGL

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

CENG 477 Introduction to Computer Graphics. Graphics Hardware and OpenGL

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

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

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

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

Blue colour text questions Black colour text sample answers Red colour text further explanation or references for the sample answers

CSE 167: Introduction to Computer Graphics Lecture #5: Rasterization. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

Graphics Hardware and OpenGL

Lectures Display List

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

Computer Graphics (Basic OpenGL)

Transformation Pipeline

Programming using OpenGL: A first Introduction

RECITATION - 1. Ceng477 Fall

Computer Graphics Course 2005

CSE 167: Introduction to Computer Graphics Lecture #5: Projection. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2017

Lecture 2 2D transformations Introduction to OpenGL

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

COMPUTER GRAPHICS LAB # 3

Visualizing Molecular Dynamics

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

Graphics Programming

SOURCES AND URLS BIBLIOGRAPHY AND REFERENCES

Introduction to OpenGL

Lecture 3. Understanding of OPenGL programming

Computer Graphics. OpenGL

Computer Graphics Seminar

Display Lists in OpenGL

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

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

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

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

COMP371 COMPUTER GRAPHICS

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

Rendering Pipeline/ OpenGL

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

CS 4204 Computer Graphics

Computer graphics MN1

CS 432 Interactive Computer Graphics

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

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

Comp 410/510 Computer Graphics Spring Input & Interaction

Rasterization Overview

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

Lecture 2 Sections 3.6, 3.7. Fri, Aug 28, 2009

2 Transformations and Homogeneous Coordinates

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

Lecture 11: Callbacks. CITS 3003 Graphics & Animation

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

Computer Graphics Primitive Attributes

Transcription:

CSCI 420 Computer Graphics Lecture 3 Interaction Jernej Barbic University of Southern California Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations [Angel Ch. 2] 1

Triangles (Clarification) Can be any shape or size Well-shaped triangles have advantages for numerical simulation Shape quality makes little difference for basic OpenGL rendering 2

Choice of Programming Language OpenGL lives close to the hardware OpenGL is not object-oriented OpenGL is not a functional language (as in, ML) Use C to expose and exploit low-level details Use C++, Java,... for toolkits Support for C in assignments 3

Client/Server Model Graphics hardware and caching Client Server CPU GPU Important for efficiency Need to be aware where data are stored Graphics driver code is on the CPU Rendering resources (buffers, shaders, textures, etc.) are on the GPU 4

The CPU-GPU bus AGP, PCI, PCI Express Fast, but limited bandwidth CPU can also read back GPU 5

Buffer Objects Store rendering data: vertex positions, normals, texture coordinates, colors, vertex indices, etc. Optimize and store on server (GPU) Client CPU bus Server GPU Store here 6

Vertex Buffer Objects Caches vertex geometric data: positions, normals, texture coordinates, colors Optimize and store on server (GPU) Required for core OpenGL profile /* vertices of the quad (will form two triangles; rendered via GL_TRIANGLE_STRIP) */ float positions[4][3] = {{-1.0, -1.0, -1.0}, {1.0, -1.0, -1.0}, {-1.0, 1.0, -1.0}, {1.0, 1.0, -1.0}}; /* colors to be assigned to vertices */ float colors[4][4] = {{0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0,1.0}, {1.0, 1.0, 0.0, 1.0}, {0.0, 1.0, 0.0, 1.0}}; 7

Vertex Buffer Object: Initialization GLuint buffer; void initvbo() { glgenbuffers(1, &buffer); glbindbuffer(gl_array_buffer, buffer); glbufferdata(gl_array_buffer, sizeof(positions) + sizeof(colors), NULL, GL_STATIC_DRAW); // init buffer s size, but don t assign any data to it // upload position data glbuffersubdata(gl_array_buffer, 0, sizeof(positions), positions); } // upload color data glbuffersubdata(gl_array_buffer, sizeof(positions), sizeof(colors), colors); 8

Old technology: Display Lists (compatibility profile only) Cache a sequence of drawing commands Very useful with complex objects that are redrawn often (e.g., with transformations) Another example: fonts (2D or 3D) Display lists can call other display lists Display lists cannot be changed Display lists can be erased / replaced Display lists are now deprecated in OpenGL Replaced with VBOs 9

Display Lists Cache a sequence of drawing commands Optimize and store on server (GPU) GLuint listname = glgenlists(1); /* new list name */ glnewlist (listname, GL_COMPILE); /* new list */ glcolor3f(1.0, 0.0, 1.0); glbegin(gl_triangles); glvertex3f(0.0, 0.0, 0.0);... glend(); glendlist(); /* at this point, OpenGL compiles the list */ glcalllist(listname); /* draw the object */ 10

Element Arrays Draw cube with 6*2*3=36 or with 8 vertices? Expense in drawing and transformation Triangle strips help to some extent Element arrays provide general solution Define (transmit) array of vertices, colors, normals Draw using index into array(s) : // (must first set up the GL_ELEMENT_ARRAY_BUFFER) gldrawelements(gl_triangles, 36, GL_UNSIGNED_INT, 0); Vertex sharing for efficient operations Extra credit for first assignment 11

Outline Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations Example 12

GLUT Program with Callbacks START Initialization Main event loop Idle() Reshape(..) Motion(..) Mouse(..) Display() Keyboard(..) Menu(..) END 13

Main Event Loop Standard technique for interaction (GLUT, Qt, wxwidgets,...) Main loop processes events Dispatch to functions specified by client Callbacks also common in operating systems Poor man s functional programming 14

Types of Callbacks Display ( ) : when window must be drawn Idle ( ) : when no other events to be handled Keyboard (unsigned char key, int x, int y) : key pressed Menu (...) : after selection from menu Mouse (int button, int state, int x, int y) : mouse button Motion (...) : mouse movement Reshape (int w, int h) : window resize Any callback can be NULL 15

Outline Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations Example 16

Screen Refresh Common: 60-100 Hz Flicker if drawing overlaps screen refresh Problem during animation Solution: use two separate frame buffers: Draw into one buffer Swap and display, while drawing into other buffer Desirable frame rate >= 30 fps (frames/second) 17

Enabling Single/Double Buffering glutinitdisplaymode(glut_single); glutinitdisplaymode(glut_double); Single buffering: Must call glfinish() at the end of Display() Double buffering: Must call glutswapbuffers() at the end of Display() Must call glutpostredisplay() at the end of Idle() If something in OpenGL has no effect or does not work, check the modes in glutinitdisplaymode 18

Outline Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations Example 19

Hidden Surface Removal Classic problem of computer graphics What is visible after clipping and projection? Object-space vs image-space approaches Object space: depth sort (Painter s algorithm) Image space: z-buffer algorithm Related: back-face culling 20

Object-Space Approach Consider objects pairwise Complexity O(k 2 ) where k = # of objects Painter s algorithm: render back-to-front Paint over invisible polygons How to sort and how to test overlap? 21

Depth Sorting First, sort by furthest distance z from viewer If minimum depth of A is greater than maximum depth of B, A can be drawn before B If either x or y extents do not overlap, A and B can be drawn independently 22

Some Difficult Cases Sometimes cannot sort polygons! Cyclic overlap Piercing Polygons One solution: compute intersections & subdivide Do while rasterizing (difficult in object space) 23

Painter s Algorithm Assessment Strengths Simple (most of the time) Handles transparency well Sometimes, no need to sort (e.g., heightfield) Weaknesses Clumsy when geometry is complex Sorting can be expensive Usage PostScript interpreters OpenGL: not supported (must implement Painter s Algorithm manually) 24

Image-space approach 3D geometry Depth image darker color is closer Source: Wikipedia 25

Depth sensor camera 26

Image-Space Approach Raycasting: intersect ray with polygons O(k) worst case (often better) Images can be more jagged (need anti-aliasing) 27

The z-buffer Algorithm z-buffer stores depth values z for each pixel Before writing a pixel into framebuffer: Compute distance z of pixel from viewer If closer, write and update z-buffer, otherwise discard 28

z-buffer Algorithm Assessment Strengths Simple (no sorting or splitting) Independent of geometric primitives Weaknesses Memory intensive (but memory is cheap now) Tricky to handle transparency and blending Depth-ordering artifacts Usage z-buffering comes standard with OpenGL; disabled by default; must be enabled 29

Depth Buffer in OpenGL glutinitdisplaymode(glut_double GLUT_RGBA GLUT_DEPTH); glenable (GL_DEPTH_TEST); Inside Display(): glclear (GL_DEPTH_BUFFER_BIT); Remember all of these! Some tricks use z-buffer in read-only mode 30

Note for Mac computers Must use the GLUT_3_2_CORE_PROFILE flag to use the core profile: glutinitdisplaymode(glut_3_2_core_profile GLUT_DOUBLE GLUT_RGBA GLUT_DEPTH); 31

Outline Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations Example 32

Specifying the Viewing Volume: Compatibility Mode Clip everything not in viewing volume Separate matrices for transformation and projection glmatrixmode (GL_PROJECTION); glloadidentity();... Set viewing volume... glmatrixmode(gl_modelview); 33

Specifying the Viewing Volume: Core Profile Clip everything not in viewing volume Set the 4x4 projection matrix manually (or via our provided openglmatrix library) (Lecture: Viewing ) 34

Parallel Viewing Orthographic projection Camera points in negative z direction Compatibility profile: glortho(xmin, xmax, ymin, ymax, near, far) Core profile: set the 4x4 matrix manually (or via our provided openglmatrix library) 35

Perspective Viewing Slightly more complex Compatibility profile: glfrustum(left, right, bottom, top, near, far) Core profile: set the 4x4 matrix manually (or via our provided openglmatrix library) 36

Simple Transformations: Compatibility Profile Rotate by given angle (in degrees) about axis given by (x, y, z) glrotate{fd}(angle, x, y, z); Translate by the given x, y, and z values gltranslate{fd}(x, y, z); Scale with a factor in the x, y, and z direction glscale{fd}(x, y, z); 37

Simple Transformations: Core Profile Rotate by given angle (in degrees) about axis given by (x, y, z) Translate by the given x, y, and z values Scale with a factor in the x, y, and z direction Create these 4x4 matrices manually (or via our provided openglmatrix library) (Lecture: Transformations ) 38

Outline Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations Example 39

Example: Rotating Colored Quad Problem: Draw a colored quad (two triangles) Rotate it about x, y, or z axis, depending on left, middle or right mouse click Stop when the space bar is pressed Quit when q or Q is pressed 40

Step 1: Defining the Vertices Use separate arrays for vertices and colors /* vertices of the quad (will form two triangles; rendered via GL_TRIANGLE_STRIP) */ float positions[4][3] = {{-1.0, -1.0, -1.0}, {1.0, -1.0, -1.0}, {-1.0, 1.0, -1.0}, {1.0, 1.0, -1.0}}; /* colors to be assigned to vertices */ float colors[4][4] = {{0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, 1.0}, {1.0, 1.0, 0.0, 1.0}, {0.0, 1.0, 0.0, 1.0}}; // black, red, yellow, green 41

Step 2: Set Up z-buffer and Double Buffering int main(int argc, char **argv) { } glutinit(&argc, argv); // double buffering for smooth animation glutinitdisplaymode (GLUT_DOUBLE GLUT_DEPTH GLUT_RGBA);... // window creation and callbacks here init(); // our custom initialization glutmainloop(); return(0); 42

Step 3: Install Callbacks Create window and set callbacks glutinitwindowsize(800, 800); glutcreatewindow( quad"); glutreshapefunc(myreshape); glutdisplayfunc(display); glutidlefunc(spinquad); glutmousefunc(mouse); glutkeyboardfunc(keyboard); 43

Step 4: Our Initialization Function #include openglmatrix.h // our own (cs420) helper library OpenGLMatrix * matrix; void init() { glclearcolor (0.0, 0.0, 0.0, 0.0); glenable(gl_depth_test); matrix = new OpenGLMatrix(); initvbo(); initpipelineprogram(); } 44

Step 5: Init Vertex Buffer Object (VBO) GLuint buffer; void initvbo() { glgenbuffers(1, &buffer); glbindbuffer(gl_array_buffer, buffer); glbufferdata(gl_array_buffer, sizeof(positions) + sizeof(colors), NULL, GL_STATIC_DRAW); // init buffer s size, but don t assign any // data to it // upload position data glbuffersubdata(gl_array_buffer, 0, sizeof(positions), positions); } // upload color data glbuffersubdata(gl_array_buffer, sizeof(positions), sizeof(colors), colors); 45

Step 6: Init Pipeline Program void initpipelineprogram() { // initialize shader pipeline program (next lecture) // } 46

Step 7: Reshape Callback Set projection and viewport, preserve aspect ratio void myreshape(int w, int h) { GLfloat aspect = (GLfloat) w / (GLfloat) h; glviewport(0, 0, w, h); matrix->setmatrixmode(openglmatrix::projection); matrix->loadidentity(); matrix->ortho(-2.0, 2.0, -2.0/aspect, 2.0/aspect, 0.0, 10.0); matrix->setmatrixmode(openglmatrix::modelview); } 47

Step 8: Display Callback Clear, rotate, draw, swap GLfloat theta[3] = {0.0, 0.0, 0.0}; void display() { glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); matrix->loadidentity(); matrix->lookat(0, 0, 0, 0, 0, -1, 0, 1, 0); // default camera matrix->rotate(theta[0], 1.0, 0.0, 0.0); matrix->rotate(theta[1], 0.0, 1.0, 0.0); matrix->rotate(theta[2], 0.0, 0.0, 1.0); bindprogram(); renderquad(); glutswapbuffers(); } 48

Step 9: Bind Program void bindprogram() { glbindbuffer(gl_array_buffer, buffer); // so that glvertexattribpointer refers to the correct buffer GLuint loc = glgetattriblocation(program, position ); glenablevertexattribarray(loc); const void * offset = (const void*) 0; glvertexattribpointer(loc, 3, GL_FLOAT, GL_FALSE, 0, offset); GLuint loc2 = glgetattriblocation(program, color ); glenablevertexattribarray(loc2); const void * offset = (const void*) sizeof(positions); glvertexattribpointer(loc2, 4, GL_FLOAT, GL_FALSE, 0, offset); } // write projection and modelview matrix to shader // next lexture 49

Step 10: Drawing the Quad Use GL_TRIANGLE_STRIP Must be careful about the vertex order in the array positions void renderquad() { GLint first = 0; GLsizei count = 4; gldrawarrays(gl_triangle_strip, first, count ); } 50

Step 11: Animation Set idle callback GLfloat delta = 2.0; GLint axis = 2; void spinquad() { // spin the quad delta degrees around the selected axis theta[axis] += delta; if (theta[axis] > 360.0) theta[axis] -= 360.0; } // display result (do not forget this!) glutpostredisplay(); 51

Step 12: Change Axis of Rotation Mouse callback void mouse(int btn, int state, int x, int y) { if ((btn==glut_left_button) && (state == GLUT_DOWN)) axis = 0; if ((btn==glut_middle_button) && (state == GLUT_DOWN)) axis = 1; } if ((btn==glut_right_button) && (state == GLUT_DOWN)) axis = 2; 52

Step 13: Toggle Rotation or Exit Keyboard callback void keyboard(unsigned char key, int x, int y) { if (key == 'q' key == 'Q') exit(0); if (key == ' ') stop =!stop; if (stop) glutidlefunc(null); else glutidlefunc(spinquad); } 53

Summary Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations Example 54

Announcements Assignment 1 will be posted this week Microsoft Visual Studio (Windows) access enabled via Microsoft s MSDN Please start early Check web page for instructions 55