Rev 1309 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1309 | chris | 1 | using System; |
| 2 | using System.Collections.Generic; |
||
| 3 | using System.Linq; |
||
| 4 | using System.Text; |
||
| 5 | using System.Threading.Tasks; |
||
| 6 | |||
| 7 | using BauzoidNET.math; |
||
| 8 | using BauzoidNET.graphics; |
||
| 9 | using BauzoidNET.graphics.model; |
||
| 10 | using BauzoidNET.graphics.renderstates; |
||
| 11 | using BauzoidNET.graphics.sprite; |
||
| 12 | |||
| 13 | namespace BauzoidNET.graphics |
||
| 14 | { |
||
| 15 | public class RenderUtil |
||
| 16 | { |
||
| 17 | private RenderUtil() { } |
||
| 18 | |||
| 19 | // Constants======================================================================================== |
||
| 20 | |||
| 21 | public const int NUM_ELLIPSE_STEPS = 16; |
||
| 22 | |||
| 23 | // Embedded Types=================================================================================== |
||
| 24 | |||
| 25 | // Fields=========================================================================================== |
||
| 26 | |||
| 27 | // Methods========================================================================================== |
||
| 1470 | chris | 28 | |
| 29 | /** Render a line in world space. */ |
||
| 30 | public static void drawLine(Graphics graphics, Line2 line, Vector4 color) |
||
| 31 | { |
||
| 32 | drawLine(graphics, line.a.x, line.a.y, line.b.x, line.b.y, color); |
||
| 33 | } |
||
| 1309 | chris | 34 | |
| 35 | /** Render a line in world space. */ |
||
| 36 | public static void drawLine(Graphics graphics, float x1, float y1, float x2, float y2, Vector4 color) |
||
| 37 | { |
||
| 38 | float[] vertices = {x1, y1, 0.0f, x2, y2, 0.0f}; |
||
| 39 | |||
| 40 | renderPrimitives(graphics, vertices, Matrix4.createIdentity(), color); |
||
| 41 | } |
||
| 1470 | chris | 42 | |
| 43 | /** Render a line in world space. */ |
||
| 44 | public static void drawLine(Graphics graphics, Line2 line, SpriteTransform transform, Vector4 color) |
||
| 45 | { |
||
| 46 | drawLine(graphics, line.a.x, line.a.y, line.b.x, line.b.y, transform, color); |
||
| 47 | } |
||
| 1309 | chris | 48 | |
| 49 | /** Render a line in pixel sprite space. */ |
||
| 50 | public static void drawLine(Graphics graphics, float x1, float y1, float x2, float y2, SpriteTransform transform, Vector4 color) |
||
| 51 | { |
||
| 52 | float[] vertices = {x1, y1, 0.0f, x2, y2, 0.0f}; |
||
| 53 | |||
| 54 | renderPrimitives(graphics, vertices, transform.getTransform(), color); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** Render an arrow in world space from startX/startY to endX/endY. */ |
||
| 58 | public static void drawArrow(Graphics graphics, float startX, float startY, float endX, float endY, Vector4 color) |
||
| 59 | { |
||
| 60 | renderPrimitives(graphics, generateArrowVertices(startX, startY, endX, endY), Matrix4.createIdentity(), color); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** Render an arrow in world space from startX/startY to endX/endY. */ |
||
| 64 | public static void drawArrow(Graphics graphics, float startX, float startY, float endX, float endY, SpriteTransform transform, Vector4 color) |
||
| 65 | { |
||
| 66 | renderPrimitives(graphics, generateArrowVertices(startX, startY, endX, endY), transform.getTransform(), color); |
||
| 67 | } |
||
| 68 | |||
| 69 | private static float[] generateArrowVertices(float startX, float startY, float endX, float endY) |
||
| 70 | { |
||
| 71 | // vector in reverse direction |
||
| 72 | float dx = startX - endX; |
||
| 73 | float dy = startY - endY; |
||
| 74 | float d = (float)Math.Sqrt((dx*dx) + (dy*dy)); |
||
| 75 | dx /= d; |
||
| 76 | dy /= d; |
||
| 77 | |||
| 78 | float arrowSize = Math.Min(d / 4.0f, 20.0f); |
||
| 79 | |||
| 80 | // side vector (normal to reverse vector) |
||
| 81 | float sideX = dy; |
||
| 82 | float sideY = -dx; |
||
| 83 | |||
| 84 | // two arrow sides |
||
| 85 | float arrowLeftX = endX + dx * arrowSize + sideX * arrowSize; |
||
| 86 | float arrowLeftY = endY + dy * arrowSize + sideY * arrowSize; |
||
| 87 | |||
| 88 | float arrowRightX = endX + dx * arrowSize - sideX * arrowSize; |
||
| 89 | float arrowRightY = endY + dy * arrowSize - sideY * arrowSize; |
||
| 90 | |||
| 91 | float[] vertices = { |
||
| 92 | startX, startY, 0.0f, |
||
| 93 | endX, endY, 0.0f, |
||
| 94 | arrowLeftX, arrowLeftY, 0.0f, |
||
| 95 | endX, endY, 0.0f, |
||
| 96 | arrowRightX, arrowRightY, 0.0f |
||
| 97 | }; |
||
| 98 | |||
| 99 | return vertices; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** Render a quad in world space. */ |
||
| 103 | public static void drawQuad(Graphics graphics, float x1, float y1, float x2, float y2, Vector4 color) |
||
| 104 | { |
||
| 105 | float[] vertices = |
||
| 106 | { |
||
| 107 | x1, y1, 0.0f, |
||
| 108 | x2, y1, 0.0f, |
||
| 109 | x2, y2, 0.0f, |
||
| 110 | x1, y2, 0.0f, |
||
| 111 | x1, y1, 0.0f, |
||
| 112 | }; |
||
| 113 | |||
| 114 | renderPrimitives(graphics, vertices, Matrix4.createIdentity(), color); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** Render a quad in pixel sprite space. */ |
||
| 118 | public static void drawQuad(Graphics graphics, float x1, float y1, float x2, float y2, SpriteTransform transform, Vector4 color) |
||
| 119 | { |
||
| 120 | float[] vertices = |
||
| 121 | { |
||
| 122 | x1, y1, 0.0f, |
||
| 123 | x2, y1, 0.0f, |
||
| 124 | x2, y2, 0.0f, |
||
| 125 | x1, y2, 0.0f, |
||
| 126 | x1, y1, 0.0f, |
||
| 127 | }; |
||
| 128 | |||
| 129 | renderPrimitives(graphics, vertices, transform.getTransform(), color); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** Render a quad that has the same position, size, rotation, etc. as a Sprite given by a SpriteTransform. */ |
||
| 133 | public static void drawQuad(Graphics graphics, SpriteTransform transform, Vector4 color) |
||
| 134 | { |
||
| 135 | float[] vertices = |
||
| 136 | { |
||
| 137 | -1, -1, 0.0f, |
||
| 138 | 1, -1, 0.0f, |
||
| 139 | 1, 1, 0.0f, |
||
| 140 | -1, 1, 0.0f, |
||
| 141 | -1, -1, 0.0f, |
||
| 142 | }; |
||
| 143 | |||
| 144 | renderPrimitives(graphics, vertices, transform.getNormalizedTransform(), color); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** Render an ellipse in world space. */ |
||
| 148 | public static void drawEllipse(Graphics graphics, float x1, float y1, float x2, float y2, Vector4 color) |
||
| 149 | { |
||
| 150 | float centerX = (x1 + x2)/2; |
||
| 151 | float centerY = (y1 + y2)/2; |
||
| 152 | |||
| 153 | float radiusX = Math.Abs(x2-x1)/2; |
||
| 154 | float radiusY = Math.Abs(y2-y1)/2; |
||
| 155 | |||
| 156 | renderPrimitives(graphics, createEllipseData(centerX, centerY, radiusX, radiusY), Matrix4.createIdentity(), color); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** Render an ellipse in pixel sprite space. */ |
||
| 160 | public static void drawEllipse(Graphics graphics, float x1, float y1, float x2, float y2, SpriteTransform transform, Vector4 color) |
||
| 161 | { |
||
| 162 | float centerX = (x1 + x2)/2; |
||
| 163 | float centerY = (y1 + y2)/2; |
||
| 164 | |||
| 165 | float radiusX = Math.Abs(x2 - x1) / 2; |
||
| 166 | float radiusY = Math.Abs(y2 - y1) / 2; |
||
| 167 | |||
| 168 | renderPrimitives(graphics, createEllipseData(centerX, centerY, radiusX, radiusY), transform.getTransform(), color); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** Render an ellipse that has the same position, size, rotation, etc. as a Sprite given by a SpriteTransform. */ |
||
| 172 | public static void drawEllipse(Graphics graphics, SpriteTransform transform, Vector4 color) |
||
| 173 | { |
||
| 174 | renderPrimitives(graphics, createEllipseData(0.0f, 0.0f, 1.0f, 1.0f), transform.getNormalizedTransform(), color); |
||
| 175 | } |
||
| 176 | |||
| 177 | public static float[] createEllipseData(float centerX, float centerY, float radiusX, float radiusY) |
||
| 178 | { |
||
| 179 | float angleStep = 360.0f / NUM_ELLIPSE_STEPS; |
||
| 180 | |||
| 181 | float[] vertices = new float[3 * (NUM_ELLIPSE_STEPS+1)]; |
||
| 182 | |||
| 183 | for (int i = 0; i < NUM_ELLIPSE_STEPS; i++) |
||
| 184 | { |
||
| 185 | vertices[i*3+0] = centerX + MathUtil.sin(angleStep * (float)i) * radiusX; |
||
| 186 | vertices[i*3+1] = centerY + MathUtil.cos(angleStep * (float)i) * radiusY; |
||
| 187 | vertices[i*3+2] = 0.0f; |
||
| 188 | } |
||
| 189 | |||
| 190 | vertices[NUM_ELLIPSE_STEPS*3+0] = vertices[0]; |
||
| 191 | vertices[NUM_ELLIPSE_STEPS*3+1] = vertices[1]; |
||
| 192 | vertices[NUM_ELLIPSE_STEPS*3+2] = vertices[2]; |
||
| 193 | |||
| 194 | return vertices; |
||
| 195 | } |
||
| 196 | |||
| 197 | public static void renderPrimitives(Graphics graphics, float[] vertices, Matrix4 transform, Vector4 color) |
||
| 198 | { |
||
| 199 | RenderStates rs = graphics.renderStates; |
||
| 200 | PrimitiveShader shader = graphics.getPrimitiveShader(); |
||
| 201 | |||
| 202 | SimpleGeometry geom = new SimpleGeometry(graphics, Geometry.PrimitiveType.LINE_STRIP); |
||
| 203 | geom.setPositions(vertices); |
||
| 204 | |||
| 205 | rs.pushModelMatrix(); |
||
| 206 | { |
||
| 207 | rs.model = transform; |
||
| 208 | |||
| 209 | shader.activate(color); |
||
| 210 | { |
||
| 211 | geom.render(); |
||
| 212 | } |
||
| 213 | shader.deactivate(); |
||
| 214 | } |
||
| 215 | rs.popModelMatrix(); |
||
| 216 | |||
| 217 | geom.dispose(); |
||
| 218 | } |
||
| 219 | |||
| 220 | |||
| 221 | } |
||
| 222 | } |