270 likes | 404 Views
Administration. Teacher Assistant : Abed Asi - email: abedas@cs.bgu.ac.il Reception hours: by email - Building 37 / office -102 Assignments: 4-5 (C/C++) - to be submitted in pairs - frontal session ?. OpenGL . is NOT: - A programming language
E N D
Administration • Teacher Assistant : Abed Asi - email: abedas@cs.bgu.ac.il • Reception hours: by email - Building 37 / office -102 • Assignments: 4-5 (C/C++) - to be submitted in pairs - frontal session ?
OpenGL • is NOT: - A programming language - Windows API(access files, etc’) • is:- A 3D graphics and modeling library- Cross Platform - highly portable and very fast- Application programming Interface (API) - Defining more than 250 commands
OpenGL libraries • OpenGL Utility Library (GLU) - A set of utility functions that perform common (but sometimes complex) tasks. • OpenGL Utility Toolkit (GLUT) - Provides functionality common to all window systems • Open a window • Get input from mouse and keyboard • Menus • Event-driven - Very basic GUI
The pipeline Mathematically intensive stage The image is displayed on your screen Creates the color image from the geometric, color and texture data Where texture and vertex data is stored
OpenGL State Machine • An abstract model of a collection of state variables • A state variable represents: • Is a light shining on the object? • What are the properties of the light? • What are the properties of the material? • Which, if any, texture should be applied? • And more … • OpenGL keeps track of all the OpenGL state variables glEnable(GLenum capability)glDisable(GLenum capability)
OpenGL Naming Convention OpenGL commands use gl prefix GLU commands use glu prefix GLUT commands use glut prefix
Coordinates and Color 0.0f3.0f0.0f • Coordinates – glVertex(…) • Usually float • No restriction on the values range • Vectors – glNormal(..) • Color – glColor(…) • Usually float • Values of Red, Green and Blue • Range – 0.0 to 1.0 0.0f0.0f0.0f 3.0f0.0f0.0f RGB
Geometric Primitives – Draw something! • glBegin() – represents the beginning of vertices definition • glEnd() – represents the end of the definition • glVertex() is used within the definition block - has no influence out of this block • Basic types of shapes: -GL_POINTS – draw isolated points - GL_LINES – draw lines - GL_TRIANGLES – triangles - GL_QUADS – squares. - And many more….
Geometric Primitives • glBegin(Glenum mode) sets the type of primitive OpenGL will interpret the next vertices list:
Geometric Primitives – GL_Points glBegin(GL_Points) V0 V1 V2 V3 V4 V5 V6 V7 glEnd() Point size?
Geometric Primitives – GL_Lines glBegin(GL_LINES) V0 V1 V2 V3 V4 V5 V6 V7 glEnd() Line size?
Geometric Primitives – GL_LINE_LOOP glBegin(GL_LINE_LOOP) V0 V1 V2 V3 V4 V5 V6 V7 glEnd() Polygon?
Geometric Primitives – GL_TRIANGLES glBegin(GL_TRIANGLES) V0 V1 V2 V3 V4 V5 V6 V7 glEnd()
Geometric Primitives – GL_Polygon glBegin(GL_POLYGON) V0 V1 V2 V3 V4 V5 V6 V7 glEnd()
Geometric Primitives – GL_QUADS glBegin(GL_QUADS) V0 V1 V2 V3 V4 V5 V6 V7 glEnd()
Winding • The combination of order and direction of the vertices matters • Counter-Clockwise winding Front facing polygon • Clockwise winding Back facing polygon Why this is important?
Back Face Culling • Back face polygons are NOT rendered glEnable(GL_CULL_FACE) glCullFace(GL_BACK) glFrontFace(…) changes the default behavior See code example 1
Coordinate Clipping • The clipping area is the region of Cartesian space that occupies the window glOrtho(Gldoubleleft, Gldoubleright, Gldoublebottom, Gldoubletop, Gldoublenear, Gldoublefar)
Viewports • Rarely would the clipping area dimensions match the window’s dimensions • The coordinate system must be mapped • glViewport maps between logical Cartesian coordinates and physical screen pixel coordinatesglViewport (GLint x, GLint y, GLsizei width, GLsizei height );
Program structure • Most OpenGL programs have a similar structure that consists of the following functions - Main(): • defines 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 - Callbacks • Display function mydisplay( ) • Input and window functions
GLUT functions • glutInit allows application to get command line arguments and initializes system • glutInitDisplayMode requests properties for the window - RGB color - Single buffering • glutWindowSizein pixels • glutCreateWindowcreate window with title “simple” • glutDisplayFunc display callback • glutMainLoop enter infinite event loop
main #include “glut.h” int main(int argc, char** argv) { glutInit (& argc, argv) ; glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB) ; glutInitWindowSize ( 500,500) ; glutCreateWindow(“Simple”) ; glutDisplayFunc(mydisplay) ; init () ; glutMainLoop () ; }
init Void init ( ) { glClearColor(0.0, 0.0, 0.0, 1.0) ; glMatrixMode(GL_PROJECTION) ; glLoadIdentity( ) ; glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0) ; }
mydisplay void mydisplay(void){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 0.0f , 0.0f); glBegin(GL_TRIANGLES); glVertex2f(0.0 f, 0.0f); glVertex2f(1.0 f, 0.0f); glVertex2f(0.0 f, 1.0f); glEnd(); glColor3f(0.0f, 1.0f, 0.0f); glBegin(GL_LINES); glVertex2f(0.0 f, 0.5f); glVertex2f(1.0 f, 0.5f); glEnd(); glFlush(); }
Your ‘Hello World’ in OpenGL • Expand code example 2 to draw a sixth shape • Draw a new INTERESTING shape in a new color • This is a warm up mission; should not take you more than an hour