001package com.fs.starfarer.api.impl.campaign.terrain;
002
003import java.awt.Color;
004
005import org.lwjgl.opengl.GL11;
006import org.lwjgl.util.vector.Vector2f;
007
008import com.fs.starfarer.api.Global;
009import com.fs.starfarer.api.graphics.SpriteAPI;
010import com.fs.starfarer.api.util.Misc;
011
012public class RingRenderer {
013
014        
015        private SpriteAPI texture;
016
017
018        public RingRenderer(String cat, String key) {
019                texture = Global.getSettings().getSprite(cat, key);
020        }
021        
022        
023        public void render(Vector2f loc, float minR, float maxR, Color color, boolean spiral, float factor, float alphaMult) {
024                
025                float middleRadius = minR + (maxR - minR) / 2f;
026                middleRadius *= factor;
027                
028                float angle = 0f;
029                
030                float circ = (float) (Math.PI * 2f * middleRadius);
031                float pixelsPerSegment = 5f;
032                //float segments = circ / pixelsPerSegment;
033                float segments = Math.round(circ / pixelsPerSegment);
034                
035                float startRad = (float) Math.toRadians(0);
036                float endRad = (float) Math.toRadians(360f);
037                float spanRad = Misc.normalizeAngle(endRad - startRad);
038                float anglePerSegment = spanRad / segments;
039                
040                float x = loc.x;
041                float y = loc.y;
042                
043                GL11.glPushMatrix();
044                GL11.glTranslatef(x * factor, y * factor, 0);
045                GL11.glRotatef(angle, 0, 0, 1);
046                
047                GL11.glEnable(GL11.GL_TEXTURE_2D);
048                texture.bindTexture();
049                //GL11.glDisable(GL11.GL_TEXTURE_2D);
050                
051                GL11.glEnable(GL11.GL_BLEND);
052                GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
053                
054                GL11.glColor4ub((byte)color.getRed(),
055                                                (byte)color.getGreen(),
056                                                (byte)color.getBlue(),
057                                                (byte)((float) color.getAlpha() * alphaMult));
058                
059                
060                float texWidth = texture.getTextureWidth();
061                float imageWidth = texture.getWidth();
062                float bandWidthInTexture = imageWidth;
063                
064                float thickness = (maxR - minR) * 1f * factor;
065                float radius = middleRadius - thickness / 2f;
066                if (spiral) {
067                        float prev = (radius + thickness);
068                        thickness = Math.min(thickness, (radius + thickness) * 0.25f);
069                        radius = prev - thickness;
070                }
071                
072                float texProgress = 0f;
073                float texHeight = texture.getTextureHeight();
074                float imageHeight = texture.getHeight();
075                float texPerSegment = pixelsPerSegment * texHeight / imageHeight * bandWidthInTexture / thickness;
076                
077                //texPerSegment = 0.01f;
078                float totalTex = Math.max(1f, Math.round(texPerSegment * segments));
079                texPerSegment = totalTex / segments;
080                
081
082                float leftTX = 0;
083                float rightTX = 1;
084//              float bandIndex = 0;
085//              float leftTX = (float) bandIndex * texWidth * bandWidthInTexture / imageWidth;
086//              float rightTX = (float) (bandIndex + 1f) * texWidth * bandWidthInTexture / imageWidth - 0.001f;
087
088                float minSpiralRadius = radius * 0.01f;
089                float spiralFactor = 1.25f;
090                //System.out.println(hashCode());
091//              if (!GLListManager.callList(token)) {
092//                      token = GLListManager.beginList();
093                        GL11.glBegin(GL11.GL_QUAD_STRIP);
094                        
095                        if (!spiral) {
096                                for (float i = 0; i < segments; i++) {
097                                        float theta = anglePerSegment * i;// + (float) Math.toRadians(angle);
098                                        float cos = (float) Math.cos(theta);
099                                        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}