Introduction to OpenGL Banafsheh Azari http://www.uni-weimar.de/cms/medien/cg.html
What You ll See Today What is OpenGL? Related Libraries OpenGL Command Syntax B. Azari http://www.uni-weimar.de/cms/medien/cg.html 2
What is OpenGL? Software interface to graphics hardware Hardware independent No commands for performing windowing tasks Provides commands to draw geometric primitivespoints, lines and polygons. http://fly.cc.fer.hr/~unreal/theredbook/ B. Azari http://www.uni-weimar.de/cms/medien/cg.html 3
Related Libraries Glu OpenGL Utility Library Standard part of OpenGL implementation Glut OpenGL Utility Toolkit Window system-independent toolkit http://www.opengl.org/developers/documentatio n/glut/glut-3.spec.pdf B. Azari http://www.uni-weimar.de/cms/medien/cg.html 4
OpenGL Command Syntax Prefix gl and initial capital letters for each word making up the command name (e.g. glclear() ) Constants begin always with GL_, use all capital letters, and use underscores to separate words (e.g. GL_COLOR_BUFFER_BIT) OpenGL brings its own data types (e.g. GLbyte, GLint) B. Azari http://www.uni-weimar.de/cms/medien/cg.html 5
OpenGL Command Formats glvertex3fv( v ) Number of components 2 - (x,y) 3 - (x,y,z) 4 - (x,y,z,w) Data Type b - byte ub - unsigned byte s - short us - unsigned short i - int ui - unsigned int f - float d - double Vector omit v for scalar form glvertex2f( x, y ) 6
Header Files B. Azari http://www.uni-weimar.de/cms/medien/cg.html 7
Additional Dependencies the glut32.lib is needed it is located in../../lib B. Azari http://www.uni-weimar.de/cms/medien/cg.html 8
GLUT Callback Functions whenever you want to use GLUT you need this functions void initglut(int argc, char **argv); void displayfunc(void); void idlefunc(void); void reshapefunc(int width, int height); void mousefunc(int button, int state, int x, int y); void mousemotionfunc(int x, int y); void keyboardfunc(unsigned char key, int x, int y); void specialfunc(int key, int x, int y); B. Azari http://www.uni-weimar.de/cms/medien/cg.html 9
OpenGL Initialization glutinit: Initializes GLUT must be called first glutinitwindowsize: Specifies the size of the window in pixels glutinitwindowposition: Specifies the screen location glutinitdisplaymode: Specifies color model, buffer, etc. glutcreatewindow: Create a window B. Azari http://www.uni-weimar.de/cms/medien/cg.html 10
User Input Callbacks (mouse) mouse function: queries button states and positions mouse motion function: tells GLUT what to do when the mouse moves B. Azari http://www.uni-weimar.de/cms/medien/cg.html 11
User Input Callbacks(keyboard) keyboard function: tells GLUT what to do when keys are pressed B. Azari http://www.uni-weimar.de/cms/medien/cg.html 12
Rendering Callback Do all of our drawing here glutdisplayfunc(displayfunc); void displayfunc(void) { glutswapbuffers(); } B. Azari http://www.uni-weimar.de/cms/medien/cg.html 13
OpenGL as a state machine you can put OpenGL into various states which remain in effect until changed states are things like: current color viewing and projection transformations void displayfunc(void) { // set clear color to white glclearcolor(0.0f, 0.0f, 0.7f, 1.0f); // clear color and depth buffer glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); // we enable the depth test glenable(gl_depth_test); // and set our depth function gldepthfunc(gl_less); glutswapbuffers(); B. Azari http://www.uni-weimar.de/cms/medien/cg.html 14
let s add an object GLUT provides some simple ones. Let s add a pyramid. glbegin(gl_triangles); glcolor3f(1.0f,0.0f,0.0f); glvertex3f( 0.0f, 10.0f, 0.0f); glvertex3f(-10.0f,-10.0f, 10.0f); glvertex3f( 10.0f,-10.0f, 10.0f); // Red // Top Of Triangle (Front) // Left Of Triangle (Front) // Right Of Triangle (Front)... glcolor3f(1.0f,0.0f,1.0f); glvertex3f( 0.0f, 10.0f, 0.0f); glvertex3f(-10.0f,-10.0f,-10.0f); glvertex3f(-10.0f,-10.0f, 10.0f); // pink // Top Of Triangle (Left) // Left Of Triangle (Left) // Right Of Triangle (Left) glend(); // Done Drawing The Pyramid Where is our object???????? B. Azari http://www.uni-weimar.de/cms/medien/cg.html 15
OpenGL s coordinate system OpenGL provides a lefthanded coordinate system By default OpenGL s virtual camera is placed at the origin of this coordinate system looking in negative z-direction camera we placed our teapot at this point as well so, yet the camera is inside the teapot y y x x z z B. Azari http://www.uni-weimar.de/cms/medien/cg.html 16
OpenGL s coordinate system we activate the model view matrix and reset it now translate the camera 50 units // switch to modelview matrix glmatrixmode(gl_modelview); // reset current matrix glloadidentity(); // translate scene gltranslatef(0.0f, 0.0f, -50.0f); // draw scene drawscene(); B. Azari http://www.uni-weimar.de/cms/medien/cg.html 17
OpenGL s coordinate system Let s add a white teapot. void drawscene() { //set color glcolor3f(1.0f,1.0f,1.0f); } // draw teapot glutsolidteapot(10.0); glpopmatrix(); B. Azari http://www.uni-weimar.de/cms/medien/cg.html 18
Lighting there is no spatial illumination and the teapot looks kinda flat we will use OpenGL s lights to illuminate the teapot. take a look at the setlights() function Just press l to active lighting B. Azari http://www.uni-weimar.de/cms/medien/cg.html 19
void setlights() { glenable(gl_lighting); // Setup light GLfloat light0_ambient[4] = {1.0f, 1.0f, 1.0f, 1.0f}; GLfloat light0_diffuse[4] = {1.0f, 1.0f, 1.0f, 1.0f}; GLfloat light0_specular[4] = {1.0f, 1.0f, 1.0f, 1.0f}; GLfloat light0_position[4] = {-10.0f, 10.0f, 10.0f, 0.0f}; gllightfv(gl_light0, GL_AMBIENT, light0_ambient); gllightfv(gl_light0, GL_DIFFUSE, light0_diffuse); gllightfv(gl_light0, GL_SPECULAR, light0_specular); gllightfv(gl_light0, GL_POSITION, light0_position); B. Azari http://www.uni-weimar.de/cms/medien/cg.html 20
Model Transformations in mousemotionfunc() an array stores mouse motion in x a this values can be used as angle of rotation in y direction // reset current matrix glloadidentity(); // translate scene gltranslatef(0.0f, 0.0f, -50.0f); // add rotation glrotatef( rotation[0], 1.0f, 0.0f, 0.0f ); glrotatef( rotation[1], 0.0f, 1.0f, 0.0f ); // set lights setlights(); glenable(gl_light0); glenable(gl_light1); // draw scene drawscene(); glutswapbuffers(); B. Azari http://www.uni-weimar.de/cms/medien/cg.html 21
Texturing there is a texture we can apply to our teapot. take a look at texturegenerator () and drawscene(). just press t to active texturing, the texture is already loaded // loading a random image glgentextures(2, &image);// Create The Texture // Typical Texture Generation Using Data from the raw... glbindtexture(gl_texture_2d, image); glpixelstorei (GL_UNPACK_ALIGNMENT, 1); texture_bytes = loadtexture("../../images/rock.raw", 3, 512,512); glteximage2d(gl_texture_2d, 0, // 0 for now // GL_TEXTURE_2D GL_RGB, // format OpenGL uses for image 512,512, // width and height 0, // border of the image GL_RGB, GL_UNSIGNED_BYTE, texture_bytes); // because pixels are stored in RGB format // because pixels are stored as unsigned numbers // our check board pixel data B. Azari http://www.uni-weimar.de/cms/medien/cg.html 22
void drawscene() { //set color glcolor3f(1.0f,1.0f,1.0f); //enable texturing if(!texture) glenable(gl_texture_2d); else gldisable(gl_texture_2d); // draw teapot glutsolidteapot(10.0); B. Azari http://www.uni-weimar.de/cms/medien/cg.html 23
References The Official Guide to Learning OpenGL, Dave Shreiner, Mason Woo, Jackie Neider, Addison- Wesley Longman, ISBN: 0321335732, 2005 http://glprogramming.com/red/ http://opengl.org http://www.khronos.org/opengl/ http://wiki.delphigl.com/ B. Azari http://www.uni-weimar.de/cms/medien/cg.html 24