# GL

> Low-level graphics library.

## Definition

* **Type:** Class
* **Namespace:** [UnityEngine](/engine/6000.5/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

```csharp
public sealed class GL
```

## Remarks

Use this class to manipulate active transformation matrices, issue rendering commands similar to OpenGL's immediate mode and do other low-level graphics tasks. Note that in almost all cases using [Graphics.RenderMesh](/engine/6000.5/script-reference/unityengine/graphics/rendermesh.md) or [CommandBuffer](/engine/6000.5/script-reference/unityengine/rendering/commandbuffer.md) is more efficient than using immediate mode drawing.

GL immediate drawing functions use whatever is the "current material" set up right now (see [Material.SetPass](/engine/6000.5/script-reference/unityengine/material/setpass.md)). The material controls how the rendering is done (blending, textures, etc.), so unless you explicitly set it to something before using GL draw functions, the material can happen to be anything. Also, if you call any other drawing commands from inside GL drawing code, they can set material to something else, so make sure it's under control as well.

GL drawing commands execute immediately. That means if you call them in Update(), they will be executed before the camera is rendered (and the camera will most likely clear the screen, making the GL drawing not visible).

The usual place to call GL drawing is most often in [Camera.OnPostRender()](/engine/6000.5/script-reference/unityengine/camera/onpostrender.md)() from a script attached to a camera, or inside an image effect function ([Camera.OnRenderImage(RenderTexture, RenderTexture)](/engine/6000.5/script-reference/unityengine/camera/onrenderimage.md)).

**Note:** The High Definition Render Pipeline (HDRP) and the Universal Render Pipeline (URP) do not support [Camera.OnPostRender()](/engine/6000.5/script-reference/unityengine/camera/onpostrender.md). Instead, use [RenderPipelineManager.endCameraRendering](/engine/6000.5/script-reference/unityengine/rendering/renderpipelinemanager/endcamerarendering.md) or [RenderPipelineManager.endFrameRendering](/engine/6000.5/script-reference/unityengine/rendering/renderpipelinemanager/endframerendering.md).

```csharp
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    // When added to an object, draws colored rays from the
    // transform position.
    public int lineCount = 100;
    public float radius = 3.0f;

    static Material lineMaterial;
    static void CreateLineMaterial()
    {
        if (!lineMaterial)
        {
            // Unity has a built-in shader that is useful for drawing
            // simple colored things.
            Shader shader = Shader.Find("Hidden/Internal-Colored");
            lineMaterial = new Material(shader);
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
            // Turn on alpha blending
            lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            // Turn backface culling off
            lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
            // Turn off depth writes
            lineMaterial.SetInt("_ZWrite", 0);
        }
    }

    // Will be called after all regular rendering is done
    public void OnRenderObject()
    {
        CreateLineMaterial();
        // Apply the line material
        lineMaterial.SetPass(0);

        GL.PushMatrix();
        // Set transformation matrix for drawing to
        // match our transform
        GL.MultMatrix(transform.localToWorldMatrix);

        // Draw lines
        GL.Begin(GL.LINES);
        for (int i = 0; i < lineCount; ++i)
        {
            float a = i / (float)lineCount;
            float angle = a * Mathf.PI * 2;
            // Vertex colors change from red to green
            GL.Color(new Color(a, 1 - a, 0, 0.8F));
            // One vertex at transform position
            GL.Vertex3(0, 0, 0);
            // Another vertex at edge of circle
            GL.Vertex3(Mathf.Cos(angle) * radius, Mathf.Sin(angle) * radius, 0);
        }
        GL.End();
        GL.PopMatrix();
    }
}
```

**Note:** This class is almost always used when you need to draw a couple of lines or triangles, and don't want to deal with meshes. If you want to avoid surprises the usage pattern is this:

```csharp
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void OnPostRender()
    {
        // Set your materials
        GL.PushMatrix();
        // yourMaterial.SetPass( );
        // Draw your stuff
        GL.PopMatrix();
    }
}
```

Where at the "// Draw your stuff" you should do SetPass() on some material previously declared, which will be used for drawing. If you dont call SetPass, then you'll get basically a random material (whatever was used before) which is not good. So do it.

## Static Fields

| Value                                                                               | Description                                                                                        |
| ----------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| [LINE\_STRIP](/engine/6000.5/script-reference/unityengine/gl/line-strip.md)         | Mode for [GL.Begin](/engine/6000.5/script-reference/unityengine/gl/begin.md): draw line strip.     |
| [LINES](/engine/6000.5/script-reference/unityengine/gl/lines.md)                    | Mode for [GL.Begin](/engine/6000.5/script-reference/unityengine/gl/begin.md): draw lines.          |
| [QUADS](/engine/6000.5/script-reference/unityengine/gl/quads.md)                    | Mode for [GL.Begin](/engine/6000.5/script-reference/unityengine/gl/begin.md): draw quads.          |
| [TRIANGLE\_STRIP](/engine/6000.5/script-reference/unityengine/gl/triangle-strip.md) | Mode for [GL.Begin](/engine/6000.5/script-reference/unityengine/gl/begin.md): draw triangle strip. |
| [TRIANGLES](/engine/6000.5/script-reference/unityengine/gl/triangles.md)            | Mode for [GL.Begin](/engine/6000.5/script-reference/unityengine/gl/begin.md): draw triangles.      |

## Static Properties

| Property                                                                         | Description                                                                    |
| -------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ |
| [invertCulling](/engine/6000.5/script-reference/unityengine/gl/invertculling.md) | Select whether to invert the backface culling (true) or not (false).           |
| [modelview](/engine/6000.5/script-reference/unityengine/gl/modelview.md)         | Gets or sets the modelview matrix.                                             |
| [sRGBWrite](/engine/6000.5/script-reference/unityengine/gl/srgbwrite.md)         | Controls whether Linear-to-sRGB color conversion is performed while rendering. |
| [wireframe](/engine/6000.5/script-reference/unityengine/gl/wireframe.md)         | Should rendering be done in wireframe?                                         |

## Static Methods

| Method                                                                                             | Description                                                                       |
| -------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
| [Begin](/engine/6000.5/script-reference/unityengine/gl/begin.md)                                   | Begin drawing 3D primitives.                                                      |
| [Clear](/engine/6000.5/script-reference/unityengine/gl/clear.md)                                   | Clear the current render buffer.                                                  |
| [ClearWithSkybox](/engine/6000.5/script-reference/unityengine/gl/clearwithskybox.md)               | Clear the current render buffer with camera's skybox.                             |
| [Color](/engine/6000.5/script-reference/unityengine/gl/color.md)                                   | Sets current vertex color.                                                        |
| [End](/engine/6000.5/script-reference/unityengine/gl/end.md)                                       | End drawing 3D primitives.                                                        |
| [Flush](/engine/6000.5/script-reference/unityengine/gl/flush.md)                                   | Sends queued-up commands in the driver's command buffer to the GPU.               |
| [GetGPUProjectionMatrix](/engine/6000.5/script-reference/unityengine/gl/getgpuprojectionmatrix.md) | Compute GPU projection matrix from camera's projection matrix.                    |
| [InvalidateState](/engine/6000.5/script-reference/unityengine/gl/invalidatestate.md)               | Invalidate the internally cached render state.                                    |
| [IssuePluginEvent](/engine/6000.5/script-reference/unityengine/gl/issuepluginevent.md)             | Send a user-defined event to a native code plugin.                                |
| [LoadIdentity](/engine/6000.5/script-reference/unityengine/gl/loadidentity.md)                     | Load an identity into the current model and view matrices.                        |
| [LoadOrtho](/engine/6000.5/script-reference/unityengine/gl/loadortho.md)                           | Helper function to set up an orthograhic projection.                              |
| [LoadPixelMatrix](/engine/6000.5/script-reference/unityengine/gl/loadpixelmatrix.md)               | Setup a matrix for pixel-correct rendering.                                       |
| [LoadProjectionMatrix](/engine/6000.5/script-reference/unityengine/gl/loadprojectionmatrix.md)     | Load an arbitrary matrix to the current projection matrix.                        |
| [MultiTexCoord](/engine/6000.5/script-reference/unityengine/gl/multitexcoord.md)                   | Sets current texture coordinate (v.x,v.y,v.z) to the actual texture `unit`.       |
| [MultiTexCoord2](/engine/6000.5/script-reference/unityengine/gl/multitexcoord2.md)                 | Sets current texture coordinate (x,y) for the actual texture `unit`.              |
| [MultiTexCoord3](/engine/6000.5/script-reference/unityengine/gl/multitexcoord3.md)                 | Sets current texture coordinate (x,y,z) to the actual texture `unit`.             |
| [MultMatrix](/engine/6000.5/script-reference/unityengine/gl/multmatrix.md)                         | Sets the current model matrix to the one specified.                               |
| [PopMatrix](/engine/6000.5/script-reference/unityengine/gl/popmatrix.md)                           | Restores the model, view and projection matrices off the top of the matrix stack. |
| [PushMatrix](/engine/6000.5/script-reference/unityengine/gl/pushmatrix.md)                         | Saves the model, view and projection matrices to the top of the matrix stack.     |
| [RenderTargetBarrier](/engine/6000.5/script-reference/unityengine/gl/rendertargetbarrier.md)       | Resolves the render target for subsequent operations sampling from it.            |
| [TexCoord](/engine/6000.5/script-reference/unityengine/gl/texcoord.md)                             | Sets current texture coordinate (v.x,v.y,v.z) for all texture units.              |
| [TexCoord2](/engine/6000.5/script-reference/unityengine/gl/texcoord2.md)                           | Sets current texture coordinate (x,y) for all texture units.                      |
| [TexCoord3](/engine/6000.5/script-reference/unityengine/gl/texcoord3.md)                           | Sets current texture coordinate (x,y,z) for all texture units.                    |
| [Vertex](/engine/6000.5/script-reference/unityengine/gl/vertex.md)                                 | Submit a vertex.                                                                  |
| [Vertex3](/engine/6000.5/script-reference/unityengine/gl/vertex3.md)                               | Submit a vertex.                                                                  |
| [Viewport](/engine/6000.5/script-reference/unityengine/gl/viewport.md)                             | Set the rendering viewport.                                                       |
