Starsector API
Loading...
Searching...
No Matches
RingRenderer.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.terrain;
2
3import java.awt.Color;
4
5import org.lwjgl.opengl.GL11;
6import org.lwjgl.util.vector.Vector2f;
7
8import com.fs.starfarer.api.Global;
9import com.fs.starfarer.api.graphics.SpriteAPI;
10import com.fs.starfarer.api.util.Misc;
11
12public class RingRenderer {
13
14
15 private SpriteAPI texture;
16
17
18 public RingRenderer(String cat, String key) {
19 texture = Global.getSettings().getSprite(cat, key);
20 }
21
22
23 public void render(Vector2f loc, float minR, float maxR, Color color, boolean spiral, float factor, float alphaMult) {
24
25 float middleRadius = minR + (maxR - minR) / 2f;
26 middleRadius *= factor;
27
28 float angle = 0f;
29
30 float circ = (float) (Math.PI * 2f * middleRadius);
31 float pixelsPerSegment = 5f;
32 //float segments = circ / pixelsPerSegment;
33 float segments = Math.round(circ / pixelsPerSegment);
34
35 float startRad = (float) Math.toRadians(0);
36 float endRad = (float) Math.toRadians(360f);
37 float spanRad = Misc.normalizeAngle(endRad - startRad);
38 float anglePerSegment = spanRad / segments;
39
40 float x = loc.x;
41 float y = loc.y;
42
43 GL11.glPushMatrix();
44 GL11.glTranslatef(x * factor, y * factor, 0);
45 GL11.glRotatef(angle, 0, 0, 1);
46
47 GL11.glEnable(GL11.GL_TEXTURE_2D);
48 texture.bindTexture();
49 //GL11.glDisable(GL11.GL_TEXTURE_2D);
50
51 GL11.glEnable(GL11.GL_BLEND);
52 GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
53
54 GL11.glColor4ub((byte)color.getRed(),
55 (byte)color.getGreen(),
56 (byte)color.getBlue(),
57 (byte)((float) color.getAlpha() * alphaMult));
58
59
60 float texWidth = texture.getTextureWidth();
61 float imageWidth = texture.getWidth();
62 float bandWidthInTexture = imageWidth;
63
64 float thickness = (maxR - minR) * 1f * factor;
65 float radius = middleRadius - thickness / 2f;
66 if (spiral) {
67 float prev = (radius + thickness);
68 thickness = Math.min(thickness, (radius + thickness) * 0.25f);
69 radius = prev - thickness;
70 }
71
72 float texProgress = 0f;
73 float texHeight = texture.getTextureHeight();
74 float imageHeight = texture.getHeight();
75 float texPerSegment = pixelsPerSegment * texHeight / imageHeight * bandWidthInTexture / thickness;
76
77 //texPerSegment = 0.01f;
78 float totalTex = Math.max(1f, Math.round(texPerSegment * segments));
79 texPerSegment = totalTex / segments;
80
81
82 float leftTX = 0;
83 float rightTX = 1;
84// float bandIndex = 0;
85// float leftTX = (float) bandIndex * texWidth * bandWidthInTexture / imageWidth;
86// float rightTX = (float) (bandIndex + 1f) * texWidth * bandWidthInTexture / imageWidth - 0.001f;
87
88 float minSpiralRadius = radius * 0.01f;
89 float spiralFactor = 1.25f;
90 //System.out.println(hashCode());
91// if (!GLListManager.callList(token)) {
92// token = GLListManager.beginList();
93 GL11.glBegin(GL11.GL_QUAD_STRIP);
94
95 if (!spiral) {
96 for (float i = 0; i < segments; i++) {
97 float theta = anglePerSegment * i;// + (float) Math.toRadians(angle);
98 float cos = (float) Math.cos(theta);
99 float sin = (float) Math.sin(theta);
100 float x1 = cos * radius;
101 float y1 = sin * radius;
102 float x2 = cos * (radius + thickness);
103 float y2 = sin * (radius + thickness);
104
105 GL11.glTexCoord2f(leftTX, texProgress);
106 GL11.glVertex2f(x1, y1);
107 GL11.glTexCoord2f(rightTX, texProgress);
108 GL11.glVertex2f(x2, y2);
109
110 texProgress += texPerSegment;
111 }
112
113 {
114 int i = 0;
115 float theta = anglePerSegment * i; // + (float) Math.toRadians(angle);
116 float cos = (float) Math.cos(theta);
117 float sin = (float) Math.sin(theta);
118 float x1 = cos * radius;
119 float y1 = sin * radius;
120 float x2 = cos * (radius + thickness);
121 float y2 = sin * (radius + thickness);
122
123 GL11.glTexCoord2f(leftTX, texProgress);
124 GL11.glVertex2f(x1, y1);
125 GL11.glTexCoord2f(rightTX, texProgress);
126 GL11.glVertex2f(x2, y2);
127 }
128 } else {
129 //System.out.println(radius);
130 //minSpiralRadius = 850f;
131 float numSpirals = (radius - minSpiralRadius) / (thickness * spiralFactor);
132 if (numSpirals < 2) numSpirals = 2;
133 float distMult = 1f;
134 float thicknessMult = 10f;
135 float theta = 0f;
136 for (float i = 0; i < segments * numSpirals; i++) {
137 distMult = 1f - (i / (segments * numSpirals));
138 //distMult *= distMult;
139 distMult = (float) Math.sqrt(distMult);
140 //thicknessMult = 0.5f + 0.5f * (1f - (i / (segments * numSpirals)));
141 thicknessMult = 1f;
142 float alpha = i / segments;
143 if (i > segments * (numSpirals - 1)) {
144 alpha = (segments * numSpirals - i) / segments;
145 }
146 if (alpha > 1) alpha = 1;
147 if (alpha < 0) alpha = 0;
148
149 GL11.glColor4ub((byte)color.getRed(),
150 (byte)color.getGreen(),
151 (byte)color.getBlue(),
152 (byte)((float) color.getAlpha() * alpha * alphaMult));
153
154
155 theta = anglePerSegment * i;// + (float) Math.toRadians(angle);
156 float cos = (float) Math.cos(theta);
157 float sin = (float) Math.sin(theta);
158 float x1 = cos * ((radius - minSpiralRadius) * distMult + minSpiralRadius);
159 float y1 = sin * ((radius - minSpiralRadius) * distMult + minSpiralRadius);
160 float x2 = cos * (((radius - minSpiralRadius) * distMult + thickness * thicknessMult) + minSpiralRadius);
161 float y2 = sin * (((radius - minSpiralRadius) * distMult + thickness * thicknessMult) + minSpiralRadius);
162
163 GL11.glTexCoord2f(leftTX, texProgress);
164 GL11.glVertex2f(x1, y1);
165 GL11.glTexCoord2f(rightTX, texProgress);
166 GL11.glVertex2f(x2, y2);
167
168 float circumferenceMult = ((radius - minSpiralRadius) * distMult + minSpiralRadius) / radius;
169 texProgress += texPerSegment * circumferenceMult;
170
171 if (i == 0) {
172 GL11.glColor4ub((byte)color.getRed(),
173 (byte)color.getGreen(),
174 (byte)color.getBlue(),
175 (byte)((float) color.getAlpha() * alphaMult));
176 }
177 }
178 }
179
180
181 GL11.glEnd();
182 //GLListManager.endList();
183 //}
184 GL11.glPopMatrix();
185
186 }
187
188}
static SettingsAPI getSettings()
Definition Global.java:51
void render(Vector2f loc, float minR, float maxR, Color color, boolean spiral, float factor, float alphaMult)
SpriteAPI getSprite(String filename)