Precept 2 Aleksey Boyko February 18, 2011

Similar documents
Lecture 4 of 41. Lab 1a: OpenGL Basics

Computer graphics MN1

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

Programming using OpenGL: A first Introduction

Introduction to Computer Graphics with OpenGL/GLUT

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

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

Basic Graphics Programming

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

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

CS 4204 Computer Graphics

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

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

Transformation, Input and Interaction. Hanyang University

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

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

C OMPUTER G RAPHICS Thursday

Computer Graphics. OpenGL

Basic Graphics Programming

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

Lectures OpenGL Introduction

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

Outline. Other Graphics Technology. OpenGL Background and History. Platform Specifics. The Drawing Process

Graphics and Computation Introduction to OpenGL

OpenGL/GLUT Intro. Week 1, Fri Jan 12

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

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

OpenGL. Toolkits.

Graphics Programming

Programming with OpenGL Part 1: Background

Computer graphics MN1

Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

Order of Transformations

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

1 (Practice 1) Introduction to OpenGL

OpenGL Tutorial. Ceng 477 Introduction to Computer Graphics

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

Computer graphics MN1

An Interactive Introduction to OpenGL Programming

Lecture 3. Understanding of OPenGL programming

Graphics Pipeline & APIs


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

Visualizing Molecular Dynamics

OpenGL refresher. Advanced Computer Graphics 2012

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

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

RECITATION - 1. Ceng477 Fall

Drawing Primitives. OpenGL basics

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

Graphics Pipeline & APIs

Assignment 1. Simple Graphics program using OpenGL

Computer Graphics, Chapt 08

Using OpenGL with CUDA

CIS 636 Interactive Computer Graphics CIS 736 Computer Graphics Spring 2011

Advanced Computer Graphics (CS & SE )

Introduction to OpenGL Week 1

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

COMPUTER GRAPHICS LAB # 3

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

Computer Graphics 1 Computer Graphics 1

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

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Computer Graphics Introduction to OpenGL

Computer Graphics. Bing-Yu Chen National Taiwan University

GLUT Tutorial. Keyboard

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

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

Spring 2013, CS 112 Programming Assignment 2 Submission Due: April 26, 2013

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

COMP 371/4 Computer Graphics Week 1

Announcements OpenGL. Computer Graphics. Spring CS4815

Computer Graphics (Basic OpenGL)

Programming with OpenGL Part 3: Three Dimensions

CSE4030 Introduction to Computer Graphics

Scientific Visualization Basics

Computer Graphics Primitive Attributes

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

Interaction Computer Graphics I Lecture 3

An Interactive Introduction to OpenGL and OpenGL ES Programming. Ed Angel Dave Shreiner

Rendering Pipeline/ OpenGL

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

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

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

Computer Graphics. Chapter 10 Three-Dimensional Viewing

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

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

Transformation Pipeline

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

Geometry: Outline. Projections. Orthographic Perspective

VTU EDUSAT PROGRAMME. Computer Graphics & Visualization

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

Introduction to OpenGL

Computer Graphics. Transformations. CSC 470 Computer Graphics 1

Image Rendering. Rendering can be divided into three sub-problems. Image Formation The hidden surface problem visibility determination steps

OpenGL Introduction Computer Graphics and Visualization

Chapter 13 Selection and Feedback

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

Transcription:

Precept 2 Aleksey Boyko February 18, 2011

Getting started Initialization Drawing Transformations Cameras Animation Input Keyboard Mouse Joystick? Textures Lights Programmable pipeline elements (shaders)

Open Graphics Library Industry standard Open source Cross-platform API 2D/3D graphics OpenGL ES for mobile devices OpenGL 1.x fixed rendering pipeline OpenGL 2+ Partially programmable pipeline via GLSL (OpenGL Shading Language) OpenGL 4.1 as of July 2010 5 programmable stages of pipeline

OpenGL Utility Toolkit What Cross-platform system-level IO Window definition Window control Monitoring keyboard/mouse input Drawing some primitives Sphere Cube Utah teapot

Why? OpenGL relies on application to provide platform dependent draw context GLUT provides means to make application code rather cross-platform Makes learning easier and starting faster

OpenGL comes installed with most modern Operating systems Get glut glut.h to build ->SDKs/include glut32.lib to link -> SDKs/libs glut32.dll to run -> Windows/system32 Other OS and IDE: Install as described in COMPILER.txt in assignment 1 OR http://www.videotutorialsrock.com/opengl_tutorial/ get_opengl_setup/home.php OR start with your assignment 2 skeleton code and add/remove as necessary

#if defined(_win32) defined( CYGWIN ) # include <windows.h> # include <GL/glut.h> #elif defined( APPLE ) # include <GLUT/glut.h> #else # include <GL/glut.h> #endif Tip: can be found in assignment 1 skeleton code file cos426_opengl.h

void main(int argc, char **argv) { glutinit(&argc, argv); glutinitdisplaymode(glut_depth GLUT_DOUBLE GLUT_RGB); glutinitwindowposition(100,100); glutinitwindowsize(320,320); glutcreatewindow("glut tutorial"); glenable(gl_depth_test); glutdisplayfunc(renderscene); } glutmainloop();

Registers a redraw callback function with glut In main: glutdisplayfunc(renderscene); We implement renderscene() before the main(): void renderscene(void) { glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glbegin(gl_polygon); glcolor3f(1, 0, 0); glvertex3f(-0.5,-0.5,0.0); glcolor3f(0, 1, 0); glvertex3f(0.5,0.0,0.0); glcolor3f(0, 0, 1); glvertex3f(0.0,0.5,0.0); glend(); glflush(); }

GL_COLOR_BUFFER_BIT Indicates the color buffer. Color can be set with glclearcolor() GL_DEPTH_BUFFER_BIT Indicates the depth buffer. Value can be set with glcleardepth() GL_STENCIL_BUFFER_BIT Indicates the stencil buffer Value can be set with glclearstencil() More here http://www.khronos.org/opengles/documentation /opengles1_0/html/glclear.html

GL_POLYGON Draw filled convex polygon. GL_TRIANGLES Pass 3n vertices, draws n triangles. GL_QUADS Pass 4n vertices, draws n quads. GL_LINES Pass 2n vertices, draws n line segments. GL_LINE_LOOP Draws outline of polygon. GL_LINE_STRIP Draw segments (v1,v2), (v2,v3),, (vn-1, vn). GL_POINTS Pass n vertices, draws n points. For more, see glbegin man page, e.g. here http://www.opengl.org/sdk/docs/man/xhtml/glbegin.xml

glbegin(gl_polygon); glcolor3f(1, 0, 0); glvertex3f(-0.5,-0.5,0.0); glcolor3f(0, 1, 0); glvertex3f(0.5,0.0,0.0); glcolor3f(0, 0, 1); glvertex3f(0.0,0.5,0.0); glend(); gllinewidth(3); //optional line width, //default is 1 glenable(gl_line_smooth); //enable antialiasing glbegin(gl_line_loop); glcolor3f(1, 0, 0); glvertex3f(-0.5,-0.5,0.0); glcolor3f(0, 1, 0); glvertex3f(0.5,0.0,0.0); glcolor3f(0, 0, 1); glvertex3f(0.0,0.5,0.0); glend();

Camera GL_PROJECTION matrix Update the view perspective when resizing the window when user or event changes the camera position Scene GL_MODELVIEW matrix Hierarchical scene representation Animation - Update the scene with time upon event upon user input

Vertex is transformed by GL_MODELVIEW matrix, then GL_PROJECTION matrix. After applying both matrices, vertex is in clip coordinates Divide by w component of vector to get normalized device coordinates (NDC). NDC: x, y, z values from -1 to 1. Initially, both matrices are identity.

There is a stack of matrices for each matrix mode Current matrix is the matrix on top of the stack for each mode Initially stack has 1 element an identity matrix glpushmatrix() pushes a copy of current matrix on top of the stack glpopmatrix() pops the stack, replacing the top matrix with the one below it WHY? Convenient for hierarchical coordinate systems, e.g. articulated bodies Scene graphs

glmatrixmode() switch active matrix stack glloadidentity() replaces current matrix with an identity matrix glloadmatrix( float M[16]) replace current matrix with M (column major) glmultmatrix(float M[16]) Multiply current matrix with M Tip: if T is the current matrix and M is matrix we re multiplying by, the result is TM

glrotatef(degrees, x,y,z) gltranslatef(x,y,z) glscalef(x,y,z)

Register a callback add to main(): glutidlefunc(renderscene); Replace the renderscene() body with the following: Bold font represents new code glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); static float angle = 0.; glmatrixmode(gl_modelview); glpushmatrix(); glrotatef(angle,0,1,0); glbegin(gl_polygon/*gl_line_loop*/); glcolor3f(1, 0, 0); glvertex3f(-0.5,-0.5,0.0); glcolor3f(0, 1, 0); glvertex3f(0.5,0.0,0.0); glcolor3f(0, 0, 1); glvertex3f(0.0,0.5,0.0); glend(); glpopmatrix(); glutswapbuffers(); angle+=.05;

OpenGL supports several buffers Front Back Left Right Permutations thereof To use this we did: Added GLUT_DOUBLE as an argument to glutinitdisplaymode() Called glutswapbuffers() in the end of renderscene()

Our options are Change the entire scene through modifying bottom of the GL_MODELVIEW stack matrix Change the current GL_PROJECTION matrix Using the previously shown matrix manipulations glmatrixmode(gl_projection); Call matrix manipulator functions Helpful utilities from GL/glu.h

#include <GL/glu.h> - just in case Describe the camera (either one) gluperspective() gluortho2d() Point the camera glulookat()

Sets up a perspective projection matrix fovy field of view angle in camera s y direction aspect aspect ratio (x/y) znear Distance from the camera to the near clipping plane zfar Distance from the camera to the far clipping plane

Set up a 2D orthographic projection matrix left, right Coordinates of the left and right clipping planes bottom, top Coordinates of the bottom and top clipping planes

Defines a viewing transformation (points the camera) eyex, eyey, eyez Specifies the position of the eye point centerx, centery, centerz Specifies the position of the reference point upx, upy, upz Specifies the direction of the up vector

Defines the viewport transformation Specifies transformation NDC to window coordinates: glviewport()

Register a callback -add to main(): glutreshapefunc(changesize); Add above the main(): void changesize(int w, int h) { // Prevent a divide by zero, when window is too short // (you cant make a window of zero width). if(h == 0) h = 1; float ratio = 1.0* w / h; // Reset the coordinate system before modifying glmatrixmode(gl_projection); glloadidentity(); // Set the viewport to be the entire window glviewport(0, 0, w, h); } // Set the correct perspective. gluperspective(45,ratio,1,1000); glmatrixmode(gl_modelview); glloadidentity(); glulookat(0.0,0.0,5.0, 0.0,0.0,-1.0, 0.0f,1.0f,0.0f);

To avoid drawing in order (painter s algorithm) we do: Add GLUT_DEPTH to arguments in the glutinitdisplaymode call Creates a z-buffer glenable(gl_depth_test); Enables depth comparison and update of z-buffer

GLU: glusphere() glucylinder() gludisk() GLUT: Solids glutsolidtorus() glutsolidteapot() Wireframes glutwireicosahedron() glutwireteapot()

Getting started Initialization Drawing Transformations Cameras Animation Input Keyboard Mouse Joystick? Textures Lights Programmable pipeline elements (shaders)

Code from this precept: http://www.cs.princeton.edu/courses/archive/spr 11/cos426/precepts/GlutTest.zip More tutorials (partly used in the presentation): http://www.lighthouse3d.com/opengl/glut http://nehe.gamedev.net/ http://www.videotutorialsrock.com/ OpenGL quick reference: http://www.khronos.org/files/opengl4-quickreference-card.pdf