OpenGL Programming Related Links The Official OpenGL Website OpenGL v1.1 Redbook

Post on 03-Jan-2016

244 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

OpenGL Programming Related Links

The Official OpenGL Websitehttp://www.opengl.org

OpenGL v1.1 Redbookhttp://www.opengl.org/documentation/red_book_1.0/

OpenGL Specificationshttp://www.opengl.org/documentation/spec.html

OpenGL Extension Registryhttp://www.opengl.org/registry/

GLUThttp://www.opengl.org/resources/libraries/glut.html

NeHe OpenGL Tutorialshttp://nehe.gamedev.net

OpenGL Rendering Pipeline

Vertex Data

Pixel Data

Display List

Evaluators

PixelOperations

VertexOperations

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.

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 List Store OpenGL commands for later use

Evaluators

Convert curves and surfaces that are described by parametric equations into vertex data. (Tessellation)

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 Assembly Texture storage, sampling and mapping

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

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

API Header Import Library Dynamic Linked Library

OpenGL gl.h opengl32.lib opengl32.dll

GLU glu.h glu32.lib glu32.dll

GLUT glut.h glut32.lib glut32.dll

For X Window system:#include <X11/Xlib.h>#include <GL/glx.h>

Platform specific header files:

For Microsoft Windows:#include <windows.h>

OpenGL header file:

#include <GL/gl.h>

GLU header file:

#include <GL/glu.h>

OpenGL extension header file:

#include “glext.h”

GLUT header file:

#include <GL/glut.h>

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.

OpenGL Command Syntax

OpenGL GLU GLUT WGL GLX AGL

gl* glu* glut* wgl* glX* agl*

Prefix

Suffix and OpenGL data type

Suffix Data type C type OpenGL type definition

b 8-bit integer char GLbyte

s 16-bit integer short GLshort

i 32-bit integer int, long GLint, GLsizei

f 32-bit floating point float GLfloat, GLclampf

d 64-bit floating point double GLdouble, GLclampd

ub 8-bit unsigned integer unsigned char GLubyte, GLboolean

us 16-bit unsigned integer unsigned short GLushort

ui 32-bit unsigned integer unsigned int, unsigned long

GLuint, GLenum, GLbitfield

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

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

Create a WindowHWND hWnd = CreateWindow(......);

Obtain Device ContextHDC 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);

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 SwappingSwapBuffers(hDC);

GLUT Window Management Functions

void glutInit(int *argc, char **argv)

void glutInitDisplayMode(unsigned int mode)

mode meaningGLUT_RGB Use RGB color mode

GLUT_INDEX Use color index mode

GLUT_ALPHA Use alpha component

GLUT_SINGLE Use single color buffer

GLUT_DOUBLE Use double color buffers

GLUT_DEPTH Use depth buffer

GLUT_STENCIL Use stencil buffer

Initialize GLUT and process command line arguments.

Initialize display mode.

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.

cursor:GLUT_CURSOR_RIGHT_ARROWGLUT_CURSOR_LEFT_ARROWGLUT_CURSOR_INFOGLUT_CURSOR_HELPGLUT_CURSOR_WAITGLUT_CURSOR_TEXTGLUT_CURSOR_CROSSHAIRGLUT_CURSOR_NONE

void glutSetCursor(int cursor)Set cursor image

void glutMainLoop(void)

Start even loop and begin event processing.

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 widthh: new window height

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: 27Backspace: 8 Delete: 127

Codes for special keys in GLUT:

GLUT_KEY_Fn Fn function key. n = 1, 2, …, 12GLUT_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;}

void glutMouseFunc(void (*func)(int button, int state, int x, int y))

button: GLUT_LEFT_BUTTON;GLUT_MIDDLE_BUTTON;GLUT_RIGHT_BUTTON;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.

void glutTimerFunc(unsigned int t, void (*func)(int value), int value)

t: Time interval (in milliseconds) before generating timer eventfunc: Timer callback functionvalue: 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.

Obtain OpenGL Information

void GLubyte *glGetString(GLenum name)

Return a pointer to a string that contains required OpenGL information.name: GL_VENDOR

GL_RENDERERGL_VERSIONGL_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:<version number><space><vender specific information>where <version number> has the following format

<major number>.<minor number>or <major number>.<minor number>.<release number>

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 name Meaning Names for functions and symbolic constants

GL_ARB_* ARB approved extension glCommandARB()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

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 name Functionality

GL_ARB_multitexture Multitexture

GL_ARB_texture_cube_map Cube environment map

GL_ARB_vertex_program Vertex program for vertex shaders

GL_ARB_fragment_program Fragment 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:

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:

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);

top related