double-hit-balls/game/main_code.cpp

353 lines
10 KiB
C++

#include "main_code.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "include/Engine.h"
#include "main_code.h"
TMyApplication* Application;
#define AREATEX_WIDTH 160
#define AREATEX_HEIGHT 560
#define SEARCHTEX_WIDTH 66
#define SEARCHTEX_HEIGHT 33
GLuint area_tex;
GLuint search_tex;
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<size_t>(time(NULL)));
ResourceManager->ShaderManager.AddShader("DefaultShader", "shader1vertex.txt", "shader1fragment.txt");
ResourceManager->ShaderManager.AddShader("FrameShader", "frameshader_vertex.txt", "frameshader_fragment.txt");
ResourceManager->ShaderManager.AddShader("ColorShader", "color_vertex.txt", "color_fragment.txt");
ResourceManager->ShaderManager.AddShader("SmaaEdge", "smaa/edge_vertex.txt", "smaa/edge_fragment.txt");
ResourceManager->ShaderManager.AddShader("SmaaBlend", "smaa/blend_vertex.txt", "smaa/blend_fragment.txt");
ResourceManager->ShaderManager.AddShader("SmaaNeighborhood", "smaa/neighborhood_vertex.txt", "smaa/neighborhood_fragment.txt");
Renderer->PushShader("DefaultShader");
ResourceManager->TexList.AddTexture("console_bkg.bmp");
ResourceManager->FrameManager.AddFrameRenderBuffer("AlbedoBuffer", 512, 512);
ResourceManager->FrameManager.AddFrameRenderBuffer("EdgeBuffer", 512, 512);
ResourceManager->FrameManager.AddFrameRenderBuffer("BlendBuffer", 512, 512);
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 512, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 0, 0));
//Choose colored or black and white -- Vladislav Khorev vladislav.khorev@fishrungames.com
#if 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(0, 1, 0, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 1, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 1, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 1, 0, 1));
#else
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 1, 1, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 1, 1, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 1, 1, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 0, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 0, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 0, 1));
#endif
pair.first.ShaderName = "ColorShader";
pair.second.RefreshBuffer();
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();
glDisable(GL_BLEND); //Don't forget to disable blending or whole thing will not work!!! -- Vladislav Khorev vladislav.khorev@fishrungames.com
Inited = true;
//This is stuff from original https://github.com/scrawl/smaa-opengl
//We must have these textures to be used for pattern matching or something -- Vladislav Khorev vladislav.khorev@fishrungames.com
unsigned char* buffer = 0;
FILE* f = 0;
buffer = new unsigned char[1024 * 1024];
f = fopen((ST::PathToResources + "smaa/smaa_area.raw").c_str(), "rb"); //rb stands for "read binary file"
if (!f)
{
std::cerr << "Couldn't open smaa_area.raw.\n";
}
fread(buffer, AREATEX_WIDTH * AREATEX_HEIGHT * 2, 1, f);
fclose(f);
f = 0;
glGenTextures(1, &area_tex);
glBindTexture(GL_TEXTURE_2D, area_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, (GLsizei)AREATEX_WIDTH, (GLsizei)AREATEX_HEIGHT, 0, GL_RG, GL_UNSIGNED_BYTE, buffer);
f = fopen((ST::PathToResources + "smaa/smaa_search.raw").c_str(), "rb");
if (!f)
{
std::cerr << "Couldn't open smaa_search.raw.\n";
}
fread(buffer, SEARCHTEX_WIDTH * SEARCHTEX_HEIGHT, 1, f);
fclose(f);
f = 0;
glGenTextures(1, &search_tex);
glBindTexture(GL_TEXTURE_2D, search_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, (GLsizei)SEARCHTEX_WIDTH, (GLsizei)SEARCHTEX_HEIGHT, 0, GL_RED, GL_UNSIGNED_BYTE, buffer);
delete[] buffer;
}
void TMyApplication::InnerDeinit()
{
Inited = false;
Loaded = false;
if (Console != NULL)
{
*Console<<"APP DEINIT\n";
}
glDeleteTextures(1, &search_tex);
glDeleteTextures(1, &area_tex);
}
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->SwitchToFrameBuffer("AlbedoBuffer");
Renderer->SetProjectionMatrix(512.f, 512.f);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
CheckGlError("");
{
//Render here all objects that you want to have -- Vladislav Khorev vladislav.khorev@fishrungames.com
TRenderParamsSetter params(pair.first);
Renderer->DrawTriangleList(pair.second);
}
CheckGlError("");
//-------------------------
//First pass - edge detection
Renderer->PushShader("SmaaEdge");
Renderer->SwitchToFrameBuffer("EdgeBuffer");
Renderer->SetProjectionMatrix(512.f, 512.f);
RenderUniform1i("albedo_tex", 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("AlbedoBuffer"));
Renderer->DrawTriangleList(rect.second);
Renderer->PopShader();
CheckGlError("");
//-------------------------
//Second pass - blend plus pattern matching
Renderer->PushShader("SmaaBlend");
Renderer->SwitchToFrameBuffer("BlendBuffer");
Renderer->SetProjectionMatrix(512.f, 512.f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
RenderUniform1i("edge_tex", 0);
RenderUniform1i("area_tex", 1);
RenderUniform1i("search_tex", 2);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("EdgeBuffer"));
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, area_tex);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, search_tex);
Renderer->DrawTriangleList(rect.second);
Renderer->PopShader();
CheckGlError("");
#if 1
//-------------------------
//Last pass - neightborhood
Renderer->PushShader("SmaaNeighborhood");
Renderer->SwitchToScreen();
Renderer->SetFullScreenViewport();
Renderer->SetProjectionMatrix(512.f, 512.f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
RenderUniform1i("albedo_tex", 0);
RenderUniform1i("blend_tex", 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("BlendBuffer"));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("AlbedoBuffer"));
Renderer->DrawTriangleList(rect.second);
Renderer->PopShader();
CheckGlError("");
#else
//... or just render intermediate buffers to see what is going on
Renderer->PushShader("DefaultShader");
Renderer->SwitchToScreen();
Renderer->SetFullScreenViewport();
Renderer->SetProjectionMatrix(512.f, 512.f);
glActiveTexture(GL_TEXTURE0);
//Choose any to render for debug:
//glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("AlbedoBuffer"));
//glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("EdgeBuffer"));
glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("BlendBuffer"));
Renderer->DrawTriangleList(rect.second);
Renderer->PopShader();
CheckGlError("");
#endif
}
void TMyApplication::InnerUpdate(size_t dt)
{
}
bool TMyApplication::IsLoaded()
{
return Loaded;
}
bool TMyApplication::IsInited()
{
return Inited;
}