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

Similar documents
Methodology for Lecture

This Lecture. Why OpenGL? Introduction to OpenGL. Programmer s View

To Do. Demo: Surreal (HW 3) This Lecture. Introduction to OpenGL. Outline. Foundations of Computer Graphics (Spring 2012)

CS 432 Interactive Computer Graphics

This Lecture. Introduction to OpenGL. Outline. Why OpenGL? Programmer s View. Foundations of Computer Graphics

To Do. Demo: Surreal (now 15+ years ago) This Lecture. Outline. Computer Graphics. CSE 167 [Win 19], Lecture 6: OpenGL 1 Ravi Ramamoorthi

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

Lecture 19: OpenGL Texture Mapping. CITS3003 Graphics & Animation

Lecture 5: Viewing. CSE Computer Graphics (Fall 2010)

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

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

Introduction to OpenGL

Discussion 3. PPM loading Texture rendering in OpenGL

CS179: GPU Programming

Lecture 07: Buffers and Textures

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

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

Overview. By end of the week:

Lecture 4 of 41. Lab 1a: OpenGL Basics

OpenGL. Jimmy Johansson Norrköping Visualization and Interaction Studio Linköping University

CS193P - Lecture 19. iphone Application Development. OpenGL ES

Texture Mapping. Mike Bailey.

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

Lecture 2 2D transformations Introduction to OpenGL

Precept 2 Aleksey Boyko February 18, 2011

QUESTION 1 [10] 2 COS340-A October/November 2009

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

OpenGL refresher. Advanced Computer Graphics 2012

Lighting and Texturing

Computer graphics MN1

C OMPUTER G RAPHICS Thursday

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

Computergraphics Exercise 15/ Shading & Texturing

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

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

INF3320 Computer Graphics and Discrete Geometry

Grafica Computazionale

OpenGL Transformations

Books, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, PointClouds, and G3D

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

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

12. Selection. - The objects selected are specified by hit records in the selection buffer. E.R. Bachmann & P.L. McDowell MV 4202 Page 1 of 13

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

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

CPSC 436D Video Game Programming

+ = To Do. Adding Visual Detail. Texture Mapping. Parameterization. Option: Varieties of projections. Foundations of Computer Graphics (Fall 2012)

Interaction Computer Graphics I Lecture 3

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

Textures. Texture Mapping. Bitmap Textures. Basic Texture Techniques

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

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

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

CS559: Computer Graphics. Lecture 12: OpenGL Li Zhang Spring 2008

1) Here is the various stages of the tetrahedron:

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

gvirtualxray Tutorial 01: Creating a Window and an OpenGL Context Using GLUT

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

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

Yazhuo Liu Homework 3

Interaction. CSCI 420 Computer Graphics Lecture 3

Texture mapping. Computer Graphics CSE 167 Lecture 9

Steiner- Wallner- Podaras

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

Computer Graphics. Bing-Yu Chen National Taiwan University

Books, OpenGL, GLUT, CUDA, OpenCL, OpenCV, PointClouds, G3D, and Qt

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

Introduction to OpenGL

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

CENG 477 Introduction to Computer Graphics. Graphics Hardware and OpenGL

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

ก ก ก.

+ = To Do. Texture Mapping. Adding Visual Detail. Parameterization. Option: Varieties of projections. Computer Graphics. geometry

3D Graphics Pipeline II Clipping. Instructor Stephen J. Guy

1 (Practice 1) Introduction to OpenGL

C++ is Fun Part 13 at Turbine/Warner Bros.! Russell Hanson

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

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

Texture Mapping and Sampling

CS212. OpenGL Texture Mapping and Related

Lectures Display List

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

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

Modeling Transform. Chapter 4 Geometric Transformations. Overview. Instancing. Specify transformation for objects 李同益

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

Computer Graphics. Chapter 10 Three-Dimensional Viewing

INF3320 Computer Graphics and Discrete Geometry

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

Stored Texture Shaders

API for creating a display window and using keyboard/mouse interations. See RayWindow.cpp to see how these are used for Assignment3

Class of Algorithms. Visible Surface Determination. Back Face Culling Test. Back Face Culling: Object Space v. Back Face Culling: Object Space.

OpenGL. 1 OpenGL OpenGL 1.2 3D. (euske) 1. Client-Server Model OpenGL

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

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

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

CT5510: Computer Graphics. Texture Mapping

Exercise 1 Introduction to OpenGL

OpenGL: Open Graphics Library. Introduction to OpenGL Part II. How do I render a geometric primitive? What is OpenGL

Computer Graphics (Basic OpenGL)

Graphics Pipeline & APIs

OpenGL. Toolkits.

Transcription:

Foundations of Computer Graphics (Fall 2012) CS 184, Lecture 8: OpenGL 2 http://inst.eecs.berkeley.edu/~cs184 To Do Continue working on HW 2. Can be difficult Class lectures, programs primary source Can leverage many sources (GL(SL) book, excellent online documentation, see links class website) It is a good idea to copy (and modify) relevant segments (Very) tough to get started, but lots of fun afterwards Methodology for Lecture Make mytest1 more ambitious Sequence of steps Demo Review of Last Demo Changed floor to all white, added global for teapot and teapotloc, moved geometry to new header file Demo 0 [set DEMO to 4 all features] #include <GL/glut.h> #include shaders.h #include geometry.h int mouseoldx, mouseoldy ; // For mouse motion GLdouble eyeloc = 2.0 ; // Where to look from; initially 0-2, 2 GLfloat teapotloc = -0.5 ; // ** NEW ** where the teapot is located GLint animate = 0 ; // ** NEW ** whether to animate or not GLuint vertexshader, fragmentshader, shaderprogram ; // shaders const int DEMO = 0 ; // ** NEW ** To turn on and off features Basic geometry setup for cubes (pillars), colors Texture Mapping (wooden floor) Geometry Basic Setup const int numobjects = 2 ; // number of objects for buffer const int numperobj = 3 ; const int ncolors = 4 ; GLuint buffers[numperobj*numobjects+ncolors] ; // ** NEW ** List of buffers for geometric data GLuint objects[numobjects] ; // For each object GLenum PrimType[numobjects] ; GLsizei NumElems[numobjects] ; // Floor Geometry is specified with a vertex array // Same for other Geometry (Cube) // The Buffer Offset Macro is from Red Book, page 103, 106 #define BUFFER_OFFSET(bytes) ((GLubyte *) NULL + (bytes)) #define NumberOf(array) (sizeof(array)/sizeof(array[0])) enum {Vertices, Colors, Elements ; // For arrays for object enum {FLOOR, CUBE ; // For objects, for the floor 1

Cube geometry (for pillars) const GLfloat wd = 0.1 ; const GLfloat ht = 0.5 ; const GLfloat _cubecol[4][3] = { {1.0, 0.0, 0.0, {0.0, 1.0, 0.0, {0.0, 0.0, 1.0, {1.0, 1.0, 0.0 ; const GLfloat cubeverts[8][3] = { {-wd, -wd, 0.0, {-wd, wd, 0.0, {wd, wd, 0.0, {wd, -wd, 0.0, {-wd, -wd, ht, {wd, -wd, ht, {wd, wd, ht, {-wd, wd, ht ; GLfloat cubecol[8][3] ; const GLubyte cubeinds[6][4] = { {0, 1, 2, 3, // BOTTOM {4, 5, 6, 7, // TOP {0, 4, 7, 1, // LEFT {0, 3, 5, 4, // FRONT {3, 2, 6, 5, // RIGHT {1, 7, 6, 2 // BACK ; Cube Geometry (separate Color) // Simple function to set the color separately. Takes out colors void initobjectnocol(gluint object, GLfloat * vert, GLint sizevert, GLubyte * inds, GLint sizeind, GLenum type) { int offset = object * numperobj ; glbindbuffer(gl_array_buffer, buffers[vertices+offset]) ; glbufferdata(gl_array_buffer, sizevert, vert,gl_static_draw); glvertexpointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)) ; glenableclientstate(gl_vertex_array) ; glbindbuffer(gl_element_array_buffer,buffers[elements+offset]) ; glbufferdata(gl_element_array_buffer, sizeind, inds,gl_static_draw); PrimType[object] = type ; NumElems[object] = sizeind ; Cube Colors Drawing with Cube Colors // Simple function to init a bunch of color buffers for the cube void initcolorscube (void) { int base = numobjects * numperobj ; for (int i = 0 ; i < ncolors ; i++) { for (int j = 0 ; j < 8 ; j++) for (int k = 0 ; k < 3 ; k++) cubecol[j][k] = _cubecol[i][k] ; glbindbuffer(gl_array_buffer, buffers[base+i]) ; glbufferdata(gl_array_buffer, sizeof(cubecol), cubecol,gl_static_draw); glcolorpointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)) ; glenableclientstate(gl_color_array) ; //in init initobjectnocol(cube, (GLfloat *) cubeverts, sizeof(cubeverts), (GLubyte *) cubeinds, sizeof (cubeinds), GL_QUADS) ; // And a function to draw with them, similar to drawobject but with color void drawcolor(gluint object, GLuint color) { int offset = object * numperobj ; int base = numobjects * numperobj ; glbindbuffer(gl_array_buffer, buffers[vertices+offset]) ; glvertexpointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)) ; glenableclientstate(gl_vertex_array) ; glbindbuffer(gl_array_buffer, buffers[base+color]) ; // Set color glcolorpointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)) ; glenableclientstate(gl_color_array) ; glbindbuffer(gl_element_array_buffer, buffers[elements+offset]) ; gldrawelements(primtype[object], NumElems[object], GL_UNSIGNED_BYTE, BUFFER_OFFSET(0)) ; Basic geometry setup for cubes (pillars), colors Texture Mapping (wooden floor) Summary OpenGL Vertex Transforms Object coords (x y z w) t vertex Modelview matrix [Object Transforms and glm::lookat] Eye coordinates (used for lighting) Projection matrix [3D to 2D, usually glm::perspective] Clip coordinates Perspective Divide (Dehomogenization) Normalized Device Coordinates Viewport Transform (glviewport) Window Coords 2

Transformations Drawing Pillars 1 (in display) Matrix Stacks glpushmatrix, glpopmatrix, glload, glmultmatrixf Useful for hierarchically defined figures, placing pillars Mytest2 uses old-style stacks. Current recommendation is STL stacks managed yourself. (You must manage the stack yourself for HW 2). Transforms Write your own translate, scale, rotate for HW 1 and HW 2 Careful of OpenGL convetion: In old-style, Right-multiply current matrix (last is first applied). glm operators follow this sometimes. Also glulookat (glm::lookat), gluperspective (glm::perspective) Remember glulookat just matrix like any other transform, affecting modelview Must come before in code, after in action to other transforms Why not usually an issue for gluperspective? glmatrixmode(gl_modelview) ; // 1st pillar gltranslatef(-0.4,-0.4,0.0) ; drawcolor(cube, 0) ; // 2nd pillar gltranslatef(0.4,-0.4,0.0) ; drawcolor(cube, 1) ; Drawing Pillars 2 Demo // 3rd pillar gltranslatef(0.4,0.4,0.0) ; drawcolor(cube, 2) ; // 4th pillar gltranslatef(-0.4,0.4,0.0) ; drawcolor(cube, 3) ; Demo 1 Does order of drawing matter? What if I move floor after pillars in code? Is this desirable? If not, what can I do about it? Basic geometry setup for cubes (pillars), colors Texture Mapping (wooden floor) Double Buffering New primitives draw over (replace) old objects Can lead to jerky sensation Solution: double buffer. Render into back (offscreen) buffer. When finished, swap buffers to display entire image at once. Changes in main and display glutinitdisplaymode (GLUT_DOUBLE GLUT_RGB GLUT_DEPTH); glutswapbuffers() ; glflush (); 3

Turning on Depth test (Z-buffer) OpenGL uses a Z-buffer for depth tests For each pixel, store nearest Z value (to camera) so far If new fragment is closer, it replaces old z, color [ less than can be over-ridden in fragment program] Simple technique to get accurate visibility (Be sure you know what fragments and pixels are) Changes in main fn, display to Z-buffer Demo Demo 2 Does order of drawing matter any more? What if I change near plane to 0? Is this desirable? If not, what can I do about it? glutinitdisplaymode (GLUT_SINGLE GLUT_RGB GLUT_DEPTH); glclear (GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT); In init function glenable(gl_depth_test) ; gldepthfunc(gl_less) ; // The default option Basic geometry setup for cubes (pillars), colors Texture Mapping (wooden floor) Demo 3 Demo Notice how teapot cycles around And that I can pause and restart animation And do everything else (zoom etc.) while teapot moves in background Drawing Teapot (in display) Simple Animation routine // ** NEW ** Put a teapot in the middle that animates glcolor3f(0.0,1.0,1.0) ; // Deprecated command to set the color // I now transform by the teapot translation for animation */ gltranslatef(teapotloc, 0.0, 0.0) ; // The following two transforms set up and center the teapot // Remember that transforms right-multiply the stack gltranslatef(0.0,0.0,0.1) ; glrotatef(90.0,1.0,0.0,0.0) ; glutsolidteapot(0.15) ; // ** NEW ** in this assignment, is an animation of a teapot // Hitting p will pause this animation; see keyboard callback void animation(void) { teapotloc = teapotloc + 0.005 ; if (teapotloc > 0.5) teapotloc = -0.5 ; glutpostredisplay() ; 4

Keyboard callback (p to pause) GLint animate = 0 ; // ** NEW ** whether to animate or not void keyboard (unsigned char key, int x, int y) { switch (key) { case 27: // Escape to quit exit(0) ; case 'p': // ** NEW ** to pause/restart animation animate =!animate ; if (animate) glutidlefunc(animation) ; else glutidlefunc(null) ; default: Display lists (extend init for pillars) Matrix stacks and transforms (draw 4 pillars) Depth testing or z-buffering Texture mapping (wooden floor) [mytest3] New globals and basic setup GLubyte woodtexture[256][256][3] ; // texture (from grsites.com) GLuint texnames[1] ; // texture buffer GLuint istex ; // blend parameter for texturing GLuint islight ; // for lighting GLint texturing = 1 ; // to turn on/off texturing GLint lighting = 1 ; // to turn on/off lighting // In Display Simple Toggles for Keyboard case 't': // ** NEW ** to turn on/off texturing ; texturing =!texturing ; glutpostredisplay() ; case 's': // ** NEW ** to turn on/off shading (always smooth) ; lighting =!lighting ; glutpostredisplay() ; gluniform1i(islight,0) ; // Turn off lighting (except on teapot, later) gluniform1i(istex,texturing) ; drawtexture(floor,texnames[0]) ; // Texturing floor // drawobject(floor) ; gluniform1i(istex,0) ; // Other items aren't textured Adding Visual Detail Basic idea: use images instead of more polygons to represent fine scale color variation Texture Mapping Important topic: nearly all objects textured Wood grain, faces, bricks and so on Adds visual detail to scenes Can be added in a fragment shader Polygonal model With surface texture 5

Setting up texture inittexture("wood.ppm", shaderprogram) ; // in init() // Very basic code to read a ppm file // And then set up buffers for texture coordinates void inittexture (const char * filename, GLuint program) { int i,j,k ; FILE * fp ; GLint err ; assert(fp = fopen(filename,"rb")) ; fscanf(fp,"%*s %*d %*d %*d%*c") ; for (i = 0 ; i < 256 ; i++) for (j = 0 ; j < 256 ; j++) for (k = 0 ; k < 3 ; k++) fscanf(fp,"%c",&(woodtexture[i][j][k])) ; fclose(fp) ; Texture Coordinates Each vertex must have a texture coordinate: pointer to texture. Interpolate for pixels (each fragment has st) // Set up Texture Coordinates glgentextures(1, texnames) ; glbindbuffer(gl_array_buffer, buffers[numobjects*numperobj+ncolors]) ; glbufferdata(gl_array_buffer, sizeof (floortex), floortex,gl_static_draw); glactivetexture(gl_texture0) ; glenable(gl_texture_2d) ; gltexcoordpointer(2,gl_float,0,buffer_offset(0)) ; glenableclientstate(gl_texture_coord_array) ; glbindtexture (GL_TEXTURE_2D, texnames[0]) ; Specifying the Texture Image glteximage2d( target, level, components, width height, border, format, type, data ) target is GL_TEXTURE_2D level is (almost always) 0 components = 3 or 4 (RGB/RGBA) width/height MUST be a power of 2 border = 0 (usually) format = GL_RGB or GL_RGBA (usually) type = GL_UNSIGNED_BYTE, GL_FLOAT, etc Texture Image and Bind to Shader glteximage2d(gl_texture_2d,0,gl_rgb, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, woodtexture) ; gltexparameterf(gl_texture_2d, GL_TEXTURE_MAG_FILTER, GL_LINEAR) ; gltexparameterf(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_LINEAR) ; gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_S, GL_REPEAT) ; gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_T, GL_REPEAT) ; // Define a sampler. See page 709 in red book, 7th ed. GLint texsampler ; texsampler = glgetuniformlocation(program, "tex") ; gluniform1i(texsampler,0) ; // Could also be GL_TEXTURE0 istex = glgetuniformlocation(program,"istex") ; Drawing with Texture void drawtexture(gluint object, GLuint texture) { int offset = object * numperobj ; int base = numobjects * numperobj + ncolors ; glbindbuffer(gl_array_buffer, buffers[vertices+offset]) ; glvertexpointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)) ; glenableclientstate(gl_vertex_array) ; glbindbuffer(gl_array_buffer, buffers[colors+offset]) ; glcolorpointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0)) ; glenableclientstate(gl_color_array) ; // Textures glactivetexture(gl_texture0) ; glenable(gl_texture_2d) ; glbindtexture(gl_texture_2d, texture) ; glenableclientstate(gl_texture_coord_array) ; glbindbuffer(gl_array_buffer, buffers[base]) ; // Texcoords gltexcoordpointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0)) ; Final Steps for Drawing (+Demo) glbindbuffer(gl_element_array_buffer, buffers[elements+offset]) ; gldrawelements(primtype[object], NumElems[object], GL_UNSIGNED_BYTE, BUFFER_OFFSET(0)) ; Vertex shader (just pass on texture coords) gl_texcoord[0] = gl_multitexcoord0 ; Fragment shader (can be more complex blend) uniform sampler2d tex ; uniform int istex ; void main (void) { if (istex > 0) gl_fragcolor = texture2d(tex, gl_texcoord[0].st) ; 6

More on Texture (very briefly) Displacement Mapping Full lecture later in course Optimizations for efficiency Mipmapping Filtering Texture Coordinate generation Texture Matrix Environment Mapping If very ambitious, read all of chapter 9 Illumination Maps Environment Maps Quake introduced illumination maps or light maps to capture lighting effects in video games Texture map: Light map Texture map + light map: Images from Illumination and Reflection Maps: Simulated Objects in Simulated and Real Environments Gene Miller and C. Robert Hoffman SIGGRAPH 1984 Advanced Computer Graphics Animation Course Notes Texture values indexed by 3D location (x,y,z) Expensive storage, or Compute on the fly, e.g. Perlin noise à Solid textures 7