diff --git a/README.md b/README.md index f22c764..65967f9 100644 --- a/README.md +++ b/README.md @@ -3,190 +3,140 @@ CIS565: Project 6: Deferred Shader ------------------------------------------------------------------------------- Fall 2013 ------------------------------------------------------------------------------- -Due Friday 11/15/2013 -------------------------------------------------------------------------------- ------------------------------------------------------------------------------- NOTE: ------------------------------------------------------------------------------- -This project requires any graphics card with support for a modern OpenGL -pipeline. Any AMD, NVIDIA, or Intel card from the past few years should work -fine, and every machine in the SIG Lab and Moore 100 is capable of running -this project. +This project requires an NVIDIA graphics card with CUDA capability! +Any card with CUDA compute capability 1.1 or higher will work fine for this project. ------------------------------------------------------------------------------- INTRODUCTION: ------------------------------------------------------------------------------- -In this project, you will get introduced to the basics of deferred shading. You will write GLSL and OpenGL code to perform various tasks in a deferred lighting pipeline such as creating and writing to a G-Buffer. +This project uses deferred shading in OpenGL using G-Buffers. +The key advantage of deferred shading is that it makes it much easier to manage +your shaders, especially when you have loads of materials and lights. ------------------------------------------------------------------------------- -CONTENTS: +FEATURES: ------------------------------------------------------------------------------- -The Project6 root directory contains the following subdirectories: - -* base/ - * PROJ_WIN/ contains the vs2010 project files - * PROJ_NIX/ contains makefile for building (tested on ubuntu 12.04 LTS) - * res/ contains resources including shader source and obj files - * src/ contains the c++ code for the project along with SOIL and tiny_obj_loader -* shared32/ contains freeglut, glm, and glew. ---- -BASE CODE TOUR ---- +- Use of G Buffers - Depth, Normal, Color, Eye space position, Shininess +- Blinn Phong lighting model +- Post process effects like bloom and toon shading +- Support for efficient rendering of multiple point lights using scissor testing in OpenGL -Most of your edits will be confined to the various fragment shader programs and main.cpp. +------------------------------------------------------------------------------- +RESULTS: +------------------------------------------------------------------------------- +*BLINN PHONG* : +![alt tag](https://raw.github.com/vimanyu/Project6-DeferredShader/master/renders/blinnPhong.png) -Some methods worth exploring are: +*TOON SHADING*: +![alt tag](https://raw.github.com/vimanyu/Project6-DeferredShader/master/renders/toonShading.png) -[initShader](https://github.com/CIS565-Fall-2013/Project6-DeferredShader/blob/master/base/src/main.cpp#L223): -This method initializes each shader program from specified source files. Note that the source name is declared inside a `#ifdef WIN32` guard block. This is done to reflect the relative directory structure between the linux and windows versions of the code. +*BLOOM*: +![alt tag](https://raw.github.com/vimanyu/Project6-DeferredShader/master/renders/bloom.png) -[initFBO](https://github.com/CIS565-Fall-2013/Project6-DeferredShader/blob/master/base/src/main.cpp#L360): -This method initializes the framebuffer objects used as render targets for the first and second stage of the pipeline. When you go to add another slot to the G buffer you will need to modify to first FBO accordingly. Try finding all the places where `colorTexture` is used (ctrl+F in windows will be helpful) and look at how textures are created, freed, added to the FBO, and assigned to the appropriate shader programs before adding your own. Also keep in mind that textures can be reused as inputs in other pipeline stages, for instance you might want access to the normals both in the lighting stage and in the post process stage. -[draw_mesh](https://github.com/CIS565-Fall-2013/Project6-DeferredShader/blob/master/base/src/main.cpp#L574), -[draw_quad](https://github.com/CIS565-Fall-2013/Project6-DeferredShader/blob/master/base/src/main.cpp#L647), -[draw_light](https://github.com/CIS565-Fall-2013/Project6-DeferredShader/blob/master/base/src/main.cpp#L657): -These methods render the scene geometry, viewing quad, and point light quad to the screen. The draw_light method in particular is interesting because it will set up the scissor window for efficient rendering of point lights. +------------------------------------------------------------------------------- +VIDEO +------------------------------------------------------------------------------- +The following is a video of the deferred shading in action -[display](https://github.com/CIS565-Fall-2013/Project6-DeferredShader/blob/master/base/src/main.cpp#L742): -This is where the graphical work of your program is done. The method is separated into three stages with the majority of work being done in stage 2. +[![ScreenShot](https://raw.github.com/vimanyu/Project6-DeferredShader/master/renders/deferredShader_video_screenshot.png)](http://www.youtube.com/watch?v=MMZ0mmsKyqM) -Stage 1 renders the scene geometry to the G-Buffer -* pass.vert -* pass.frag +------------------------------------------------------------------------------- +BUILDING AND RUNNING CODE +------------------------------------------------------------------------------- +The code has been tested on Visual Studio 2012/2010 and cuda 5.5 on a laptop with compute capability 1.0 as well as 3.0. -Stage 2 renders the lighting passes and accumulates to the P-Buffer -* shade.vert -* ambient.frag -* point.frag -* diagnostic.frag +Keyboard bindings for interactivity: -Stage 3 renders the post processing -* post.vert -* post.frag - -[keyboard](https://github.com/CIS565-Fall-2013/Project6-DeferredShader/blob/master/base/src/main.cpp#L870): -This is a good reference for the key mappings in the program. -WASDQZ - Movement -X - Toggle scissor test -R - Reload shaders -1 - View depth -2 - View eye space normals -3 - View Diffuse color -4 - View eye space positions -5 - View lighting debug mode -0 - Standard view +Key|Action +---|--- +'1'| View depth +'2'| View eye space normals +'3'| View diffuse Color +'4'| View eye space positions +'5'| View lighting debug mode +'6'| View shininess +'7'| View toon shading +'8'| View bloom effect +'0'| Standard view +'r'| Reset camera +'x'| Toggle OpenGL Scissors + +Apart from this, WSADQZ and mouse can be used for movement. ------------------------------------------------------------------------------- -REQUIREMENTS: +PERFORMANCE ANALYSIS 1: Packed G Buffer ------------------------------------------------------------------------------- +There is another branch on github named "PackedGBuffer" in which I have packed my G-Buffer in a different manner. -In this project, you are given code for: -* Loading .obj files -* Rendering to a minimal G buffer: - * Depth - * Normal - * Color - * Eye space position -* Rendering simple ambient and directional lighting to texture -* Example post process shader to add a vignette +The main difference lies in the way normals have been packed. -You are required to implement: -* Either of the following effects - * Bloom (feel free to use [GPU Gems](http://http.developer.nvidia.com/GPUGems/gpugems_ch21.html) as a rough guide) - * "Toon" Shading (with basic silhouetting) -* Point light sources -* An additional G buffer slot and some effect showing it off +In the **original G-buffer**, there were separate G-Buffers for normals and shininess -**NOTE**: Implementing separable convolution will require another link in your pipeline and will count as an extra feature if you do performance analysis with a standard one-pass 2D convolution. The overhead of rendering and reading from a texture _may_ offset the extra computations for smaller 2D kernels. +Normals buffer: -You must implement two of the following extras: -* The effect you did not choose above -* Screen space ambient occlusion -* Compare performance to a normal forward renderer with - * No optimizations - * Coarse sort geometry front-to-back for early-z - * Z-prepass for early-z -* Optimize g-buffer format, e.g., pack things together, quantize, reconstruct z from normal x and y (because it is normalized), etc. - * Must be accompanied with a performance analysis to count -* Additional lighting and pre/post processing effects! (email first please, if they are good you may add multiple). +Component|Data +---|--- +Component 1| normal.x +Component 2| normal.y +Component 3| normal.z -------------------------------------------------------------------------------- -README -------------------------------------------------------------------------------- -All students must replace or augment the contents of this Readme.md in a clear -manner with the following: +Shininess buffer: + +Component|Data +---|--- +Component 1| shininess + +In the **packed G-Buffer**, the normals G-Buffer has + +Component|Data +---|--- +Component 1| normal.x +Component 2| normal.y +Component 3| shininess + +For this, the third component of the normal was made on the fly using thie formula, -* A brief description of the project and the specific features you implemented. -* At least one screenshot of your project running. -* A 30 second or longer video of your project running. To create the video you - can use http://www.microsoft.com/expression/products/Encoder4_Overview.aspx -* A performance evaluation (described in detail below). +``` +normal.z = sqrt(1- normal.x*normal.x - normal.y*normal.y); +``` + +Model|Lighting|Without packed G-Buffer(fps)|With packed G-Buffer(fps) +---|---|---|--- +cornell_box.obj|Blinn-Phong|64.48|66.87 +cornell_box.obj|Bloom|7.29|7.28 +sponza.obj|Blinn-Phong|32.54|33.60 +sponza.obj|Bloom|6.52|6.54 ------------------------------------------------------------------------------- -PERFORMANCE EVALUATION +PERFORMANCE ANALYSIS 2: Effect of Bloom kernel ------------------------------------------------------------------------------- -The performance evaluation is where you will investigate how to make your -program more efficient using the skills you've learned in class. You must have -performed at least one experiment on your code to investigate the positive or -negative effects on performance. +Model: Cornell_box -We encourage you to get creative with your tweaks. Consider places in your code -that could be considered bottlenecks and try to improve them. +Blinn-Phong without bloom: 64.48 fps -Each student should provide no more than a one page summary of their -optimizations along with tables and or graphs to visually explain any -performance differences. - -------------------------------------------------------------------------------- -THIRD PARTY CODE POLICY -------------------------------------------------------------------------------- -* Use of any third-party code must be approved by asking on the Google groups. - If it is approved, all students are welcome to use it. Generally, we approve - use of third-party code that is not a core part of the project. For example, - for the ray tracer, we would approve using a third-party library for loading - models, but would not approve copying and pasting a CUDA function for doing - refraction. -* Third-party code must be credited in README.md. -* Using third-party code without its approval, including using another - student's code, is an academic integrity violation, and will result in you - receiving an F for the semester. - -------------------------------------------------------------------------------- -SELF-GRADING -------------------------------------------------------------------------------- -* On the submission date, email your grade, on a scale of 0 to 100, to Liam, - liamboone@gmail.com, with a one paragraph explanation. Be concise and - realistic. Recall that we reserve 30 points as a sanity check to adjust your - grade. Your actual grade will be (0.7 * your grade) + (0.3 * our grade). We - hope to only use this in extreme cases when your grade does not realistically - reflect your work - it is either too high or too low. In most cases, we plan - to give you the exact grade you suggest. -* Projects are not weighted evenly, e.g., Project 0 doesn't count as much as - the path tracer. We will determine the weighting at the end of the semester - based on the size of each project. +**With Bloom** +Kernel Size| FPS +---|--- +100 X 100| 7.29 +50 X 50| 13.10 +25 X 25| 22.42 +10 X 10| 35.93 +5 X 5| 47.29 ---- -SUBMISSION ---- -As with the previous projects, you should fork this project and work inside of -your fork. Upon completion, commit your finished project back to your fork, and -make a pull request to the master repository. You should include a README.md -file in the root directory detailing the following - -* A brief description of the project and specific features you implemented -* At least one screenshot of your project running. -* A link to a video of your project running. -* Instructions for building and running your project if they differ from the - base code. -* A performance writeup as detailed above. -* A list of all third-party code used. -* This Readme file edited as described above in the README section. --- ACKNOWLEDGEMENTS --- -This project makes use of [tinyobjloader](http://syoyo.github.io/tinyobjloader/) and [SOIL](http://lonesock.net/soil.html) +Referred this paper for toon shading, +http://www.cs.rutgers.edu/~decarlo/671/readings/decaudin_1996.pdf + +Referred this page for bloom(glow) effect. Implemented separable convolution technique +http://http.developer.nvidia.com/GPUGems/gpugems_ch21.html + diff --git a/base/PROJ_WIN/P6/P6/P6.vcxproj b/base/PROJ_WIN/P6/P6/P6.vcxproj index 61a6915..5f6398f 100644 --- a/base/PROJ_WIN/P6/P6/P6.vcxproj +++ b/base/PROJ_WIN/P6/P6/P6.vcxproj @@ -19,12 +19,14 @@ Application true Unicode + v110 Application false true Unicode + v110 @@ -67,7 +69,7 @@ true true $(SolutionDir)$(Configuration);..\..\..\..\shared32\glew\lib;..\..\..\..\shared32\freeglut\lib;%(AdditionalLibraryDirectories) - freeglut.lib;glew32.lib;%(AdditionalDependencies) + freeglut.lib;glew32.lib;SOIL.lib;%(AdditionalDependencies) Console @@ -83,6 +85,7 @@ + @@ -91,6 +94,7 @@ + diff --git a/base/PROJ_WIN/P6/P6/P6.vcxproj.filters b/base/PROJ_WIN/P6/P6/P6.vcxproj.filters index 8fd6655..ede674b 100644 --- a/base/PROJ_WIN/P6/P6/P6.vcxproj.filters +++ b/base/PROJ_WIN/P6/P6/P6.vcxproj.filters @@ -67,5 +67,11 @@ Resource Files + + Resource Files + + + Resource Files + \ No newline at end of file diff --git a/base/PROJ_WIN/P6/SOIL/SOIL.vcxproj b/base/PROJ_WIN/P6/SOIL/SOIL.vcxproj index f341f68..2cff1d2 100644 --- a/base/PROJ_WIN/P6/SOIL/SOIL.vcxproj +++ b/base/PROJ_WIN/P6/SOIL/SOIL.vcxproj @@ -20,10 +20,12 @@ StaticLibrary Unicode true + v110 StaticLibrary Unicode + v110 diff --git a/base/res/cornell/cornell_box.mtl b/base/res/cornell/cornell_box.mtl index aae8474..420b114 100644 --- a/base/res/cornell/cornell_box.mtl +++ b/base/res/cornell/cornell_box.mtl @@ -2,23 +2,42 @@ newmtl white Ka 0 0 0 Kd 0.9 0.9 0.9 Ks 0 0 0 +Ns 0.0 newmtl red Ka 0 0 0 Kd 1 0 0 Ks 0 0 0 +Ns 2.0 newmtl green Ka 0 0 0 Kd 0 1 0 Ks 0 0 0 +Ns 0.0 newmtl blue Ka 0 0 0 Kd 0 0 1 Ks 0 0 0 +Ns 0.0 newmtl light Ka 20 20 20 Kd 1 1 1 Ks 0 0 0 +Ns 0.0 + +newmtl yellow +Ka 0 0 0 +Kd 1 1 0 +Ks 0 0 0 +Ns 0.0 + +newmtl purple +Ka 0 0 0 +Kd 0.33 0.1 0.55 +Ks 0 0 0 +Ns 0.6 + + diff --git a/base/res/cornell/cornell_box.obj b/base/res/cornell/cornell_box.obj index 43e021f..16cfdbb 100644 --- a/base/res/cornell/cornell_box.obj +++ b/base/res/cornell/cornell_box.obj @@ -77,7 +77,7 @@ v 556.0 548.8 0.0 f -4 -3 -2 -1 o short_block -usemtl white +usemtl purple v 130.0 165.0 65.0 v 82.0 165.0 225.0 @@ -110,7 +110,7 @@ v 82.0 0.0 225.0 f -4 -3 -2 -1 o tall_block -usemtl white +usemtl yellow v 423.0 330.0 247.0 v 265.0 330.0 296.0 @@ -118,7 +118,7 @@ v 314.0 330.0 456.0 v 472.0 330.0 406.0 f -4 -3 -2 -1 -usemtl white +usemtl yellow v 423.0 0.0 247.0 v 423.0 330.0 247.0 v 472.0 330.0 406.0 diff --git a/base/res/shaders/bloom.frag b/base/res/shaders/bloom.frag new file mode 100644 index 0000000..4562ac2 --- /dev/null +++ b/base/res/shaders/bloom.frag @@ -0,0 +1,100 @@ +#version 330 + +//////////////////////////// +// ENUMERATIONS +//////////////////////////// + +#define DISPLAY_DEPTH 0 +#define DISPLAY_NORMAL 1 +#define DISPLAY_POSITION 2 +#define DISPLAY_COLOR 3 +#define DISPLAY_TOTAL 4 +#define DISPLAY_LIGHTS 5 + + +///////////////////////////////////// +// Uniforms, Attributes, and Outputs +//////////////////////////////////// +uniform sampler2D u_Posttex; +uniform sampler2D u_Shininess; +uniform sampler2D u_RandomNormaltex; +uniform sampler2D u_RandomScalartex; + + +uniform int u_ScreenWidth; +uniform int u_ScreenHeight; + +in vec2 fs_Texcoord; + +out vec4 out_Color; +/////////////////////////////////////// + + + + +uniform float zerothresh = 1.0f; +uniform float falloff = 0.1f; + +uniform int kernelX = 100; + +///////////////////////////////////// +// UTILITY FUNCTIONS +///////////////////////////////////// + +//Helper function to automicatlly sample and unpack positions +vec3 sampleCol(vec2 texcoords) { + return texture(u_Posttex,texcoords).xyz; +} + +float sampleBloomAlpha(vec2 texcoords) { + return texture(u_Shininess,texcoords).r; +} + +//Get a random normal vector given a screen-space texture coordinate +//Actually accesses a texture of random vectors +vec3 getRandomNormal(vec2 texcoords) { + ivec2 sz = textureSize(u_RandomNormaltex,0); + return texture(u_RandomNormaltex,vec2(texcoords.s* (u_ScreenWidth)/sz.x, + (texcoords.t)*(u_ScreenHeight)/sz.y)).rgb; +} + + +//Get a random scalar given a screen-space texture coordinate +//Fetches from a random texture +float getRandomScalar(vec2 texcoords) { + ivec2 sz = textureSize(u_RandomScalartex,0); + return texture(u_RandomScalartex,vec2(texcoords.s*u_ScreenWidth/sz.x, + texcoords.t*u_ScreenHeight/sz.y)).r; +} + +/////////////////////////////////// +// MAIN +////////////////////////////////// +void main() { + vec3 color = sampleCol(fs_Texcoord); + + int kxHalf = kernelX/2; + int count = 0; + float delX = 1.0/u_ScreenWidth; + + vec3 sumColor = vec3(0.0); + + for(int i=-kxHalf; i<=kxHalf; ++i) + { + vec2 texCoord = vec2(fs_Texcoord.s+i*delX, fs_Texcoord.t); + + float alpha = sampleBloomAlpha(texCoord); + vec3 color = sampleCol(texCoord); + + sumColor += alpha*color; + count++; + } + + sumColor = 1.0/count * sumColor; + + out_Color = vec4(sumColor,1.0); + + //out_Color = vec4(sampleCol(fs_Texcoord),1.0); + return; +} + diff --git a/base/res/shaders/diagnostic.frag b/base/res/shaders/diagnostic.frag index ac73727..19ee4e8 100644 --- a/base/res/shaders/diagnostic.frag +++ b/base/res/shaders/diagnostic.frag @@ -10,6 +10,7 @@ #define DISPLAY_COLOR 3 #define DISPLAY_TOTAL 4 #define DISPLAY_LIGHTS 5 +#define DISPLAY_SPECULAR 6 ///////////////////////////////////// @@ -23,6 +24,7 @@ uniform sampler2D u_Positiontex; uniform sampler2D u_Colortex; uniform sampler2D u_RandomNormaltex; uniform sampler2D u_RandomScalartex; +uniform sampler2D u_Shininesstex; uniform float u_Far; uniform float u_Near; @@ -89,6 +91,11 @@ float getRandomScalar(vec2 texcoords) { texcoords.t*u_ScreenHeight/sz.y)).r; } +float sampleShininess(vec2 texcoords) { + return texture(u_Shininesstex,texcoords).r; +} + + /////////////////////////////////// // MAIN ////////////////////////////////// @@ -101,6 +108,7 @@ void main() { vec3 normal = sampleNrm(fs_Texcoord); vec3 position = samplePos(fs_Texcoord); vec3 color = sampleCol(fs_Texcoord); + float shininess = sampleShininess(fs_Texcoord); vec3 light = u_Light.xyz; float lightRadius = u_Light.w; @@ -117,6 +125,9 @@ void main() { case(DISPLAY_COLOR): out_Color = vec4(color, 1.0); break; + case(DISPLAY_SPECULAR): + out_Color = vec4( vec3(shininess),1.0); + break; case(DISPLAY_LIGHTS): case(DISPLAY_TOTAL): break; diff --git a/base/res/shaders/directional.frag b/base/res/shaders/directional.frag index a34daab..a8be61d 100644 --- a/base/res/shaders/directional.frag +++ b/base/res/shaders/directional.frag @@ -105,8 +105,7 @@ void main() { float lightRadius = u_Light.w; float diffuse = max(0.0, dot(normalize(light),normal)); - out_Color = vec4(color*u_LightIl*diffuse,1.0f); -} + out_Color = vec4(color*u_LightIl*diffuse,1.0f); return; } diff --git a/base/res/shaders/pass.frag b/base/res/shaders/pass.frag index e37dcbf..4301ed6 100644 --- a/base/res/shaders/pass.frag +++ b/base/res/shaders/pass.frag @@ -2,6 +2,7 @@ uniform float u_Far; uniform vec3 u_Color; +uniform float u_Shininess; in vec3 fs_Normal; in vec4 fs_Position; @@ -9,10 +10,12 @@ in vec4 fs_Position; out vec4 out_Normal; out vec4 out_Position; out vec4 out_Color; +out float out_Shininess; void main(void) { out_Normal = vec4(normalize(fs_Normal),0.0f); out_Position = vec4(fs_Position.xyz,1.0f); //Tuck position into 0 1 range out_Color = vec4(u_Color,1.0); + out_Shininess = u_Shininess; } diff --git a/base/res/shaders/point.frag b/base/res/shaders/point.frag index 98b90e0..53a8f9d 100644 --- a/base/res/shaders/point.frag +++ b/base/res/shaders/point.frag @@ -11,7 +11,6 @@ #define DISPLAY_TOTAL 4 #define DISPLAY_LIGHTS 5 - ///////////////////////////////////// // Uniforms, Attributes, and Outputs //////////////////////////////////// @@ -23,6 +22,7 @@ uniform sampler2D u_Positiontex; uniform sampler2D u_Colortex; uniform sampler2D u_RandomNormaltex; uniform sampler2D u_RandomScalartex; +uniform sampler2D u_Shininesstex; uniform float u_Far; uniform float u_Near; @@ -79,6 +79,9 @@ vec3 getRandomNormal(vec2 texcoords) { (texcoords.t)*(u_ScreenHeight)/sz.y)).rgb; } +float sampleShininess(vec2 texcoords) { + return texture(u_Shininesstex,texcoords).r; +} //Get a random scalar given a screen-space texture coordinate //Fetches from a random texture @@ -102,14 +105,37 @@ void main() { vec3 color = sampleCol(fs_Texcoord); vec3 light = u_Light.xyz; float lightRadius = u_Light.w; - out_Color = vec4(0,0,0,1.0); + vec3 posToLight = light-position; + if( u_DisplayType == DISPLAY_LIGHTS ) { //Put some code here to visualize the fragment associated with this point light + out_Color = vec4(light,1.0f); } else { //Put some code here to actually compute the light from the point light + float shininess = sampleShininess(fs_Texcoord); + float distSq = dot(posToLight,posToLight); + float radiusSq = lightRadius*lightRadius; + + float attenuation = 1 - distSq/radiusSq; + float strength = u_LightIl*attenuation; + vec3 posToLightDir = normalize(posToLight); + float diffuse = max(0.0, dot(posToLightDir,normal)); + + vec3 reflect = 2*diffuse*normal - posToLightDir; + + float specular = pow( max(0.0,dot(normalize(reflect),normalize(-position))),shininess); + if (distSq0.001f) + out_Color+= strength*vec4(specular*vec3(1.0),1.0f); + } + else + out_Color = vec4(vec3(0),1); + } return; } diff --git a/base/res/shaders/post.frag b/base/res/shaders/post.frag index 2bf5afc..1188747 100644 --- a/base/res/shaders/post.frag +++ b/base/res/shaders/post.frag @@ -10,7 +10,7 @@ #define DISPLAY_COLOR 3 #define DISPLAY_TOTAL 4 #define DISPLAY_LIGHTS 5 - +#define DISPLAY_BLOOM 8 ///////////////////////////////////// // Uniforms, Attributes, and Outputs @@ -18,21 +18,20 @@ uniform sampler2D u_Posttex; uniform sampler2D u_RandomNormaltex; uniform sampler2D u_RandomScalartex; +uniform sampler2D u_BloomPass1tex; uniform int u_ScreenWidth; uniform int u_ScreenHeight; +uniform int u_DisplayType; in vec2 fs_Texcoord; out vec4 out_Color; /////////////////////////////////////// - - - uniform float zerothresh = 1.0f; uniform float falloff = 0.1f; - +uniform int kernelY = 100; ///////////////////////////////////// // UTILITY FUNCTIONS @@ -43,6 +42,10 @@ vec3 sampleCol(vec2 texcoords) { return texture(u_Posttex,texcoords).xyz; } +vec3 sampleBloomPass1(vec2 texcoords) { + return texture(u_BloomPass1tex,texcoords).xyz; +} + //Get a random normal vector given a screen-space texture coordinate //Actually accesses a texture of random vectors vec3 getRandomNormal(vec2 texcoords) { @@ -65,10 +68,32 @@ float getRandomScalar(vec2 texcoords) { ////////////////////////////////// const float occlusion_strength = 1.5f; void main() { - vec3 color = sampleCol(fs_Texcoord); + + + vec3 bloomColor = vec3(0.0); + + if (u_DisplayType == DISPLAY_BLOOM) + { + int kyHalf = kernelY/2; + int count = 0; + float delY = 1.0/u_ScreenHeight; + for(int i=-kyHalf; i<=kyHalf; ++i) + { + vec2 texCoord = vec2(fs_Texcoord.s, fs_Texcoord.t+i*delY); + vec3 color = sampleBloomPass1(texCoord); + bloomColor += color; + count++; + } + + bloomColor = 1.0/count * bloomColor; + } + vec3 color = sampleCol(fs_Texcoord)+5*bloomColor; float gray = dot(color, vec3(0.2125, 0.7154, 0.0721)); float vin = min(2*distance(vec2(0.5), fs_Texcoord), 1.0); out_Color = vec4(mix(pow(color,vec3(1.0/1.8)),vec3(gray),vin), 1.0); + + //out_Color = vec4(sampleBloomPass1(fs_Texcoord),1.0); + //out_Color = vec4(bloomColor,1.0); return; } diff --git a/base/res/shaders/toon.frag b/base/res/shaders/toon.frag new file mode 100644 index 0000000..24d0d7d --- /dev/null +++ b/base/res/shaders/toon.frag @@ -0,0 +1,238 @@ +#version 330 + +//////////////////////////// +// ENUMERATIONS +//////////////////////////// + +#define DISPLAY_DEPTH 0 +#define DISPLAY_NORMAL 1 +#define DISPLAY_POSITION 2 +#define DISPLAY_COLOR 3 +#define DISPLAY_TOTAL 4 +#define DISPLAY_LIGHTS 5 +#define DISPLAY_SPECULAR 6 +#define DISPLAY_TOON 7 +#define KERNEL_SIZE 9 + +///////////////////////////////////// +// Uniforms, Attributes, and Outputs +//////////////////////////////////// +uniform mat4 u_Persp; + +uniform sampler2D u_Depthtex; +uniform sampler2D u_Normaltex; +uniform sampler2D u_Positiontex; +uniform sampler2D u_Colortex; +uniform sampler2D u_RandomNormaltex; +uniform sampler2D u_RandomScalartex; +uniform sampler2D u_Shininesstex; + +uniform float u_Far; +uniform float u_Near; +uniform int u_DisplayType; + +uniform int u_ScreenWidth; +uniform int u_ScreenHeight; + +uniform vec4 u_Light; +uniform float u_LightIl; + +in vec2 fs_Texcoord; + +out vec4 out_Color; +/////////////////////////////////////// + + + + +uniform float zerothresh = 1.0f; +uniform float falloff = 0.1f; + + +///////////////////////////////////// +// UTILITY FUNCTIONS +///////////////////////////////////// + +//Depth used in the Z buffer is not linearly related to distance from camera +//This restores linear depth +float linearizeDepth(float exp_depth, float near, float far) { + return (2 * near) / (far + near - exp_depth * (far - near)); +} + +//Helper function to automatically sample and unpack normals +vec3 sampleNrm(vec2 texcoords) { + return texture(u_Normaltex,texcoords).xyz; +} + +//Helper function to automicatlly sample and unpack positions +vec3 samplePos(vec2 texcoords) { + return texture(u_Positiontex,texcoords).xyz; +} + +//Helper function to automicatlly sample and unpack positions +vec3 sampleCol(vec2 texcoords) { + return texture(u_Colortex,texcoords).xyz; +} + +//Get a random normal vector given a screen-space texture coordinate +//Actually accesses a texture of random vectors +vec3 getRandomNormal(vec2 texcoords) { + ivec2 sz = textureSize(u_RandomNormaltex,0); + return texture(u_RandomNormaltex,vec2(texcoords.s* (u_ScreenWidth)/sz.x, + (texcoords.t)*(u_ScreenHeight)/sz.y)).rgb; +} + +float sampleShininess(vec2 texcoords) { + return texture(u_Shininesstex,texcoords).r; +} + +//Get a random scalar given a screen-space texture coordinate +//Fetches from a random texture +float getRandomScalar(vec2 texcoords) { + ivec2 sz = textureSize(u_RandomScalartex,0); + return texture(u_RandomScalartex,vec2(texcoords.s*u_ScreenWidth/sz.x, + texcoords.t*u_ScreenHeight/sz.y)).r; +} + + +vec4 getToonColor(float intensity) { + vec4 color = vec4(vec3(0.0),1.0); + if(intensity >0.9) + color = vec4( vec3(1.0),1.0); + else if(intensity >0.7) + color = vec4( vec3(0.8),1.0); + else if(intensity >0.5) + color = vec4( vec3(0.6),1.0); + else if(intensity >0.3) + color = vec4( vec3(0.4),1.0); + else if(intensity >0.0) + color = vec4( vec3(0.2),1.0); + + return color; +} + +/////////////////////////////////// +// MAIN +////////////////////////////////// +const float occlusion_strength = 1.5f; + +vec3 kernelNormals[KERNEL_SIZE]; +float kernelDepths[KERNEL_SIZE]; + +float depthsDiff[KERNEL_SIZE]; +float normalsDiff[KERNEL_SIZE]; + +vec2 offset[KERNEL_SIZE]; + +float d_threshold; +float n_threshold; + +void initOffset() +{ + float delX = 1.0/u_ScreenWidth; + float delY = 1.0/u_ScreenHeight; + + //int kernelX = sqrt(KERNEL_SIZE)/2; + int kernelX = 1; + int idx =0; + for(int j=kernelX; j>= -kernelX; --j) + for(int i= -kernelX; i<=kernelX; ++i) + { + offset[idx++] = vec2(i*delX,j*delY); + } + +} + +void fillKernelValues(vec2 texCoord) +{ + for(int i=0; i< KERNEL_SIZE;++i) + { + float exp_depth = texture(u_Depthtex,texCoord+offset[i]).r; + kernelDepths[i] = linearizeDepth(exp_depth,u_Near,u_Far); + kernelNormals[i] = sampleNrm(texCoord+offset[i]); + } +} + +void main() { + + float exp_depth = texture(u_Depthtex, fs_Texcoord).r; + float lin_depth = linearizeDepth(exp_depth,u_Near,u_Far); + + vec3 normal = sampleNrm(fs_Texcoord); + vec3 position = samplePos(fs_Texcoord); + vec3 color = sampleCol(fs_Texcoord); + vec3 light = u_Light.xyz; + vec3 posToLight = light-position; + + + //Referred + //http://www.cs.rutgers.edu/~decarlo/671/readings/decaudin_1996.pdf + //for the edge detection filter + + if( u_DisplayType == DISPLAY_TOON ) + { + + initOffset(); + + for(int i=0; igdmax) + gdmax = depthsDiff[i]; + if (depthsDiff[i]gnmax) + gnmax = normalsDiff[i]; + if (normalsDiff[i] NEARP) { @@ -669,14 +758,16 @@ void draw_light(vec3 pos, float strength, mat4 sc, mat4 vp, float NEARP) { vec4 up = vp * vec4(pos + radius*cam.up, 1.0); vec4 center = vp * vec4(pos, 1.0); - left = sc * left; - up = sc * up; - center = sc * center; left /= left.w; up /= up.w; center /= center.w; + + left = sc * left; + up = sc * up; + center = sc * center; + float hw = glm::distance(left, center); float hh = glm::distance(up, center); @@ -709,6 +800,15 @@ void updateDisplayText(char * disp) { case(DISPLAY_LIGHTS): sprintf(disp, "Displaying Lights"); break; + case(DISPLAY_SPECULAR): + sprintf(disp, "Displaying Shininess"); + break; + case(DISPLAY_TOON): + sprintf(disp, "Displaying Toon Shading"); + break; + case(DISPLAY_BLOOM): + sprintf(disp, "Displaying Bloom"); + break; } } @@ -754,7 +854,7 @@ void display(void) glDisable(GL_DEPTH_TEST); glBlendFunc(GL_ONE, GL_ONE); glClear(GL_COLOR_BUFFER_BIT); - if(display_type == DISPLAY_LIGHTS || display_type == DISPLAY_TOTAL) + if(display_type == DISPLAY_LIGHTS || display_type == DISPLAY_TOTAL || display_type == DISPLAY_BLOOM) { setup_quad(point_prog); if(doIScissor) glEnable(GL_SCISSOR_TEST); @@ -768,8 +868,11 @@ void display(void) 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.5, 0.5, 0.0, 1.0); - - draw_light(vec3(2.5, -2.5, 5.0), 0.50, sc, vp, NEARP); + + for(int i=0; i shapes; +typedef struct{ + glm::vec3 pos; + float strength; + float radius; +} light_t; + +std::vector lights; + typedef struct { std::vector vertices; std::vector normals; @@ -37,6 +45,7 @@ typedef struct { std::vector indices; std::string texname; glm::vec3 color; + float shininess; } mesh_t; typedef struct { @@ -48,6 +57,7 @@ typedef struct { unsigned int vbo_texcoords; glm::vec3 color; std::string texname; + float shininess; } device_mesh_t; typedef struct { @@ -83,7 +93,10 @@ enum Display { DISPLAY_POSITION = 2, DISPLAY_COLOR = 3, DISPLAY_TOTAL = 4, - DISPLAY_LIGHTS = 5 + DISPLAY_LIGHTS = 5, + DISPLAY_SPECULAR = 6, + DISPLAY_TOON = 7, + DISPLAY_BLOOM=8 }; char* loadFile(char *fname, GLint &fSize); @@ -93,6 +106,7 @@ void initShade(); void initPass(); void initMesh(); +void initLights(); device_mesh_t uploadMesh(const mesh_t & mesh); void display(void); diff --git a/renders/blinnPhong.png b/renders/blinnPhong.png new file mode 100644 index 0000000..09d3712 Binary files /dev/null and b/renders/blinnPhong.png differ diff --git a/renders/bloom.png b/renders/bloom.png new file mode 100644 index 0000000..0de49cd Binary files /dev/null and b/renders/bloom.png differ diff --git a/renders/deferredShader_video_screenshot.png b/renders/deferredShader_video_screenshot.png new file mode 100644 index 0000000..ba15990 Binary files /dev/null and b/renders/deferredShader_video_screenshot.png differ diff --git a/renders/toonShading.png b/renders/toonShading.png new file mode 100644 index 0000000..4dd44bb Binary files /dev/null and b/renders/toonShading.png differ