CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

Similar documents
glviewport(0, 0, w, h); h = 1;

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Lecture 09: Shaders (Part 1)

Philip Calderon CSE 520 Lab 3 Color Shader

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

Supplement to Lecture 22

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Waves, Displacement, Reflections. Arun Rao CS 594

Erik Anchondo cse 520 lab 4

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

Some advantages come from the limited environment! No classes. Stranight ans simple code. Remarkably. Avoids most of the bad things with C/C++.

Objectives. Coupling GLSL to Applications Example applications

Objectives. Open GL Shading Language (GLSL)

Introduction to the OpenGL Shading Language (GLSL)

Programming shaders & GPUs Christian Miller CS Fall 2011

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

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

The Basic Computer Graphics Pipeline, OpenGL-style. Introduction to the OpenGL Shading Language (GLSL)

- Surface Rendering -

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

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

Lab 9 - Metal and Glass

The Transition from RenderMan to the OpenGL Shading Language (GLSL)

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

GLSL: Creating GLSL Programs - Overview

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

Programmable Graphics Hardware

1)Write a shader program that renders a regular pentagon with textured image like the one shown below.

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

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

12.2 Programmable Graphics Hardware

Programmable shader. Hanyang University

Shader Programming. Daniel Wesslén, Stefan Seipel, Examples

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

EECS 487: Interactive Computer Graphics

GLSL II. Objectives. Coupling GLSL to Applications Example applications

Objectives Shading in OpenGL. Front and Back Faces. OpenGL shading. Introduce the OpenGL shading methods. Discuss polygonal shading

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

An Introduction to 3D Computer Graphics, Stereoscopic Image, and Animation in OpenGL and C/C++ Fore June

Stripes, Rings, and Dots!

CGT520 Lighting. Lighting. T-vertices. Normal vector. Color of an object can be specified 1) Explicitly as a color buffer

Letterkenny Institute of Technology

CS559 Computer Graphics Fall 2015

Computer Graphics (CS 4731) & 2D Graphics Systems

OpenGL Programmable Shaders

-=Bui Tuong Phong's Lighting=- University of Utah, but with shaders. Anton Gerdelan Trinity College Dublin

Computer Graphics Lighting

Computer Graphics Lighting. Why Do We Care About Lighting?

Shaders. Slide credit to Prof. Zwicker

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Computer Graphics with OpenGL ES (J. Han) Chapter 6 Fragment shader

GLSL v1.20. Scott MacHaffie Schrödinger, Inc.

Computer Graphics (CS 543) Lecture 8a: Per-Vertex lighting, Shading and Per-Fragment lighting

Shaders. Introduction. OpenGL Grows via Extensions. OpenGL Extensions. OpenGL 2.0 Added Shaders. Shaders Enable Many New Effects

Yazhuo Liu Homework 3

Computer Graphics Coursework 1

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

GPU Programming EE Final Examination

GPU Programming EE Final Examination

GPU Programming EE Final Examination

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

GPU Programming EE Midterm Examination

To Do. Demo for mytest3. Methodology for Lecture. Importance of Lighting. Brief primer on Color. Foundations of Computer Graphics (Spring 2012)

[175 points] The purpose of this assignment is to give you practice with shaders in OpenGL.

Stored Texture Shaders

Lighting and Shading II. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

The GLSL API. Mike Bailey. Oregon State University. Geometry Shader. Program. The GLSL Shader-creation Process. create. compile. Vertex.

Today. Texture mapping in OpenGL. Texture mapping. Basic shaders for texturing. Today. Computergrafik

Ambient and Diffuse Light

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

Using Shaders for Lighting

Lets assume each object has a defined colour. Hence our illumination model is looks unrealistic.

CMPS160 Shader-based OpenGL Programming. All slides originally from Prabath Gunawardane, et al. unless noted otherwise

Today. Rendering - III. Outline. Texturing: The 10,000m View. Texture Coordinates. Specifying Texture Coordinates in GL

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

Best practices for effective OpenGL programming. Dan Omachi OpenGL Development Engineer

Using Shaders for Lighting

Programmable Graphics Hardware

CMPS160 Shader-based OpenGL Programming

EDAF80 Introduction to Computer Graphics. Seminar 3. Shaders. Michael Doggett. Slides by Carl Johan Gribel,

GPU Programming EE Final Examination

Real-time Graphics 3. Lighting, Texturing

Reading Material. Vertex- and Fragment Shaders. Pipeline. Vertex Shader. Primitive Assembly. Input data can be. Produces

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

1 of 5 9/10/ :11 PM

Preparing for Texture Access. Stored Texture Shaders. Accessing Texture Maps. Vertex Shader Texture Access

Computer Graphics. Illumination and Shading

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

Graphics Processing Units (GPUs) V1.2 (January 2010) Outline. High-Level Pipeline. 1. GPU Pipeline:

The Rasterization Pipeline

OpenGL shaders and programming models that provide object persistence

CS 380 Introduction to Computer Graphics. LAB (1) : OpenGL Tutorial Reference : Foundations of 3D Computer Graphics, Steven J.

Sign up for crits! Announcments

Review of Thursday. xa + yb xc + yd

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

Illumination & Shading I

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

Introduction to Computer Graphics. Hardware Acceleration Review

Transcription:

CSE 4431/5331.03M Advanced Topics in 3D Computer Graphics TA: Margarita Vinnikov mvinni@cse.yorku.ca

What do we need to do to use shaders in our program?

Shaders Work flow For multiple shaders 1. Create program 2. Attach shader object to appropriate program 3. Link the shader 4. Verify the shader program 5. Verify that link was successful 6. Use the shader for vertex or fragment processing http://www.lighthouse3d.com/tutorials/glsl-tutorial/creating-a-program/

void setshaders() char *vs = NULL,*fs = NULL,*fs2 = NULL; //Create program v = glcreateshader(gl_vertex_shader); f = glcreateshader(gl_fragment_shader); //read shaders source code vs = textfileread("toon.vert"); fs = textfileread("toon.frag"); const char * ff = fs; const char * ff2 = fs2; //connect with the source glshadersource(v, 1, &vv,null); glshadersource(f, 1, &ff,null); free(vs);free(fs); glcompileshader(v); glcompileshader(f); p = glcreateprogram(); glattachshader(p,f); glattachshader(p,f2); gllinkprogram(p); gluseprogram(p); /// activate the program when rendering http://www.lighthouse3d.com/tutorials/glsl-tutorial/creating-a-program/ GLuint v,f,f2,p; glewinit() setshaders(); glutmainloop(); char *textfileread(char *fn) FILE *fp; char *content = NULL; int count=0; if (fn!= NULL) fp = fopen(fn,"rt"); if (fp!= NULL) fseek(fp, 0, SEEK_END); count = ftell(fp); rewind(fp); if (count > 0) content = (char*)malloc(sizeof (char) * (count+1)); count = fread(content,sizeof (char),count,fp); content[count] = '\0'; fclose(fp); return content;

Debugging Shaders void printshaderinfolog(gluint obj) void setshaders() int infologlength = 0; int charswritten = 0; char *infolog; glgetshaderiv(obj, GL_INFO_LOG_LENGTH, &infologlength); glcompileshader(v); glcompileshader(f); //verify successful compilation printshaderinfolog(vshader); printshaderinfolog(fshader); if (infologlength > 0) infolog = (char *)malloc(infologlength); glgetshaderinfolog(obj, infologlength, &charswritten, infolog); printf("%s\n",infolog); free(infolog);

Debugging Sahaders void setshaders() gllinkprogram(p); printprograminfolog(p); void printprograminfolog(gluint obj) int infologlength = 0; int charswritten = 0; char *infolog; glgetprogramiv(obj, GL_INFO_LOG_LENGTH,&infologLength); if (infologlength > 0) infolog = (char *)malloc (infologlength); glgetprograminfolog(obj, infologlength, &charswritten, infolog); printf("%s\n",infolog); free(infolog);

Debugging Shaders void setshaders() gllinkprogram(p); if (checklinkedprogram(program) gluseprogramobject(program); GLint checklinkedprogram(gluint program) GLint linked; glgetshaderiv(program, GL_LINK_STATUS, &linked); if (!linked) GLint length; GLchar * log; glgetshaderiv(program, GL_INFO_LOG_LENGTH, &length); log = (GLchar *)malloc(length); glgetshaderinfolog(program, length, &length, log); printf("linked log = '%s \n", log); return linked;

void gldeleteshader(gluint id); void gldeleteprogram(gluint id);

What do we do with shaders?

Simple Vertex Shader void main() gl_position = gl_projectionmatrix * gl_modelviewmatrix * gl_vertex; OR void main() gl_position = gl_modelviewprojectionmatrix * gl_vertex; OR void main() gl_position = ftransform();

Simple Fragment Shader void main() gl_fragcolor = vec4(0.4,0.4,0.8,1.0); OR void main() gl_fragcolor = gl_color;

Flatten Vertex Shader To flatten a 3D model, set z coordinate to zero prior to applying the modelview transformation. void main() vec4 v = vec4(gl_vertex); v.z = 0.0; gl_position = gl_modelviewprojectionmatrix * v;

Cartoon Shader Large areas of constant color with sharp transitions between them. Vertex Shader: varying float intensity; void main() vec3 ld; vec3 lightdir = vec3(1,0,0); intensity = dot(lightdir,gl_normal); gl_position = ftransform();

Cartoon Shader Fragment Shader: varying float intensity; void main() vec4 color; if (intensity > 0.95) color = vec4(1.0,0.5,0.5,1.0); else if (intensity > 0.5) color = vec4(0.6,0.3,0.3,1.0); else if (intensity > 0.25) color = vec4(0.4,0.2,0.2,1.0); else color = vec4(0.2,0.1,0.1,1.0); gl_fragcolor = color;

Use the code to read and instantiate shaders Implement simple shaders Implement flatten shader Change the vertices such that z = sin(5.0*x )*0.25 Bonus: come up with an interesting perturbation Implement cartoon shader Modify light vector and intensity gradation in an interesting way

Vertex animation Keep track of time, or a frame counter But a vertex shader can t keep track of values between vertices, let alone between frames. define time variable in the OpenGL application, pass it to the shader as a uniform variable. In setup: loc = glgetuniformlocation(p,"time"); void renderscene(void) gluniform1f(loc, time); //draw your model glutsolidteapot(1);

Modify your code so that you animate either the flatten shader or cartoon shader Bonus: do both Bonus: make creative animation

Do you have a Virtual Light Source? point source - shines light equally in all directions. It needs: a 3d world position rgb colours L s, for its specular light colour L d, for its diffuse light colour L a, for its ambient light colour You can create these 4 variables directly in your shader, but if the light needs to move or change colour then you should use uniform variables instead.

Do you have Surface Properties? Every object in your scene should have some surface properties. These properties define the factor of each lighting approximation that should be reflected by the surface. your object can have its own, unique 3d world position rgb colours K s, for its specular reflectance factor rough surface should have a very low specular reflectance factor A shiny, smooth, surface will have a high specular reflectance factor K d, for its diffuse reflectance factor The diffuse reflectance factor is usually used to give the object an unique, base colour K a, for its ambient reflectance factor a surface normal; the facing direction of the surface

Check Point Did you implement light source in your code? You can rotate the light source Do you have surface properties setup for your models? If not see additional material for the lab. I expect light implemented when you submit the lab.

Quiz Ambient Ambient + Diffuse Specular Point Light Spot Light

Diffuse Light 1. The normal vector and the light direction vector (gl_lightsource[0].position) has to be normalized I0 is the reflected intensity, Ld is the light's diffuse color (gl_lightsource[0].diffuse), Md is the material's diffuse coefficient (gl_frontmaterial.diffuse). 2. Cos(θ) = lightdir. normal / ( lightdir * normal ) 3. Note that we want gl_normal and lightdir to be normalized normal = 1 lightdir = 1 So that Cos(θ) = lightdir. normal 4. OpenGL stores the lights direction in eye space coordinates; hence we need to transform the normal to eye space in order to compute the dot product.

Eye Space To transform the normal to eye space use the gl_normalmatrix. This matrix is the transpose of the inverse of the 3x3 upper left sub matrix from the modelview matrix. varying vec3 normal; void main() normal = gl_normalmatrix * gl_normal; ;

Ambient Light Ia is the ambient intensity, Ma is the global ambent (gl_lightmodel.ambient) La is the light's ambinent color (gl_lightsource[0].ambient), Ma is the material's ambient coefficient (gl_frontmaterial.ambient),

Specular Lights

Your Turn Follow instructions attached implement light shaders Bonus: create an interesting light shader

Write you name and student number in comments at the top of your code Duplicate your.cpp Name it lab3_first_last.cpp Submit your code as follows: submit 4431 lab1 filename(s)

http://ogldev.atspace.co.uk/ http://www.lighthouse3d.com/tutorials/glsl-tutorial/