Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2)

Similar documents
Building Models. Prof. George Wolberg Dept. of Computer Science City College of New York

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Computer Graphics (CS 543) Lecture 4a: Linear Algebra for Graphics (Points, Scalars, Vectors)

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

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

CS475/CS675 - Computer Graphics. OpenGL Drawing

Building Models. Objectives Introduce simple data structures for building polygonal models. Vertex lists Edge lists

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

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

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

CS 432 Interactive Computer Graphics

Tutorial 04. Harshavardhan Kode. September 14, 2015

Computer Graphics CS 543 Lecture 5 (Part 2) Implementing Transformations

CS 543 Lecture 1 (Part 3) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

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

Computer Graphics (CS 543) Lecture 4a: Introduction to Transformations

CS452/552; EE465/505. Models & Viewing

Objectives. Programming with OpenGL Part 5: More GLSL. Program Object. Reading a Shader. Shader Reader. Linking Shaders with Application

Computer Graphics (CS 543) Lecture 1 (Part 2): Introduction to OpenGL/GLUT (Part 1)

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

Computer Graphics (CS 4731) OpenGL/GLUT (Part 2)

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

Programming with OpenGL Complete Programs Objectives Build a complete first program

Interaction. CSCI 420 Computer Graphics Lecture 3

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

Meshing and Geometry

CS452/552; EE465/505. Image Formation

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

6.S096 Lecture 9 Visualization

CS452/552; EE465/505. Transformations

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

Programming with OpenGL Part 5: More GLSL. Ed Angel Professor Emeritus of Computer Science University of New Mexico

WebGL A quick introduction. J. Madeira V. 0.2 September 2017

Recall: Indexing into Cube Map

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

Programming with OpenGL Part 3: Three Dimensions

CS452/552; EE465/505. Review & Examples

Lecture 3 Advanced Computer Graphics (CS & SE )

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

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

Computer Graphics (CS 4731) OpenGL/GLUT(Part 1)

Interaction Computer Graphics I Lecture 3

Introduction to Computer Graphics with WebGL

Building Models. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

CS 548: COMPUTER GRAPHICS PORTRAIT OF AN OPENGL PROGRAM SPRING 2015 DR. MICHAEL J. REALE

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

8 Three-Dimensional Object Representations. Chapter 8. Three-Dimensional Object Representations. Department of Computer Science and Engineering 8-1

Building Models. Objectives. Introduce simple data structures for building polygonal models. OpenGL vertex arrays. Vertex lists Edge lists

Computer Graphics (CS 4731) & 2D Graphics Systems

Starting out with OpenGL ES 3.0. Jon Kirkham, Senior Software Engineer, ARM

We assume that you are familiar with the following:

Vertex Buffer Objects

Vertex Buffer Objects. Vertex Buffer Objects: The Big Idea

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

COMP371 COMPUTER GRAPHICS

Display Lists. Conceptually similar to a graphics file. In client-server environment, display list is placed on server

CS Computer Graphics: Hidden Surface Removal

Programming with WebGL Part 1: Background. CS 432 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Introduction to Computer Graphics with WebGL

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

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

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

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

Interaction. CSCI 480 Computer Graphics Lecture 3

Computer Graphics (CS 543) Lecture 10: Soft Shadows (Maps and Volumes), Normal and Bump Mapping

CS 4731 Lecture 3: Introduction to OpenGL and GLUT: Part II. Emmanuel Agu

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

Introduction to Computer Graphics with OpenGL/GLUT

Rendering Objects. Need to transform all geometry then

Objectives. Programming with WebGL Part 1: Background. Retained vs. Immediate Mode Graphics. Early History of APIs. PHIGS and X.

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

Lecture 19: OpenGL Texture Mapping. CITS3003 Graphics & Animation

Order of Transformations

3D Modeling. 3D Modeling 1

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

Computer Graphics. OpenGL

CS 432 Interactive Computer Graphics

An Introduction to OpenGL Programming

Shaders. Slide credit to Prof. Zwicker

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

UNIT 4 GEOMETRIC OBJECTS AND TRANSFORMATIONS-1

Announcement. Homework 1 has been posted in dropbox and course website. Due: 1:15 pm, Monday, September 12

GEOMETRIC OBJECTS AND TRANSFORMATIONS I

API Background. Prof. George Wolberg Dept. of Computer Science City College of New York

We will use WebGL 1.0. WebGL 2.0 is now being supported by most browsers but requires a better GPU so may not run on older computers or on most cell

Objectives. transformation. General Transformations. Affine Transformations. Notation. Pipeline Implementation. Introduce standard transformations

Tutorial 12: Real-Time Lighting B

3d Programming I. Dr Anton Gerdelan

Lecture 3. Programming with OpenGL GLUT + GLEW. CS 354 Computer Graphics Sunday, January 20, 13

Computer Graphics (4731) Lecture 4: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

Computer Graphics Course 2005

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

Geometry Shaders. And how to use them

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Computer Graphics. Bing-Yu Chen National Taiwan University

RECITATION - 1. Ceng477 Fall

Computer Graphics (CS 543) Lecture 2b: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

Tutorial 1: Your First Triangle!

Computer Graphics (CS 543) Lecture 9 (Part 2): Clipping. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Transcription:

Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2) Prof Emmanuel Agu Computer Science Dept. Worcester Polytechnic Institute (WPI)

Modeling a Cube In 3D, declare vertices as (x,y,z) using point3 v[3] Define global arrays for vertices and colors typedef vex3 point3; point3 vertices[] = {point3(-1.0,-1.0,-1.0), point3(1.0,-1.0,-1.0), point3(1.0,1.0,-1.0), point3(-1.0,1.0,-1.0), point3(-1.0,-1.0,1.0), point3(1.0,-1.0,1.0), point3(1.0,1.0,1.0), point3(-1.0,1.0,1.0)}; typedef vec3 color3; color3 colors[] = {color3(0.0,0.0,0.0), color3(1.0,0.0,0.0), color3(1.0,1.0,0.0), color(0.0,1.0,0.0), color3(0.0,0.0,1.0), color3(1.0,0.0,1.0), color3(1.0,1.0,1.0), color3(0.0,1.0,1.0});

Drawing a triangle from list of indices Draw a triangle from a list of indices into the array vertices and assign a color to each index void triangle(int a, int b, int c, int d) { vcolors[i] = colors[d]; position[i] = vertices[a]; vcolors[i+1] = colors[d]); position[i+1] = vertices[b]; vcolors[i+2] = colors[d]; position[i+2] = vertices[c]; i+=3; }

Normal Vector Normal vector: Direction of each polygon Each mesh polygon has a normal vector Normal vector used in shading Normal vector light vector determines shading

Draw cube from faces void colorcube( ) { quad(0,3,2,1); quad(2,3,7,6); quad(0,4,7,3); quad(1,2,6,5); quad(4,5,6,7); quad(0,1,5,4); } 1 5 6 2 4 7 Normal vector Note: vertices ordered so that we obtain correct outward facing normals 0 3

Old Way: Inefficient Previously drew cube by its 6 faces using 6 glbegin, 6 glend 6 glcolor 24 glvertex More commands if we use texture and lighting E.g: to draw each face glbegin(gl_quad) glvertex(x1, y1, z1); glvertex(x2, y2, z2); glvertex(x3, y3, z3); glvertex(x4, y4, z4); glend( );

Vertex Arrays Previously: OpenGL provided a facility called vertex arrays for storing rendering data Six types of arrays were supported initially Vertices Colors Color indices Normals Texture coordinates Edge flags Now vertex arrays can be used for any attributes

New Way: Drawing the cube Drawing Similar to 2D Move array of 3D mesh vertices to vertex buffer object Draw mesh using gldrawarrays

Full Example: Rotating Cube Program behaviour: Draw colored cube Use 3 button mouse to change direction of rotation Use idle function to increment angle of rotation

Cube Vertices // Vertices of a unit cube centered at origin // sides aligned with axes point4 vertices[8] = { point4( -0.5, -0.5, 0.5, 1.0 ), point4( -0.5, 0.5, 0.5, 1.0 ), point4( 0.5, 0.5, 0.5, 1.0 ), point4( 0.5, -0.5, 0.5, 1.0 ), point4( -0.5, -0.5, -0.5, 1.0 ), point4( -0.5, 0.5, -0.5, 1.0 ), point4( 0.5, 0.5, -0.5, 1.0 ), point4( 0.5, -0.5, -0.5, 1.0 ) };

Colors // RGBA colors color4 vertex_colors[8] = { color4( 0.0, 0.0, 0.0, 1.0 ), // black color4( 1.0, 0.0, 0.0, 1.0 ), // red color4( 1.0, 1.0, 0.0, 1.0 ), // yellow color4( 0.0, 1.0, 0.0, 1.0 ), // green color4( 0.0, 0.0, 1.0, 1.0 ), // blue color4( 1.0, 0.0, 1.0, 1.0 ), // magenta color4( 1.0, 1.0, 1.0, 1.0 ), // white color4( 0.0, 1.0, 1.0, 1.0 ) // cyan };

Quad Function // quad generates two triangles (a,b,c) and (a,c,d) for each face and // assigns colors to the vertices int Index = 0; // Index goes from 1 to 6, one per face void quad( int a, int b, int c, int d ) { colors[index] = vertex_colors[a]; points[index] = vertices[a]; Index++; colors[index] = vertex_colors[b]; points[index] = vertices[b]; Index++; colors[index] = vertex_colors[c]; points[index] = vertices[c]; Index++; colors[index] = vertex_colors[a]; points[index] = vertices[a]; Index++; colors[index] = vertex_colors[c]; points[index] = vertices[c]; Index++; colors[index] = vertex_colors[d]; points[index] = vertices[d]; Index++; }

Color Cube // generate 12 triangles: 36 vertices and 36 colors void colorcube() { quad( 1, 0, 3, 2 ); quad( 2, 3, 7, 6 ); quad( 3, 0, 4, 7 ); quad( 6, 5, 1, 2 ); quad( 4, 5, 6, 7 ); quad( 5, 4, 0, 1 ); }

Initialization I void init() { colorcube(); // Generates cube data in application // Create a vertex array object GLuint vao; glgenvertexarrays ( 1, &vao ); glbindvertexarray ( vao );

Initialization II // Create and initialize a buffer object and move points // data to GPU GLuint buffer; glgenbuffers( 1, &buffer ); glbindbuffer( GL_ARRAY_BUFFER, buffer ); glbufferdata( GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors), NULL, GL_STATIC_DRAW );

Initialization III glbuffersubdata( GL_ARRAY_BUFFER, 0, sizeof(points), points ); glbuffersubdata( GL_ARRAY_BUFFER, sizeof(points), sizeof(colors), colors ); // Load shaders and use the resulting shader program GLuint program = InitShader( "vshader36.glsl", "fshader36.glsl" ); gluseprogram( program );

Initialization IV // set up vertex arrays GLuint vposition = glgetattriblocation( program, "vposition" ); glenablevertexattribarray( vposition ); glvertexattribpointer( vposition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); GLuint vcolor = glgetattriblocation( program, "vcolor" ); glenablevertexattribarray( vcolor ); glvertexattribpointer( vcolor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points)) ); theta = glgetuniformlocation( program, "theta" );

Display Callback void display( void ) { glclear( GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT ); gluniform3fv( theta, 1, theta ); gldrawarrays( GL_TRIANGLES, 0, NumVertices ); } glutswapbuffers();

Mouse Callback void mouse( int button, int state, int x, int y ) { if ( state == GLUT_DOWN ) { switch( button ) { case GLUT_LEFT_BUTTON: axis = Xaxis; break; case GLUT_MIDDLE_BUTTON: axis = Yaxis; break; case GLUT_RIGHT_BUTTON: axis = Zaxis; break; } } }

Idle Callback void idle( void ) { theta[axis] += 0.01; if ( theta[axis] > 360.0 ) { theta[axis] -= 360.0; } } glutpostredisplay();

Hidden Surface Removal We want to see only surfaces in front of other surfaces OpenGL uses hidden surface technique called the z buffer algorithm Z buffer uses distance from viewer (depth) to determine closer objects Objects rendered so that only front objects appear in image Draw face A (front face) Do not draw faces B and C

Using OpenGL s z buffer algorithm Z buffer uses an extra buffer, (the z buffer), to store depth information as geometry travels down the pipeline 3 steps to set up Z buffer: 1. Requested in main.c glutinitdisplaymode (GLUT_SINGLE GLUT_RGB GLUT_DEPTH) 2. Enabled in init.c glenable(gl_depth_test) 3. Cleared in the display callback glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT)

3D Mesh file formats 3D meshes usually stored in 3D file format Format defines how vertices, edges, and faces are declared Over 400 different file format Polygon File Format (PLY) used a lot in graphics Originally PLY was used to store 3D files from 3D scanner We can get PLY models from web to work with We will use PLY files in this class

Georgia Tech Large Models Archive

Stanford 3D Scanning Repository Lucy: 28 million faces Happy Buddha: 9 million faces

References Angel and Shreiner Hill and Kelley