Interaction Computer Graphics I Lecture 3

Similar documents
Interaction. CSCI 480 Computer Graphics Lecture 3

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

Order of Transformations

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

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

Basic Graphics Programming

OpenGL/GLUT Intro. Week 1, Fri Jan 12

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


Computer graphics MN1

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

CSC 470 Computer Graphics

Programming with OpenGL Part 3: Three Dimensions

SOURCES AND URLS BIBLIOGRAPHY AND REFERENCES

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

Precept 2 Aleksey Boyko February 18, 2011

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

Display Lists in OpenGL

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)

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

Basic Graphics Programming

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

Computer graphics MN1

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

Introduction to Computer Graphics with OpenGL/GLUT

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

Lecture 3 Advanced Computer Graphics (CS & SE )

Lectures OpenGL Introduction

Exercise 1 Introduction to OpenGL

Using OpenGL with CUDA

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

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

1 (Practice 1) Introduction to OpenGL

Lecture 3. Understanding of OPenGL programming

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

C OMPUTER G RAPHICS Thursday

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Fundamental Types of Viewing

OpenGL refresher. Advanced Computer Graphics 2012

Computer Graphics (Basic OpenGL)

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

RECITATION - 1. Ceng477 Fall

Display Lists. Conceptually similar to a graphics file. In client-server environment, display list is placed on server

Lectures Display List

Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

COMPUTER GRAPHICS LAB # 3

1.2 Basic Graphics Programming

UNIT 7 LIGHTING AND SHADING. 1. Explain phong lighting model. Indicate the advantages and disadvantages. (Jun2012) 10M

Programming using OpenGL: A first Introduction

Lecture 4 of 41. Lab 1a: OpenGL Basics

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

Lecture 11: Callbacks. CITS 3003 Graphics & Animation

Computer Graphics. Bing-Yu Chen National Taiwan University

Introduction to OpenGL

Computer Graphics Course 2005

Lecture 5: Viewing. CSE Computer Graphics (Fall 2010)

Graphics Hardware and OpenGL

CS D Transformation. Junqiao Zhao 赵君峤

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

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

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

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

QUESTION 1 [10] 2 COS340-A October/November 2009

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

Introduction to OpenGL: Part 2

Computer Graphics. OpenGL

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

Transformation Pipeline

CS 4204 Computer Graphics

Computer graphics MN1

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

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

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

1) Program to display a set of values {fij} as a rectangular mesh. #include<gl/glut.h> #define maxx 20. #define maxy 20. #define dx 15.

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

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

Graphics Programming

OpenGL Introduction Computer Graphics and Visualization

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

Computer Graphics Primitive Attributes

COS340A Assignment 1 I Pillemer Student# March 25 th 2007 p1/15

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

Rendering Pipeline/ OpenGL

Input and Interaction

Transformations (Rotations with Quaternions) October 24, 2005

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

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

Objectives. transformation. General Transformations. Affine Transformations. Notation. Pipeline Implementation. Introduce standard transformations

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

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

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

Comp 410/510 Computer Graphics Spring Input & Interaction

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

Building Models. Objectives Introduce simple data structures for building polygonal models. Vertex lists Edge lists

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

OpenGL and X, Column 1: An OpenGL Toolkit

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

Transcription:

15-462 Computer Graphics I Lecture 3 Interaction Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations January 21, 2003 [Angel Ch. 3] Frank Pfenning Carnegie Mellon University http://www.cs.cmu.edu/~fp/courses/graphics/

Surface Orientation (Clarification) Right-hand rule Triangle strip drawn 0-1-2, 2-1-3, 2-3-4, etc. All triangles face same direction (here: back) Similarly for quad strips 0-1-3-2, 2-3-5-4, etc. Orientable surfaces; discard back faces: glenable(gl_cull_face); glcullface(gl_back); /* do not draw back faces */ 01/21/2003 15-462 Computer Graphics I 2

Choice of Programming Language OpenGL lives close to the hardware OpenGL is not object-oriented OpenGL is not functional Use C to expose and exploit low-level details Use C++, Java, O Caml,... for toolkits Support for C and C++ in assignments O Caml soon? 01/21/2003 15-462 Computer Graphics I 3

Client/Server Model Graphics hardware and caching Important for efficiency Need to be aware where data are stored Examples: vertex arrays, display lists 01/21/2003 15-462 Computer Graphics I 4

Display Lists Encapsulate a sequence of drawing commands Optimize and store on server GLuint listname = glgenlists(1); /* new 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(); gltranslatef(1.5, 0.0, 0.0); /* offset next object */ glendlist(); glcalllist(listname); /* draw one */ 01/21/2003 15-462 Computer Graphics I 5

Display Lists Details Useful for sequences of transformations Important for complex surfaces Another example: fonts Hierarchical display lists supported Display lists cannot be changed Display lists can be replaced Not necessary in first assignment 01/21/2003 15-462 Computer Graphics I 6

Vertex Arrays Draw cube with 6*4=24 or with 8 vertices? Expense in drawing and transformation Strips help to some extent Vertex arrays provide general solution Advanced (new in OpenGL 1.2) Define (transmit) array of vertices, colors, normals Draw using index into array(s) Vertex sharing for efficient operations Not needed for first assignment 01/21/2003 15-462 Computer Graphics I 7

Outline Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations Example 01/21/2003 15-462 Computer Graphics I 8

Main Event Loop Standard technique for interaction Main loop processes events Dispatch to functions specified by client Callbacks also common in operating systems Poor man s functional programming Mediates between client and window system 01/21/2003 15-462 Computer Graphics I 9

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 Menu (...): after selection from menu Mouse (int button, int state, int x, int y): mouse Motion (...): mouse movement Reshape (int w, int h): window resize Any callback can be NULL 01/21/2003 15-462 Computer Graphics I 10

Outline Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations Example 01/21/2003 15-462 Computer Graphics I 11

Screen Refresh Common: 60-100 Hz Flicker if drawing overlaps screen refresh Problem during animation Example (cube_single.c) Solution two frame buffers: Draw into one buffer Swap and display, while drawing into other buffer Desirable frame rate >= 30 fps (frames/second) 01/21/2003 15-462 Computer Graphics I 12

Enabling Modes One example of many glutinitdisplaymode (GLUT_SINGLE); glutinitdisplaymode (GLUT_DOUBLE); glutswapbuffers (); If something has no effect, check mode Example (cube.c) 01/21/2003 15-462 Computer Graphics I 13

Outline Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations Example 01/21/2003 15-462 Computer Graphics I 14

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: ray cast (z-buffer algorithm) Related: back-face culling 01/21/2003 15-462 Computer Graphics I 15

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? 01/21/2003 15-462 Computer Graphics I 16

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 01/21/2003 15-462 Computer Graphics I 17

Some Difficult Cases Sometimes cannot sort polygons! Cyclic overlap Piercing Polygons One solution: compute intersections Do while rasterizing (difficult in object space) 01/21/2003 15-462 Computer Graphics I 18

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 OpenGL (by default) PostScript interpreters 01/21/2003 15-462 Computer Graphics I 19

Image-Space Approach Raycasting: intersect ray with polygons O(k) worst case (often better) Images can be more jagged 01/21/2003 15-462 Computer Graphics I 20

The z-buffer Algorithm z-buffer with depth value z for each pixel Before writing a pixel into framebuffer Compute distance z of pixel origin from viewer If closer write and update z-buffer, otherwise discard 01/21/2003 15-462 Computer Graphics I 21

z-buffer Algorithm Assessment Strengths Simple (no sorting or splitting) Independent of geometric primitvies Weaknesses Memory intensive (but memory is cheap now) Tricky to handle transparency and blending Depth-ordering artifacts Usage OpenGL when enabled 01/21/2003 15-462 Computer Graphics I 22

Depth Buffer in OpenGL glutinitdisplaymode(glut_depth); glenable (GL_DEPTH_TEST); glclear (GL_DEPTH_BUFFER_BIT); Remember all of these! Some tricks use z-buffer read-only 01/21/2003 15-462 Computer Graphics I 23

Outline Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations Example 01/21/2003 15-462 Computer Graphics I 24

Specifying the Viewing Volume Clip everything not in viewing volume Separate matrices for transformation and projection glmatrixmode (GL_PROJECTION); glloadidentity();... Set viewing volume... glmatrixmode(gl_modelview); 01/21/2003 15-462 Computer Graphics I 25

Parallel Viewing Orthographic projection Camera points in negative z direction glortho(xmin, xmax, ymin, ymax, near, far) 01/21/2003 15-462 Computer Graphics I 26

Perspective Viewing Slightly more complex glfrustum(xmin, xmax, ymin, ymax, near, far) 01/21/2003 15-462 Computer Graphics I 27

Simple Transformations Rotate by given angle (in degrees) about ray from origin through (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); 01/21/2003 15-462 Computer Graphics I 28

Outline Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations Example 01/21/2003 15-462 Computer Graphics I 29

Example: Rotating Color Cube Adapted from [Angel, Ch. 4] Problem: Draw a color cube Rotate it about x, y, or z axis, depending on left, middle or right mouse click Stop when space bar is pressed Quit when q or Q is pressed 01/21/2003 15-462 Computer Graphics I 30

Step 1: Defining the Vertices Use parallel arrays for vertices and colors /* vertices of cube about the origin */ GLfloat vertices[8][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}, {-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 edges */ GLfloat colors[8][3] = {{0.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, {1.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 0.0, 1.0}, {1.0, 1.0, 1.0}, {0.0, 1.0, 1.0}}; 01/21/2003 15-462 Computer Graphics I 31

Step 2: Set Up Enable depth testing and double buffering int main(int argc, char **argv) { glutinit(&argc, argv); /* double buffering for smooth animation */ glutinitdisplaymode (GLUT_DOUBLE GLUT_DEPTH GLUT_RGB);... /* window creation and callbacks here */ glenable(gl_depth_test); glutmainloop(); return(0); } 01/21/2003 15-462 Computer Graphics I 32

Step 3: Install Callbacks Create window and set callbacks glutinitwindowsize(500, 500); glutcreatewindow("cube"); glutreshapefunc(myreshape); glutdisplayfunc(display); glutidlefunc(spincube); glutmousefunc(mouse); glutkeyboardfunc(keyboard); 01/21/2003 15-462 Computer Graphics I 33

Step 4: Reshape Callback Enclose cube, preserve aspect ratio void myreshape(int w, int h) { GLfloat aspect = (GLfloat) w / (GLfloat) h; glviewport(0, 0, w, h); glmatrixmode(gl_projection); glloadidentity(); if (w <= h) /* aspect <= 1 */ glortho(-2.0, 2.0, -2.0/aspect, 2.0/aspect, -10.0, 10.0); else /* aspect > 1 */ glortho(-2.0*aspect, 2.0*aspect, -2.0, 2.0, -10.0, 10.0); glmatrixmode(gl_modelview); } 01/21/2003 15-462 Computer Graphics I 34

Step 5: Display Callback Clear, rotate, draw, flush, swap GLfloat theta[3] = {0.0, 0.0, 0.0}; void display(void) { glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glloadidentity(); glrotatef(theta[0], 1.0, 0.0, 0.0); glrotatef(theta[1], 0.0, 1.0, 0.0); glrotatef(theta[2], 0.0, 0.0, 1.0); colorcube(); glflush(); glutswapbuffers(); } 01/21/2003 15-462 Computer Graphics I 35

Step 6: Drawing Faces Call face(a, b, c, d) with vertex index Orient consistently void colorcube(void) { face(0,3,2,1); face(2,3,7,6); face(0,4,7,3); face(1,2,6,5); face(4,5,6,7); face(0,1,5,4); } 01/21/2003 15-462 Computer Graphics I 36

Step 7: Drawing a Face Use vector form of primitives and attributes void face(int a, int b, int c, int d) {glbegin(gl_polygon); glcolor3fv(colors[a]); glvertex3fv(vertices[a]); glcolor3fv(colors[b]); glvertex3fv(vertices[b]); glcolor3fv(colors[c]); glvertex3fv(vertices[c]); glcolor3fv(colors[d]); glvertex3fv(vertices[d]); glend(); } 01/21/2003 15-462 Computer Graphics I 37

Step 8: Animation Set idle callback GLfloat delta = 2.0; GLint axis = 2; void spincube() { /* spin cube delta degrees about selected axis */ theta[axis] += delta; if (theta[axis] > 360.0) theta[axis] -= 360.0; } /* display result */ glutpostredisplay(); 01/21/2003 15-462 Computer Graphics I 38

Step 9: 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; } 01/21/2003 15-462 Computer Graphics I 39

Step 10: 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(spincube); } 01/21/2003 15-462 Computer Graphics I 40

Summary Client/Server Model Callbacks Double Buffering Hidden Surface Removal Simple Transformations Example 01/21/2003 15-462 Computer Graphics I 41

Announcements Please verify access to graphics lab and login Follow account setup instruction on web page! Check web page for C++ instructions First guest lecture on Feb 20 on programmable pixel shaders (Cass Everitt, Nvidia) Assignment 1 movie from Fall 02 01/21/2003 15-462 Computer Graphics I 42