Stored Texture Shaders

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

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

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

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

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

CS 432 Interactive Computer Graphics

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

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

CS452/552; EE465/505. Texture Mapping in WebGL

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

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

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

Lecture 19: OpenGL Texture Mapping. CITS3003 Graphics & Animation

CISC 3620 Lecture 7 Lighting and shading. Topics: Exam results Buffers Texture mapping intro Texture mapping basics WebGL texture mapping

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

1) Here is the various stages of the tetrahedron:

Texture Mapping. Computer Graphics, 2015 Lecture 9. Johan Nysjö Centre for Image analysis Uppsala University

Steiner- Wallner- Podaras

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

Computergraphics Exercise 15/ Shading & Texturing

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

Lighting and Texturing

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

INF3320 Computer Graphics and Discrete Geometry

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

Vertex & Fragment shaders

Introduction to the OpenGL Shading Language (GLSL)

INF3320 Computer Graphics and Discrete Geometry

CS179: GPU Programming

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

Stripes, Rings, and Dots!

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

Lecture 22 Sections 8.8, 8.9, Wed, Oct 28, 2009

Grafica Computazionale

Real-time Graphics 3. Lighting, Texturing

Discussion 3. PPM loading Texture rendering in OpenGL

CMPS160 Shader-based OpenGL Programming

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

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

Methodology for Lecture

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

Course 17: State-of-the-Art in Hardware Rendering. The OpenGL Shading Language Randi Rost 3Dlabs, Inc.

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

Tuesday, 23 March OpenGL Shading Language

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

1 of 5 9/10/ :11 PM

Lecture 07: Buffers and Textures

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

To Do. Review of Last Demo. Methodology for Lecture. Geometry Basic Setup. Outline. Foundations of Computer Graphics (Fall 2012)

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

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

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

Texturing. Slides done by Tomas Akenine-Möller and Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Lab 9 - Metal and Glass

-=Catmull's Texturing=1974. Part I of Texturing

Texture and other Mappings

Waves, Displacement, Reflections. Arun Rao CS 594

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

OpenGL Texture Mapping. Objectives Introduce the OpenGL texture functions and options

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

Yazhuo Liu Homework 3

Objectives. Texture Mapping and NURBS Week 7. The Limits of Geometric Modeling. Modeling an Orange. Three Types of Mapping. Modeling an Orange (2)

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

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Texture Mapping CSCI 4229/5229 Computer Graphics Fall 2016

Graphics. Texture Mapping 고려대학교컴퓨터그래픽스연구실.

CT5510: Computer Graphics. Texture Mapping

Texturing. Slides done bytomas Akenine-Möller and Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Department of Computer Sciences Graphics Spring 2013 (Lecture 19) Bump Maps

Introduction to Computer Graphics with WebGL

Textures. Texture Mapping. Bitmap Textures. Basic Texture Techniques

三維繪圖程式設計 3D Graphics Programming Design 第七章基礎材質張貼技術嘉大資工系盧天麒

Shadow Rendering. CS7GV3 Real-time Rendering

OpenGL & Visualization

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

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

Advanced Graphics. The Shader knows. Alex Benton, University of Cambridge Supported in part by Google UK, Ltd

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

Programmable Graphics Hardware

Programming shaders & GPUs Christian Miller CS Fall 2011

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

GLSL Language Details

Design Focus. GLSL Language Details. Additions from C++ Additions for Graphics

ก ก ก.

GPU Programming EE Final Examination

Texture Mapping. Mike Bailey.

Mobile Application Programing: Android. OpenGL Operation

Cap. 3 Textures. Mestrado em Engenharia Informática (6931) 1º ano, 1º semestre

Programmable Graphics Hardware

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

OUTLINE. Implementing Texturing What Can Go Wrong and How to Fix It Mipmapping Filtering Perspective Correction

General Purpose computation on GPUs. Liangjun Zhang 2/23/2005

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

Texturas. Objectives. ! Introduce Mapping Methods. ! Consider two basic strategies. Computação Gráfica

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

12.2 Programmable Graphics Hardware

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

CS212. OpenGL Texture Mapping and Related

Texture Mapping. Texture Mapping. Map textures to surfaces. Trompe L Oeil ( Deceive the Eye ) The texture. Texture map

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

Transcription:

Stored Texture Shaders 157

Preparing for Texture Access These steps are the same when using a shader as when using fixed functionality Make a specific texture unit active by calling glactivetexture Create a texture object and bind it to the active texture unit by calling glbindtexture Set texture parameters by calling gltexparameter Define the texture by calling glteximage Not required when using a shader: Enabling the desired texture on the texture unit by calling glenable Setting the texture function by calling gltexenv December 9, 2005 158

Accessing Texture Maps In your shader, declare a uniform variable of type sampler In your application, call gluniform1i to specify the texture unit to be accessed From within your shader, call one of the built-in in texture functions 1D/2D/3D textures Depth textures Cube maps Projective versions also provided December 9, 2005 159

Vertex Shader Texture Access Textures can be accessed from either a fragment shader or a vertex shader However, an implementation is allowed to report 0 as the number of supported vertex texture image units Current generation of hardware may report 0 Could be a portability issue for some applications Level-of of-detail is handled differently: Some texture calls are allowed only within a vertex shader and express the level-of of-detail as an absolute value Other texture calls are allowed only within a fragment shader and the level-of of-detail parameter is used to bias the value computed by the graphics hardware December 9, 2005 160

Application Code static void init2dtexture(glint texunit, GLint texname, GLint texwidth, GLint texheight, GLubyte *texptr) { glactivetexture(gl_texture0 + texunit); glbindtexture(gl_texture_2d, texname); gltexparameterf(gl_texture_2d, GL_TEXTURE_WRAP_S, GL_REPEAT); gltexparameterf(gl_texture_2d, GL_TEXTURE_WRAP_T, GL_REPEAT); gltexparameterf(gl_texture_2d, GL_TEXTURE_MAG_FILTER, GL_LINEAR); gltexparameterf(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glteximage2d(gl_texture_2d, 0, GL_RGB, texwidth, texheight,, 0, GL_RGB, GL_UNSIGNED_BYTE, texptr); } glactivetexture(gl_ (GL_TEXTURE0); December 9, 2005 161

Earth Fragment Shader (1 Texture) varying float LightIntensity; uniform sampler2d EarthTexture; void main (void) { vec3 lightcolor = vec3 (texture2d(earthtexture EarthTexture, gl_texcoord TexCoord[0]. [0].st)); gl_fragcolor = vec4 (lightcolor( * LightIntensity,, 1.0); } December 9, 2005 162

Multitexture Example Blue Marble images by Reto Stöckli of the NASA/Goddard Space Flight Center Put clouds in red component and gloss map in green component of one texture December 9, 2005 163

Multitexture Fragment Shader uniform sampler2d EarthDay; uniform sampler2d EarthNight; uniform sampler2d EarthCloudGloss; varying float Diffuse; varying vec3 Specular; varying vec2 TexCoord; void main (void) { vec2 clouds = texture2d(earthcloudgloss EarthCloudGloss, TexCoord). ).rg; vec3 daytime = (texture2d(earthday EarthDay, TexCoord). ).rgb * Diffuse + Specular * clouds.g) * (1.0 - clouds.r) + clouds.r * Diffuse; vec3 nighttime = texture2d(earthnight EarthNight, TexCoord). ).rgb * (1.0 - clouds.r) * 2.0; vec3 color = daytime; if (Diffuse <= 0.1) color = mix(nighttime, daytime, (Diffuse + 0.1) * 5.0); gl_fragcolor = vec4 (color, 1.0); } December 9, 2005 164

Stored Texture Shader Demo December 9, 2005 165

Uses For Texture Memory Normals Gloss values Control values Polynomial coefficient values Intermediate values from a multipass algorithm Lookup tables Complex function values Noise Trig functions Random numbers??? December 9, 2005 166

Procedural Texture Shaders 167

Procedural Textures A procedural texture is a texture that is computed in a shader rather than stored in a texture map Advantages: Can be a continuous mathematical function rather than a discrete array of pixel values therefore infinite precision is possible Shader code is likely to be a few kilobytes rather than a few megabytes for a texture map Can be paramaterized,, allowing a lot of flexibility at run time Disadvantages: Programming skill required (not so for texture maps) Texture lookup might be faster than procedural texture computation on May have aliasing characteristics that are difficult to overcome (texture mapping hardware is built to deal with aliasing issues, e.g., mipmaps) Hardware differences may lead to somewhat different appearance on different platforms Often a hybrid approach will be the right answer December 9, 2005 168

Stripe Vertex Shader // Stripe Shader Courtesy Lightwork Design uniform vec3 uniform vec3 uniform vec3 uniform vec3 LightPosition; LightColor; EyePosition; Specular; uniform vec3 Ambient; uniform float Kd; varying vec3 varying vec3 DiffuseColor; SpecularColor; December 9, 2005 169

Stripe Vertex Shader void main(void) { vec3 ecposition = vec3 (gl( gl_modelviewmatrix * gl_vertex); vec3 tnorm = normalize(gl gl_normalmatrix * gl_normal); vec3 lightvec = normalize(lightposition - ecposition); vec3 viewvec = normalize(eyeposition - ecposition); vec3 Hvec = normalize(viewvec + lightvec); float spec = clamp(dot(hvec Hvec, tnorm), 0.0, 1.0); spec = pow(spec, 16.0); DiffuseColor = LightColor * vec3 (Kd( * dot(lightvec lightvec, tnorm)); DiffuseColor = clamp(ambient + DiffuseColor,, 0.0, 1.0); SpecularColor = clamp((lightcolor * Specular * spec), 0.0, 1.0); } gl_texcoord TexCoord[0] = gl_multitexcoord0; gl_position = ftransform(); December 9, 2005 170

Stripe Fragment Shader uniform vec3 StripeColor; uniform vec3 BackColor; uniform float Width; uniform float Fuzz; uniform float Scale; varying vec3 DiffuseColor; varying vec3 SpecularColor; void main(void) { float scaled_t = fract(gl gl_texcoord[0].t * Scale); float frac1 = clamp(scaled_t / Fuzz, 0.0, 1.0); float frac2 = clamp((scaled_t - Width) / Fuzz, 0.0, 1.0); frac1 = frac1 * (1.0 - frac2); frac1 = frac1 * frac1 * (3.0 - (2.0 * frac1)); vec3 finalcolor = mix(backcolor BackColor, StripeColor,, frac1); finalcolor = finalcolor * DiffuseColor + SpecularColor; } gl_fragcolor = vec4 (finalcolor( finalcolor,, 1.0); December 9, 2005 171

Lattice Fragment Shader varying vec3 varying vec3 DiffuseColor; SpecularColor; uniform vec2 Scale; uniform vec2 Threshold; uniform vec3 SurfaceColor; void main (void) { float ss = fract(gl gl_texcoord[0].s * Scale.s); float tt = fract(gl gl_texcoord[0].t * Scale.t); if ((ss > Threshold.s) && (tt( > Threshold.t)) discard; } vec3 finalcolor = SurfaceColor * DiffuseColor + SpecularColor; gl_fragcolor = vec4 (finalcolor( finalcolor,, 1.0); December 9, 2005 172

Dimple Vertex Shader varying vec3 LightDir; varying vec3 EyeDir; uniform vec3 LightPosition; attribute vec3 Tangent; December 9, 2005 173

Dimple Vertex Shader void main(void) { EyeDir = vec3 (gl( gl_modelviewmatrix * gl_vertex); gl_position = ftransform(); gl_texcoord TexCoord[0] = gl_multitexcoord0; vec3 n = normalize(gl gl_normalmatrix * gl_normal); vec3 t = normalize(gl gl_normalmatrix * Tangent); vec3 b = cross(n, t); vec3 v; v.x = dot(lightposition LightPosition,, t); v.y = dot(lightposition LightPosition,, b); v.z = dot(lightposition LightPosition,, n); LightDir = normalize(v); } v.x = dot(eyedir EyeDir,, t); v.y = dot(eyedir EyeDir,, b); v.z = dot(eyedir EyeDir,, n); EyeDir = normalize(v); December 9, 2005 174

Dimple Fragment Shader varying vec3 LightDir; varying vec3 EyeDir; uniform vec3 SurfaceColor; // = (0.7, 0.6, 0.18) uniform float BumpDensity; // = 16.0 uniform float BumpSize; // = 0.15 uniform float SpecularFactor; // = 0.5 December 9, 2005 175

Dimple Fragment Shader void main (void) { vec3 litcolor; vec2 c = BumpDensity * gl_texcoord TexCoord[0]. [0].st; vec2 p = fract(c) - vec2(0.5); float d, f; d = p.x * p.x + p.y * p.y; f = 1.0 / sqrt(d + 1.0); if (d >= BumpSize) { p = vec2(0.0); f = 1.0; } vec3 normdelta = vec3(p.x, p.y, 1.0) * f; litcolor = SurfaceColor * max(dot(normdelta normdelta, LightDir), 0.0); vec3 reflectdir = reflect(lightdir LightDir, normdelta); float spec = max(dot(eyedir EyeDir, reflectdir), ), 0.0); spec *= SpecularFactor; litcolor = min(litcolor + spec, vec3(1.0)); } gl_fragcolor = vec4(litcolor litcolor,, 1.0); December 9, 2005 176

Procedural Shader Demo December 9, 2005 177