summaryrefslogtreecommitdiffstats
path: root/src/glbody.cpp
blob: b1be10f5ec2d4667581dd6be8db53ba6264a6da4 (plain)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/***************************************************************************
 *   Copyright (C) 2008, 2012 by Walter Roth                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "glbody.h"

#include <QImage>
#include <QtOpenGL/QGLWidget>
#include <QFile>

/**Constructor does NOT create the surface.
*/
GLBody::GLBody(float radius, const GLColorRgba &color, const QString textureFile)
{
  m_surfaceIsValid = false;
  this->m_textureFile = textureFile; //DO NOT call setTextureFile here! It needs a current context!
  m_texture = 0;
  this->m_color = color;
  m_specularColor = cl_White;
  m_shininess = 50;
  this->m_radius = radius;
  m_drawingMode = GL_TRIANGLES;
  m_selected = false;
  m_points = NULL;
  m_ownPointsContainer = false;
  m_indices = NULL;
  m_ownIndexContainer = false;
  m_firstPoint = 0;
  m_nextPoint = 0;
  m_firstIndex = 0;
  m_nextIndex = 0;
}

/** Destructor
 */
GLBody::~GLBody()
{
	  if(m_texture != 0){//delete an existing texture
	    glDeleteTextures(1, &m_texture);
	    m_texture = 0;
	  }
      if(m_ownPointsContainer)
          delete m_points;
      if(m_ownIndexContainer)
          delete m_indices;
}

/** Creates the surface. Should be called, when a GL engine is already running.
  * Is called automatically by draw, if required.
  */
void GLBody::makeSurface(QVector<GLPoint> *pointContainer, QVector<GLshort> * indexContainer)
{
    if(pointContainer == NULL)
    {
        m_points = new QVector<GLPoint>();
        m_ownPointsContainer = true;
    }
    else m_points = pointContainer;
    if(indexContainer == NULL)
    {
        m_indices = new QVector<GLshort>();
        m_ownIndexContainer = true;
    }
    else m_indices = indexContainer;
  setTexture(m_textureFile);
  m_surfaceIsValid = true;
}

void GLBody::calculateDrawMatrix()
{
    m_drawMatrix = m_transformationMatrix;
}

/** Draws the surface and calls makeSurface if required.
 * Needs an active (made current) GL-Context.
 */
void GLBody::draw(GLESRenderer * renderer){
  if(!m_surfaceIsValid)
    makeSurface(NULL, NULL);
  if(!m_points || m_points->size() == 0) //check for existing points
  {
      qDebug() << "GLESBody::draw  ERROR: No points!";
      return;
  }
  int stride = sizeof(GLPoint);
  bool oldTextureEnabled = false;

  //set colors
  renderer->setAmbientAndDiffuseColor(m_color);

  //enable required arrays
  renderer->activateAttributeArray(GLESRenderer::VERTEX_LOCATION,
                                   (*m_points)[0].vertexPointer(), stride);
  if (renderer->isLightingEnabled())
      renderer->activateAttributeArray(GLESRenderer::NORMAL_LOCATION,
                                      (*m_points)[0].normalPointer(), stride);
  if(m_texture != 0){
    glActiveTexture(GL_TEXTURE0); //use texture unit 0
    glBindTexture(GL_TEXTURE_2D, m_texture);
    oldTextureEnabled = renderer->isTextureEnabled();
    renderer->setTextureEnabled(true);
   }
  else renderer->setTextureEnabled(false);
  renderer->activateAttributeArray(GLESRenderer::TEXCOORD_LOCATION,
               (*m_points)[0].texCoordPointer(), stride);
  renderer->activateAttributeArray(GLESRenderer::COLOR_LOCATION,
               (*m_points)[0].colorPointer(), stride);

//   for(int i = 0; i < indices.count(); i++)
//    points[indices[i]].debugOutput("Point" +QString::number(i) + ":" + QString::number(indices[i]) + " ");

  calculateDrawMatrix();
  renderer->pushMvMatrix();
  renderer->addTransformation(m_drawMatrix);
  if(m_firstIndex >= 0 && m_nextIndex > 0)
    glDrawElements(m_drawingMode, m_nextIndex -m_firstIndex,
                   GL_UNSIGNED_SHORT, &m_indices->data()[m_firstIndex]);
  else
    glDrawArrays(m_drawingMode, m_firstPoint, m_nextPoint - m_firstPoint);

  if(isSelected())
  {
      bool lightingOn = renderer->isLightingEnabled();
      if(lightingOn)
          renderer->setLightingEnabled(false);
      GLColorRgba pointColor(m_color.inverted());
      renderer->setAmbientAndDiffuseColor(pointColor);
#ifdef GLES
      glDrawArrays(GL_LINES, 0, m_points->size());
#else
      int oldPointSize = renderer->getPointSize();
      renderer->setPointSize(5);
      glDrawArrays(GL_POINTS, 0, m_points->size());
      renderer->setPointSize(oldPointSize);
#endif
      if(lightingOn)
          renderer->setLightingEnabled(true);
  }
  //disable arrays
  renderer->disableAttributeArrays();
  if(m_texture != 0)
    renderer->setTextureEnabled(oldTextureEnabled);
  renderer->popMvMatrix();
}

/** Set texture from file. Returns true on success
 */
bool GLBody::setTexture(const QString & textureFile){
  if(textureFile == "")
    return false;

  QImage  image;
  if(m_texture != 0){//delete an existing texture
    glDeleteTextures(1, &m_texture);
    m_texture = 0;
  }

  qDebug("%s", QString("Loading Texture:" + textureFile).toLatin1().data());
  if (image.load(textureFile)){
   //we have got a valid image, give it to GL
    if (image.format() != QImage::Format_ARGB32) //make shure, that we have 32bit colors
      image = image.convertToFormat(QImage::Format_ARGB32);
    image = QGLWidget::convertToGLFormat(image);
    glGenTextures(1, &m_texture); // get a number (name) for the new texture and create a new texture object
    if(m_texture == 0) //GL does not work properly
    {
      qDebug("Could not create texture object. GL-engine not yet active?");
      return false;
    }
    glBindTexture(GL_TEXTURE_2D, m_texture); // Bind texture object to OpenGL.
    //All following calls work on the bound texture object.
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
       GL_NEAREST); //use values of nearest pixel for magnification.
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
        GL_NEAREST); // use nearest pixel minimization.
    // Replace with other values if aliasing occurs. See doc for GL_TEXTURE_MAG_FILTER

    //The next call copies the pixel data from the QImage, which is no longer needed afterwards.
    glTexImage2D(GL_TEXTURE_2D,
       0,                          // use only one resolution
       GL_RGBA,                    //internal format to use for the texture
       image.width(),
       image.height(),
       0,                          //width of the texture border, may be 0 or 1
       GL_RGBA,                    //format of the QImage
       GL_UNSIGNED_BYTE,           // QImage uses values from 0 to 255 for R,G,B,A
       image.bits()                //pointer to the first pixel in image
    );
    glBindTexture(GL_TEXTURE_2D, 0); // finally remove texture from OpenGL machine
    return true;
  }
  else{ //loading failed
    qDebug("Texture loading failed");
    return false;
  }
}

/** Set texture file.
 */
bool GLBody::setTextureFile(const QString & textureFile){
 this->m_textureFile = textureFile;
 if(m_texture != 0) //we have an activated texture already. Replace it.
  return setTexture(textureFile);
 else return true; //texture is not yet active, leave it as it is
}

/**
  * Moves the body by adding vMove to all vertices.
  */
void GLBody::move(QVector3D vMove)
{
    for(int i = m_firstPoint; i < m_nextPoint; i++)
        (*m_points)[i].move(vMove);
    m_center = m_center + vMove;
}

/**
  * Returns true, when line through p1 and p2 intersects body shpere
  */
bool GLBody::isHit(QVector3D p1, QVector3D p2)
{
   QVector3D lineVector = p2 - p1;
   double area = QVector3D::crossProduct((m_center - p1), lineVector).length();
   double distance = area / lineVector.length();
   //qDebug("Radius: %f", radius);
   return distance < m_radius;
}

/*
 * Returns true, if enclosing spheres touch or intersect
 */
bool GLBody::spheresAreColliding(const GLBody *other)
{
    bool result;
    if(this == other)
        result = false;
    else
    {
        double distance = (m_center - other->m_center).length();
        result = (m_radius + other->m_radius) >= distance;
    }
    if(result)
        return true; //hook for debugger
    else return false; //hook for debugger
}

/**
  * Set new center and invalidate surface.
  */
void GLBody::setCenter(const QVector3D & newVal)
{
    m_center = newVal;
    m_surfaceIsValid = false;
}