# Use Haxe to GLSL

**URL:** https://community.haxe.org/t/use-haxe-to-glsl/3047
**Category:** Showcase
**Tags:** macro, library-showcase
**Created:** [May 17, 2021, 12:16pm UTC](https://community.haxe.org/t/use-haxe-to-glsl/3047 "2021-05-17T12:16:12Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rainy](https://community.haxe.org/user_avatar/community.haxe.org/rainy/32/1619_2.png) [@rainy](https://community.haxe.org/u/rainy)
#### Post date: [May 17, 2021, 12:16pm UTC](https://community.haxe.org/t/use-haxe-to-glsl/3047/1 "2021-05-17T12:16:13Z")

</div>

I created a GLSL library that allows OpenFL to be written directly in Haxe.

Github: [https://github.com/rainyt/openfl-glsl](https://github.com/rainyt/openfl-glsl)

Allow Haxe to write GLSL code directly in OpenFL and use it directly in OpenFL.

## Test.hx

```haxe
@:debug

class Test extends glsl.OpenFLShader {

public function new() {

super();

}

override function fragment() {

super.fragment();

var v:Vec2 = this.gl_openfl_TextureCoordv * 0.5;

this.gl_FragColor = texture2D(gl_openfl_Texture, v);

}

}

```

## Test.hx to GLSL

Finally, the fragment method of Test.hx will be compiled into GLSL and can be used directly by OpenFL.

```haxe
#pragma header

void main(void){

#pragma body

vec2 v=openfl_TextureCoordv*0.5;

gl_FragColor=texture2D(openfl_Texture,v);

}

```

Use:

```haxe
var bitmap = new Bitmap();

bitmap.shader = new Test();

```

Now, you can use Haxe to directly convert to GLSL, you can use it without OpenFL, and then define your own variables.

Haxe:

```haxe
package glsl;

import glsl.OpenFLShader.float;
import VectorMath.vec4;

/**
 * Use Haxe to convert to GLSL, access through fragmentSource and vertexSource
 */
@:debug
class Haxe2GLSL extends BaseGLSL {
	@:define("TEXT 1")
	public function fragment():Void {
		this.gl_FragColor = vec4(float(TEXT), 1., 0., 1.);
	}

	public function vertex():Void {
		this.gl_Position = vec4(1, 1, 1, 1);
	}
}

@:autoBuild(glsl.macro.GLSLCompileMacro.build("glsl"))
class BaseGLSL {
	
	public var gl_FragColor:Vec4;

	public var gl_Position:Vec4;
}

```

USE:

```haxe
trace(Haxe2GLSL.fragmentSource);
trace(Haxe2GLSL.vertexSource);

```

GLSL:

```haxe
#define TEXT 1

 void main(void){
  gl_FragColor=vec4(float(TEXT),1.,0.,1.);

}

```

```haxe
 void main(void){
  gl_Position=vec4(1.,1.,1.,1.);

}

```

---

<div class="post-metadata">

### Author: ![rainy](https://community.haxe.org/user_avatar/community.haxe.org/rainy/32/1619_2.png) [@rainy](https://community.haxe.org/u/rainy)
#### Post date: [May 18, 2021, 8:13am UTC](https://community.haxe.org/t/use-haxe-to-glsl/3047/2 "2021-05-18T08:13:13Z")

</div>

Now, just add `<define name="output" value="./bin"/>`or`-D output=./bin` to output all GLSL to the bin directory.

> **[rainyt/openfl-glsl-samples](https://github.com/rainyt/openfl-glsl-samples)**
>
> Contribute to rainyt/openfl-glsl-samples development by creating an account on GitHub.

Start:

```haxe
haxe haxebuild.hxml

```

After running, a bin directory will be generated, which contains GLSL files

---

<div class="post-metadata">

### Author: ![rainy](https://community.haxe.org/user_avatar/community.haxe.org/rainy/32/1619_2.png) [@rainy](https://community.haxe.org/u/rainy)
#### Post date: [May 19, 2021, 12:45am UTC](https://community.haxe.org/t/use-haxe-to-glsl/3047/3 "2021-05-19T00:45:54Z")

</div>

haxelib Update to 0.0.5:

1. Supplement: Supplement to operators of `EUnop` and `Binop`.
2. New: Newly added subclasses inherit the variables of the parent class. For example, variables such as `:glsl`/`:uniform` created in the parent class will be inherited by the subclass.
3. Fix: Fix `elseif` statement parsing.
4. Fix: Fix `==` statement parsing.
5. New: Added GLSL syntax formatting.
6. New: Added support for Define`output`, which can define an output directory. After compilation, all GLSL will be compiled into the directory.
7. New: Added syntax support for `(conf)?else:else`.

---

<div class="post-metadata">

### Author: ![rainy](https://community.haxe.org/user_avatar/community.haxe.org/rainy/32/1619_2.png) [@rainy](https://community.haxe.org/u/rainy)
#### Post date: [May 19, 2021, 11:16pm UTC](https://community.haxe.org/t/use-haxe-to-glsl/3047/4 "2021-05-19T23:16:51Z")

</div>

haxelib Update to 0.0.6

1. Added: Added `break` grammar recognition.
2. Improvement: Improve the generation of `;`.
3. Improvement: The redundant blank lines are automatically removed.
4. Fix: Fix the problem that the array variable reference access error.
5. Improvement: Modify the GLSL method and the sequence of variables.

At the same time, I added a lot of interesting examples of GLSL in openfl-glsl-samples: text stroke, circle mask, rounded corner rendering, nine-square grid rendering, etc.

---

<div class="post-metadata">

### Author: ![rainy](https://community.haxe.org/user_avatar/community.haxe.org/rainy/32/1619_2.png) [@rainy](https://community.haxe.org/u/rainy)
#### Post date: [May 29, 2021, 12:40am UTC](https://community.haxe.org/t/use-haxe-to-glsl/3047/5 "2021-05-29T00:40:55Z")

</div>

haxelib Update to 0.0.9:

1. Improvement: Improve the formatting of GLSL to make GLSL look more beautiful.
2. Improvement: Added support for `else if`.
3. Improvement: Use `/t` instead of spaces.
4. New: Added support for OpenFLGraphicsShader, which can be used in the `beginShaderFill` of OpenFL.
5. Improvement: If super.fragment() and super.vertex() are not implemented, #pragma body can be removed.
6. Fix: Fix the wrong definition of `@:attribute` in OpenFlShader. When accessing `@:attribute`, you need to use `a_variable name` for access.
