Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for stripes in colored polygons #590

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void PolygonGroup2dOpenGl::setup(const std::shared_ptr<::RenderingContextInterfa
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

mvpMatrixHandle = glGetUniformLocation(program, "uMVPMatrix");
scaleFactorHandle = glGetUniformLocation(program, "scaleFactors");

ready = true;
glDataBuffersGenerated = true;
Expand Down Expand Up @@ -119,6 +120,10 @@ void PolygonGroup2dOpenGl::render(const std::shared_ptr<::RenderingContextInterf
glUseProgram(program);

glUniformMatrix4fv(mvpMatrixHandle, 1, false, (GLfloat *)mvpMatrix);
if (scaleFactorHandle >= 0) {
glUniform2f(scaleFactorHandle, screenPixelAsRealMeterFactor,
pow(2.0, ceil(log2(screenPixelAsRealMeterFactor))));
}

shaderProgram->preRender(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class PolygonGroup2dOpenGl : public GraphicsObjectInterface,
int program = 0;

int mvpMatrixHandle;
int scaleFactorHandle;
int positionHandle;
int styleIndexHandle;
GLuint attribBuffer = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
#include "OpenGlHelper.h"
#include "ColorPolygonGroup2dShaderOpenGl.h"

ColorPolygonGroup2dShaderOpenGl::ColorPolygonGroup2dShaderOpenGl() {
ColorPolygonGroup2dShaderOpenGl::ColorPolygonGroup2dShaderOpenGl(bool isStriped)
: isStriped(isStriped),
programName(std::string("UBMAP_ColorPolygonGroupShaderOpenGl_") + (isStriped ? "striped" : "std")) {
this->polygonStyles.resize(sizeStyleValuesArray);
}

const std::string ColorPolygonGroup2dShaderOpenGl::programName = "UBMAP_ColorPolygonGroupShaderOpenGl";

std::shared_ptr<ShaderProgramInterface> ColorPolygonGroup2dShaderOpenGl::asShaderProgramInterface() { return shared_from_this(); }

std::string ColorPolygonGroup2dShaderOpenGl::getProgramName() { return programName; }
Expand Down Expand Up @@ -72,42 +72,98 @@ void ColorPolygonGroup2dShaderOpenGl::setStyles(const ::SharedBytes & styles) {
}

std::string ColorPolygonGroup2dShaderOpenGl::getVertexShader() {
return OMMVersionedGlesShaderCode(320 es,
precision highp float;

uniform mat4 uMVPMatrix;
in vec2 vPosition;
in float vStyleIndex;
// polygonStyles: {vec4 color, float opacity} - stride = 5
uniform float polygonStyles[5 * 16];
uniform int numStyles;

out vec4 color;

void main() {
int styleIndex = int(floor(vStyleIndex + 0.5));
if (styleIndex < 0) {
styleIndex = 0;
} else if (styleIndex > numStyles) {
styleIndex = numStyles;
return isStriped ? OMMVersionedGlesShaderCode(320 es,
// Striped Shader
precision highp float;

uniform mat4 uMVPMatrix;
in vec2 vPosition;
in float vStyleIndex;
// polygonStyles: {vec4 color, float opacity, stripe width, gap width} - stride = 7
uniform float polygonStyles[7 * 16];
uniform int numStyles;

out vec4 color;
out vec2 stripeInfo;
out vec2 uv;

void main() {
int styleIndex = int(floor(vStyleIndex + 0.5));
if (styleIndex < 0) {
styleIndex = 0;
} else if (styleIndex > numStyles) {
styleIndex = numStyles;
}
styleIndex = styleIndex * 5;
color = vec4(polygonStyles[styleIndex], polygonStyles[styleIndex + 1],
polygonStyles[styleIndex + 2], polygonStyles[styleIndex + 3] * polygonStyles[styleIndex + 4]);
stripeInfo = vec2(polygonStyles[styleIndex + 5], polygonStyles[styleIndex + 6]);
uv = vPosition;
gl_Position = uMVPMatrix * vec4(vPosition, 0.0, 1.0);
}
styleIndex = styleIndex * 5;
color = vec4(polygonStyles[styleIndex], polygonStyles[styleIndex + 1],
polygonStyles[styleIndex + 2], polygonStyles[styleIndex + 3] * polygonStyles[styleIndex + 4]);
gl_Position = uMVPMatrix * vec4(vPosition, 0.0, 1.0);
});
) : OMMVersionedGlesShaderCode(320 es,
// Default Color Shader
precision highp float;

uniform mat4 uMVPMatrix;
in vec2 vPosition;
in float vStyleIndex;
// polygonStyles: {vec4 color, float opacity} - stride = 5
uniform float polygonStyles[5 * 16];
uniform int numStyles;

out vec4 color;

void main() {
int styleIndex = int(floor(vStyleIndex + 0.5));
if (styleIndex < 0) {
styleIndex = 0;
} else if (styleIndex > numStyles) {
styleIndex = numStyles;
}
styleIndex = styleIndex * 5;
color = vec4(polygonStyles[styleIndex], polygonStyles[styleIndex + 1],
polygonStyles[styleIndex + 2], polygonStyles[styleIndex + 3] * polygonStyles[styleIndex + 4]);
gl_Position = uMVPMatrix * vec4(vPosition, 0.0, 1.0);
});
}

std::string ColorPolygonGroup2dShaderOpenGl::getFragmentShader() {
return OMMVersionedGlesShaderCode(320 es,
precision highp float;

in vec4 color;
out vec4 fragmentColor;

void main() {
fragmentColor = color;
fragmentColor.a = 1.0;
fragmentColor *= color.a;
});
return isStriped ? OMMVersionedGlesShaderCode(320 es,
// Striped Shader
precision highp float;

uniform vec2 scaleFactors;

in vec4 color;
in vec2 stripeInfo;
in vec2 uv;

out vec4 fragmentColor;

void main() {
float disPx = (uv.x + uv.y) / scaleFactors.y;
float totalPx = stripeInfo.x + stripeInfo.y;
float adjLineWPx = stripeInfo.x / scaleFactors.y * scaleFactors.x;
if (mod(disPx, totalPx) > adjLineWPx) {
fragmentColor = vec4(0.0);
return;
}

fragmentColor = color;
fragmentColor.a = 1.0;
fragmentColor *= color.a;
}
) : OMMVersionedGlesShaderCode(320 es,
// Default Color Shader
precision highp float;

in vec4 color;
out vec4 fragmentColor;

void main() {
fragmentColor = color;
fragmentColor.a = 1.0;
fragmentColor *= color.a;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ColorPolygonGroup2dShaderOpenGl : public BaseShaderProgramOpenGl,
public PolygonGroupShaderInterface,
public std::enable_shared_from_this<ShaderProgramInterface> {
public:
ColorPolygonGroup2dShaderOpenGl();
ColorPolygonGroup2dShaderOpenGl(bool isStriped);

virtual std::shared_ptr<ShaderProgramInterface> asShaderProgramInterface() override;

Expand All @@ -37,12 +37,13 @@ class ColorPolygonGroup2dShaderOpenGl : public BaseShaderProgramOpenGl,
virtual std::string getFragmentShader() override;

private:
const static std::string programName;
bool isStriped = false;
const std::string programName;

std::recursive_mutex styleMutex;
std::vector<GLfloat> polygonStyles;
GLint numStyles = 0;

const int sizeStyleValues = 5;
const int sizeStyleValues = isStriped ? 7 : 5;
const int sizeStyleValuesArray = sizeStyleValues * 16;
};
4 changes: 2 additions & 2 deletions android/src/main/cpp/graphics/shader/ShaderFactoryOpenGl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ std::shared_ptr<ColorCircleShaderInterface> ShaderFactoryOpenGl::createColorCirc
return std::make_shared<ColorCircleShaderOpenGl>();
}

std::shared_ptr<PolygonGroupShaderInterface> ShaderFactoryOpenGl::createPolygonGroupShader() {
return std::make_shared<ColorPolygonGroup2dShaderOpenGl>();
std::shared_ptr<PolygonGroupShaderInterface> ShaderFactoryOpenGl::createPolygonGroupShader(bool isStriped) {
return std::make_shared<ColorPolygonGroup2dShaderOpenGl>(isStriped);
}

std::shared_ptr<PolygonPatternGroupShaderInterface> ShaderFactoryOpenGl::createPolygonPatternGroupShader(bool fadeInPattern) {
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/cpp/graphics/shader/ShaderFactoryOpenGl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ShaderFactoryOpenGl : public ShaderFactoryInterface {

std::shared_ptr<ColorCircleShaderInterface> createColorCircleShader() override;

std::shared_ptr<PolygonGroupShaderInterface> createPolygonGroupShader() override;
std::shared_ptr<PolygonGroupShaderInterface> createPolygonGroupShader(bool isStriped) override;

std::shared_ptr<PolygonPatternGroupShaderInterface> createPolygonPatternGroupShader(bool fadeInPattern) override;

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions bridging/ios/MCShaderFactoryInterface+Private.mm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bridging/ios/MCShaderFactoryInterface.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion djinni/graphics/shader/shader.djinni
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ shader_factory_interface = interface +c +j +o {
create_line_group_shader(): line_group_shader_interface;
create_color_shader() : color_shader_interface;
create_color_circle_shader() : color_circle_shader_interface;
create_polygon_group_shader(): polygon_group_shader_interface;
create_polygon_group_shader(is_striped: bool): polygon_group_shader_interface;
create_polygon_pattern_group_shader(fade_in_pattern: bool): polygon_pattern_group_shader_interface;

create_text_shader() : text_shader_interface;
Expand Down
Loading
Loading