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

Similar documents
SE313: Computer Graphics and Visual Programming. Computer Graphics Notes Gazihan Alankus, Fall 2011

Graphics Programming

Precept 2 Aleksey Boyko February 18, 2011

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

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Lecture 3. Understanding of OPenGL programming

Programming using OpenGL: A first Introduction

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

Lectures OpenGL Introduction

OpenGL Introduction Computer Graphics and Visualization

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

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

Drawing Primitives. OpenGL basics

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

Announcements OpenGL. Computer Graphics. Spring CS4815

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

Computer Graphics. Chapter 10 Three-Dimensional Viewing

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

Computer Graphics (Basic OpenGL)

Notes on Assignment. Notes on Assignment. Notes on Assignment. Notes on Assignment

Introduction to Computer Graphics with OpenGL/GLUT

Using OpenGL with CUDA

COMPUTER GRAPHICS LAB # 3

GLUT Tutorial. Keyboard

Lecture 4 of 41. Lab 1a: OpenGL Basics

Getting Started. Overview (1): Getting Started (1): Getting Started (2): Getting Started (3): COSC 4431/5331 Computer Graphics.

Transformation Pipeline

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Computer Graphics Course 2005

C OMPUTER G RAPHICS Thursday

Drawing and Coordinate Systems

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

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

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

Graphics Hardware and OpenGL

Drawing and Coordinate Systems

Programming with OpenGL Part 3: Three Dimensions

1 (Practice 1) Introduction to OpenGL

Lecture 4. Viewing, Projection and Viewport Transformations

Draw the basic Geometry Objects. Hanyang University

Assignment 1. Simple Graphics program using OpenGL

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

Basic Graphics Programming

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

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

OpenGL refresher. Advanced Computer Graphics 2012

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

Computer Graphics. Bing-Yu Chen National Taiwan University

Bob s Concise Introduction to Doxygen

Programming with OpenGL Part 1: Background

The View Frustum. Lecture 9 Sections 2.6, 5.1, 5.2. Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2011

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

OpenGL Transformations

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

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

API for creating a display window and using keyboard/mouse interations. See RayWindow.cpp to see how these are used for Assignment3

VTU EDUSAT PROGRAMME. Computer Graphics & Visualization

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

Introduction to OpenGL Week 1

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

Introduction to OpenGL: Part 2

OpenGL: Open Graphics Library. Introduction to OpenGL Part II. How do I render a geometric primitive? What is OpenGL

Programming with OpenGL Part 1: Background

Fundamental Types of Viewing

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

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

6. Make use of glviewport() to display two sine curves on the same screen, one on the

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

Overview. By end of the week:

MORE OPENGL. Pramook Khungurn CS 4621, Fall 2011

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

Announcements. Submitting Programs Upload source and executable(s) (Windows or Mac) to digital dropbox on Blackboard

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

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

Computer Graphics. OpenGL

CSE 167: Lecture #4: Vertex Transformation. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

COMP 371/4 Computer Graphics Week 1

Interaction. CSCI 480 Computer Graphics Lecture 3

Computer graphic -- Programming with OpenGL I

Interaction Computer Graphics I Lecture 3

Graphics Programming. 1. The Sierpinski Gasket. Chapter 2. Introduction:

OpenGL Tutorial. Ceng 477 Introduction to Computer Graphics

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

CS 4204 Computer Graphics

Geometry: Outline. Projections. Orthographic Perspective

Lecture 9 Sections 2.6, 5.1, 5.2. Wed, Sep 16, 2009

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

Lecture 3 Sections 2.2, 4.4. Mon, Aug 31, 2009

Computer Graphics: Line Drawing Algorithms

Lecture 2 2D transformations Introduction to OpenGL

Computer graphics MN1

COMP3421 Computer Graphics

Basic Graphics Programming

CSE 167: Introduction to Computer Graphics Lecture #4: Vertex Transformation

Rasterization Overview

CSCI 4620/8626. The 2D Viewing Pipeline

Exercise 1 Introduction to OpenGL

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

RECITATION - 1. Ceng477 Fall

Transcription:

SE313: Computer Graphics and Visual Programming Computer Graphics Notes Gazihan Alankus, Spring 2012 Computer Graphics 3D computer graphics: geometric modeling of objects in the computer and rendering them Geometrical model on the computer -> Image on the screen Define the model in Cartesian coordinates (x, y, z) You could do the calculations yourself, but graphics packages like OpenGL does this for us. 1

How it works in your computer In the past GPU was a separate computer accessed over the network. Today it s still like a separate computer, but in your graphics card. Using OpenGL commands you tell it how things are and how it should transform things, it does it for you and draws it. OpenGL only draws, does not deal with creating windows or getting input. They are handled using windowing toolkits like Qt, wxwindows, etc. We will use glut in C/C++ because it s simple. In Java we use JOGL. Let s look at some code: http://www.cse.wustl.edu/~gazihan/teaching/12spring/se313/material/week1/ Then we will make sure you can run it in your laptops. Note: We learned about glut and OpenGL primitives using slides from week 3. We also learned about different coordinate frames (modeling, world, etc.) Here is an incomplete summary: 2

Different coordinate frames Here is the center of the world Nasreddin Hoca You can take your reference point to be anywhere you want, as long as you are consistent. We ll see that different things have different reference frames. Modeling coordinates An artist creates a model and gives it to you. It inherently has a coordinate frame. Let s say the artist creates a house and a tree in two different files. Both files have an origin and coordinate frames. These coordinates of the house and the tree are called modeling coordinates. World coordinates The art assets by themselves are not very useful and we want to create a scene out of them. Therefore, we want to bring them in to a new coordinate frame. This is called world coordinates or scene 3

coordinates. Above is an empty world with a coordinate frame. Now we want to place our house and the tree in the world. If we just put the geometric models in the world, here is what it would look like: 4

This is not what we want. We want the house to be lower and the tree on the right. We add and subtract to the house s and the tree s coordinates to get them to where we want them: Even though we modified locations of the vertices of the tree and the house, we brought them to a common coordinate frame, which is the world coordinate frame. We can still keep track of the previous coordinate frames of the house and the tree in this new coordinates: These are called local coordinate frames. Everything in the scene has a local coordinate frame, and everything is transformed to find their places in the world coordinate frame. Viewing coordinates Bringing objects to their places in the world is useful, but we need a camera to create an image of the scene. Our camera can be located in the scene similar to the other objects. The camera also has a coordinate frame. There is a box that the camera can see. 5

This is the viewing volume in the camera s coordinates. Everything in this volume gets orthogonally projected into 2D. Imagine pushing on the box along the z direction to make it flat, that s exactly how it turns into 2D. When you look at an OpenGL window, this viewing volume is what you are looking at. Therefore, to see objects in your OpenGL window, you have to bring your world into this viewing volume. You do that by transforming your world to the viewing volume. The final coordinates are called viewing coordinates. OpenGL OpenGL is a state machine. You give it commands to change its state, and you tell it to draw things using the current state. Introduction You can draw primitive geometric shapes in OpenGL by giving vertices (points) one by one to OpenGL. Ideally an artist would create it in an authoring software (like the tree and the house above). But we will do it by hand for now. Example: glbegin(gl_polygon); glend(); glvertex2f(-0.5, -0.5); glvertex2f(-0.5, 0.5); glvertex2f(0.5, 0.5); glvertex2f(0.5, -0.5); 6

This tells OpenGL to start drawing a polygon, gives it four 2D points (z is assumed 0 then) and ends the polygon. If we do not do anything special, this gets drawn in the viewing coordinates (see figure above). Since the corners of the viewing volume are 1, this set of points will draw a square that takes ¼ of the screen and will be in the middle. This is the output that confirms what we said. You need glut, or some other windowing tool to create the window for you and take care of other things as well. Below is a complete program: #include <GL/glut.h> //you may have to remove GL/ in windows void mydisplay(){ glclear(gl_color_buffer_bit); glbegin(gl_polygon); glend(); glflush(); glvertex2f(-0.5, -0.5); glvertex2f(-0.5, 0.5); glvertex2f(0.5, 0.5); glvertex2f(0.5, -0.5); 7

} int main(int argc, char** argv){ } glutcreatewindow("simple"); glutdisplayfunc(mydisplay); glutmainloop(); Primitives In addition to polygons, we can also draw the following primitives using vertices: 8

Attributes of Primitives OpenGL is stateful. You can set the state of OpenGL to determine how it will draw things. Some basic attributes are: Color glcolor3f(1.0, 0.0, 0.0) Point size glpointsize(2.0) 9

Line width gllinewidth(2.0) Line pattern glenable(gl_line_stipple) gllinestipple(1, 0x1C47) gldisable(gl_line_stipple) Color blending glenable(gl_blend); gldisable(gl_blend); Polygon modes glpolygonmode() Play with them to see how they change things. Google them and read man pages. Glut Glut opens the window for you and runs some functions that you give to it. These are called callback functions because glut calls them back when it needs to. glutdisplayfunc(renderscene); glutreshapefunc(changesize); glutidlefunc(renderscene); glutkeyboardfunc(processnormalkeys); glutspecialfunc(processspecialkeys); You create the functions that we gave as arguments (renderscene, changesize, etc.). Glut calls them when necessary. Glut takes care of keyboard input, too. There is a great tutorial here Some important conventions to keep in mind about Glut: Never call the callback functions (renderscene etc.) yourself. Always let Glut call them. Only set the callback functions in the main function. For example, do not call glutdisplayfunc(someotherrenderscene); to change what s being rendered. Instead, use variables to keep state, and let the renderscene function act differently with respect to those variables. Animation with glut Here is why we observe animation: 10

Glut calls your renderscene function many times a second o This is because you set up a timer with gluttimerfunc(10, updatescene, 0); and in it, force glut to repaint the screen using glutpostredisplay() Your renderscene function acts differently in time o This is because you change the program state using variables You need to have some variables that define the current state of the animation, make the renderscene function use those variables to draw the scene, and change those variables in updatescene so that renderscene draws something different the next time it is called. When you animate and see flickering on screen, it may be because you did not open your window in double-buffered mode: glutinitdisplaymode(glut_depth GLUT_DOUBLE GLUT_RGBA); With double buffering, the frame you see is not the one that OpenGL is currently drawing. This way, we never see incomplete frames. Drawing in three dimensions Up till now we drew OpenGL primitives in two dimensions and we used calls like glvertex2d(0.5, 1.0); to specify points using their x and y coordinates. In three dimensions, we also have the z coordinate, and specify vertices this way: glvertex3d(0.5, 1.0, 0.2); In fact, when you specify a two dimensional vertex, it is equivalent to specifying one with the z coordinate equal to zero. Therefore, the following are equivalent: glvertex2d(0.5, 1.0); glvertex3d(0.5, 1.0, 0.0); We can use any 3D point to specify the vertices for our OpenGL primitives. By locating the camera in an arbitrary location in the space (we will learn later) we can take a virtual picture of the scene. Orthographic vs. Perspective Projection Taking virtual pictures is achieved using projection of 3D points to a 2D plane, as we talked about in class before. There are two methods: orthogonal projection and perspective projection. 11

[Images are borrowed from http://db-in.com] By default the projection is orthographic. In orthographic projection when things move away they do not get smaller. In perspective projection, they become smaller like it is in the real world. Both have their uses. In orthographic projection, we simply move and scale the world to fit them in our viewing half cube, and project it to 2D. 12

In perspective projection, we warp the space so that a cut pyramid (frustum) in the world is mapped to our viewing halfcube. When it is projected into 2D, we get our image. The above image summarizes this warping process. [Image borrowed from http://www.terathon.com/gdc07_lengyel.pdf] We can set the parameters of the orthographic projection using glortho(left, right, bottom, top, near, far), or if we don t care about the z axis, gluortho2d(left, right, bottom, top). This is equivalent to calling glortho with -1 and 1 for near and far values. OpenGL uses left, right, bottom, top, near, far in world coordinates to define a rectangular volume to be visible. The camera is directed towards the negative z axis. We can set a perspective projection using glfrustum(left, right, bottom, top, near, far) or gluperspective(fovy angle, aspect ratio, neardistance, fardistance). In both of them, the eye is located at the origin, there is a near plane and a small rectangle on it that maps to the screen, and a far plane with a larger rectangle. These three are on a pyramid. The following image helps explain this better: 13

gluperspective is easier to use and should be preferred. gluperspective(fovy angle, aspect ratio, neardistance, fardistance). Fovy angle is the angle between the top and the bottom of the pyramid. Aspect ratio is the width/height of the rectangle. Neardistance is the distance of the near plane to the eye, where we start seeing things. Far distance is the distance to the far plane that we don t see beyond. After this, we are still looking towards the z axis. Therefore, if we start with an identity matrix in our projection matrix, and call gluperspective(90, 1.0, 0.1, 100.0) 14

Then what we have at the z=-1 plane stays the same, while everything else scales: However, it is better to set a fov angle that matches the approximate distance of your eye to the screen and the size of the screen. 40 can be a good approximation. Setting up projection Projection is implemented something called the perspective matrix. We will learn about the different types of matrices in OpenGL later. You usually set the projection matrix once and don t touch it afterwards: void init() { } or glmatrixmode(gl_projection); glloadidentity(); gluperspective(40, 1.0, 0.1, 100.0); void init() { glmatrixmode(gl_projection); glloadidentity(); gluortho2d(-100.0, 100.0, -100.0, 100.0); 15

} We will see how to move and orient the camera later. 16