1 /*
2  *  JLib - Jacob's Library.
3  *  Copyright (C) 2003, 2004  Juan Carlos Seijo P�rez
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Library General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Library General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Library General Public
16  *  License along with this library; if not, write to the Free
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Juan Carlos Seijo P�rez
20  *  jacob@mainreactor.net
21  */
22 
23 ///////////////////////////////////////////////////////////////////////////////
24 // @author: Juan Carlos Seijo P�rez
25 // @date: 29/04/2003
26 // @description: C�mara.
27 //
28 //   ^ __>                            Vista (ejes locales de c�mara):
29 //   | up                              ^ z (yaw)
30 //   |        __________>              |
31 // /---\      eye-target         *     |
32 // | x |O---------------------->***    | x (pitch)
33 // \---/ eye                   target  o-------->
34 //                                              y (roll)
35 ///////////////////////////////////////////////////////////////////////////////
36 
37 #include <JLib/Graphics/JGLCamera.h>
38 
39 // Crea una c�mara
JGLCamera(JPoint _eye,JPoint _target,JVector _up)40 JGLCamera::JGLCamera(JPoint _eye,
41                      JPoint _target,
42                      JVector _up)
43 {
44   orgEye = _eye;
45   orgTarget = _target;
46   orgUp = _up;
47 
48   Reset();
49 }
50 
51 // Calcula los vectores base en funci�n del ojo y el objetivo
ComputeBasis()52 void JGLCamera::ComputeBasis()
53 {
54   // Usa el eje z de esta c�mara para apuntar al objetivo
55   camera.R[2] = (camera.O - target).Unit();
56 
57   // El eje x ser� perpendicular al plano formado por up y eye-target
58   camera.R[0] = camera.R[1].Cross(camera.R[2]).Unit();
59 
60   // El eje y es ortogonal a los anteriores
61   camera.R[1] = camera.R[2].Cross(camera.R[0]).Unit();
62 }
63 
64 // Modifica la posici�n de la c�mara (eye) la cantidad indicada
Translate(JVector deltaPos)65 void JGLCamera::Translate(JVector deltaPos)
66 {
67   deltaPos = camera.TransformVectorToParent(deltaPos);
68   camera.O += deltaPos;
69 
70   ComputeBasis();
71 }
72 
73 // Mueve la c�mara (eye) a la posici�n indicada
TranslateTo(JVector pos)74 void JGLCamera::TranslateTo(JVector pos)
75 {
76   camera.O = pos;
77 
78   ComputeBasis();
79 }
80 
81 // Rota la c�mara en torno al eje x pasando por el objetivo
Rotate(float x,float y,float z)82 void JGLCamera::Rotate(float x, float y, float z)
83 {
84   JScalar tLen = (camera.O - target).Length();
85   camera.O = target;
86 
87   camera.RotateAboutX(x);
88   camera.RotateAboutY(y);
89   camera.RotateAboutZ(z);
90 
91   camera.O += camera.R[2] * tLen;
92 }
93 
94 // Modifica la posici�n del objetivo la cantidad indicada
TargetTranslate(JVector deltaTarget)95 void JGLCamera::TargetTranslate(JVector deltaTarget)
96 {
97   deltaTarget = camera.TransformVectorToParent(deltaTarget);
98   target += deltaTarget;
99 
100   ComputeBasis();
101 }
102 
103 // Mueve la posici�n de objetivo a la posici�n indicada
TargetTranslateTo(JVector pos)104 void JGLCamera::TargetTranslateTo(JVector pos)
105 {
106   target = camera.TransformVectorToParent(pos);
107 
108   ComputeBasis();
109 }
110 
111 // Rota la c�mara en torno:
112 // - al eje x (Pitch, direcci�n perpendicular al plano eye-target/up)
113 // - al eje y (Yaw, direcci�n up)
114 // - al eje z (Roll, direcci�n eye-target)
TargetRotate(float x,float y,float z)115 void JGLCamera::TargetRotate(float x, float y, float z)
116 {
117   float tLen = (camera.O - target).Length();
118   target = camera.O;
119 
120   camera.RotateAboutX(x);                           // Giramos la cantidad indicada
121   camera.RotateAboutY(y);
122   camera.RotateAboutZ(z);
123 
124   target -= camera.R[2] * tLen;    // Desplazamos la longitud original en la nueva direcci�n
125 }
126 
127 // Zoom. Especifica la distancia eye-target
Zoom(float distance)128 void JGLCamera::Zoom(float distance)
129 {
130   if (distance <= 0)
131     return;
132 
133   camera.O = target;
134   camera.O -= (camera.R[2] * distance);
135 
136   ComputeBasis();
137 }
138 
139 // Ajusta la perspectiva (m�s o menos distorsi�n)
Perspective(float distortAmount)140 void JGLCamera::Perspective(float distortAmount)
141 {
142 }
143 
144 // Establece la proyecci�n de c�mara
Set()145 void JGLCamera::Set()
146 {
147   glLoadIdentity();
148   gluLookAt(camera.O.x, camera.O.y, camera.O.z,
149             target.x, target.y, target.z,
150             camera.Y().x, camera.Y().y, camera.Y().z);
151 }
152 
153 // Reset de c�mara
Reset()154 void JGLCamera::Reset()
155 {
156   camera.O = orgEye;
157   target = orgTarget;
158 
159   // Inicializamos el eje Y de la base al vector up que nos dan
160   camera.R[1] = orgUp;
161 
162   ComputeBasis();
163 }
164