tunnel/jni/main_code.cpp

435 lines
10 KiB
C++
Raw Normal View History

2013-01-23 20:14:11 +00:00
#include "main_code.h"
#ifdef TARGET_ANDROID
#include "android_api.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "include/Engine.h"
#include "main_code.h"
2013-06-02 09:05:19 +00:00
const int CONST_TUBE_COUNT = 6;
2013-01-27 21:48:57 +00:00
const int CONST_MAX_BOXES = 50;
2013-01-29 19:20:42 +00:00
const float CONST_VELOCITY = 2.f;
2013-01-27 21:48:57 +00:00
2013-01-29 19:20:42 +00:00
int countTubes = 0;
2013-01-27 21:48:57 +00:00
2013-01-29 19:20:42 +00:00
TFlexModel GenerateFlexModel(int i)
{
countTubes++;
2013-01-27 21:48:57 +00:00
2013-01-29 19:20:42 +00:00
if (countTubes <= 5)
{
return ResourceManager->FlexModelManager.InstanciateModel("tube1.xml");
}
else
{
return ResourceManager->FlexModelManager.InstanciateModel("tube"+tostr(i+1)+".xml");
}
}
2013-01-27 21:48:57 +00:00
2013-06-02 09:05:19 +00:00
TFlexModel GeneratePhysicsModel(int i)
{
countTubes++;
if (countTubes <= 5)
{
return ResourceManager->FlexModelManager.InstanciateModel("tube1_physics.xml");
}
else
{
return ResourceManager->FlexModelManager.InstanciateModel("tube"+tostr(i+1)+"_physics.xml");
}
}
2013-01-23 20:14:11 +00:00
void TMyApplication::InnerInit()
{
*Console<<"Inner init go!\n";
#ifdef TARGET_ANDROID
2013-02-21 13:51:18 +00:00
ST::PathToResources = "";
2013-01-23 20:14:11 +00:00
#endif
#ifdef TARGET_WIN32
#ifdef NDEBUG
2013-02-21 13:51:18 +00:00
ST::PathToResources = "resources/";
2013-01-23 20:14:11 +00:00
#else
2013-02-21 13:51:18 +00:00
ST::PathToResources = "../../../assets/";
2013-01-23 20:14:11 +00:00
#endif
#endif
#ifdef TARGET_IOS
2013-02-21 13:51:18 +00:00
ST::PathToResources = "assets/";
2013-01-23 20:14:11 +00:00
#endif
ResourceManager->TexList.AddTexture(CONST_CONSOLE_TEX_NAME);
ResourceManager->ShaderManager.AddShader("DefaultShader", "gui_transparent.vertex", "gui_transparent.fragment");
Renderer->PushShader("DefaultShader");
ResourceManager->FontManager.AddFont("droid_sans14", "droid_sans14_font_bitmap.bmp32", "droid_sans14_font_charmap.txt");
ResourceManager->FontManager.PushFont("droid_sans14");
2013-01-27 21:48:57 +00:00
//ResourceManager->SoundManager.LoadMusic("level1ogg.ogg");
2013-01-23 20:14:11 +00:00
2013-01-27 21:48:57 +00:00
//ResourceManager->SoundManager.LoadSound("shot.wav");
2013-01-23 20:14:11 +00:00
//Renderer->MovePhi(pi/6);
2013-01-28 10:40:51 +00:00
Renderer->Camera = TPitCamera();
2013-01-23 20:14:11 +00:00
ResourceManager->LightManager.SetLightDirection(vec3(-1,0,-1));
ResourceManager->LightManager.SetLightColor(vec4(1,0,0,1));
2013-06-02 09:05:19 +00:00
ResourceManager->TexList.AddTexture("pit.png");
2013-01-23 20:14:11 +00:00
ResourceManager->TexList.AddTexture("tube.png");
2013-01-27 21:48:57 +00:00
ResourceManager->TexList.AddTexture("tube_i.png");
//ResourceManager->FlexModelManager.LoadModelFromXml("model.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("box.xml");
2013-01-23 20:14:11 +00:00
ResourceManager->FlexModelManager.LoadModelFromXml("pit.xml");
2013-06-02 09:05:19 +00:00
2013-01-23 20:14:11 +00:00
ResourceManager->FlexModelManager.LoadModelFromXml("tube1.xml");
2013-01-27 21:48:57 +00:00
ResourceManager->FlexModelManager.LoadModelFromXml("tube2.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("tube3.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("tube4.xml");
2013-01-29 19:20:42 +00:00
ResourceManager->FlexModelManager.LoadModelFromXml("tube5.xml");
2013-06-02 09:05:19 +00:00
ResourceManager->FlexModelManager.LoadModelFromXml("tube6.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("tube1_physics.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("tube2_physics.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("tube3_physics.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("tube4_physics.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("tube5_physics.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("tube6_physics.xml");
2013-01-23 20:14:11 +00:00
FlexModel = ResourceManager->FlexModelManager.InstanciateModel("pit.xml");
2013-02-04 07:44:34 +00:00
//FlexModel.MoveModel(vec3(0, -36.f, -105.f)); //Landscape screen
2013-06-02 09:05:19 +00:00
FlexModel.MoveModel(vec3(0, -23.f, -50.f)); //Portrait screen
FlexModel.ScaleModel(40.f);
2013-01-23 20:14:11 +00:00
FlexModel.RefreshBuffer();
2013-06-02 09:05:19 +00:00
TubeModelArr.resize(6);
TubeBodyArr.resize(6);
2013-01-23 20:14:11 +00:00
for (size_t i = 0; i < TubeModelArr.size(); ++i)
{
2013-06-02 09:05:19 +00:00
int modelNum = rand() % CONST_TUBE_COUNT;
TubeModelArr[i] = GenerateFlexModel(modelNum);
2013-01-27 21:48:57 +00:00
TubeModelArr[i].PushModelDataTriangleList();
2013-06-02 09:05:19 +00:00
TubeModelArr[i].ScaleModel(25.4f);
2013-01-27 21:48:57 +00:00
TubeModelArr[i].MoveModel(vec3(0, 0, -2500.f*i));
2013-01-23 20:14:11 +00:00
TubeModelArr[i].RefreshBuffer();
2013-01-27 21:48:57 +00:00
2013-06-02 09:05:19 +00:00
TubeBodyArr[i].AssignFromFlexModel(GeneratePhysicsModel(modelNum));
TubeBodyArr[i].Scale(25.4f);
TubeBodyArr[i].Move(vec3(0, 0, -2500.f*i));
2013-01-23 20:14:11 +00:00
}
2013-01-27 21:48:57 +00:00
/*
for (int i=0; i< CONST_MAX_BOXES; i++)
{
BoxList.push_back(GenerateBox(i));
}*/
2013-01-23 20:14:11 +00:00
//Uncomment to start music playing
//ResourceManager->SoundManager.PlayMusic("level1ogg.ogg");
2013-01-29 19:20:42 +00:00
Velocity = CONST_VELOCITY;
2013-01-27 21:48:57 +00:00
2013-01-28 10:40:51 +00:00
PrevCamVec = boost::get<TPitCamera>(Renderer->Camera).CamVec;
2013-01-27 21:48:57 +00:00
RotateShift = vec2(0.f, 0.f);
2013-01-23 20:14:11 +00:00
*Console<<"Inner init end!\n";
}
void TMyApplication::InnerDeinit()
{
}
void TMyApplication::InnerDraw()
{
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2013-06-02 09:05:19 +00:00
Renderer->PushPerspectiveProjectionMatrix(pi/6, Renderer->GetMatrixWidth() / Renderer->GetMatrixHeight(), 1.f, 8500.f);
2013-01-23 20:14:11 +00:00
Renderer->PushMatrix();
Renderer->SetGLCamView();
for (size_t i = 0; i < TubeModelArr.size(); ++i)
{
TubeModelArr[i].Draw();
}
2013-06-02 09:05:19 +00:00
2013-01-23 20:14:11 +00:00
Renderer->PopMatrix();
2013-01-28 10:40:51 +00:00
//Renderer->PopProjectionMatrix();
2013-01-23 20:14:11 +00:00
2013-01-27 21:48:57 +00:00
glClear(GL_DEPTH_BUFFER_BIT);
Renderer->PushMatrix();
vec4 q;
2013-01-28 10:40:51 +00:00
vec3 camVec = boost::get<TPitCamera>(Renderer->Camera).CamVec;
2013-01-27 21:48:57 +00:00
2013-01-28 10:40:51 +00:00
vec4 camQuat = boost::get<TPitCamera>(Renderer->Camera).CameraQuat;
2013-01-27 21:48:57 +00:00
vec3 n = Normalize(CrossProduct(camVec, PrevCamVec));
float cos_a = min(DotProduct(camVec, PrevCamVec), 1.f);
n = InverseQuat(camQuat) * vec4(n) * (camQuat);
float sin_a = sqrtf(1 - cos_a*cos_a);
2013-01-29 19:20:42 +00:00
if (sin_a > 0.001f)
{
2013-01-27 21:48:57 +00:00
2013-01-29 19:20:42 +00:00
q = vec4(n.v[0] * sin_a, n.v[1] * sin_a, n.v[2] * sin_a, cos_a);
Renderer->RotateMatrix(q);
}
2013-01-23 20:14:11 +00:00
FlexModel.Draw();
2013-01-27 21:48:57 +00:00
Renderer->PopMatrix();
2013-01-28 10:40:51 +00:00
Renderer->PopProjectionMatrix();
2013-01-27 21:48:57 +00:00
2013-01-23 20:14:11 +00:00
}
void TMyApplication::InnerUpdate(cardinal dt)
{
2013-01-28 10:40:51 +00:00
vec3 camShift = boost::get<TPitCamera>(Renderer->Camera).CamShift;
2013-01-23 20:14:11 +00:00
2013-01-28 10:40:51 +00:00
vec3 camVec = boost::get<TPitCamera>(Renderer->Camera).CamVec;
2013-01-23 20:14:11 +00:00
camShift += camVec * (static_cast<float>(dt)*Velocity);
2013-01-27 21:48:57 +00:00
while (camShift.v[2] < -2500.f)
{
camShift.v[2] += 2500.f;
TubeModelArr.erase(TubeModelArr.begin());
2013-06-02 09:05:19 +00:00
TFlexModel model;
model = GenerateFlexModel(rand() % CONST_TUBE_COUNT);
model.ScaleModel(25.4f);
TubeModelArr.push_back(model);
2013-01-27 21:48:57 +00:00
for (size_t i = 0; i < TubeModelArr.size()-1; ++i)
{
TubeModelArr[i].MoveModel(vec3(0, 0, 2500.f));
TubeModelArr[i].RefreshBuffer();
}
TubeModelArr[TubeModelArr.size()-1].MoveModel(vec3(0, 0, -2500.f*(TubeModelArr.size()-1)));
TubeModelArr[TubeModelArr.size()-1].RefreshBuffer();
2013-06-02 09:05:19 +00:00
TubeBodyArr.erase(TubeBodyArr.begin());
TVolumeBody body;
body.AssignFromFlexModel(GeneratePhysicsModel(rand() % CONST_TUBE_COUNT));
body.Scale(25.4f);
TubeBodyArr.push_back(body);
2013-01-27 21:48:57 +00:00
2013-06-02 09:05:19 +00:00
for (size_t i = 0; i < TubeBodyArr.size()-1; ++i)
2013-01-27 21:48:57 +00:00
{
2013-06-02 09:05:19 +00:00
TubeBodyArr[i].Move(vec3(0, 0, 2500.f));
2013-01-27 21:48:57 +00:00
}
2013-06-02 09:05:19 +00:00
TubeBodyArr[TubeBodyArr.size()-1].Move(vec3(0, 0, -2500.f*(TubeBodyArr.size()-1)));
2013-01-29 19:20:42 +00:00
2013-01-27 21:48:57 +00:00
}
2013-01-23 20:14:11 +00:00
2013-01-28 10:40:51 +00:00
boost::get<TPitCamera>(Renderer->Camera).CamShift = camShift;
2013-01-23 20:14:11 +00:00
if (Velocity != 0)
{
2013-01-28 10:40:51 +00:00
vec3 a = boost::get<TPitCamera>(Renderer->Camera).CamShift;
vec3 b = a + 100.f * boost::get<TPitCamera>(Renderer->Camera).CamVec;
2013-01-23 20:14:11 +00:00
if (a != b)
{
2013-01-27 21:48:57 +00:00
BOOST_FOREACH(TVolumeBody& body, TubeBodyArr)
2013-01-23 20:14:11 +00:00
{
2013-01-27 21:48:57 +00:00
if (body.CheckCollision(a,b))
{
Velocity = 0.f;
}
}
/*
BOOST_FOREACH(auto& i, BoxList)
{
if (i.second.CheckCollision(a,b))
2013-01-23 20:14:11 +00:00
{
2013-01-27 21:48:57 +00:00
Velocity = 0.f;
}
}*/
}
}
2013-01-23 20:14:11 +00:00
2013-01-29 19:20:42 +00:00
//const float CONST_ROTATE_SPEED = 0.0004f;
2013-01-27 21:48:57 +00:00
const float CONST_ROTATE_SPEED = 0.001f;
2013-01-23 20:14:11 +00:00
2013-01-27 21:48:57 +00:00
float rotateShift = Length(RotateShift) * 0.01f / 2.f;
vec4 quat = vec4(RotateAxis.v[0] * sin(rotateShift*dt*CONST_ROTATE_SPEED), RotateAxis.v[1] * sin(rotateShift*dt*CONST_ROTATE_SPEED), RotateAxis.v[2] * sin(rotateShift*dt*CONST_ROTATE_SPEED), cos(rotateShift*dt*CONST_ROTATE_SPEED));
2013-01-28 10:40:51 +00:00
boost::get<TPitCamera>(Renderer->Camera).RotateByQuat(quat);
2013-01-27 21:48:57 +00:00
2013-01-28 10:40:51 +00:00
boost::get<TPitCamera>(Renderer->Camera).CalcCamVec();
2013-01-23 20:14:11 +00:00
2013-01-29 19:20:42 +00:00
UpdatePitVector(dt);
OnUpdateSignal();
OnUpdateSignal.disconnect_all_slots();
2013-01-23 20:14:11 +00:00
}
void TMyApplication::InnerOnMove(vec2 shift)
{
2013-01-27 21:48:57 +00:00
RotateShift += shift;
2013-01-23 20:14:11 +00:00
2013-01-27 21:48:57 +00:00
//float len = Length(shift);
vec3 rotation = Normalize(vec3(RotateShift, 0)); //Inverse
2013-01-23 20:14:11 +00:00
2013-02-04 09:20:51 +00:00
//rotation.v[1] =-rotation.v[1]; //Inverse
2013-01-23 20:14:11 +00:00
vec3 z = vec3(0,0,-1);
2013-01-28 10:40:51 +00:00
vec4 startQuat = boost::get<TPitCamera>(Renderer->Camera).CameraQuat;
2013-01-23 20:14:11 +00:00
2013-01-27 21:48:57 +00:00
RotateAxis = Normalize(CrossProduct(rotation, z));
2013-01-23 20:14:11 +00:00
2013-01-27 21:48:57 +00:00
RotateAxis = startQuat * vec4(RotateAxis) * InverseQuat(startQuat);
2013-01-23 20:14:11 +00:00
2013-01-27 21:48:57 +00:00
//RotateShift = len * 0.01f / 2.f;
2013-01-23 20:14:11 +00:00
2013-01-27 21:48:57 +00:00
UpdatePitVector(0);
}
2013-01-23 20:14:11 +00:00
2013-01-27 21:48:57 +00:00
void TMyApplication::InnerOnTapUp(vec2 p)
{
RotateShift = vec2(0.f, 0.f);
2013-01-23 20:14:11 +00:00
2013-01-27 21:48:57 +00:00
RotateAxis = vec3(0,0,0);
2013-01-29 19:20:42 +00:00
if (p.v[0] < 50 && p.v[1] < 50 && Velocity == 0)
{
OnUpdateSignal.connect(boost::bind(&TMyApplication::RecreateLevel, this));
}
2013-01-27 21:48:57 +00:00
}
2013-02-06 20:40:30 +00:00
void TMyApplication::InnerOnTapUpAfterMove(vec2 p)
2013-01-27 21:48:57 +00:00
{
RotateShift = vec2(0.f, 0.f);
RotateAxis = vec3(0,0,0);
2013-01-23 20:14:11 +00:00
}
void TMyApplication::OnMouseWheel(short int delta)
{
2013-01-29 19:20:42 +00:00
OnUpdateSignal.connect(boost::bind(&TMyApplication::RecreateLevel, this));
2013-01-27 21:48:57 +00:00
}
void TMyApplication::UpdatePitVector(cardinal dt)
{
//Update pit vector
2013-01-28 10:40:51 +00:00
vec3 camVec = boost::get<TPitCamera>(Renderer->Camera).CamVec;
2013-01-27 21:48:57 +00:00
vec4 q;
vec3 n = -Normalize(CrossProduct(camVec, PrevCamVec));
float min_cos_a = min(DotProduct(camVec, PrevCamVec), 1.f);
const float diff_alpha = pi/96;
2013-01-29 19:20:42 +00:00
2013-01-27 21:48:57 +00:00
float angle = acos(min_cos_a);
2013-01-29 19:20:42 +00:00
2013-01-27 21:48:57 +00:00
if (angle >= diff_alpha)
{
//Correcting PrevCamVec;
min_cos_a = cosf(diff_alpha);
vec4 quat(-n.v[0]*sin(diff_alpha/2.f), -n.v[1]*sin(diff_alpha/2.f), -n.v[2]*sin(diff_alpha/2.f), cos(diff_alpha/2.f));
PrevCamVec = quat * vec4(camVec) * InverseQuat(quat);
}
//Catch up
const float omega = 0.0002f;
2013-01-29 19:20:42 +00:00
if (angle < omega*dt)
2013-01-27 21:48:57 +00:00
{
PrevCamVec = camVec;
}
else
{
vec4 delta_quat(n.v[0]*sin(omega * dt / 2.f), n.v[1]*sin(omega * dt / 2.f), n.v[2]*sin(omega * dt / 2.f), cos(omega * dt / 2.f));
PrevCamVec = delta_quat * vec4(PrevCamVec) * InverseQuat(delta_quat);
}
2013-01-29 19:20:42 +00:00
}
void TMyApplication::RecreateLevel()
{
Velocity = CONST_VELOCITY;
boost::get<TPitCamera>(Renderer->Camera).CamShift = ZeroVec3;
for (size_t i = 0; i < 3; ++i)
{
TubeModelArr[i] = GenerateFlexModel(0);
TubeModelArr[i].PushModelDataTriangleList();
2013-06-02 09:05:19 +00:00
TubeModelArr[i].ScaleModel(25.4f);
2013-01-29 19:20:42 +00:00
TubeModelArr[i].MoveModel(vec3(0, 0, -2500.f*i));
TubeModelArr[i].RefreshBuffer();
2013-06-02 09:05:19 +00:00
TubeBodyArr[i].AssignFromFlexModel(GeneratePhysicsModel(0));
TubeBodyArr[i].Scale(25.4f);
TubeBodyArr[i].Move(vec3(0, 0, -2500.f*i));
2013-01-29 19:20:42 +00:00
}
2013-01-23 20:14:11 +00:00
}