Computer graphic -- Programming with OpenGL I

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

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

Lecture 4 of 41. Lab 1a: OpenGL Basics

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

CSC 8470 Computer Graphics. What is Computer Graphics?

Introduction to Computer Graphics with OpenGL/GLUT

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

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

Programming using OpenGL: A first Introduction

Computer Graphics. OpenGL

RECITATION - 1. Ceng477 Fall

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

Geometry Primitives. Computer Science Department University of Malta. Sandro Spina Computer Graphics and Simulation Group. CGSG Geometry Primitives

Rendering. Part 1 An introduction to OpenGL

2D Drawing Primitives

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

Programming of Graphics

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

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

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

Graphics Programming

Precept 2 Aleksey Boyko February 18, 2011

How shapes are represented in 3D Graphics. Aims and objectives By the end of the lecture you will be able to describe

OpenGL. Toolkits.

Assignment 1. Simple Graphics program using OpenGL

CS 4204 Computer Graphics

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

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

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

Exercise 1 Introduction to OpenGL

Lecture 3 Advanced Computer Graphics (CS & SE )

COMPUTER GRAPHICS LAB # 3

Computer Graphics 1 Computer Graphics 1

void drawdot(glint x, GLint y) { glbegin(gl_points); glvertex2i(x,y); glend();

Computer Graphics (Basic OpenGL)

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

Programming with OpenGL Part 1: Background

Computer Graphics. Bing-Yu Chen National Taiwan University

Introduction to OpenGL Week 1

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

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

Draw the basic Geometry Objects. Hanyang University

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

Computer graphics MN1

OPENGL AND GLSL. Computer Graphics

OpenGL refresher. Advanced Computer Graphics 2012

OpenGL Primitives. Examples: Lines. Points. Polylines. void drawdot(glint x, GLint y) { glbegin(gl_points); glvertex2i(x,y); glend(); }

Basic Graphics Programming

Drawing Primitives. OpenGL basics

Graphics Hardware and OpenGL

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

Basic Graphics Programming

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

CS130 : Computer Graphics Lecture 2: Graphics Pipeline. Tamar Shinar Computer Science & Engineering UC Riverside

The Rendering Pipeline (1)

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

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

Introduction to OpenGL

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

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

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

Graphics Pipeline & APIs

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

CS130 : Computer Graphics. Tamar Shinar Computer Science & Engineering UC Riverside

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

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

COMP 371/4 Computer Graphics Week 1

Building Models. Prof. George Wolberg Dept. of Computer Science City College of New York

Sign up for crits! Announcments

Luiz Fernando Martha André Pereira

6.837 Introduction to Computer Graphics Assignment 5: OpenGL and Solid Textures Due Wednesday October 22, 2003 at 11:59pm

CS 591B Lecture 9: The OpenGL Rendering Pipeline

Computer Graphics. Anders Hast. måndag 25 mars 13

Graphics Pipeline & APIs

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

Computer Graphics and Visualization. Graphics Systems and Models

OpenGL Introduction Computer Graphics and Visualization

E.Order of Operations

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Computer Graphics. Chapter 3 Computer Graphics Software

Filled Area Primitives. CEng 477 Introduction to Computer Graphics METU, 2007

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

Computer Graphics, Chapt 08

CSE528 Computer Graphics: Theory, Algorithms, and Applications

Models and Architectures

Lectures OpenGL Introduction

Lecture 2 2D transformations Introduction to OpenGL

Programming with OpenGL Part 1: Background

3D Graphics and OpenGl. First Steps

Surface Graphics. 200 polys 1,000 polys 15,000 polys. an empty foot. - a mesh of spline patches:

Three Main Themes of Computer Graphics

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

Scan Conversion of Polygons. Dr. Scott Schaefer

CS 381 Computer Graphics, Fall 2008 Midterm Exam Solutions. The Midterm Exam was given in class on Thursday, October 23, 2008.

Clipping & Culling. Lecture 11 Spring Trivial Rejection Outcode Clipping Plane-at-a-time Clipping Backface Culling

LECTURE 02 OPENGL API

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

CSCI 4620/8626. Coordinate Reference Frames

Transcription:

Computer graphic -- Programming with OpenGL I

A simple example using OpenGL Download the example code "basic shapes", and compile and run it Take a look at it, and hit ESC when you're done. It shows the following image: 2

Overview of How the Program Works How does the program work? The basic idea is that we tell OpenGL the 3D coordinates of all of the vertices of our shapes. OpenGL uses the standard x and y axes, with the positive x direction pointing toward the right and the positive y direction pointing upward. In 3D we need another dimension, the z dimension. The positive z direction points out of the screen. 3

Overview of How the Program Works How does OpenGL use these 3D coordinates? It simulates the way that our eyes work. Take a look at the following picture. 4

Overview of How the Program Works OpenGL converts all of the 3D points to pixel coordinates before it draws anything. To do this, it draws a line from each point in the scene to your eye and takes the intersection of the lines and the screen rectangle Draw a triangle: it converts the three vertices into pixel coordinates and draws a "2D" triangle using those coordinates. 5

Overview of How the Program Works The user's "eye" is always at the origin and looking in the negative z direction. OpenGL doesn't draw anything that is behind the "eye". (After all, it isn't the all-seeing eye of Sauron.) z direction The eye of Sauron, The Lord of the Rings 6

Overview of How the Program Works How far away is the screen rectangle from your eye? It doesn't matter. No matter how far away the screen rectangle is, a given 3D point will map to the same pixel coordinates. All that matters is angle that your eye can see. Perspective projection: all projectors meet at the center of projection (COP) P P 7

Going Through the Source Code All of this stuff about pixel coordinates is great and all, but as programmers, we want to see some codes. Take a look at main.cpp. the code is free. 8

Going Through the Source Code Take a look at main.cpp. The second thing you'll notice is that it's heavily commented. Let's go through the file. 9

Going Through the Source Code First, we include our header files. Pretty standard stuff for C++. If we're using a Mac, we want our program to include GLUT/glut.h and OpenGL/OpenGL.h; Linux/Unix/Windows, include GL/glut.h. 10

Going Through the Source Code We'll have this line near the top of main.cpp in all of our programs. By this line, we don't have to type std:: a lot; for example, so we can use cout instead of std::cout. 11

Going Through the Source Code This function handles any keys pressed by the user. For now, all that it does is quit the program when the user presses ESC, by calling exit. The function is passed the x and y coordinates of the mouse, but we don't need them. 12

Going Through the Source Code The initrendering function initializes our rendering parameters. For now, it doesn't do much. We'll pretty much always want to call glenable(gl_depth_test) when we initialize rendering. The call makes sure that an object shows up behind an object in front of it that has already been drawn, which we want to happen. Note that glenable, like every OpenGL function, begins with "gl". 13

Going Through the Source Code The handleresize function is called whenever the window is resized. w and h are the new width and height of the window. There are a couple of things to notice. When we pass 45.0 to gluperspective, we're telling OpenGL the angle that the user's eye can see. The1.0 indicates not to draw anything with a z coordinate of greater than -1. This is so that when something is right next to our eye, it doesn't fill up the whole screen. The 200.0 tells OpenGL not to draw anything with a z coordinate less than -200. We don't care very much about stuff that's really far away. 14

Going Through the Source Code The handleresize function is called whenever the window is resized. w and h are the new width and height of the window. The content of handleresize will be not change much in our other projects, so you don't have to worry about it too much. There are a couple of things to notice. When we pass 45.0 to gluperspective, we're telling OpenGL the angle that the user's eye can see. The1.0 indicates not to draw anything with a z coordinate of greater than -1. This is so that when something is right next to our eye, it doesn't fill up the whole screen. The 200.0 tells OpenGL not to draw anything with a z coordinate less than -200. We don't care very much about stuff that's really far away. (-1, -200) 15

Going Through the Source Code gluperspective gl: OpenGL library glu : a GLU (GL Utility) function. glut: GL Utility Toolkit. Don't worry about the difference among OpenGL, GLU, and GLUT. 16

Going Through the Source Code drawscene for 3D drawing. glclear to clear information from the last time we drew. In most OpenGL program, you'll want to do this. 17

Going Through the Source Code transformations For now, ignore this 18

Going Through the Source Code begin the substance of our program. This part draws the trapezoid. glbegin(gl_quads) to tell OpenGL to start drawing quadrilaterals. glvertex3f, specify the four 3D coordinates of the vertices. "3 means three coordinates "f" means in float format All of the "f"'s after the vertex coordinates force the compiler to treat the numbers as floats. glend(): stop after drawing quadrilaterals Every call to glbegin must have a matching call to glend. 19

Going Through the Source Code begin the substance of our program. 20

Going Through the Source Code glbegin/glend calls are nowadays deprecated after OpenGL 3.0 standard. They will probably keep working in the foreseeable future. The OpenGL 3.0 specification marks several features as deprecated, including the venerable glbegin/glend mechanism, display lists, matrix and attribute stacks, and the portion of the fixed function pipe subsumed by shaders (lighting, fog, texture mapping, and texture coordinate generation). http://www.opengl.org/registry/ 21

Going Through the Source Code Draw the pentagon. split it up into three triangles, which is pretty standard for OpenGL. glbegin(gl_triangles) to tell OpenGL to draw triangles. tell it the coordinates of the vertices of the triangles. OpenGL automatically puts the coordinates together in groups of three. Each group of three coordinates represents one triangle. 1 3 2 22

Going Through the Source Code draw the triangle. We haven't called glend() to tell OpenGL that we're done drawing triangles yet, so it knows that we're still giving it triangle coordinates. 1 3 2 23

Going Through the Source Code glend(): finish drawing triangles To draw the above four triangles using four calls to glbegin(gl_triangles) and four glend(). However, this makes the program slower, and you shouldn't do it. we can pass other geometry shape to glbegin besides GL_TRIANGLES and GL_QUADS, but triangles and quadrilaterals are the most common 1 3 2 24

Going Through the Source Code This line makes OpenGL actually move the scene to the window. We'll call it whenever we're done drawing a scene. 25

Going Through the Source Code main function. We start by initializing GLUT. It will appear in all of our programs, don't have to worry too much about it. glutinitwindowsize, set the window to be 400x400. glutcreatewindow, tell it what title for the window. initrendering, initialize OpenGL rendering. 26

Going Through the Source Code point GLUT to the functions that we wrote to handle keypresses and drawing and resizing the window. One important thing to note: we're not allowed to draw anything except inside the drawscene function that we explicitly give to GLUT, or inside functions that drawscene calls (or functions that they call, etc.). 27

Going Through the Source Code glutmainloop, tells GLUT to do its thing. tell GLUT to capture key and mouse input, to draw the scene when it has to by calling our drawscene function. glutmainloop, like a defective boomerang, never returns. GLUT just takes care of the rest of our program's execution. return 0 so that the compiler doesn't complain about the main function not returning anything, but the program will never get to that line. that's how our first OpenGL program works. 28

OpenGL function format function name dimensions glvertex3f(x,y,z,w) belongs to GL library x,y,z,w are floats glvertex3fv(p) p is a pointer to an array 29

OpenGL function format glvertex3f(x,y,z,w) x,y and z are coordinates and w is a factor, so the real coordinates is (x/w, y/w, z/w). The default values of z and w are z =0 and w=1, respectively. For examples: glvertex3f(1, 2, -3, 3) -> glvertex3f(1/3, 2/3, -1, 1) glvertex3f(1, 2, -3) -> glvertex3f(1, 2, -3, 1) glvertex3f(1, 2) -> glvertex3f(1, 2, 0, 1) 30

OpenGL Primitives glvertex3f(x,y,z) 31

OpenGL Primitives OpenGL automatically puts the coordinates one by one. GL_POINTS GL_LINES OpenGL automatically puts the coordinates together in groups of two. GL_LINE_STRIP GL_LINE_LOOP 32

OpenGL Primitives GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_QUAD_STRIP GL_POLYGON 33

Polygon Issues OpenGL will only display polygons correctly that are Simple: edges cannot cross Convex: All points on line segment between two points in a polygon are also in the polygon Flat: all vertices are in the same plane User program can check if above true OpenGL will produce output if these conditions are violated but it may not be what is desired Triangles satisfy all conditions p 1 p 2 nonsimple polygon nonconvex polygon 34

Polygon Issues How can we plot those polygon which does not satisfy these conditions? nonsimple polygon : edges DO cross nonconvex polygon : There are points on line segment between two points in a polygon are NOT in the polygon Flat: all vertices are NOT in the same plane Solution: divide them using Triangles because triangles satisfy all conditions 1 3 2 nonsimple polygon nonconvex polygon 35

Polygon Issues Subdividing: improve a polygonal approximation to a surface using approximating triangles 20 triangles 80 triangles 320 triangles 36

Polygon Issues Do something huge! Demo 37

Polygon Issues Do something huge! 38

Hints for polygonizing surfaces Keep polygon orientations consistent all clockwise or all counterclockwise important for polygon culling and two-sided lighting 39

Hints for polygonizing surfaces Watch out for any nontriangular polygons three vertices of a triangle are always on a plane; any polygon with four or more vertices might not 40

Hints for polygonizing surfaces There's a trade-off between the display speed and the image quality few polygons render quickly but might have a jagged appearance; millions of tiny polygons probably look good but might take a long time to render use large polygons where the surface is relatively flat, and small polygons in regions of high curvature 41

Hints for polygonizing surfaces Try to avoid T-intersections in your models there's no guarantee that the line segments AB and BC lie on exactly the same pixels as the segment AC this can cause cracks to appear in the surface 42

Some terms Rendering: the process by which a computer creates images from models. models, or objects: constructed from geometric primitives - points, lines, and polygons - that are specified by their vertices. 43

Some terms pixel: the smallest visible element the display hardware can put on the screen. The final rendered image consists of pixels drawn on the screen. Bitplane: an area of memory that holds one bit of information (for instance, what color it is supposed to be) for every pixel on the screen. framebuffer : Organized by the bitplanes. It holds all the information that the graphics display needs to control the color and intensity of all the pixels on the screen. 44

24-bit true color 8 0 1 0 0 1 0 1 1 Bitplane registers 0 1 0 0 1 0 1 1 8 bit DAC Color Guns Blue 75 pixel 8 1 0 1 0 1 1 0 0 Green 172 8 bit DAC 8 DAC: digital-to-analog converter 0 0 0 0 1 0 1 0 8 bit DAC Red 10 CRT Raster Frame Buffer 45

The end of this lecture! 46