Skip to content

Commit

Permalink
Added rounded corners to ui test
Browse files Browse the repository at this point in the history
  • Loading branch information
david-palm committed Sep 21, 2022
1 parent 271b7b2 commit 98a4ec6
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 29 deletions.
41 changes: 37 additions & 4 deletions app/src/main/assets/shaders/ui/card.fs
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
#version 320 es
precision highp float;

uniform vec4 uColor;
// All corner radii sorted from left to right, top to bottom
uniform vec2 uRadii[4];
const float EPSILON = 0.02;

in vec2 fragmentPosition;
out vec4 fragColor;

uniform mat4 uModel;
uniform vec4 uColor;
// All corner radii sorted from left to right, top to bottom. Radii are given in percentages
uniform vec2 uRoundCorners[4];

bool isClose(float a, float b){
return (abs(a - b) < EPSILON);
}
void main(){
fragColor = uColor;
fragColor = vec4(0.0, 0.0, 0.0, 0.0);

// Calculating corner points. Radii are multiplied by 0.5 to convert percentage into coordinate
vec2 corners[4];
corners[0] = vec2(-1.0 + uRoundCorners[0].x, 1.0 - uRoundCorners[0].y);
corners[1] = vec2(1.0 - uRoundCorners[1].x, 1.0 - uRoundCorners[1].y);
corners[2] = vec2(-1.0 + uRoundCorners[2].x, -1.0 + uRoundCorners[2].y);
corners[3] = vec2(1.0 - uRoundCorners[3].x, -1.0 + uRoundCorners[3].y);

// Coloring everything except the corners
if( fragmentPosition.x > corners[0].x && fragmentPosition.x < corners[1].x){
fragColor = uColor;
}
if( fragmentPosition.y < corners[0].y && fragmentPosition.y > corners[2].y){
fragColor = uColor;
}

// Calculating if fragment belongs to rounded corner
for(int i = 0; i < 4; i++){
// Transposing position into "corner space"
vec2 positionsEllipseSpace = fragmentPosition - corners[i];
float x = pow(positionsEllipseSpace.x, 2.0) / pow(corners[i].x, 2.0);
float y = pow(positionsEllipseSpace.y, 2.0) / pow(corners[i].y, 2.0);
if(x + y <= 1.0){
fragColor = vec4(0.8, 0.8, 0.8, 1.0);
}
}
}
3 changes: 3 additions & 0 deletions app/src/main/assets/shaders/ui/card.vs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ layout(location=1) in vec2 aTexCoord;

uniform mat4 uModel;

out vec2 fragmentPosition;

void main(){
fragmentPosition = aPosition;
gl_Position = uModel * vec4(aPosition, 0.0, 1.0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig){
layerStack.add(new Scene(new Vec2(width, height), context));
// Creating UI test
CardMaterial material = new CardMaterial(context, new Vec4(0.8f, 0.8f, 0.8f, 0.2f));
Card card = new Card(material, new Vec2(0.0f, -0.9f), new Vec2(1.0f, 0.2f), 0.0f);
Card card = new Card(material, new Vec2(0.0f, -0.9f), new Vec2(1.0f, 0.2f), 0.0f, 0.5f);;
layerStack.add(new Component(card));

GLES32.glEnable(GLES32.GL_DEPTH_TEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ public void sendToShader(){
if(object instanceof Mat4){
GLES32.glUniformMatrix4fv(id, 1, false, ((Mat4) object).getValues());
}
if(object instanceof Vec2[]){
// Converting Vec2 array into float array so that it can be passed to the shader
float[] array = new float[((Vec2[]) object).length * 2];
for(int i = 0; i < ((Vec2[]) object).length; i++){
array[i * 2] = ((Vec2[]) object)[i].x;
array[i * 2 + 1] = ((Vec2[]) object)[i].y;
}
GLES32.glUniform2fv(id, ((Vec2[]) object).length, array, 0);
}
if(object instanceof Vec3[]){
// Converting Vec3 array into float array so that it can be passed to the shader
float[] array = new float[((Vec3[]) object).length * 3];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Scene(Vec2 aspectRatio, Context context){
// Creating camera and lights
camera = new Camera(new Vec3( 0.0f, 0.0f, -5.0f), 60.0f, 9.0f / 16.0f);
pointLights = new PointLight[1];
pointLights[0] = new PointLight(new Vec3(1.0f, 0.0f, 1.0f), new Vec3(1.0f, 1.0f, 1.0f), 1.0f);
pointLights[0] = new PointLight(new Vec3(1.0f, 0.0f, 1.0f), new Vec3(1.0f, 1.0f, 1.0f), 1.25f);
ambientLight = new Vec3(0.25f, 0.25f, 0.25f);

// Creating a cube
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,49 @@

public class Card extends Object2D {
CardMaterial material;
Vec2[] roundCorners;

public Card (Context context){
super(new Rectangle(), new CardMaterial(context));
material = new CardMaterial(context);
public Card(CardMaterial material, Vec2 position, Vec2 scale, float rotation, Vec2[] roundCorners){
super(new Rectangle(), material, position, scale, rotation);
this.material = material;
this.roundCorners = roundCorners;
}

public Card(CardMaterial material, Vec2 position, Vec2 scale, float rotation, Vec2 roundCorners){
super(new Rectangle(), material, position, scale, rotation);
this.material = material;
this.roundCorners = new Vec2[4];
for(int i = 0; i < 4; i++){
this.roundCorners[i] = roundCorners;
}
}

public Card(CardMaterial material, Vec2 position, Vec2 scale, float rotation, float roundCorners){
super(new Rectangle(), material, position, scale, rotation);
this.material = material;
this.roundCorners = new Vec2[4];
for(int i = 0; i < 4; i++){
this.roundCorners[i] = new Vec2(roundCorners, roundCorners);
}
}

public Card(CardMaterial material, Vec2 position, Vec2 scale, float rotation){
super(new Rectangle(), material, position, scale, rotation);
this.material = material;
this.roundCorners = new Vec2[4];
}


public Card (Context context){
super(new Rectangle(), new CardMaterial(context));
material = new CardMaterial(context);
roundCorners = new Vec2[4];
}

public Card(CardMaterial material){
super(new Rectangle(), material);
this.material = material;
roundCorners = new Vec2[4];
}

@Override
Expand All @@ -40,6 +70,23 @@ public void onRender(){
// Sending matrices to shader
material.getShader().setModel(model);
material.getShader().setColor(material.getColor());
material.getShader().setRoundCorners(roundCorners);
mesh.onRender();
}

public Vec2[] getRoundCorners(){
return roundCorners;
}

public void setRoundCorners(Vec2[] roundCorners){
if(roundCorners.length <= 4){
for(int i = 0; i < roundCorners.length; i++){
this.roundCorners[i] = roundCorners[i];
}
}
if(roundCorners.length > 4){
Log.e("CardMaterial", "A maximum of four corners is allowed");
this.roundCorners = new Vec2[4];
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.opengl3renderer.ui.object2d.card;

import android.content.Context;
import android.util.Log;

import com.example.opengl3renderer.math.Vec2;
import com.example.opengl3renderer.math.Vec4;
import com.example.opengl3renderer.renderer.Texture;
import com.example.opengl3renderer.ui.object2d.Material2D;
Expand All @@ -11,35 +13,24 @@ public class CardMaterial extends Material2D {
boolean textured;
Vec4 color;
Texture texture;
float roundness;

public CardMaterial(Context context){
shader = new CardShader(context);
textured = false;
color = new Vec4(1.0f);
texture = null;
roundness = 0.0f;
}
public CardMaterial(Context context, Vec4 color){
shader = new CardShader(context);
textured = false;
this.color = color;
texture = null;
roundness = 0.0f;
}
public CardMaterial(Context context, Texture texture){
shader = new CardShader(context);
textured = true;
color = null;
this.texture = texture;
roundness = 0.0f;
}
public CardMaterial(Context context, Vec4 color, float roundness){
shader = new CardShader(context);
textured = false;
this.color = color;
texture = null;
this.roundness = roundness;
}

public CardShader getShader(){
Expand Down Expand Up @@ -74,11 +65,4 @@ public void setTexture(Texture texture){
this.texture = texture;
}

public float getRoundness(){
return roundness;
}

public void setRoundness(float roundness){
this.roundness = roundness;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;

import com.example.opengl3renderer.math.Vec2;
import com.example.opengl3renderer.math.Vec4;
import com.example.opengl3renderer.renderer.Shader;
import com.example.opengl3renderer.renderer.Uniform;
Expand All @@ -11,18 +12,23 @@ public class CardShader extends Object2DShader {
Uniform textured;
Uniform color;
Uniform texture;
Uniform roundness;
Uniform roundCorners;

public CardShader(Context context){
super(context, "shaders/ui/card.vs", "shaders/ui/card.fs");
textured = new Uniform("uTextured", shaderProgramId);
color = new Uniform("uColor", shaderProgramId);
texture = new Uniform("uTexture", shaderProgramId);
roundness = new Uniform("uRoundness", shaderProgramId);
roundCorners = new Uniform("uRoundCorners", shaderProgramId);
}

public void setColor(Vec4 color){
this.color.setObject(color);
this.color.sendToShader();
}

public void setRoundCorners(Vec2[] roundCorners){
this.roundCorners.setObject(roundCorners);
this.roundCorners.sendToShader();
}
}

0 comments on commit 98a4ec6

Please sign in to comment.