summaryrefslogtreecommitdiff
path: root/source/modules/asura-core/graphics/image.cpp
blob: a0dff8ddfccdd0609f49d399a0a78c18727ccb75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <asura-utils/exceptions/exception.h>

#include "../core_config.h"

#include "shader.h"
#include "image.h"
#include "gfx_device.h"

using namespace AEIO;
using namespace AEImage;

namespace AsuraEngine
{
	namespace 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;
		}

	} // Graphics 
} // AsuraEngine