/** * Sets polygon with repeating texture region, the size of repeating grid is equal to region size * * @param region - region to repeat * @param vertices - cw vertices of polygon * @param density - number of regions per polygon width bound */ public void setPolygon(TextureRegion region, float[] vertices, float density) { this.region = region; vertices = offset(vertices); Polygon polygon = new Polygon(vertices); Polygon tmpPoly = new Polygon(); Polygon intersectionPoly = new Polygon(); EarClippingTriangulator triangulator = new EarClippingTriangulator(); int idx; Rectangle boundRect = polygon.getBoundingRectangle(); if (density == -1) density = boundRect.getWidth() / region.getRegionWidth(); float regionAspectRatio = (float) region.getRegionHeight() / (float) region.getRegionWidth(); cols = (int) (Math.ceil(density)); gridWidth = boundRect.getWidth() / density; gridHeight = regionAspectRatio * gridWidth; rows = (int) Math.ceil(boundRect.getHeight() / gridHeight); for (int col = 0; col < cols; col++) { for (int row = 0; row < rows; row++) { float[] verts = new float[8]; idx = 0; verts[idx++] = col * gridWidth; verts[idx++] = row * gridHeight; verts[idx++] = (col) * gridWidth; verts[idx++] = (row + 1) * gridHeight; verts[idx++] = (col + 1) * gridWidth; verts[idx++] = (row + 1) * gridHeight; verts[idx++] = (col + 1) * gridWidth; verts[idx] = (row) * gridHeight; tmpPoly.setVertices(verts); Intersector.intersectPolygons(polygon, tmpPoly, intersectionPoly); verts = intersectionPoly.getVertices(); if (verts.length > 0) { parts.add(snapToGrid(verts)); ShortArray arr = triangulator.computeTriangles(verts); indices.add(arr.toArray()); } else { // adding null for key consistancy, needed to get col/row from key // the other alternative is to make parts - IntMap<FloatArray> parts.add(null); } } } buildVertices(); }
private final void compute(int verticesLength) { // boolean isTransparent = shader.getVertexShader().getTransparency() != 0.; if (polygonCount + 1 >= this.polygons.length) increasePolygonCapacity(); Polygon p = polygons[polygonCount++]; p.length = verticesLength; // We store the current shader for later shading p.setShader(shader); for (int i = 0; i < verticesLength; i++) { p.vertices[i] = vertexCount; vertexCount += Polygon.VERTEX_LENGTH; } // sky box shaders need the matrix... environment.setMatrix(matrix); // **** shader.shadePolygon(p, vertexData, environment); int numClip = environment.getNumClippingPlanes(); if (numClip > 0) { ClippingPlaneSoft[] cp = environment.getClippingPlanes(); // Intersector.dehomogenize(p,vertexData); for (int k = 0; k < numClip; k++) { double d = VecMat.dot(cp[k].getNormal(), cp[k].getPoint()); // System.out.println("d "+d); int res = Intersector.clipToHalfspace(p, cp[k].getNormal(), -1, d, this); if (res == -1) { // clipped out p.setShader(null); vertexCount -= p.length * Polygon.VERTEX_LENGTH; polygonCount--; return; } } } for (int i = 0; i < p.length; i++) { perspective.perspective(vertexData, p.vertices[i]); } boolean clippedAway = clipPolygon(); if (!clippedAway) { // if (p.length == 0) // System.out.println("ZEROLENGTH!"); // TODO: check wether we sort before computing this. // boolean isTransparent = (shader.getVertexShader().getTransparency() != // 0.)||shader.hasTexture()||shader.interpolateAlpha(); // shader might have changed after lighting: boolean isTransparent = p.getShader().needsSorting(); //// isTransparent = false; if (triangulate) { PolygonShader ps = p.getShader(); if (polygonCount + 1 >= this.polygons.length) increasePolygonCapacity(); polygonCount--; for (int i = 0, pl = p.length - 2; i < pl; i++) { Polygon pi = polygons[polygonCount++]; pi.length = 3; pi.vertices[0] = p.vertices[0]; pi.vertices[1] = p.vertices[i + 1]; pi.vertices[2] = p.vertices[i + 2]; pi.setShader(ps); if (queueOpaque || isTransparent) { if (sortOpaque || isTransparent) pi.computeCenterZ(vertexData); // pi.computeMaxZ(vertexData); // return; } else { renderer.renderPolygon(pi, vertexData, ps.isOutline()); pi.setShader(null); // TODO:check wether this is save // vertexCount -= p.length * Polygon.VERTEX_LENGTH; polygonCount--; } } return; } else { if (queueOpaque || isTransparent) { if (sortOpaque || isTransparent) p.computeCenterZ(vertexData); // p.computeMaxZ(vertexData); return; } else { renderer.renderPolygon(p, vertexData, p.getShader().isOutline()); } } } // if the polygon was clipped completely or if the polygon was opaque and therefore // rendered already, we can free its resources: p.setShader(null); vertexCount -= p.length * Polygon.VERTEX_LENGTH; polygonCount--; }