Skip to content

Commit

Permalink
Set GLSL version from a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
oakes committed Feb 7, 2021
1 parent 8b40fd5 commit 0d3a48c
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/paranim/gl/entities.nim
Expand Up @@ -2,6 +2,13 @@ import paranim/gl, paranim/gl/uniforms, paranim/gl/attributes
from paranim/primitives import nil
import nimgl/opengl
import glm
from strutils import format

const version =
when defined(emscripten):
"300 es"
else:
"330"

type
TwoDEntityUniforms = tuple[u_matrix: Uniform[Mat3x3[GLfloat]], u_color: Uniform[Vec4[GLfloat]]]
Expand Down Expand Up @@ -81,26 +88,26 @@ proc getInstanceAttr[T](attr: Attribute[T], i: int, uni: var Uniform[Vec4[T]]) =

const twoDVertexShader =
"""
#version 330
#version $1
uniform mat3 u_matrix;
in vec2 a_position;
void main()
{
gl_Position = vec4((u_matrix * vec3(a_position, 1)).xy, 0, 1);
}
"""
""".format(version)

const twoDFragmentShader =
"""
#version 330
#version $1
precision mediump float;
uniform vec4 u_color;
out vec4 o_color;
void main()
{
o_color = u_color;
}
"""
""".format(version)

proc initTwoDEntity*(data: openArray[GLfloat]): UncompiledTwoDEntity =
## Initialize a 2D entity whose shape is determined by `data`.
Expand All @@ -117,7 +124,7 @@ proc initTwoDEntity*(data: openArray[GLfloat]): UncompiledTwoDEntity =

const instancedTwoDVertexShader =
"""
#version 330
#version $1
uniform mat3 u_matrix;
in vec2 a_position;
in mat3 a_matrix;
Expand All @@ -128,19 +135,19 @@ const instancedTwoDVertexShader =
v_color = a_color;
gl_Position = vec4((u_matrix * a_matrix * vec3(a_position, 1)).xy, 0, 1);
}
"""
""".format(version)

const instancedTwoDFragmentShader =
"""
#version 330
#version $1
precision mediump float;
in vec4 v_color;
out vec4 o_color;
void main()
{
o_color = v_color;
}
"""
""".format(version)

proc initInstancedEntity*(entity: UncompiledTwoDEntity): UncompiledInstancedTwoDEntity =
## Initialize an instanced 2D entity.
Expand Down Expand Up @@ -182,7 +189,7 @@ proc `[]=`*(instancedEntity: var UncompiledInstancedTwoDEntity, i: int, entity:

const imageVertexShader =
"""
#version 330
#version $1
uniform mat3 u_matrix;
uniform mat3 u_texture_matrix;
in vec2 a_position;
Expand All @@ -192,11 +199,11 @@ const imageVertexShader =
gl_Position = vec4((u_matrix * vec3(a_position, 1)).xy, 0, 1);
v_tex_coord = (u_texture_matrix * vec3(a_position, 1)).xy;
}
"""
""".format(version)

const imageFragmentShader =
"""
#version 330
#version $1
precision mediump float;
uniform sampler2D u_image;
in vec2 v_tex_coord;
Expand All @@ -205,7 +212,7 @@ const imageFragmentShader =
{
o_color = texture(u_image, v_tex_coord);
}
"""
""".format(version)

proc initImageEntity*(data: openArray[GLubyte], width: int, height: int): UncompiledImageEntity =
## Initialize an entity that renders the provided texture `data`.
Expand Down Expand Up @@ -244,7 +251,7 @@ proc initImageEntity*(data: openArray[GLubyte], width: int, height: int): Uncomp

const instancedImageVertexShader =
"""
#version 330
#version $1
uniform mat3 u_matrix;
in vec2 a_position;
in mat3 a_matrix;
Expand All @@ -255,11 +262,11 @@ const instancedImageVertexShader =
gl_Position = vec4((u_matrix * a_matrix * vec3(a_position, 1)).xy, 0, 1);
v_tex_coord = (a_texture_matrix * vec3(a_position, 1)).xy;
}
"""
""".format(version)

const instancedImageFragmentShader =
"""
#version 330
#version $1
precision mediump float;
uniform sampler2D u_image;
in vec2 v_tex_coord;
Expand All @@ -268,7 +275,7 @@ const instancedImageFragmentShader =
{
o_color = texture(u_image, v_tex_coord);
}
"""
""".format(version)

proc initInstancedEntity*(entity: UncompiledImageEntity): UncompiledInstancedImageEntity =
## Initialize an instanced image entity.
Expand Down

0 comments on commit 0d3a48c

Please sign in to comment.