Discussion 3. PPM loading Texture rendering in OpenGL

Similar documents
CT5510: Computer Graphics. Texture Mapping

CS 432 Interactive Computer Graphics

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

Steiner- Wallner- Podaras

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

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

Lecture 07: Buffers and Textures

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

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

Lecture 19: OpenGL Texture Mapping. CITS3003 Graphics & Animation

CPSC 436D Video Game Programming

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

Shading/Texturing. Dr. Scott Schaefer

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

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

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

Computergraphics Exercise 15/ Shading & Texturing

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

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

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

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

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

Texture Mapping 1/34

Texture Mapping. Mike Bailey.

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

Magnification and Minification

ก ก ก.

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

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

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

Geometry Shaders. And how to use them

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

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

Introduction to Computer Graphics with WebGL

CS212. OpenGL Texture Mapping and Related

Texture Mapping 1/34

Texture Mapping CSCI 4229/5229 Computer Graphics Fall 2016

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

Overview. By end of the week:

INF3320 Computer Graphics and Discrete Geometry

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

CS179: GPU Programming

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

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

COMP371 COMPUTER GRAPHICS

GRAFIKA KOMPUTER. ~ M. Ali Fauzi

Computer graphics Labs: OpenGL (2/2) Vertex Shaders and Fragment Shader

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

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

Lighting and Texturing

Computação Gráfica. Computer Graphics Engenharia Informática (11569) 3º ano, 2º semestre. Chap. 4 Windows and Viewports

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

Methodology for Lecture

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

Textures. Texture Mapping. Bitmap Textures. Basic Texture Techniques

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

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

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

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

Image I/O and OpenGL Textures

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

INF3320 Computer Graphics and Discrete Geometry

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

CS 450: COMPUTER GRAPHICS REVIEW: STATE, ATTRIBUTES, AND OBJECTS SPRING 2015 DR. MICHAEL J. REALE

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

OpenGL & Visualization

Lecture 5 3D graphics part 3

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

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

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

Stored Texture Shaders

Texture mapping. Computer Graphics CSE 167 Lecture 9

Tutorial 3: Texture Mapping

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

OpenGL Performances and Flexibility

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

Mali Developer Resources. Kevin Ho ARM Taiwan FAE

WebGL. Creating Interactive Content with WebGL. Media #WWDC14. Session 509 Dean Jackson and Brady Eidson WebKit Engineers

CS 432 Interactive Computer Graphics

Computer graphics Labs: OpenGL (1/3) Geometric transformations and projections

CSE 167. Discussion 03 ft. Glynn 10/16/2017

Shadow Rendering. CS7GV3 Real-time Rendering

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

Computer Graphics Texture Mapping

OpenGL ES for iphone Games. Erik M. Buck

Mixing graphics and compute with multiple GPUs

Grafica Computazionale

Texture and other Mappings

Yazhuo Liu Homework 3

From system point of view, a graphics application handles the user input, changes the internal state, called the virtual world by modeling or

Tutorial 12: Real-Time Lighting B

Shading. Flat shading Gouraud shading Phong shading

CS452/552; EE465/505. Review & Examples

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

OpenGL Performances and Flexibility. Visual Computing Laboratory ISTI CNR, Italy

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

Introduction to OpenGL

Introductory Seminar

IntMu.Lab5. Download all the files available from

Transcription:

Discussion 3 PPM loading Texture rendering in OpenGL

PPM Loading - Portable PixMap format 1. 2. Code for loadppm(): http://ivl.calit2.net/wiki/images/0/09/loadppm.txt ppm file format: Header: 1. P6: byte format. P3: ASCII text 2. 3. 4. 3. 4. # comments The image width & height Maximum value of colour components for pixels e.g. (0...255) color values here One byte per color in the order of R, G, B, 0=black, 255=white. Standard convention: top to bottom left to right order.

PPM Loading - loadppm() explained 1. 2. 3. 4. 5. Use fgets to read lines and skip the comments Read width and height using sscanf of format %s %s and stoi Skip the maximum value of color Use fread to read all the remaining bytes of size N = width *height*3 Returns unsigned char* rawdata, which is a unsigned byte array of size N. (Do not forget to delete this after passing it to GL)

vr_test_pattern.ppm Rendered image result:

Texture Rendering - UV space (0,0) (1, 0) Y U 7 6 2 3 X 8 5 Z 0 (0,1) V 1

Texture Rendering - UV coordinates sample (1, 0) (0,0) const GLfloat vertices[...][3] = { // "Front" vertices { -10.0, -10.0, 10.0 },{ 10.0, -10.0, 10.0 },{ 10.0, 10.0, 10.0 },{ -10.0, 10.0, 10.0 }, (0,1) // "Back" vertices Y V. 7 const GLfloat uvs[48]= { 6 } // Front face 2 3 0.0f, 1.0f, // 0 const GLuint indices[6][6] = { 1.0f, 1.0f, // 1 // Front face 1.0f, 0.0f, // 2 X { 0, 1, 2, 2, 3, 0}, 0.0f, 0.0f, // 3 8 5 Z 0 } 1 } U

Texture Rendering - Criteria Explained left front top right back bottom

Texture Rendering - OpenGL 1. Create texture is similar with creating vertex buffers. After acquiring the width, height and data: GLuint texture_id; glgentextures(1, texture_id); glbindtexture(gl_texture_2d, texture_id); glteximage2d(gl_texture_2d, 0, GL_RGB, _width, _height, 0, GL_RGB, GL_UNSIGNED_BYTE, _data); // Setup filtering (explained on slide 13) gltexparameteri(gl_texture_2d, GL_TEXTURE_MAG_FILTER, GL_NEAREST); gltexparameteri(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

Texture Rendering - OpenGL 2. Setup shader - vertex In vertex shader, add the following: // Assuming that you used locatio=0 for position of vertices. layout(location=1) in vec2 vertexuv; // Output data; will be interpolated for each fragment. out vec2 UV; // in main function:... // UV of the vertex. No special space for this one. UV = vertexuv;...

Texture Rendering - OpenGL 2. Setup shader - fragment In fragment shader, add the following:... // Interpolated values from the vertex shaders in vec2 UV; // Output data out vec3 color; // fragment shader needs to know which texture to access. uniform sampler2d mytexturesampler; // there are 4 fragment in a pixel because we used 4x AA.... // In main function replace your original color assignment with the following. color = texture( mytexturesampler, UV).rgb;

Texture Rendering - OpenGL 3. Setup texture sample handle to be used for sending texture to the shader. // Get a handle for our "mytexturesampler" uniform, and an ID for uv coordinates. GLuint texturespl_handle = glgetuniformlocation(shaderprogram, "mytexturesampler"); 4. Setup uv handle is the same with creating vertex buffers. GLuint uv_id; glgenbuffers(1, &uv_id); glbindbuffer(gl_array_buffer, uv_id); glbufferdata(gl_array_buffer, sizeof(uvs), uvs, GL_STATIC_DRAW);

Texture Rendering - OpenGL 4. Use our previous setups in the render loop, function draw(): // Bind texture in Texture Unit 0 glactivetexture(gl_texture0); glbindtexture(gl_texture_2d, texture_id); // Set our "mytexturesampler" sampler to user Texture Unit 0 gluniform1i(texturespl_handle, 0); // Second attribute buffer: UVs. assuming you were using attribarry(0) for vertex positions. glenablevertexattribarray(1); glbindbuffer(gl_array_buffer, uv_id);... // Do not forget to disable this one the same way you disable AttribArray(0) gldisablevertexattribarray(0);

Filtering and Mipmapping Recall when we set up the textures, we called: gltexparameteri(gl_texture_2d, GL_TEXTURE_MAG_FILTER, GL_NEAREST); gltexparameteri(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_NEAREST); GL_TEXTURE_MAG_FILTER: magnifying image. (GL_NEAREST, GL_LINEAR) GL_TEXTURE_MIN_FILTER: minifying image. (GL_NEAREST, GL_LINEAR GL_NEAREST_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR) GL_NEAREST: uses the value of the texture element that is nearest to the center of the pixel. GL_LINEAR: looks around, and mixes the colours according to the distance to each other. Avoids the hard edges.

Filtering and Mipmapping Mipmap: are pre-calculated, optimized sequences of images, each of which is a progressively lower resolution representation of the same image. (wiki) For linear filtering, if the texture is seen from far away, mixing only 4 texels won t be enough. Actually, if your 3D model is so far away than it takes only 1 fragment on screen, ALL the texels of the image should be averaged to produce the final color. This is obviously not done for performance reasons. GL_[X]_MIPMAP_[Y]: Y - chooses one(nearest) or two mipmaps(linear), X - how to use the value.

GUESS which one? GL_NEAREST, GL_LINEAR, or GL_LINEAR_MIPMAP_LINEAR A B C

Tips 1. USE linear for MAG and mipmap for MIN! And don t forget to add one more line: // Generate mipmaps, by the way. glgeneratemipmap(gl_texture_2d); 2. Add some key_callback code if you are working without the HMD first, e.g. 4:left, 6: right, 8: top, 2: bottom, 5: back, space:front. Very useful for debugging your UV coordinates.

References 1. 2. 3. 4. loadppm(): http://ivl.calit2.net/wiki/images/0/09/loadppm.txt Texture rendering tutorial from OpenGL: http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/ More GL review: http://www.opengl-tutorial.org/beginners-tutorials/ Tutorial with complete sample code for skybox: https://learnopengl.com/code_viewer.php?code=advanced/cubemaps_reflecti on