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

Similar documents
Graphics and Computation Introduction to OpenGL

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

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

Introduction to OpenGL

Precept 2 Aleksey Boyko February 18, 2011

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

Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

Understand how real-world lighting conditions are approximated by OpenGL

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

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

Computer graphics MN1

Scientific Visualization Basics

Lighting. CSC 7443: Scientific Information Visualization

Visualizing Molecular Dynamics

Lighting. Chapter 5. Chapter Objectives. After reading this chapter, you'll be able to do the following:

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


20 GLuint objects; 36 Scale += 0.1; 37 break; 38 case GLUT_KEY_DOWN:

Computer Graphics. Transformations. CSC 470 Computer Graphics 1

Graphics Pipeline & APIs

1 /* 4 C:\opencv\build\include. 6 C:\opencv\build\x86\vc10\lib

Lecture 4 Advanced Computer Graphics (CS & SE )

Graphics Pipeline & APIs

Lecture 4 of 41. Lab 1a: OpenGL Basics

5.2 Shading in OpenGL

Lectures OpenGL Introduction

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

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

Introduction to OpenGL

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

OpenGL Tutorial. Ceng 477 Introduction to Computer Graphics

3D Graphics and API with OpenGL

Lecture 3. Understanding of OPenGL programming

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

Computer Graphics. Bing-Yu Chen National Taiwan University

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

Programming using OpenGL: A first Introduction

Drawing Primitives. OpenGL basics

Order of Transformations

Rendering Pipeline/ OpenGL

Using OpenGL with CUDA

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

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

Computer graphics MN1

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

Transformation, Input and Interaction. Hanyang University

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

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

INF3320 Computer Graphics and Discrete Geometry

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

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

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

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

Illumination and Shading

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

C OMPUTER G RAPHICS Thursday

1 (Practice 1) Introduction to OpenGL

Illumination and Shading ECE 567

OpenGL. Toolkits.

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

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

Computer graphic -- Programming with OpenGL 2

(21) OpenGL GUI. OpenGL GUI 1 UNIX MAGAZINE UNIX. SGI (Silicon Graphics Inc.) Windows PC GUI. UNIX Windows GUI. Java. 1 prefposition() X X

Shading and Illumination

Programming with OpenGL Part 1: Background

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

CS 4204 Computer Graphics

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

Fundamental Types of Viewing

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

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

Computer Graphics Lighting

Computer Graphics Lighting. Why Do We Care About Lighting?

Announcements OpenGL. Computer Graphics. Spring CS4815

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

Programming with OpenGL Part 3: Three Dimensions

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Graphics Hardware and OpenGL

Graphics Programming

DONALD HOUSE: CPSC 8170, FALL 2018 DESIGN OF A REAL-TIME ANIMATION PROGRAM

Exercise 1 Introduction to OpenGL

Cheating: In case of cheating, all parts involved (source(s) and receiver(s)) get zero.

CS Computer Graphics: Illumination and Shading I

CS Computer Graphics: Illumination and Shading I

Introduction to OpenGL Week 1

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

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

Basic Graphics Programming

FAKULTI TEKNOLOGI MAKLUMAT DAN KOMUNIKASI BITM INTERACTIVE COMPUTER GRAPHICS LAB SESSION 4. C++ - OpenGL

Assignment 1. Simple Graphics program using OpenGL

SOURCES AND URLS BIBLIOGRAPHY AND REFERENCES

Lecture 2 2D transformations Introduction to OpenGL

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

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

Display Issues Week 5

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

OpenGL refresher. Advanced Computer Graphics 2012

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

Transcription:

Outline 433-380 Graphics and Computation Introduction to OpenGL OpenGL Background and History Other Graphics Technology Drawing Viewing and Transformation Lighting GLUT Resources Some images in these slides are taken from The OpenGL Programming Manual, which is copyright Addison Wesley and the OpenGL Architecture Review Board. http://www.opengl.org/documentation/red_book_1.0/ 2 OpenGL Background and History Other Graphics Technology OpenGL = Open Graphics Library Developed at Silicon Graphics (SGI) Successor to IrisGL Cross Platform (Win32, Mac OS X, Unix, Linux) Only does 3D Graphics. No Platform Specifics (Windowing, Fonts, Input, GUI) Version 1.4 widely available Two Libraries GL (Graphics Library) GLU (Graphics Library Utilities) Low Level Graphics OpenGL Scene Graphs, BSPs OpenSceneGraph, Java3D, VRML, PLIB DirectX (Direct3D) Can mix some DirectX with OpenGL (e.g OpenGL and DirectInput in Quake III) 3 4 Platform Specifics Platform Specific OpenGL Interfaces Windows (WGL) X11 (GLX) Motif (GLwMwidget) Mac OS X (CGL/AGL/NSOpenGL) Cross Platform OpenGL Interface Qt (QGLWidget, QGLContext) Java (JOGL) GLUT (GL Utility Library) ClearTheScreen(); DrawTheScene(); CompleteDrawing(); SwapBuffers(); The Drawing Process In animation there are usually two buffers. Drawing usually occurs on the background buffer. When it is complete, it is brought to the front (swapped). This gives a smooth animation without the viewer seeing the actual drawing taking place. Only the final image is viewed. The technique to swap the buffers will depend on which windowing library you are using with OpenGL. 5 6 1

Clearing the Window Setting the Colour glclearcolor(0.0, 0.0, 0.0, 0.0); glclear(gl_color_buffer_bit); Typically you will clear the colour and depth buffers. glclearcolor(0.0, 0.0, 0.0, 0.0); glcleardepth(0.0); glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); 7 Colour is specified in (R,G,B,A) form [Red, Green, Blue, Alpha], with each value being in the range of 0.0 to 1.0. There are many variants of the glcolor command glcolor4f(red, green, blue, alpha); glcolor3f(red, green, blue); glcolor3f(0.0, 0.0, 0.0); /* Black */ glcolor3f(1.0, 0.0, 0.0); /* Red */ glcolor3f(0.0, 1.0, 0.0); /* Green */ glcolor3f(1.0, 1.0, 0.0); /* Yellow */ glcolor3f(1.0, 0.0, 1.0); /* Magenta */ glcolor3f(1.0, 1.0, 1.0); /* White */ 8 Complete Drawing the Scene Need to tell OpenGL you have finished drawing your scene. glfinish(); Drawing in OpenGL Use glbegin() to start drawing, and glend() to stop. glbegin() can draw in many different styles (see figure) glvertex3f(x,y,z) specifies a point in 3D space. or glflush(); For more information see Chapter of the Red Book: http://fly.cc.fer.hr/~unreal/theredbook/chapter02.html or http://www.gamedev.net/download/redbook.pdf glbegin(gl_polygon); glvertex3f(0.0, 0.0, 0.0); glvertex3f(0.0, 3.0, 1.0); glvertex3f(3.0, 3.0, 3.0); glvertex3f(4.0, 1.5, 1.0); glvertex3f(3.0, 0.0, 0.0); glend(); 9 10 Mixing Geometry with Colour Viewing the Scene: The Camera Specifying vertices can be mixed with colour and other types of commands for interesting drawing results. glbegin(gl_polygon); glcolor3f(1.0, 0.0, 0.0); glvertex3f(0.0, 0.0, 0.0); glcolor3f(0.0, 1.0, 0.0) glvertex3f(3.0, 1.0, 0.0); glcolor3f(0.0, 0.0, 1.0); glvertex3f(3.0, 0.0, 0.0); glend(); 11 12 2

& x # $! $ y! v = $ z! $! % w" OpenGL Vertices OpenGL uses a 4 component vector to represent a vertex. Known as a homogenous coordinate system z = 0 in 2D space w = 1 usually For further information on homogenous coordinate systems as used in projective geometry and in OpenGL, see Appendix G of the Red Book http://fly.cc.fer.hr/~unreal/theredbook/appendixg.html Vertex Transformation v ' = Mv 13 14 The ModelView Matrix glmatrixmode(gl_modelview); Specifying the ModelView matrix is analogous to Positioning and aiming the camera (viewing transformation) Positioning and orienting the model (modeling transformation) The Projection Matrix glmatrixmode(gl_projection); Specifying the Projection matrix is like choosing a lens for a camera. It lets you specify field of view and other parameters. 15 16 OpenGL Matrix Operations Perspective Projection (glfrustrum) glmatrixmode(mode); glloadidentity(); glmultmatrix(); glloadmatrix(); Identity Matrix " 1 0 0 0% $ ' 0 1 0 0 I = $ ' $ 0 0 1 0' $ ' # 0 0 0 1& 17 glfrustrum(left, right, bottom, top, near, far); 18 3

Perspective Projection (gluperspective) from GLU Orthographic (Parallel) Projection (glortho) gluperspective(fovy, aspect, near, far); fovy = field of view angle in degrees, in the y direction. aspect = aspect ratio that determines the field of view in the x direction (ratio of width to height). glortho(left, right, bottom, top, near, far); 19 20 glulookat Translation Transformation Specifies the camera position, the point where the camera is pointing, and the orientation vector of the camera. glulookat(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz); gltranslatef(x,y,z); 21 22 Rotation Transformations Scaling Transformations glrotatef(angle, x, y,z); glscale(x,y,z); Specifies an angle and an axis (x,y,z) to rotate around. 23 24 4

Order of Transformations Transformations in Action 25 26 The Matrix Stack glpushmatrix() and glpopmatrix() glpushmatrix(); gltranslatef(10,10,0); DrawRedTriangle(); glpushmatrix(); gltranslatef(20,10,0); DrawBlueTriangle(); glpopmatrix(); glpopmatrix(); DrawGreenTriangle(); 40 30 20 10 y 0 x 0 10 20 30 40 27 OpenGL Lighting Eight available lights (individually toggled) Lighting types Emitted Ambient Diffuse Specular Material Colours and Properties Lighting Demo 28 Setting up an OpenGL Light gllight{if[v](light, pname, param) GLfloat lightambient[] = { 0.4, 0.5, 0.0, 1.0 ; GLfloat lightdiffuse[] = { 1.0, 1.0, 1.0, 1.0 ; GLfloat lightspecular[] = { 1.0, 1.0, 1.0, 1.0; GLfloat lightposition[] = {1.0, 1.0, 1.0, 0.0; Parameter Name GL_AMBIENT GL_DIFFUSE GL_SPECULAR Default Value (0.0, 0.0, 0.0, 1.0) (1.0, 1.0, 1.0, 1.0) (1.0, 1.0, 1.0, 1.0) Meaning ambient RGBA intensity of light diffuse RGBA intensity of light specular RGBA intensity of light gllightfv(gl_light0, GL_AMBIENT, lightambient); gllightfv(gl_light0, GL_DIFFUSE, lightdiffuse); gllightfv(gl_light0, GL_SPECULAR, lightspecular); gllightfv(gl_light0, GL_POSITION, lightposition); GL_POSITION GL_SPOT_DIRECTION GL_SPOT_EXPONENT GL_SPOT_CUTOFF GL_CONSTANT_ATTENUATION (0.0, 0.0, 1.0, 0.0) (0.0, 0.0, -1.0) 0.0 180.0 1.0 (x, y, z, w) position of light (x, y, z) direction of spotlight spotlight exponent spotlight cutoff angle constant attenuation factor glenable(gl_light0); glenable(gl_lighting); GL_LINEAR_ATTENUATION GL_QUADRATIC_ATTENUATION 0.0 0.0 linear attenuation factor quadratic attenuation factor 29 Table 6-1 : Default Values for pname Parameter of gllight*() 30 5

Material Properties glmaterial{if[v](glenum face GLenum pname, TYPE param); GLfloat material[] = {0.1, 0.5, 0.8, 1.0 ; glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE, material); GLfloat matspecular[] = { 1.0, 1.0, 1.0, 1.0 ; GLfloat lowshininess[] = { 5.0 ; glmaterialfv(gl_front, GL_SPECULAR, matspecular); glmaterialfv(gl_front, GL_SHININESS, lowshininess); glmaterial Default Parameters Parameter Name Default Value Meaning GL_AMBIENT (0.2, 0.2, 0.2, 1.0) ambient color of material GL_DIFFUSE (0.8, 0.8, 0.8, 1.0) diffuse color of material GL_AMBIENT_AND_DIFFUSE ambient and diffuse color of material GL_SPECULAR (0.0, 0.0, 0.0, 1.0) specular color of material GL_SHININESS 0.0 specular exponent GL_EMISSION (0.0, 0.0, 0.0, 1.0) emissive color of material GL_COLOR_INDEXES (0,1,1) ambient, diffuse, and specular color indices Table 6-2 : Default Values for pname Parameter of glmaterial*() 31 32 Normal Vectors glbegin(gl_polygon); glnormal3fv(n0); glvertex3fv(v0); glnormal3fv(n1); glvertex3fv(v1); glnormal3fv(n2); glvertex3fv(v2); glnormal3fv(n3); glvertex3fv(v3); 33 glend(); 34 Normal Vectors (2) Hidden Surface Removal In order for OpenGL to remove hidden surfaces, you need to enable depth testing for the depth buffer. glenable(gl_depth_test); while (1) { glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); GetCurrentViewingPosition(); DrawObjectA(); DrawObjectB(); http://www.codeguru.com/cpp/g-m/opengl/article.php/c2681/ 35 36 6

GLUT GLUT = GL Utility Library Easy, stable, simple to use library for showing OpenGL demos. Limited to simple windows, mouse/keyboard input, and some simple 3D shapes. Most OpenGL demos and code on the web use GLUT. Default implementation in C (bindings for many languages available: Python, Perl etc) #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h>... Example GLUT Program in C int main(int argc, char** argv) { glutinitdisplaymode(glut_rgb GLUT_DEPTH GLUT_DOUBLE); glutinitwindowsize(1024, 768); glutinitwindowposition(0, 0); glutinit(argc, argv); glutcreatewindow( OpenGL Demo ); glutreshapefunc(reshape); glutdisplayfunc(display); glutidlefunc(display); glutkeyboardfunc(keyboard); glutmousefunc(mouse); glutmotionfunc(motion); InitGL(); 37 glutmainloop(); 38 Typical InitGL() Function void InitGL(void) { float ambientlight[] = {0.5, 0.5, 0.5, 0.0; float diffuselight[] = {0.5, 0.0, 0.5, 0.0; float lightposition[] = {1.0, 1.0, 1.0, 0.0; glclearcolor(0.0, 0.0, 0.0, 0.0); glcleardepth(1.0); gldepthfunc(gl_lequal); glenable(gl_depth_test); glenable(gl_color_material); glshademodel(gl_smooth); glcolormaterial(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE); glenable(gl_blend); Typical GLUT Reshape Function void reshape(int width, int height); { glviewport(0, 0, width, height); glmatrixmode(gl_projection); glloadidentity(); gluperspective(fovy, aspect, znear, zfar); gllightfv(gl_light0, GL_AMBIENT, ambientlight); gllightfv(gl_light0, GL_DIFFUSE, diffuselight); gllightfv(gl_light0, GL_POSITION, lightposition); glenable(gl_normalize); glenable(gl_lighting); glenable(gl_light0); glmatrixmode(gl_projection); glloadidentity(); gluperspective(fovy, aspect, znear, zfar); glmatrixmode(gl_modelview); 39 40 Typical GLUT Display Function GLUT on Unix (cmd line) void display(void) { UpdateWorld(); glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glmatrixmode(gl_projection); glloadidentity(); gluperspective(fovy, aspect, znear, zfar); glmatrixmode(gl_modelview); glloadidentity(); glulookat(0.0, 0.0, 150.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); gcc main.c -I/usr/X11R6/include -L/usr/X11R6/lib -lgl -lglu -lglut -lm Using frameworks on Unix, includes should be: #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> RenderScene(); glutswapbuffers(); 41 42 7

GLUT on Mac OS X (cmd line) OpenGL Books gcc main.c -F/System/Library/Frameworks -framework GLUT -framework OpenGL Using frameworks on Mac OS X, includes should be: #include <GLUT/glut.h> #include <OpenGL/gl.h> #include <OpenGL/glu.h> 43 44 Resources Project Preparation OpenGL Home Page: www.opengl.org OpenGL Tutors http://www.xmission.com/~nate/tutors.html NeHe Tutorials: http://nehe.gamedev.net OpenGL Red Book (Programming Manual) http://www.opengl.org/documentation/red_book_1.0/ OpenGL Blue Book (Reference Manual) http://www.opengl.org/documentation/blue_book_1.0/ Using GLUT with Visual C++ Express Edition (see PDF File) Read Chapters 1-6 of OpenGL Red Book Familiarise yourself with OpenGL Blue Book Play with OpenGL Tutors Learn about GLUT Do NeHe Tutorial Lessons 1-5 (with GLUT) 45 46 8