1 Graphics CSCI 343, Fall 2015 Lecture 25 Texture Mapping.

Slides:



Advertisements
Similar presentations
1 Understanding of OpenGL TA: Dong Hyun Jeong Instructor : Dr. Kalpathi Subramanian Texture Mapping.
Advertisements

TEXTURE MAPPING JEFF CHASTINE 1. TEXTURE MAPPING Applying an image (or a texture ) to geometry 2D images (rectangular) 3D images (volumetric – such as.
OpenGL Texture Mapping
Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =
CSC345: Advanced Graphics & Virtual Environments
OpenGL Texture Mapping
OpenGL Texture Mapping Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
1 Lecture 12 Texture Mapping uploading of the texture to the video memory the application of the texture onto geometry.
OpenGL Texture Mapping April 16, Angel: Interactive Computer Graphics 3E © Addison-Wesley 2002 Basic Stragegy Three steps to applying a texture.
CS 4731: Computer Graphics Lecture 17: Texturing Emmanuel Agu.
Texture Mapping A way of adding surface details Two ways can achieve the goal:  Surface detail polygons: create extra polygons to model object details.
1 Texture Maps Jeff Parker Oct Objectives Introduce Mapping Methods Texture Mapping Environment Mapping Bump Mapping Billboards Consider basic.
Texture Mapping. To add surface details… World of Warcraft, Blizzard Inc. More polygons (slow and hard to handle small details) Less polygons but with.
Computer Graphics Texture Mapping Eriq Muhammad Adams
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
2IV60 Computer Graphics set 10: Texture mapping Jack van Wijk TU/e.
Texture Mapping. Scope Buffers Buffers Various of graphics image Various of graphics image Texture mapping Texture mapping.
Texture Mapping. Introduction What is Texture Mapping? Types of Texture Mapping –1D, 2D and 3D SDL and OpenGL.
Texture Mapping Course: Computer Graphics Presented by Fan Chen
Computer Graphics Ben-Gurion University of the Negev Fall 2012.
CS 4363/6353 TEXTURE MAPPING PART II. WHAT WE KNOW We can open image files for reading We can load them into texture buffers We can link that texture.
ECSE-4750 Computer Graphics Fall 2004 Prof. Michael Wozny TA. Abhishek Gattani TA. Stephen
CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
Texture Mapping. 2 Motivation A typical modern graphics card can handle 10s of millions of polygons a second. How many individual blades of grass are.
OpenGL Texture Mapping. 2 Objectives Introduce the OpenGL texture functions and options.
CS 480/680 Computer Graphics OpenGL Texture Mapping Dr. Frederick C Harris, Jr. Fall 2011.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
2 COEN Computer Graphics I Evening’s Goals n Discuss displaying and reading image primitives n Describe texture mapping n Discuss OpenGL modes and.
1 3D API OPENGL ES v1.0 Owned by Silicon Graphics (SGL) Control was then transferred to Khronos Group Introduction.
Texture Mapping. 2 3 Loading Textures void glTexImage2D (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border,
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Graphics CSCI 343, Fall 2015 Lecture 5 Color in WebGL.
Vertex Buffer Objects and Shader Attributes. For Further Reading Angel 7 th Ed: –Most parts of Chapter 2. Beginning WebGL: –Chapter 1: vertex Buffer Objects,
 Learn how you can use the shader through OpenGL ES  Add texture on object and make the object have a different look!!
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
Mouse Input. For Further Reading Learning WebGL, Lesson 11: 2.
MP3 Frequently Asked Questions (IN OFFICE HOURS).
Texture Mapping. For Further Reading Angel 7 th Ed: ­Chapter 7: 7.3 ~ 7.9 Beginning WebGL: ­Chapter 3 2.
Viewing and Texture Mapping In OPENGL. VIEWING 1.One or more objects 2.A viewer with a projection surface 3.Projectors that go from the object(s) to.
Texture Mapping CEng 477 Introduction to Computer Graphics.
Texture Mapping Fall, 2016.
Texture Mapping 고려대학교 컴퓨터 그래픽스 연구실 kucg.korea.ac.kr.
Texture Mapping We can improve the realism of graphics models by mapping a texture pattern (image) onto the modeled object surface. We refer to this technique.
OpenGL Texture Mapping
Texture Mapping Part II
OpenGL Texture Mapping
Introduction to Computer Graphics with WebGL
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Chapters VIII Image Texturing
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 2: Complete Programs
Introduction to Computer Graphics with WebGL
Mipmaps Lecture 16 Mon, Oct 1, 2007.
3D Game Programming Texture Mapping
Introduction to Computer Graphics with WebGL
Computer Graphics Practical Lesson 6
OpenGL Texture Mapping
OpenGL Texture Mapping
Mipmaps Lecture 13 Wed, Oct 12, 2005.
Programming Textures Lecture 15 Fri, Sep 28, 2007.
Texture Mapping Ed Angel Professor Emeritus of Computer Science
CS 480/680 (Fall 2018) Part 2: Texture Loading
3D Game Programming Texture Mapping
Textures in WebGL.
Introduction to Computer Graphics with WebGL
OpenGL Texture Mapping
Presentation transcript:

1 Graphics CSCI 343, Fall 2015 Lecture 25 Texture Mapping

2 Texture Mapping in WebGL Texture mapping in WebGL is done once the primitives are rasterized as each fragment is processed. Texture mapping acts as part of the shading process, as the fragment color is computed. Texture mapping uses the coordinate transformation between the texture (s, t) and surface (u, v) to compute the texel value to place at a given pixel position.

3 Specifying a Texture Texture is specified as an array of texels, of type Uint8: var myTexels = new Uint8Array(4 * 256 * 256] One byte for each of red, green, blue, and a. Width is power of 2 Height is power of 2 Create texture by either: 1) Write a program to generate the texture OR2) Read it in from an image file.

4 Reading from an image file 1)We can read in an image within the JavaScript file: var image = new Image(); image.src = "myPicture.gif"; 2) Read in the texture in the html file: and access it in the JavaScript file: var image = document.getElementById("texImage");

5 Configuring the Texture texture = gl.createTexture(); gl.bindTexture( gl.TEXTURE_2D, texture ); //Flip the Y values to match the WebGL coordinates gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true); //Specify the image as a texture array: gl.texImage2D( gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image ); //Link texture to a sampler in fragment shader gl.uniform1i(gl.getUniformLocation(program, "texture"), 0);

6 Specifying that an array holds a texture We specify that a given array contains texel values with the following WebGL function: gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, imageData); Generally: gl.texImage2D(target, level, iformat, width, height, border, format, type, texelArray);

7 Set up Texture Coordinates (0, 0) (0, 1)(1, 1) (1, 0) s t var texCoord = [ vec2(0, 0), vec2(0, 1), vec2(1, 1), vec2(1, 0) ];

8 Assigning a Texture to a Polygon function quad( a, b, c, d) { pointsArray.push(vertices[a]); texCoordsArray.push(texCoord[0]); pointsArray.push(vertices[b]); texCoordsArray.push(texCoord[1]); pointsArray.push(vertices[c]); texCoordsArray.push(texCoord[2]); pointsArray.push(vertices[a]); texCoordsArray.push(texCoord[0]); pointsArray.push(vertices[c]); texCoordsArray.push(texCoord[2]); pointsArray.push(vertices[d]); texCoordsArray.push(texCoord[3]); }

9 Passing Texture Coordinates to the Vertex Shader var tBuffer = gl.createBuffer(); gl.bindBuffer( gl.ARRAY_BUFFER, tBuffer ); gl.bufferData( gl.ARRAY_BUFFER, flatten(texCoordsArray), gl.STATIC_DRAW ); var vTexCoord = gl.getAttribLocation( program, "vTexCoord" ); gl.vertexAttribPointer( vTexCoord, 2, gl.FLOAT, false, 0, 0 ); gl.enableVertexAttribArray( vTexCoord );

10 Specifying Texture Parameters Specify Texture parameters with gl.texParameteri( ) a)Specifying Texture wrapping: What happens if the texture coordinates go out of bounds? Can specify wrapping (start at 0 again) Or, can clamp the value (with the value of the last texel). gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); or gl.TEXTURE_WRAP_T or gl.CLAMP_TO_EDGE

11 Dealing with Mismatched Scales Magnification is when each texel is larger than each pixel. Minification is when each texel is smaller than each pixel. In these cases, you can choose to use the nearest texel, or you can choose to use a weighted average of neighboring texels (linear filtering). gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); glTexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); Or GL_NEAREST

12 The vertex shader attribute vec4 vPosition; attribute vec4 vColor; attribute vec2 vTexCoord;// Input texture coordinates varying vec4 fColor; varying vec2 fTexCoord; // output to fragment shader void main() { // Code for transformations, if any fColor = vColor; fTexCoord = vTexCoord; gl_Position = vPosition; }

13 The fragment shader precision mediump float; varying vec4 fColor; varying vec2 fTexCoord; uniform sampler2D texture; //Mechanism to sample texture void main() { //Blend texture and color gl_FragColor = fColor * texture2D( texture, fTexCoord ); //If want texture alone, use //gl_FragColor = texture2D(texture, fTexCoord); }

14 Working with multiple textures //Load in the image files into separate variables (or an array) var image = [ ]; image[0] = new Image(); image[0].onload = function() { configureTexture( image[0], 0 ); } image[0].src = "flowers.jpg"; image[1] = new Image(); image[1].onload = function() { configureTexture( image[1], 1 ); } image[1].src = "earth.jpg";

15 Configuring Multiple Textures To work with multiple textures, we create multiple texture objects, one for each texture. Each is identified by an integer value. To work with a given texture, we bind it (using the identifier). function configureTexture( image, id ) { texture[id] = gl.createTexture(); gl.bindTexture( gl.TEXTURE_2D, texture[id] ); gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true); gl.texImage2D( gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image ); gl.uniform1i(gl.getUniformLocation(program, "texture"), 0); }

16 Specifying Textures in Render( ) // We use binding to specify which texture to use during the display. gl.bindTexture( gl.TEXTURE_2D, texture[0] ); gl.drawArrays( gl.TRIANGLES, 0, numVertices/2 ); gl.bindTexture( gl.TEXTURE_2D, texture[1] ); gl.drawArrays( gl.TRIANGLES, numVertices/2, numVertices/2 ); //etc.

17 Using Mipmaps WebGL can automatically generate mipmaps from a texel array. (e.g. 1x1, 2x2, 4x4, 8x8, 16x16, 32x32, and 64x64 textures): gl.generateMipMaps(gl.TEXTURE_2D);

18 Filtering with Mipmaps Specify type of filtering with: gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST); OR: gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST); OR: gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, tl.NEAREST_MIPMAP_LINEAR); OR: gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);