File: DrawableObject.cpp

package info (click to toggle)
transcend 0.3.dfsg1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,816 kB
  • ctags: 2,912
  • sloc: cpp: 26,890; ansic: 693; sh: 210; makefile: 131; perl: 67
file content (229 lines) | stat: -rw-r--r-- 6,274 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Modification History
 *
 * 2004-June-11   Jason Rohrer
 * Created.
 *
 * 2004-June-16   Jason Rohrer
 * Added fuctions for modifying object position, rotation, and transparency.
 *
 * 2004-June-18   Jason Rohrer
 * Changed to use triangles instead of polygons.
 *
 * 2004-June-20   Jason Rohrer
 * Added fuction for modifying scale.
 * Added function for testing circle containment.
 *
 * 2004-June-21   Jason Rohrer
 * Added fuction for getting minimum border distance.
 */



#include <GL/gl.h>
#include <float.h>


#include "DrawableObject.h"



DrawableObject::DrawableObject( int inNumTriangleVertices,
                                Vector3D **inTriangleVertices,
                                Color **inTriangleVertexFillColors,
                                int inNumBorderVertices,
                                Vector3D **inBorderVertices,
                                Color **inBorderVertextColors,
                                float inBorderWidth )
    : mNumTriangleVertices( inNumTriangleVertices ),
      mTriangleVertices( inTriangleVertices ),
      mTriangleVertexFillColors( inTriangleVertexFillColors ),
      mNumBorderVertices( inNumBorderVertices ),
      mBorderVertices( inBorderVertices ),
      mBorderVertexColors( inBorderVertextColors ),
      mBorderWidth( inBorderWidth ) {

    }

        

DrawableObject::~DrawableObject() {

    int i;
    for( i=0; i<mNumTriangleVertices; i++ ) {
        delete mTriangleVertices[i];
        delete mTriangleVertexFillColors[i];
        }
    for( i=0; i<mNumBorderVertices; i++ ) {
        delete mBorderVertices[i];
        delete mBorderVertexColors[i];
        }
    
    delete [] mTriangleVertices;
    delete [] mTriangleVertexFillColors;
    delete [] mBorderVertices;
    delete [] mBorderVertexColors;
    }



void DrawableObject::rotate( Angle3D *inRotation ) {
    int i;
    for( i=0; i<mNumTriangleVertices; i++ ) {
        mTriangleVertices[i]->rotate( inRotation );
        }
    for( i=0; i<mNumBorderVertices; i++ ) {
        mBorderVertices[i]->rotate( inRotation );
        }
    }



void DrawableObject::move( Vector3D *inPosition ) {
    int i;
    for( i=0; i<mNumTriangleVertices; i++ ) {
        mTriangleVertices[i]->add( inPosition );
        }
    for( i=0; i<mNumBorderVertices; i++ ) {
        mBorderVertices[i]->add( inPosition );
        }
    }



void DrawableObject::scale( double inScale ) {
    int i;
    for( i=0; i<mNumTriangleVertices; i++ ) {
        mTriangleVertices[i]->scale( inScale );
        }
    for( i=0; i<mNumBorderVertices; i++ ) {
        mBorderVertices[i]->scale( inScale );
        }
    }



void DrawableObject::fade( double inAlphaScale ) {
    int i;
    for( i=0; i<mNumTriangleVertices; i++ ) {
        mTriangleVertexFillColors[i]->a *= inAlphaScale;
        }
    for( i=0; i<mNumBorderVertices; i++ ) {
        mBorderVertexColors[i]->a *= inAlphaScale;
        }
    }



char DrawableObject::isBorderInCircle( Vector3D *inCenter,
                                       double inRadius ) {
    for( int i=0; i<mNumBorderVertices; i++ ) {
        if( mBorderVertices[i]->getDistance( inCenter ) <= inRadius ) {
            return true;
            }
        }
    
    // else none inside circle
    return false;
    }



double DrawableObject::getBorderMaxDistance( Vector3D *inPoint ) {
    double maxDistance = 0;
    for( int i=0; i<mNumBorderVertices; i++ ) {
        double distance = mBorderVertices[i]->getDistance( inPoint );
        
        if( distance > maxDistance ) {
            maxDistance = distance;
            }
        }
    
    return maxDistance;
    }



double DrawableObject::getBorderMinDistance( Vector3D *inPoint ) {
    double minDistance = DBL_MAX;
    for( int i=0; i<mNumBorderVertices; i++ ) {
        double distance = mBorderVertices[i]->getDistance( inPoint );
        
        if( distance < minDistance ) {
            minDistance = distance;
            }
        }
    
    return minDistance;
    }


        
void DrawableObject::draw( double inScale, Angle3D *inRotation,
                           Vector3D *inPosition ) {

    Vector3D **worldTriangleVertices = new Vector3D*[ mNumTriangleVertices ];
    Vector3D **worldBorderVertices = new Vector3D*[ mNumBorderVertices ];
    int i;
    
    // transform the vertices
    // (do this once here before drawing since we draw them twice,
    //  once for the filled polygon and once for the border)
    for( i=0; i<mNumTriangleVertices; i++ ) {
        worldTriangleVertices[i] = new Vector3D( mTriangleVertices[i] );
        worldTriangleVertices[i]->scale( inScale );
        worldTriangleVertices[i]->rotate( inRotation );
        worldTriangleVertices[i]->add( inPosition );
        }
    for( i=0; i<mNumBorderVertices; i++ ) {
        worldBorderVertices[i] = new Vector3D( mBorderVertices[i] );
        worldBorderVertices[i]->scale( inScale );
        worldBorderVertices[i]->rotate( inRotation );
        worldBorderVertices[i]->add( inPosition );
        }
    
    // draw the filled polygon
    glBegin( GL_TRIANGLES );
    
        for( i=0; i<mNumTriangleVertices; i++ ) {
            glColor4f( mTriangleVertexFillColors[i]->r,
                       mTriangleVertexFillColors[i]->g,
                       mTriangleVertexFillColors[i]->b,
                       mTriangleVertexFillColors[i]->a );

            glVertex2d( worldTriangleVertices[i]->mX,
                        worldTriangleVertices[i]->mY );
            }
    
    glEnd();


    // draw the border
    glLineWidth( mBorderWidth );
    
    glBegin( GL_LINE_LOOP );
    
        for( i=0; i<mNumBorderVertices; i++ ) {
            glColor4f( mBorderVertexColors[i]->r,
                       mBorderVertexColors[i]->g,
                       mBorderVertexColors[i]->b,
                       mBorderVertexColors[i]->a );

            glVertex2d( worldBorderVertices[i]->mX,
                        worldBorderVertices[i]->mY );
            }
    
    glEnd();


    for( i=0; i<mNumTriangleVertices; i++ ) {
        delete worldTriangleVertices[i];
        }
    delete [] worldTriangleVertices;

    for( i=0; i<mNumBorderVertices; i++ ) {
        delete worldBorderVertices[i];
        }
    delete [] worldBorderVertices;

    }