Teacher Assistant : Tamir Grossinger email: tamirgr@gmail.com Reception hours: by email - Building 37 / office -102 Assignments: 4 programing using C++ 1 theoretical You can find everything you need in the red book:opengl programing guide 2
C++ is regarded as an intermediate-level programing language C++ has object oriented features C++ has directed access to the memory and has no garbage collector C++ has very good performance compare to other high level programing languages 3
is NOT: - A programming language (as we study it) - 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 4
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 5
6
Mathematically intensive stage The image is displayed on your screen Where texture and vertex data is stored Creates the color image from the geometric, color and texture data 7
OpenGL commands use gl prefix GLU commands use glu prefix GLUT commands use glut prefix 8
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.0f 3.0f 0.0f 0.0f 0.0f 0.0f 3.0f 0.0f 0.0f R G B 9
10
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. 11
glbegin(glenum mode) sets the type of primitive OpenGL will interpret the next vertices list: 12
glbegin(gl_points) V0 V1 V2 V3 V4 V5 V6 V7 glend() Point size? 13
glbegin(gl_lines) V0 V1 V2 V3 V4 V5 V6 V7 glend() Line size? 14
glbegin(gl_line_loop) V0 V1 V2 V3 V4 V5 V6 V7 glend() Polygon? 15
glbegin(gl_triangles) V0 V1 V2 V3 V4 V5 V6 V7 glend() 16
glbegin(gl_polygon) V0 V1 V2 V3 V4 V5 V6 V7 glend() 17
glbegin(gl_quads) V0 V1 V2 V3 V4 V5 V6 V7 glend() 18
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 coordinates When we change window s dimensions OpenGL calculate again image size and fit it to the window glviewport (GLint x, GLint y, GLsizei width, GLsizei height ); 19
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 20
glutinit allows application to get command line arguments and initializes system glutinitdisplaymode requests properties for the window - RGB color - Single buffering glutinitwindowsize in pixels glutcreatewindow create window with title simple glutdisplayfunc display callback glutmainloop enter infinite event loop 21
#include glut.h int main(int argc, char** argv) { glutinit (& argc, argv) ; glutinitdisplaymode (GLUT_SINGLE) ; glutinitwindowsize ( 500,500) ; glutcreatewindow( Simple ) ; glutdisplayfunc(mydisplay) ; } init () ; glutmainloop () ; 22
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) ; 23
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); gllinewidth(8.4); glbegin(gl_lines); glvertex2f(0.0 f, 0.5f); glvertex2f(1.0 f, 0.5f); glend(); glflush(); } 24
You should download from the course site a zip file includes: glut32.dll glut32.lib, glut.h Copy it to the project folder (where the source code and extract it there) In Visual studio go to project menu -> properties -> C/C++ -> general In the right menu choose additional include directory and add glut.h path 25
26
Choose linker in the left menu -> input -> additional dependencies in the right menu -> choose edit and add glut32.lib Now after you apply changes you can run your program (press ctrl F5). 27
For lifeg.c you have to include opengl libraries: gcc hello.c lgl lglu lglut o hello 28
Expand code example 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 29