Download presentation
Presentation is loading. Please wait.
Published byClare Greer Modified over 9 years ago
1
OpenGL Programming Related Links The Official OpenGL Website http://www.opengl.org OpenGL v1.1 Redbook http://www.opengl.org/documentation/red_book_1.0/ OpenGL Specifications http://www.opengl.org/documentation/spec.html OpenGL Extension Registry http://www.opengl.org/registry/ GLUT http://www.opengl.org/resources/libraries/glut.html NeHe OpenGL Tutorials http://nehe.gamedev.net
2
OpenGL Rendering Pipeline Vertex Data Pixel Data Display List Evaluators Pixel Operations Vertex Operations Primitive Assembly Rasterization Texture Assembly Fragment Operations Frame Buffer OpenGL can be considered as a state machine. It has many states (modes) that control various aspects of the rendering pipeline. These states remain effective until you change them.
3
Vertex Operations Modeling, Viewing and Projection transformations Lighting calculations Automatic texture coordinate generation Primitive Assembly Assemble vertices into groups of triangles Clipping Polygon face culling Perspective division and viewport mapping Display ListStore OpenGL commands for later use Evaluators Convert curves and surfaces that are described by parametric equations into vertex data. (Tessellation)
4
Rasterization Convert vertex and pixel data into fragments Polygon shading Fragment: A square area corresponding to a pixel. Fragment Operations Apply texel (texture element) Fog effect calculations Antialiasing Scissor, alpha, stencil and depth tests Blending, dithering, logical operations and frame buffer masking Pixel Operations Pixel data packing and unpacking Pixel scaling, biasing and clamping Transfer pixel data inside frame buffer Transfer pixel data from frame buffer to texture memory Texture AssemblyTexture storage, sampling and mapping
5
GLU (OpenGL Utility Library) OpenGL Related Libraries A set of API functions that use low level OpenGL functions to perform some high level operations. GLU is part of OpenGL standard. Viewing and projection transformation Polygon tessellation Quadric Surfaces NURBs Platform Specific OpenGL Extensions GLX for X Window System WGL for Microsoft Windows AGL for Apple Macintosh OpenGL Extensions Libraries that add new OpenGL functions
6
GLUT (OpenGL Utility Toolkit) A platform independent toolkit for OpenGL programming. GLUT is written by Mark Kilgard and is not part of OpenGL standard. Window management functions for OpenGL rendering Callback driven event processing for input devices Idle routine and timers Simple pop-up menu Simple bitmap and outline fonts Utility functions to generate various solid and wire frame objects Headers and Libraries APIHeaderImport LibraryDynamic Linked Library OpenGLgl.hopengl32.libopengl32.dll GLUglu.hglu32.libglu32.dll GLUTglut.hglut32.libglut32.dll
7
For X Window system: #include Platform specific header files: For Microsoft Windows: #include OpenGL header file: #include GLU header file: #include OpenGL extension header file: #include “glext.h” GLUT header file: #include glext.h can be downloaded from OpenGL Extension Registry glut.h implicitly includes gl.h and glu.h. glut.h also contains linker directives that automatically include necessary library files.
8
OpenGL Command Syntax OpenGLGLUGLUTWGLGLXAGL gl*glu*glut*wgl*glX*agl* Prefix Suffix and OpenGL data type SuffixData typeC typeOpenGL type definition b8-bit integercharGLbyte s16-bit integershortGLshort i32-bit integerint, longGLint, GLsizei f32-bit floating pointfloatGLfloat, GLclampf d64-bit floating pointdoubleGLdouble, GLclampd ub8-bit unsigned integerunsigned charGLubyte, GLboolean us16-bit unsigned integerunsigned shortGLushort ui32-bit unsigned integerunsigned int, unsigned long GLuint, GLenum, GLbitfield
9
Example: glVertex2i(1, 3); glVertex3f(1.0, 2.0, 3.0); A suffix of v indicates the command takes a pointer to an array of values instead of a series of individual arguments. Example: glColor3f(1.0, 0.0, 0.0); GLfloat color_array[3] = {1.0, 0.0, 0.0}; glColor3fv(color_array); GLclampf and GLclampd indicate floating point data types that need to be clamped between –1.0 and 1.0. v-suffix
10
Using OpenGL in Windows Program Create a Window Obtain Device Context Set Pixel Format Create OpenGL Rendering Context Initialize OpenGL Rendering States Render Using OpenGL Delete OpenGL Rendering Context Release Device Context Destroy Window
11
Create a Window HWND hWnd = CreateWindow(......); Obtain Device Context HDC hDC=GetDC(hWnd); Set Pixel Format PIXELFORMATDESCRIPTOR pfd; ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion=1; pfd.dwFlags=PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType=PFD_TYPE_RGBA; pfd.cColorBits=24; pfd.cDepthBits=16; pfd.cStencilBits=0; int pixelFormat = ChoosePixelFormat(hDC, &pfd); SetPixelFormat(hDC, pixelFormat, &pfd);
12
Create OpenGL Rendering Context Delete OpenGL Rendering Context Release Device Context HGLRC hRC=wglCreateContext(hDC); wglMakeCurrent(hDC, hRC); wglMakeCurrent(NULL, NULL); wglDeleteContext(hRC); ReleaseDC(hWnd, hDC); Double Buffer Swapping SwapBuffers(hDC);
13
GLUT Window Management Functions void glutInit(int *argc, char **argv) void glutInitDisplayMode(unsigned int mode) modemeaning GLUT_RGBUse RGB color mode GLUT_INDEXUse color index mode GLUT_ALPHAUse alpha component GLUT_SINGLEUse single color buffer GLUT_DOUBLEUse double color buffers GLUT_DEPTHUse depth buffer GLUT_STENCILUse stencil buffer Initialize GLUT and process command line arguments. Initialize display mode.
14
void glutInitWindowPosition(int x, int y) void glutInitWindowSize(int width, int height) void glutCreateWindow(char *s) Initialize window position, in pixels, relative to upper left corner of the screen. Initialize window size in pixels. Create a window with string *s as window title. void glutFullScreen(void) Switch to full screen mode void glutSetWindowTitle(char *s) Set window title to string *s void glutReshapeWindow(int width, int height) Change window size.
15
cursor: GLUT_CURSOR_RIGHT_ARROW GLUT_CURSOR_LEFT_ARROW GLUT_CURSOR_INFO GLUT_CURSOR_HELP GLUT_CURSOR_WAIT GLUT_CURSOR_TEXT GLUT_CURSOR_CROSSHAIR GLUT_CURSOR_NONE void glutSetCursor(int cursor) Set cursor image void glutMainLoop(void)Start even loop and begin event processing.
16
GLUT Callback Functions void glutDisplayFunc(void (*func)(void)) Define the display callback function. This function is called when a screen redraw event is processed. Post a screen redraw event in the event queue, thus forcing a screen redraw. void glutPostRedisplay(void) void glutReshapeFunc(void (*func)(int w, int h)) Define the reshape callback function. This function is called when the window size changes. w:new window width h:new window height
17
void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y)) Define the keyboard callback function. This function is called when a key with ASCII code is pressed. void glutSpecialFunc(void (*func)(int key, int x, int y)) Define the special keyboard callback function. This function is called when a special key is pressed. key:ASCII code of the key. x, y:Mouse cursor coordinates relative to upper left corner of the window. key:Code of the special key. x, y:Mouse cursor coordinates relative to upper left corner of the window. Escape, Backspace, and Delete keys are generated as ASCII characters. Escape:27 Backspace:8 Delete:127
18
Codes for special keys in GLUT: GLUT_KEY_Fn Fn function key. n = 1, 2, …, 12 GLUT_KEY_LEFT Left directional key. GLUT_KEY_UP Up directional key. GLUT_KEY_RIGHT Right directional key. GLUT_KEY_DOWN Down directional key. GLUT_KEY_PAGE_UP Page up key. GLUT_KEY_PAGE_DOWN Page down key. GLUT_KEY_HOME Home key. GLUT_KEY_END End key. GLUT_KEY_INSERT Insert key. int glutGetModifiers(void) Returns the state of modifier keys (SHIFT, CTRL and ALT). Must be called in a keyboard or mouse callback function. Masks for return value: GLUT_ACTIVE_SHIFT; GLUT_ACTIVE_CTRL; GLUT_ACTIVE_ALT; state = glutGetModifiers(); if (state & GLUT_ACTIVE_CTRL) {Ctrl key is down;}
19
void glutMouseFunc(void (*func)(int button, int state, int x, int y)) button: GLUT_LEFT_BUTTO N; GLUT_MIDDLE_BUTTO N; GLUT_RIGHT_BUTTO N; state:GLUT_UP (release); GLUT_DOWN (press); x, y:Mouse cursor coordinates relative to upper left corner of the window Define the mouse callback function. This function is called when a mouse button is pressed or released. void glutMotionFunc(void (*func)(int x, int y)) Defines the mouse motion callback function. This function is called when the mouse moves within the window while one or more buttons are pressed. void glutPassiveMotionFunc(void (*func)(int x, int y)) Defines the mouse passive motion callback function. This function is called when the mouse moves within the window with no buttons pressed.
20
void glutTimerFunc(unsigned int t, void (*func)(int value), int value) t:Time interval (in milliseconds) before generating timer event func:Timer callback function value:An integer value passed to the timer callback function Define timer callback function. This function is called when a timer event is processed. The timer event is only generated once. void glutIdleFunc(void (*func)(void)) Define idle callback function. This function is called when the operating system is in idle state. This is useful for processing background tasks.
21
Obtain OpenGL Information void GLubyte *glGetString(GLenum name) Return a pointer to a string that contains required OpenGL information. name:GL_VENDOR GL_RENDERER GL_VERSION GL_EXTENSIONS GL_VENDOR returns the name of the company responsible for the OpenGL implementation. GL_RENDERER returns an identifier for the OpenGL renderer. GL_VERSION returns a version string with the following format: where has the following format. or..
22
OpenGL Extensions glGetString(GL_EXTENSIONS) returns a pointer to a string that contains all supported OpenGL extension names, separated by space. Extension Name Conventions Extension nameMeaningNames for functions and symbolic constants GL_ARB_*ARB approved extensionglCommandARB() GL_DEFINITION_ARB GL_EXT_*Extension supported by more than one company glCommandEXT() GL_DEFINITION_EXT GL_XYZ_*Extension supported by company XYZ glCommandXYZ() GL_DEFINITION_XYZ
23
When an ARB approved extension gets promoted to core feature in a new OpenGL version, the string “ARB” is removed from names of extension functions and symbolic constants, but the extension name remains unchanged. Extension nameFunctionality GL_ARB_multitextureMultitexture GL_ARB_texture_cube_mapCube environment map GL_ARB_vertex_programVertex program for vertex shaders GL_ARB_fragment_programFragment program for pixel shaders GL_NV_*Extensions of NVidia Corporation GL_ATI_*Extensions of ATI Technologies Inc. More information about OpenGL extensions at OpenGL Extension Registry: http://www.opengl.org/registry/ Examples of OpenGL extensions:
24
Use OpenGL Extension on Microsoft Windows 1.Include header files glext.h and wglext.h 2.Use glGetString(GL_EXTENSIONS) to check extension support. 3.Use wglGetProcAddress() to get pointers to extension functions. PFNGLACTIVETEXTUREARBPROC glActiveTexture; glActiveTexture=(PFNGLACTIVETEXTUREARBPROC) wglGetProcAddress("glActiveTextureARB"); PFNGLACTIVETEXTUREPROC glActiveTexture; glActiveTexture=(PFNGLACTIVETEXTUREPROC) wglGetProcAddress("glActiveTexture"); Example: For all OpenGL versions that support GL_ARB_multitexture: For OpenGL version 1.3 and higher:
25
OpenGL State Management void glEnable(GLenum cap) void glDisable(GLenum cap) Turns on a capability Turns off a capability GLboolean glIsEnabled(GLenum cap) Check if a capability is turned on. Return GL_TRUE if on; GL_FLASE if off. void glGetBooleanv(GLenum pname, GLboolean *pvalue) void glGetIntegerv(GLenum pname, GLint *pvalue) void glGetFloatv(GLenum pname, GLfloat *pvalue) void glGetDoublev(GLenum pname, GLdouble *pvalue) void glGetPointerv(GLenum pname, GLvoid *pvalue) Get state variables Example (Get current rendering color set by glColor): GLfloat color[4]; glGetFloatv(GL_CURRENT_COLOR, color);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.