summaryrefslogtreecommitdiffstats
path: root/headtrack_stereo_demo/src/glutcallbacks.cpp
blob: 9b582f80b03c800c27ead4dea8f05a66120f4816 (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
#include <wiimote.h>
#include <GL\glew.h>
#include <GL\glut.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);
}

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;

    if (g_GameState.m_bStereoEnabled)
    {
      //enable greyscale shader
      glUseProgram(g_GameState.m_GreyScaleShaderProgram);
    }
    else
    {
      //disable greyscale shader
      glUseProgram(0);
    }
    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;
  }
}

void Display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);

  double l_d3EyePosInMM[3];
  bool   l_bEyeLocated;

  if (g_GameState.m_bStereoEnabled)
  {
    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);
  }
  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();
  }
    
  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();
}