简介
本序列由澳大利亚的一个游戏引擎爱好者整理制作,使用了前瞻的语言技术,
经常多次的修改制作,一个个小的知识点贯通,非常具有引导学习意义!
Bilibili站转载学习,非个人研究,鸣谢!
原作者Github
Bilibili直达链接
YouTube直达链接
#1 添加渲染顶点Buffer类
#2 添加渲染接口类
#3 实现基于OpenGL渲染的实现类
#1 添加渲染顶点Buffer类
// Buffer.h
#pragma once
namespace Aurora
{
// 顶点Buffer缓冲
class VertexBuffer
{
public:
virtual ~VertexBuffer() {}
virtual void Bind() const = 0;
virtual void Unbind() const = 0;
static VertexBuffer* Create(float* vertices, uint32_t size);
};
// 顶点顺序Buffer缓冲
class IndexBuffer
{
public:
virtual ~IndexBuffer(){}
virtual void Bind() const = 0;
virtual void Unbind() const = 0;
virtual uint32_t GetCount() const = 0;
static IndexBuffer* Create(uint32_t* indices, uint32_t size);
};
}
// Buffer.cpp
#include "aopch.h"
#include "Buffer.h"
#include "Renderer.h"
#include "Platform/OpenGL/OpenGLBuffer.h"
namespace Aurora {
VertexBuffer* VertexBuffer::Create(float* vertices, uint32_t size)
{
switch (Renderer::GetAPI())
{
case RendererAPI::None: AO_CORE_ASSERT(false, "RendererAPI::None is currently not supported!"); return nullptr;
case RendererAPI::OpenGL: return new OpenGLVertexBuffer(vertices, size);
}
AO_CORE_ASSERT(false, "Unknown RendererAPI!");
return nullptr;
}
IndexBuffer* IndexBuffer::Create(uint32_t* indices, uint32_t size)
{
switch (Renderer::GetAPI())
{
case RendererAPI::None: AO_CORE_ASSERT(false, "RendererAPI::None is currently not supported!"); return nullptr;
case RendererAPI::OpenGL: return new OpenGLIndexBuffer(indices, size);
}
AO_CORE_ASSERT(false, "Unknown RendererAPI!");
return nullptr;
}
}
#2 添加渲染接口类
// Renderer.h
#pragma once
namespace Aurora {
// 渲染类型
enum class RendererAPI
{
None = 0,
OpenGL = 1
};
// 渲染接口
class Renderer
{
public:
inline static RendererAPI GetAPI() { return s_RendererAPI; }
private:
static RendererAPI s_RendererAPI;
};
}
// Renderer.cpp
#include "aopch.h"
#include "Renderer.h"
namespace Aurora {
RendererAPI Renderer::s_RendererAPI = RendererAPI::OpenGL;
}
#3 实现基于OpenGL渲染的实现类
// OpenGLBuffer.h
#pragma once
#include "Aurora/Renderer/Buffer.h"
namespace Aurora {
// OpenGL顶点渲染的缓冲类
class OpenGLVertexBuffer : public VertexBuffer
{
public:
OpenGLVertexBuffer(float* vertices, uint32_t size);
virtual ~OpenGLVertexBuffer();
virtual void Bind() const;
virtual void Unbind() const;
private:
uint32_t m_RendererID;
};
// OpenGL顶点的渲染顺序缓冲类
class OpenGLIndexBuffer : public IndexBuffer
{
public:
OpenGLIndexBuffer(uint32_t* indices, uint32_t count);
virtual ~OpenGLIndexBuffer();
virtual void Bind() const;
virtual void Unbind() const;
virtual uint32_t GetCount() const { return m_Count; }
private:
uint32_t m_RendererID;
uint32_t m_Count;
};
}
// OpenGLBuffer.cpp
#include "aopch.h"
#include "OpenGLBuffer.h"
#include <glad/glad.h>
namespace Aurora {
/
// VertexBuffer /
/
OpenGLVertexBuffer::OpenGLVertexBuffer(float* vertices, uint32_t size)
{
glCreateBuffers(1, &m_RendererID);
glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
}
OpenGLVertexBuffer::~OpenGLVertexBuffer()
{
glDeleteBuffers(1, &m_RendererID);
}
void OpenGLVertexBuffer::Bind() const
{
glBindBuffer(GL_ARRAY_BUFFER, m_RendererID);
}
void OpenGLVertexBuffer::Unbind() const
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
/
// IndexBuffer //
/
OpenGLIndexBuffer::OpenGLIndexBuffer(uint32_t* indices, uint32_t count)
: m_Count(count)
{
glCreateBuffers(1, &m_RendererID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(uint32_t), indices, GL_STATIC_DRAW);
}
OpenGLIndexBuffer::~OpenGLIndexBuffer()
{
glDeleteBuffers(1, &m_RendererID);
}
void OpenGLIndexBuffer::Bind() const
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_RendererID);
}
void OpenGLIndexBuffer::Unbind() const
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
}
http://auroraengine.vaiwan.com/idrok/Aurora