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

Similar documents
Stored Texture Shaders

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.

CS 432 Interactive Computer Graphics

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

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

Lecture 19: OpenGL Texture Mapping. CITS3003 Graphics & Animation

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

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

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

1) Here is the various stages of the tetrahedron:

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

Computergraphics Exercise 15/ Shading & Texturing

Steiner- Wallner- Podaras

Lighting and Texturing

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

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

CS179: GPU Programming

Vertex & Fragment shaders

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

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

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

INF3320 Computer Graphics and Discrete Geometry

Grafica Computazionale

Discussion 3. PPM loading Texture rendering in OpenGL

INF3320 Computer Graphics and Discrete Geometry

Introduction to the OpenGL Shading Language (GLSL)

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

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

Real-time Graphics 3. Lighting, Texturing

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

Stripes, Rings, and Dots!

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

CMPS160 Shader-based OpenGL Programming

Methodology for Lecture

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

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

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

Tuesday, 23 March OpenGL Shading Language

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

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

1 of 5 9/10/ :11 PM

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

Texture and other Mappings

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

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

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

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

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

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

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

Texturing. Slides done by Tomas 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 Shaders for Visualization. The Basic Computer Graphics Pipeline

Textures. Texture Mapping. Bitmap Textures. Basic Texture Techniques

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

Lab 9 - Metal and Glass

Waves, Displacement, Reflections. Arun Rao CS 594

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

Shadow Rendering. CS7GV3 Real-time Rendering

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

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Yazhuo Liu Homework 3

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

Introduction to Computer Graphics with WebGL

ก ก ก.

Texture Mapping. Mike Bailey.

GPU Programming EE Final Examination

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

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

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

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

OpenGL & Visualization

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

GLSL Language Details

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

CS212. OpenGL Texture Mapping and Related

Programmable Graphics Hardware

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

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

Overview. By end of the week:

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

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

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

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

Shading. Flat shading Gouraud shading Phong shading

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

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

12.2 Programmable Graphics Hardware

Transcription:

Stored Texture Shaders 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 157 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 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 159 December 9, 2005 160

Application Code Earth Fragment Shader (1 Texture) 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); varying float LightIntensity; uniform sampler2d EarthTexture; vec3 lightcolor = vec3 (texture2d(earthtexture EarthTexture, gl_texcoord TexCoord[0]. [0].st)); = vec4 (lightcolor( * LightIntensity,, 1.0); glteximage2d(gl_texture_2d, 0, GL_RGB, texwidth, texheight,, 0, GL_RGB, GL_UNSIGNED_BYTE, texptr); glactivetexture(gl_ (GL_TEXTURE0); December 9, 2005 161 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 Multitexture Fragment Shader uniform sampler2d EarthDay; uniform sampler2d EarthNight; uniform sampler2d EarthCloudGloss; varying float Diffuse; Specular; varying vec2 TexCoord; 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); = vec4 (color, 1.0); December 9, 2005 163 December 9, 2005 164

Stored Texture Shader Demo 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 165 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 LightPosition; LightColor; EyePosition; Specular; Ambient; uniform float Kd; DiffuseColor; SpecularColor; 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 169 December 9, 2005 170 Stripe Fragment Shader StripeColor; BackColor; uniform float Width; uniform float Fuzz; uniform float Scale; DiffuseColor; 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; Lattice Fragment Shader DiffuseColor; SpecularColor; uniform vec2 Scale; uniform vec2 Threshold; SurfaceColor; 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; = vec4 (finalcolor( finalcolor,, 1.0); = vec4 (finalcolor( finalcolor,, 1.0); December 9, 2005 171 December 9, 2005 172

Dimple Vertex Shader LightDir; EyeDir; LightPosition; attribute vec3 Tangent; 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 173 December 9, 2005 174 Dimple Fragment Shader LightDir; EyeDir; SurfaceColor; // = (0.7, 0.6, 0.18) uniform float BumpDensity; // = 16.0 uniform float BumpSize; // = 0.15 uniform float SpecularFactor; // = 0.5 Dimple Fragment Shader 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)); = vec4(litcolor litcolor,, 1.0); December 9, 2005 175 December 9, 2005 176

Procedural Shader Demo December 9, 2005 177