Skip to content

Commit

Permalink
Switch Inochi2D to premultipled alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Jan 23, 2022
1 parent fc08476 commit 7bb3920
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion shaders/basic/basic.frag
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ uniform sampler2D fbo;
uniform float opacity;

void main() {
outColor = texture(fbo, texUVs) * vec4(1, 1, 1, opacity);
outColor = texture(fbo, texUVs) * opacity;
}
6 changes: 6 additions & 0 deletions source/inochi2d/core/dbg.d
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ void inDbgSetBuffer(vec3[] points, ushort[] indices) {
*/
void inDbgDrawPoints(vec4 color, mat4 transform = mat4.identity) {
glEnable(GL_POINT_SMOOTH);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);

glBindVertexArray(dbgVAO);

dbgShader.use();
Expand All @@ -112,6 +114,7 @@ void inDbgDrawPoints(vec4 color, mat4 transform = mat4.identity) {
glDrawElements(GL_POINTS, indiceCount, GL_UNSIGNED_SHORT, null);
glDisableVertexAttribArray(0);

glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
glDisable(GL_POINT_SMOOTH);
}

Expand All @@ -120,6 +123,8 @@ void inDbgDrawPoints(vec4 color, mat4 transform = mat4.identity) {
*/
void inDbgDrawLines(vec4 color, mat4 transform = mat4.identity) {
glEnable(GL_LINE_SMOOTH);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);

glBindVertexArray(dbgVAO);

dbgShader.use();
Expand All @@ -133,5 +138,6 @@ void inDbgDrawLines(vec4 color, mat4 transform = mat4.identity) {
glDrawElements(GL_LINES, indiceCount, GL_UNSIGNED_SHORT, null);
glDisableVertexAttribArray(0);

glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
glDisable(GL_LINE_SMOOTH);
}
2 changes: 1 addition & 1 deletion source/inochi2d/core/nodes/part/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ void inDrawTextureAtPart(Texture texture, Part part) {
partShader.use();
partShader.setUniform(mvp,
inGetCamera().matrix *
mat4.translation(part.transform.matrix() * vec3(1, 1, 1))
mat4.translation(vec3(part.transform.matrix() * vec4(1, 1, 1, 1)))
);
partShader.setUniform(gopacity, part.opacity);

Expand Down
19 changes: 15 additions & 4 deletions source/inochi2d/core/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ private {
GLuint fColor;
GLuint fStencil;

vec4 inClearColor;


Shader[] blendingShaders;

Expand Down Expand Up @@ -70,6 +72,8 @@ package(inochi2d) {
// Some defaults that should be changed by app writer
inCamera = new Camera;

inClearColor = vec4(0, 0, 0, 0);

// Shader for scene
sceneShader = new Shader(import("scene.vert"), import("scene.frag"));
sceneMVP = sceneShader.getUniformLocation("mvp");
Expand All @@ -95,7 +99,7 @@ package(inochi2d) {
/**
Begins rendering to the framebuffer
*/
void inBeginScene() {
void inBeginScene() {
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);

Expand All @@ -104,13 +108,13 @@ void inBeginScene() {

// Bind our framebuffer
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fBuffer);
glClearColor(0, 0, 0, 0);
glClearColor(inClearColor.r, inClearColor.g, inClearColor.b, inClearColor.a);
glClear(GL_COLOR_BUFFER_BIT);

// Everything else is the actual texture used by the meshes at id 0
glActiveTexture(GL_TEXTURE0);

glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}

/**
Expand Down Expand Up @@ -151,7 +155,7 @@ void inDrawScene(vec4 area) {
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

sceneShader.use();
sceneShader.setUniform(sceneMVP,
Expand Down Expand Up @@ -264,4 +268,11 @@ void inDumpViewport(ref ubyte[] dumpTo) {

ri++;
}
}

/**
Sets the background clear color
*/
void inSetClearColor(float r, float g, float b, float a) {
inClearColor = vec4(r, g, b, a);
}
20 changes: 19 additions & 1 deletion source/inochi2d/core/texture.d
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public:

// Set default filtering and wrapping
this.setFiltering(Filtering.Linear);
this.setWrapping(Wrapping.Mirror);
this.setWrapping(Wrapping.Clamp);
}

/**
Expand Down Expand Up @@ -362,4 +362,22 @@ void inEndTextureLoading() {
enforce(started, "Texture loading pass not started!");
started = false;
textureBindings.length = 0;
}

void inTexPremultiply(ref ubyte[] data) {
foreach(i; 0..data.length/4) {
data[((i*4)+0)] = cast(ubyte)((cast(int)data[((i*4)+0)] * cast(int)data[((i*4)+3)])/255);
data[((i*4)+1)] = cast(ubyte)((cast(int)data[((i*4)+1)] * cast(int)data[((i*4)+3)])/255);
data[((i*4)+2)] = cast(ubyte)((cast(int)data[((i*4)+2)] * cast(int)data[((i*4)+3)])/255);
}
}

void inTexUnPremuliply(ref ubyte[] data) {
foreach(i; 0..data.length/4) {
if (data[((i*4)+3)] == 0) continue;

data[((i*4)+0)] = cast(ubyte)(cast(int)data[((i*4)+0)] * 255 / cast(int)data[((i*4)+3)]);
data[((i*4)+1)] = cast(ubyte)(cast(int)data[((i*4)+1)] * 255 / cast(int)data[((i*4)+3)]);
data[((i*4)+2)] = cast(ubyte)(cast(int)data[((i*4)+2)] * 255 / cast(int)data[((i*4)+3)]);
}
}

0 comments on commit 7bb3920

Please sign in to comment.