Skip to content

Commit

Permalink
Fixed "wrong API-Use" (OpenGL) according to RenderDoc -> VertexBuffer…
Browse files Browse the repository at this point in the history
…s are now scoped pointers
  • Loading branch information
Zang3th committed Jun 11, 2022
1 parent ee4af77 commit 218a55e
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 134 deletions.
45 changes: 27 additions & 18 deletions Engine/Rendering/Renderables/Cubemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Engine
{
// ----- Private -----

Ref<VertexArray> Cubemap::CreateVao()
void Cubemap::InitGpuStorage()
{
float skyboxVertices[] =
static const float skyboxVertices[] =
{
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
Expand Down Expand Up @@ -52,49 +52,58 @@ namespace Engine
};

//Create and bind vao
Ref<VertexArray> vao = MakeRef<VertexArray>();
vao->Bind();
_vao = MakeScope<VertexArray>();
_vao->Bind();

//Create vbo, send it data and configure vao
VertexBuffer vbo(&skyboxVertices, sizeof(skyboxVertices), GL_STATIC_DRAW);
vao->DefineAttributes(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);
_vboVert = MakeScope<VertexBuffer>(&skyboxVertices, sizeof(skyboxVertices), GL_STATIC_DRAW);
_vao->DefineAttributes(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);

//Unbind vao
vao->Unbind();

return vao;
_vao->Unbind();
}

// ----- Public -----

Cubemap::Cubemap(const std::array<const char*, 6>& faces, Shader* shader)
: _vao(CreateVao()),
_cubemapTexture(MakeScope<CubemapTexture>(faces)),
: _cubemapTexture(MakeScope<CubemapTexture>(faces)),
_shader(shader),
_verticeCount(36)
{}
{
InitGpuStorage();
}

uint32 Cubemap::Draw(const glm::mat4& projMatrix, const glm::mat4& viewMatrix) const
{
_shader->Bind();

//Remove translation section
glm::mat4 view = glm::mat4(glm::mat3(viewMatrix));

//Bind shader
_shader->Bind();

//Bind texture
_cubemapTexture->Bind();

//Bind vao and vbo
_vao->Bind();
_vboVert->Bind();

//Set uniforms
_shader->SetUniformMat4f("view", view);
_shader->SetUniformMat4f("projection", projMatrix);
_shader->SetUniform1i("textureSampler", 0);

//Set texture
_cubemapTexture->Bind();
_vao->Bind();

//Render
GLCall(glDrawArrays(GL_TRIANGLES, 0, _verticeCount));

//Unbind vao and vbo
_vboVert->Unbind();
_vao->Unbind();

//Unbind texture
_cubemapTexture->Unbind();

//Unbind shader
_shader->Unbind();

//Return rendered vertices
Expand Down
5 changes: 3 additions & 2 deletions Engine/Rendering/Renderables/Cubemap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ namespace Engine
class Cubemap
{
private:
Ref<VertexArray> _vao;
Scope<VertexArray> _vao;
Scope<VertexBuffer> _vboVert;
Scope<CubemapTexture> _cubemapTexture;
Shader* _shader;
uint32 _verticeCount;

static Ref<VertexArray> CreateVao();
void InitGpuStorage();

public:
Cubemap(const std::array<const char*, 6>& faces, Shader* shader);
Expand Down
2 changes: 1 addition & 1 deletion Engine/Rendering/Renderables/CubemapTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Engine
GLCall(glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, localBuffer));

std::string cubemapInfo = "(X: " + std::to_string(width) + ", Y: " + std::to_string(height) + ", Channels: " + std::to_string(nrChannels) + ")";
Logger::Info("Loaded", "Heightmap", faces[i]);
Logger::Info("Loaded", "Cubemap", faces[i]);
Logger::Info("", "", cubemapInfo);
}
}
Expand Down
54 changes: 33 additions & 21 deletions Engine/Rendering/Renderables/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,30 @@ namespace Engine
{
// ----- Private -----

Ref<VertexArray> Model::CreateVaoFromMesh(Mesh* mesh)
void Model::InitGpuStorage(Mesh* mesh)
{
//Create and bind vao
Ref<VertexArray> vao = MakeRef<VertexArray>();
vao->Bind();
_vao = MakeScope<VertexArray>();
_vao->Bind();

//Create vbo's, send it data and configure vao
VertexBuffer vbo1(&mesh->vertices[0], mesh->vertices.size() * sizeof(glm::vec3), GL_STATIC_DRAW);
vao->DefineAttributes(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), nullptr);
_vboVert = MakeScope<VertexBuffer>(&mesh->vertices[0], mesh->vertices.size() * sizeof(glm::vec3), GL_STATIC_DRAW);
_vao->DefineAttributes(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), nullptr);

VertexBuffer vbo2(&mesh->texCoords[0], mesh->texCoords.size() * sizeof(glm::vec2), GL_STATIC_DRAW);
vao->DefineAttributes(1, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), nullptr);
_vboTex = MakeScope<VertexBuffer>(&mesh->texCoords[0], mesh->texCoords.size() * sizeof(glm::vec2), GL_STATIC_DRAW);
_vao->DefineAttributes(1, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), nullptr);

VertexBuffer vbo3(&mesh->normals[0], mesh->normals.size() * sizeof(glm::vec3), GL_STATIC_DRAW);
vao->DefineAttributes(2, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), nullptr);
_vboNorm = MakeScope<VertexBuffer>(&mesh->normals[0], mesh->normals.size() * sizeof(glm::vec3), GL_STATIC_DRAW);
_vao->DefineAttributes(2, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), nullptr);

VertexBuffer vbo4(&mesh->tangents[0], mesh->tangents.size() * sizeof(glm::vec3), GL_STATIC_DRAW);
vao->DefineAttributes(3, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), nullptr);
_vboTang = MakeScope<VertexBuffer>(&mesh->tangents[0], mesh->tangents.size() * sizeof(glm::vec3), GL_STATIC_DRAW);
_vao->DefineAttributes(3, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), nullptr);

//Create ibo
IndexBuffer ibo(&mesh->indices[0], mesh->indices.size() * sizeof(uint32));
_ibo = MakeScope<IndexBuffer>(&mesh->indices[0], mesh->indices.size() * sizeof(uint32));

//Unbind vao
vao->Unbind();

return vao;
_vao->Unbind();
}

void Model::SetModelMatrix()
Expand All @@ -53,8 +51,7 @@ namespace Engine
// ----- Public -----

Model::Model(Mesh* mesh)
: _vao(CreateVaoFromMesh(mesh)),
_model(glm::mat4(1.0f)),
: _model(glm::mat4(1.0f)),
_position(0.0f),
_texture1(mesh->texture1),
_texture2(mesh->texture2),
Expand All @@ -65,11 +62,8 @@ namespace Engine
_gotNormalMap(mesh->gotNormalMap),
_rotationX(0.0f), _rotationY(0.0f), _rotationZ(0.0f),
_size(1.0f)
{}

VertexArray* Model::GetVAO() const
{
return _vao.get();
InitGpuStorage(mesh);
}

glm::mat4 Model::GetModelMatrix() const
Expand All @@ -87,6 +81,24 @@ namespace Engine
return _gotNormalMap;
}

void Model::BindBuffers() const
{
_vao->Bind();
_vboVert->Bind();
_vboTex->Bind();
_vboNorm->Bind();
_vboTang->Bind();
}

void Model::UnbindBuffers() const
{
_vboTang->Unbind();
_vboNorm->Unbind();
_vboTex->Unbind();
_vboVert->Unbind();
_vao->Unbind();
}

void Model::ChangePosition(const glm::vec3& position)
{
_position += position;
Expand Down
11 changes: 7 additions & 4 deletions Engine/Rendering/Renderables/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ namespace Engine
class Model
{
private:
Ref<VertexArray> _vao;
Scope<VertexArray> _vao;
Scope<VertexBuffer> _vboVert, _vboTex, _vboNorm, _vboTang;
Scope<IndexBuffer> _ibo;
glm::mat4 _model;
glm::vec3 _position;
Texture *_texture1, *_texture2, *_texture3, *_texture4, *_texture5;
Expand All @@ -25,16 +27,17 @@ namespace Engine
float _rotationX, _rotationY, _rotationZ;
float _size;

static Ref<VertexArray> CreateVaoFromMesh(Mesh* mesh);
void SetModelMatrix();
void InitGpuStorage(Mesh* mesh);
void SetModelMatrix();

public:
explicit Model(Mesh* mesh);
[[nodiscard]] VertexArray* GetVAO() const;
[[nodiscard]] glm::mat4 GetModelMatrix() const;
[[nodiscard]] uint32 GetVerticeCount() const;
[[nodiscard]] int32 GotNormalMap() const;

void BindBuffers() const;
void UnbindBuffers() const;
void ChangePosition(const glm::vec3& position);
void ChangeRotation(float rotX, float rotY, float rotZ);
void ChangeSize(float size);
Expand Down
45 changes: 28 additions & 17 deletions Engine/Rendering/Renderables/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ namespace Engine
{
// ----- Private -----

Ref<VertexArray> Sprite::CreateSpriteVao()
void Sprite::InitGpuStorage()
{
//Create and bind vao
Ref<VertexArray> vao = MakeRef<VertexArray>();
vao->Bind();

//Create data
static const float vertices[] =
{
Expand All @@ -23,14 +19,17 @@ namespace Engine
1.0f, 0.0f, 1.0f, 0.0f
};

//Create and bind vao
_vao = MakeScope<VertexArray>();
_vao->Bind();

//Create vbo, send it data and configure vao
VertexBuffer vbo(vertices, sizeof(vertices), GL_STATIC_DRAW);
vao->DefineAttributes(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);
_vboVert = MakeScope<VertexBuffer>(vertices, sizeof(vertices), GL_STATIC_DRAW);
_vao->DefineAttributes(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);

//Unbind vao
vao->Unbind();

return vao;
_vao->Unbind();
_vboVert->Unbind();
}

void Sprite::SetModelMatrix()
Expand All @@ -55,34 +54,46 @@ namespace Engine
// ----- Public -----

Sprite::Sprite(Texture* texture, Shader* shader, glm::vec3 color)
: _vao(CreateSpriteVao()),
_model(glm::mat4(1.0f)),
: _model(glm::mat4(1.0f)),
_color(color),
_texture(texture),
_shader(shader),
_position(glm::vec2(0.0f)),
_size(glm::vec2(1.0f)),
_rotation(0.0f),
_verticeCount(6)
{}
{
InitGpuStorage();
}

uint32 Sprite::Draw(const glm::mat4& projMatrix) const
{
//Bind shader
_shader->Bind();


//Bind texture
_texture->Bind();

//Bind vao and vbo
_vao->Bind();
_vboVert->Bind();

//Set uniforms
_shader->SetUniformVec3f("color", _color);
_shader->SetUniformMat4f("model", _model);
_shader->SetUniformMat4f("projection", projMatrix);

_texture->Bind();
_vao->Bind();

//Render quad
GLCall(glDrawArrays(GL_TRIANGLES, 0, _verticeCount));

//Unbind vao and vbo
_vboVert->Unbind();
_vao->Unbind();

//Unbind texture
_texture->Unbind();

//Unbind shader
_shader->Unbind();

//Return rendered vertices
Expand Down
7 changes: 4 additions & 3 deletions Engine/Rendering/Renderables/Sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace Engine
class Sprite
{
private:
Ref<VertexArray> _vao;
Scope<VertexArray> _vao;
Scope<VertexBuffer> _vboVert;
glm::mat4 _model;
glm::vec3 _color;
Texture* _texture;
Expand All @@ -24,8 +25,8 @@ namespace Engine
float _rotation;
uint32 _verticeCount;

static Ref<VertexArray> CreateSpriteVao();
void SetModelMatrix();
void InitGpuStorage();
void SetModelMatrix();

public:
Sprite(Texture* texture, Shader* shader, glm::vec3 color);
Expand Down
Loading

0 comments on commit 218a55e

Please sign in to comment.