You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
196 lines
6.6 KiB
C++
196 lines
6.6 KiB
C++
|
4 years ago
|
/******************************************************************************
|
||
|
4 years ago
|
* File - simple_render_scene.cpp
|
||
|
4 years ago
|
* Author - Joey Pollack
|
||
|
|
* Date - 2021/10/27 (y/m/d)
|
||
|
|
* Mod Date - 2021/10/27 (y/m/d)
|
||
|
|
* Description - Displays a simple scene that tests basic render features.
|
||
|
|
******************************************************************************/
|
||
|
|
|
||
|
4 years ago
|
#include "simple_render_scene.h"
|
||
|
4 years ago
|
#include <core/core.h>
|
||
|
|
#include <utils/helpers.h>
|
||
|
|
#include <utils/logger.h>
|
||
|
4 years ago
|
#include <graphics/graphics.h>
|
||
|
4 years ago
|
#include <input/input_manager.h>
|
||
|
4 years ago
|
#include <assets/types/image.h>
|
||
|
4 years ago
|
#include <utils/stb/std_image_write.h>
|
||
|
4 years ago
|
#include <utils/stb/stb_image.h>
|
||
|
4 years ago
|
|
||
|
|
namespace lunarium
|
||
|
|
{
|
||
|
|
SimpleRenderScene::SimpleRenderScene(uint32_t logCat)
|
||
|
4 years ago
|
: BaseScene(logCat)
|
||
|
4 years ago
|
{
|
||
|
|
|
||
|
|
}
|
||
|
4 years ago
|
|
||
|
|
SimpleRenderScene::~SimpleRenderScene()
|
||
|
|
{
|
||
|
4 years ago
|
delete mpTestImageLoad;
|
||
|
4 years ago
|
}
|
||
|
|
|
||
|
4 years ago
|
void SimpleRenderScene::OnLoad()
|
||
|
|
{
|
||
|
4 years ago
|
Logger::Info(mLogCat, "Running Simple Render Test Scene");
|
||
|
4 years ago
|
mTextBoxWidth = 500;
|
||
|
|
|
||
|
4 years ago
|
|
||
|
4 years ago
|
// Currently the full default window size
|
||
|
|
mImageSize.Width = 1280;
|
||
|
|
mImageSize.Height = 720;
|
||
|
4 years ago
|
|
||
|
4 years ago
|
// Create the render textures
|
||
|
|
mFrameBufferOne = Core::Graphics().CreateRenderTexture(mImageSize.Width, mImageSize.Height, 4);
|
||
|
|
mFrameBufferTwo = Core::Graphics().CreateRenderTexture(1024, 1024, 4);
|
||
|
|
|
||
|
4 years ago
|
// Load test image
|
||
|
|
int w, h, n;
|
||
|
|
stbi_set_flip_vertically_on_load(1);
|
||
|
|
unsigned char* buffer = stbi_load("LinkToThePast1_sized.png", &w, &h, &n, 0);
|
||
|
|
|
||
|
|
mpTestImageLoad = new Image();
|
||
|
|
mpTestImageLoad->SetData(buffer);
|
||
|
|
mpTestImageLoad->SetFormat(ImageFormat::RGBA);
|
||
|
|
|
||
|
|
if (n == 3)
|
||
|
|
{
|
||
|
|
mpTestImageLoad->SetFormat(ImageFormat::RGB);
|
||
|
|
}
|
||
|
|
|
||
|
|
mpTestImageLoad->SetWidth(w);
|
||
|
|
mpTestImageLoad->SetHeight(h);
|
||
|
|
|
||
|
4 years ago
|
mSrcWidth = w;
|
||
|
|
mSrcHeight = h;
|
||
|
|
|
||
|
4 years ago
|
Core::Graphics().RegisterImage(*mpTestImageLoad);
|
||
|
|
|
||
|
4 years ago
|
angle = 0.0f;
|
||
|
4 years ago
|
box_angle = 0.0f;
|
||
|
4 years ago
|
}
|
||
|
|
|
||
|
|
void SimpleRenderScene::OnTick(double delta)
|
||
|
|
{
|
||
|
|
if (Core::Input().IsKeyDown(KeyCode::ESCAPE))
|
||
|
|
{
|
||
|
|
Core::GetInstance().SignalShutdown();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Textbox size adjustment
|
||
|
|
if (Core::Input().IsKeyDown(KeyCode::LEFT))
|
||
|
|
{
|
||
|
|
mTextBoxWidth -= 10;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Core::Input().IsKeyDown(KeyCode::RIGHT))
|
||
|
|
{
|
||
|
|
mTextBoxWidth += 10;
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
if (Core::Input().IsKeyDown(KeyCode::R))
|
||
|
|
{
|
||
|
|
mSrcWidth -= 10.0f;
|
||
|
|
mSrcHeight -= 10.0f;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Core::Input().IsKeyDown(KeyCode::F))
|
||
|
|
{
|
||
|
|
mSrcWidth += 10.0f;
|
||
|
|
mSrcHeight += 10.0f;
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
mTextBoxWidth = Math::ClampI(mTextBoxWidth, 20, 500);
|
||
|
|
|
||
|
|
// Image Size adjustment
|
||
|
|
if (Core::Input().IsKeyDown(KeyCode::DOWN))
|
||
|
|
{
|
||
|
|
mImageSize.Width -= 10;
|
||
|
|
mImageSize.Height -= 10;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Core::Input().IsKeyDown(KeyCode::UP))
|
||
|
|
{
|
||
|
|
mImageSize.Width += 10;
|
||
|
|
mImageSize.Height += 10;
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
if (Core::Input().IsKeyDown(KeyCode::PAGE_UP))
|
||
|
|
{
|
||
|
|
angle += 0.1f;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Core::Input().IsKeyDown(KeyCode::PAGE_DOWN))
|
||
|
|
{
|
||
|
|
angle -= 0.1f;
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
if (Core::Input().IsKeyPressed(KeyCode::Q))
|
||
|
|
{
|
||
|
|
// Test writing out a rendered image with transparency
|
||
|
|
IGraphics& g = Core::Graphics();
|
||
|
|
Color prev = g.GetClearColor();
|
||
|
|
g.SetClearColor(Color(0.0f, 0.0f, 0.0f, 0.0f));
|
||
|
4 years ago
|
OpRes result = Core::GetInstance().BeginRenderToTexture(mFrameBufferTwo);
|
||
|
4 years ago
|
if (Failed(result))
|
||
|
|
{
|
||
|
4 years ago
|
Logger::Warn(mLogCat, "Unable to render to texture: %s", result.Description.c_str());
|
||
|
4 years ago
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
Logger::Info(mLogCat, "Running transparent image test");
|
||
|
4 years ago
|
|
||
|
|
g.DrawFilledBox(Rectangle(500, 400, 300, 300), Color(0.5f, 0.0f, 0.75f, 1.0f));
|
||
|
|
g.DrawString("This is a test of rendering an image with transparency",
|
||
|
|
Rectangle::MakeFromTopLeft(50, 50, 600, 200), Color(0.0f, 1.0f, 0.2f, 1.0f), 0.5f);
|
||
|
|
|
||
|
|
mpRenderedImage = Core::GetInstance().EndRenderToTexture();
|
||
|
|
g.SetClearColor(prev);
|
||
|
|
|
||
|
|
stbi_flip_vertically_on_write(1);
|
||
|
|
stbi_write_png("lunarium_test_image.png", mpRenderedImage->GetWidth(), mpRenderedImage->GetHeight(), 4,
|
||
|
|
mpRenderedImage->GetData(), mpRenderedImage->GetWidth() * 4);
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
mImageSize.Width = Math::ClampI(mImageSize.Width, 320, 1280);
|
||
|
|
mImageSize.Height = Math::ClampI(mImageSize.Height, 180, 720);
|
||
|
|
|
||
|
|
// Render to texture testing
|
||
|
4 years ago
|
OpRes result = Core::GetInstance().BeginRenderToTexture(mFrameBufferOne);
|
||
|
4 years ago
|
if (Failed(result))
|
||
|
|
{
|
||
|
4 years ago
|
Logger::Error(mLogCat, "Unable to render to texture: %s", result.Description.c_str());
|
||
|
4 years ago
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
IGraphics& g = Core::Graphics();
|
||
|
|
|
||
|
|
g.DrawFilledEllipse(glm::vec2(600, 300), glm::vec2(100, 150), Color(1.0f, 0.0f, 1.0f, 1.0f), 100);
|
||
|
4 years ago
|
g.DrawString("This is a test of the text renderer!", Rectangle::MakeFromTopLeft(100, 200, mTextBoxWidth, 300),
|
||
|
4 years ago
|
Color(0.0f, 1.0f, 1.0f, 1.0f), 0.5f, g.DefaultFont());
|
||
|
|
|
||
|
|
mpRenderedImage = Core::GetInstance().EndRenderToTexture();
|
||
|
4 years ago
|
|
||
|
|
box_angle += 0.01f;
|
||
|
4 years ago
|
}
|
||
|
|
|
||
|
|
void SimpleRenderScene::OnRender(IGraphics* g)
|
||
|
|
{
|
||
|
4 years ago
|
g->DrawImage(*mpRenderedImage, Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mpRenderedImage->GetWidth(), (float)mpRenderedImage->GetHeight()),
|
||
|
4 years ago
|
Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(1.0f, 1.0f, 1.0f, 1.0f), angle);
|
||
|
4 years ago
|
|
||
|
4 years ago
|
g->DrawBox(Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mImageSize.Width, (float)mImageSize.Height), Color(0.0f, 0.0f, 0.0f, 1.0f), 1.0f, angle);
|
||
|
4 years ago
|
|
||
|
4 years ago
|
g->DrawBox(Rectangle(400, 400, 128.0f, 128.0f), Color(0.0f, 1.0f, 0.0f, 1.0f), 2.0f, box_angle);
|
||
|
4 years ago
|
|
||
|
4 years ago
|
//g->DrawImage(*mpTestImageLoad, glm::vec2(0.0f, 0.0f), Color::White());
|
||
|
4 years ago
|
//Rectangle src = Rectangle::MakeFromTopLeft(0.0f, 0.0f, (float)mpTestImageLoad->GetWidth(), (float)mpTestImageLoad->GetHeight());
|
||
|
|
//Rectangle src = Rectangle::MakeFromTopLeft(0.0f, 0.0f, mSrcWidth, mSrcHeight);
|
||
|
|
Rectangle src = Rectangle::MakeFromTopLeft(0.0f, 0.0f, 16, 16);
|
||
|
|
Rectangle dest = Rectangle::MakeFromTopLeft(100.0f, 100.0f, 512.0f, 512.0f);
|
||
|
|
g->DrawImage(*mpTestImageLoad, src,
|
||
|
|
dest, Color(1.0f, 1.0f, 1.0f, 0.9f));
|
||
|
|
|
||
|
|
// g->DrawImage(*mpTestImageLoad, src,
|
||
|
|
// dest, Color(1.0f, 1.0f, 1.0f, 0.8f));
|
||
|
4 years ago
|
}
|
||
|
|
}
|