#include "main_code.h" #include #include #include #include #include "include/Engine.h" #include "main_code.h" TMyApplication* Application; #define USE_SHADER_X16 #ifdef USE_SHADER_X16 std::vector linesToRenderBoxesX16(const std::vector>& lineArr) { std::vector lineRenderPairArr; size_t boxCount = lineArr.size() / 16; if (lineArr.size() % 16 > 0) { boxCount++; } lineRenderPairArr.resize(boxCount); //Set some initial values: for (size_t i = 0; i < lineRenderPairArr.size(); i++) { for (size_t j = 0; j < 16; j++) { lineRenderPairArr[i].first.Vec4Map["lineParams" + boost::lexical_cast(j)] = Vector4f(0, 0, 9999, 0); } } for (size_t i = 0; i < lineArr.size(); i++) { size_t boxIndex = i / 16; Vector2f startPoint = lineArr[i].first; Vector2f endPoint = lineArr[i].second; auto& pair = lineRenderPairArr[boxIndex]; pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0), startPoint(1), 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0), endPoint(1), 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), endPoint(1), 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0), startPoint(1), 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), endPoint(1), 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), startPoint(1), 0)); pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); Vector2f lineVec = endPoint - startPoint; Vector2f lineNormVec = { -lineVec(1), lineVec(0) }; lineNormVec.normalize(); //float distance = abs(normVec.x*(position.x - startPoint.x) + normVec.y*(position.y - startPoint.y)); //distance = a*x + b*y + c, here we calculate a, b,c and pass them to shader float a = lineNormVec(0); float b = lineNormVec(1); float c = -(lineNormVec(0)*startPoint(0) + lineNormVec(1)*startPoint(1)); pair.first.Vec4Map["lineParams" + boost::lexical_cast(i % 16)] = Vector4f(a, b, c, 0); } for (auto& pair : lineRenderPairArr) { pair.second.RefreshBuffer(); } return lineRenderPairArr; } #else std::vector linesToRenderBoxes(const std::vector>& lineArr) { std::vector lineRenderPairArr; lineRenderPairArr.resize(lineArr.size()); for (size_t i = 0; i < lineArr.size(); i++) { Vector2f startPoint = lineArr[i].first; Vector2f endPoint = lineArr[i].second; auto& pair = lineRenderPairArr[i]; pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0), startPoint(1), 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0), endPoint(1), 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), endPoint(1), 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0), startPoint(1), 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), endPoint(1), 0)); pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), startPoint(1), 0)); pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1)); Vector2f lineVec = endPoint - startPoint; Vector2f lineNormVec = { -lineVec(1), lineVec(0) }; lineNormVec.normalize(); //float distance = abs(normVec.x*(position.x - startPoint.x) + normVec.y*(position.y - startPoint.y)); //distance = a*x + b*y + c, here we calculate a, b,c and pass them to shader float a = lineNormVec(0); float b = lineNormVec(1); float c = -(lineNormVec(0)*startPoint(0) + lineNormVec(1)*startPoint(1)); pair.first.Vec4Map["lineParams"] = Vector4f(a, b, c, 0); pair.second.RefreshBuffer(); } return lineRenderPairArr; } #endif void TMyApplication::InnerInit() { 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"; } srand (static_cast(time(NULL))); ResourceManager->ShaderManager.AddShader("DefaultShader", "shader1vertex.txt", "shader1fragment.txt"); ResourceManager->ShaderManager.AddShader("ColorShader", "color_vertex.txt", "color_fragment.txt"); #ifdef USE_SHADER_X16 ResourceManager->ShaderManager.AddShader("AntialiasingShader", "ms_vertex.txt", "ms_fragment_x16.txt"); #else ResourceManager->ShaderManager.AddShader("AntialiasingShader", "ms_vertex.txt", "ms_fragment.txt"); #endif Renderer->PushShader("DefaultShader"); ResourceManager->TexList.AddTexture("console_bkg.bmp"); std::vector> lineArr; for (int i = 0; i < 10; i++) { lineArr.push_back({ {10.f + i * 20.f, 10.f}, { 20.f + i * 20.f, 400.f } }); lineArr.push_back({ { 20.f + i * 20.f, 400.f },{ 30.f + i * 20.f, 10.f } }); }; #ifdef USE_SHADER_X16 lineRenderPairArr = linesToRenderBoxesX16(lineArr); #else lineRenderPairArr = linesToRenderBoxes(lineArr); #endif Renderer->SetOrthoProjection(); Renderer->SetFullScreenViewport(); Inited = true; } void TMyApplication::InnerDeinit() { Inited = false; Loaded = false; if (Console != NULL) { *Console<<"APP DEINIT\n"; } } void TMyApplication::InnerOnTapDown(Vector2f p) { } void TMyApplication::InnerOnTapUp(Vector2f p) { } void TMyApplication::InnerOnTapUpAfterMove(Vector2f p) { } void TMyApplication::InnerOnMove(Vector2f p, Vector2f shift) { } void TMyApplication::OnFling(Vector2f v) { } void TMyApplication::InnerDraw() { //Render the frame Renderer->PushShader("AntialiasingShader"); Renderer->SetProjectionMatrix(512.f, 512.f); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); CheckGlError(""); for (auto& pair : lineRenderPairArr) { TRenderParamsSetter params(pair.first); Renderer->DrawTriangleList(pair.second); } Renderer->PopShader(); CheckGlError(""); } void TMyApplication::InnerUpdate(size_t dt) { } bool TMyApplication::IsLoaded() { return Loaded; } bool TMyApplication::IsInited() { return Inited; }