Project

General

Profile

Download (6.29 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / meshfile / TextureFactory.java @ 6a96e571

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.examples.meshfile;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
public class TextureFactory
29
  {
30
  static final int COLOR_BLACK  = 0xff000000;
31
  static final int TEXTURE_HEIGHT = 256;
32

    
33
  private static TextureFactory mThis;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
  private TextureFactory()
38
    {
39

    
40
    }
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  public static TextureFactory getInstance()
45
    {
46
    if( mThis==null ) mThis = new TextureFactory();
47

    
48
    return mThis;
49
    }
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  private float computeAngle(float dx, float dy)
54
    {
55
    float PI = (float)Math.PI;
56
    double angle = Math.atan2(dy,dx);
57
    float ret = (float)(3*PI/2-angle);
58

    
59
    if( ret>2*PI ) ret-= 2*PI;
60

    
61
    return ret;
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  private void drawCurrVertex(Canvas canvas, Paint paint, float left, float r, float stroke, float pX, float pY, float cX, float cY, float nX, float nY)
67
    {
68
    pX = (0.5f+pX)*TEXTURE_HEIGHT;
69
    pY = (0.5f-pY)*TEXTURE_HEIGHT;
70
    cX = (0.5f+cX)*TEXTURE_HEIGHT;
71
    cY = (0.5f-cY)*TEXTURE_HEIGHT;
72
    nX = (0.5f+nX)*TEXTURE_HEIGHT;
73
    nY = (0.5f-nY)*TEXTURE_HEIGHT;
74

    
75
    canvas.drawLine(left+pX,pY,left+cX,cY,paint);
76

    
77
    float aX = pX-cX;
78
    float aY = pY-cY;
79
    float bX = cX-nX;
80
    float bY = cY-nY;
81

    
82
    float aLen = (float)Math.sqrt(aX*aX+aY*aY);
83
    float bLen = (float)Math.sqrt(bX*bX+bY*bY);
84

    
85
    aX /= aLen;
86
    aY /= aLen;
87
    bX /= bLen;
88
    bY /= bLen;
89

    
90
    float sX = (aX-bX)/2;
91
    float sY = (aY-bY)/2;
92
    float sLen = (float)Math.sqrt(sX*sX+sY*sY);
93
    sX /= sLen;
94
    sY /= sLen;
95

    
96
    float startAngle = computeAngle(bX,-bY);
97
    float endAngle   = computeAngle(aX,-aY);
98
    float sweepAngle = endAngle-startAngle;
99
    if( sweepAngle<0 ) sweepAngle += 2*Math.PI;
100

    
101
    float R = r*TEXTURE_HEIGHT+stroke/2;
102

    
103
    float A = (float)(R/(Math.cos(sweepAngle/2)));
104

    
105
    float rX = cX + A*sX;
106
    float rY = cY + A*sY;
107

    
108
    startAngle *= 180/(Math.PI);
109
    sweepAngle *= 180/(Math.PI);
110

    
111
    canvas.drawArc( left+rX-R, rY-R, left+rX+R, rY+R, startAngle, sweepAngle, false, paint);
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  void drawRoundedPolygon(Canvas canvas, Paint paint, int left, float[] vertices, float stroke, int color, float r)
117
    {
118
    stroke *= TEXTURE_HEIGHT;
119

    
120
    paint.setAntiAlias(true);
121
    paint.setStrokeWidth(stroke);
122
    paint.setColor(color);
123
    paint.setStyle(Paint.Style.FILL);
124

    
125
    canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
126

    
127
    paint.setColor(COLOR_BLACK);
128
    paint.setStyle(Paint.Style.STROKE);
129

    
130
    int length = vertices.length;
131
    int numVertices = length/2;
132

    
133
    float prevX = vertices[length-2];
134
    float prevY = vertices[length-1];
135
    float currX = vertices[0];
136
    float currY = vertices[1];
137
    float nextX = vertices[2];
138
    float nextY = vertices[3];
139

    
140
    for(int vert=0; vert<numVertices; vert++)
141
      {
142
      drawCurrVertex(canvas, paint, left, r, stroke, prevX,prevY,currX,currY,nextX,nextY);
143

    
144
      prevX = currX;
145
      prevY = currY;
146
      currX = nextX;
147
      currY = nextY;
148

    
149
      if( 2*(vert+2)+1 < length )
150
        {
151
        nextX = vertices[2*(vert+2)  ];
152
        nextY = vertices[2*(vert+2)+1];
153
        }
154
      else
155
        {
156
        nextX = vertices[0];
157
        nextY = vertices[1];
158
        }
159
      }
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  public Bitmap createTexture(float[] vertices, int[] colors, float S, float R, boolean addFinalBlack)
165
    {
166
    int extra = addFinalBlack ? 1:0;
167
    int numColors = colors.length;
168

    
169
    Paint paint = new Paint();
170
    Bitmap bitmap = Bitmap.createBitmap( (numColors+extra)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
171
    Canvas canvas = new Canvas(bitmap);
172

    
173
    paint.setAntiAlias(true);
174
    paint.setTextAlign(Paint.Align.CENTER);
175
    paint.setStyle(Paint.Style.FILL);
176

    
177
    paint.setColor(COLOR_BLACK);
178
    canvas.drawRect(0, 0, (numColors+extra)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
179

    
180
    for(int face=0; face<numColors; face++)
181
      {
182
      drawRoundedPolygon(canvas, paint, face*TEXTURE_HEIGHT, vertices, S, colors[face], R);
183
      }
184

    
185
    return bitmap;
186
    }
187
  }
(5-5/5)