summaryrefslogtreecommitdiff
path: root/Client/Source/Graphics/GfxDevice.h
blob: d81d5740a9ee52f92a54ca7f0430d80d033d17d1 (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
#ifndef DEVICE_H
#define DEVICE_H
#include "../Math/Math.h"

#include "../Utilities/Type.h"
#include "../Utilities/Assert.h"
#include "../Graphics/Shader.h"
#include "../Math/Vector2.hpp"
#include "../Math/Vector3.hpp"
#include "../Math/Vector4.hpp"
#include "../Math/Matrix44.h"

#include "Shader.h"
#include "Texture.h"
#include "DeviceDefine.h"
#include "VertexBuffer.h"
#include "DynamicVertexBuffer.h"
#include "Color.h"

struct GfxDeviceSetting 
{
	ETextureFilterMode filterMode; // 默认的贴图过滤模式 
	ETextureWrapMode wrapMode;     // 默认的贴图平铺模式
};

// 当前绑定的shader
struct ShaderState
{
    Shader* shader;
    operator bool() 
    {
        return shader != NULL;
    }
	GLuint GetID() {
		if (shader == nullptr)
			return 0;
		GLint id = shader->GetID();
		return id;
	}
};

// 对渲染相关API的封装
class GfxDevice
{
public:
	GfxDevice();
	~GfxDevice();

	void Initialize(GfxDeviceSetting setting);

	void Enable(EDeviceEnable enabled);
	void Disable(EDeviceEnable enabled);
	void IsEnable(uint flag);

	void SetDepthTest(EDepthTest testing);
	void SetCullFace(ECullFace face);
	void SetStencilMask(byte stencilMask);
	void SetStencilOp(EStencilOp op);

	void SetAntiAliasing(int level = 0);

    void Clear(int clearFlag);

    void UseShader(Shader* shader, int idx);
    void UnuseShader();
    void SetUniformVec2(const char* name, Vector2f vec2);
    void SetUniformVec3(const char* name, Vector3f vec3);
    void SetUniformVec4(const char* name, Vector4f vec4);
    void SetUniformMat4(const char* name, Matrix44 mat4);
    void SetUniformTexture(const char* name, Texture* texture);

	void BeginFrame();
	void EndFrame();
	void PresentFrame();

	bool IsInsideFrame();

	DynamicVertexBuffer* GetSharedVBO() { return &m_DynamicVBO; }

	GET_SET(Color, ClearColor, m_ClearColor);
	GET_SET(ETextureFilterMode, DefaultFilterMode, m_DefaultFilterMode);
	GET_SET(ETextureWrapMode, DefaultWrapMode, m_DefaultWrapMode);

private: 
	bool m_IsInsideFrame;

	// 渲染状态
	uint            m_EnableFlag;
	Color m_ClearColor;
	EDepthTest      m_DepthTest;
	EStencilTest    m_StencilTest;
	EStencilOp      m_StencilOp;
	byte            m_StencilMask;

    ShaderState m_Shader; // 当前绑定的shader

	// 贴图默认设置
	ETextureFilterMode m_DefaultFilterMode;
	ETextureWrapMode m_DefaultWrapMode;

	DynamicVertexBuffer m_DynamicVBO; // 共享的VBO,用来做立即渲染

};

extern GfxDevice g_GfxDevice;

#define g_SharedVBO (*g_GfxDevice.GetSharedVBO())

#endif