~ubuntu-branches/ubuntu/trusty/3depict/trusty-proposed

« back to all changes in this revision

Viewing changes to src/gl/drawables.cpp

  • Committer: Package Import Robot
  • Author(s): D Haley
  • Date: 2013-07-20 18:31:32 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20130720183132-5ak932y2x6pwo4fs
Tags: 0.0.14-1
* Update to upstream, 0.0.14
* Enable mathgl2.x configure option
* Modify build-depends, libmgl-dev >= 2.1.32

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
17
*/
18
18
 
 
19
#if defined(WIN32) || defined(WIN64)
 
20
#include <GL/glew.h>
 
21
#endif
 
22
 
19
23
#include "drawables.h"
20
24
 
21
25
#include "common/colourmap.h"
22
26
 
23
27
 
 
28
 
 
29
 
24
30
const float DEPTH_SORT_REORDER_EPSILON = 1e-2;
25
31
 
26
32
//Static class variables
27
33
//====
28
34
const Camera *DrawableObj::curCamera = 0;
 
35
bool DrawableObj::useAlphaBlend;
 
36
TexturePool *DrawableObj::texPool;
 
37
 
 
38
unsigned int DrawableObj::winX;
 
39
unsigned int DrawableObj::winY;
 
40
 
29
41
//==
30
42
 
31
43
 
94
106
        ASSERT(isExplodable());
95
107
}
96
108
 
 
109
void DrawableObj::setTexPool(TexturePool *t)
 
110
{
 
111
        if(texPool)
 
112
                delete texPool;
 
113
 
 
114
        texPool=t;
 
115
 
 
116
}
 
117
 
 
118
void DrawableObj::clearTexPool()
 
119
{
 
120
        ASSERT(texPool);
 
121
        delete texPool;
 
122
 
 
123
}
 
124
 
97
125
Point3D DrawableObj::getCentroid() const
98
126
{
99
127
        ASSERT(!isExplodable());
136
164
{
137
165
        glColor4f(r,g,b,a);
138
166
        glBegin(GL_POINT);
139
 
        glVertex3f(origin[0],origin[1],origin[2]);
 
167
        glVertex3fv(origin.getValueArr());
140
168
        glEnd();
141
169
}
142
170
 
197
225
 
198
226
        glLineWidth(lineSize);
199
227
        glBegin(GL_LINES);
 
228
        glVertex3fv(origin.getValueArr());
200
229
        
201
230
        if(arrowSize < sqrt(std::numeric_limits<float>::epsilon()) || !drawArrow)
202
231
        {
203
 
                glVertex3f(origin[0],origin[1],origin[2]);
204
232
                glVertex3f(vector[0]+origin[0],vector[1]+origin[1],vector[2]+origin[2]);
205
233
                glEnd();
206
 
                
207
234
                //restore the old line size
208
235
                glLineWidth(oldLineWidth);
209
 
                
210
236
                glPopAttrib();
211
237
                return ;
212
238
        }
213
 
        else
214
 
        {
215
 
 
216
 
                glVertex3f(origin[0],origin[1],origin[2]);
217
 
 
218
 
                
219
 
                glVertex3f(vector[0]+origin[0],vector[1]+origin[1],vector[2]+origin[2]);
220
 
                glEnd();
221
 
                //restore the old line size
222
 
                glLineWidth(oldLineWidth);
223
 
                
224
 
                glPopAttrib();
225
 
        }
 
239
        glVertex3f(vector[0]+origin[0],vector[1]+origin[1],vector[2]+origin[2]);
 
240
        glEnd();
 
241
        //restore the old line size
 
242
        glLineWidth(oldLineWidth);
 
243
        
 
244
        glPopAttrib();
226
245
 
227
246
 
228
247
 
318
337
        
319
338
        //Now, having the needed coords, we can draw the cone
320
339
        glBegin(GL_TRIANGLE_FAN);
321
 
        glNormal3f(axis[0],axis[1],axis[2]);
 
340
        glNormal3fv(axis.getValueArr());
322
341
        glVertex3f(0,0,0);
323
342
        for(unsigned int ui=0; ui<NUM_CONE_SEGMENTS; ui++)
324
343
        {
325
344
                Point3D n;
326
345
                n=ptArray[ui];
327
346
                n.normalise();
328
 
                glNormal3f(n[0],n[1],n[2]);
329
 
                glVertex3f(ptArray[ui][0],ptArray[ui][1],ptArray[ui][2]);
 
347
                glNormal3fv(n.getValueArr());
 
348
                glVertex3fv(ptArray[ui].getValueArr());
330
349
        }
331
350
 
332
351
        glEnd();
336
355
        glBegin(GL_TRIANGLE_FAN);
337
356
        glNormal3f(-axis[0],-axis[1],-axis[2]);
338
357
        for(unsigned int ui=NUM_CONE_SEGMENTS; ui--;) 
339
 
                glVertex3f(ptArray[ui][0],ptArray[ui][1],ptArray[ui][2]);
 
358
                glVertex3fv(ptArray[ui].getValueArr());
340
359
        glEnd();
341
360
 
342
361
        glPopMatrix();
406
425
{
407
426
        glColor4f(r,g,b,a);
408
427
        glBegin(GL_TRIANGLES);
409
 
                glVertex3f((vertices[0])[0],
410
 
                        (vertices[0])[1], (vertices[0])[2]);
411
 
                glVertex3f((vertices[1])[0],
412
 
                        (vertices[1])[1], (vertices[1])[2]);
413
 
                glVertex3f((vertices[2])[0],
414
 
                        (vertices[2])[1], (vertices[2])[2]);
415
 
        glEnd();
 
428
                for(size_t ui=0;ui<3;ui++)
 
429
                        glVertex3fv(vertices[ui].getValueArr());
 
430
        glEnd();
 
431
}
 
432
 
 
433
void DrawQuad::getBoundingBox(BoundCube &b) const
 
434
{
 
435
        b.setBounds(vertices,4);
 
436
}
 
437
 
 
438
void DrawQuad::draw() const
 
439
{
 
440
        ASSERT(false);
 
441
}
 
442
 
 
443
void DrawQuad::setVertices(const Point3D *v) 
 
444
{
 
445
        for(size_t ui=0;ui<4;ui++)
 
446
                vertices[ui]=v[ui];
 
447
}
 
448
 
 
449
Point3D DrawQuad::getOrigin() const
 
450
{
 
451
        return Point3D::centroid(vertices,4);
 
452
}
 
453
 
 
454
void DrawQuad::recomputeParams(const vector<Point3D> &vecs, 
 
455
                        const vector<float> &scalars, unsigned int mode)
 
456
{
 
457
        switch(mode)
 
458
        {
 
459
                case DRAW_QUAD_BIND_ORIGIN:
 
460
                {
 
461
                        ASSERT(vecs.size() ==1 && scalars.size() ==0);
 
462
                        
 
463
                        Point3D curOrig=getOrigin();
 
464
 
 
465
                        Point3D delta = vecs[0]-curOrig;
 
466
 
 
467
                        for(size_t ui=0;ui<4;ui++)
 
468
                                vertices[ui]+=delta;
 
469
 
 
470
 
 
471
                        break;
 
472
                }
 
473
                default:
 
474
                        ASSERT(false);
 
475
        }
 
476
}
 
477
 
 
478
DrawTexturedQuad::DrawTexturedQuad() :textureData(0), textureId((unsigned int)-1)
 
479
{
 
480
}
 
481
 
 
482
DrawTexturedQuad::~DrawTexturedQuad()
 
483
{
 
484
        if(textureData)
 
485
                delete[] textureData;
 
486
 
 
487
        texPool->closeTexture(textureId);
 
488
}
 
489
 
 
490
void DrawTexturedQuad::draw() const
 
491
{
 
492
        ASSERT(glIsTexture(textureId));
 
493
        
 
494
        glEnable(GL_TEXTURE_2D);
 
495
        glBindTexture(GL_TEXTURE_2D,textureId);
 
496
        glPushAttrib(GL_CULL_FACE);
 
497
        glDisable(GL_CULL_FACE);
 
498
        
 
499
        
 
500
        glBindTexture(GL_TEXTURE_2D,textureId);
 
501
 
 
502
        glColor4f(1.0f,1.0f,1.0f,1.0f);
 
503
        const float COORD_SEQ_X[]={ 0,0,1,1};
 
504
        const float COORD_SEQ_Y[]={ 0,1,1,0};
 
505
        glBegin(GL_QUADS);
 
506
                for(size_t ui=0;ui<4;ui++)
 
507
                {
 
508
                        glTexCoord2f(COORD_SEQ_X[ui],COORD_SEQ_Y[ui]);
 
509
                        glVertex3fv(vertices[ui].getValueArr());
 
510
                }
 
511
        glEnd();
 
512
 
 
513
        glPopAttrib();
 
514
        glDisable(GL_TEXTURE_2D);       
 
515
}
 
516
 
 
517
//Call sequence
 
518
// - resize destination for texture
 
519
// - set texture by pixels
 
520
// - rebind texture
 
521
void DrawTexturedQuad::resize(size_t numX, size_t numY, 
 
522
                                        unsigned int nChannels)
 
523
{
 
524
        //reallocate texture as required
 
525
        if(textureData)
 
526
        {
 
527
                if( numX*numY*nChannels != nX*nY*channels)
 
528
                {
 
529
                        delete[] textureData;
 
530
                        textureData = new unsigned char[numX*numY*nChannels];
 
531
                }
 
532
        }
 
533
        else
 
534
                textureData = new unsigned char[numX*numY*nChannels];
 
535
 
 
536
        nX=numX;
 
537
        nY=numY;
 
538
        channels=nChannels;
 
539
 
 
540
}
 
541
 
 
542
void DrawTexturedQuad::rebindTexture()
 
543
{
 
544
        ASSERT(texPool);
 
545
        ASSERT(textureData);
 
546
        if(textureId == -1)
 
547
                texPool->genTexID(textureId);
 
548
        
 
549
        //Construc the texture
 
550
        glBindTexture(GL_TEXTURE_2D,textureId);
 
551
        glPixelStorei(GL_UNPACK_ALIGNMENT,1);
 
552
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
553
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
 
554
        glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); 
 
555
        ASSERT(channels == 3);
 
556
 
 
557
        glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,nX,nY,
 
558
                0,GL_RGB,GL_UNSIGNED_BYTE,textureData);
 
559
 
 
560
 
 
561
 
 
562
}
 
563
 
 
564
void DrawTexturedQuad::setData(size_t x, size_t y, unsigned char *entry)
 
565
{
 
566
        ASSERT(textureData);
 
567
        ASSERT(channels == 3);
 
568
 
 
569
        for(size_t ui=0;ui<channels;ui++)
 
570
                textureData[(y*nX + x)*channels + ui] = entry[ui];      
416
571
}
417
572
 
418
573
 
544
699
        if(!q || !qCap[0] || !qCap[1])
545
700
                return;
546
701
 
547
 
        //Cross product desired direction with default
 
702
        //Cross product desired drection with default
548
703
        //direction to produce rotation vector
549
704
        Point3D dir(0.0f,0.0f,1.0f);
550
705
 
687
842
 
688
843
DrawManyPoints::~DrawManyPoints() 
689
844
{
690
 
        wantsLight=false;
691
 
        haveCachedBounds=false;
692
845
}
693
846
 
694
847
void DrawManyPoints::getBoundingBox(BoundCube &b) const
1068
1221
                                        //the text direction is now the x-axis
1069
1222
                                        glTranslatef(advance/2.0f,halfHeight,0);
1070
1223
                                        //spin text around its front direction 180 degrees
1071
 
                                        //no need to translate as text sits at its baseline
 
1224
                                        //no need to trnaslate as text sits at its baseline
1072
1225
                                        glRotatef(180,0,0,1);
1073
1226
                                        //move halfway along text, noting that 
1074
1227
                                        //the text direction is now the x-axis
1329
1482
        }
1330
1483
}
1331
1484
 
1332
 
DrawTexturedQuadOverlay::DrawTexturedQuadOverlay() : texPool(0)
 
1485
DrawTexturedQuadOverlay::DrawTexturedQuadOverlay()  
1333
1486
{
1334
1487
}
1335
1488
 
1336
1489
DrawTexturedQuadOverlay::~DrawTexturedQuadOverlay()
1337
1490
{
1338
 
#ifdef DEBUG
1339
 
        textureOK=false;
1340
 
#endif
 
1491
        texPool->closeTexture(textureId);
1341
1492
}
1342
1493
 
1343
1494
void DrawTexturedQuadOverlay::setSize(float s)
1364
1515
 
1365
1516
        glEnable(GL_TEXTURE_2D);
1366
1517
        glBindTexture(GL_TEXTURE_2D,textureId);
 
1518
        glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 
1367
1519
        
1368
1520
        // Draw overlay quad 
1369
1521
        glColor3f(1.0f,1.0f,1.0f);
1393
1545
bool DrawTexturedQuadOverlay::setTexture(const char *textureFile)
1394
1546
{
1395
1547
        ASSERT(texPool);        
1396
 
        unsigned int dummy;
1397
 
 
1398
 
        textureOK= texPool->openTexture(textureFile,textureId,dummy);
 
1548
        textureOK= texPool->openTexture(textureFile,textureId);
1399
1549
        return textureOK;
1400
1550
}
1401
1551
 
1404
1554
        b.setInvalid();
1405
1555
}
1406
1556
 
 
1557
DrawAnimatedOverlay::DrawAnimatedOverlay()
 
1558
{
 
1559
        fadeIn=0.0f;
 
1560
        delayBeforeShow=0.0f;
 
1561
        textureOK=false;
 
1562
        resetTime();
 
1563
}
 
1564
 
 
1565
DrawAnimatedOverlay::~DrawAnimatedOverlay()
 
1566
{
 
1567
}
 
1568
 
 
1569
void DrawAnimatedOverlay::resetTime()
 
1570
{
 
1571
        gettimeofday(&animStartTime,NULL);
 
1572
}
 
1573
 
 
1574
bool DrawAnimatedOverlay::setTexture(const vector<string> &texFiles,
 
1575
                        float replayTime)
 
1576
{
 
1577
        repeatInterval=replayTime;
 
1578
 
 
1579
        textureOK=texPool->openTexture3D(texFiles, textureId);
 
1580
        return textureOK;
 
1581
}
 
1582
 
 
1583
void DrawAnimatedOverlay::setSize(float newLen)
 
1584
{
 
1585
        length=newLen;
 
1586
}
 
1587
 
 
1588
 
 
1589
void DrawAnimatedOverlay::draw() const
 
1590
{
 
1591
        if(!textureOK)
 
1592
                return;
 
1593
 
 
1594
        float texCoordZ;
 
1595
        float alphaVal=1.0f;
 
1596
        {
 
1597
        timeval t;
 
1598
        gettimeofday(&t,NULL);
 
1599
        float animDeltaTime=(float)(t.tv_sec - animStartTime.tv_sec) +
 
1600
                (t.tv_usec-animStartTime.tv_usec)/1.0e6;
 
1601
 
 
1602
        //Skip if we wish to show later
 
1603
        if(animDeltaTime < delayBeforeShow)
 
1604
                return;
 
1605
 
 
1606
 
 
1607
        if(fadeIn > 0.0f && (delayBeforeShow + fadeIn > animDeltaTime) ) 
 
1608
                alphaVal= (animDeltaTime - delayBeforeShow )/(fadeIn) ;
 
1609
 
 
1610
        texCoordZ=fmod(animDeltaTime,repeatInterval);
 
1611
        texCoordZ/=repeatInterval;
 
1612
        }
 
1613
        ASSERT(glIsTexture(textureId));
 
1614
        
 
1615
        glMatrixMode(GL_PROJECTION);    
 
1616
        glPushMatrix();
 
1617
        glLoadIdentity();
 
1618
        gluOrtho2D(0, winX, winY, 0);
 
1619
 
 
1620
        glMatrixMode(GL_MODELVIEW);
 
1621
        glPushMatrix();
 
1622
        glLoadIdentity();
 
1623
 
 
1624
        glEnable(GL_TEXTURE_3D);
 
1625
        glBindTexture(GL_TEXTURE_3D,textureId);
 
1626
        glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MULT); 
 
1627
        
 
1628
        // Draw overlay quad 
 
1629
        glColor4f(1.0f,1.0f,1.0f,alphaVal);
 
1630
        glBegin(GL_QUADS);
 
1631
                glTexCoord3f(0.0f,0.0f,texCoordZ);
 
1632
                glVertex3f(position[0]-length/2.0,position[1]-length/2.0,0.0);
 
1633
                glTexCoord3f(0.0f,1.0f,texCoordZ);
 
1634
                glVertex3f(position[0]-length/2.0,position[1]+length/2.0,0.0);
 
1635
                glTexCoord3f(1.0f,1.0f,texCoordZ);
 
1636
                glVertex3f(position[0]+length/2.0,position[1]+length/2.0,0.0);
 
1637
                glTexCoord3f(1.0f,0.0f,texCoordZ);
 
1638
                glVertex3f(position[0]+length/2.0,position[1]-length/2.0,0.0);
 
1639
        glEnd();
 
1640
 
 
1641
        glDisable(GL_TEXTURE_3D);       
 
1642
 
 
1643
        glPopMatrix(); //Pop modelview matrix
 
1644
 
 
1645
        glMatrixMode(GL_PROJECTION);
 
1646
        glPopMatrix();
 
1647
 
 
1648
        glMatrixMode(GL_MODELVIEW);
 
1649
}
 
1650
 
 
1651
void DrawAnimatedOverlay::getBoundingBox(BoundCube &b) const
 
1652
{
 
1653
        b.setInvalid();
 
1654
}
1407
1655
 
1408
1656
DrawColourBarOverlay::DrawColourBarOverlay() 
1409
1657
{
1487
1735
        //to 1 opengl unit by inversion 
1488
1736
        const float FTGL_DEFAULT_UNIT_SCALE=1.0/72.0;
1489
1737
 
1490
 
        glColor3f(1.0f,1.0f,1.0f);      
1491
1738
        font->FaceSize(3);
1492
1739
        glDisable(GL_CULL_FACE);
1493
1740
        glPushMatrix();
1627
1874
                                ptsCacheOK=true;
1628
1875
                        }
1629
1876
 
1630
 
                        if(alphaVal < 1.0f)
 
1877
                        if(alphaVal < 1.0f && useAlphaBlend)
1631
1878
                        {
1632
1879
                                //We need to generate some points, then sort them by distance
1633
1880
                                //from eye (back to front), otherwise they will not blend properly
1662
1909
                                                        ((float)(ptsCache[idx].second.v[1]))/255.0f,
1663
1910
                                                        ((float)(ptsCache[idx].second.v[2]))/255.0f, 
1664
1911
                                                        alphaVal);
1665
 
                                        glVertex3f(ptsCache[idx].first[0],ptsCache[idx].first[1],ptsCache[idx].first[2]);
 
1912
                                        glVertex3fv(ptsCache[idx].first.getValueArr());
1666
1913
                                }
1667
1914
                                glEnd();
1668
1915
                                glDepthMask(GL_TRUE);
1678
1925
                                                        ((float)(ptsCache[ui].second.v[1]))/255.0f,
1679
1926
                                                        ((float)(ptsCache[ui].second.v[2]))/255.0f, 
1680
1927
                                                        1.0f);
1681
 
                                        glVertex3f(ptsCache[ui].first[0],ptsCache[ui].first[1],ptsCache[ui].first[2]);
 
1928
                                        glVertex3fv(ptsCache[ui].first.getValueArr());
1682
1929
                                }
1683
1930
                                glEnd();
1684
1931
                        }
1693
1940
        //Draw the bounding box as required
1694
1941
        if(drawBoundBox)
1695
1942
        {
 
1943
                float alphaUse;
 
1944
 
 
1945
                if(useAlphaBlend)
 
1946
                        alphaUse=boxColourA;
 
1947
                else
 
1948
                        alphaUse=1.0f;
 
1949
                
1696
1950
                drawBox(field->getMinBounds(),field->getMaxBounds(),
1697
 
                        boxColourR, boxColourG,boxColourB,boxColourA);
 
1951
                        boxColourR, boxColourG,boxColourB,alphaUse);
1698
1952
        }
1699
1953
        //Draw the projections
1700
1954
}
1790
2044
 
1791
2045
        //This could be optimised by using triangle strips
1792
2046
        //rather than direct triangles.
1793
 
        if(a < 1.0f)
 
2047
        if(a < 1.0f && useAlphaBlend )
1794
2048
        {
1795
2049
                //We need to sort them by distance
1796
2050
                //from eye (back to front), otherwise they will not blend properly