CS 432 Interactive Computer Graphics

Similar documents
Lecture 19: OpenGL Texture Mapping. CITS3003 Graphics & Animation

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

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

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

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

CS212. OpenGL Texture Mapping and Related

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

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

Discussion 3. PPM loading Texture rendering in OpenGL

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

Lecture 07: Buffers and Textures

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

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

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

CS179: GPU Programming

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

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

CT5510: Computer Graphics. Texture Mapping

Texture Mapping. Mike Bailey.

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

INF3320 Computer Graphics and Discrete Geometry

GRAFIKA KOMPUTER. ~ M. Ali Fauzi

Grafica Computazionale

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

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

Texture Mapping CSCI 4229/5229 Computer Graphics Fall 2016

CS335 Graphics and Multimedia. Slides adopted from OpenGL Tutorial

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

CS 432 Interactive Computer Graphics

Steiner- Wallner- Podaras

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

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

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

Lighting and Texturing

INF3320 Computer Graphics and Discrete Geometry

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

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

Computer Graphics. Bing-Yu Chen National Taiwan University

Computergraphics Exercise 15/ Shading & Texturing

Methodology for Lecture

Overview. By end of the week:

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

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

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

Stored Texture Shaders

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

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

Introduction to Computer Graphics with WebGL

Magnification and Minification

Introduction to Computer Graphics with WebGL

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

ก ก ก.

Textures. Texture Mapping. Bitmap Textures. Basic Texture Techniques

Texture Mapping 1/34

CPSC 436D Video Game Programming

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Texture Mapping 1/34

Texture Mapping and Sampling

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

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

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

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

Overview. Goals. MipMapping. P5 MipMap Texturing. What are MipMaps. MipMapping in OpenGL. Generating MipMaps Filtering.

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

Imaging and Raster Primitives

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

Copyright Khronos Group 2012 Page 1. Teaching GL. Dave Shreiner Director, Graphics and GPU Computing, ARM 1 December 2012

CS 432 Interactive Computer Graphics

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

Lecture 5 3D graphics part 3

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Computer Graphics Texture Mapping

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

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

Most device that are used to produce images are. dots (pixels) to display the image. This includes CRT monitors, LCDs, laser and dot-matrix

CS 432 Interactive Computer Graphics

Texture and other Mappings

Texturing Theory. Overview. All it takes is for the rendered image to look right. -Jim Blinn 11/10/2018

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

Computer Graphics (CS 543) Lecture 8a: Per-Vertex lighting, Shading and Per-Fragment lighting

CS 432 Interactive Computer Graphics

COMP371 COMPUTER GRAPHICS

Objectives Shading in OpenGL. Front and Back Faces. OpenGL shading. Introduce the OpenGL shading methods. Discuss polygonal shading

Shading/Texturing. Dr. Scott Schaefer

OPENGL RENDERING PIPELINE

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

Texture mapping. Computer Graphics CSE 167 Lecture 9

last time put back pipeline figure today will be very codey OpenGL API library of routines to control graphics calls to compile and load shaders

Shading. Flat shading Gouraud shading Phong shading

Tutorial 3: Texture Mapping

VGP353 Week 2. Agenda: Assignment #1 due Introduce shadow maps. Differences / similarities with shadow textures Added benefits Potential problems

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

Computer Graphics (CS 543) Lecture 8c: Environment Mapping (Reflections and Refractions)

Z-buffer Pipeline and OpenGL. University of Texas at Austin CS384G - Computer Graphics

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

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

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

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

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

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

Transcription:

CS 432 Interactive Computer Graphics Lecture 7 Part 2 Texture Mapping in OpenGL Matt Burlick - Drexel University - CS 432 1

Topics Texture Mapping in OpenGL Matt Burlick - Drexel University - CS 432 2

Reading Angel Chapter 7 Red Book Chapter 6 Chapter 8 Matt Burlick - Drexel University - CS 432 3

Texture Mapping in OpenGL Basic Strategy: 1. Get the texture data Read or generate image 2. As GPU for a texture 3. Make a texture active (bind it) and move data to that texture (put on GPU) 4. Specify texture parameters Wrapping, filtering 5. Assign texture coordinates to vertices Proper mapping function is left to application 6. Draw the scene, making texture units active and associating textures with texture units as necessary. Matt Burlick - Drexel University - CS 432 4

Design Ideas You may want to include with the object (static or not?) Texture coordinates Texture Matt Burlick - Drexel University - CS 432 5

Create Texture Object Texel values can be up to 4D (RGBA) Loading textures is expensive Faster to bind (glbindtexture) then to load (glteximage*d) So load once, and reuse/bin when needed Textures are similar to VBOs and VAOs Create texture names: GLuint texname[4]; glgentextures(4, texname); Enable/activate a texture object glbindtexture(gl_texture_2d, texname[0]); Matt Burlick - Drexel University - CS 432 6

Multi-Texturing We can use multi-texturing to allow for several textures per object. This is done by assigning textures to texture units Think of texture locations a matrix: GL_TEXTURE0 GL_TEXTURE1 GL_TEXTUREN GL_TEXTURE_2D GL_TEXTURE_2D GL_TEXTURE_2D GL_TEXTURE_CUBE_MAP GL_TEXTURE_CUBE_MAP GL_TEXTURE_CUBE_MAP Matt Burlick - Drexel University - CS 432 7

Multi-Texturing When drawing, we do the following: We must first select/activate a texture unit: glactivetexture(gl_texture0); Next we must make sure that the texture type we want within that unit is active glenable(gl_texture_2d) Finally we just associate a texture with the texture type in that texture unit glbindtexture(gl_texture_2d, textures[0]); Matt Burlick - Drexel University - CS 432 8

Step 1: Get Texture Data We define a texture image from an array of texels (texture elements) in CPU memory GLubyte my_texels[512][512][3] We may populate this texture image by Manually/systematically providing values Loading from image Matt Burlick - Drexel University - CS 432 9

Step 1: Create Data GLubyte image[64][64][3]; //Create a 64x64 checkerboard pattern for(int i=0; i<64; i++){ } for(int j=0; j<64; j++){ } GLubyte c = (((i&(0x8)==0)^((j&0x8)==0))*255; image[i][j][0] = c; image[i][j][1] = c; image[i][j][2] = c; Matt Burlick - Drexel University - CS 432 10

Step 1: Load Data For simplicity in this class to load texture from images let s 1. Convert an image to a portable pix map (PPM) using an online utility: http://ziin.pl/en/utilities/convert/ 2. Load the PPM into an unsigned byte array. Code provided for you in the Drawable class in the sample code: static unsigned char* ppmread(char* filename, int* width, int* height); Matt Burlick - Drexel University - CS 432 11

Step 2: Bind Data to Texture Unit We must first request from GPU a list of available textures getgentextures(3,textures) Then we must make active the texture we want glbindtexture(gl_texture_2d, textures[2]); Next we must move the data into the texture glteximage2d(target, level, components, w, h, border, format, type, texels) target: Type of texture, e.g. GL_TEXTURE_2D level: Used for mipmapping (discussed later) components: # of elements per texel (RGB, etc..) w,h: Width and Height of texture in pixels border: Use for smoothing (typically 0 or 1) format and type: Describe texels texels: Pointer to texel array EX: glteximage2d(gl_texture_2d,0,gl_rgb,512,512,0, GL_RGB,GL_UNSIGNED_BYTE,my_texels) Matt Burlick - Drexel University - CS 432 12

Step 3: Texture Parameters OpenGL has a variety of parameters that determine how texture is applied Wrapping parameters determine what happens if s and t are outside the (0,1) range Filter mode allows us to use area averaging instead of point samples Mipmapping allows us to use textures at multiple resolutions Environment parameters determine how texture mapping interacts with shading Matt Burlick - Drexel University - CS 432 13

Wrapping Mode Clamping: If ss, tt > 1 use 1, if ss, tt < 0 use 0 gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_S, GL_CLAMP; Repeating: Use ss, tt mmmmmmmmmmmm 1 gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_T, GL_REPEAT); t texture s GL_REPEAT wrapping GL_CLAMP wrapping Matt Burlick - Drexel University - CS 432 14

Magnification and Minification One texel can cover more than one pixel (magnification) or one pixel can be covered by more than one texel (minification). To specify how to deal with this, for each situation (OpenGL detects if either situation occurs) we can specify: Use point sampling (nearest texel) Use linear filtering (2x2 filter) to obtain texture values Texture Polygon Texture Polygon Magnification Minification Matt Burlick - Drexel University - CS 432 15

Filter Modes Modes determined by gltexparameteri(target, type, mode) Examples: gltexparameteri(gl_texture_2d, GL_TEXTURE_MAG_FILTER, GL_NEAREST); gltexparameteri(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_LINEAR); Note: Linear filtering requires a border of an extra texel for filtering at edges (border = 1) Matt Burlick - Drexel University - CS 432 16

Mipmapped Textures Mipmapping allows for prefiltered texture maps of decreasing resolutions Lessens interpolation errors for smaller textured objects Probably want higher resolution images for closer objects, lower resolution for further objects Declare mipmap level during texture definition glteximage2d(gl_texture_*d, level, ) Alternatively allow OpenGL to make the mipmaps glgeneratemipmaps(gl_texture_2d); gltexparameterf(gl_texture_2d, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST); Matt Burlick - Drexel University - CS 432 17

Step 4: Assign Texture Coordinates We talked about different ways in Part 1 Let s look at the simple plane->plane mapping: Matt Burlick - Drexel University - CS 432 18

Step 5: Rendering with Textures Just need to Make sure texture location attributes are linked and enabled vtexture = glgetattriblocation(program, vtexcoord ); glenablevertexattribarray(vtexture); glvertexattribpointer(vtexture,2,gl_float, GL_FALSE,0, BUFFER_OFFSET(sizeof(vertices)+ sizeof(normals))); Make sure desired texture unit is active (for drawing) glactivetexture(gl_texture0); glenable(gl_texture_2d) glbindtexture(gl_texture_2d,texture); gluniform1i(glgetuniformlocation(program, texture ),0); Matt Burlick - Drexel University - CS 432 19

Shaders Vertex Shader Often just pass the texture attribute through to fragment shader Fragment Shader Use a texture sampler to get the color from the current texture at the specified texture location Matt Burlick - Drexel University - CS 432 20

Vertex Shader in vec4 vposition; //vertex position in object cords in vec4 vcolor; //vertex color from application in vec2 vtexcoord; //texture coord from app out vec4 color; //output color to be interpolated out vec2 texcoord; //output tex coordinate to be interpolated Matt Burlick - Drexel University - CS 432 21

Fragment Shader in vec4 color; //color from rasterizer in vec2 texcoord; //texture coordinate from rasterizer uniform sampler2d textureid; //texture object from application; out vec4 fcolor; void main(){ fcolor = color*texture(textureid,texcoord); } Matt Burlick - Drexel University - CS 432 22

Example: Crate = Lights + Texture Matt Burlick - Drexel University - CS 432 23

Crate Example: Building Object Matt Burlick - Drexel University - CS 432 24

Crate Example : Building (cont) Continued Matt Burlick - Drexel University - CS 432 25

Crate Example: Coords Matt Burlick - Drexel University - CS 432 26

Crate Example: Draw Cube Matt Burlick - Drexel University - CS 432 27

Crate Example: Shaders #version 150 in vec4 vposition; in vec3 vnormal; in vec2 vtexcoord; out vec4 color; out vec2 texcoord; uniform mat4 model_matrix; uniform mat4 camera_matrix; uniform mat4 proj_matrix; uniform vec4 lightpos; uniform vec4 ambientproduct, diffuseproduct, specularproduct; uniform float alpha; void main() { texcoord = vtexcoord; //Then all the lighting effects // to compute output color.. //. } #version 150 in vec4 color; in vec2 texcoord; uniform sampler2d textureid; out vec4 fcolor; void main() { fcolor = texture(textureid,texcoord)*color; } Matt Burlick - Drexel University - CS 432 28