Skip to content

Writing a shader

Adam Gorski edited this page Aug 16, 2021 · 1 revision

Writing a shader

Writing XFDraw shaders is easy, as the language is nearly identical to OpenGL's GLSL.

To start, create two text files for your vertex and fragment shaders. Then, make sure they are copied to the output directory when you build. In VS this can be found in the properties window with Copy to Output Directory set to Copy if newer.

Next, you can add the base code for the vertex shader: basicVS.glsl

layout (location = 0) in vec3 coord_data;
layout (location = 1) in vec2 uv_data;

out vec2 uv_data;

uniform vec3 cameraPosition;
uniform mat3 cameraRotation;

void main()
{
	gl_Position = cameraRotation * (coord_data - cameraPosition);
}
Note: The layouts don't do anything other than reorganize the in's according to location. In XFDraw they dont have anything to do with size.

Next, you can add the base code for the fragment shader: basicFS.glsl

out byte4 FragColor;
in vec2 uv_data;

uniform vec2 textureSize;
uniform sampler2D myTexture;

void main()
{
	FragColor = texture(myTexture, uv_data * textureSize);
}
Note: XFDraw uses real coordinates when sampling textures, NOT values ranging from 0 to 1. Because of this we need to multiply values ranging from 0 to 1 by the texture size.

And thats it! Obviously you will need a XYZ_UV buffer to power this shader, but the basics are really similar compared to OpenGL!

Clone this wiki locally