CS 432 Interactive Computer Graphics

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

CS 432 Interactive Computer Graphics

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

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

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

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

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

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

CS 432 Interactive Computer Graphics

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

COMP371 COMPUTER GRAPHICS

Rendering. Part 1 An introduction to OpenGL

Geometry Shaders. And how to use them

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

OPENGL AND GLSL. Computer Graphics

2D Drawing Primitives

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

Rendering Objects. Need to transform all geometry then

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

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

Objectives. Topic 4: OpenGL An Example Program. simple.cpp revisited. Defining Objects in OpenGL Programs. Build a complete OpenGL program

Teacher Assistant : Tamir Grossinger Reception hours: by - Building 37 / office -102 Assignments: 4 programing using

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

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

Transformation, perspective projection, and LookAT in. Ming Ouhyoung, September 2018

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

Programming with OpenGL Part 2: Complete Programs Computer Graphics I, Fall

CPSC 436D Video Game Programming

GLSL Overview: Creating a Program

CENG 477 Introduction to Computer Graphics. Graphics Hardware and OpenGL

CS130 : Computer Graphics. Tamar Shinar Computer Science & Engineering UC Riverside

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

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

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

How OpenGL Works. Retained Mode. Immediate Mode. Introduction To OpenGL

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

Basic Graphics Programming

CS475/CS675 - Computer Graphics. OpenGL Drawing

CS 432 Interactive Computer Graphics

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

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

CS 432 Interactive Computer Graphics

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

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

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

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

Geometry Primitives. Computer Science Department University of Malta. Sandro Spina Computer Graphics and Simulation Group. CGSG Geometry Primitives

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

Computer Graphics Seminar

CS 4204 Computer Graphics

Computer Graphics. OpenGL

Drawing with OpenGL. Chapter 3. Chapter Objectives

Programming with OpenGL Complete Programs Objectives Build a complete first program

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

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

CSC 8470 Computer Graphics. What is Computer Graphics?

CS130 : Computer Graphics Lecture 2: Graphics Pipeline. Tamar Shinar Computer Science & Engineering UC Riverside

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

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

Computer Graphics with OpenGL ES (J. Han) Chapter VII Rasterizer

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

Lecture 4 of 41. Lab 1a: OpenGL Basics

CS418 OpenGL & GLUT Programming Tutorial (I) Presented by : Wei-Wen Feng 1/30/2008

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

Interaction. CSCI 420 Computer Graphics Lecture 3

Computer graphic -- Programming with OpenGL I

CS 428: Fall Introduction to. OpenGL primer. Andrew Nealen, Rutgers, /13/2010 1

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

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

Introduction to Computer Graphics with OpenGL/GLUT

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

Graphics Programming

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

CSE 167: Introduction to Computer Graphics Lecture #5: Visibility, OpenGL

Basic Graphics Programming

Lectures OpenGL Introduction

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

Programming of Graphics

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

11/1/13. Basic Graphics Programming. Teaching Assistant. What is OpenGL. Course Producer. Where is OpenGL used. Graphics library (API)

Assignment 1. Simple Graphics program using OpenGL

COMPUTER GRAPHICS LAB # 3

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

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

CS380: Computer Graphics Screen Space & World Space. Sung-Eui Yoon ( 윤성의 ) Course URL:

This library uses only GL functions but contains code for creating common objects and simplifying viewing.

Graphics Programming. 1. The Sierpinski Gasket. Chapter 2. Introduction:

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

Luiz Fernando Martha André Pereira

Programming using OpenGL: A first Introduction

Tutorial 04. Harshavardhan Kode. September 14, 2015

CS 432 Interactive Computer Graphics

The Projection Matrix

CMSC 425: Lecture 4 More about OpenGL and GLUT Tuesday, Feb 5, 2013

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

Lecture 3 Advanced Computer Graphics (CS & SE )

CS 432 Interactive Computer Graphics

OpenGL Introduction Computer Graphics and Visualization

Introduction to OpenGL/GLSL and WebGL GLSL

Transcription:

CS 432 Interactive Computer Graphics Lecture 2 Part 1 Primitives and Buffers Matt Burlick - Drexel University - CS 432 1

Rendering in OpenGL Ok, so now we want to actually draw stuff! OpenGL (like most rendering APIs) is a state machine that can render a limited set of primitives The main idea: The client program copies data to the GPU The client program then calls a sequence of Change some states Request some drawing using some data on the GPU Matt Burlick - Drexel University - CS 432 2

Rendering in OpenGL So we need to know: Available states to set How to copy data to GPU What we need to store on GPU in order to draw. Calls to draw Matt Burlick - Drexel University - CS 432 3

Rendering in OpenGL All of our objects will be specified via vertices Vertices can have attributes like: Vertex location Vertex color Vertex normal (for 3D graphics) When it comes time to draw an object we then need to tell the GPU where to get the data from I.e. where each attribute data is located. As we ll see in a moment, the data will be stored in things called vertex buffers To make things easier, the state information containing which buffers are used and where each attribute is stored within that buffer can be saved in something called a vertex array object (VAO). So let s start there! Matt Burlick - Drexel University - CS 432 4

Vertex Array Objects First thing s first, let s create a VAO to store this state information! To do this we request an ID from the GPU: GLuint abuffer; glgenvertexarrays(1, &abuffer); Once we ve created a VAO we can make it the active one with the command: glbindvertexarray(abuffer); Matt Burlick - Drexel University - CS 432 5

Vertex Array Objects Once a VAO is active: Any drawing requests will use its stored state information to determine what buffer(s) is being used, and where the attributes are within it. Any new assignment of vertex attribute related states (changes of buffers, changes to locations within buffers) will be stored/updated in this active VAO So let s start creating data and associating it with the current VAO! Matt Burlick - Drexel University - CS 432 6

Vertices Therefore, the first thing we need to do when creating an object is to specify the vertices. Lets use the data types provided in Angel s vec.h file: vec2, vec3, vec4 If we re doing a 2D application we can just specify a 2D vertex using a vec2 object (although we ll see later that we may want a 3 rd coordinate even for 2D graphics) vec2 point = vec2(0,0); //x,y We can also use this data type to specify colors vec4 blue = vec4(0,0,1,1); //r,g,b,a Matt Burlick - Drexel University - CS 432 7

Vertex Locations In this course we ll talk about various coordinate systems. The first two we ll talk about are the camera coordinate system and the window coordinate system. The window coordinate system refers to screen pixels. These go from (0,0) to (width, height) as specified in our glutinitwindowsize(width,height) function The camera coordinates go from (-1,-1) to (1,1) OpenGL automatically maps the camera coordinates to the window coordinates such that (-1,-1) (0,0) and (1,1) (width,height) Matt Burlick - Drexel University - CS 432 8

Vertex Locations So the camera coordinate system is a square. What happens if the window is not a square? Stretching We may want to force our OpenGL program to render to an area (square?) inside of the window so there is no stretching. This area is called the viewport. Graphics Window Viewport Matt Burlick - Drexel University - CS 432 9

Graphics Window Vertex Locations We can set the viewport using the glviewport function glviewport(glint x, GLint y, GLsizei w, GLsizei h); And whenever the window is reshaped, we can call this function. In the main assign a callback function: glutreshapefunc(reshape); The callback function will get passed the current window s width and height which we can then use to set the viewport: void reshape(glsizei w, GLsizei h){ GLsizei m = w; if (h<w) m = h; glviewport(0,0,m,m); } Viewport Matt Burlick - Drexel University - CS 432 10

Vertex Arrays Typically we ll store sets of vertices pertaining to objects in arrays. Depending on if your sets need to grow or not, you could use static or dynamic arrays: vec2 points[4]; vec2 *points = new vec2[4]; vector<vec2> points; You can easily get the data form this via &points[0] Remember to delete your pointers when you re done with them! Often this data can be deleted once it has been moved to the GPU Matt Burlick - Drexel University - CS 432 11

Moving Data to the GPU Ok so now we have our vertices as data on the client application (CPU). Next step is to move that data to the GPU Memory on the graphics card that are used to store vertex information are called vertex buffers. To process to move vertices onto a vertex buffer are as follows: 1. Allocate a vertex buffer object (VBO) for the data 2. Create the data (in our CPU-bound client application) 3. Make sure the desired VAO is active. 4. Bind the VBO to the current VAO 5. Move the data to the active VBO Matt Burlick - Drexel University - CS 432 12

VBOs Start by getting a valid buffer object name(s) GLuint buffer; glgenbuffers(1, &buffer); We ll probably want to make sure we are using the VAO we want, so: glbindvertexarray(abuffer); Next we ll bind the VBO to the VAO: glbindbuffer(gl_array_buffer, buffer); And finally we ll copy the data from the client application to the buffer that s currently bound to the VBO glbufferdata(gl_array_buffer, sizeof(points), points, GL_DRAWHINT); Where the most common types of GL_DRAWHINT are GL_STATIC_DRAW - Optimized if data won t change often GL_DYNAMIC_DRAW - Optimized if data will change often Matt Burlick - Drexel University - CS 432 13

Buffer Size Note: If our array is static, we can just use size(data) If our array is dynamic, we must keep track of how many elements are in it and the data type. Given we have n elements each of which are a vec2, we need to move: n*size(vec2) Matt Burlick - Drexel University - CS 432 14

Moving Data to the GPU GLuint VBO, VAO; void init(){ glclearcolor(1,0,1.0,1.0,1.0); Create a VAO to bundle together the object s attributes Get a buffer Bind this buffer to the VAO s array buffer so it s the active buffer Create data } glgenvertexarray(&vao); glbindvertexarray(vao); glgenbuffers(1,&vbo); glbindbuffer(gl_array_buffer,vbo); vec2 vertices[2]; vertices[0] = vec2(0.0,0.0); vertices[1] = vec2(1.0,1.0); Copy data to the active buffer glbufferdata(gl_array_buffer,sizeof(vertices),vertices,gl_static_draw); Matt Burlick - Drexel University - CS 432 15

Sub-Buffers Sometimes we only want to replace some of the data in a buffer Maybe only some over the vertices changed. Maybe we re storing both position and color in the same buffer and only want to change colors. To do this we can use the glbuffersubdata function: glbuffersubdata(glenum target, GLint offset, GLsizei size, void* data); Matt Burlick - Drexel University - CS 432 16

Deleting Buffers Since our graphics should be high-performing, memory management is a big deal. Therefore we should have a way to delete memory from the GPU when we re done with it. gldeletebuffers(glsizei n, const Gluint* ids); Matt Burlick - Drexel University - CS 432 17

Primitives Ok so now we have vertex locations stored on the graphics card. How does it know how to assemble these into objects? Modern OpenGL allows for assembling vertices into variations of primitives: Points Lines Triangles Any other polygons should be converted into a set of triangles. Though technically most OpenGL still allow for quadrilaterals and polygons Matt Burlick - Drexel University - CS 432 18

Point and Line Primitives Line Segments are the basic graphical entity Used to approximate curves We can choose how to render the vertices as lines: GL_POINTS Each vertex is displayed at a size of at least one pixel GL_LINES Successive pairs of vertices are interpreted as endpoints of individual segments GL_LINE_STRIP Successive vertices act as points in a continuing line strip GL_LINE_LOOP Same as GL_LINE_STRIP, but the last vertex connects to the first vertex. This creates an polygon Matt Burlick - Drexel University - CS 432 19

Line Primitives Matt Burlick - Drexel University - CS 432 20

Triangle Primitives We can also choose different ways to render triangles using vertices: GL_TRIANGLES Same idea as lines, but each successive group of three vertices specifies a new triangle GL_TRIANGLE_STRIP Similar to line-strip, but each vertex is combined with the previous 2 to create a new triangle. GL_TRIANGLE_FAN The first point is the first vertex for all triangles. The first triangle also uses the next to vertices. Subsequent triangles just use the last used vertex and one more one. This can be used to render a polygon as a set of triangles (albeit naively) Matt Burlick - Drexel University - CS 432 21

Triangles Matt Burlick - Drexel University - CS 432 22

Rendering States Some useful states that we can change include: glpointsize(float) glpolygonmode(face,mode) Where face can be GL_FRONT_AND_BACK GL_FRONT GL_BACK And mode can be GL_FILL GL_LINE GL_POINT Matt Burlick - Drexel University - CS 432 23

Rendering Objects Now that we know what the different states and assembling methods are, and since we already have vertex data on the GPU we re ready to draw! To render the object (in the display function) you: 1. Make its vertex array object active With glbindvertexarray(vao); This will switch all attribute information (like the VBO, where in the VBO attributes come from, etc..) to the states stored in the VAO. 2. Make active the desired GPU program 3. Set any OpenGL state variables you want to be applied to this object This may include uniform variables in the shader program. 4. Tell the GPU to draw the data by specifying How (the primitive types) What (starting location in buffer and how many elements) Matt Burlick - Drexel University - CS 432 24

Modern OpenGL void display(void) { glclear(gl_color_buffer_bit); glbindervertexarray (VAO); gluseprogram(program); gldrawarrays(gl_lines, 0, 32); } glflush(); Note we are not doing any calculations on the CPU nor moving any data! Only do these on the CPU if necessary Tell the GPU to draw this data as lines, using locations 0 through 32 in the buffer Matt Burlick - Drexel University - CS 432 25

Shaders void display(void) { glclear(gl_color_buffer_bit); } glbindervertexarray (VAO); gluseprogram(program); gldrawarrays(gl_lines, 0, 32); glflush(); Of course this may raise the questions.. How do the GPU know where the color information is? Or any other information about the vertices for that matter. There is one more piece to the puzzle! The GPU code!!! We call these small programs, shaders. Matt Burlick - Drexel University - CS 432 26