File: screenCoordSystem.cpp

package info (click to toggle)
libqglviewer 2.2.6-3-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,944 kB
  • ctags: 3,728
  • sloc: cpp: 26,782; makefile: 66; sh: 45
file content (123 lines) | stat: -rw-r--r-- 4,004 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
/****************************************************************************

 Copyright (C) 2002-2007 Gilles Debunne (Gilles.Debunne@imag.fr)

 This file is part of the QGLViewer library.
 Version 2.2.6-3, released on August 28, 2007.

 http://artis.imag.fr/Members/Gilles.Debunne/QGLViewer

 libQGLViewer 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.

 libQGLViewer 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 libQGLViewer; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*****************************************************************************/

#include "screenCoordSystem.h"
#include <stdio.h>
#include <stdlib.h> // RAND_MAX

using namespace qglviewer;
using namespace std;

void Viewer::init()
{
  for (int i=0; i<nbSaucers; i++)
    {
      Vec pos;
      pos.x = rand() / static_cast<float>(RAND_MAX) - 0.5;
      pos.y = rand() / static_cast<float>(RAND_MAX) - 0.5;
      pos.z = rand() / static_cast<float>(RAND_MAX) - 0.5;

      Quaternion ori(Vec(static_cast<float>(rand()) / RAND_MAX,
			  static_cast<float>(rand()) / RAND_MAX,
			  static_cast<float>(rand()) / RAND_MAX),
		      rand() / static_cast<float>(RAND_MAX) * M_PI);

      saucerPos[i].setPosition(pos);
      saucerPos[i].setOrientation(ori);

      saucerColor[i].setRgb(int(255.0 * rand() / static_cast<float>(RAND_MAX)),
			    int(255.0 * rand() / static_cast<float>(RAND_MAX)),
			    int(255.0 * rand() / static_cast<float>(RAND_MAX)));
    }

  restoreStateFromFile();
  help();
}

QString Viewer::helpString() const
{
  QString text("<h2>S c r e e n C o o r d S y s t e m</h2>");
  text += "This example illustrates the <i>startScreenCoordinatesSystem()</i> function ";
  text += "which enables a GL drawing directly into the screen coordinate system.<br><br>";
  text += "The arrows are drawned using this method. The screen projection coordinates ";
  text += "of the objects is determined using <code>camera()->projectedCoordinatesOf()</code>, ";
  text += "thus <i>attaching</i> the 2D arrows to 3D objects.";
  return text;
}

void Viewer::drawSaucer() const
{
  static GLUquadric* quadric = gluNewQuadric();

  glTranslatef(0.0, 0.0, -0.014);
  gluCylinder(quadric, 0.015, 0.03, 0.004, 32, 1);
  glTranslatef(0.0, 0.0, 0.004);
  gluCylinder(quadric, 0.03, 0.04, 0.01, 32, 1);
  glTranslatef(0.0, 0.0, 0.01);
  gluCylinder(quadric, 0.05, 0.03, 0.02, 32, 1);
  glTranslatef(0.0, 0.0, 0.02);
  gluCylinder(quadric, 0.03, 0.0, 0.003, 32, 1);
  glTranslatef(0.0, 0.0, -0.02);
}

void Viewer::draw()
{
  static Vec proj[nbSaucers];

  int i;
  // Draw 3D flying saucers
  for (i=0; i<nbSaucers; i++)
    {
      glPushMatrix();
      glMultMatrixd(saucerPos[i].matrix());
      qglColor(saucerColor[i]);
      drawSaucer();
      glPopMatrix();
    }

  // Draw the arrows
  qglColor(foregroundColor());
  startScreenCoordinatesSystem();
  for (i=0; i<nbSaucers; i++)
    {
      glBegin(GL_POLYGON);
      proj[i] = camera()->projectedCoordinatesOf(saucerPos[i].position());
      // The small z offset makes the arrow slightly above the saucer, so that it is always visible
      glVertex3fv(proj[i] + Vec(-55, 0, -0.001));
      glVertex3fv(proj[i] + Vec(-17,-5, -0.001));
      glVertex3fv(proj[i] + Vec( -5, 0, -0.001));
      glVertex3fv(proj[i] + Vec(-17, 5, -0.001));
      glEnd();
    }
  stopScreenCoordinatesSystem();

  // Draw text id
  glDisable(GL_LIGHTING);
  for (i=0; i<nbSaucers; i++)
    drawText(int(proj[i].x)-60, int(proj[i].y)+4, QString::number(i));
  glEnable(GL_LIGHTING);
}