0% found this document useful (0 votes)
364 views4 pages

WebGL Cheat Sheet

This document provides a cheat sheet summarizing the key WebGL functions for buffers, framebuffers, renderbuffers, textures, programs, shaders, and other common WebGL operations like culling, blending, and stencil buffering. It lists the functions, their parameters, and common values for those parameters to create, manage, and use various WebGL objects and features.

Uploaded by

Praveen Mahesh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
364 views4 pages

WebGL Cheat Sheet

This document provides a cheat sheet summarizing the key WebGL functions for buffers, framebuffers, renderbuffers, textures, programs, shaders, and other common WebGL operations like culling, blending, and stencil buffering. It lists the functions, their parameters, and common values for those parameters to create, manage, and use various WebGL objects and features.

Uploaded by

Praveen Mahesh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

WebGL Cheat Sheet

Buffers

v0.2

http://blog.nihilogic.dk/

Note: It is implied that all functions and symbolic names are methods and properties on a WebGL context object

Framebuffers
Object createFramebuffer( void ) Create a framebuffer object deleteFramebuffer( Object buffer ) void Delete a framebuffer object. bindFramebuffer( ulong target, Object buffer ) void Bind a framebuffer, target must be FRAMEBUFFER. ulong checkFramebufferStatus( ulong target ) Return the framebuffer completeness status of a framebuffer object. Return values are: FRAMEBUFFER_COMPLETE FRAMEBUFFER_INCOMPLETE_ATTACHMENT FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT FRAMEBUFFER_INCOMPLETE_DIMENSIONS FRAMEBUFFER_UNSUPPORTED ulong framebufferRenderbuffer( ulong target, ulong att, ulong rbtarget, Object rbuffer ) Attach a renderbuffer object to a framebuffer object. Accepted values for attachment are: DEPTH_ATTACHMENT STENCIL_ATTACHMENT any COLOR_ATTACHMENT0

Object createBuffer( void ) Create a WebGLBuffer buffer object deleteBuffer( Object buffer ) void Delete a WebGLBuffer buffer object bindBuffer( ulong target, Object buffer ) void Bind a buffer object. Accepted values for target are: ARRAY_BUFFER void ELEMENT_ARRAY_BUFFER

bufferData( ulong target, Object dta, ulong usage )


Create and initialize a buffer object's data store. Accepted values for usage are: STREAM_DRAW DYNAMIC_DRAW STATIC_DRAW

void void

bufferData( ulong target, long size, ulong usage )


Set the size of a buffer object's data store.

bufferSubData( ulong target, ulong offset,


Object data ) Update a subset of a buffer object's data store. getBufferParameter( ulong target, ulong value ) Return parameter, pname, of a buffer object: BUFFER_SIZE BUFFER_USAGE

any

bool any

isBuffer( Object buffer )


Determine if an object is a buffer object. getParameter( ulong pname ) Relevant parameters: ARRAY_BUFFER_BINDING ELEMENT_ARRAY_BUFFER_BINDING

getFramebufferAttachmentParameter(
ulong target, ulong attachment, ulong pname ) Return attachment parameters of a framebuffer object. Accepted values for attachment are: FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE FRAMEBUFFER_ATTACHMENT_OBJECT_NAME FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL FRAMEBUFFER_ATTACHMENT_TEXTURE_ CUBE_MAP_FACE

Renderbuffers
Object createRenderbuffer( void ) Create a renderbuffer object deleteRenderbuffer( Object buffer ) void Delete a renderbuffer object. bindRenderbuffer( ulong target, Object buffer ) void Bind a renderbuffer, target must be RENDERBUFFER. getRenderbufferParameter( ulong target, any ulong pname ) Return parameter, pname, of a renderbuffer object: RENDERBUFFER_WIDTH RENDERBUFFER_HEIGHT RENDERBUFFER_INTERNAL_FORMAT RENDERBUFFER_RED_SIZE RENDERBUFFER_GREEN_SIZE RENDERBUFFER_BLUE_SIZE RENDERBUFFER_ALPHA_SIZE RENDERBUFFER_DEPTH_SIZE RENDERBUFFER_STENCIL_SIZE void

ulong framebufferTexture2D( ulong target, ulong att, ulong textarget, Object tex, ulong level ) Attach a texture image to a framebuffer object. Accepted values for textarget are: TEXTURE_2D TEXTURE_CUBE_MAP_POSITIVE_X TEXTURE_CUBE_MAP_NEGATIVE_X TEXTURE_CUBE_MAP_POSITIVE_Y TEXTURE_CUBE_MAP_NEGATIVE_Y TEXTURE_CUBE_MAP_POSITIVE_Z TEXTURE_CUBE_MAP_NEGATIVE_Z void

pixelStorei( ulong pname, long param )


Set pixel storage modes. Accepted pname values are: PACK_ALIGNMENT UNPACK_ALIGNMENT

Array readPixels( long x, long y, ulong width, ulong height, ulong format, ulong type ) Read a block of pixels from the frame buffer. Accepted format values are: ALPHA RGB RGBA Accepted type values are: UNSIGNED_BYTE UNSIGNED_SHORT_4_4_4_4 UNSIGNED_SHORT_5_5_5_1 UNSIGNED_SHORT_5_6_5 bool any

renderbufferStorage( ulong target,


ulong format, ulong width, ulong height ) Create and initialize a renderbuffer object's data store. Accepted values for format are: RGBA4 RGB5_A1 STENCIL_INDEX8 RGB565 DEPTH_COMPONENT16

bool any

isRenderbuffer( Object buffer )


Determine if an object is a renderbuffer object. getParameter( ulong pname ) Relevant parameters: RENDERBUFFER_BINDING MAX_RENDERBUFFER_SIZE

isFramebuffer( Object buffer )


Determine if an object is a framebuffer object.

getParameter( ulong pname )


Relevant parameters: RED_BITS BLUE_BITS FRAMEBUFFER_BINDING GREEN_BITS ALPHA_BITS

Program objects
Object createProgram( void ) Create a program object validateProgram( Object program ) void Validate a program object linkProgram( Object program ) void Link a program object useProgram( ulong program ) void Install a program as part of current rendering state deleteProgram( Object program ) void Delete a program object getProgramParameter( Object pgm, ulong pname ) any Return parameter, pname, from a program object: LINK_STATUS INFO_LOG_LENGTH DELETE_STATUS VALIDATE_STATUS ATTACHED_SHADERS ACTIVE_UNIFORMS ACTIVE_ATTRIBUTES ACTIVE_ATTRIBUTE_MAX_LENGTH ACTIVE_UNIFORM_MAX_LENGTH string getProgramInfoLog( Object program ) Return the information log for a program object bool isProgram( Object program ) Determine if an object is a program object. getParameter( ulong pname ) any Relevant parameters: CURRENT_PROGRAM

Textures
Object createTexture( void ) Create a texture deleteTexture( Object texture ) void Delete a texture. bindTexture( ulong target, Object texture ) void Bind a texture to a texturing target. Accepted values for target are: TEXTURE_2D void any Select active texture unit. TEXTURE_CUBE_MAP

activeTexture( ulong texture ) getTexParameter( ulong target, ulong pname )


Return parameter, pname, of a texture: TEXTURE_WRAP_S TEXTURE_WRAP_T TEXTURE_MAG_FILTER TEXTURE_MIN_FILTER

void void void

texParameterf( ulong target, ulong pname, float v ) texParameteri( ulong target, ulong pname, long v )
Set texture parameters.

texImage2D( ulong target, long level,


ulong intformat, ulong width, ulong height, long border, ulong format, ulong type, Object data ) Specify a two-dimensional texture image from a WebGLArray of pixel data. See readPixels for accepted type values. Accepted values for intformat and format are: ALPHA LUMINANCE RGB RGBA LUMINANCE_ALPHA

Shaders
Object createShader( ulong shaderType ) Create a shader object. Parameter shaderType must be VERTEX_SHADER or FRAGMENT_SHADER. compileShader( Object shader ) void Compile a shader object attachShader( Object program, Object shader ) void detachShader( Object program, Object shader ) void Attach/detach a shader object. deleteShader( Object shader ) void Delete a shader object getShaderParameter( Object shader, ulong pname ) any Return parameter, pname, from a shader object: SHADER_TYPE DELETE_STATUS COMPILE_STATUS INFO_LOG_LENGTH SHADER_SOURCE_LENGTH string getShaderInfoLog( Object shader ) Return the information log for a shader object string getShaderSource( Object shader ) shaderSource( Object shader, string source ) void Get/set the source code in a shader object 1 Array getAttachedShaders ( Object program ) Return the shader objects attached to a program. bool isShader( Object shader ) Determine if an object is a shader object. getParameter( ulong pname ) any Relevant parameters: SHADER_COMPILER MAX_VARYING_VECTORS void void

texImage2D( ulong target, long level, Object data,


[bool flipY], [bool asPreMultipliedAlpha] ) Specify a two-dimensional texture image from either an ImageData object or a HTMLImageElement, HTMLCanvasElement or HTMLVideoElement. texSubImage2D( ulong target, long level, long xoffset, long yoffset, ulong width, ulong height, ulong format, ulong type, Object data ) Specify a two-dimensional texture subimage from a WebGLArray of pixel data. texSubImage2D( ulong target, long level, long xoffset, long yoffset, Object data, [bool flipY], [bool asPreMultipliedAlpha] ) Specify a two-dimensional texture subimage from either an ImageData object or a HTMLImageElement, HTMLCanvasElement or a HTMLVideoElement. copyTexImage2D( ulong target, long level, ulong intformat, long x, long y, ulong width, ulong height, long border ) Copy pixels into a 2D texture image. See framebufferTexture2D for accepted target values. copyTexSubImage2D( ulong target, long level, ulong intformat, long xoffset, long yoffset, long x, long y, ulong width, ulong height ) Copy a two-dimensional texture subimage. generateMipmap( ulong target ) Generate a complete set of mipmaps for a texture. isTexture( Object buffer ) Determine if an object is a texture. getParameter( ulong pname ) Relevant parameters: TEXTURE_BINDING_2D TEXTURE_BINDING_CUBE_MAP MAX_TEXTURE_SIZE MAX_CUBE_MAP_TEXTURE_SIZE ACTIVE_TEXTURE MAX_TEXTURE_IMAGE_UNITS MAX_VERTEX_TEXTURE_IMAGE_UNITS MAX_COMBINED_TEXTURE_IMAGE_UNITS

void

void

void

void bool any

Culling
void void

enable|disable( CULL_FACE ) cullFace( ulong mode )


Specify facet culling mode, accepted values are: FRONT BACK FRONT_AND_BACK

void any

frontFace( ulong mode )


Define front/back-facing mode: CW or CCW

getParameter( ulong pname )


Parameters: CULL_FACE_MODE or FRONT_FACE

Blending
void void

Stencil buffer
void void

enable|disable( BLEND )
Enable/disable blending blendFunc( ulong sfactor, ulong dfactor ) Specify pixel arithmetic. Accepted values for sfactor and dfactor are: ZERO ONE SRC_COLOR DST_COLOR SRC_ALPHA DST_ALPHA CONSTANT_COLOR CONSTANT_ALPHA ONE_MINUS_SRC_ALPHA ONE_MINUS_DST_ALPHA ONE_MINUS_SRC_COLOR ONE_MINUS_DST_COLOR ONE_MINUS_CONSTANT_COLOR ONE_MINUS_CONSTANT_ALPHA In addition, sfactor can also be SRC_ALPHA_SATURATE

enable|disable( STENCIL_TEST )
Enable/disable stencil testing.

stencilFunc( ulong func, long ref, ulong mask )


Set front and back function and reference value for stencil testing. Parameter func is one of: NEVER GREATER LESS EQUAL NOTEQUAL GEQUAL LEQUAL ALWAYS

void

stencilFuncSeparate( ulong face, ulong func,


long ref, ulong mask ) Set front and/or back function and reference value for stencil testing. Accepted values for face are: FRONT BACK FRONT_AND_BACK

void

stencilMask( ulong mask )


Control the front and back writing of individual bits in the stencil planes. stencilMaskSeparate( ulong face, ulong mask ) Control the front and/or back writing of individual bits in the stencil planes. stencilOp( ulong sfail, ulong dpfail, ulong dppass ) Set front and back stencil test actions. Accepted values for sfail, dpfail and dppass are: KEEP REPLACE ZERO INVERT INCR DECR INCR_WRAP DECR_WRAP

void

blendFuncSeparate( ulong srcRGB, ulong dstRGB,


ulong srcAlpha, ulong dstAlpha ) Specify pixel arithmetic for RGB and alpha components separately. blendEquation( ulong mode ) Specify the equation used for both the RGB blend equation and the Alpha blend equation. Accepted values for mode are: FUNC_ADD FUNC_SUBTRACT FUNC_REVERSE_SUBTRACT void

void

void

void

blendEquationSeparate( ulong modeRGB,


ulong modeAlpha ) Set the RGB blend equation and the alpha blend equation separately. blendColor( float red, float green, float blue, float alpha ) Set the blend color getParameter( ulong pname ) Relevant parameters: BLEND BLEND_DST_RGB BLEND_DST_ALPHA BLEND_EQUATION_RGB BLEND_COLOR BLEND_SRC_RGB BLEND_SRC_ALPHA BLEND_EQUATION_ALPHA

void

stencilOpSeparate( ulong face, ulong sfail,


ulong dpfail, ulong dppass ) Set front and/or back stencil test actions. clearStencil( long s ) Specify the clear value for the stencil buffer. getParameter( ulong pname ) Relevant parameters: STENCIL_TEST STENCIL_CLEAR_VALUE STENCIL_FUNC STENCIL_FAIL STENCIL_REF STENCIL_VALUE_MASK STENCIL_WRITEMASK STENCIL_BACK_FUNC STENCIL_BACK_FAIL STENCIL_BACK_REF STENCIL_BITS STENCIL_BACK_WRITEMASK STENCIL_BACK_VALUE_MASK STENCIL_BACK_PASS_DEPTH_FAIL STENCIL_BACK_PASS_DEPTH_PASS STENCIL_PASS_DEPTH_FAIL STENCIL_PASS_DEPTH_PASS

void any

void

any

Depth buffer
void void

enable|disable( DEPTH_TEST )
Enable/disable depth testing.

depthFunc( ulong func )


Specify the value used for depth buffer comparisons. Parameter func is one of: NEVER GREATER LESS EQUAL NOTEQUAL GEQUAL LEQUAL ALWAYS

Array data
Object Object Object Object Object Object Object void

void void

depthMask( bool flag )


Enable or disable writing into the depth buffer.

depthRange( float nearVal, float farVal )


Specify mapping of depth values from normalized device coordinates to window coordinates. clearDepth( float depth ) Specify the clear value for the depth buffer enable|disable( POLYGON_OFFSET_FILL ) Enable/disable polygon offset. polygonOffset( float factor, float units ) Set the scale and units used to calculate depth values. getParameter( ulong pname ) Relevant parameters: DEPTH_TEST DEPTH_WRITEMASK DEPTH_FUNC POLYGON_OFFSET_UNITS DEPTH_RANGE DEPTH_CLEAR_VALUE DEPTH_BITS POLYGON_OFFSET_FACTOR

createFloatArray( Array values ) createByteArray( Array values ) createUnsignedByteArray( Array values ) createShortArray( Array values ) createUnsignedShortArray( Array values ) createIntArray( Array values ) createUnsignedIntArray( Array values )
Create WebGL array objects from JS arrays.

void void void any

drawArrays( ulong mode, long first, ulong count )


Render primitives from array data. Accepted mode values are: POINTS LINES LINE_STRIP TRIANGLES TRIANGLE_FAN LINE_LOOP TRIANGLE_STRIP

void

drawElements( ulong mode, ulong count,


ulong type, ulong offset ) Render primitives from array data. Accepted type values are: UNSIGNED_BYTE UNSIGNED_SHORT

Uniform variables
ulong getUniformLocation( Object program, string name ) Return the location of a uniform variable. Object getActiveUniform( Object program, ulong idx ) Return information about an active uniform variable. Returns an object: { size: ..., type: ..., name: ... }. getUniform( Object program, ulong location ) any Return the value of a uniform variable uniform[1234][if]( ulong location, ... ) void Specify 1-4 float or int values of a uniform variable. uniform[1234][if]v( ulong location, Array v ) void Specify the value of a uniform variable as an array of 1-4 float or int values. uniformMatrix[234]fv( ulong location, void bool transpose, Object value ) Specify the value of a matrix uniform variable using arrays of float values. getParameter( ulong pname ) any Relevant parameters: MAX_VERTEX_UNIFORM_VECTORS MAX_FRAGMENT_UNIFORM_VECTORS

Multisampling
void

enable|disable( SAMPLE_COVERAGE )
If enabled, the fragment's coverage is ANDed with the temporary coverage value. enable|disable( SAMPLE_ALPHA_TO_COVERAGE ) If enabled, use the alpha value at the corresponding sample location to determine each bit. sampleCoverage( float value, bool invert ) Specify multisample coverage parameters. getParameter( ulong pname ) Relevant parameters: SAMPLE_COVERAGE_VALUE SAMPLE_COVERAGE_INVERT SAMPLE_BUFFERS SAMPLES

void

void any

Misc.
void void void void void

viewport( long x, long y, ulong w, ulong h )


Set the viewport.

lineWidth( float width )


Specify the width of rasterized lines.

Attribute variables
ulong getAttribLocation( Object program, string name ) Return the location of an attribute variable. Object getActiveAttrib( Object program, ulong idx ) Return information about an active attribute variable. Returns an object: { size: ..., type: ..., name: ... }. getVertexAttrib( Object idx, ulong pname ) any Return a generic vertex attribute parameter. Accepted pname values are: VERTEX_ATTRIB_ARRAY_ENABLED VERTEX_ATTRIB_ARRAY_SIZE VERTEX_ATTRIB_ARRAY_STRIDE VERTEX_ATTRIB_ARRAY_TYPE VERTEX_ATTRIB_ARRAY_NORMALIZED VERTEX_ATTRIB_ARRAY_BUFFER_BINDING CURRENT_VERTEX_ATTRIB void

flush( void )
Force execution of GL commands in finite time.

finish( void )
Block until all GL execution is complete.

clear( ulong mask )


Clear buffers to preset values, mask is the bitwise OR of one or more of COLOR_BUFFER_BIT STENCIL_BUFFER_BIT DEPTH_BUFFER_BIT

void void

enable|disable( DITHER )
Enable/disable dithering of color comps or indices.

colorMask( bool red, bool green,

vertexAttribPointer( ulong idx, long size,


ulong type, bool norm, long stride, ulong offset ) Define an array of generic vertex attribute data. Accepted type values are: FIXED FLOAT BYTE SHORT UNSIGNED_BYTE UNSIGNED_SHORT

bool blue, bool alpha ) Enable and disable writing of frame buffer color components. clearColor( float red, float green, void float blue, float alpha ) Specify clear values for the color buffers. scissor( long x, long y, ulong width, ulong height ) void Define the scissor box. ulong getError( void ) Return error information. Error values are: OUT_OF_MEMORY INVALID_ENUM INVALID_VALUE INVALID_OPERATION INVALID_FRAMEBUFFER_OPERATION NO_ERROR any

void void

vertexAttrib[1234]f( ulong idx, ... )


Specify 1-4 float values of a generic vertex attribute.

vertexAttrib[1234]fv( ulong idx, Array v )


Specify the value of a generic vertex attribute as an array of 1-4 float values. bindAttribLocation( Object program, ulong idx, string name ) Associate a generic vertex attribute index with a named attribute variable. enableVertexAttribArray( ulong idx ) disableVertexAttribArray( ulong idx ) Enable or disable a generic vertex attribute array getParameter( ulong pname ) Relevant parameters: MAX_VERTEX_ATTRIBS

getParameter( ulong pname )


Parameters values: VIEWPORT MAX_VIEWPORT_DIMS COLOR_CLEAR_VALUE SCISSOR_BOX LINE_WIDTH ALIASED_POINT_SIZE_RANGE ALIASED_LINE_WIDTH_RANGE COLOR_WRITEMASK SUBPIXEL_BITS

void

void void any

Notes: [1] Not implemented in one or more browsers. Sources: https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/doc/spec/WebGL-spec.html (2010-02-16) http://www.khronos.org/opengles/sdk/docs/man/ (2009-10-23) http://mxr.mozilla.org/mozilla-central/source/content/canvas/src/WebGLContextGL.cpp (2010-02-16) http://trac.webkit.org/browser/trunk/WebCore/html/canvas/WebGLRenderingContext.cpp (2010-02-16)

You might also like