Waves, Displacement, Reflections. Arun Rao CS 594

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

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

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

Objectives. Coupling GLSL to Applications Example applications

Objectives. Open GL Shading Language (GLSL)

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

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

Introduction to the OpenGL Shading Language (GLSL)

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

Programmable Graphics Hardware

Supplement to Lecture 22

12.2 Programmable Graphics Hardware

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

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

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

GLSL II. Objectives. Coupling GLSL to Applications Example applications

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

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

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

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

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

Programming shaders & GPUs Christian Miller CS Fall 2011

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

Lab 9 - Metal and Glass

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

Shader Programming 1. Examples. Vertex displacement mapping. Daniel Wesslén 1. Post-processing, animated procedural textures

CS559 Computer Graphics Fall 2015

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

Stored Texture Shaders

Computer Graphics (CS 543) Lecture 8c: Environment Mapping (Reflections and Refractions)

1 of 5 9/10/ :11 PM

GPU Programming EE Final Examination

GPU Programming EE Final Examination

Tuesday, 23 March OpenGL Shading Language

GPU Programming EE Final Examination

Stripes, Rings, and Dots!

GPU Programming EE Midterm Examination

Review of Thursday. xa + yb xc + yd

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

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

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

GPU Programming EE Final Examination

GLSL. OpenGL Shading Language. OpenGL 1.5 Logical Diagram. OpenGL 2.0 Logical Diagram

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book Also take CS 4731 Computer Graphics

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

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book Also take CS 4731 Computer Graphics

Bump Mapping. Mike Bailey. What is Bump-Mapping?

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

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

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

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

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

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

CS 4620 Midterm, October 23, 2018 SOLUTION

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

Bump Mapping. Mike Bailey. Oregon State University. What is Bump-Mapping?

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

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

Sign up for crits! Announcments

Zeyang Li Carnegie Mellon University

Lecture 09: Shaders (Part 1)

CMPS160 Shader-based OpenGL Programming

OUTLINE. Tessellation in OpenGL Tessellation of Bezier Surfaces Tessellation for Terrain/Height Maps Level of Detail

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

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

GPU Programming EE Midterm Examination

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book IMGD 4000 (D 10) 1

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

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

Optimal Shaders Using High-Level Languages

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

Shaders. Slide credit to Prof. Zwicker

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

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

Using Shaders for Lighting

Using Shaders for Lighting

OPENGL RENDERING PIPELINE

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

Real-time Graphics 3. Lighting, Texturing

11 - Bump Mapping. Bump-Mapped Objects. Bump-Mapped Objects. Bump-Mapped Objects. Limitations Of Texture Mapping. Bumps: Perturbed Normals

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

Overview. By end of the week:

Computer Graphics Coursework 1

SUMMARY. CS380: Introduction to Computer Graphics Projection Chapter 10. Min H. Kim KAIST School of Computing 18/04/12. Smooth Interpolation

COMP3421. Particle Systems, Rasterisation

CS 418: Interactive Computer Graphics. Basic Shading in WebGL. Eric Shaffer

CS452/552; EE465/505. Intro to Lighting

CS 130 Final. Fall 2015

CS452/552; EE465/505. Lighting & Shading

SUMMARY. CS380: Introduction to Computer Graphics Texture Mapping Chapter 15. Min H. Kim KAIST School of Computing 18/05/03.

Real-time Graphics 6. Reflections, Refractions

OpenGL Programmable Shaders

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

Canonical Shaders for Optimal Performance. Sébastien Dominé Manager of Developer Technology Tools

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

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

3D Viewing Episode 2

CMSC427 Final Practice v2 Fall 2017

Graphics Processing Units (GPUs) V2.2 (January 2013) Jan Kautz, Anthony Steed, Tim Weyrich

Transcription:

Waves, Displacement, Reflections Arun Rao CS 594

Goals Simple looking waves Waves displace and change orientation of things on top Reflections on surface Partially see through

How do we create the Waves? Large Mesh 120 x 120 quads of size 1 Vertex Shader use sine functions needs to change based on time

Waves.vert ver. 1 uniform float const float const float const float const float Time; xdiv = 10.0; // scalar applied to gl_vertex.x zdiv = 8.0; // scalar applied to gl_vertex.y magx = 1.6; // magnitude of wave coming in x magz = 1.0; // magnitude of wave coming in z void main(void) { float h1, h2; // determine current position h1 = sin((gl_vertex.z / zdiv) + Time) * magz; h2 = sin((gl_vertex.x / xdiv) + Time) * magx; h1 = h1 + h2; } // apply position gl_position = gl_modelviewprojectionmatrix * vec4(gl_vertex.x, gl_vertex.y + h1, gl_vertex.z,1);

What about Lighting? Assuming we ve setup lighting correctly We ll do lighting per-fragment Need to calculate surface normal light position, diffuse and ambient material setup Take partial derivative of equation in x and z directions d/dx( sin(u) ) = cos(u) * d/dx(u) Take cross product of two vectors

Calculate normal varying vec3 normal...... vec3 v1; // along +z axis vec3 v2; // along +x axis Code? // get our two slopes in xy and zy // d( sin(u) ) = cos(u)d(u) v1 = vec3( 0.0, cos((gl_vertex.z / zdiv) + Time) * magz / zdiv, 1.0); v2 = vec3( 1.0, cos((gl_vertex.x / xdiv) + Time) * magx / xdiv, 0.0); // find our normal, it will definitely point up! normal = normalize(cross(v1,v2));

Code? varying vec3 lightdir, halfvector; varying vec4 diffuse, ambient;... normal = normalize(gl_normalmatrix * normal); lightdir = normalize(vec3(gl_lightsource[1].position)); halfvector = normalize(gl_lightsource[1].halfvector.xyz); diffuse = gl_frontmaterial.diffuse * gl_lightsource[1].diffuse; ambient = gl_frontmaterial.ambient * gl_lightsource[1].ambient; ambient += gl_lightmodel.ambient * gl_frontmaterial.ambient;

Fragment Shader Code? void main() { vec3 n,halfv; float NdotL,NdotHV; /* The ambient term will always be present */ vec4 color = ambient; /* a fragment shader can't write a varying variable, hence we need a new variable to store the normalized interpolated normal */ n = normalize(normal); /* compute the dot product between normal and ldir */ NdotL = max(dot(n,lightdir),0.0); if (NdotL > 0.0) { color += diffuse * NdotL; halfv = normalize(halfvector); NdotHV = max(dot(n,halfv),0.0); color += gl_frontmaterial.specular * gl_lightsource[0].specular * pow(ndothv, gl_frontmaterial.shininess); } gl_fragcolor = color; }

Awesome!!!

How do we move our objects? Incorporate same code as above into our other shaders as a function Pick a point to apply displacement and change of normals I cheat and use fixed point because of later on

Loosing direction!! We need an object rotating in place Push Matrix Position Rotate Apply Displacement & Orientation via Vertex Shader Pop Matrix PROBLEMS!!!!!

Order Matters!! Wave function applied to objects cause: normal matrix to be wrong lighting to get screwed up doesn t look like wading through water Hacky fix by doing transforms in shader and rebuilding normal matrix per-vertex!!!

What do we do in our vertex shaders? Copy the ModelView Matrix & Normal Matrix (mvmat & nmat) Run the Wave function and get our vertical displacment, normal to surface and basis matrix for rotation Perform translate then rotate on copy of ModelView Matrix Apply spin about RELATIVE y-axis to ModelView Matrix Rebuild Normal Matrix calculate varying normal to pass to fragment shader gl_position = gl_projectionmatrix * mvmat * v

vec3 testposition = vec3( 0.0, 0.0, -10 ); mat4 wave_rot_mat;... void wave_func( inout vec3 tp, inout vec3 tn) { vec3 v1, v2, pos1, pos2; float h1, h2; // determine current height displacement h1 = sin((testposition.z / zdiv) + Time) * magz; h2 = sin((testposition.x / xdiv) + Time) * magx; tp = vec3( 0.0, h1 + h2, 0.0); // only take y component // get our two slopes in xy and zy // d/dx( sin(u) ) = cos(u)d/dx(u) // v1 with respect to z // v2 with respect to x v1 = vec3( 0.0, cos((testposition.z / zdiv) + Time) * magz / zdiv, 1.0); v2 = vec3( 1.0, cos((testposition.x / xdiv) + Time) * magx / xdiv, 0.0); } // find our normal, should be in positive y direction v1 = normalize(v1); v2 = normalize(v2); tn = normalize(cross(v1,v2)); // should have our basis vectors!! v1, v2, tn... now build a matrix wave_rot_mat = identity; wave_rot_mat[0] = vec4( v2, 0.0); wave_rot_mat[1] = vec4( tn, 0.0); wave_rot_mat[2] = vec4( v1, 0.0);

void main(void) { vec3 tp = vec3(0.0,0.0,0.0); vec3 tn = vec3(0.0,0.0,0.0); mat4 mvmat = gl_modelviewmatrix; mat3 nmat = gl_normalmatrix; // apply wave displacement wave_func(tp,tn); // translation mat4 tempmat = identity; tempmat[3][0] = testposition.x; tempmat[3][1] = tp.y; tempmat[3][2] = testposition.z; mvmat *= tempmat; // apply rotation due to normal at wave mvmat *= wave_rot_mat;...

uniform float Spin; // a rotation value sent in... mat3 nmat = gl_normalmatrix;... // did our translate and rotate due to wave... // apply rotation about y-axis tempmat = identity; tempmat[0][0] = cos(spin); tempmat[2][0] = sin(spin); tempmat[0][2] = -sin(spin); tempmat[2][2] = cos(spin); tempmat[3][1] = CubeScale; mvmat *= tempmat; // rebuild normal matrix build_norm_mat(mvmat,nmat); // we happen to know the scale of the cube below us, // let's reuse tp tp = gl_vertex.xyz; normal = normalize( nmat * gl_normal ); gl_position = gl_projectionmatrix * mvmat * vec4(tp,1);

How do we build the normal matrix? Need the upper-left 3x3 of ModelView Matrix Invert need determinant Transpose Remember (A -1 ) T = (A T ) -1

Inverse of 3x3 Matrix

Transpose of 3x3 Matrix

Do them both at the same time! mat3 tnmat; float d; for( int i = 0; i < 3; i++) for( int k = 0; k < 3; k++) nmat[i][k] = mvmat[i][k]; d = determinant(nmat); // take determinant and transpose at same time tnmat[0][0] = determinant(mat2(nmat[1][1], nmat[1][2], nmat[2][1], nmat[2][2])); tnmat[1][0] = determinant(mat2(nmat[0][2], nmat[0][1], nmat[2][2], nmat[2][1])); tnmat[2][0] = determinant(mat2(nmat[0][1], nmat[0][2], nmat[1][1], nmat[1][2])); tnmat[0][1] = determinant(mat2(nmat[1][2], nmat[1][0], nmat[2][2], nmat[2][1])); tnmat[1][1] = determinant(mat2(nmat[0][0], nmat[0][2], nmat[2][1], nmat[2][2])); tnmat[2][1] = determinant(mat2(nmat[1][2], nmat[0][0], nmat[1][2], nmat[1][0])); tnmat[0][2] = determinant(mat2(nmat[1][0], nmat[1][1], nmat[2][0], nmat[2][1])); tnmat[1][2] = determinant(mat2(nmat[0][1], nmat[0][0], nmat[2][1], nmat[2][0])); tnmat[2][2] = determinant(mat2(nmat[0][0], nmat[0][1], nmat[1][0], nmat[1][1])); nmat = tnmat / d;

Determinant Functions float determinant(mat3 m) { float t = 0.0; t = m[0][0] * ((m[1][1] * m[2][2]) - (m[2][1] * m[1][2])); t+= m[0][1] * ((m[1][2] * m[2][0]) - (m[2][2] * m[1][0])); t+= m[0][2] * ((m[1][0] * m[2][1]) - (m[2][0] * m[1][1])); } return t; float determinant(mat2 m) { return ((m[0][0] * m[1][1]) - (m[1][0] * m[0][1])); }

More Awesome!!!

Still need reflections! Found a cubemap online Did the GL_TEXTURE_CUBE_MAP bindings Access of bound cubemap in shader by texture unit not texture object id! Need eye direction and normal of surface Perform texture lookup in frag. shader

Need The Eye Direction Vector from Vertex Shader varying vec3 eyedir;... float h1, h2;... // determine current position h1 = sin((gl_vertex.z / zdiv) + Time) * magz; h2 = sin((gl_vertex.x / xdiv) + Time) * magx; h1 = h1 + h2; vec4 p = vec4( gl_vertex.x, gl_vertex.y + h1, gl_vertex.z, 1);... eyedir = vec3( gl_modelviewmatrix * p);...

In the Frag Shader uniform samplercube CubeMapTex; varying vec3 normal, lightdir, halfvector, eyedir; varying vec4 diffuse,ambient; void main(void) { float NdotL, NdotHV; vec4 color = ambient; vec3 n = normalize(normal); NdotL = max(dot(n,lightdir),0.0); if( NdotL > 0.0 ) { color += diffuse * NdotL; NdotHV = max( dot( n, normalize(halfvector)), 0.0); color += gl_frontmaterial.specular * gl_lightsource[1].specular * pow(ndothv, gl_frontmaterial.shininess); } } // our cube map forces us to make the reflectdir negative... // mainly cause of our texture resources... anyway :) vec3 reflectdir = -reflect( eyedir, normal); vec4 texturecolor = texturecube( CubeMapTex, normalize(reflectdir)); gl_fragcolor = vec4( vec3(mix( color, texturecolor,.6)),.8);