Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

Similar documents
Introduction to OpenGL

Precept 2 Aleksey Boyko February 18, 2011

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

Graphics and Computation Introduction to OpenGL

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

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

Computer graphics MN1

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

Computer Graphics. Transformations. CSC 470 Computer Graphics 1

Visualizing Molecular Dynamics

3D Graphics and API with OpenGL

CIS 636 Interactive Computer Graphics CIS 736 Computer Graphics Spring 2011

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

Advanced Computer Graphics (CS & SE )

Graphics Pipeline & APIs

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

Introduction to OpenGL

1 Transformations. Chapter 1. Transformations. Department of Computer Science and Engineering 1-1

Graphics Pipeline & APIs

OpenGL. Toolkits.

Computer Graphics. Chapter 10 Three-Dimensional Viewing

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

Introduction to OpenGL

Computer graphics MN1

Order of Transformations

CS354 Computer Graphics Viewing and Modeling

C OMPUTER G RAPHICS Thursday

Scientific Visualization Basics

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

Describe the Orthographic and Perspective projections. How do we combine together transform matrices?

CS 591B Lecture 9: The OpenGL Rendering Pipeline

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

The Viewing Pipeline adaptation of Paul Bunn & Kerryn Hugo s notes

Rendering Pipeline/ OpenGL

OpenGL Tutorial. Ceng 477 Introduction to Computer Graphics

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

12. Selection. - The objects selected are specified by hit records in the selection buffer. E.R. Bachmann & P.L. McDowell MV 4202 Page 1 of 13

Shading and Illumination

Computer Graphics. Chapter 7 2D Geometric Transformations

CSCI E-74. Simulation and Gaming

Transformation Pipeline

Computer graphics Labs: OpenGL (1/3) Geometric transformations and projections

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

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

Lecture 4 Advanced Computer Graphics (CS & SE )

5.2 Shading in OpenGL

Methodology for Lecture. Importance of Lighting. Outline. Shading Models. Brief primer on Color. Foundations of Computer Graphics (Spring 2010)

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

OpenGL. 1 OpenGL OpenGL 1.2 3D. (euske) 1. Client-Server Model OpenGL

Shading in OpenGL. Outline. Defining and Maintaining Normals. Normalization. Enabling Lighting and Lights. Outline

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

Transformation, Input and Interaction. Hanyang University

Graphics Programming

Fundamental Types of Viewing

1 (Practice 1) Introduction to OpenGL

Lectures OpenGL Introduction

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

Matrix-Rechnung I ' z =... Universität Frankfurt

CS4202: Test. 1. Write the letter corresponding to the library name next to the statement or statements that describe library.

3D Graphics Pipeline II Clipping. Instructor Stephen J. Guy

OpenGL Transformations

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

Lecture 4 of 41. Lab 1a: OpenGL Basics

Graphics and Visualization

cs123 Lab 3 OpenGL Part One 1 Introduction 2 OpenGL Basics 2.2 The Rendering Pipeline 2.1 The CPU and the GPU 2.3 Basic Syntax of OpenGL commands

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

CSC 470 Computer Graphics

Illumination and Shading

yahoo.com

COMP Computer Graphics and Image Processing. 5: Viewing 1: The camera. In part 1 of our study of Viewing, we ll look at ˆʹ U ˆ ʹ F ˆʹ S

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

3D Graphics for Game Programming (J. Han) Chapter II Vertex Processing

CS Computer Graphics: Illumination and Shading I

CS Computer Graphics: Illumination and Shading I

CS 4204 Computer Graphics

Android and OpenGL. Android Smartphone Programming. Matthias Keil. University of Freiburg

Transformations (Rotations with Quaternions) October 24, 2005

Illumination Model. The governing principles for computing the. Apply the lighting model at a set of points across the entire surface.

Models and Architectures

Fachhochschule Regensburg, Germany, February 15, 2017

CSE 167: Lecture #8: Lighting. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2011

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

Lecture 4. Viewing, Projection and Viewport Transformations

Viewing Transformation

CSE 690: GPGPU. Lecture 2: Understanding the Fabric - Intro to Graphics. Klaus Mueller Stony Brook University Computer Science Department

part 3 Martin Samuelčík Room I4

Drawing Primitives. OpenGL basics

Understand how real-world lighting conditions are approximated by OpenGL

OpenGL. White Square Code 11/4/09. OpenGL. OpenGL. OpenGL

Lecture 5b. Transformation

Computer Viewing Computer Graphics I, Fall 2008

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

COMS 4160: Problems on Transformations and OpenGL

Programming using OpenGL: A first Introduction

CS 4204 Computer Graphics

Computer graphics MN1

Using GLU/GLUT Objects

IntMu.Lab5. Download all the files available from

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

6. Modelview Transformations

Transcription:

Introduction to OpenGL Transformations, Viewing and Lighting Ali Bigdelou

Modeling From Points to Polygonal Objects Vertices (points) are positioned in the virtual 3D scene Connect points to form polygons Complex objects are composed out of many polygons

Manipulating points (vectors) in 3D space Three important transformations y y y x x x Translation Scaling Rotation

3D Translation & Rotation in Cartesian Coordinates t p M p x 3 3 ' z y x p 3D Point Translation z y x t t t p p ' Rotation around z-axis Performing rotation and traslation p p 1 cos sin sin cos '

Homogenous Coordinates 1 1 1 1 1 T I t t t z y x Translations matrix Translation p T I p 1 ' Homogeneous coordinates allow translations to be performed by matrix mutiplication The column space is extended with an additional dimension Translations can now be represented with a matrix 1 z y x z y x

Homogenous Coordinates p R p 1 p M p x 4 4 ' 1 1 R R Rotation matrix Rotation From to t p M p x 3 3 '

Example: 3D Euclidean Transformation 1 1 1 z y x T R z y x First rotation around the z-axis then translation (Notice inverse interpretation of multiplication)

3D Scaling 1 * 1 1 z y x s s s z y x z y x

Euler-Angle for Rotation R=R z R y R x Rotation around the z-axis: Rotation around the y-axis: Rotation around the x-axis:

OpenGL (Open Graphics Library) since 1992 API Specification Platform-, Programming Language-, Hardware- Independent access to accelerated Graphics-Hardware (Graphics-Cards, GPUs) Developed by Silicon Graphics, implemented mostly through graphics drivers OpenGL is a state-machine Current Version 4.1 (July 26, 21) Extensions add additional functionality to OpenGL Developed by hardware manufacturers Often adopted by other manufacturers Accessibly through libraries like GLEW (OpenGL Extension Wrangler) New OpenGL releases include some extensions into the specification Usage: Visualization, Gaming and Augmented Reality Websites http://www.opengl.org, http://glew.sourceforge.net/

OpenGL API Function Naming Convection <Library prefix><main command><optional argument count> <Optional argument type> Example: glcolor3f gl : Prefix OpenGL function Color: Main Command 3 : number of arguments f : type of arguments (here float) glcolor4f, glcolor3i, glvertex3f,...

OpenGL Rendering Pipeline

Object Coordinate System and Camera y z Object Coordinate Sytem Default Camera: Camera Position (eye) (,,) View-Vector (center) (,,-1) x Up-Vector (up) (,1,) Set Camera with glulookat(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz)

Basic Primitives Draw Primitive: glbegin(gl_polygon); glvertex3f(.,., -1.); glvertex3f(., 3., -1.); glvertex3f(4., 3., -1.); glvertex3f(6., 1.5, -1.); glvertex3f(4.,., -1.); glend(); y z x

OpenGL Rendering Pipeline

3D Transformations in OpenGL Translation gltranslatef(7.f,.f,.f); Rotation // Translate 7 units to the right // (positiv x-axis) glrotatef(35.f,.f,.f,1.f); // Rotate 35 Degrees around the z- // axis (counter-clockwise) Scale glscalef(.5f,.5f,.5f); Directly applying transformation matrix glmultmatrixf(pointertomatrix); Commands generate a 4x4 matrix and multiply it with the current modelview matrix Attention: OpenGL stores matrices in column-major order // Scale along x-,y- and z-axis by a // factor of.5 // Multiply current matrix with a 4x4 // Matrix pointed by pointertomatrix

Order of transformations A(BC) = (AB)C, order of multiplication is irrelevant AB!= BA, order of transformations is relevent gltranslatef( 2.f,.f,.f ); glrotatef( 45.f,.f,.f, 1.f ); glrotatef( 45.f,.f,.f, 1.f ); gltranslatef( 2.f,.f,.f );

OpenGL Rendering Pipeline

Projection Set Projection matrix glmatrixmode(gl_projection); glloadidentity(); gluperspective(fovy, aspect, near, far); or glfrustum(left, right, bottom, top, near, far); or glortho(left, right, bottom, top, near, far), Attention: near >= 1., otherwise depth accuracy unacceptable

OpenGL Rendering Pipeline Divide with homogeneous coordinate Normalized device coordinates

Viewport Size of rendered image on monitor window (in pixels) glviewport(x,y,width,height); x,y: lower left corner of the window rectangle width,height of the window rectangle

External helpers for models GLUT Objects glutsolidsphere(gldouble radius, GLint slices, GLint stacks), glutsolidcube(gldouble size), glutsolidcone(gldouble base, GLdouble height, GLint slices, GLint stacks), glutsolidteapot(gldouble size) and more Also available as wireframe (wireframe visualization can also be enabled through OpenGL calls) glutwireteapot(gldouble size) http://www.opengl.org/resources/libraries/glut/

OpenGL is a State-Machine Every change on the state affects subsequent calls and is only explicitly change with a new OpenGL call E.g.. glcolor3f(1.,.,.), the vertex color is set to red until stated otherwise The current state can be pushed onto a stack and recalled later glpushattrib(glbitfield mask)pushes the state specified by mark on the stack e.g. glpushattrib(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT) stores the current color and depth information on the stack glpopattrib() restores the state from the top of the stack Matrices can also be pushed on a separate stack glpushmatrix()push current matrix glpopmatrix() pop/restore matrix( basically set the current matrix to the one stored at the top of the matrix stack)

Direct operations on OpenGL matrices glmatrixmode(matrix) matrix: GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE, GL_COLOR specify which matrix should be affected by subsequent operations glloadidentity(); set current matrix to identity glloadmatrix{f,d}(pointertomatrix); set current matrix to the one stored in pointertomatrix glmultmatrix{f,d}(pointertomatrix); multiply current matrix with the one stored in pointertomatrix GLdouble model[16]; glgetdoublev(gl_modelview_matrix,model); read and store matrix (here modelview matrix) into model

Multiple Objects and Transformations glpushmatrix(); glcolor3f(1.f,.f,.f); glutsolidteapot(5.); // Red teapot gltranslatef(.f,25.f,.f); glcolor3f(.f,1.f,.f); glutsolidteapot(5.); // Green teapot gltranslatef(2.f,.f,.f); glcolor3f(.f,.f,1.f); glutsolidteapot(5.); // Blue teapot gltranslatef(.f,-15.f,.f); glrotatef(45.f,.f,.f,1.f); glcolor3f(.f,1.f,1.f); glutsolidteapot(5.); // Cyan teapot glpopmatrix(); glpushmatrix(); glcolor3f(1.f,.f,1.f); gltranslatef(.f,-25.f,.f); glutsolidteapot(5.); // Magenta teapot glpopmatrix();

Depthbuffer What happens if objects occlude each other? In the final image the objects would overlap Solution: Depthbuffer For each pixel a depth value is stored Depthfunc specifies condition for pixels passing the depth test glenable(gl_depth_test); gldepthfunc(gl_lequal); //default GL_LEQUAL: The incoming pixel (fragment) passes the depth test if its depth is less or equal to the one stored in the buffer Depth Test Disabled Depth Test Enabled

OpenGL Lighting

Different Types of Light Ambient Light Global light emitted from a scene Ambient Diffuse Light Depends on surface normal + light direction Specular Light Depends on surface normal + light direction + camera (eye) position Diffuse Emitted Light Light is emitted from polygon Specular

Phong Lighting Model + + Ambient Diffuse Specular = Phong lighting

Light in OpenGL GLfloat light_ambient[] = {.,.,., 1. }; GLfloat light_diffuse[] = { 1., 1., 1., 1. }; GLfloat light_specular[] = { 1., 1., 1., 1. }; GLfloat light_position[] = { 1., 1., 1.,. }; gllightfv(gl_light, GL_AMBIENT, light_ambient); gllightfv(gl_light, GL_DIFFUSE, light_diffuse); gllightfv(gl_light, GL_SPECULAR, light_specular); gllightfv(gl_light, GL_POSITION, light_position); glenable(gl_lighting); glenable(gl_light); Maximal number of lights:int maxnumlights = ; glgetintegerv(gl_max_lights, &maxnumlights); The performance decreases with increasing number of lights

Light in OpenGL GLfloat light_position[] = { 1., 1., 1., w }; gllightfv(gl_light, GL_POSITION, light_position); Positional light source w> Directional light source w =

OpenGL is not a GUI Toolkit OpenGL does not offer functionality to generate and manage GUI windows OpenGL assumes that the programmer provides a valid GUI window together with an OpenGL render context Window and render context can be provided through different toolkits, such as: GLUT, Qt, FLTK, MFC,... GLUT OpenGL Utility Library: A simple platform independent library for generating a window with OpenGL context. Can also handle keyboard and mouse events. Websites http://www.opengl.org/resources/libraries/glut/, http://openglut.sourceforge.net/, http://www.xmission.com/~nate/glut.html

Setup an OpenGL App with GLUT Download GLUT source-code or the precompiled library for your platform (easier for beginners) Include header files glut.h und glu.h Include GLUT and GLU library as linker inputs (glut32.lib, glu32.lib) Most common error: Forgot to include header and libraries Download http://www.opengl.org/resources/libraries/glut/, http://openglut.sourceforge.net/, http://www.xmission.com/~nate/glut.html

GLUT App Min. requirements

GLUT App Min. requirements

GLUT App Min. requirements

GLUT App Min. requirements

GLUT App Min. requirements

This was an introduction, OpenGL has a LOT more to offer For more info see: The Red Book Version 1.1 is online http://www.opengl.org/documentation/red_book/ NeHe Online Tutorials including code http://nehe.gamedev.net/ The OpenGL specification (very technical, but includes everything) http://www.opengl.org/ GLUT Specification http://www.opengl.org/resources/libraries/glut/spec3/spec3.html OpenGL Interactive Tutorials http://www.xmission.com/~nate/tutors.html Common OpenGL Pitfalls http://www.opengl.org/resources/features/kilgardtechniques/oglpitfall/ www.gamedev.net, Lots of useful articles and active developer community