Textures in WebGL.

Slides:



Advertisements
Similar presentations
TEXTURE MAPPING JEFF CHASTINE 1. TEXTURE MAPPING Applying an image (or a texture ) to geometry 2D images (rectangular) 3D images (volumetric – such as.
Advertisements

1 Lecture 12 Texture Mapping uploading of the texture to the video memory the application of the texture onto geometry.
Michael Robertson Yuta Takayama. WebGL is OpenGL on a web browser. OpenGL is a low-level 3D graphics API Basically, WebGL is a 3D graphics API that generates.
CSE Real Time Rendering. TBT (Not So) Real Time Rendering.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Texture Maps Jeff Parker Oct Objectives Introduce Mapping Methods Texture Mapping Environment Mapping Bump Mapping Billboards Consider basic.
Texturing in OpenGL Lab #7. Creating Textures glEnable ( GL_TEXTURE_2D ); GLuint myTex; glGenTextures ( 1, (GLuint*) &myTex ); glBindTexture ( GL_TEXTURE_2D,
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.
WebGL: in-browser 3D graphics Nick Whitelegg Maritme and Technology Faculty Southampton Solent University.
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.
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 4 More on WebGL.
CG1 Labs Wei Li. Back Face Culling // enable back-face culling glEnable( GL_CULL_FACE ); // orientation of front-facing polygons glFrontFace( GL_CCW );
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.
80 줄로 웹지엘 입문하기. canvas javascript WebGL Library GLSL 개발환경.
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 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
User Input and Animation. For Further Reading Angel 7 th Ed: –Chapter 3: Input and Animation. –Chapter 4: Rotation and other transformation. Beginning.
1 Graphics CSCI 343, Fall 2015 Lecture 21 Lighting and Shading III.
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,
Programming with OpenGL Part 0: 3D API. For Further Reading Angel 7 th Ed: –2.2: JavaScript –2.3: OpenGL & WebGL –2.8: fragment & vertex shaders Beginning.
Shading. For Further Reading Angel 7 th Ed: ­Chapter 6 2.
1 Graphics CSCI 343, Fall 2015 Lecture 25 Texture Mapping.
 Learn how you can use the shader through OpenGL ES  Add texture on object and make the object have a different look!!
Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
3D Objects in WebGL Loading 3D model files. Announcement Midterm exam on 12/2 –No Internet connection. –Example code and exam questions will be on USB.
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.
Advanced Texture Mapping Bump Mapping & Environment Mapping (Reflection)
MP3.. Start at the very beginning. Almost. Either start from nothing yourself, or use the empty template for this MP. Run through the provided files are.
1 Introduction to Computer Graphics with WebGL ADOPTED from Ed Angel Chapter 2 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
An Introduction to WebGL Programming Ed Angel Dave Shreiner.
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Real-Time Rendering Buffers in OpenGL 3.3
The Basics: HTML5, Drawing, and Source Code Organization
Tips for Environment Mapping
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.
Texture Mapping Part II
Objectives Simple Shaders Programming shaders with GLSL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
OpenGL Texture Mapping
Introduction to Computer Graphics with WebGL
Chapters VIII Image Texturing
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 2: Complete Programs
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Chapter VI OpenGL ES and Shader
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
OpenGL Texture Mapping
Advanced Texture Mapping
Introduction to Computer Graphics with WebGL
Mouse Input.
Texture Mapping Ed Angel Professor Emeritus of Computer Science
CS 480/680 (Fall 2018) Part 2: Texture Loading
Introduction to Computer Graphics with WebGL
Presentation transcript:

Textures in WebGL

For Further Reading Angel 7th Ed: Beginning WebGL: Section 7.5: Texture Mapping in WebGL. Beginning WebGL: Chapter 3 Learning WebGL, Lesson 5: http://learningwebgl.com/blog/?p=507

Textured Cube

Summary (1/2) OpenGL setup (house keeping) for textures: gl.createTexture() gl.bindTexture() gl.pixelStorei() gl.texImage2D() gl.texParameter()

Summary (2/2) Creating image in JS. Assuming the image file is at the same folder on the server. Cross-Origin Resource Sharing (CORS) is discussed in P.60 of the “Beginning WebGL” eBook. Adding texture coordinates to vertex attributes. Texture lookup in GLSL shaders.

Cross-Origin Resource Sharing Cross-Origin Resource Sharing (CORS) prohibits loading textures from local files. To solve this, please invoke Chrome with --allow-file-access-from-files Or use Firefox instead! More detail in: http://www.realtimerendering.com/blog/setting-up-a-windows-server-for-webgl/

Textured Cube (HTML code) <script id="vertex-shader" type="x-shader/x-vertex"> ... attribute vec2 vTexCoord; varying vec2 fTexCoord; void main() { fColor = (ambient + diffuse) * vColor; fTexCoord = vTexCoord;  passing to fragment shader gl_Position = projectionMatrix * ... } </script>

<script id="fragment-shader" type="x-shader/x-fragment"> <script id="fragment-shader" type="x-shader/x-fragment"> ... varying vec2 fTexCoord; uniform sampler2D texture; void main() { gl_FragColor = fColor * texture2D( texture, fTexCoord ); } </script>

Textured Cube (JavaScript code) var pointsArray = []; var colorsArray = []; var normalsArray = []; var texCoordsArray = []; var texture; var texCoord = [ vec2(0, 0), vec2(0, 1), vec2(1, 1), vec2(1, 0) ];

function configureTexture( image ) { texture = gl. createTexture(); gl function configureTexture( image ) { texture = gl.createTexture(); gl.bindTexture( gl.TEXTURE_2D, texture ); gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true); gl.texImage2D( gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image ); gl.generateMipmap( gl.TEXTURE_2D ); gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR ); gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST ); gl.uniform1i(gl.getUniformLocation(program, "texture"), 0); } function quad(a, b, c, d) { ... pointsArray.push(vertices[a]); colorsArray.push(vertexColors[a]); normalsArray.push(normal); texCoordsArray.push(texCoord[0]);

window. onload = function init() {. var tBuffer = gl window.onload = function init() { ... 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 ); // // Initialize a texture var image = new Image(); image.onload = function() { configureTexture( image ); } image.src = "SA2011_black.gif";

Lab Time! Use MS Paint (小畫家) to draw something and save it to a GIF file. Use it as the texture for the above “Textured Cube” example. You may need to create a short cut to Chrome and add --allow-file-access-from-files to the target command.