1 /*****************************************************************************
2  * DynaMechs: A Multibody Dynamic Simulation Library
3  *
4  * Copyright (C) 1994-2001  Scott McMillan   All Rights Reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *****************************************************************************
20  *     File: dmGLCamera.cpp
21  *   Author: Scott McMillan
22  *  Created: 22 March 1997
23  *  Summary: camera base class for viewing objects
24  *****************************************************************************/
25 
26 #include <dmGLCamera.hpp>
27 
28 //============================================================================
29 // class dmGLCamera
30 //============================================================================
31 
32 //----------------------------------------------------------------------------
33 //   Function: dmGLCamera Constructor
34 //    Summary: This will initialize a camera that will view a scene
35 //             constructed in a Performer scene graph
36 // Parameters: Performer channel this camera will be responsible for
37 //    Returns: none
38 //----------------------------------------------------------------------------
dmGLCamera()39 dmGLCamera::dmGLCamera()
40 {
41    reset();
42 }
43 
44 //----------------------------------------------------------------------------
45 //   Function: dmGLCamera Destructor
46 //    Summary:
47 // Parameters:
48 //    Returns: none
49 //----------------------------------------------------------------------------
~dmGLCamera()50 dmGLCamera::~dmGLCamera()
51 {
52 #ifdef DEBUG
53    cerr << "destructing dmGLCamera" << endl;
54 #endif
55 }
56 
57 //----------------------------------------------------------------------------
58 //   Function: reset
59 //    Summary: Reset most of the camera position, orientation, and FOV
60 //             parameters.
61 // Parameters: none
62 //    Returns: none
63 //----------------------------------------------------------------------------
reset()64 void dmGLCamera::reset()
65 {
66    for (int i=0; i<4; i++)
67    {
68       for (int j=0; j<4; j++)
69       {
70          m_view_mat[i][j] = 0.0;
71       }
72       m_view_mat[i][i] = 1.0;
73    }
74 
75    setPerspective(45.0, 1.0, 1.0, 100.0);
76 }
77 
78 
79 //----------------------------------------------------------------------------
80 //   Function: setPerspective
81 //    Summary: set field of view, and keep it in the
82 //             range [22.5, 120]
83 // Parameters: new field of view, in degrees
84 //    Returns: none
85 //----------------------------------------------------------------------------
setPerspective(GLfloat fov,GLfloat aspect,GLfloat near_clip,GLfloat far_clip)86 void dmGLCamera::setPerspective(GLfloat fov, GLfloat aspect,
87                                   GLfloat near_clip, GLfloat far_clip)
88 {
89    m_fov = fov;
90    m_aspect = aspect;
91    m_near_clip = near_clip;
92    m_far_clip = far_clip;
93 
94    applyPerspective();
95 }
96 
97 //----------------------------------------------------------------------------
98 //    Summary:
99 // Parameters:
100 //    Returns:
101 //----------------------------------------------------------------------------
applyPerspective()102 void dmGLCamera::applyPerspective()
103 {
104    glMatrixMode (GL_PROJECTION);
105    glLoadIdentity ();
106    gluPerspective(m_fov, m_aspect, m_near_clip, m_far_clip);
107 }
108 
109 //----------------------------------------------------------------------------
110 //    Summary:
111 // Parameters:
112 //    Returns:
113 //----------------------------------------------------------------------------
setViewMat(GLfloat mat[4][4])114 void dmGLCamera::setViewMat(GLfloat mat[4][4])
115 {
116    for (int i=0; i<4; i++)
117    {
118       for (int j=0; j<4; j++)
119       {
120          m_view_mat[i][j] = mat[i][j];
121       }
122    }
123 }
124 
125 //----------------------------------------------------------------------------
126 //    Summary:
127 // Parameters:
128 //    Returns:
129 //----------------------------------------------------------------------------
getViewMat(GLfloat mat[4][4])130 void dmGLCamera::getViewMat(GLfloat mat[4][4])
131 {
132    for (int i=0; i<4; i++)
133    {
134       for (int j=0; j<4; j++)
135       {
136          mat[i][j] = m_view_mat[i][j];
137       }
138    }
139 }
140 
141 //----------------------------------------------------------------------------
142 //   Function: updateView
143 //    Summary: adds an arbitrary position and orientation change to the
144 //             existing m_view_mat.
145 // Parameters: The change is specified either by another
146 //             pfMatrix, pfCoord, pos/hpr vector pair, or six floats
147 //    Returns: none
148 //----------------------------------------------------------------------------
updateViewMat(GLfloat (*)[4])149 void dmGLCamera::updateViewMat(GLfloat (*)[4])  //delta_mat[4][4])
150 {
151    cerr << "dmGLCamera::updateViewMat: not implemented" << endl;
152 
153    //m_view_mat.preMult(delta_mat);
154 }
155 
156 //----------------------------------------------------------------------------
157 //   Function: applyView
158 //    Summary: Transfer the camera position and orientation data stored in the
159 //             class to the channel.  The new view will take affect in the
160 //             Performer simulation only after this function is called
161 // Parameters: none
162 //    Returns: none
163 //----------------------------------------------------------------------------
applyView()164 void dmGLCamera::applyView()
165 {
166    glMatrixMode(GL_MODELVIEW);
167    glLoadMatrixf((GLfloat *)m_view_mat);
168 }
169