summaryrefslogtreecommitdiffstats
path: root/matchblox/engine
diff options
context:
space:
mode:
Diffstat (limited to 'matchblox/engine')
-rw-r--r--matchblox/engine/C_3DObject.cpp10
-rw-r--r--matchblox/engine/C_3DObject.h4
-rw-r--r--matchblox/engine/C_Block.cpp10
-rw-r--r--matchblox/engine/C_Block.h2
-rw-r--r--matchblox/engine/C_Box.cpp8
-rw-r--r--matchblox/engine/C_Box.h2
-rw-r--r--matchblox/engine/C_Hand.cpp8
-rw-r--r--matchblox/engine/C_Log.h4
-rw-r--r--matchblox/engine/C_MatchBloxEngine.cpp212
-rw-r--r--matchblox/engine/C_MatchBloxEngine.h21
10 files changed, 230 insertions, 51 deletions
diff --git a/matchblox/engine/C_3DObject.cpp b/matchblox/engine/C_3DObject.cpp
index b9186cd..24c759a 100644
--- a/matchblox/engine/C_3DObject.cpp
+++ b/matchblox/engine/C_3DObject.cpp
@@ -1,3 +1,4 @@
+#include <GL/glew.h>
#include <GL/glut.h>
#include <vector>
#include <iostream>
@@ -78,15 +79,22 @@ void C_3DObject::TransRotateScale()
glScaled(m_Scale.x, m_Scale.y, m_Scale.z);
}
-void C_3DObject::Render()
+void C_3DObject::Render(GLint f_iTexLocation)
{
//apply OpenGL settings for rendering this Object
glPushAttrib(GL_TEXTURE_BIT);
glEnable(GL_TEXTURE_2D);
//modulate the texture color with the computed material
//colors
+ glActiveTexture(GL_TEXTURE0);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, m_uiColorTex);
+
+
+ if (f_iTexLocation != 0)
+ {
+ glUniform1i(f_iTexLocation, 0);
+ }
//set material properties
glMaterialfv(GL_FRONT, GL_AMBIENT, m_Mat.m_fAmb);
diff --git a/matchblox/engine/C_3DObject.h b/matchblox/engine/C_3DObject.h
index 2ad5a88..c025d11 100644
--- a/matchblox/engine/C_3DObject.h
+++ b/matchblox/engine/C_3DObject.h
@@ -42,8 +42,8 @@ public:
void TransRotateScale(); //applies the scaling, translation and rotation
//for this object
- virtual void Render(); //renders the object using the current projection
- //and modelview matrices
+ virtual void Render(GLint f_iTexLocation); //renders the object using the current projection
+ //and modelview matrices
protected:
Vect3D_t m_Pos,
diff --git a/matchblox/engine/C_Block.cpp b/matchblox/engine/C_Block.cpp
index 81d3b66..50432ac 100644
--- a/matchblox/engine/C_Block.cpp
+++ b/matchblox/engine/C_Block.cpp
@@ -24,7 +24,7 @@ C_Block::~C_Block()
}
-void C_Block::Render(unsigned int f_iElapsedTime)
+void C_Block::Render(unsigned int f_iElapsedTime, GLint f_iTexLocation)
{
unsigned int l_uiDeltaTime = f_iElapsedTime - m_uiAnimStart;
double l_Scale, l_Scalexz, l_Frac;
@@ -46,7 +46,7 @@ void C_Block::Render(unsigned int f_iElapsedTime)
//glScaled(l_Scale, l_Scale, l_Scale);
SetScale(l_Scale, l_Scale, l_Scale);
TransRotateScale();
- C_3DObject::Render();
+ C_3DObject::Render(f_iTexLocation);
break;
case BS_FADE_OUT:
@@ -54,7 +54,7 @@ void C_Block::Render(unsigned int f_iElapsedTime)
// glScaled(l_Scale, l_Scale, l_Scale);
SetScale(l_Scale, l_Scale, l_Scale);
TransRotateScale();
- C_3DObject::Render();
+ C_3DObject::Render(f_iTexLocation);
break;
case BS_COLLIDE: //wobble
@@ -64,14 +64,14 @@ void C_Block::Render(unsigned int f_iElapsedTime)
l_Scalexz = 1.0 - sin(l_Frac)*0.2;
SetScale(l_Scalexz, l_Scale, l_Scalexz);
TransRotateScale();
- C_3DObject::Render();
+ C_3DObject::Render(f_iTexLocation);
break;
case BS_IDLE:
default:
SetScale(1.0, 1.0, 1.0);
TransRotateScale();
- C_3DObject::Render();
+ C_3DObject::Render(f_iTexLocation);
break;
}
diff --git a/matchblox/engine/C_Block.h b/matchblox/engine/C_Block.h
index f42c2ba..fcc1808 100644
--- a/matchblox/engine/C_Block.h
+++ b/matchblox/engine/C_Block.h
@@ -21,7 +21,7 @@ public:
MatProps_t f_Mat);
~C_Block();
- void Render(unsigned int f_iElapsedTime);
+ void Render(unsigned int f_iElapsedTime, GLint f_iTexLocation);
inline void SetState(BlockAnimState f_State, unsigned int f_uiElapsedTime)
{ if (m_CurrState != f_State) {m_CurrState = f_State; m_uiAnimStart = f_uiElapsedTime; } }
diff --git a/matchblox/engine/C_Box.cpp b/matchblox/engine/C_Box.cpp
index 343ae98..573c78c 100644
--- a/matchblox/engine/C_Box.cpp
+++ b/matchblox/engine/C_Box.cpp
@@ -66,7 +66,7 @@ void C_Box::RandomizeTiles()
}
}
-void C_Box::Render()
+void C_Box::Render(GLint f_iTexLocation)
{
//
glPushMatrix();
@@ -75,7 +75,7 @@ void C_Box::Render()
TransRotateScale();
//render the box
- C_3DObject::Render();
+ C_3DObject::Render(f_iTexLocation);
//render the tiles
@@ -89,7 +89,7 @@ void C_Box::Render()
//tile with a hole
glPushMatrix();
m_pTiles[l_iTileIndex]->TransRotateScale();
- m_pTiles[l_iTileIndex]->Render();
+ m_pTiles[l_iTileIndex]->Render(f_iTexLocation);
glPopMatrix();
}
else
@@ -101,7 +101,7 @@ void C_Box::Render()
m_LowLeftTilePos.z - (double)y * m_dTileSize);
glPushMatrix();
m_pTiles[4]->TransRotateScale();
- m_pTiles[4]->Render();
+ m_pTiles[4]->Render(f_iTexLocation);
glPopMatrix();
}
}
diff --git a/matchblox/engine/C_Box.h b/matchblox/engine/C_Box.h
index ed8c133..a517135 100644
--- a/matchblox/engine/C_Box.h
+++ b/matchblox/engine/C_Box.h
@@ -17,7 +17,7 @@ public:
~C_Box();
void RandomizeTiles();
- void Render();
+ void Render(GLint f_iTexLocation);
private:
C_3DObject **m_pTiles; //the tile objects for the top face of the box (holes)
diff --git a/matchblox/engine/C_Hand.cpp b/matchblox/engine/C_Hand.cpp
index 796be88..785ea12 100644
--- a/matchblox/engine/C_Hand.cpp
+++ b/matchblox/engine/C_Hand.cpp
@@ -35,19 +35,19 @@ void C_Hand::Render(unsigned int f_iElapsedTime)
{
case HS_COLLIDE:
m_Mat.setEmi(1.0, 0.0, 0.0, 1.0);
- C_3DObject::Render();
+ C_3DObject::Render(0);
break;
case HS_GRAB:
- C_3DObject::Render();
+ C_3DObject::Render(0);
break;
case HS_RELEASE:
- C_3DObject::Render();
+ C_3DObject::Render(0);
break;
case HS_IDLE:
default:
- C_3DObject::Render();
+ C_3DObject::Render(0);
break;
}
diff --git a/matchblox/engine/C_Log.h b/matchblox/engine/C_Log.h
index 2b598dd..a376e76 100644
--- a/matchblox/engine/C_Log.h
+++ b/matchblox/engine/C_Log.h
@@ -2,6 +2,8 @@
#define C_LOG_HEADER_FILE
+
+
class C_Log
{
public:
@@ -13,7 +15,7 @@ public:
bool LogSessionTotals(int f_iUserID, int f_iGameID, unsigned int f_uiTotalTime);
private:
-// FILE *m_pLogFile;
+
};
#endif //C_LOG_HEADER_FILE
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;
diff --git a/matchblox/engine/C_MatchBloxEngine.h b/matchblox/engine/C_MatchBloxEngine.h
index 4e5fcf3..5d90291 100644
--- a/matchblox/engine/C_MatchBloxEngine.h
+++ b/matchblox/engine/C_MatchBloxEngine.h
@@ -6,7 +6,7 @@
#include "message_input.h"
#include "typedefs.h"
#include <list>
-#include "C_Smoother.h"
+#include "C_Smoother.h"
//#include <pair>
using namespace std;
@@ -157,9 +157,9 @@ public:
~C_MatchBloxEngine();
GameResult ProcessMsgs(void);
- void Render(unsigned int f_uiElapsedTime);
-
- bool NewGame(int f_iUserID, int f_iGameId, BoxSize f_BS);
+ void Render(unsigned int f_uiElapsedTime, input_payload_wiimote &f_HTWiimote, FrustumParms_t &f_FrustParms);
+
+ bool NewGame(int f_iGameMode);
bool StartGame();
bool Pause();
bool Resume();
@@ -189,14 +189,25 @@ private:
//mapped to the z=0.0 in world coordinates
BoundingBox_t m_WorldBox; //an invisible box that limits the movement of the
//player
+ bool m_bHeadTrackingEnabled,
+ m_bStereoEnabled;
+ GLuint m_GrayScaleShaderProgram; //handle to the grayscale shader program
+ GLint m_GSConvScaleLocation, //handle to the g_ConversionScale variable in the shader
+ m_GSTextureLocation; //
+ GLfloat m_GSConvScale[3]; //grayscale conversion scale (default 0.299, 0.587, 0.114)
//init routines
bool LoadModels(const char* f_strModelDir);
void DeleteModels();
void LoadTexture(const char* f_BmpName, GLuint &f_uiTexHandle);
+ bool LoadGrayScaleShader();
//render routines
+ void RenderEnv(unsigned int f_uiElapsedTime);
+ void RenderMain(unsigned int f_uiElapsedTime);
+ void RenderOverlay(unsigned int f_uiElapsedTime);
+
void RenderGetReady();
void RenderHUD(unsigned int f_uiElapsedTime);
void RenderResults();
@@ -213,8 +224,6 @@ private:
void GameFinished();
//wiimote functions
- //bool FindIRDots(input_payload_wiimote *f_pWiimote, ir_dot_t f_Dot[2]);
- //bool CalcWiimoteRelativeCursorPos(input_payload_wiimote *f_pWiimote, Vect3D_t *f_pRelPos);
bool ConvertWiimoteToWorld(input_payload_wiimote *f_pWiimote, Vect3D_t *f_WorldPos);
};