PNG is now a resource.

You can now load textures threaded.
PNG will now throw exceptions when errors happend
SpriteComponent is now working without billboarding.
This commit is contained in:
Tleety
2016-02-10 17:50:17 +01:00
parent 04c23be136
commit 53265bbc90
17 changed files with 149 additions and 83 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ DrawBloomPass::DrawBloomPass(IRenderer* renderer)
void DrawBloomPass::InitializeTextures()
{
m_WhiteTexture = ResourceManager::Load<Texture>("Textures/Core/White.png");
m_WhiteTexture = CommonFunctions::LoadTexture("Textures/Core/White.png", false);
}
void DrawBloomPass::InitializeShaderPrograms()
+10 -7
View File
@@ -11,10 +11,11 @@ DrawFinalPass::DrawFinalPass(IRenderer* renderer, LightCullingPass* lightCulling
void DrawFinalPass::InitializeTextures()
{
m_WhiteTexture = ResourceManager::Load<Texture>("Textures/Core/White.png");
m_BlackTexture = ResourceManager::Load<Texture>("Textures/Core/Black.png");
m_NeutralNormalTexture = ResourceManager::Load<Texture>("Textures/Core/NeutralNormalMap.png");
m_GreyTexture = ResourceManager::Load<Texture>("Textures/Core/Grey.png");
m_WhiteTexture = CommonFunctions::LoadTexture("Textures/Core/White.png", false);
m_BlackTexture = CommonFunctions::LoadTexture("Textures/Core/Black.png", false);
m_NeutralNormalTexture = CommonFunctions::LoadTexture("Textures/Core/NeutralNormalMap.png", false);
m_GreyTexture = CommonFunctions::LoadTexture("Textures/Core/Grey.png", false);
m_ErrorTexture = CommonFunctions::LoadTexture("Textures/Core/ErrorTexture.png", false);
}
void DrawFinalPass::InitializeFrameBuffers()
@@ -56,7 +57,7 @@ void DrawFinalPass::InitializeShaderPrograms()
m_ExplosionEffectProgram->Link();
GLERROR("Creating explosion program");
m_SpriteProgram = ResourceManager::Load<ShaderProgram>("#m_SpriteProgram");
m_SpriteProgram = ResourceManager::Load<ShaderProgram>("#SpriteProgram");
m_SpriteProgram->AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/Sprite.vert.glsl")));
m_SpriteProgram->AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/Sprite.frag.glsl")));
m_SpriteProgram->Compile();
@@ -222,10 +223,12 @@ void DrawFinalPass::DrawSprites(std::list<std::shared_ptr<RenderJob>>&jobs, Rend
for(auto& job : jobs) {
auto spriteJob = std::dynamic_pointer_cast<SpriteJob>(job);
if(spriteJob) {
if (spriteJob) {
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(spriteJob->Matrix));
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ViewMatrix()));
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix()));
glUniform3fv(glGetUniformLocation(shaderHandle, "CameraPos"), 1, glm::value_ptr(scene.Camera->Position()));
glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(spriteJob->Color));
glUniform4fv(glGetUniformLocation(shaderHandle, "FillColor"), 1, glm::value_ptr(spriteJob->FillColor));
glUniform1f(glGetUniformLocation(shaderHandle, "FillPercentage"), spriteJob->FillPercentage);
@@ -234,7 +237,7 @@ void DrawFinalPass::DrawSprites(std::list<std::shared_ptr<RenderJob>>&jobs, Rend
if (spriteJob->DiffuseTexture != nullptr) {
glBindTexture(GL_TEXTURE_2D, spriteJob->DiffuseTexture->m_Texture);
} else {
glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture);
glBindTexture(GL_TEXTURE_2D, m_ErrorTexture->m_Texture);
}
glActiveTexture(GL_TEXTURE1);
+4 -4
View File
@@ -7,16 +7,16 @@ Model::Model(std::string fileName)
for (auto& group : m_RawModel->MaterialGroups) {
if (!group.TexturePath.empty()) {
group.Texture = std::shared_ptr<Texture>(ResourceManager::Load<Texture>(group.TexturePath));
group.Texture = std::shared_ptr<Texture>(CommonFunctions::LoadTexture(group.TexturePath, false));
}
if (!group.NormalMapPath.empty()) {
group.NormalMap = std::shared_ptr<Texture>(ResourceManager::Load<Texture>(group.NormalMapPath));
group.NormalMap = std::shared_ptr<Texture>(CommonFunctions::LoadTexture(group.NormalMapPath, false));
}
if (!group.SpecularMapPath.empty()) {
group.SpecularMap = std::shared_ptr<Texture>(ResourceManager::Load<Texture>(group.SpecularMapPath));
group.SpecularMap = std::shared_ptr<Texture>(CommonFunctions::LoadTexture(group.SpecularMapPath, false));
}
if (!group.IncandescenceMapPath.empty()) {
group.IncandescenceMap = std::shared_ptr<Texture>(ResourceManager::Load<Texture>(group.IncandescenceMapPath));
group.IncandescenceMap = std::shared_ptr<Texture>(CommonFunctions::LoadTexture(group.IncandescenceMapPath, false));
}
}
+8 -14
View File
@@ -4,40 +4,35 @@ PNG::PNG(std::string path)
{
FILE* file = fopen(path.c_str(), "rb");
if (!file) {
LOG_ERROR("Failed to open texture file \"%s\": %s", path.c_str(), const_cast<const char*>(strerror(errno)));
return;
throw Resource::FailedLoadingException("Failed to open texture file.");
}
png_byte header[8];
fread(header, 1, 8, file);
bool isPNG = !png_sig_cmp(header, 0, 8);
if (!isPNG) {
LOG_ERROR("Failed to load texture file \"%s\": File isn't PNG", path.c_str());
fclose(file);
return;
throw Resource::FailedLoadingException("File is not PNG.");
}
// Initialize libpng
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, (png_error_ptr)&PNG::pngErrorFunction, (png_error_ptr)&PNG::pngErrorFunction);
if (!png_ptr) {
LOG_ERROR("libpng: Failed to initialze png_struct");
png_destroy_read_struct(&png_ptr, nullptr, nullptr);
fclose(file);
return;
throw Resource::FailedLoadingException("Failed to initialze png_struct.");
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
LOG_ERROR("libpng: Failed to initialze png_info");
png_destroy_read_struct(&png_ptr, nullptr, nullptr);
fclose(file);
return;
throw Resource::FailedLoadingException("Failed to initialze png_info.");
}
png_infop info_end_ptr = png_create_info_struct(png_ptr);
if (!info_end_ptr) {
LOG_ERROR("libpng: Failed to initialze second png_info");
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
fclose(file);
return;
throw Resource::FailedLoadingException("Failed to initialze second png_info.");
}
png_init_io(png_ptr, file);
@@ -51,8 +46,8 @@ PNG::PNG(std::string path)
unsigned int width, height;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
if (bit_depth != 8) {
LOG_ERROR("libpng: Unsupported bit depth \"%i\" of image \"%s\", must be 8", bit_depth, path.c_str());
return;
throw Resource::FailedLoadingException("Unsupported bit depth. Must be 8");
}
switch (color_type) {
case PNG_COLOR_TYPE_RGB:
@@ -60,8 +55,7 @@ PNG::PNG(std::string path)
Format = Image::ImageFormat::RGBA;
break;
default:
LOG_ERROR("libpng: Unsupported color format \"%i\" of image \"%s\"", color_type, path.c_str());
return;
throw Resource::FailedLoadingException("Unsupported color format.");
}
// Convert RGB to RGBA, since DirectX rather treat them all the same way
+12 -5
View File
@@ -106,16 +106,21 @@ void Renderer::Draw(RenderFrame& frame)
for (auto scene : frame.RenderScenes){
SortRenderJobsByDepth(*scene);
GLERROR("SortByDepth");
m_PickingPass->Draw(*scene);
GLERROR("Drawing pickingpass");
m_LightCullingPass->GenerateNewFrustum(*scene);
GLERROR("Generate frustums");
m_LightCullingPass->FillLightList(*scene);
GLERROR("Filling light list");
m_LightCullingPass->CullLights(*scene);
GLERROR("LightCulling");
m_DrawFinalPass->Draw(*scene);
GLERROR("Draw Geometry+Light");
//m_DrawScenePass->Draw(*scene);
GLERROR("Renderer::Draw m_DrawScenePass->Draw");
m_TextPass->Draw(*scene, *m_DrawFinalPass->FinalPassFrameBuffer());
GLERROR("Draw Text");
}
m_DrawBloomPass->Draw(m_DrawFinalPass->BloomTexture());
@@ -136,7 +141,8 @@ void Renderer::Draw(RenderFrame& frame)
}
m_ImGuiRenderPass->Draw();
glfwSwapBuffers(m_Window);
GLERROR("Imgui draw");
glfwSwapBuffers(m_Window);
}
PickData Renderer::Pick(glm::vec2 screenCoord)
@@ -146,8 +152,8 @@ PickData Renderer::Pick(glm::vec2 screenCoord)
void Renderer::InitializeTextures()
{
m_ErrorTexture = ResourceManager::Load<Texture>("Textures/Core/ErrorTexture.png");
m_WhiteTexture = ResourceManager::Load<Texture>("Textures/Core/White.png");
m_ErrorTexture = CommonFunctions::LoadTexture("Textures/Core/ErrorTexture.png", false);
m_WhiteTexture = CommonFunctions::LoadTexture("Textures/Core/White.png", false);
}
@@ -155,6 +161,7 @@ void Renderer::SortRenderJobsByDepth(RenderScene &scene)
{
//Sort all forward jobs so transparency is good.
scene.TransparentObjects.sort(Renderer::DepthSort);
scene.SpriteJobs.sort(Renderer::DepthSort);
}
void Renderer::GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type)
+15 -14
View File
@@ -2,24 +2,25 @@
Texture::Texture(std::string path)
{
PNG* img = ResourceManager::Load<PNG, true>(path); //TODO: Make this threaded. Catch exeptions in all other load places.
PNG image(path);
//PNG image(path);
if (image.Width == 0 && image.Height == 0 || image.Format == Image::ImageFormat::Unknown) {
//image = PNG("Textures/Core/ErrorTexture.png");
return; // Temporary fix to remove crash
/*
if (image.Width == 0 && image.Height == 0 || image.Format == Image::ImageFormat::Unknown) {
LOG_ERROR("Couldn't even load the error texture. This is a dark day indeed.");
return;
}*/
}
//if (img->Width == 0 && img->Height == 0 || img->Format == Image::ImageFormat::Unknown) {
// //image = PNG("Textures/Core/ErrorTexture.png");
// //return; // Temporary fix to remove crash
this->Width = image.Width;
this->Height = image.Height;
// if (img->Width == 0 && img->Height == 0 || img->Format == Image::ImageFormat::Unknown) {
// LOG_ERROR("Couldn't even load the error texture. This is a dark day indeed.");
// return;
// }
//}
this->Width = img->Width;
this->Height = img->Height;
GLint format;
switch (image.Format) {
switch (img->Format) {
case Image::ImageFormat::RGB:
format = GL_RGB;
break;
@@ -32,7 +33,7 @@ Texture::Texture(std::string path)
glGenTextures(1, &m_Texture);
glBindTexture(GL_TEXTURE_2D, m_Texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, format, image.Width, image.Height, 0, format, GL_UNSIGNED_BYTE, image.Data);
glTexImage2D(GL_TEXTURE_2D, 0, format, img->Width, img->Height, 0, format, GL_UNSIGNED_BYTE, img->Data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -1,2 +1,19 @@
#include "Rendering/Util/CommonFunctions.h"
Texture* CommonFunctions::LoadTexture(std::string path, bool threaded)
{
Texture* img;
try {
if(threaded) {
img = ResourceManager::Load<Texture, true>(path);
} else {
img = ResourceManager::Load<Texture, false>(path);
}
} catch (const Resource::StillLoadingException&) {
img = ResourceManager::Load<Texture>("Textures/Core/ErrorTexture.png");
} catch (const std::exception&) {
img = nullptr;
}
return img;
}