summaryrefslogtreecommitdiff
path: root/Runtime/Graphics/Texture.h
blob: 5c23fd73fa33d7a7811ad057392f896e5e7d96ae (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
106
107
108
109
110
111
112
113
#pragma once
#include <exception>
#include <string>
#include "Runtime/Lua/LuaHelper.h"
#include "../Utilities/UtilMacros.h"
#include "OpenGL.h"
#include "Runtime/Math/Math.h"

class ImageData;

enum ETextureType
{
    TEX_2D,
    TEX_CUBE,
};

enum ETextureFormat
{
    RGBA32,
    RGB24,
    RGB16,
    R8,
    A8,
};

enum ETextureWrapMode
{
	Clamp,
	Repeat,
    Mirror,
};

enum ETextureFilterMode
{
	Nearest,
	Linear,
};

// GPU侧的导入设置
struct TextureSetting 
{
    bool keepImageData; // 是否保存图片数据
    int type; // 图片类型
    int format; // 内部格式
    int wrapMode;    // 包围
    int filterMode;  // 滤波
};

class TextureException : public std::exception
{
public:
     TextureException(const char* what)
         : std::exception(what)
     {}
	 TextureException(int glError) 
	 {
		 g_sharedGLErrorMsg = std::to_string(glError);
		 std::exception(g_sharedGLErrorMsg.c_str());
	 }
};

class Texture : public LuaBind::NativeClass<Texture>
{
public: 
	Texture(TextureSetting setting, int width, int height)/*throw TextureException*/; // 空贴图
	Texture(TextureSetting setting, ImageData* imgData)/*throw TextureException*/;
	Texture(LuaBind::VM* vm, TextureSetting setting, ImageData* imgData)/*throw TextureException*/;
	~Texture();

	void UpdateSubImage(Rect rect, int format, int type, const void* data);

	GET(int, Width, m_Width);
	GET(int, Height, m_Height);

	GET(GLuint, GpuID, m_GPUID);
	
private: 
	void Init(TextureSetting setting, ImageData* imgData);

    //------------------------------------------------------------------------------

	GLuint m_GPUID;

	int m_Width, m_Height;

    int m_Type;
    int m_Format;
    int m_FilterMode;
    int m_WrapMode;

    bool m_KeepPixelData; // 是否保存图像像素数据,默认导入后不保存

    LuaBind::MemberRef m_ImageData; //图片像素数据

    //------------------------------------------------------------------------------

    LUA_BIND_DECL_CLASS(Texture);

    LUA_BIND_DECL_METHOD(_New);

    LUA_BIND_DECL_METHOD(_GetWidth);
    LUA_BIND_DECL_METHOD(_GetHeight);
    LUA_BIND_DECL_METHOD(_GetSize);

    LUA_BIND_DECL_METHOD(_GetType);
    LUA_BIND_DECL_METHOD(_GetFormat);
    LUA_BIND_DECL_METHOD(_GetWrapMode);
    LUA_BIND_DECL_METHOD(_GetFilterMode);

    LUA_BIND_DECL_METHOD(_IsKeepImageData);
    LUA_BIND_DECL_METHOD(_GetImageData);

};