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

Similar documents
Graphics Pipeline & APIs

Graphics Programming

Graphics Pipeline & APIs

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

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

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

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

Precept 2 Aleksey Boyko February 18, 2011

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

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

Lectures OpenGL Introduction

Introduction to OpenGL

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

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

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

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

Programming using OpenGL: A first Introduction

Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

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

Drawing Primitives. OpenGL basics

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

Lecture 2. Shaders, GLSL and GPGPU


Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

OpenGL Tutorial. Ceng 477 Introduction to Computer Graphics

Graphics Hardware and OpenGL

Visualizing Molecular Dynamics

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Rendering Pipeline/ OpenGL

C OMPUTER G RAPHICS Thursday

E.Order of Operations

Bob s Concise Introduction to Doxygen

Lecture 3. Understanding of OPenGL programming

Lecture 4 of 41. Lab 1a: OpenGL Basics

Tutorial on GPU Programming #2. Joong-Youn Lee Supercomputing Center, KISTI

Transformation Pipeline

Programming with OpenGL Part 3: Three Dimensions

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

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

Rasterization Overview

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

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

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

Computer Graphics (Basic OpenGL)

Intro to OpenGL III. Don Fussell Computer Science Department The University of Texas at Austin

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

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

Graphics and Computation Introduction to OpenGL

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

Basic Graphics Programming

X. GPU Programming. Jacobs University Visualization and Computer Graphics Lab : Advanced Graphics - Chapter X 1

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Computer graphics MN1

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

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

Assignment 1. Simple Graphics program using OpenGL

Programming with OpenGL Part 1: Background

Computer Graphics Course 2005

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

Introduction to OpenGL Week 1

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

Introduction to Shaders.

Computer Graphics. Transformations. CSC 470 Computer Graphics 1

Computer Graphics. Bing-Yu Chen National Taiwan University

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

Introduction to OpenGL

Announcements OpenGL. Computer Graphics. Spring CS4815

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

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

OpenGL. Toolkits.

Intro to OpenGL II. Don Fussell Computer Science Department The University of Texas at Austin

Introduction to OpenGL: Part 2

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

Introduction to OpenGL

1.2 Basic Graphics Programming

Module 13C: Using The 3D Graphics APIs OpenGL ES

Computer Graphics. OpenGL

Introduction to OpenGL

Programming with OpenGL Part 1: Background

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

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

Computer graphic -- Programming with OpenGL 2

COMP 371/4 Computer Graphics Week 1

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

Graphics Hardware. Instructor Stephen J. Guy

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

Introduction to Computer Graphics with OpenGL/GLUT

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

compatibility mode core mode

OpenGL Transformations

An Overview GLUT GLSL GLEW

Graphics Processing Unit Architecture (GPU Arch)

Advanced Rendering Techniques

Models and Architectures

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

Scientific Visualization Basics

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

World Coordinate System

Programmable shader. Hanyang University

Normalized Device Coordinate System (NDC) World Coordinate System. Example Coordinate Systems. Device Coordinate System

Transcription:

OpenGL OpenGL OpenGL Is a mechanism to create images in a frame buffer Is an API to access that mechanism Is well specified OpenGL Is not a window system Is not a user interface Is not a display mechanism Does not even own the framebuffer It is owned by the window system so it can be shared But OpenGL defines its aaributes carefully White Square Code // Draw a white square against a black background #include <windows.h> #include <stdio.h> #define GLUT_DISABLE_ATEXIT_HACK #include <GL/glut.h> // yuck! OpenGL GLUT void draw() { glclear(gl_color_buffer_bit); glloadidentity(); glortho(0, 4, 0, 4, -1, 1); glbegin(gl_polygon); glvertex2i(1, 1); glvertex2i(3, 1); glvertex2i(3, 3); glvertex2i(1, 3); glend(); glflush(); } int main(int argc, char** argv) { glutinit(&argc, argv); glutinitmode(glut_single GLUT_RGBA); glutcreatewindow("whitesquare"); glutfunc(draw); glutmainloop(); } 1

OpenGL PorGon of White Square Code glclear(gl_color_buffer_bit); // black background glloadidentity(); glortho(0, 4, 0, 4, -1, 1); // int cast to double glbegin(gl_polygon); // draw white square glvertex2i(1, 1); glvertex2i(3, 1); glvertex2i(3, 3); glvertex2i(1, 3); glend(); glflush(); // force completion Red Book Example glclearcolor(0.0, 0.0, 0.0, 0.0); glclear(gl_color_buffer_bit); glcolor3f(1.0, 1.0, 1.0); glortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glbegin(gl_polygon); glvertex3f(0.25, 0.25, 0.0); glvertex3f(0.75, 0.25, 0.0); glvertex3f(0.75, 0.75, 0.0); glvertex3f(0.25, 0.75, 0.0); glend(); glflush(); // force completion State tables COLOR_CLEAR_VALUE 0,0,0,0 OpenGL 2.0 Spec, Table 6.21. Control 2

State tables OpenGL 2.0, Table 6.5. Current Values and Associated Data Snippet From the OpenGL 2.0 Spec VerGces are specified by giving their coordinates in two, three, or four dimensions. This is done using one of several versions of the Vertex command: void Vertex{234}{sifd}( T coords ); void Vertex{234}{sifd}v( T coords ); A call to any Vertex command specifies four coordinates: x, y, z, and w. The x coordinate is the first coordinate, y is second, z is third, and w is fourth. A call to Vertex2 sets the x and y coordinates; the z coordinate is implicitly set to zero and the w coordinate to one. OpenGL Philosophy PlaXorm and window system independent Rendering only Aims to be real Gme Takes advantage of graphics hardware where it exists State system Client server system Immediate mode 3

The OpenGL Pipeline OpenGL Shaded Quad Code glclearcolor(1, 1, 1, 1); // white glclear(gl_color_buffer_bit); glloadidentity(); glortho(0, 100, 0, 100, -1, 1); glbegin(gl_triangle_strip); glcolor3f(0, 0.5, 0); // dark green glvertex2i(11, 31); glvertex2i(37, 71); glcolor3f(0.5, 0, 0); // dark red glvertex2i(91, 38); glvertex2i(65, 71); glend(); glflush(); OpenGL Pipeline Emphasis is on data types Diagram ignores Pixel pipeline Texture memory lists is not part of OpenGL 4

Vertex Assembly (data types) float x,y,z,w; float r,g,b,a; } vertex; Vertex Assembly (OpenGL) Vertex assembly Force input to canonical format Convert to internal representagon E.g., x, y to float IniGalize unspecified values E.g., z = 0, w=1 Insert current modal state E.g., color to 0,0.5,0,1 Or create using evaluators Error detecgon INVALID_ENUM INVALID_VALUE INVALID_OPERATION Especially between Begin and End Vertex Assembly (OpenGL) glcolor3f(0, 0.5, 0); glvertex2i(11, 31); glvertex2i(37, 71); glcolor3f(0.5, 0, 0); // no effect float x,y,z,w; // 11, 31, 0, 1 float r,g,b,a; // 0, 0.5, 0, 1 } vertex; float x,y,z,w; // 37, 71, 0, 1 float r,g,b,a; // 0, 0.5, 0, 1 } vertex; 5

Vertex OperaGons OpenGL Transform coordinates 4x4 matrix arithmegc Compute (vertex) lighgng Compute texture coordinates In our case: Scale (arbitrary 100x100) coordinates to fit window No lighgng, no texture coordinates PrimiGve Assembly (data types) float x,y,z,w; float r,g,b,a; } vertex; vertex v0,v1,v2; } triangle; or vertex v0,v1; } line; or vertex v0; } point; PrimiGve Assembly OpenGL Group vergces into primigves: points, lines, or triangles Decompose polygons to triangles Duplicate vergces in strips or fans In our case: Create two triangles from a strip: glbegin(gl_triangle_strip); glcolor(green); glvertex2i( ); // 0 glvertex2i( ); // 1 glcolor(red); glvertex2i( ); // 2 glvertex2i( ); // 3 glend(); 0 1 3 2 6

PrimiGve OperaGons OpenGL Clip to the window boundaries Actually to the frustum surfaces Perform back face / front face ops Culling Color assignment for 2 side lighgng In our case Nothing happens RasterizaGon (data types) float x,y,z,w; float r,g,b,a; } vertex; vertex v0,v1,v2 } triangle; short int x,y; float depth; float r,g,b,a; } fragment; RasterizaGon OpenGL Determine which pixels are included in the primigve Generate a fragment for each such pixel Assign aaributes (e.g., color) to each fragment In our case: 7

Fragment OperaGons OpenGL Texture mapping Fragment lighgng (OpenGL 2.0) Fog Scissor test Alpha test In our case, nothing happens: (2 D array of pixels) float x,y,z,w; float r,g,b,a; } vertex; vertex v0,v1,v2 } triangle; short int x,y; float depth; float r,g,b,a; } fragment; int depth; byte r,g,b,a; } pixel; Fragment Ops OpenGL Color blending Depth tesgng (aka z buffering) Conversion to pixels In our case, conversion to pixels: Key idea: images are built in the framebuffer, not just placed there! 8

Some more detail Modeling and Viewing OpenGL provides no funcgons itself for directly specifying a view it has no policy for how a camera is to be specified It provides no data structures for model hierarchies. Instead it provides fundamental tools that allow the construcgon of many different camera models and hierachies. Modelview Matrix A stack of matrices is maintained called the modelview stack. The current modelview matrix is used to mulgply vergces at the first stage of the rendering pipeline equivalent to matrix C.M (OpenGL notagon) C = CTM, M: WC >VC glmatrixmode(gl_modelview) making changes to modelview 9

Matrix OperaGons glloadmatrix{f}{d}(const GLfloat *m); replaces current matrix glmultmatrix{f}{d} (const GLfloat *m); if c is current matrix then cm is the new one glpushmatrix{f}{d} (); pushes copy of current matrix down on stack; glpopmatrix(); restores top of stack to be current matrix. Example: Object Hierarchy Suppose the current modelview matrix is M: WC >VC (i.e., based on VRP, VPN,VUV). GObject *object; //pointer to graphics object glmatrixmodel(gl_modelview); glpushmatrix(); // push and duplicate current matrix glmultmatrix(object >CTM); // mulgply M by CTM mydraw( object ); // now draw all faces in object glpopmatrix(); //restore original M The ProjecGon Matrix glmatrixmode(gl_projection); subsequent matrix ops affect this stack (only 2 deep) A perspecgve projecgon can be specified by: glloadidengty(); glfrustum(len, right, boaom, top, near, far); each argument is GLdouble 10

TransformaGons gltranslate{d}{f}(x,y,z); translagon matrix T(x,y,z) glscale{d}{f}(x,y,z); scaling matrix S(x,y,z) glrotate{d}{f}(angle, x, y, z); matrix for posigve (ang clockwise) rotagon of angle degrees about vector (x,y,z) If C is current matrix, and Q is transformagon matrix, then new current matrix is CQ (OpenGL notagon) CauGons OpenGL uses a RH coordinate system throughout (hence the default VPN is the negagve z axis). It adopts the convengon of points as column vectors and post mulgplicagon: The transpose of all our matrices should be used! Shading and Colours Shading properges glshademodel(gl_smooth GL_FLAT) Colour glcolornt{v}(r,g,b,{a}) N=3,4 T=b,s,i,ub,ui,us v implies passing a pointer to array of colours 11

LighGng and Materials Many lighgng parameters Specify a material for primigves emissive, ambient, shininess, specular GLfloat mat_spec = { 0.5, 0.5, 1.0, 1.0}; glmaterialfv(gl_front, GL_SPECULAR, mat_spec) glcolormaterial(gl_front, GL_DIFFUSE) Lights Must enable a light with materials GLfloat light_pos ={ 1.0, 2.0, 1.0, 0.0} gllighxv(gl_light0, GL_POSITION, light_pos) glenable(gl_lighting) glenable(gl_light0) Fixed OpenGL Pipeline Geometry ModelView TransformaPon LighPng PerspecPve TransformaPon Viewport TransformaPon and Clipping Geometry Processing (T&L) (MulP )Texturing Scan Conversion Fog Scissor Alpha Stencil Depth Blending Dither LogicOp RasterizaPon Per Fragment OperaPons Frame Buffer 12

Vertex Shaders Geometry ModelView TransformaPon LighPng PerspecPve TransformaPon Viewport TransformaPon and Clipping User Defined Vertex Processing Geometry Processing (T&L) SubsPtute fixed funcpon pipeline with programmable version Vertex Shader has to do everything (except clipping) Vertex Shaders: What for? Custom transform, lighgng, and skinning Vertex Programs: What for? Custom cartoon style lighgng 13

Vertex Shaders: What for? Dynamic displacements of surfaces by objects Vertex Shaders What is a vertex shader? A small program running on GPU for each vertex GPU instrucgon set to perform all vertex math Reads an untransformed, unlit vertex Creates a transformed vertex OpGonally Lights a vertex Creates texture coordinates Creates fog coordinates Creates point sizes Latest GPUs: can read from texture Pixel Shaders RasterizaPon (MulP )Texturing (MulP) Texturing Scan Conversion Fog SubsPtute fixed funcpon pipeline with programmable version User Defined Fragment Processing 14

Pixel Shaders LiAle assembly program for every pixel What for? E.g., per pixel lighgng Pixel Shaders What is a pixel shader? A small program running on GPU for each fragment GPU instrucgon set to perform math (lin. algebra, ) Takes a fragment (depth/color) and texture coordinates Creates an output color (or discard) OpGonally Read texture Perform computagon Summary OpenGL is an architecture Mechanism (the OpenGL pipeline) Interface (API) to feed and manage the mechanism The pipeline is best understood in terms of data types: Vertex PrimiGve (point, line, triangle) Fragment Pixel 15

Coursework Second coursework: OpenGL Due: Wednesday 16/Dec/2009 Hand in both: Write up Code ( handin ) End 16