File: sphere.cpp

package info (click to toggle)
tulip 3.7.0dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 39,428 kB
  • sloc: cpp: 231,403; php: 11,023; python: 1,128; sh: 671; yacc: 522; makefile: 315; xml: 63; lex: 55
file content (251 lines) | stat: -rwxr-xr-x 9,259 bytes parent folder | download
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
 *
 * This file is part of Tulip (www.tulip-software.org)
 *
 * Authors: David Auber and the Tulip development Team
 * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
 *
 * Tulip is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Tulip is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 */
#include <GL/glew.h>

#include <tulip/OpenGlConfigManager.h>

#include <iostream>

#include <tulip/StringProperty.h>
#include <tulip/ColorProperty.h>
#include <tulip/GlDisplayListManager.h>
#include <tulip/GlTextureManager.h>

#include <tulip/Graph.h>
#include <tulip/Glyph.h>
#include <tulip/EdgeExtremityGlyph.h>
#include <tulip/GlTools.h>

using namespace std;
using namespace tlp;

/** \addtogroup glyph */
/*@{*/
/// A 3D glyph.
/**
 * This glyph draws a textured sphere using the "viewTexture" node
 * property value. If this property has no value, the sphere
 * is then colored using the "viewColor" node property value.
 */
class Sphere: public Glyph, public EdgeExtremityGlyphFrom3DGlyph {
public:
  Sphere(GlyphContext *gc = NULL);
  Sphere(EdgeExtremityGlyphContext *gc);
  virtual ~Sphere();
  virtual void getIncludeBoundingBox(BoundingBox &boundingBox,node);
  virtual void draw(node n, float lod);
  virtual void draw(edge e, node n, const Color& glyphColor, const Color &borderColor, float lod);

private:
  void generateBuffers(int space);
  inline void drawGlyph(const Color& glyphColor, const string& texture,
                        const string& texturePath, float lod);
};

GLYPHPLUGIN(Sphere, "3D - Sphere", "Bertrand Mathieu", "09/07/2002", "Textured sphere", "1.0", 2)
EEGLYPHPLUGIN(Sphere, "3D - Sphere", "Bertrand Mathieu", "09/07/2002", "Textured sphere", "1.0", 2)

//=========================================================================================
Sphere::Sphere(GlyphContext *gc) :
  Glyph(gc), EdgeExtremityGlyphFrom3DGlyph(NULL) {
}
Sphere::Sphere(EdgeExtremityGlyphContext *gc) :
  Glyph(NULL), EdgeExtremityGlyphFrom3DGlyph(gc) {
}

Sphere::~Sphere() {
}
//=====================================================
void Sphere::getIncludeBoundingBox(BoundingBox &boundingBox,node) {
  boundingBox[0] = Coord(-0.35f, -0.35f, -0.35f);
  boundingBox[1] = Coord(0.35f, 0.35f, 0.35f);
}

static GLuint buffers[] = { 0, 0, 0 };

static GLfloat *vertex;
static GLfloat *texturesCoord;
static GLushort *indices;
//=====================================================
void Sphere::draw(node n, float lod) {
  drawGlyph(glGraphInputData->getElementColor()->getNodeValue(n),
            glGraphInputData->getElementTexture()->getNodeValue(n),
            glGraphInputData->parameters->getTexturePath(), lod);
}

void Sphere::draw(edge e, node, const Color& glyphColor, const Color&, float lod) {
  glEnable(GL_LIGHTING);
  drawGlyph(glyphColor,
            edgeExtGlGraphInputData->getElementTexture()->getEdgeValue(e),
            edgeExtGlGraphInputData->parameters->getTexturePath(), lod);
}

//=====================================================
void Sphere::generateBuffers(int space) {
  int vertexCount = (90 / space) * (360 / space) * 4;

  glGenBuffers(3, buffers);

  double PI = 3.1415926535897;
  vertex = new GLfloat[vertexCount * 3* 2 ];
  texturesCoord= new GLfloat[vertexCount*2*2];
  indices = new GLushort[vertexCount*2*2];

  int n;

  n = 0;

  for(float j = 0; j <= 90 - space; j+=space) {
    for(float i = 0; i <= 360 - space; i+=space) {
      indices[n]=n;
      indices[n+1]=n+1;
      indices[n+2]=n+2;
      indices[n+3]=n+3;
      indices[vertexCount*2-n]=n+vertexCount;
      indices[vertexCount*2-n-1]=n+vertexCount+1;
      indices[vertexCount*2-n-2]=n+vertexCount+2;
      indices[vertexCount*2-n-3]=n+vertexCount+3;

      vertex[n*3] = static_cast<float>(sin((i) / 180 * PI) * sin((j) / 180 * PI) /2.);
      vertex[n*3+1] = static_cast<float>(cos((i) / 180 * PI) * sin((j) / 180 * PI) /2.);
      vertex[n*3+2] = static_cast<float>(-cos((j) / 180 * PI) /2.);
      vertex[(vertexCount+n)*3]=vertex[n*3];
      vertex[(vertexCount+n)*3+1]=vertex[n*3+1];
      vertex[(vertexCount+n)*3+2]=-vertex[n*3+2];
      texturesCoord[n*2] = 1-((i) / 360);
      texturesCoord[n*2+1] = (2 * j) / 360;
      texturesCoord[(vertexCount+n)*2] = texturesCoord[n*2];
      texturesCoord[(vertexCount+n)*2+1] = -texturesCoord[n*2+1];
      n++;

      vertex[n*3] = static_cast<float>(sin((i) / 180 * PI) * sin((j + space) / 180 * PI) /2.);
      vertex[n*3+1] = static_cast<float>(cos((i) / 180 * PI) * sin((j + space) / 180 * PI) /2.);
      vertex[n*3+2] = static_cast<float>(-cos((j + space) / 180 * PI) /2.);
      vertex[(vertexCount+n)*3]=vertex[n*3];
      vertex[(vertexCount+n)*3+1]=vertex[n*3+1];
      vertex[(vertexCount+n)*3+2]=-vertex[n*3+2];
      texturesCoord[n*2] = 1-(i) / 360;
      texturesCoord[n*2+1] = (2 * (j + space)) / 360;
      texturesCoord[(vertexCount+n)*2] = texturesCoord[n*2];
      texturesCoord[(vertexCount+n)*2+1] = -texturesCoord[n*2+1];
      n++;

      vertex[n*3] = static_cast<float>(sin((i + space) / 180 * PI) * sin((j) / 180 * PI) /2.);
      vertex[n*3+1] = static_cast<float>(cos((i + space) / 180 * PI) * sin((j) / 180 * PI) /2.);
      vertex[n*3+2] = static_cast<float>(-cos((j) / 180 * PI) /2.);
      vertex[(vertexCount+n)*3]=vertex[n*3];
      vertex[(vertexCount+n)*3+1]=vertex[n*3+1];
      vertex[(vertexCount+n)*3+2]=-vertex[n*3+2];
      texturesCoord[n*2] = 1-(i + space) / 360;
      texturesCoord[n*2+1] = (2 * j) / 360;
      texturesCoord[(vertexCount+n)*2] = texturesCoord[n*2];
      texturesCoord[(vertexCount+n)*2+1] = -texturesCoord[n*2+1];
      n++;

      vertex[n*3] = static_cast<float>(sin((i + space) / 180 * PI) * sin((j + space) / 180 * PI) /2.);
      vertex[n*3+1] = static_cast<float>(cos((i + space) / 180 * PI) * sin((j + space) / 180 * PI) /2.);
      vertex[n*3+2] = static_cast<float>(-cos((j + space) / 180 * PI) /2.);
      vertex[(vertexCount+n)*3]=vertex[n*3];
      vertex[(vertexCount+n)*3+1]=vertex[n*3+1];
      vertex[(vertexCount+n)*3+2]=-vertex[n*3+2];
      texturesCoord[n*2] = 1-(i + space) / 360;
      texturesCoord[n*2+1] = (2 * (j + space)) / 360;
      texturesCoord[(vertexCount+n)*2] = texturesCoord[n*2];
      texturesCoord[(vertexCount+n)*2+1] = -texturesCoord[n*2+1];
      n++;
    }
  }

  indices[vertexCount]=vertexCount*2-1;

  glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
  glBufferData(GL_ARRAY_BUFFER, vertexCount*3*2*sizeof(GLfloat),vertex, GL_STATIC_DRAW);
  glBindBuffer(GL_ARRAY_BUFFER, buffers[1]);
  glBufferData(GL_ARRAY_BUFFER, vertexCount*2*2*sizeof(GLfloat),texturesCoord, GL_STATIC_DRAW);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[2]);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertexCount*2*sizeof(GLushort), indices, GL_STATIC_DRAW);
}
/*@}*/
void Sphere::drawGlyph(const Color& glyphColor, const string& texture,
                       const string& texturePath, float) {
  bool canUseGlew = OpenGlConfigManager::getInst().canUseGlew();

  int space = 9;
  int vertexCount = (90 / space) * (360 / space) * 4;

  if (canUseGlew) {
    if (buffers[0] == 0) {
      generateBuffers(space);
    }
  }
  else {
    if (GlDisplayListManager::getInst().beginNewDisplayList("Sphere_sphere")) {
      GLUquadricObj *quadratic;
      quadratic = gluNewQuadric();
      gluQuadricNormals(quadratic, GLU_SMOOTH);
      gluQuadricTexture(quadratic, GL_TRUE);
      gluSphere(quadratic, 0.5f, 30, 30);
      GlDisplayListManager::getInst().endNewDisplayList();
      gluDeleteQuadric(quadratic);
    }
  }

  tlp::setMaterial(glyphColor);

  if (texture.size() != 0) {
    GlTextureManager::getInst().activateTexture(texturePath + texture);
  }

  OpenGlConfigManager::getInst().activatePolygonAntiAliasing();

  if (canUseGlew) {
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
    glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
    glNormalPointer(GL_FLOAT, 0, BUFFER_OFFSET(0));

    if (texture.size() != 0) {
      glEnableClientState(GL_TEXTURE_COORD_ARRAY);
      glBindBuffer(GL_ARRAY_BUFFER, buffers[1]);
      glTexCoordPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));
    }

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[2]);
    glDrawElements(GL_TRIANGLE_STRIP, vertexCount, GL_UNSIGNED_SHORT,BUFFER_OFFSET(0));
    glDrawElements(GL_TRIANGLE_STRIP, vertexCount, GL_UNSIGNED_SHORT,BUFFER_OFFSET(vertexCount*sizeof(GLushort)));

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);

    if (texture.size() != 0)
      glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  }
  else {
    GlDisplayListManager::getInst().callDisplayList("Sphere_sphere");
  }

  OpenGlConfigManager::getInst().desactivatePolygonAntiAliasing();

  GlTextureManager::getInst().desactivateTexture();
}