25
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

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

Embed Size (px)

Citation preview

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

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

Page 2: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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.

Page 3: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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)

Page 4: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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

Page 5: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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

Page 6: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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

Page 7: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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.

Page 8: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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

Page 9: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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

Page 10: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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

Page 11: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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

Page 12: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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

Page 13: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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.

Page 14: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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.

Page 15: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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.

Page 16: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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

Page 17: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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

Page 18: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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

Page 19: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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.

Page 20: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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.

Page 21: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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>

Page 22: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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

Page 23: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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:

Page 24: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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:

Page 25: OpenGL Programming Related Links The Official OpenGL Website  OpenGL v1.1 Redbook

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