|
|
|
|
@ -58,7 +58,8 @@ namespace lunarium
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mpWindow = pWindow;
|
|
|
|
|
ResizeCanvas();
|
|
|
|
|
mpFBTexture = new Image;
|
|
|
|
|
ResizeCanvas(); // Also calls InitTextureFrameBuffer
|
|
|
|
|
|
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
@ -104,26 +105,59 @@ namespace lunarium
|
|
|
|
|
void OglGraphics::ResizeCanvas()
|
|
|
|
|
{
|
|
|
|
|
// Get viewport size from glfw window
|
|
|
|
|
int width = 0;
|
|
|
|
|
int height = 0;
|
|
|
|
|
glfwGetFramebufferSize(mpWindow->GetWindow(), &width, &height);
|
|
|
|
|
glfwGetFramebufferSize(mpWindow->GetWindow(), &mFBWidth, &mFBHeight);
|
|
|
|
|
|
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
|
mProjection = glm::ortho(0.0f, (GLfloat)width, (GLfloat)height, 0.0f, -1.0f, 1.0f);
|
|
|
|
|
glViewport(0, 0, mFBWidth, mFBHeight);
|
|
|
|
|
mProjection = glm::ortho(0.0f, (GLfloat)mFBWidth, (GLfloat)mFBHeight, 0.0f, -1.0f, 1.0f);
|
|
|
|
|
|
|
|
|
|
Logger::Log(LogCategory::GRAPHICS, LogLevel::INFO_VERBOSE,
|
|
|
|
|
"glViewport set to %d, %d", width, height);
|
|
|
|
|
"glViewport set to %d, %d", mFBWidth, mFBHeight);
|
|
|
|
|
|
|
|
|
|
InitTextureFrameBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OglGraphics::BeginDraw()
|
|
|
|
|
void OglGraphics::BeginDraw(RenderTarget rt)
|
|
|
|
|
{
|
|
|
|
|
mRT = rt;
|
|
|
|
|
|
|
|
|
|
if (mRT == RenderTarget::RT_IMAGE)
|
|
|
|
|
{
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OglGraphics::EndDraw()
|
|
|
|
|
Image* OglGraphics::EndDraw()
|
|
|
|
|
{
|
|
|
|
|
if (mRT == RenderTarget::RT_IMAGE)
|
|
|
|
|
{
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mpFBTexture->mGLTextureID);
|
|
|
|
|
|
|
|
|
|
// Buffer width and height (in pixels) is the same as the screen size
|
|
|
|
|
// Need to multiply these by the number bytes per pixel
|
|
|
|
|
int bufferSize = mFBWidth * mFBHeight * 4; // NOTE: Assuming 4 channels for now
|
|
|
|
|
unsigned char* buffer = new unsigned char[bufferSize];
|
|
|
|
|
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*)buffer);
|
|
|
|
|
|
|
|
|
|
mpFBTexture->FreeRawData();
|
|
|
|
|
mpFBTexture->mWidth = mFBWidth;
|
|
|
|
|
mpFBTexture->mHeight = mFBHeight;
|
|
|
|
|
mpFBTexture->mRawData = buffer;
|
|
|
|
|
mpFBTexture->mRawDataSize = bufferSize;
|
|
|
|
|
mpFBTexture->mFormat = ImageFormat::RGBA;
|
|
|
|
|
|
|
|
|
|
// return a copy of the image
|
|
|
|
|
return new Image(*mpFBTexture);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mpWindow->SwapBuffers();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
@ -274,11 +308,13 @@ namespace lunarium
|
|
|
|
|
glm::mat4 trans = glm::translate(id, pos);
|
|
|
|
|
trans = glm::scale(trans, glm::vec3(destination.Width, destination.Height, 1.0f));
|
|
|
|
|
|
|
|
|
|
mImageShader.MakeActive();
|
|
|
|
|
|
|
|
|
|
// float widthDiff = image.GetWidth() - source.Width;
|
|
|
|
|
|
|
|
|
|
// if (source.Width == image.GetWidth() && source.Height == image.GetHeight())
|
|
|
|
|
// {
|
|
|
|
|
// mImageShader.MakeActive();
|
|
|
|
|
|
|
|
|
|
// mImageShader.SetUniformf("uvManip", { 1.0f, 0.0f, 1.0f, 0.0f }); // No uv Manipulation
|
|
|
|
|
// mImageShader.SetUniformMatrix("model", 1, glm::value_ptr(trans));
|
|
|
|
|
// mImageShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection));
|
|
|
|
|
@ -293,6 +329,8 @@ namespace lunarium
|
|
|
|
|
float yScale = source.Height / image.GetHeight();
|
|
|
|
|
float yOffset = source.Top / image.GetHeight();
|
|
|
|
|
|
|
|
|
|
Logger::Log(LogCategory::GRAPHICS, LogLevel::INFO_VERBOSE, "uvManip Values: %f, %f, %f, %f", xScale, xOffset, yScale, yOffset);
|
|
|
|
|
|
|
|
|
|
mImageShader.SetUniformf("uvManip", { xScale, xOffset, yScale, yOffset });
|
|
|
|
|
mImageShader.SetUniformMatrix("model", 1, glm::value_ptr(trans));
|
|
|
|
|
mImageShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection));
|
|
|
|
|
@ -432,15 +470,6 @@ namespace lunarium
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OglGraphics::InitTextSystem()
|
|
|
|
|
{
|
|
|
|
|
// TODO: Check to see if Dear ImGui can be used to render text
|
|
|
|
|
// If it can render text on the app window and doesn't need
|
|
|
|
|
// to render onto one of it's own windows then it should work.
|
|
|
|
|
// If not add FreeType to the project and put the text rendering
|
|
|
|
|
// into it's own class.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OglGraphics::InitShapeSystem()
|
|
|
|
|
{
|
|
|
|
|
// Create rectangle buffers
|
|
|
|
|
@ -474,4 +503,39 @@ namespace lunarium
|
|
|
|
|
Logger::Log(LogCategory::GRAPHICS, LogLevel::WARNING, "Unable to build shape shader program.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OglGraphics::InitTextureFrameBuffer()
|
|
|
|
|
{
|
|
|
|
|
// Frame buffer
|
|
|
|
|
glGenFramebuffers(1, &mFBO);
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
|
|
|
|
|
|
|
|
|
|
// Texture
|
|
|
|
|
glGenTextures(1, &mpFBTexture->mGLTextureID);
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mpFBTexture->mGLTextureID);
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, mFBWidth, mFBHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
|
|
// Attach texture
|
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mpFBTexture->mGLTextureID, 0);
|
|
|
|
|
|
|
|
|
|
// Render Buffer for depth/stencil testing
|
|
|
|
|
glGenRenderbuffers(1, &mRBO);
|
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, mRBO);
|
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, mFBWidth, mFBHeight);
|
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
|
|
|
|
|
|
|
|
|
// Atach the render buffer
|
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mRBO);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
|
{
|
|
|
|
|
Logger::Log(LogCategory::GRAPHICS, LogLevel::WARNING, "Unable to initialize framebuffer for rendering to a texture");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|