Objectives. Programming with OpenGL Part 5: More GLSL. Program Object. Reading a Shader. Shader Reader. Linking Shaders with Application

Similar documents
Programming with OpenGL Part 5: More GLSL. Ed Angel Professor Emeritus of Computer Science University of New Mexico

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

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

Lecture 3. Programming with OpenGL GLUT + GLEW. CS 354 Computer Graphics Sunday, January 20, 13

Today s Agenda. Shaders fundamentals. Programming with shader-based OpenGL

Programming with OpenGL Part 3: Three Dimensions

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

Objectives. Open GL Shading Language (GLSL)

GLSL II. Objectives. Coupling GLSL to Applications Example applications

Computer Graphics (CS 4731) & 2D Graphics Systems

Objectives. Coupling GLSL to Applications Example applications

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

Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2)

CS 432 Interactive Computer Graphics

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

Programming with OpenGL Part 1: Background

Interaction. CSCI 420 Computer Graphics Lecture 3

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

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Building Models. Prof. George Wolberg Dept. of Computer Science City College of New York

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

Announcement. Homework 1 has been posted in dropbox and course website. Due: 1:15 pm, Monday, September 12

3D Game Programming Programmable Pipelines. Ming-Te Chi Department of Computer Science, National Chengchi University

Meshing and Geometry

CSE Real Time Rendering Week 3 & 4

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

Lecture 3 Advanced Computer Graphics (CS & SE )

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

Supplement to Lecture 22

Computer Graphics (CS 543) Lecture 1 (Part 2): Introduction to OpenGL/GLUT (Part 1)

Philip Calderon CSE 520 Lab 3 Color Shader

COMP371 COMPUTER GRAPHICS

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

Erik Anchondo cse 520 lab 4

1. Write a shader program that animates the morphing of Figure 'A' to Figure 'B' and back.

CSE 167: Introduction to Computer Graphics Lecture #7: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2016

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

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

2a. The triangles scale increases (expands) when the 'e' key is pressed and decreases (contracts) when the 'c' key is pressed.

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

COMPUTER GRAPHICS LAB # 3

Interaction. CSCI 480 Computer Graphics Lecture 3

Lecture 3. Understanding of OPenGL programming

Interaction Computer Graphics I Lecture 3

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

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

CS475/CS675 - Computer Graphics. OpenGL Drawing

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Computer Graphics (CS 4731) OpenGL/GLUT(Part 1)

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

Introduction to OpenGL: Part 2

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Computer Graphics (4731) Lecture 4: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

OPEN GL BACKGROUND. Objectives. Early History of APIs. SGI and GL. PHIGS and X. Modified from Angel 6e book slides

Programming using OpenGL: A first Introduction

Shaders. Slide credit to Prof. Zwicker

Drawing Primitives. OpenGL basics

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

OpenGL/GLUT Intro. Week 1, Fri Jan 12

CS 432 Interactive Computer Graphics

CS380: Computer Graphics Screen Space & World Space. Sung-Eui Yoon ( 윤성의 ) Course URL:

Computer Graphics. Bing-Yu Chen National Taiwan University

Computer Graphics (CS 4731) OpenGL/GLUT (Part 2)

GLSL 1: Basics. J.Tumblin-Modified SLIDES from:

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

Comp 410/510 Computer Graphics Spring Input & Interaction

An Overview GLUT GLSL GLEW

Fog example. Fog is atmospheric effect. Better realism, helps determine distances

Computer Graphics (CS 543) Lecture 4a: Linear Algebra for Graphics (Points, Scalars, Vectors)

Transformations. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Computer Graphics CS 543 Lecture 5 (Part 2) Implementing Transformations

OpenGL refresher. Advanced Computer Graphics 2012

Introduction to Computer Graphics. Hardware Acceleration Review

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

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

Objectives. transformation. General Transformations. Affine Transformations. Notation. Pipeline Implementation. Introduce standard transformations

Computer Graphics (Basic OpenGL)

CS452/552; EE465/505. Image Formation

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

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

The Graphics Pipeline

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

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

The Graphics Pipeline

WebGL A quick introduction. J. Madeira V. 0.2 September 2017

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

CS452/552; EE465/505. Image Processing Frame Buffer Objects

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

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

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

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

Lectures OpenGL Introduction

Introduction to Computer Graphics with OpenGL/GLUT

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

Using OpenGL with CUDA

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

LECTURE 02 OPENGL API

Computer graphics MN1

VR-programming tools (procedural) More VRML later in this course! (declarative)

Transcription:

Objectives Programming with OpenGL Part : More GLSL CS Interactive Computer Graphics Prof. David E. Breen Department of Computer Science Coupling shaders to applications - Reading - Compiling - Linking Vertex Attributes Setting up uniform variables Example applications Linking Shaders with Application Program Object Read shaders Compile shaders Create a program object Link everything together Link variables in application with variables in shaders - Vertex attributes - Uniform variables Container for shaders - Can contain multiple shaders - Other GLSL functions GLuint myprogobj; myprogobj = glcreateprogram(); /* define shader objects here */ gluseprogram(myprogobj); gllinkprogram(myprogobj); Reading a Shader Shader Reader Shaders are added to the program object and compiled Usual method of passing a shader is as a null-terminated string using the function glshadersource If the shader is in a file, we can write a reader to convert the file to a string #include <stdio.h> static char* readshadersource(const char* shaderfile) FILE* fp = fopen(shaderfile, "r"); if ( fp == NULL ) return NULL; fseek(fp, 0L, SEEK_END); long size = ftell(fp);

Shader Reader (cont) Adding a Vertex Shader fseek(fp, 0L, SEEK_SET); char* buf = new char[size + ]; fread(buf,, size, fp); buf[size] = '\0'; fclose(fp); GLuint vshader; GLchar vshaderfile[] = my_vertex_shader ; GLchar* vsource = readshadersource(vshaderfile); vshader = glcreateshader(gl_vertex_shader); glshadersource(vshader,, &vsource, NULL); glcompileshader(vshader); glattachshader(myprogobj, vshader); return buf; Go to InitShader.cpp Vertex Attributes Vertex Attribute Example Vertex attributes are named in the shaders Linker forms a table Application can get index from table and tie it to an application variable Similar process for uniform variables #define BUFFER_OFFSET( offset ) ((GLvoid*) (offset)) GLuint loc = glgetattriblocation( program, "vposition" ); glenablevertexattribarray( loc ); glvertexattribpointer( loc,, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); 9 0 Uniform Variable Example Double Buffering GLint angleparam; angleparam = glgetuniformlocation(myprogobj, "angle"); /* angle defined in shader */ Updating the value of a uniform variable opens the door to animating an application - Execute gluniform in display callback - Force a redraw through glutpostredisplay() /* my_angle set in application */ GLfloat my_angle; my_angle =.0 /* or some other value */ gluniformf(angleparam, my_angle); Need to prevent a partially redrawn frame buffer from being displayed Draw into back buffer Display front buffer Swap buffers after updating finished

Adding Double Buffering Idle Callback Request a double buffer - glutinitdisplaymode(glut_double) Swap buffers Idle callback specifies function to be executed when no other actions pending - glutidlefunc(myidle); void mydisplay() glclear( ); gldrawarrays(); glutswapbuffers(); void myidle() // recompute display glutpostredisplay(); Attribute and Varying Qualifiers Adding Color Starting with GLSL. attribute and varying qualifiers have been replaced by in and out qualifiers No changes needed in application Vertex shader example: #version. attribute vec vposition; varying vec color; #version. in vec vposition; out vec color; If we set a color in the application, we can send it to the shaders as a vertex attribute or as a uniform variable depending on how often it changes Let s associate a color with each vertex Set up an array of same size as positions Send to GPU as a buffer object Setting Colors Setting Up Buffer Object typedef vec color; color base_colors[] = color(.0, 0.0. 0.0),. color colors[numvertices]; vec points[numvertices]; //need larger buffer glbufferdata(gl_array_buffer, sizeof(points) + sizeof(colors), NULL, GL_STATIC_DRAW); //in loop setting positions //load data separately colors[i] = basecolors[color_index] position[i] =. glbuffersubdata(gl_array_buffer, 0, sizeof(points), points); glbuffersubdata(gl_array_buffer, sizeof(points), sizeof(colors), colors);

Second Vertex Array Coloring Each Vertex // vposition and vcolor identifiers in vertex shader loc = glgetattriblocation(program, vposition ); glenablevertexattribarray(loc); glvertexattribpointer(loc,, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); loc = glgetattriblocation(program, vcolor ); glenablevertexattribarray(loc); glvertexattribpointer(loc,, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points))); in vec vposition, vcolor; out vec color; void main() gl_position = vec(vposition, ); color = vcolor; 9 Coloring Each Fragment Drawing Data in Buffers in vec color; out vec fragcolor; void main() fragcolor = vec(color, ); void mydisplay() glclear( ); gldrawarrays(gl_triangles, ); glutswapbuffers(); Vertex Shader Applications Wave Motion Vertex Shader Moving vertices - Morphing - Wave motion - Fractals Lighting - More realistic models - Cartoon shaders in vec vposition; out vec color; uniform float xs, zs, // frequencies uniform float h; // height scale uniform float time; // time from app void main() vec t = vposition; t.y = vposition.y + h*sin(time + xs*vposition.x) + h*sin(time + zs*vposition.z); gl_position = t;

Particle System Vertex vs Fragment Lighting in vec vposition; uniform mat ModelViewProjectionMatrix; uniform vec vel; uniform float g, m, t; void main() vec object_pos; object_pos.x = vposition.x + vel.x*t; object_pos.y = vposition.y + vel.y*t + g/(.0*m)*t*t; object_pos.z = vposition.z + vel.z*t; gl_position = ModelViewProjectionMatrix*vec(object_pos,); per vertex lighting (Gouraud shading) per fragment lighting (Phong shading) 0 Fragment Shader Applications Texture mapping Procedural textures Programming with OpenGL Part : Three Dimensions environment mapping bump mapping Objectives Three-dimensional Applications In OpenGL, two-dimensional applications are a special case of three-dimensional graphics Going to D Develop a more sophisticated threedimensional example - Sierpinski gasket: a fractal Introduce hidden-surface removal - Not much changes - Use vec, gluniformf - Have to worry about the order in which primitives are rendered or use hidden-surface removal

Sierpinski Gasket (D) Example Start with a triangle Five subdivisions Connect bisectors of sides and remove central triangle Repeat The gasket as a fractal Gasket Program Consider the filled area (black) and the perimeter (the length of all the lines around the filled triangles) As we continue subdividing - the area goes to zero - but the perimeter goes to infinity #include <GL/glut.h> /* initial triangle */ point v[] =point(-.0, -0.), point(.0, -0.), point (0.0,.); This is not an ordinary geometric object - It is neither two- nor three-dimensional int n; /* number of recursive steps */ It is a fractal (fractional dimension) object Draw one triangle Triangle Subdivision static int i = 0; void triangle( point a, point b, point c) /* display one triangle */ points[i] = a; i++; points[i] = b; i++; points[i] = c; i++; 9 void divide_triangle(point a, point b, point c, int m) /* triangle subdivision using vertex numbers */ point ab, ac, bc; if(m>0) ab = (a + b )/; ac = (a + c)/; bc = (b + c)/; divide_triangle(a, ab, ac, m-); divide_triangle(c, ac, bc, m-); divide_triangle(b, bc, ac, m-); else(triangle(a,b,c)); /* draw triangle at end of recursion */ 0

display and init Functions main Function void display() glclear(gl_color_buffer_bit); gldrawarrays(gl_triangles, 0, NumVertices); glflush(); void myinit() vec v[] = point(.. divide_triangles(v[0], v[], v[], n);.. int main(int argc, char **argv) n=; glutinit(&argc, argv); glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowsize(00, 00); glutcreatewindow( D Gasket"); glutdisplayfunc(display); myinit(); glutmainloop(); Example Moving to D Five subdivisions We can easily make the program threedimensional by using point v[] and we start with a tetrahedron D Gasket Almost Correct We can subdivide each of the four faces Because the triangles are drawn in the order they are specified in the program, the front triangles are not always rendered in front of triangles behind them get this want this Appears as if we remove a solid tetrahedron from the center leaving four smaller tetrahedra Code almost identical to D example

Hidden-Surface Removal We want to see only those surfaces in front of other surfaces OpenGL uses a hidden-surface method called the z-buffer algorithm that saves depth information as objects are rendered so that only the front objects appear in the image Z-buffering Z-buffering (depth-buffering) is a visible surface detection algorithm Implementable in hardware and software Requires data structure (z-buffer) in addition to frame buffer. Z-buffer stores values [0.. ZMAX] corresponding to depth of each point. If the point is closer than one in the buffers, it will replace the buffered values Z-buffering Z-buffering w/ front/back clipping 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + + = = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 for (y = 0; y < YMAX; y++) for (x = 0; x < XMAX; x++) F[x][y] = BACKGROUND_VALUE; Z[x][y] = -; /* Back value in NPC */ for (each polygon) for (each pixel in polygon s projection) pz = polygon s z-value at pixel coordinates (x,y) if (pz < FRONT && pz > Z[x][y]) /* New point is behind front plane & closer than previous point */ Z[x][y] = pz; F[x][y] = polygon s color at pixel coordinates (x,y) 9 99 Foley/VanDam/Finer/Huges/Phillips ICG 0 Using the z-buffer algorithm It must be - Requested in main.c glutinitdisplaymode (GLUT_SINGLE GLUT_RGB GLUT_DEPTH) - Enabled in init.c glenable(gl_depth_test) - Cleared in the display callback glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT) Surface vs Volume Subdvision In our example, we divided the surface of each face We could also divide the volume using the same midpoints The midpoints define four smaller tetrahedrons, one for each vertex Keeping only these tetrahedrons removes a volume in the middle See text for code

Volume Subdivision 9