Programming with OpenGL Part 2: Complete Programs

Slides:



Advertisements
Similar presentations
OpenGL Open a Win32 Console Application in Microsoft Visual C++.
Advertisements

OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
Chapter 2: Graphics Programming
Pemrograman OpenGL Dasar
 The success of GL lead to OpenGL (1992), a platform-independent API that was  Easy to use  Close enough to the hardware to get excellent performance.
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
30/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 1: Introduction.
Programming with OpenGL Part 0: 3D API March 1, 2007.
Open Graphics Library (OpenGL)
Using OpenGL in Visual C++ Opengl32.dll and glu32.dll should be in the system folder Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
Graphics Systems and OpenGL. Business of Generating Images Images are made up of pixels.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Programming with OpenGL Review.
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 43 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 43 Computer Graphics Programming with OpenGL I.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 6: Three Dimensions Ed Angel Professor.
Introduction to Graphics Programming. Graphics API.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
CS 480/680 Computer Graphics Programming with Open GL Part 2: Complete Programs Dr. Frederick C Harris, Jr. Fall 2011.
Hank Childs, University of Oregon Oct. 28th, 2016 CIS 441/541: Introduction to Computer Graphics Lecture 16: textures.
Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran
Programming with OpenGL Part 1: Background
OpenGL Transformations
Programming with OpenGL Part 1: Background
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 1: Background
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
Programming with OpenGL Part 3: Three Dimensions
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
Introduction to Computer Graphics with WebGL
Models and Architectures
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Programming with OpenGL
Programming with OpenGL Part 1: Background
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 3: Three Dimensions
Introduction to OpenGL
Introduction to Computer Graphics
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 1: Background
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 1: Background
Programming with OpenGL Part 6: Three Dimensions
Programming with OpenGL Part 3: Shaders
Programming with OpenGL Part 3: Three Dimensions
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Input and Interaction Ed Angel Professor Emeritus of Computer Science,
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Programming with OpenGL Part 3: Three Dimensions
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Presentation transcript:

Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Objectives Build a complete first program Simple viewing Introduce shaders Introduce a standard program structure Simple viewing Two-dimensional viewing as a special case of three-dimensional viewing Initialization steps and program structure E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Program Structure Most OpenGL programs have a similar structure that consists of the following functions main(): specifies the callback functions opens one or more windows with the required properties enters event loop (last executable statement) init(): sets the state variables Viewing Attributes initShader():read, compile and link shaders callbacks Display function Input and window functions E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

simple.c revisited main() function similar to last lecture Mostly GLUT functions init() will allow more flexible colors initShader() will hides details of setting up shaders for now Key issue is that we must form a data array to send to GPU and then render it E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

main.c includes gl.h specify window properties display callback #include <GL/glew.h> #include <GL/glut.h> int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("simple"); glutDisplayFunc(mydisplay); glewInit(); init(); glutMainLoop(); } includes gl.h specify window properties display callback set OpenGL state and initialize shaders enter event loop E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window (the rendering context) RGB color Single buffering Properties logically ORed together glutWindowSize in pixels glutWindowPosition from top-left corner of display glutCreateWindow create window with title “simple” glutDisplayFunc display callback glutMainLoop enter infinite event loop E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Immediate Mode Graphics Geometry specified by vertices Locations in space( 2 or 3 dimensional) Points, lines, circles, polygons, curves, surfaces Immediate mode Each time a vertex is specified in application, its location is sent to the GPU Old style uses glVertex Creates bottleneck between CPU and GPU Removed from OpenGL 3.1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Retained Mode Graphics Put all vertex and attribute data in array Send array to GPU to be rendered immediately Almost OK but problem is we would have to send array over each time we need another render of it Better to send array over and store on GPU for multiple renderings E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Display Callback Once we get data to GLU, we can initiate the rendering with a simple callback Arrays are buffer objects that contain vertex arrays void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_TRIANGLES, 0, 3); glFlush(); } E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Vertex Arrays Vertices can have many attributes Position Color Texture Coordinates Application data A vertex array holds these data Using types in vec.h point2 vertices[3] = {point2(0.0, 0.0), point2( 0.0, 1.0), point2(1.0, 1.0)}; E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Vertex Array Object Bundles all vertex data (positions, colors, ..,) Get name for buffer then bind At this point we have a current vertex array but no contents Use of glBindVertexArray lets us switch between VBOs Glunit abuffer; glGenVertexArrays(1, &abuffer); glBindVertexArray(abuffer); E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Buffer Object Buffers objects allow us to transfer large amounts of data to the GPU Need to create, bind and identify data Data in current vertex array is sent to GPU Gluint buffer; glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(points), points); E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Initialization Vertex array objects and buffer objects can be set up on init() Also set clear color and other OpeGL parameters Also set up shaders as part of initialization Read Compile Link First let’s consider a few other issues E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Coordinate Systems The units in points are determined by the application and are called object, world, model or problem coordinates Viewing specifications usually are also in object coordinates Eventually pixels will be produced in window coordinates OpenGL also uses some internal representations that usually are not visible to the application but are important in the shaders E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

OpenGL Camera OpenGL places a camera at the origin in object space pointing in the negative z direction The default viewing volume is a box centered at the origin with sides of length 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Orthographic Viewing In the default orthographic view, points are projected forward along the z axis onto the plane z=0 z=0 z=0 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Viewports Do not have use the entire window for the image: glViewport(x,y,w,h) Values in pixels (window coordinates) E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Transformations and Viewing In OpenGL, projection is carried out by a projection matrix (transformation) Transformation functions are also used for changes in coordinate systems Pre 3.0 OpenGL had a set of transformation functions which have been deprecated Three choices Application code GLSL functions vec.h and mat.h E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012