将游戏层和UI层分开渲染 并提高了缩放质量
This commit is contained in:
@@ -26,8 +26,11 @@ RenderManager::RenderManager(SDL_Window *window)
|
||||
// 设置窗口尺寸
|
||||
_windowWidth = width;
|
||||
_windowHeight = height;
|
||||
// 设置正交投影矩阵
|
||||
_orthoMatrix = glm::ortho(0.0f, (float)width, (float)height, 0.0f, -1.0f, 1.0f);
|
||||
|
||||
// 游戏的初始正交投影矩阵
|
||||
_GameOrthoMatrix = glm::ortho(0.0f, (float)width / 1.2f, (float)height / 1.2f, 0.0f, -1.0f, 1.0f);
|
||||
// UI的初始正交投影矩阵
|
||||
_UIOrthoMatrix = glm::ortho(0.0f, (float)width, (float)height, 0.0f, -1.0f, 1.0f);
|
||||
|
||||
// 设置清屏颜色
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
@@ -217,7 +220,7 @@ void RenderManager::Init2DTextureProgram()
|
||||
|
||||
// 设置着色器 uniforms
|
||||
glUniformMatrix4fv(_currentRenderParams.UnimLocs[0], 1, GL_FALSE, glm::value_ptr(model));
|
||||
glUniformMatrix4fv(_currentRenderParams.UnimLocs[1], 1, GL_FALSE, glm::value_ptr(_orthoMatrix));
|
||||
glUniformMatrix4fv(_currentRenderParams.UnimLocs[1], 1, GL_FALSE, glm::value_ptr(_OrthoMatrix));
|
||||
|
||||
// 绑定纹理并设置采样器
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
@@ -297,6 +300,7 @@ void RenderManager::SetCurrentBufferObject(std::string name)
|
||||
void RenderManager::ClearScreen()
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
_frameRenderCount = 0;
|
||||
}
|
||||
|
||||
void RenderManager::SwapBuffer()
|
||||
@@ -317,12 +321,21 @@ void RenderManager::CloseClipRect()
|
||||
|
||||
void RenderManager::DrawTexture(RefPtr<Texture> textureObj, const SDL_Rect *srcrect, const SDL_FRect *dstrect, const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip, void *userdata)
|
||||
{
|
||||
_frameRenderCount++;
|
||||
if (_currentRenderParams.DrawFunc != nullptr)
|
||||
_currentRenderParams.DrawFunc(textureObj, srcrect, dstrect, angle, center, flip, userdata);
|
||||
else
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "找不到渲染逻辑函数\n");
|
||||
}
|
||||
|
||||
void RenderManager::SetOrthoMatrixType(int type)
|
||||
{
|
||||
if (type == 0)
|
||||
_OrthoMatrix = _GameOrthoMatrix;
|
||||
else
|
||||
_OrthoMatrix = _UIOrthoMatrix;
|
||||
}
|
||||
|
||||
GLuint RenderManager::CompileShader(GLenum type, std::string Path)
|
||||
{
|
||||
std::ifstream Source("shader/" + Path);
|
||||
|
||||
@@ -31,8 +31,12 @@ private:
|
||||
// 窗口尺寸
|
||||
int _windowWidth;
|
||||
int _windowHeight;
|
||||
// 正交投影矩阵
|
||||
glm::mat4 _orthoMatrix;
|
||||
// 游戏正交投影矩阵
|
||||
glm::mat4 _GameOrthoMatrix;
|
||||
// UI的正交投影矩阵
|
||||
glm::mat4 _UIOrthoMatrix;
|
||||
// 渲染的正交投影矩阵
|
||||
glm::mat4 _OrthoMatrix;
|
||||
|
||||
// 渲染器上下文
|
||||
SDL_GLContext _ctx;
|
||||
@@ -46,6 +50,9 @@ private:
|
||||
// 当前使用渲染参数
|
||||
GL_RenderParams _currentRenderParams;
|
||||
|
||||
public:
|
||||
// 单帧渲染调用次数
|
||||
int _frameRenderCount = 0;
|
||||
public:
|
||||
RenderManager(SDL_Window *window);
|
||||
~RenderManager();
|
||||
@@ -69,6 +76,9 @@ public:
|
||||
void CloseClipRect();
|
||||
// 绘制纹理
|
||||
void DrawTexture(RefPtr<Texture> texture, const SDL_Rect *srcrect, const SDL_FRect *dstrect, const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip, void *userdata = nullptr);
|
||||
// 设置正交投影矩阵类型
|
||||
void SetOrthoMatrixType(int type);
|
||||
|
||||
|
||||
private:
|
||||
// 编译着色器
|
||||
|
||||
@@ -35,9 +35,9 @@ bool Texture::Init(std::string PngPath)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
// 缩小过滤:使用最近邻采样
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
// 放大过滤:使用最近邻采样
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
// 处理像素对齐(确保SDL的像素行对齐与OpenGL兼容)
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
Reference in New Issue
Block a user