double-hit-balls/game/main_code.cpp

430 lines
13 KiB
C++
Raw Permalink Normal View History

2017-01-10 12:43:06 +00:00
#include "main_code.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "include/Engine.h"
#include "main_code.h"
2017-11-14 17:04:04 +00:00
TMyApplication* Application;
2017-01-10 12:43:06 +00:00
2017-11-14 17:04:04 +00:00
2017-12-11 09:30:51 +00:00
Matrix3f quatToMatrix(Vector4f q) {
Matrix3f result;
double sqw = q(3)*q(3);
double sqx = q(0)*q(0);
double sqy = q(1)*q(1);
double sqz = q(2)*q(2);
// invs (inverse square length) is only required if quaternion is not already normalised
double invs = 1 / (sqx + sqy + sqz + sqw);
result(0,0) = (sqx - sqy - sqz + sqw)*invs; // since sqw + sqx + sqy + sqz =1/invs*invs
result(1,1) = (-sqx + sqy - sqz + sqw)*invs;
result(2,2) = (-sqx - sqy + sqz + sqw)*invs;
double tmp1 = q(0)*q(1);
double tmp2 = q(2)*q(3);
result(1, 0) = 2.0 * (tmp1 + tmp2)*invs;
result(0, 1) = 2.0 * (tmp1 - tmp2)*invs;
tmp1 = q(0)*q(2);
tmp2 = q(1)*q(3);
result(2,0) = 2.0 * (tmp1 - tmp2)*invs;
result(0,2) = 2.0 * (tmp1 + tmp2)*invs;
tmp1 = q(1)*q(2);
tmp2 = q(0)*q(3);
result(2, 1) = 2.0 * (tmp1 + tmp2)*invs;
result(1, 2) = 2.0 * (tmp1 - tmp2)*invs;
return result;
}
2018-01-21 21:05:15 +00:00
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerInit()
2017-01-10 12:43:06 +00:00
{
Application = this;
#ifdef TARGET_WIN32
#ifdef NDEBUG
ST::PathToResources = "resources/";
#else
ST::PathToResources = "../../../assets/";
#endif
#endif
#ifdef TARGET_IOS
ST::PathToResources = "assets/";
#endif
if (Console != NULL)
{
*Console<<"APP INIT\n";
}
2017-11-14 17:04:04 +00:00
srand (static_cast<size_t>(time(NULL)));
2017-01-10 12:43:06 +00:00
ResourceManager->ShaderManager.AddShader("DefaultShader", "shader1vertex.txt", "shader1fragment.txt");
ResourceManager->ShaderManager.AddShader("FrameShader", "frameshader_vertex.txt", "frameshader_fragment.txt");
2017-12-02 10:25:02 +00:00
ResourceManager->ShaderManager.AddShader("ColorShader", "color_vertex.txt", "color_fragment.txt");
ResourceManager->ShaderManager.AddShader("SSAA_4X", "SSAA_4X.vertex", "SSAA_4X.frag");
2017-12-11 09:30:51 +00:00
ResourceManager->ShaderManager.AddShader("ParallaxShader", "parallax_vertex.txt", "parallax_fragment.txt");
2018-01-21 21:05:15 +00:00
ResourceManager->ShaderManager.AddShader("PhongShader", "phong_vertex.txt", "phong_fragment.txt");
2017-01-10 12:43:06 +00:00
Renderer->PushShader("DefaultShader");
2017-11-14 17:04:04 +00:00
2018-01-21 21:05:15 +00:00
2017-01-10 12:43:06 +00:00
ResourceManager->TexList.AddTexture("console_bkg.bmp");
2018-01-19 14:23:11 +00:00
ResourceManager->TexList.AddTexture("background.jpg");
ResourceManager->TexList.AddTexture("HeightMap.png");
ResourceManager->TexList.AddTexture("NormalMap.png");
ResourceManager->TexList.AddTexture("linesAll.png");
2017-11-14 17:04:04 +00:00
2018-01-21 21:05:15 +00:00
Vector2f const bottomLeft(-500, -500);
float const W = 1000;
float const H = 1000;
Vector2f const backgroundBottomLeft(-1000, -1000);
float const backgroundW = 2000;
float const backgroundH = 2000;
2018-01-19 14:23:11 +00:00
{
//resolution of background image
float const imageW = 512;
float const imageH = 512;
2017-12-04 11:14:14 +00:00
2018-01-21 21:05:15 +00:00
background.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].emplace_back(Vector3f(backgroundBottomLeft[0], 0, backgroundBottomLeft[1]));
background.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].emplace_back(Vector3f(backgroundBottomLeft[0], 0, backgroundBottomLeft[1] + backgroundH));
background.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].emplace_back(Vector3f(backgroundBottomLeft[0] + backgroundW, 0, backgroundBottomLeft[1] + backgroundH));
2018-01-19 14:23:11 +00:00
2018-01-21 21:05:15 +00:00
background.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].emplace_back(Vector3f(backgroundBottomLeft[0] + backgroundW, 0, backgroundBottomLeft[0] + backgroundH));
background.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].emplace_back(Vector3f(backgroundBottomLeft[0] + backgroundW, 0, backgroundBottomLeft[0]));
background.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].emplace_back(Vector3f(backgroundBottomLeft[0], 0, backgroundBottomLeft[0]));
2017-12-04 11:14:14 +00:00
2018-01-19 14:23:11 +00:00
float const tw = W / imageW;
float const th = H / imageH;
2017-12-04 11:14:14 +00:00
2018-01-19 14:23:11 +00:00
background.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].emplace_back(Vector2f(0, th));
background.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].emplace_back(Vector2f(0, 0));
background.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].emplace_back(Vector2f(tw, 0));
background.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].emplace_back(Vector2f(tw, 0));
background.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].emplace_back(Vector2f(tw, th));
background.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].emplace_back(Vector2f(0, th));
2017-12-04 11:14:14 +00:00
2018-01-19 14:23:11 +00:00
background.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].emplace_back(Vector4f(1, 1, 1, 1));
background.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].emplace_back(Vector4f(1, 1, 1, 1));
background.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].emplace_back(Vector4f(1, 1, 1, 1));
2017-12-04 11:14:14 +00:00
2018-01-19 14:23:11 +00:00
background.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].emplace_back(Vector4f(1, 1, 1, 1));
background.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].emplace_back(Vector4f(1, 1, 1, 1));
background.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].emplace_back(Vector4f(1, 1, 1, 1));
}
2017-01-10 12:43:06 +00:00
2018-01-19 14:23:11 +00:00
{
//resolution of linesAll.png
float const texW = 900;
float const texH = 900;
2017-01-10 12:43:06 +00:00
2018-01-19 14:23:11 +00:00
float const step = 12;
float const thick = 10;
2017-12-02 10:25:02 +00:00
2018-01-21 21:05:15 +00:00
auto f = [this, texW, texH, thick, W, H] (const Vector3f &p1, const Vector3f &p2, float t) {
2018-01-19 14:23:11 +00:00
auto pv = p2 - p1;
Vector3f ortho(pv[2], 0, pv[0]);
ortho.normalize();
auto p3 = p2 + ortho * thick;
auto p4 = p1 + ortho * thick;
2017-12-04 11:14:14 +00:00
2018-01-21 21:05:15 +00:00
float extraHeight = std::min(20.f, -(t - 1)*t*100.f);
const size_t segmentCount = 20;
for (int i = 0; i < segmentCount; i++)
{
auto pStart = p1 + pv * i/ segmentCount;
auto pEnd = p1 + pv * (i+1)/ segmentCount;
auto pStartShifted = pStart + ortho * thick;
auto pEndShifted = pEnd + ortho * thick;
float innerHeightStart = std::min(20.f, -(i / 20.f) * (i / 20.f - 1.f)*100.f);
float innerHeightEnd = std::min(20.f, -((i+1) / 20.f) * ((i+1) / 20.f - 1.f)*100.f);
Vector3f extraShiftStart = { 0, std::min(innerHeightStart, extraHeight), 0 };
Vector3f extraShiftEnd = { 0, std::min(innerHeightEnd, extraHeight), 0 };
fabricRender.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(pStart + extraShiftStart);
fabricRender.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(pEnd + extraShiftEnd);
fabricRender.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(pEndShifted + extraShiftEnd);
fabricRender.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(pEndShifted + extraShiftEnd);
fabricRender.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(pStartShifted + extraShiftStart);
fabricRender.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(pStart + extraShiftStart);
auto texThick = thick / texW;
auto m = (pStart[0] + pEnd[0]) / 2 / texW;
auto yStart = (H / texH)*(i / 20.f);
auto yEnd = (H / texH)*((i+1) / 20.f);
fabricRender.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(m, yStart));
fabricRender.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(m, yEnd));
fabricRender.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(m + texThick, yEnd));
fabricRender.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(m + texThick, yEnd));
fabricRender.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(m + texThick, yStart));
fabricRender.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(m, yStart));
}
2018-01-18 15:29:21 +00:00
2018-01-19 14:23:11 +00:00
};
2018-01-18 15:29:21 +00:00
2018-01-19 14:23:11 +00:00
Vector3f const stepDirection(1, 0, 0);
Vector3f p1(bottomLeft[0], 0, -bottomLeft[1]);
2018-01-18 15:29:21 +00:00
2018-01-19 14:23:11 +00:00
while (p1[0] < bottomLeft[0] + W) {
2017-12-02 10:25:02 +00:00
2018-01-21 21:05:15 +00:00
float t = (p1(0) - bottomLeft(0)) / W;
2018-01-19 14:23:11 +00:00
auto p2 = p1 - Vector3f(0, 0, W);
2018-01-21 21:05:15 +00:00
f(p1, p2, t);
2017-12-02 10:25:02 +00:00
2018-01-19 14:23:11 +00:00
auto p3 = p1 + step * stepDirection;
2018-01-21 21:05:15 +00:00
f(p3, p2, t);
2018-01-19 14:23:11 +00:00
p1 = p3;
}
2018-01-18 15:29:21 +00:00
2018-01-19 14:23:11 +00:00
}
2018-01-18 15:29:21 +00:00
2018-01-21 21:05:15 +00:00
//background.first.ShaderName ="ParallaxShader";
//DefaultShader
background.first.ShaderName = "DefaultShader";
2018-01-19 14:23:11 +00:00
fabricRender.first.ShaderName = "ParallaxShader";
2018-01-21 21:05:15 +00:00
//fabricRender.first.ShaderName = "PhongShader";
2018-01-18 15:29:21 +00:00
/*
* Line below should be in tes-engine/include/ShaderManager/ShaderManager.h
*/
std::string const CONST_STRING_HEIGHTMAP_UNIFORM = "HeightMap";
2018-01-19 14:23:11 +00:00
background.first.SamplerMap[CONST_STRING_TEXTURE_UNIFORM] = "background.jpg";
2018-01-18 15:29:21 +00:00
2018-01-19 14:23:11 +00:00
fabricRender.first.SamplerMap[CONST_STRING_NORMALMAP_UNIFORM] = "NormalMap.png";
fabricRender.first.SamplerMap[CONST_STRING_HEIGHTMAP_UNIFORM] = "HeightMap.png";
fabricRender.first.SamplerMap[CONST_STRING_TEXTURE_UNIFORM] = "linesAll.png";
2018-01-18 15:29:21 +00:00
2018-01-19 14:23:11 +00:00
background.second.RefreshBuffer();
fabricRender.second.RefreshBuffer();
2018-01-21 21:05:15 +00:00
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0));
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 512, 0));
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0));
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0));
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 0, 0));
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0));
rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 0));
rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 1));
rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(1, 1));
rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(1, 1));
rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(1, 0));
rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 0));
rect.second.RefreshBuffer();
Renderer->SetOrthoProjection();
Renderer->SetFullScreenViewport();
2018-01-19 14:23:11 +00:00
2018-01-18 15:29:21 +00:00
Inited = true;
2017-01-10 12:43:06 +00:00
}
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerDeinit()
2017-01-10 12:43:06 +00:00
{
Inited = false;
Loaded = false;
if (Console != NULL)
{
*Console<<"APP DEINIT\n";
}
}
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerOnTapDown(Vector2f p)
{
2017-12-04 11:14:14 +00:00
2017-01-10 12:43:06 +00:00
}
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerOnTapUp(Vector2f p)
{
2017-12-04 11:14:14 +00:00
2017-01-10 12:43:06 +00:00
}
2017-02-26 23:07:37 +00:00
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerOnTapUpAfterMove(Vector2f p)
2017-02-26 23:07:37 +00:00
{
2017-12-04 11:14:14 +00:00
2017-02-26 23:07:37 +00:00
}
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerOnMove(Vector2f p, Vector2f shift)
2017-01-10 12:43:06 +00:00
{
2017-12-11 09:30:51 +00:00
phi += shift(1)*0.02f;
2017-12-04 11:14:14 +00:00
2017-12-11 09:30:51 +00:00
if (phi < pi/12)
{
phi = pi / 12;
}
if (phi > pi / 2)
{
phi = pi / 2;
}
alpha -= shift(0)*0.02f;
2017-01-10 12:43:06 +00:00
}
2017-11-14 17:04:04 +00:00
void TMyApplication::OnFling(Vector2f v)
2017-01-10 12:43:06 +00:00
{
}
2017-12-11 09:30:51 +00:00
void TMyApplication::OnMouseWheel(short int delta)
{
distance += delta;
if (distance > 2500)
{
distance = 2500;
}
if (distance < 100)
{
distance = 100;
}
}
2017-12-04 11:14:14 +00:00
void TMyApplication::InnerDraw()
2018-01-21 21:05:15 +00:00
{
DrawScene();
}
void TMyApplication::DrawScene()
2017-01-10 12:43:06 +00:00
{
2017-12-11 09:30:51 +00:00
Renderer->SetPerspectiveProjection(pi / 6, 10.f, 10000.f);
2017-01-10 12:43:06 +00:00
2017-12-11 09:30:51 +00:00
Renderer->SetFullScreenViewport();
2017-01-10 12:43:06 +00:00
2017-12-11 09:30:51 +00:00
Renderer->PushMatrix();
2017-01-10 12:43:06 +00:00
2017-12-11 09:30:51 +00:00
Renderer->TranslateMatrix(Vector3f(0, 0, -distance));
2017-01-10 12:43:06 +00:00
2017-12-11 09:30:51 +00:00
Vector4f quat1 = Vector4f(sin(phi / 2), 0, 0, cos(phi / 2));
Vector4f quat2 = Vector4f(0, sin(alpha / 2), 0, cos(alpha / 2));
2017-01-10 12:43:06 +00:00
2017-12-11 09:30:51 +00:00
Renderer->RotateMatrix(quat1);
2017-01-10 12:43:06 +00:00
2017-12-11 09:30:51 +00:00
Renderer->RotateMatrix(quat2);
2017-01-10 12:43:06 +00:00
2017-12-11 09:30:51 +00:00
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
2018-01-19 14:23:11 +00:00
glDisable(GL_DEPTH_TEST);
2017-12-11 09:30:51 +00:00
CheckGlError("");
auto mat1 = quatToMatrix(quat1);
auto mat2 = quatToMatrix(quat2);
2017-01-10 12:43:06 +00:00
2018-01-21 21:05:15 +00:00
Vector3f lightPos = { 0.f, -1.f, -1.f };
2017-12-11 09:30:51 +00:00
2018-01-19 14:23:11 +00:00
Vector3f eye = mat2 * mat1 * Vector3f(0.0f, 0.f, -distance);
{
TRenderParamsSetter params(background.first);
RenderUniform3fv("eye", eye.data());
RenderUniform3fv("lightPos", lightPos.data());
Matrix3f normMatrix = Renderer->GetModelviewMatrix().inverse().transpose().block<3, 3>(0, 0);
2017-01-10 12:43:06 +00:00
2018-01-19 14:23:11 +00:00
RenderUniformMatrix3fv("NormalMatrix", false, normMatrix.data());
RenderUniformMatrix4fv("ModelViewMatrix", false, Renderer->GetModelviewMatrix().data());
RenderUniformMatrix3fv("ModelViewMatrix3x3", false, Renderer->GetModelviewMatrix().block<3, 3>(0, 0).data());
2017-01-10 12:43:06 +00:00
2018-01-19 14:23:11 +00:00
Renderer->DrawTriangleList(background.second);
}
2017-01-10 12:43:06 +00:00
{
2018-01-19 14:23:11 +00:00
TRenderParamsSetter params(fabricRender.first);
2017-12-11 09:30:51 +00:00
2018-01-21 21:05:15 +00:00
//RenderUniform3fv("cameraPos", eye.data());
2017-12-11 09:30:51 +00:00
RenderUniform3fv("eye", eye.data());
RenderUniform3fv("lightPos", lightPos.data());
2017-01-10 12:43:06 +00:00
2018-01-21 21:05:15 +00:00
//RenderUniform3fv("ambientLight", Vector3f(0.0f, 0.0f, 0.0f).data());
//RenderUniform3fv("specularLight", Vector3f(0.0f, 0.0f, 0.0f).data());
//RenderUniform3fv("ambientLight", Vector3f(0.0f, 0.0f, 0.0f).data());
//RenderUniform1f("specularPower", 20.f);
Matrix3f normMatrix = Renderer->GetModelviewMatrix().inverse().transpose().block<3, 3>(0, 0);
2017-12-11 09:30:51 +00:00
RenderUniformMatrix3fv("NormalMatrix", false, normMatrix.data());
RenderUniformMatrix4fv("ModelViewMatrix", false, Renderer->GetModelviewMatrix().data());
2018-01-21 21:05:15 +00:00
RenderUniformMatrix3fv("ModelViewMatrix3x3", false, Renderer->GetModelviewMatrix().block<3, 3>(0, 0).data());
2017-12-11 09:30:51 +00:00
2018-01-21 21:05:15 +00:00
Renderer->DrawTriangleList(fabricRender.second);
2017-01-10 12:43:06 +00:00
}
2018-01-19 14:23:11 +00:00
2017-12-11 09:30:51 +00:00
Renderer->PopMatrix();
2017-01-10 12:43:06 +00:00
2017-12-04 11:14:14 +00:00
CheckGlError("");
2018-01-21 21:05:15 +00:00
2017-01-10 12:43:06 +00:00
}
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerUpdate(size_t dt)
2017-01-10 12:43:06 +00:00
{
}
2017-11-14 17:04:04 +00:00
bool TMyApplication::IsLoaded()
2017-01-10 12:43:06 +00:00
{
return Loaded;
}
2017-11-14 17:04:04 +00:00
bool TMyApplication::IsInited()
2017-01-10 12:43:06 +00:00
{
return Inited;
}