You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The WebGLRenderingContext class provides the WebGL 1.0 rendering context (OpenGL ES 2.0). It is obtained from an HTMLCanvasElement or OffscreenCanvas via getContext("webgl"). SpawnDev.SpawnJS provides the full WebGL 1.0 API surface through WebGLRenderingContextBase, which contains all methods shared between WebGL 1.0 and 2.0. All WebGL constants are accessible via the static WebGLConstants class.
FramebufferTexture2D(int target, int attachment, int textarget, WebGLTexture texture, int level)
void
Attaches a texture to a framebuffer.
DeleteFramebuffer(WebGLFramebuffer framebuffer)
void
Deletes a framebuffer.
Other Methods
Method
Return Type
Description
Flush()
void
Flushes the GL command queue.
Finish()
void
Blocks until all GL commands are complete.
GetError()
int
Returns the current error code.
ReadPixels(int x, int y, int width, int height, int format, int type, TypedArray pixels)
void
Reads pixel data from the framebuffer.
GetExtension(string name)
SpawnJSObject?
Returns a WebGL extension, or null.
GetParameter<T>(int pname)
T
Returns a GL parameter value.
WebGLConstants
All WebGL constants are available as static fields on WebGLConstants:
WebGLConstants.VERTEX_SHADER// 35633WebGLConstants.FRAGMENT_SHADER // 35632
WebGLConstants.ARRAY_BUFFER // 34962
WebGLConstants.ELEMENT_ARRAY_BUFFER // 34963
WebGLConstants.STATIC_DRAW // 35044
WebGLConstants.TRIANGLES // 4
WebGLConstants.TEXTURE_2D // 3553
WebGLConstants.COLOR_BUFFER_BIT // 16384
WebGLConstants.DEPTH_BUFFER_BIT // 256// ... and many more
Example
// Get WebGL context from an OffscreenCanvasusingvarcanvas=newOffscreenCanvas(800,600);usingvargl=canvas.GetWebGLContext();// Set clear color and cleargl.ClearColor(0.0f,0.0f,0.0f,1.0f);gl.Clear(WebGLConstants.COLOR_BUFFER_BIT);// Create and compile vertex shaderusingvarvertShader=gl.CreateShader(WebGLConstants.VERTEX_SHADER);gl.ShaderSource(vertShader,@" attribute vec4 aPosition; void main() { gl_Position = aPosition; }");gl.CompileShader(vertShader);// Create and compile fragment shaderusingvarfragShader=gl.CreateShader(WebGLConstants.FRAGMENT_SHADER);gl.ShaderSource(fragShader,@" precision mediump float; void main() { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }");gl.CompileShader(fragShader);// Create programusingvarprogram=gl.CreateProgram();gl.AttachShader(program,vertShader);gl.AttachShader(program,fragShader);gl.LinkProgram(program);gl.UseProgram(program);// Create vertex bufferfloat[]vertices={0.0f,0.5f,-0.5f,-0.5f,0.5f,-0.5f};usingvarvertexArray=newFloat32Array(vertices);usingvarbuffer=gl.CreateBuffer();gl.BindBuffer(WebGLConstants.ARRAY_BUFFER,buffer);gl.BufferData(WebGLConstants.ARRAY_BUFFER,vertexArray,WebGLConstants.STATIC_DRAW);// Set up vertex attributeintposLocation=gl.GetAttribLocation(program,"aPosition");gl.VertexAttribPointer(posLocation,2,WebGLConstants.FLOAT,false,0,0);gl.EnableVertexAttribArray(posLocation);// Drawgl.Viewport(0,0,800,600);gl.DrawArrays(WebGLConstants.TRIANGLES,0,3);