PHP Code:
void GLRenderer::GLRenderScene()
{
NT_GL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT))
NT_GL(glMatrixMode(GL_MODELVIEW))
NT_GL(glLoadIdentity())
/*
-Generate the matrix to represent the current view direction and position
*/
NT_GL(glMatrixMode(GL_MODELVIEW))
NT_GL(glPushMatrix())
NT_GL(glLoadIdentity())
Vector3 View = Export.mpPlayer.mpCam->mPosition + Export.mpPlayer.mpCam->mView;//Export.mpCam->mPosition + Export.mpCam->mView;
Vector3 Pos = Export.mpPlayer.mpCam->mPosition;
Vector3 Up = Export.mpPlayer.mpCam->mUp;
// gluLookAt(Pos.x,Pos.y, Pos.z, View.x, View.y, View.z, Up.x, Up.y, Up.z);
#if 1
R_GenerateViewMatrix(RAD2DEG(Export.mpPlayer.mpCam->xRadians),RAD2DEG(Export.mpPlayer.mpCam->yRadians),0,Pos);
#elif 0
Matrix4x4 X,Y,trans,final;
X.InitFromRadians(-Export.mpPlayer.mpCam->xRadians,0,0);
Y.InitFromRadians(0,-Export.mpPlayer.mpCam->yRadians,0);
trans.SetTranslation(&(Pos * -1.0f));
final = X * Y * trans;
R_Replace4x4GLMatrix(final,GL_MODELVIEW);
#elif 0
Quaternion temp;
// temp.InitFromRadians(Export.mpPlayer.mpCam->xRadians,Export.mpPlayer.mpCam->yRadians,0);
Vector3 tempView(0,0,-1);
Vector3 Strafe(1,0,0);
Quaternion32RotateVector3Radians(&tempView,Export.mpPlayer.mpCam->xRadians,Export.mpPlayer.mpCam->yRadians,0);
Quaternion32RotateVector3Radians(&Strafe,Export.mpPlayer.mpCam->xRadians,Export.mpPlayer.mpCam->yRadians,0);
Vector3 up = CrossProduct(&Strafe,&tempView);
Vector3 eye = Pos + tempView;
gluLookAt(Pos.x,Pos.y,Pos.z,eye.x,eye.y,eye.z,0,1,0);
#endif
#if 1
R_BuildBillboardMatrix(Export.mpPlayer.mpCam->mStrafe,View,Up,mBillBoardMatrix);
#else
Matrix3x3 Rot_Only;
Rot_Only.InitFromRadians(-Export.mpPlayer.mpCam->xRadians,-Export.mpPlayer.mpCam->yRadians,0);
Rot_Only.Invert();
Matrix4x4 ToSend;
ToSend = Rot_Only;
ToSend.FillGLMatrix(mBillBoardMatrix);
#endif
/*
-Render everything that has been sent to the renderer
*/
RenderQuake3SingleTexturedFaces();
RenderQuake3MultiTexturedFaces();
RenderMS3DModels();
RenderAllDebugSpheres();
RenderallDebugWireframeSpheres();
RenderAllMatrixDebugSpheres();
RenderAllDebugLines();
RenderAllDebugBoxes();
RenderAllDebugPoints();
RenderAllDebugTriangles();
RenderBasicQuads();
RenderSkyBox();
RenderSkySphere();
RenderAlphaBlendedQuads(); //NEEDS TO COME LAST!!!
RenderAllDebugBillboards(); //needs to come last also cuz it now has alpha blend stuff
NT_GL(glPopMatrix())
/*
-This needs to come last because Text is drawn ontop of everything else (otherwise it can be depth-tested away)
*/
gpTextManager->DrawText();
SwapBuffers(gpNTGLAPI->NT_DEVICE_CONTEXT);
/*
-This clears the containers so they can be re-filled for the next scene
*/
ResetScene();
NT_GL(glMatrixMode(GL_MODELVIEW))
NT_GL(glLoadIdentity())
}
And here is an example of how an object accesses the renderer functionality:
PHP Code:
void MainGameCycle()
{
double currenttime = gpFPS->FPS_GETTIME();
//It just so happens that the last time we swapped the color buffer was the last time we ran
//any of the physics frames
double dt = currenttime - gpFPS->LastTimeBufferSwapped;
if(dt >= gpPhysicsManager->mSecondsPerFrame) //this works
{
gpTextManager->PushLineOfText(" Teletubbies deserve it ",1,1,1,1);
Export.mKeys = Export.CheckKeyBoard();
gpFPS->UpdateFPS();
gpPhysicsManager->mTimeBuffer += dt;
float discrepancy = currenttime - (gpFPS->LastTimeBufferSwapped+gpPhysicsManager->mSecondsPerFrame);
//this way we don't lose ANY time
if(discrepancy>0)
{
gpPhysicsManager->mTimeBuffer += discrepancy;
}
Export.mpPlayer.mpCam->MatrixCheckMouse();
ptest_hovertank->Update();
ptest_hovertank1->Update();
#define DRAW_CLOSEST_NODE 1
#if DRAW_CLOSEST_NODE
int closest_node_dist = 999999999.0f;
int closest_node_index = 0;
for(int c = 0; c < waypoint_nodes.size(); c++)
{
Vector3 & node_pos = *waypoint_nodes[c];
float closest_dist_sq = (node_pos - Export.mpPlayer.CurrentState.mPosition).BasicLength();
if(closest_dist_sq < closest_node_dist)
{
closest_node_dist = closest_dist_sq;
closest_node_index = c;
}
}
Vector3 &closest_node_pos = *waypoint_nodes[closest_node_index];
gpGLRenderer->AddDebugLineToRenderer(closest_node_pos.x,closest_node_pos.y,closest_node_pos.z,closest_node_pos.x,closest_node_pos.y + 100.0f,closest_node_pos.z,1.0f,1.0f,1.0f,5.0f);
DebugPrintInt("closest_node_index",closest_node_index,gpTextManager);
#endif
//indexes into std::vector<vector3*> waypoint_nodes
static int PATH_TEST_START = 6;
static int PATH_TEST_END = 11;
static std::stack<int> StackIntPath;
static std::vector<int> VectorIntPath;
static HighResSecondsTimer PathTestTimer;
PathTestTimer.Update();
if(Export.IsStateTrue(RMOUSEDOWN) && PathTestTimer.mTimeBuffer >= .5f)
{
PathTestTimer.ResetTimeBuffer();
//Find a random start and end
int random_start=0,random_end=0;
do
{
random_start = rand()%waypoint_nodes.size();
random_end = rand()%waypoint_nodes.size();
}while (random_start == random_end);
PATH_TEST_START = random_start;
PATH_TEST_END = random_end;
//Fill the stack of index values
//For debugging, find the entire path immediately so we can draw it
FindPath(PathMatrices,NUM_PATH_MATRICES,random_start,random_end,StackIntPath);
VectorIntPath.clear();
while(StackIntPath.size())
{
VectorIntPath.push_back(StackIntPath.top());
StackIntPath.pop();
}
}
int num_moves = GetNumMoves(PathMatrices,NUM_PATH_MATRICES,PATH_TEST_START,PATH_TEST_END);
DebugPrintInt("#moves",num_moves,gpTextManager);
DebugPrintInt("Path start",PATH_TEST_START,gpTextManager);
DebugPrintInt("Path end",PATH_TEST_END,gpTextManager);
gpTextManager->PushLineOfText(" ",-1024,1,1,1);
gpTextManager->PushLineOfText("Printing path: " ,-1024,1,1,1);
for(int i = 0; i < VectorIntPath.size(); i++)
{
int this_index = VectorIntPath[i];
DebugPrintInt("Node#",this_index,gpTextManager);
}
if(waypoint_nodes.size())
{
Vector3 path_start = *waypoint_nodes[PATH_TEST_START];
Vector3 path_end = *waypoint_nodes[PATH_TEST_END];
gpGLRenderer->AddWireframeDebugSphereToRenderer(path_start.x,path_start.y,path_start.z,0,0,0,0,0,0,70.0f);
gpGLRenderer->AddWireframeDebugSphereToRenderer(path_end.x,path_end.y,path_end.z,0,0,0,1,1,1,70.0f);
int num_lines_draw = 0;
for(int i = 0; i < (int)VectorIntPath.size(); i++)
{
Vector3 pos = *waypoint_nodes[VectorIntPath[i]];
gpGLRenderer->AddWireframeDebugSphereToRenderer(pos.x,pos.y,pos.z,0,0,0,0.0f,1.0f,0.0f,50.0f);
}
// DebugPrintInt("#lines drawn",num_lines_draw,gpTextManager);
for(i = 0; i < (int)VectorIntPath.size()-1; i++)
{
Vector3 pos = *waypoint_nodes[VectorIntPath[i]];
Vector3 pos1 = *waypoint_nodes[VectorIntPath[i+1]];
// gpGLRenderer->AddWireframeDebugSphereToRenderer(pos.x,pos.y,pos.z,0,0,0,0.0f,1.0f,0.0f,50.0f);
gpGLRenderer->AddDebugLineToRenderer(pos.x,pos.y,pos.z,pos1.x,pos1.y,pos1.z,1.0f,1.0f,1.0f,5.0f);
}
}
std::vector<Vector3*>::iterator waypoint_ptr;
for(waypoint_ptr = waypoint_nodes.begin(); waypoint_ptr != waypoint_nodes.end(); waypoint_ptr++)
{
Vector3 & ref = **waypoint_ptr;
gpGLRenderer->AddWireframeDebugSphereToRenderer(ref.x,ref.y,ref.z,0.0f,0.0f,0.0f,1.0f,1.0f,0.0f,15.0);
}
if(GetKeyState('E') & 0x80)
{
ptest_hovertank->CurrentState.mPosition = Vector3(METER(3),1000,0);
ptest_hovertank->CurrentState.mLinearMomentum = ptest_hovertank->CurrentState.mLinearVelocity = Vector3(0,0,0);
ptest_hovertank->CurrentState.mAngularMomentum = ptest_hovertank->CurrentState.mAngularVelocity = Vector3(0,0,0);
ptest_hovertank->CurrentState.mQuat_Orientation.InitFromRadians(0,0,0);
ptest_hovertank->CurrentState.mMat_Orientation.MakeIdentity();
ptest_hovertank->CurrentState.mEuler_Orientation = Vector3(0,0,0);
}
if(GetKeyState('T') & 0x80)
{
if(player_starts.size())
{
Export.mpPlayer.CurrentState.mPosition = *player_starts[1];
ptest_hovertank->CurrentState.mPosition = *player_starts[0];
}
else
{
ptest_hovertank->CurrentState.mPosition = Vector3(METER(3),4000,METER(1));
Export.mpPlayer.CurrentState.mPosition = Vector3(METER(5),4000,METER(2));
}
ptest_hovertank->CurrentState.mLinearMomentum = ptest_hovertank->CurrentState.mLinearVelocity = Vector3(0,0,0);
ptest_hovertank->CurrentState.mAngularMomentum = ptest_hovertank->CurrentState.mAngularVelocity = Vector3(0,0,0);
ptest_hovertank->CurrentState.mQuat_Orientation.InitFromRadians(0,DEG2RAD(179),0);
ptest_hovertank->CurrentState.mMat_Orientation = ptest_hovertank->CurrentState.mQuat_Orientation.ToMatrix();
Vector3 *euler = &ptest_hovertank->CurrentState.mEuler_Orientation;
ptest_hovertank->CurrentState.mMat_Orientation.ToRadians(&euler->x,&euler->y,&euler->z);
ptest_hovertank1->CurrentState.mPosition = Vector3(METER(3),2000,METER(1));
ptest_hovertank1->CurrentState.mLinearMomentum = ptest_hovertank->CurrentState.mLinearVelocity = Vector3(0,0,0);
ptest_hovertank1->CurrentState.mAngularMomentum = ptest_hovertank->CurrentState.mAngularVelocity = Vector3(0,0,0);
ptest_hovertank1->CurrentState.mQuat_Orientation.InitFromRadians(0,DEG2RAD(179),0);
ptest_hovertank1->CurrentState.mMat_Orientation = ptest_hovertank->CurrentState.mQuat_Orientation.ToMatrix();
euler = &ptest_hovertank1->CurrentState.mEuler_Orientation;
ptest_hovertank1->CurrentState.mMat_Orientation.ToRadians(&euler->x,&euler->y,&euler->z);
}
if(GetKeyState('Y') & 0x80)
{
ptest_hovertank->CurrentState.mLinearMomentum = ptest_hovertank->CurrentState.mLinearVelocity = Vector3(0,0,0);
}
if(GetKeyState('R') & 0x80)
{
ptest_hovertank->CurrentState.mLinearMomentum = ptest_hovertank->CurrentState.mLinearVelocity = Vector3(0,0,0);
ptest_hovertank->CurrentState.mAngularMomentum = ptest_hovertank->CurrentState.mAngularVelocity = Vector3(0,0,0);
ptest_hovertank->CurrentState.mQuat_Orientation.InitFromRadians(0,0,0);
ptest_hovertank->CurrentState.mMat_Orientation.MakeIdentity();
ptest_hovertank->CurrentState.mEuler_Orientation = Vector3(0,0,0);
ptest_hovertank1->CurrentState.mLinearMomentum = ptest_hovertank->CurrentState.mLinearVelocity = Vector3(0,0,0);
ptest_hovertank1->CurrentState.mAngularMomentum = ptest_hovertank->CurrentState.mAngularVelocity = Vector3(0,0,0);
ptest_hovertank1->CurrentState.mQuat_Orientation.InitFromRadians(0,0,0);
ptest_hovertank1->CurrentState.mMat_Orientation.MakeIdentity();
ptest_hovertank1->CurrentState.mEuler_Orientation = Vector3(0,0,0);
}
float force = 100;
if(GetKeyState(VK_LSHIFT) & 0x80)
force = force * 10;
if(Export.mKeys & FORWARD)
{
Export.mpPlayer.CurrentState.mLinearMomentum += Export.mpPlayer.mpCam->mView*force;
}
if(Export.mKeys & BACKWARD)
{
Export.mpPlayer.CurrentState.mLinearMomentum -= Export.mpPlayer.mpCam->mView*force;
}
if(Export.mKeys & SIDESTEPRIGHT)
{
Export.mpPlayer.CurrentState.mLinearMomentum += Export.mpPlayer.mpCam->mStrafe*force;
}
if(Export.mKeys & SIDESTEPLEFT)
{
Export.mpPlayer.CurrentState.mLinearMomentum -= Export.mpPlayer.mpCam->mStrafe*force;
}
if(GetKeyState(VK_SPACE) & 0x80)
{
Export.mpPlayer.CurrentState.mLinearMomentum += Export.mpPlayer.mpCam->mUp * force;
}
if(GetKeyState('G') & 0x80)
{
Export.mpPlayer.CurrentState.mLinearMomentum *= 1-(10*gpFPS->mTimeFrac);
}
gpPhysicsManager->PHYSICS_RUNFRAMES();
Export.mpPlayer.mpCam->mPosition=Export.mpPlayer.CurrentState.mPosition;
Export.pRenderScene();
gpFPS->LastTimeBufferSwapped = currenttime;
}
else //Yield processor time for your ........ing aol
{
double next_frame_time = gpFPS->LastTimeBufferSwapped + gpPhysicsManager->mSecondsPerFrame;
//FIXME: Have to ........ around with this, have to change the magical fraction to
// determine the right percent that will still allow me to get the exact framerate I want
double sleep_seconds = (next_frame_time - currenttime);
double sleep_ms = SECONDS_TO_MS(sleep_seconds) * .1f;
Sleep((int)sleep_ms);
}
}