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

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

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

Graphics Programming

Shadow Algorithms. CSE 781 Winter Han-Wei Shen

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

OpenGL/GLUT Intro. Week 1, Fri Jan 12

OpenGL refresher. Advanced Computer Graphics 2012

CS4620/5620: Lecture 14 Pipeline

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Pipeline Operations. CS 4620 Lecture 10

Pipeline Operations. CS 4620 Lecture 14

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

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

Programming with OpenGL Part 3: Three Dimensions

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

Precept 2 Aleksey Boyko February 18, 2011

Models and Architectures

Basic Graphics Programming

Rendering Pipeline/ OpenGL

Computer Graphics. Bing-Yu Chen National Taiwan University

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

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

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

Graphics Hardware and OpenGL

The Traditional Graphics Pipeline

Interaction. CSCI 480 Computer Graphics Lecture 3

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Rasterization Overview

CSE 167: Introduction to Computer Graphics Lecture #5: Rasterization. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

Introduction to Computer Graphics with WebGL

CS 4620 Program 3: Pipeline

Motivation. Culling Don t draw what you can t see! What can t we see? Low-level Culling

VISIBILITY & CULLING. Don t draw what you can t see. Thomas Larsson, Afshin Ameri DVA338, Spring 2018, MDH

Exercise 1 Introduction to OpenGL

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

1.2 Basic Graphics Programming

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

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

CS 428: Fall Introduction to. Polygon rendering: additional topics. Andrew Nealen, Rutgers, /14/2009 1

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

The Traditional Graphics Pipeline

CS 498 VR. Lecture 18-4/4/18. go.illinois.edu/vrlect18

Drawing Fast The Graphics Pipeline

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

The Traditional Graphics Pipeline

Interaction. CSCI 420 Computer Graphics Lecture 3

CS Computer Graphics: Hidden Surface Removal

Models and Architectures. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

Lecture 2. Determinants. Ax = 0. a 11 x 1 + a 12 x a 1n x n = 0 a 21 x 1 + a 22 x a 2n x n = 0

Interaction Computer Graphics I Lecture 3

Visualizing Molecular Dynamics

Programming using OpenGL: A first Introduction

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

Lectures OpenGL Introduction

Basic Graphics Programming

CSE4030 Introduction to Computer Graphics

Rendering approaches. 1.image-oriented. 2.object-oriented. foreach pixel... 3D rendering pipeline. foreach object...

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

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

Lecture 3. Understanding of OPenGL programming

Deferred Rendering Due: Wednesday November 15 at 10pm

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

Computer Graphics Course 2005

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

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

1.2.3 The Graphics Hardware Pipeline

OpenGl Pipeline. triangles, lines, points, images. Per-vertex ops. Primitive assembly. Texturing. Rasterization. Per-fragment ops.

Computergrafik. Matthias Zwicker. Herbst 2010

Performance OpenGL Programming (for whatever reason)

An Overview GLUT GLSL GLEW

Graphics Hardware. Instructor Stephen J. Guy

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

Computer Graphics (Basic OpenGL)

Lecture 3 Advanced Computer Graphics (CS & SE )

Blue colour text questions Black colour text sample answers Red colour text further explanation or references for the sample answers

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

CS 130 Exam I. Fall 2015

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

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

The Rasterization Pipeline

Rendering Objects. Need to transform all geometry then

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

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


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

Programming with OpenGL Part 1: Background

CSE 167: Introduction to Computer Graphics Lecture #4: Vertex Transformation

2 Transformations and Homogeneous Coordinates

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

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

Drawing Fast The Graphics Pipeline

Surface shading: lights and rasterization. Computer Graphics CSE 167 Lecture 6

CS GPU and GPGPU Programming Lecture 2: Introduction; GPU Architecture 1. Markus Hadwiger, KAUST

Introduction to Shaders.

CMSC427: Computer Graphics Lecture Notes Last update: November 21, 2014

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

Drawing Fast The Graphics Pipeline

CS 543 Lecture 1 (Part II): Intro to OpenGL and GLUT (Part I) Emmanuel Agu

CS 4204 Computer Graphics

Transcription:

Intro to OpenGL III Don Fussell Computer Science Department The University of Texas at Austin University of Texas at Austin CS354 - Computer Graphics Don Fussell

Where are we? Continuing the OpenGL basic pipeline

OpenGL API Example glshademodel(gl_smooth); // smooth color interpolation glenable(gl_depth_test); // enable hidden surface removal glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glbegin(gl_triangles); // every 3 vertexes makes a triangle glcolor4ub(255, 0, 0, 255); // RGBA=(1,0,0,100%) glvertex3f(-0.8, 0.8, 0.3); // XYZ=(-8/10,8/10,3/10) glcolor4ub(0, 255, 0, 255); // RGBA=(0,1,0,100%) glvertex3f( 0.8, 0.8, -0.2); // XYZ=(8/10,8/10,-2/10) glcolor4ub(0, 0, 255, 255); // RGBA=(0,0,1,100%) glvertex3f( 0.0, -0.8, -0.2); // XYZ=(0,-8/10,-2/10) glend();

GLUT API Example #include <GL/glut.h> // includes necessary OpenGL headers void display() { // << insert code on prior slide here >> glutswapbuffers(); } void main(int argc, char **argv) { // request double-buffered color window with depth buffer glutinitdisplaymode(glut_rgba GLUT_DOUBLE GLUT_DEPTH); glutinit(&argc, argv); glutcreatewindow( simple triangle ); glutdisplayfunc(display); // function to render window glutmainloop(); }

Rasterization Last time we covered how rasterization is done. Lots of linear interpolation in specialpurpose hardware, so it s fast. Application Vertex batching & assembly Clipping NDC to window space Rasterization Fragment shading Depth testing Color update Depth buffer Framebuffer

Simple Fragment Shading We also talked a little about fragment shading, that is, the simple interpolated color shading that can be done in the rasterizer. There s much more to come. Application Vertex batching & assembly Clipping NDC to window space Rasterization Fragment shading Depth testing Color update Depth buffer Framebuffer

Color Interpolation Our simple triangle is drawn with smooth color interpolation Recall: glshademodel(gl_smooth) How is color interpolated? Think of a plane equation to computer each color component (say red) as a function of (x,y) Just done for samples positions within the triangle " redness "= A x + B y + C red red red

Barycentric Coordinates N Area(PMN) = α Area(LMN) Area(LPN) = β Area(LMN) P Area(LMP) = γ Area(LMN) L M Note: α + β + γ = 0 by construction attribute(p) = α attribute(l) + β attribute(m) + γ attribute(n)

Hardware Triangle Rendering Rates Top GPUs can setup over a billion triangles per second for rasterization Triangle setup & rasterization is just one of the (many, many) computation steps in GPU rendering

A Simplified Graphics Pipeline Application Vertex batching & assembly Clipping NDC to window space Ensure closer objects obscure (hide) more distant objects Rasterization Fragment shading Depth testing Color update Depth buffer Framebuffer

Interpolating Window Space Z Plane equation coefficients (A, B, C) generated by multiplying inverse matrix by vector of per-vertex attributes = z z z z z z y x y x y x C B A N M L N N M M L L 1 1 1 1

Simple Triangle Vertex Depth Assume glviewport(0,0,500,500) has been called And gldepthrange(0,1) L=(50, 450, 0.65) N=(450,450,0.4) L z = 0.65 M z = 0.40 N z = 0.40 M=(250,50,0.4) CS 354

Interpolating Window Space Z 50 250 450 Substitute per-vertex (x,y) and Z values for the L, M, and N vertexes 450 50 450 1 1 1 1 0.65 0.4 0.4 = A B C Complete Z plane equation Z(x,y) = -0.000625*x + 0.0003125*y + 0.540625 z z z A z = -0.000625 B z = 0.0003125 C z = 0.540625

Depth Buffer Algorithm Simple, brute force Every color sample in framebuffer has corresponding depth sample Discrete, solves occlusion in pixel space Memory intensive, but fast for hardware Basic algorithm Clear the depth buffer to its maximum far value (generally 1.0) Interpolate fragment s Z Read fragment s corresponding depth buffer sample Z value If interpolated Z is less than (closer) than Z from depth buffer Then replace the depth buffer Z with the fragment s Z And also allow the fragment s shaded color to update the corresponding color value in color buffer Otherwise discard fragment Do not update depth or color buffer

Depth Buffer Example Fragment gets rasterized Fragment s Z value is interpolated Resulting Z value is 0.65 Read the corresponding pixel s Z value Reads the value 0.8 Evaluate depth function 0.65 GL_LESS 0.8 is true So 0.65 replaces 0.8 in the depth buffer Second primitive rasterizes same pixel Fragment s Z value is interpolated Resulting Z value is 0.72 Read the corresponding pixel s Z value Reads the value 0.65 Evaluate depth function 0.72 GL_LESS 0.65 is false So the fragment s depth value and color value are discarded

Depth Test Operation fragment depth pixel depth time 0.8 0.65 0.65<0.8 is true depth test passes 0.65 0.72 0.65<0.8 is false depth test fails 0.65

Depth Buffer Visualized Depth-tested 3D scene Z or depth values white = 1.0 (far), black = 0.0 (near)

OpenGL API for Depth Testing Simple to use Most applications just enable depth testing and hidden surfaces are removed Enable it: glenable(gl_depth_test) Disabled by default Must have depth buffer allocated for it to work Example: glutinitdisplaymode(glut_rgba GLUT_DOUBLE GLUT_DEPTH) More control Clearing the depth buffer glclear(gl_depth_buffer_bit otherbits) glcleardepth(zvalue) Initial value is 1.0, the maximum Z value in the depth buffer gldepthfunc(zfunc) zfunc is one of GL_LESS, GL_GREATER, GL_EQUAL, GL_GEQUAL, GL_LEQUAL, GL_ALWAYS, GL_NEVER, GL_NOTEQUAL Initial value is GL_LESS gldepthmask(boolean) True means write depth value if depth test passes; if false, don t write Initial value is GL_TRUE gldepthrange Maps NDC Z values to window-space Z values Initially [0,1], mapping to the entire available depth range

Not Just for View Occlusion Depth Buffers also Useful for Shadow Generation Without Shadows Projected Shadow Map With Shadows Light s View Light s View Depth

A Simplified Graphics Pipeline Application Vertex batching & assembly Clipping NDC to window space Write shaded color to color buffer Rasterization Fragment shading Depth testing Color update Depth buffer Framebuffer

Next Lecture Graphics Math, Transforms Interpolation, vector math, and number representations for computer graphics

Programming tips 3D graphics, whether OpenGL or Direct3D or any other API, can be frustrating You write a bunch of code and the result is Nothing but black window; where did your rendering go??

Things to Try Set your clear color to something other than black! It is easy to draw things black accidentally so don t make black the clear color But black is the initial clear color Did you draw something for one frame, but the next frame draws nothing? Are you using depth buffering? Did you forget to clear the depth buffer? Remember there are near and far clip planes so clipping in Z, not just X & Y Have you checked for glgeterror? Call glgeterror once per frame while debugging so you can see errors that occur For release code, take out the glgeterror calls Not sure what state you are in? Use glgetintegerv or glgetfloatv or other query functions to make sure that OpenGL s state is what you think it is Use glutswapbuffers to flush your rendering and show to the visible window Likewise glfinish makes sure all pending commands have finished Try reading http://www.slideshare.net/mark_kilgard/avoiding-19-common-opengl-pitfalls This is well worth the time wasted debugging a problem that could be avoided

Thanks Presentation approach and figures from David Luebke [2003] Brandon Lloyd [2007] Geometric Algebra for Computer Science [Dorst, Fontijne, Mann] via Mark Kilgard