diff --git a/core/assets/shaders/planet.fragment.glsl b/core/assets/shaders/planet.fragment.glsl new file mode 100755 index 0000000000..17602c704f --- /dev/null +++ b/core/assets/shaders/planet.fragment.glsl @@ -0,0 +1,9 @@ +#ifdef GL_ES +precision mediump float; +#endif + +varying vec4 v_col; + +void main(){ + gl_FragColor = v_col; +} diff --git a/core/assets/shaders/planet.vertex.glsl b/core/assets/shaders/planet.vertex.glsl new file mode 100755 index 0000000000..3a9adcf278 --- /dev/null +++ b/core/assets/shaders/planet.vertex.glsl @@ -0,0 +1,16 @@ +attribute vec4 a_position; +attribute vec3 a_normal; +attribute vec4 a_color; +uniform mat4 u_projModelView; +varying vec4 v_col; + +const vec3 ambientColor = vec3(1.0); +const vec3 ambientDir = normalize(vec3(1.0, 1.0, 1.0)); +const vec3 diffuse = vec3(0.5); + +void main(){ + vec3 norc = ambientColor * clamp(lerp(dot(a_normal, ambientDir), 1.0, 0.6), 0.0, 1.0); + + v_col = a_color * vec4(norc, 1.0); + gl_Position = u_projModelView * a_position; +} diff --git a/core/src/mindustry/graphics/PlanetRenderer.java b/core/src/mindustry/graphics/PlanetRenderer.java index 09f905136d..0a3e283360 100644 --- a/core/src/mindustry/graphics/PlanetRenderer.java +++ b/core/src/mindustry/graphics/PlanetRenderer.java @@ -4,6 +4,7 @@ import arc.*; import arc.graphics.*; import arc.graphics.g2d.*; import arc.graphics.g3d.*; +import arc.graphics.gl.*; import arc.math.*; import arc.math.geom.*; import arc.struct.*; @@ -11,11 +12,11 @@ import arc.util.*; import arc.util.noise.*; public class PlanetRenderer{ - private ImmediateRenderer3D rend = new ImmediateRenderer3D(500000, true, true, 0); + private ImmediateRenderer3D rend = new ImmediateRenderer3D(500000, true, true, 0, new Shader(Core.files.internal("shaders/planet.vertex.glsl").readString(), Core.files.internal("shaders/planet.fragment.glsl").readString())); private Camera3D cam = new Camera3D(); - private ShortArray tmpIndices = new ShortArray(); - private ShortArray indices = new ShortArray(); + private IntArray tmpIndices = new IntArray(); + private IntArray indices = new IntArray(); private Array vertices = new Array<>(); private Color[] colors = {Color.royal, Color.tan, Color.forest, Color.olive, Color.lightGray, Color.white}; @@ -23,16 +24,17 @@ public class PlanetRenderer{ { int div = 100; - generate(15, 15, 15, div, div); + ico(); + //generate(15, 15, 15, div, div); } public void draw(){ Draw.flush(); - Gl.clearColor(0, 0, 0, 1); + Gl.clearColor(1, 1, 1, 1); Gl.clear(Gl.depthBufferBit | Gl.colorBufferBit); Gl.enable(Gl.depthTest); - Tmp.v1.trns(Time.time(), 20f); + Tmp.v1.trns(Time.time() * 2f, 20f); cam.position.set(Tmp.v1.x, 0f, Tmp.v1.y); cam.resize(Core.graphics.getWidth(), Core.graphics.getHeight()); cam.update(); @@ -40,11 +42,56 @@ public class PlanetRenderer{ cam.update(); rend.begin(cam.combined(), Gl.triangleStrip); - drawSphere(); + drawTri(); rend.end(); Gl.disable(Gl.depthTest); } + void ico(){ + float s = 2f/Mathf.sqrt(5), c = 1f/Mathf.sqrt(5); + + Array topPoints = new Array<>(); + topPoints.add(new Vec3(0, 0, 1)); + for(int i = 0; i < 5; i++){ + topPoints.add(new Vec3(s*Mathf.cos(i*2*Mathf.pi/5f), s*Mathf.sin(i*2*Mathf.pi/5f), c)); + } + topPoints.addAll(topPoints.map(v -> v.cpy().scl(-1, 1, -1))); + Array points = topPoints; + + vertices.addAll(points.map(p -> { + Vertex v = new Vertex(); + v.pos.set(p).scl(4f); + v.normal.set(p).nor(); + v.color.set(Color.royal); + return v; + })); + + for(int i = 0; i < 5; i++){ + indices.add(0,i+1,(i+1)%5+1); + indices.add(6,i+7,(i+1)%5+7); + indices.add(i+1,(i+1)%5+1,(7-i)%5+7); + indices.add(i+1,(7-i)%5+7,(8-i)%5+7); + } + + Array newVertices = new Array<>(); + IntArray newIndices = new IntArray(); + for(int i = 0; i < indices.size; i += 3){ + Vertex v1 = vertices.get(indices.get(i)); + Vertex v2 = vertices.get(indices.get(i + 1)); + Vertex v3 = vertices.get(indices.get(i + 2)); + + Vec3 nor = v1.normal.cpy().add(v2.normal).add(v3.normal).nor(); + v1 = v1.copy(); + v2 = v2.copy(); + v3 = v3.copy(); + v1.normal = v2.normal = v3.normal = nor; + newIndices.add(newVertices.size, newVertices.size + 1, newVertices.size + 2); + newVertices.add(v1, v2, v3); + } + vertices = newVertices; + indices = newIndices; + } + void generate(float width, float height, float depth, int divisionsU, int divisionsV){ float angleUFrom = 0, angleUTo = 360f, angleVFrom = 0, angleVTo = 180f; final float hw = width * 0.5f; @@ -103,6 +150,18 @@ public class PlanetRenderer{ return colors[Mathf.clamp((int)(value * colors.length), 0, colors.length - 1)]; } + void drawTri(){ + for(int i = 0; i < indices.size; i += 3){ + Vertex v1 = vertices.get(indices.get(i)); + Vertex v2 = vertices.get(indices.get(i + 1)); + Vertex v3 = vertices.get(indices.get(i + 2)); + + v1.d(); + v2.d(); + v3.d(); + } + } + void drawSphere(){ for(int i = 0; i < indices.size; i += 4){ Vertex v1 = vertices.get(indices.get(i)); @@ -132,5 +191,18 @@ public class PlanetRenderer{ rend.texCoord(uv.x, uv.y); rend.vertex(pos); } + + Vertex copy(){ + Vertex v = new Vertex(); + v.color.set(color); + v.normal.set(normal); + v.uv.set(uv); + v.pos.set(pos); + return v; + } + + public String toString(){ + return pos.toString(); + } } }