GLshape.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <deque>
3 #ifdef __APPLE__
4 #include <OpenGL/glu.h>
5 #else
6 #include <GL/glu.h>
7 #endif
8 #include "GLutil.h"
9 #include "GLlink.h"
10 #include "GLshape.h"
11 #include "GLtexture.h"
12 
13 GLshape::GLshape() : m_shininess(0.2), m_texture(NULL), m_requestCompile(false), m_shadingList(0), m_wireFrameList(0), m_highlight(false)
14 {
15  for (int i=0; i<16; i++) m_trans[i] = 0.0;
16  m_trans[0] = m_trans[5] = m_trans[10] = m_trans[15] = 1.0;
17  for (int i=0; i<4; i++) m_specular[i] = 0;
18 }
19 
21 {
22  if (m_texture){
23  if (m_texture->image.size()) glDeleteTextures(1, &m_textureId);
24  delete m_texture;
25  }
26  if (m_shadingList) glDeleteLists(m_shadingList, 1);
27  if (m_wireFrameList) glDeleteLists(m_wireFrameList, 1);
28 }
29 
30 size_t GLshape::draw(int i_mode)
31 {
32  glPushMatrix();
33  glMultMatrixd(m_trans);
34  if (m_requestCompile){
35  m_shadingList = doCompile(false);
36  m_wireFrameList = doCompile(true);
37  m_requestCompile = false;
38  }
39  glCallList(i_mode == GLlink::DM_SOLID ? m_shadingList : m_wireFrameList);
40  glPopMatrix();
41  return m_triangles.size();
42 }
43 
44 void GLshape::setVertices(unsigned int nvertices, const float *vertices)
45 {
46  m_vertices.resize(nvertices);
47  for (size_t i=0; i<nvertices; i++){
48  m_vertices[i] = Eigen::Vector3f(vertices[i*3 ],
49  vertices[i*3+1],
50  vertices[i*3+2]);
51  }
52 }
53 
54 void GLshape::setTriangles(unsigned int ntriangles, const int *vertexIndices)
55 {
56  m_triangles.resize(ntriangles);
57  for (size_t i=0; i<ntriangles; i++){
58  m_triangles[i] = Eigen::Vector3i(vertexIndices[i*3 ],
59  vertexIndices[i*3+1],
60  vertexIndices[i*3+2]);
61  }
62 }
63 
64 void GLshape::setNormals(unsigned int nnormal, const float *normals)
65 {
66  m_normals.resize(nnormal);
67  for (size_t i=0; i<nnormal; i++){
68  m_normals[i] = Eigen::Vector3f(normals[i*3 ],
69  normals[i*3+1],
70  normals[i*3+2]);
71  }
72 }
73 
74 void GLshape::setDiffuseColor(float r, float g, float b, float a)
75 {
76  m_diffuse[0] = r; m_diffuse[1] = g; m_diffuse[2] = b; m_diffuse[3] = a;
77 }
78 
79 void GLshape::setColors(unsigned int ncolors, const float *colors)
80 {
81  m_colors.resize(ncolors);
82  for (size_t i=0; i<ncolors; i++){
83  m_colors[i] = Eigen::Vector3f(colors[i*3 ],
84  colors[i*3+1],
85  colors[i*3+2]);
86  }
87 }
88 
89 void GLshape::normalPerVertex(bool flag)
90 {
91  m_normalPerVertex = flag;
92 }
93 
94 void GLshape::solid(bool flag)
95 {
96  m_solid = flag;
97 }
98 
99 void GLshape::setNormalIndices(unsigned int len, const int *normalIndices)
100 {
101  m_normalIndices.resize(len);
102  for (size_t i=0; i<len; i++){
103  m_normalIndices[i] = normalIndices[i];
104  }
105 }
106 
107 void GLshape::setTextureCoordinates(unsigned int ncoords, const float *coordinates)
108 {
109  m_textureCoordinates.resize(ncoords);
110  for (size_t i=0; i<ncoords; i++){
111  m_textureCoordinates[i] = Eigen::Vector2f(coordinates[i*2 ],
112  coordinates[i*2+1]);
113  }
114 }
115 
116 void GLshape::setTextureCoordIndices(unsigned int len, const int *coordIndices)
117 {
118  m_textureCoordIndices.resize(len);
119  for (size_t i=0; i<len; i++){
120  m_textureCoordIndices[i] = coordIndices[i];
121  }
122 }
123 
125 {
126  m_texture = texture;
127 }
128 
130 {
131  m_requestCompile = true;
132 }
133 
134 int GLshape::doCompile(bool isWireFrameMode)
135 {
136  if (isWireFrameMode){
137  if (m_wireFrameList) glDeleteLists(m_wireFrameList, 1);
138  }else{
139  if (m_shadingList) glDeleteLists(m_shadingList, 1);
140  }
141 
142  //std::cout << "doCompile" << std::endl;
143  int list = glGenLists(1);
144  glNewList(list, GL_COMPILE);
145 
146  if (m_solid){
147  glEnable(GL_CULL_FACE);
148  }else{
149  glDisable(GL_CULL_FACE);
150  }
151  double scale[3];
152  for (int i=0; i<3; i++){
153  scale[i] = sqrt(m_trans[i]*m_trans[i]
154  +m_trans[i+4]*m_trans[i+4]
155  +m_trans[i+8]*m_trans[i+8]);
156  }
157 
158  bool drawTexture = false;
159  if (!isWireFrameMode && m_texture && !m_highlight){
160  drawTexture = true;
161  glGenTextures(1, &m_textureId);
162  glBindTexture(GL_TEXTURE_2D, m_textureId);
163 
164  if (m_texture->repeatS){
165  glTexParameteri(GL_TEXTURE_2D,
166  GL_TEXTURE_WRAP_S, GL_REPEAT);
167  }else{
168  glTexParameteri(GL_TEXTURE_2D,
169  GL_TEXTURE_WRAP_S, GL_CLAMP);
170  }
171  if (m_texture->repeatT){
172  glTexParameteri(GL_TEXTURE_2D,
173  GL_TEXTURE_WRAP_T, GL_REPEAT);
174  }else{
175  glTexParameteri(GL_TEXTURE_2D,
176  GL_TEXTURE_WRAP_T, GL_CLAMP);
177  }
178  int format;
179  if (m_texture->numComponents == 3){
180  format = GL_RGB;
181  }else if (m_texture->numComponents == 4){
182  format = GL_RGBA;
183  }
184  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
185  gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
187  format, GL_UNSIGNED_BYTE,
188  &m_texture->image[0]);
189 
190  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
191  GL_LINEAR_MIPMAP_LINEAR);
192  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
193  GL_LINEAR);
194  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
195 
196  glEnable(GL_TEXTURE_2D);
197  }
198 
199  if (!isWireFrameMode) glBegin(GL_TRIANGLES);
200  if (m_highlight){
201  float red[] = {1,0,0,1};
202  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
203  }else{
204  glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, m_diffuse);
205  //glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, m_specular);
206  glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, m_shininess);
207  }
208  for(size_t j=0; j < m_triangles.size(); ++j){
209  if (isWireFrameMode) glBegin(GL_LINE_LOOP);
210  if (!m_normalPerVertex){
211  unsigned int p;
212  if (m_normalIndices.size() == 0){
213  p = j;
214  }else{
215  p = m_normalIndices[j];
216  }
217  if (p < m_normals.size()){
218  const Eigen::Vector3f &n = m_normals[p];
219  glNormal3f(scale[0]*n[0], scale[1]*n[1], scale[2]*n[2]);
220  }
221  }
222  for(int k=0; k < 3; ++k){
223  long vertexIndex = m_triangles[j][k];
224  if (m_normalPerVertex){
225  int p;
226  if (m_normalIndices.size() == 0){
227  p = vertexIndex;
228  }else{
229  p = m_normalIndices[j*3+k];
230  }
231  const Eigen::Vector3f &n = m_normals[p];
232  glNormal3f(scale[0]*n[0], scale[1]*n[1], scale[2]*n[2]);
233  }
234  if (drawTexture){
235  int texCoordIndex = m_textureCoordIndices[j*3+k];
236  glTexCoord2d(m_textureCoordinates[texCoordIndex][0],
237  -m_textureCoordinates[texCoordIndex][1]);
238  }
239  glVertex3fv(m_vertices[vertexIndex].data());
240  }
241  if (isWireFrameMode) glEnd(); // GL_LINE_LOOP
242  }
243  if (!isWireFrameMode) glEnd(); // GL_TRIANGLES
244  if (drawTexture) glDisable(GL_TEXTURE_2D);
245 
246  // point cloud
247  if (!m_triangles.size()&&m_vertices.size()){
248  glPointSize(3);
249  glDisable(GL_LIGHTING);
250  glBegin(GL_POINTS);
251  for (size_t i=0; i<m_vertices.size(); i++){
252  if (m_colors.size()>=m_vertices.size())
253  glColor3fv(m_colors[i].data());
254  glVertex3fv(m_vertices[i].data());
255  }
256  glEnd();
257  glEnable(GL_LIGHTING);
258  }
259  glEndList();
260 
261  return list;
262 }
263 
265 {
266  m_shininess = s;
267 }
268 
269 void GLshape::setSpecularColor(float r, float g, float b)
270 {
271  m_specular[0] = r; m_specular[1] = g; m_specular[2] = b;
272 }
273 
274 
275 void GLshape::highlight(bool flag)
276 {
277  if (m_highlight != flag) compile();
278  m_highlight = flag;
279 }
280 
281 void GLshape::divideLargeTriangles(double maxEdgeLen)
282 {
283  std::vector<Eigen::Vector3i> new_triangles;
284  std::vector<int> new_normalIndices, new_textureCoordIndices;
285 
286  double scale[3];
287  for (int i=0; i<3; i++){
288  scale[i] = sqrt(m_trans[i]*m_trans[i]
289  +m_trans[i+4]*m_trans[i+4]
290  +m_trans[i+8]*m_trans[i+8]);
291  }
292  //std::cout << "normal per vertex:" << m_normalPerVertex << std::endl;
293  for (size_t i=0; i<m_triangles.size(); i++){
294  std::deque<Eigen::Vector3i> dq_triangles;
295  dq_triangles.push_back(m_triangles[i]);
296  std::deque<Eigen::Vector3i> dq_normals;
297  Eigen::Vector3i n;
298  if (m_normalPerVertex){
299  for (int j=0; j<3; j++){
300  if (m_normalIndices.size() == 0){
301  n[j] = m_triangles[i][j];
302  }else{
303  n[j] = m_normalIndices[i*3+j];
304  }
305  }
306  }else{
307  if (m_normalIndices.size() == 0){
308  n[0] = i;
309  }else{
310  n[0] = m_normalIndices[i];
311  }
312  }
313  dq_normals.push_back(n);
314  std::deque<Eigen::Vector3i> dq_textureCoordIndices;
315  if (m_texture){
316  dq_textureCoordIndices.push_back(
317  Eigen::Vector3i(m_textureCoordIndices[i*3],
318  m_textureCoordIndices[i*3+1],
319  m_textureCoordIndices[i*3+2]));
320  }
321  while(dq_triangles.size()){
322  Eigen::Vector3i tri = dq_triangles.front();
323  dq_triangles.pop_front();
324  Eigen::Vector3i n = dq_normals.front();
325  dq_normals.pop_front();
326  Eigen::Vector3i tc;
327  if (m_texture){
328  tc = dq_textureCoordIndices.front();
329  dq_textureCoordIndices.pop_front();
330  }
331  //
332  double l[3];
333  for (int j=0; j<3; j++){
334  Eigen::Vector3f e = m_vertices[tri[j]] - m_vertices[tri[(j+1)%3]];
335  for (int k=0; k<3; k++) e[k] *= scale[k];
336  l[j] = e.norm();
337  }
338  int maxidx;
339  if (l[0] > l[1]){
340  maxidx = l[0] > l[2] ? 0 : 2;
341  }else{
342  maxidx = l[1] > l[2] ? 1 : 2;
343  }
344  if (l[maxidx] <= maxEdgeLen){
345  new_triangles.push_back(tri);
346  if (m_normalPerVertex){
347  for (int j=0; j<3; j++) new_normalIndices.push_back(n[j]);
348  }else{
349  new_normalIndices.push_back(n[0]);
350  }
351  for (int j=0; j<3; j++) new_textureCoordIndices.push_back(tc[j]);
352  }else{
353  int vnew = m_vertices.size();
354  m_vertices.push_back(
355  (m_vertices[tri[maxidx]]+m_vertices[tri[(maxidx+1)%3]])/2);
356  dq_triangles.push_back(
357  Eigen::Vector3i(tri[maxidx], vnew, tri[(maxidx+2)%3]));
358  dq_triangles.push_back(
359  Eigen::Vector3i(vnew,tri[(maxidx+1)%3],tri[(maxidx+2)%3]));
360  if (m_normalPerVertex){
361  int nnew = m_normals.size();
362  m_normals.push_back(
363  (m_normals[n[maxidx]]+m_normals[n[(maxidx+1)%3]])/2);
364  dq_normals.push_back(
365  Eigen::Vector3i(n[maxidx], nnew, n[(maxidx+2)%3]));
366  dq_normals.push_back(
367  Eigen::Vector3i(nnew,n[(maxidx+1)%3],n[(maxidx+2)%3]));
368  }else{
369  dq_normals.push_back(n);
370  dq_normals.push_back(n);
371  }
372  if (m_texture){
373  int tcnew = m_textureCoordinates.size();
374  m_textureCoordinates.push_back(
375  (m_textureCoordinates[tc[maxidx]]
376  +m_textureCoordinates[tc[(maxidx+1)%3]])/2);
377  dq_textureCoordIndices.push_back(
378  Eigen::Vector3i(tc[maxidx], tcnew, tc[(maxidx+2)%3]));
379  dq_textureCoordIndices.push_back(
380  Eigen::Vector3i(tcnew, tc[(maxidx+1)%3], tc[(maxidx+2)%3]));
381  }
382  }
383  }
384  }
385 
386  m_triangles = new_triangles;
387  m_normalIndices = new_normalIndices;
388  m_textureCoordIndices = new_textureCoordIndices;
389 }
390 
391 void GLshape::computeAABB(const hrp::Vector3& i_p, const hrp::Matrix33& i_R,
392  hrp::Vector3& o_min, hrp::Vector3& o_max)
393 {
394  hrp::Vector3 relP = getPosition();
395  hrp::Matrix33 relR;
396  getRotation(relR);
397  hrp::Vector3 p = i_p + i_R*relP;
398  hrp::Matrix33 R = i_R*relR;
399  hrp::Vector3 v;
400  for (size_t i=0; i<m_vertices.size(); i++){
401  v = R*hrp::Vector3(m_vertices[i][0],m_vertices[i][1],m_vertices[i][2]);
402  if (i==0){
403  o_min = v; o_max = v;
404  }else{
405  for (int j=0; j<3; j++){
406  if (o_min[j] > v[j]) o_min[j] = v[j];
407  if (o_max[j] < v[j]) o_max[j] = v[j];
408  }
409  }
410  }
411  o_min += p;
412  o_max += p;
413 }
std::vector< int > m_normalIndices
Definition: GLshape.h:47
int doCompile(bool isWireFrameMode)
Definition: GLshape.cpp:134
void compile()
Definition: GLshape.cpp:129
void divideLargeTriangles(double maxEdgeLen)
Definition: GLshape.cpp:281
void setNormals(unsigned int nnormal, const float *normals)
Definition: GLshape.cpp:64
void computeAABB(const hrp::Vector3 &i_p, const hrp::Matrix33 &i_R, hrp::Vector3 &o_min, hrp::Vector3 &o_max)
Definition: GLshape.cpp:391
void setTexture(GLtexture *texture)
Definition: GLshape.cpp:124
std::vector< Eigen::Vector3f > m_vertices
Definition: GLshape.h:44
void setTextureCoordinates(unsigned int len, const float *coordinates)
Definition: GLshape.cpp:107
void setTextureCoordIndices(unsigned int len, const int *coordinates)
Definition: GLshape.cpp:116
bool repeatS
Definition: GLtexture.h:9
void setSpecularColor(float r, float g, float b)
Definition: GLshape.cpp:269
int height
Definition: GLtexture.h:8
int m_wireFrameList
Definition: GLshape.h:53
void setVertices(unsigned int nvertices, const float *vertices)
Definition: GLshape.cpp:44
bool m_highlight
Definition: GLshape.h:55
png_uint_32 i
hrp::Matrix33 getRotation()
std::vector< Eigen::Vector3f > m_colors
Definition: GLshape.h:44
GLshape()
Definition: GLshape.cpp:13
Eigen::Vector3d Vector3
hrp::Vector3 getPosition()
Eigen::Matrix3d Matrix33
void solid(bool flag)
Definition: GLshape.cpp:94
std::vector< Eigen::Vector3i > m_triangles
Definition: GLshape.h:46
int numComponents
Definition: GLtexture.h:7
int width
Definition: GLtexture.h:8
void setShininess(float s)
Definition: GLshape.cpp:264
std::vector< int > m_textureCoordIndices
Definition: GLshape.h:47
void red()
std::vector< Eigen::Vector2f, Eigen::aligned_allocator< Eigen::Vector2f > > m_textureCoordinates
Definition: GLshape.h:45
~GLshape()
Definition: GLshape.cpp:20
def j(str, encoding="cp932")
int m_shadingList
Definition: GLshape.h:53
void setColors(unsigned int ncolors, const float *colors)
Definition: GLshape.cpp:79
std::vector< Eigen::Vector3f > m_normals
Definition: GLshape.h:44
n
void setTriangles(unsigned int ntriangles, const int *vertexIndices)
Definition: GLshape.cpp:54
void setDiffuseColor(float r, float g, float b, float a)
Definition: GLshape.cpp:74
GLuint m_textureId
Definition: GLshape.h:54
float m_shininess
Definition: GLshape.h:48
float m_specular[4]
Definition: GLshape.h:48
double m_trans[16]
Definition: GLcoordinates.h:26
GLtexture * m_texture
Definition: GLshape.h:51
bool m_normalPerVertex
Definition: GLshape.h:49
void highlight(bool flag)
Definition: GLshape.cpp:275
JSAMPIMAGE data
void setNormalIndices(unsigned int len, const int *normalIndices)
Definition: GLshape.cpp:99
bool repeatT
Definition: GLtexture.h:9
std::vector< unsigned char > image
Definition: GLtexture.h:10
size_t draw(int i_mode)
Definition: GLshape.cpp:30
bool m_solid
Definition: GLshape.h:50
void normalPerVertex(bool flag)
Definition: GLshape.cpp:89
float m_diffuse[4]
Definition: GLshape.h:48
bool m_requestCompile
Definition: GLshape.h:52


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:50