summaryrefslogtreecommitdiffstats
path: root/headtrack_stereo_demo/src/glutcallbacks.cpp
blob: 6002a853628f04f0b89df0f6d9cf46fb7444c315 (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
#include <wiimote.h>
#include <GL\glew.h>
#include <GL\glut.h>
#include <stdlib.h>

#include "typedefs.h"
#include "stereoheadtrackfrustum.h"
#include "scenerenderer.h"

extern GameState_t g_GameState;

void ExitProgram()
{
  printf("Finished!\n");


  //we prolly should do some cleanup
  if (g_GameState.m_pTrackingWiimote != NULL)
  {
    g_GameState.m_pTrackingWiimote->Disconnect();
    delete g_GameState.m_pTrackingWiimote;
  }

  exit(0);
}

inline void GSConvScaleChange(int index, GLfloat delta)
{
  g_GameState.m_GSConvScale[index] += delta;
  if (g_GameState.m_bStereoEnabled) 
    glUniform3fv(g_GameState.m_GSConvScaleLoc, 1, g_GameState.m_GSConvScale);
}

void Keyboard(unsigned char f_cKey, int f_iX, int f_iY)
{
  switch(f_cKey)
  {
  case 'q':
  case 'Q':
  case 27 :
    ExitProgram();
    break;

  case 's':
  case 'S': //enable /disable stereo vision
    g_GameState.m_bStereoEnabled = !g_GameState.m_bStereoEnabled;
    break;

  case 'h':
  case 'H': //enable disable head tracking
    if (g_GameState.m_pTrackingWiimote != NULL && g_GameState.m_pTrackingWiimote->IsConnected())
    {
      g_GameState.m_bHeadTrackingEnabled = !g_GameState.m_bHeadTrackingEnabled;
    }
    break;

  case ' ':
    ////set y angle correction to current y angle of the the center of the two ir dots
    //if (g_Wiimote.IsConnected())
    //{
    //  wiimote_state::ir::dot l_Dot[2];  //shorthand to the ir dots
    //  //check if we see at least 2 IR dots
    //  if (FindIRDots(&g_Wiimote.IR, l_Dot))
    //  {      
    //    //calculate the distance of the head (in mm)
    //    double  l_dHeadDistInMM = CalcHeadDistInMM(l_Dot, &g_GameState.m_HeadTrackParms), //the distance between the head and camera (in mm)
    //            l_dEyeYCam = (double)(l_Dot[0].RawY + l_Dot[1].RawY) / 2.0;
    //  
    //    // of the eye relative to the camera 
    //    g_GameState.m_HeadTrackParms.m_dYAngleCorrection = g_GameState.m_HeadTrackParms.m_dRadPerCameraPixel * (l_dEyeYCam - g_GameState.m_HeadTrackParms.m_dCameraYCenter);
    //  }
    //}
    break;

  case 'u': GSConvScaleChange(0,  0.05); break;
  case 'j': GSConvScaleChange(0, -0.05); break;

  case 'i': GSConvScaleChange(1,  0.05); break;
  case 'k': GSConvScaleChange(1, -0.05); break;

  case 'o': GSConvScaleChange(2,  0.05); break;
  case 'l': GSConvScaleChange(2, -0.05); break;
  }
}

void Display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);

  double l_d3EyePosInMM[3];
  bool   l_bEyeLocated;

  if (g_GameState.m_bStereoEnabled)
  {
    //enable grayscale shader
    glUseProgram(g_GameState.m_GreyScaleShaderProgram);
    //set the conversion scale
    glUniform3fv(g_GameState.m_GSConvScaleLoc, 1, g_GameState.m_GSConvScale);
    
    for (int CurrentEye=0; CurrentEye<2; CurrentEye++) //STEREO_LEFT_EYE = 0, STEREO_RIGHT_EYE = 1
    {
      l_bEyeLocated = false;
      if (g_GameState.m_bHeadTrackingEnabled)
      {
        l_bEyeLocated = CalcEyePosInMM(g_GameState.m_pTrackingWiimote, 
                                       (EyeOrigin_t)CurrentEye, 
                                       g_GameState.m_FrustumParms, 
                                       l_d3EyePosInMM);
      }
      if(!l_bEyeLocated)
      {
        //set default eye coordinates
        l_d3EyePosInMM[0] = g_GameState.m_FrustumParms.m_dEyeDistMM * ((double)CurrentEye - 0.5);
        l_d3EyePosInMM[1] = 0.0;
        l_d3EyePosInMM[2] = g_GameState.m_FrustumParms.m_dDefHeadDistMM;
      }

      //set colormask
      if (CurrentEye == STEREO_LEFT_EYE)
      {
        glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
      }
      else
      {
        glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
      }

      //clear depth buffers
      glClear(GL_DEPTH_BUFFER_BIT);

      //set frustum
      SetFrustum(g_GameState.m_FrustumParms, l_d3EyePosInMM);

      RenderScene();
    }
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    //disable greyscale shader
    glUseProgram(0);
  }
  else // !g_GameState.m_bStereoEnabled
  {
    l_bEyeLocated = false;
    if (g_GameState.m_bHeadTrackingEnabled)
    {
      l_bEyeLocated = CalcEyePosInMM(g_GameState.m_pTrackingWiimote, 
                                     MONO_CENTER, 
                                     g_GameState.m_FrustumParms, 
                                     l_d3EyePosInMM);
    }
    if(!l_bEyeLocated)
    {
      //set default eye coordinates
      l_d3EyePosInMM[0] = 0.0;
      l_d3EyePosInMM[1] = 0.0;
      l_d3EyePosInMM[2] = g_GameState.m_FrustumParms.m_dDefHeadDistMM;
    }

    //clear depth buffer
    glClear(GL_DEPTH_BUFFER_BIT);

    //set frustum
    SetFrustum(g_GameState.m_FrustumParms, l_d3EyePosInMM);

    RenderScene();
  }

  GLdouble width = (GLdouble)glutGet(GLUT_SCREEN_WIDTH),
           height = (GLdouble)glutGet(GLUT_SCREEN_HEIGHT);
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
  glScaled(1.0, -1.0, 1.0);
  glTranslatef(0.0, -height, 0.0);
  
  glMatrixMode(GL_MODELVIEW);
  glPushAttrib(GL_LIGHTING_BIT);
  glShadeModel(GL_FLAT);
  glDisable(GL_LIGHTING);
  glPushMatrix();
  glLoadIdentity();
  glBegin(GL_QUADS);
  glColor3f(0.2f, 0.2f, 0.2f);
  glVertex3f(0.0f, 0.0f, -0.5f);
  glVertex3f(0.0f, 30.0f, -0.5f);
  glVertex3f(325.0f, 30.0f, -0.5f);
  glVertex3f(325.0f, 0.0f, -0.5f);
  glEnd();

  glColor3f(1.0, 1.0, 1.0);
  glRasterPos3f(5.0, 22.0, 0.5);
  char string[256];
  sprintf(string, "RGB conv scale: %4.3f, %4.3f, %4.3f", 
    g_GameState.m_GSConvScale[0],
    g_GameState.m_GSConvScale[1],
    g_GameState.m_GSConvScale[2]);
  int len = strlen(string);
  for (int i=0; i<len; i++)
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15, string[i]);

  glPopMatrix();
  glMatrixMode(GL_PROJECTION);
  glPopMatrix();

  glPopAttrib();
  
  glutSwapBuffers();
} // Display


void Idle(void)
{
  if (g_GameState.m_pTrackingWiimote != NULL && g_GameState.m_pTrackingWiimote->IsConnected())
  {
    g_GameState.m_pTrackingWiimote->RefreshState();
  }

  glutPostRedisplay();
}

void Reshape(int f_iWidth, int f_iHeight)
{
  //set the new viewport dimension
  glViewport(0, 0, f_iWidth, f_iHeight);

  //should we set a new projection matrix?
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  double l_dHalfHeight = 0.5 * g_GameState.m_FrustumParms.m_dScreenHeightWorld,
         l_dHalfWidth = l_dHalfHeight * g_GameState.m_FrustumParms.m_dScreenAspect,
         l_zNear = 2.0 * g_GameState.m_FrustumParms.m_dScreenHeightWorld;

  glFrustum(-l_dHalfWidth, l_dHalfWidth, -l_dHalfHeight, l_dHalfHeight, l_zNear, l_zNear + 1000.0);
  glTranslated(0.0, 0.0, -l_zNear);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}