summaryrefslogtreecommitdiffstats
path: root/matchblox/engine/C_MatchBloxEngine.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'matchblox/engine/C_MatchBloxEngine.cpp')
-rw-r--r--matchblox/engine/C_MatchBloxEngine.cpp212
1 files changed, 186 insertions, 26 deletions
diff --git a/matchblox/engine/C_MatchBloxEngine.cpp b/matchblox/engine/C_MatchBloxEngine.cpp
index 6984239..2a660fd 100644
--- a/matchblox/engine/C_MatchBloxEngine.cpp
+++ b/matchblox/engine/C_MatchBloxEngine.cpp
@@ -1,3 +1,4 @@
+#include <GL/glew.h>
#include <GL/glut.h>
#include <time.h>
@@ -21,6 +22,8 @@
#include "C_Smoother.h"
#include "C_MatchBloxEngine.h"
#include "font.h"
+#include "stereoheadtrackfrustum.h"
+#include "textfile.h"
extern "C" {
extern GLuint g_iBase;
@@ -34,24 +37,33 @@ C_MatchBloxEngine::C_MatchBloxEngine(const char *f_strModelPath,
{
//create logger
-
- //Load models
- if (LoadModels(f_strModelPath))
+
+ m_State = ES_INITIALISED;
+ //load and build the grayscale shader program
+ if (!LoadGrayScaleShader())
{
- //set state to initialised;
- m_State = ES_INITIALISED;
+ m_State = ES_ERROR;
}
- else
+ m_GSConvScale[0] = 0.299; //grayscale conversion scale (default 0.299, 0.587, 0.114)
+ m_GSConvScale[1] = 0.587;
+ m_GSConvScale[2] = 0.114;
+
+ //Load models
+ if (!LoadModels(f_strModelPath))
{
+ //set state to initialised;
m_State = ES_ERROR;
}
-
+
//initialise a random seed
srand ( time(NULL) );
//init vars
m_CurrentBox = BS_SMALL;
+ m_bHeadTrackingEnabled = false;
+ m_bStereoEnabled = false;
+
//init the world bounding box
m_WorldBox.m_Min = Vect3D_t(-15.0, -5.0, -15.0);
m_WorldBox.m_Max = Vect3D_t(15.0, 15.0, 15.0);
@@ -182,11 +194,75 @@ GameResult C_MatchBloxEngine::ProcessMsgs(void)
}
-void C_MatchBloxEngine::Render(unsigned int f_uiElapsedTime)
+void C_MatchBloxEngine::Render(unsigned int f_uiElapsedTime, input_payload_wiimote &f_HTWiimote, FrustumParms_t &f_FrustParms)
{
- //glMatrixMode(GL_MODELVIEW);
- //glLoadIdentity();
+ Vect3D_t l_LeftEye, l_CenterEye, l_RightEye;
+
+ if (f_HTWiimote.posDataValid)
+ {
+ //calculate the eye positions
+ CalcEyePosInMM(&f_HTWiimote, STEREO_LEFT_EYE, f_FrustParms, l_LeftEye);
+ CalcEyePosInMM(&f_HTWiimote, STEREO_RIGHT_EYE, f_FrustParms, l_RightEye);
+ CalcEyePosInMM(&f_HTWiimote, MONO_CENTER, f_FrustParms, l_CenterEye);
+ }
+ else
+ {
+ //set default positions
+ l_LeftEye = l_RightEye = l_CenterEye = Vect3D_t(0.0, 0.0, f_FrustParms.m_dDefHeadDistMM);
+ l_LeftEye.x = f_FrustParms.m_dEyeDistMM * -0.5;
+ l_RightEye.x = f_FrustParms.m_dEyeDistMM * 0.5;
+ }
+
+ //always render the environment map in color, using the mono head tracked frustum
+ SetFrustum(f_FrustParms, l_CenterEye, true);
+ RenderEnv(f_uiElapsedTime);
+
+ if (!m_bStereoEnabled)
+ {
+ //render the 3d shit once
+ SetFrustum(f_FrustParms, l_CenterEye);
+ RenderMain(f_uiElapsedTime);
+ }
+ else
+ {
+ //enable grayscale shader
+ glUseProgram(m_GrayScaleShaderProgram);
+ //set the conversion scale
+ glUniform3fv(m_GSConvScaleLocation, 1, m_GSConvScale);
+
+ for (int CurrentEye=0; CurrentEye<2; CurrentEye++) //STEREO_LEFT_EYE = 0, STEREO_RIGHT_EYE = 1
+ {
+ //clear depth buffer
+ glClear(GL_DEPTH_BUFFER_BIT);
+
+ //set colormask
+ if (CurrentEye == STEREO_LEFT_EYE)
+ {
+ glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
+ //set frustum
+ SetFrustum(f_FrustParms, l_LeftEye);
+ }
+ else
+ {
+ glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
+ //set frustum
+ SetFrustum(f_FrustParms, l_RightEye);
+ }
+
+ RenderMain(f_uiElapsedTime);
+ }
+
+ //restore the colormask
+ glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+ //disable greyscale shader
+ glUseProgram(0);
+ }
+
+ RenderOverlay(f_uiElapsedTime);
+}
+void C_MatchBloxEngine::RenderEnv(unsigned int f_uiElapsedTime)
+{
glPushMatrix();
//set camera pitch
@@ -196,6 +272,11 @@ void C_MatchBloxEngine::Render(unsigned int f_uiElapsedTime)
m_pEnvMap->Render();
glPopMatrix();
+}
+
+void C_MatchBloxEngine::RenderMain(unsigned int f_uiElapsedTime)
+{
+ GLint l_iTexShaderLocation = m_bStereoEnabled? m_GSTextureLocation : 0;
glPushMatrix();
@@ -203,27 +284,49 @@ void C_MatchBloxEngine::Render(unsigned int f_uiElapsedTime)
glTranslated(0.0, 0.0, -16.0);
glRotated(20.0, 1.0, 0.0, 0.0);
- m_pBox[(int)m_CurrentBox]->Render();
+ //always render the box
+ m_pBox[(int)m_CurrentBox]->Render(l_iTexShaderLocation);
switch (m_State)
{
+ case ES_PLAYING_GRAB_BLOCK:
+ case ES_PLAYING_PUT_BLOCK:
+ m_pBlock[(int)m_CurrentBlock]->Render(f_uiElapsedTime, l_iTexShaderLocation);
+ m_pHand->Render(f_uiElapsedTime); //do not break also render the hand
case ES_INITIALISED:
case ES_ERROR:
m_pHand->Render(f_uiElapsedTime);
break;
+ }
+
+ glPopMatrix();
+}
+
+void C_MatchBloxEngine::RenderOverlay(unsigned int f_uiElapsedTime)
+{
+ switch (m_State)
+ {
+ case ES_INITIALISED:
+ case ES_ERROR:
+ break;
case ES_GET_READY:
//render some GET READY text
RenderGetReady();
+ if (f_uiElapsedTime - m_uiLastPosValid > 5000)
+ {
+ RenderPointHere();
+ }
break;
case ES_PLAYING_GRAB_BLOCK:
case ES_PLAYING_PUT_BLOCK:
- m_pBlock[(int)m_CurrentBlock]->Render(f_uiElapsedTime);
- m_pHand->Render(f_uiElapsedTime);
-
//render timer and block count
RenderHUD(f_uiElapsedTime);
+ if (f_uiElapsedTime - m_uiLastPosValid > 5000)
+ {
+ RenderPointHere();
+ }
break;
case ES_PAUSED:
@@ -234,14 +337,6 @@ void C_MatchBloxEngine::Render(unsigned int f_uiElapsedTime)
//render results...
break;
}
-
- if (f_uiElapsedTime - m_uiLastPosValid > 5000)
- {
- RenderPointHere();
- }
-
- glPopMatrix();
-
}
void C_MatchBloxEngine::RenderGetReady()
@@ -325,14 +420,20 @@ void C_MatchBloxEngine::RenderText(GLint x, GLint y, char *string, struct ColorS
glPopAttrib();
}
-bool C_MatchBloxEngine::NewGame(int f_iUserID, int f_iGameId, BoxSize f_BS)
+bool C_MatchBloxEngine::NewGame(int f_iGameMode)
{
if(m_State == ES_INITIALISED)
{
- m_CurrentBox = f_BS;
-
+ switch (f_iGameMode)
+ {
+ case 0:
+ m_bHeadTrackingEnabled = true;
+ m_bStereoEnabled = true;
+ m_CurrentBox = BoxSize::BS_MED;
+ }
+
//prepare a session struct for administration
- m_pCurrentSession = new GameSession(m_GameSettings.m_iNrOfTurns, f_BS);
+ m_pCurrentSession = new GameSession(m_GameSettings.m_iNrOfTurns, m_CurrentBox);
//randomize the box tiles
m_pBox[(int)m_CurrentBox]->RandomizeTiles();
@@ -413,6 +514,65 @@ bool C_MatchBloxEngine::Abort()
return false;
}
+bool C_MatchBloxEngine::LoadGrayScaleShader()
+{
+ GLint l_bStatus;
+ char *vs_text = NULL,*fs_text = NULL;
+ GLuint vs = glCreateShader(GL_VERTEX_SHADER),
+ fs = glCreateShader(GL_FRAGMENT_SHADER);
+
+ vs_text = textFileRead("shaders\\grayscale.vert");
+ if (vs_text == NULL)
+ {
+ return false;
+ }
+ fs_text = textFileRead("shaders\\grayscale.frag");
+ if (fs_text == NULL)
+ {
+ free(vs_text);
+ return false;
+ }
+
+ glShaderSource(vs, 1, (const char**)&vs_text,NULL);
+ glShaderSource(fs, 1, (const char**)&fs_text,NULL);
+
+ free(vs_text);
+ free(fs_text);
+
+ glCompileShader(vs);
+// printShaderInfoLog(vs);
+ glGetShaderiv(vs, GL_COMPILE_STATUS, &l_bStatus);
+ if (l_bStatus == GL_FALSE)
+ {
+ return false;
+ }
+
+ glCompileShader(fs);
+// printShaderInfoLog(fs);
+ glGetShaderiv(fs, GL_COMPILE_STATUS, &l_bStatus);
+ if (l_bStatus == GL_FALSE)
+ {
+ return false;
+ }
+
+ m_GrayScaleShaderProgram = glCreateProgram();
+ glAttachShader(m_GrayScaleShaderProgram,fs);
+ glAttachShader(m_GrayScaleShaderProgram,vs);
+
+ glLinkProgram(m_GrayScaleShaderProgram);
+// printProgramInfoLog(g_GameState.m_GreyScaleShaderProgram);
+ glGetProgramiv(m_GrayScaleShaderProgram, GL_LINK_STATUS, &l_bStatus);
+ if (l_bStatus == GL_FALSE)
+ {
+ return false;
+ }
+
+ m_GSConvScaleLocation = glGetUniformLocation(m_GrayScaleShaderProgram, "g_ConversionWeights");
+ m_GSTextureLocation = glGetUniformLocation(m_GrayScaleShaderProgram, "g_TexSampler");
+
+ return true;
+}
+
bool C_MatchBloxEngine::LoadModels(const char* f_strModelDir)
{
MatProps_t l_Mat;