tunnel/jni/main_code.cpp

364 lines
8.5 KiB
C++

#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"
const int CONST_TUBE_COUNT = 1;
const int CONST_MAX_BOXES = 50;
TFlexModel GenerateFlexModel(int i)
{
return ResourceManager->FlexModelManager.InstanciateModel("tube"+tostr(i+1)+".xml");
}
/*
std::pair<TFlexModel, TVolumeBody> GenerateBox(int pos)
{
std::pair<TFlexModel, TVolumeBody> r;
r.first = ResourceManager->FlexModelManager.InstanciateModel("box.xml");
//r.first.ScaleModel(0.1f);
r.first.MoveModel(vec3(rand() % 800 - 400, rand() % 800 - 400, -2500.f+pos*(-100)));
r.first.RefreshBuffer();
r.second.AssignFromFlexModel(r.first);
return r;
}*/
void TMyApplication::InnerInit()
{
*Console<<"Inner init go!\n";
#ifdef TARGET_ANDROID
ResourceManager->PathToResources = "";
#endif
#ifdef TARGET_WIN32
#ifdef NDEBUG
ResourceManager->PathToResources = "resources/";
#else
ResourceManager->PathToResources = "../../../assets/";
#endif
#endif
#ifdef TARGET_IOS
ResourceManager->PathToResources = "assets/";
#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");
//ResourceManager->SoundManager.LoadMusic("level1ogg.ogg");
//ResourceManager->SoundManager.LoadSound("shot.wav");
//Renderer->MovePhi(pi/6);
Renderer->CameraMover = TPitCamera();
ResourceManager->LightManager.SetLightDirection(vec3(-1,0,-1));
ResourceManager->LightManager.SetLightColor(vec4(1,0,0,1));
ResourceManager->TexList.AddTexture("bt_box_yellow.bmp");
ResourceManager->TexList.AddTexture("tube.png");
ResourceManager->TexList.AddTexture("tube_i.png");
//ResourceManager->FlexModelManager.LoadModelFromXml("model.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("box.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("pit.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("tube1.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("tube2.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("tube3.xml");
ResourceManager->FlexModelManager.LoadModelFromXml("tube4.xml");
FlexModel = ResourceManager->FlexModelManager.InstanciateModel("pit.xml");
//FlexModel.ScaleModel(0.03f);
//FlexModel.MoveModel(vec3(0, -1.2f, -3.5f));
//FlexModel.ScaleModel(0.3f);
FlexModel.MoveModel(vec3(0, -36.f, -105.f));
FlexModel.RefreshBuffer();
TubeModelArr.resize(4);
TubeBodyArr.resize(4);
for (size_t i = 0; i < TubeModelArr.size(); ++i)
{
TubeModelArr[i] = GenerateFlexModel(rand() % CONST_TUBE_COUNT);
TubeModelArr[i].PushModelDataTriangleList();
TubeModelArr[i].MoveModel(vec3(0, 0, -2500.f*i));
TubeModelArr[i].RefreshBuffer();
TubeBodyArr[i].AssignFromFlexModel(TubeModelArr[i]);
}
/*
for (int i=0; i< CONST_MAX_BOXES; i++)
{
BoxList.push_back(GenerateBox(i));
}*/
//Uncomment to start music playing
//ResourceManager->SoundManager.PlayMusic("level1ogg.ogg");
Velocity = 4.f;
PrevCamVec = boost::get<TPitCamera>(Renderer->CameraMover).CamVec;
RotateShift = vec2(0.f, 0.f);
*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);
Renderer->PushPerspectiveProjectionMatrix(pi/6, Renderer->GetMatrixWidth() / Renderer->GetMatrixHeight(), 1.f, 4500.f);
Renderer->PushMatrix();
Renderer->SetGLCamView();
for (size_t i = 0; i < TubeModelArr.size(); ++i)
{
TubeModelArr[i].Draw();
}
/*
BOOST_FOREACH(auto& i, BoxList)
{
i.first.Draw();
}*/
Renderer->PopMatrix();
Renderer->PopProjectionMatrix();
glClear(GL_DEPTH_BUFFER_BIT);
Renderer->PushMatrix();
vec4 q;
vec3 camVec = boost::get<TPitCamera>(Renderer->CameraMover).CamVec;
vec4 camQuat = boost::get<TPitCamera>(Renderer->CameraMover).CameraQuat;
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);
q = vec4(n.v[0] * sin_a, n.v[1] * sin_a, n.v[2] * sin_a, cos_a);
Renderer->RotateMatrix(q);
FlexModel.Draw();
Renderer->PopMatrix();
}
void TMyApplication::InnerUpdate(cardinal dt)
{
vec3 camShift = boost::get<TPitCamera>(Renderer->CameraMover).CamShift;
vec3 camVec = boost::get<TPitCamera>(Renderer->CameraMover).CamVec;
camShift += camVec * (static_cast<float>(dt)*Velocity);
while (camShift.v[2] < -2500.f)
{
camShift.v[2] += 2500.f;
TubeModelArr.erase(TubeModelArr.begin());
TubeModelArr.push_back(GenerateFlexModel(rand() % CONST_TUBE_COUNT));
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();
for (size_t i = 0; i < TubeBodyArr.size(); ++i)
{
TubeBodyArr[i].AssignFromFlexModel(TubeModelArr[i]);
}
/*
BOOST_FOREACH(auto& i, BoxList)
{
i.first.Draw();
}*/
}
boost::get<TPitCamera>(Renderer->CameraMover).CamShift = camShift;
if (Velocity != 0)
{
vec3 a = boost::get<TPitCamera>(Renderer->CameraMover).CamShift;
vec3 b = a + 100.f * boost::get<TPitCamera>(Renderer->CameraMover).CamVec;
if (a != b)
{
BOOST_FOREACH(TVolumeBody& body, TubeBodyArr)
{
if (body.CheckCollision(a,b))
{
Velocity = 0.f;
}
}
/*
BOOST_FOREACH(auto& i, BoxList)
{
if (i.second.CheckCollision(a,b))
{
Velocity = 0.f;
}
}*/
}
}
UpdatePitVector(dt);
const float CONST_ROTATE_SPEED = 0.001f;
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));
boost::get<TPitCamera>(Renderer->CameraMover).RotateByQuat(quat);
boost::get<TPitCamera>(Renderer->CameraMover).CalcCamVec();
}
void TMyApplication::InnerOnMove(vec2 shift)
{
RotateShift += shift;
//float len = Length(shift);
vec3 rotation = Normalize(vec3(RotateShift, 0)); //Inverse
rotation.v[1] =-rotation.v[1]; //Inverse
vec3 z = vec3(0,0,-1);
vec4 startQuat = boost::get<TPitCamera>(Renderer->CameraMover).CameraQuat;
RotateAxis = Normalize(CrossProduct(rotation, z));
RotateAxis = startQuat * vec4(RotateAxis) * InverseQuat(startQuat);
//RotateShift = len * 0.01f / 2.f;
UpdatePitVector(0);
}
void TMyApplication::InnerOnTapUp(vec2 p)
{
RotateShift = vec2(0.f, 0.f);
RotateAxis = vec3(0,0,0);
}
void TMyApplication::InnerOnTapUpAfterShift(vec2 p)
{
RotateShift = vec2(0.f, 0.f);
RotateAxis = vec3(0,0,0);
}
void TMyApplication::OnMouseWheel(short int delta)
{
Velocity = 4.f;
boost::get<TPitCamera>(Renderer->CameraMover).CamShift = ZeroVec3;
}
void TMyApplication::UpdatePitVector(cardinal dt)
{
//Update pit vector
vec3 camVec = boost::get<TPitCamera>(Renderer->CameraMover).CamVec;
vec4 q;
vec3 n = -Normalize(CrossProduct(camVec, PrevCamVec));
float min_cos_a = min(DotProduct(camVec, PrevCamVec), 1.f);
const float diff_alpha = pi/96;
float angle = acos(min_cos_a);
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;
if (angle < omega * dt)
{
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);
}
}