1 /****************************************************************************
2 
3  Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.7.2.
6 
7  http://www.libqglviewer.com - contact@libqglviewer.com
8 
9  This file may be used under the terms of the GNU General Public License
10  versions 2.0 or 3.0 as published by the Free Software Foundation and
11  appearing in the LICENSE file included in the packaging of this file.
12  In addition, as a special exception, Gilles Debunne gives you certain
13  additional rights, described in the file GPL_EXCEPTION in this package.
14 
15  libQGLViewer uses dual licensing. Commercial/proprietary software must
16  purchase a libQGLViewer Commercial License.
17 
18  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 
21 *****************************************************************************/
22 
23 #include "cameraLight.h"
24 
25 using namespace std;
26 using namespace qglviewer;
27 
draw()28 void Viewer::draw() {
29   // Place light at camera position
30   const Vec cameraPos = camera()->position();
31   const GLfloat pos[4] = {(float)cameraPos[0], (float)cameraPos[1],
32                           (float)cameraPos[2], 1.0f};
33   glLightfv(GL_LIGHT1, GL_POSITION, pos);
34 
35   // Orientate light along view direction
36   glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, camera()->viewDirection());
37 
38   drawSpiral();
39 }
40 
41 // Draws a spiral
drawSpiral()42 void Viewer::drawSpiral() {
43   const float nbSteps = 1000.0;
44 
45   glBegin(GL_QUAD_STRIP);
46   for (int i = 0; i < nbSteps; ++i) {
47     const float ratio = i / nbSteps;
48     const float angle = 21.0 * ratio;
49     const float c = cos(angle);
50     const float s = sin(angle);
51     const float r1 = 1.0 - 0.8f * ratio;
52     const float r2 = 0.8f - 0.8f * ratio;
53     const float alt = ratio - 0.5f;
54     const float nor = 0.5f;
55     const float up = sqrt(1.0 - nor * nor);
56     glColor3f(1.0 - ratio, 0.2f, ratio);
57     glNormal3f(nor * c, up, nor * s);
58     glVertex3f(r1 * c, alt, r1 * s);
59     glVertex3f(r2 * c, alt + 0.05f, r2 * s);
60   }
61   glEnd();
62 }
63 
init()64 void Viewer::init() {
65   // Light setup
66   glDisable(GL_LIGHT0);
67   glEnable(GL_LIGHT1);
68 
69   // Light default parameters
70   const GLfloat light_ambient[4] = {1.0, 1.0, 1.0, 1.0};
71   const GLfloat light_specular[4] = {1.0, 1.0, 1.0, 1.0};
72   const GLfloat light_diffuse[4] = {1.0, 1.0, 1.0, 1.0};
73 
74   glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 3.0);
75   glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 10.0);
76   glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0.1f);
77   glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.3f);
78   glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.3f);
79   glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
80   glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
81   glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
82 
83   // Restore previous viewer state.
84   restoreStateFromFile();
85 
86   // Opens help window
87   help();
88 }
89 
helpString() const90 QString Viewer::helpString() const {
91   QString text("<h2>C a m e r a L i g h t</h2>");
92   text += "See the <b>Mouse</b> tab and the documentation web pages for "
93           "details.<br><br>";
94   text += "Press <b>Escape</b> to exit the viewer.";
95   return text;
96 }
97