summaryrefslogtreecommitdiff
path: root/Source/modules/asura-core/Graphics/Image.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-08-07 21:08:47 +0800
committerchai <chaifix@163.com>2019-08-07 21:08:47 +0800
commit0c391fdbce5a079cf03e483eb6174dd47806163d (patch)
treeb06cd7a9d0ae0d9bb9e82f3dcb786dfce11f8628 /Source/modules/asura-core/Graphics/Image.cpp
parent9686368e58e25cbd6dc37d686bdd2be3f80486d6 (diff)
*misc
Diffstat (limited to 'Source/modules/asura-core/Graphics/Image.cpp')
-rw-r--r--Source/modules/asura-core/Graphics/Image.cpp103
1 files changed, 103 insertions, 0 deletions
diff --git a/Source/modules/asura-core/Graphics/Image.cpp b/Source/modules/asura-core/Graphics/Image.cpp
new file mode 100644
index 0000000..36d2478
--- /dev/null
+++ b/Source/modules/asura-core/Graphics/Image.cpp
@@ -0,0 +1,103 @@
+#include <asura-base/Exception.h>
+
+#include "../CoreConfig.h"
+
+#include "Shader.h"
+#include "Image.h"
+#include "GfxDevice.h"
+
+using namespace AEFileSystem;
+using namespace AEImage;
+
+namespace_begin(AsuraEngine)
+namespace_begin(Graphics)
+
+Image::Image()
+ : m_Width(0)
+ , m_Height(0)
+{
+}
+
+Image::~Image()
+{
+}
+
+bool Image::Load(ImageData* imgData)
+{
+ if (!imgData) return false;
+
+ if (m_TexID == 0)
+ {
+ glGenTextures(1, &m_TexID);
+ if (m_TexID == 0)
+ throw Exception("OpenGL glGenTextures failed.");
+ }
+
+ glBindTexture(GL_TEXTURE_2D, m_TexID);
+ imgData->Lock();
+ int width = imgData->width;
+ int height = imgData->height;
+ TextureFormat tf = ConvertColorFormat(imgData->format);
+ glTexImage2D(
+ GL_TEXTURE_2D
+ , 0
+ , tf.internalformat
+ , width, height
+ , 0
+ , tf.externalformat
+ , tf.type
+ , imgData->pixels
+ );
+
+ m_Width = imgData->width;
+ m_Height = imgData->height;
+ imgData->Unlock();
+ GLenum err = glGetError();
+ if (err != GL_NO_ERROR)
+ throw Exception("OpenGL glTexImage2D cause error, error code=%d", err);
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ return true;
+}
+
+bool Image::Load(ImageData* imgData, const AEMath::Vector2i& pos)
+{
+ if (!imgData) return false;
+
+ glBindTexture(GL_TEXTURE_2D, m_TexID);
+ imgData->Lock();
+ int width = imgData->width;
+ int height = imgData->height;
+ TextureFormat tf = ConvertColorFormat(imgData->format);
+ glTexSubImage2D(
+ GL_TEXTURE_2D
+ , 0
+ , pos.x
+ , pos.y
+ , imgData->width
+ , imgData->height
+ , tf.externalformat
+ , tf.type
+ , imgData->pixels
+ );
+ imgData->Unlock();
+ GLenum err = glGetError();
+ if (err != GL_NO_ERROR)
+ throw Exception("OpenGL glTexSubImage2D cause error, error code=%d", err);
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ return true;
+}
+
+uint32 Image::GetWidth()
+{
+ return m_Width;
+}
+
+uint32 Image::GetHeight()
+{
+ return m_Height;
+}
+
+namespace_end
+namespace_end \ No newline at end of file