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

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

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

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

CS 432 Interactive Computer Graphics

Lecture 19: OpenGL Texture Mapping. CITS3003 Graphics & Animation

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

GRAFIKA KOMPUTER. ~ M. Ali Fauzi

Texture Mapping. Mike Bailey.

CT5510: Computer Graphics. Texture Mapping

ก ก ก.

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

Textures. Texture Mapping. Bitmap Textures. Basic Texture Techniques

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

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

Lecture 07: Buffers and Textures

CS212. OpenGL Texture Mapping and Related

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

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

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

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

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

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

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

Discussion 3. PPM loading Texture rendering in OpenGL

Lighting and Texturing

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

Computergraphics Exercise 15/ Shading & Texturing

Steiner- Wallner- Podaras

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

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

Texture Mapping CSCI 4229/5229 Computer Graphics Fall 2016

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

Texture Mapping and Sampling

Overview. By end of the week:

CS179: GPU Programming

+ = Texturing: Glue n-dimensional images onto geometrical objects. Texturing. Texture magnification. Texture coordinates. Bilinear interpolation

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

Grafica Computazionale

INF3320 Computer Graphics and Discrete Geometry

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

Texture and other Mappings

CS335 Graphics and Multimedia. Slides adopted from OpenGL Tutorial

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

Texture mapping. Computer Graphics CSE 167 Lecture 9

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

Shading. Flat shading Gouraud shading Phong shading

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

Methodology for Lecture

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

CMSC 425: Lecture 12 Texture Mapping Thursday, Mar 14, 2013

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

Computer Graphics. Bing-Yu Chen National Taiwan University

COMP371 COMPUTER GRAPHICS

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

Computational Strategies

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

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Mipmaps. Lecture 23 Subsection Fri, Oct 30, Hampden-Sydney College. Mipmaps. Robb T. Koether. Discrete Sampling.

OpenGL & Visualization

Computer Graphics. Three-Dimensional Graphics VI. Guoying Zhao 1 / 73

Lecture 5 3D graphics part 3

CSE 167: Lecture 11: Textures 2. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2011

Practical Texturing (WebGL) CS559 Fall 2016 Lecture 20 November 7th 2016

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

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

CPSC 436D Video Game Programming

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

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

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

Shading/Texturing. Dr. Scott Schaefer

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

CS GPU and GPGPU Programming Lecture 12: GPU Texturing 1. Markus Hadwiger, KAUST

Introduction to OpenGL

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

Computer Graphics Texture Mapping

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

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

CS GPU and GPGPU Programming Lecture 11: GPU Texturing 1. Markus Hadwiger, KAUST

Introduction to Computer Graphics with WebGL

FAKULTI TEKNOLOGI MAKLUMAT DAN KOMUNIKASI BITM INTERACTIVE COMPUTER GRAPHICS LAB SESSION 4. C++ - OpenGL

Interaction. CSCI 420 Computer Graphics Lecture 3

CS GPU and GPGPU Programming Lecture 16+17: GPU Texturing 1+2. Markus Hadwiger, KAUST

CS 432 Interactive Computer Graphics

INF3320 Computer Graphics and Discrete Geometry

Announcements. Written Assignment 2 is out see the web page. Computer Graphics

Introduction to Computer Graphics with WebGL

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

AMCS / CS 247 Scientific Visualization Lecture 10: (GPU) Texture Mapping. Markus Hadwiger, KAUST

Image I/O and OpenGL Textures

Tutorial 3: Texture Mapping

Page 6 PROCESSOR PROCESSOR FRAGMENT FRAGMENT. texy[1] texx. texy[1] texx. texy[0] texy[0]

Computer Graphics

CS 130 Final. Fall 2015

Programmable Graphics Hardware

Imaging and Raster Primitives

Chapter 9 Texture Mapping An Overview and an Example Steps in Texture Mapping A Sample Program Specifying the Texture Texture Proxy Replacing All or

Assignment #5: Scalar Field Visualization 3D: Direct Volume Rendering

Shadow Rendering. CS7GV3 Real-time Rendering

Textures. Datorgrafik 2006

Transcription:

CSCI 42 Computer Graphic Lecture 2 Texture Mapping A way of adding urface detail Texture Mapping Jernej Barbic Univerity of Southern California Texture Mapping + Shading Filtering and Mipmap Non-color Texture Map [Angel Ch. 7] Two way can achieve the goal: Model the urface with more polygon» Slow down rendering peed» Hard to model fine feature Map a texture to the urface» Thi lecture» Image complexity doe not affect complexity of proceing Efficiently upported in hardware 2 Trompe L Oeil ( Deceive the Eye ) Map texture to urface Window and column in the dome are painted, not a real 3D object texture map Similar idea with texture mapping: Jeuit Church, Vienna, Autria Rather than modeling the intricate 3D geometry, replace it with an image! 3 an image image mapped to a 3D polygon The polygon can have arbitrary ize, hape and 3D poition 4 The texture Texture map Texture i a bitmap image Can ue an image library to load image into memory Or can create image yourelf within the program (,) (,) (,) (,) 2D array: unigned char texture[height][width][4] Or unrolled into D array: unigned char texture[4*height*width] Pixel of the texture are called texel Texel coordinate caled to [,] range (,) texture image (,) (,) 3D polygon (,) 5 6

Texture map Texture coordinate (,) (,) (,) (,) (,) (,) texture image (,) 3D polygon (,) 7 texture image creen image For each pixel, lookup into the texture image to obtain color. 8 The t coordinate ytem t Note: alo called uv pace t (.,.7) Texture mapping: key lide (.7,.55) (,,) =. t =.7 triangle in 3D (-2,,) =.7 t =.55 9 (.35,.5) (2,-,) =.35 t =.5 Specifying texture coordinate in OpenGL (core profile) Specifying texture coordinate in OpenGL (compatibility profile) Ue VBO Either create a eparate VBO for texture coordinate, or put them with vertex poition into one VBO =. t =.7 =.7 t =.55 =.35 t =.5 Ue gltexcoord2f State machine: Texture coordinate remain valid until you change them Example (from the previou lide) : glenable(gl_texture_2d); // turn texture mapping on glbegin(gl_triangles); =. t =.7 gltexcoord2f(.35,.5); glvertex3f(2.,-.,.); gltexcoord2f(.7,.55); glvertex3f(-2.,.,.); gltexcoord2f(.,.7); glvertex3f(.,.,.); glend(); gldiable(gl_texture_2d); // turn texture mapping off =.7 t =.55 =.35 t =.5 2 2

t What if texture coordinate are outide of [,]? Solution : Repeat texture gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_S, GL_REPEAT) gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_T, GL_REPEAT) t 3 4 Solution 2: Clamp to [,] gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) t ue thi color Combining texture mapping and hading 5 Source: Jeremy Birn 6 Outline Texture interpolation Introduction Filtering and Mipmap Non-color texture map Texture mapping in OpenGL 5 x 5 texture T (,) coordinate typically not directly at pixel in the texture, but in between 7 (,) (.25,) (.5,) (.75,) (,) 8 3

Texture interpolation coordinate typically not directly at pixel in the texture, but in between Solution: Ue the nearet neighbor to determine color» Fater, but wore quality» gltexparameteri(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_NEAREST); Linear interpolation» Incorporate color of everal neighbor to determine color» Slower, better quality» gltexparameteri(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_LINEAR) Texture image i hrunk in ditant part of the image Thi lead to aliaing Can be fixed with filtering bilinear in pace trilinear in pace and level of detail (mipmapping) Filtering aliaing 9 2 Mipmapping Pre-calculate how the texture hould look at variou ditance, then ue the appropriate texture at each ditance Reduce / fixe the aliaing problem Mipmapping Each mipmap (each image below) repreent a level of depth (LOD). Decreae image 2x at each level 2 22 Mipmapping in OpenGL Generate mipmap automatically (for the currently bound texture): Core profile: glgeneratemipmap(gl_texture_2d); Compatibility profile: glubuild2dmipmap(gl_texture_2d, component, width, height, format, type, data) Outline Introduction Filtering and Mipmap Non-color texture map Texture mapping in OpenGL Mut alo intruct OpenGL to ue mipmap: gltexparameteri(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) 23 24 4

Texture do not have to repreent color Specularity (patche of hinine) Tranparency (patche of clearne) Normal vector change (bump map) Reflected light (environment map) Bump mapping How do you make a urface look rough? Option : model the urface with many mall polygon Option 2: perturb the normal vector before the hading calculation» Fake mall diplacement above or below the true urface» The urface doen t actually change, but hading make it look like there are irregularitie!» A texture tore information about the fake height of the urface Shadow Change in urface height (diplacement map) 25 26 Bump mapping We can perturb the normal vector without having to make any actual change to the hape. Thi illuion can be een through how? Light Mapping Quake ue light map in addition to texture map. Texture map are ued to add detail to urface, and light map are ued to tore pre-computed illumination. The two are multiplied together at runtime, and cached for efficiency. Texture Map Only Texture + Light Map Original model (5M) Simplified (5) Simple model with bump map 27 Light Map 28 Outline Introduction Filtering and Mipmap Non-color texture map Texture mapping in OpenGL 29 OpenGL Texture Mapping (Core Profile) During initialization:. Read texture image from file into an array in memory, or generate the image uing your program 2. Initialize the texture (glteximage2d) 3. Specify texture mapping parameter:» Repeat/clamp, filtering, mipmapping, etc. 4. Make VBO for the texture coordinate 5. Create VAO In diplay():. Bind VAO 2. Select the texture unit, and texture (uing glbindtexture) 3. Render (e.g., gldrawarray) 3 5

Read texture image from file into an array in memory Can ue our ImageIO library ImageIO * imageio = new ImageIO(); if (imageio->loadjpeg(imagefilename)!= ImageIO::OK) cout << Error reading image << imagefilename <<. << endl; exit(exit_failure); See tarter code for hw2 Initializing the texture Do once during initialization, for each texture image in the cene, by calling glteximage2d The dimenion of texture image mut be a multiple of 4 (Note: they do NOT have to be a power of 2) Can load texture dynamically if GPU memory i carce: Delete a texture (if no longer needed) uing gldeletetexture 3 32 glteximage2d glteximage2d(gl_texture_2d, level, internalformat, width, height, border, format, type, data) GL_TEXTURE_2D: pecifie that it i a 2D texture Level: ued for pecifying level of detail for mipmapping (default: ) InternalFormat Often: GL_RGB or GL_RGBA Determine how the texture i tored internally Width, Height The ize of the texture mut be a multiple of 4 Border (often et to ) Format, Type Specifie what the input data i (GL_RGB, GL_RGBA, ) Specifie the input data type (GL_UNSIGNED_BYTE, GL_BYTE, ) Regardle of Format and Type, OpenGL convert the data to internalformat Data: pointer to the image buffer 33 Global variable: GLUint texhandle; During initialization: Texture Initialization // create an integer handle for the texture glgentexture(, &texhandle); int code = inittexture( ky.jpg, texhandle); if (code!= ) printf( Error loading the texture image.\n ); exit(exit_failure); Function inittexture() i given in the tarter code for hw2. 34 VBO Layout: poition, texture coordinate (for 2 vertice) VBO gg5 53v ff&$ #422 424d ^^3d aa7y oart J^23 Gr/% po x po y po z po2 x in vec3 poition 4 byte per floating-point value po2 po2 y z tc u tc v tc2 u in vec2 texcoord tc2 v 35 #verion 5 in vec3 poition; in vec2 texcoord; out vec2 tc; Texture Shader: Vertex Program uniform mat4 modelviewmatrix; uniform mat4 projectionmatrix; input vertex poition and texture coordinate output texture coordinate; they will be paed to the fragment program (interpolated by hardware) tranformation matrice void main() // compute the tranformed and projected vertex poition (into gl_poition) gl_poition = projectionmatrix * modelviewmatrix * vec4(poition,.f); // pa-through the texture coordinate tc = texcoord; 36 6

Texture Shader: Fragment Program #verion 5 in vec2 tc; // input tex coordinate (computed by the interpolator) out vec4 c; // output color (the final fragment color) uniform ampler2d textureimage; // the texture image void main() // compute the final fragment color, // by looking up into the texture map c = texture(textureimage, tc); VAO code ( texcoord hader variable) During initialization: glbindvertexarray(vao); // bind the VAO // bind the VBO buffer (mut be previouly created) glbindbuffer(gl_array_buffer, buffer); // get location index of the texcoord hader variable GLuint loc = glgetattriblocation(program, texcoord ); glenablevertexattribarray(loc); // enable the texcoord attribute // et the layout of the texcoord attribute data cont void * offet = (cont void*) izeof(poition); GLizei tride = ; glvertexattribpointer(loc, 2, GL_FLOAT, GL_FALSE, tride, offet); 37 38 Multitexturing The ability to ue multiple texture imultaneouly in a hader Ueful for bump mapping, diplacement mapping, etc. The different texture unit are denoted by GL_TEXTURE, GL_TEXTURE, GL_TEXTURE2, etc. In imple application (our homework), we only need one unit void ettextureunit(glint unit) glactivetexture(unit); // elect the active texture unit // get a handle to the textureimage hader variable GLint h_textureimage = glgetuniformlocation(program, textureimage ); // deem the hader variable textureimage to read from texture unit unit gluniformi(h_textureimage, unit - GL_TEXTURE); 39 The diplay function void diplay() // put all the uual code here (clear creen, et up camera, upload // the modelview matrix and projection matrix to GPU, etc.) // // elect the active texture unit ettextureunit(gl_texture); // it i afe to alway ue GL_TEXTURE // elect the texture to ue ( texhandle wa generated by glgentexture) glbindtexture(gl_texture_2d, texhandle); // here, bind the VAO and render the object uing the VAO (a uual) // glutswapbuffer(); 4 Texture mapping in OpenGL (Compatibility Profile) During your initialization:. Read texture image from file into an array in memory, or generate the image uing your program 2. Specify texture mapping parameter» Wrapping, filtering, etc. 3. Initialize and activate the texture In diplay():. Enable OpenGL texture mapping 2. Draw object: Aign texture coordinate to vertice 3. Diable OpenGL texture mapping 4 Enable/diable texture mode (Compatibility Profile) Mut be done before rendering any primitive that are to be texture-mapped glenable(gl_texture_2d) gldiable(gl_texture_2d) Succeively enable/diable texture mode to witch between drawing textured/non-textured polygon Changing texture: Only one texture i active at any given time (with OpenGL extenion, more than one can be ued imultaneouly; thi i called multitexturing) Ue glbindtexture to elect the active texture 42 7

Rendering (compatibility profile) void diplay() // no modulation of texture color with lighting; ue texture color directly gltexenvf(gl_texture_env, GL_TEXTURE_ENV_MODE, GL_REPLACE); Rendering (compatibility profile) (part 2) glbegin(gl_quads); // draw a textured quad gltexcoord2f(.,.); glvertex3f(-2.,-.,.); gltexcoord2f(.,.); glvertex3f(-2.,.,.); gltexcoord2f(.,.); glvertex3f(.,.,.); gltexcoord2f(.,.); glvertex3f(.,-.,.); glend(); // turn on texture mapping (thi diable tandard OpenGL lighting, unle in GL_MODULATE mode) glenable(gl_texture_2d); // turn off texture mapping gldiable(gl_texture_2d); (continue on next page) 43 // draw ome non-texture mapped object (tandard OpenGL lighting will be ued if it i enabled) // witch back to texture mode, etc. // end diplay() 44 Summary Introduction Filtering and Mipmap Non-color texture map Texture mapping in OpenGL 45 8