Object now follows the behaviour of Shader

This commit is contained in:
Robert 2021-01-21 01:12:25 +01:00
parent 4fe91a12e2
commit 87b27eae70
7 changed files with 32 additions and 19 deletions

View file

@ -66,7 +66,7 @@ int main(int argc, char** argv)
return -1;
}
oglu::Object square(vertices, sizeof(vertices), indices, sizeof(indices), topology, sizeof(topology));
oglu::Object square = oglu::MakeObject(vertices, sizeof(vertices), indices, sizeof(indices), topology, sizeof(topology));
while (!glfwWindowShouldClose(window))
{
@ -76,7 +76,7 @@ int main(int argc, char** argv)
shader->SetUniform("ourColor", &oglu::Color::Transparent);
shader->Use();
square.BindAndDraw();
square->BindAndDraw();
glfwSwapBuffers(window);
glfwPollEvents();

View file

@ -1,7 +1,7 @@
#ifndef COLOR_HPP
#define COLOR_HPP
#include "core.hpp"
#include <core.hpp>
#include <glad/glad.h>
namespace oglu

View file

@ -1,6 +1,8 @@
#ifndef CORE_HPP
#define CORE_HPP
#include <memory>
#ifdef OGLU_WIN32
#ifdef OGLU_BUILD_DLL
#define OGLU_API __declspec(dllexport)

View file

@ -1,7 +1,7 @@
#ifndef DRAWABLE_HPP
#define DRAWABLE_HPP
#include "core.hpp"
#include <core.hpp>
#include <glad/glad.h>
namespace oglu
@ -15,11 +15,12 @@ namespace oglu
const GLvoid* pointer;
} VertexAttribute;
class OGLU_API Object
class OGLU_API AbstractObject
{
public:
Object();
Object(const GLfloat* vertices, size_t verticesSize, const GLuint* indices, size_t indicesSize, const VertexAttribute* topology, size_t topologySize);
AbstractObject(const AbstractObject& other);
friend std::shared_ptr<AbstractObject> OGLU_API MakeObject(const GLfloat* vertices, size_t verticesSize, const GLuint* indices, size_t indicesSize, const VertexAttribute* topology, size_t topologySize);
GLuint GetVAO() { return VAO; }
void Bind();
@ -29,11 +30,15 @@ namespace oglu
void BindAndDraw();
private:
AbstractObject(const GLfloat* vertices, size_t verticesSize, const GLuint* indices, size_t indicesSize, const VertexAttribute* topology, size_t topologySize);
inline void RegisterVertexAttribPointer(GLuint index, const VertexAttribute& topology);
GLuint VAO;
GLsizei count;
};
typedef std::shared_ptr<AbstractObject> Object;
}
#endif

View file

@ -1,7 +1,6 @@
#ifndef SHADER_HPP
#define SHADER_HPP
#include <memory>
#include <core.hpp>
#include <glad/glad.h>

View file

@ -2,15 +2,22 @@
namespace oglu
{
Object::Object() :
VAO(0)
AbstractObject::AbstractObject(const AbstractObject& other) :
VAO(other.VAO), count(other.count)
{
}
Object::Object(const GLfloat* vertices, size_t verticesSize,
Object MakeObject(const GLfloat* vertices, size_t verticesSize, const GLuint* indices, size_t indicesSize, const VertexAttribute* topology, size_t topologySize)
{
AbstractObject* obj = new AbstractObject(vertices, verticesSize, indices, indicesSize, topology, topologySize);
return Object(obj);
}
AbstractObject::AbstractObject(const GLfloat* vertices, size_t verticesSize,
const GLuint* indices, size_t indicesSize,
const VertexAttribute* topology, size_t topologySize) :
VAO(0)
VAO(0), count(0)
{
topologySize /= sizeof(VertexAttribute);
@ -39,29 +46,29 @@ namespace oglu
count = indicesSize / sizeof(GLuint);
}
void Object::Bind()
void AbstractObject::Bind()
{
glBindVertexArray(VAO);
}
void Object::Unbind()
void AbstractObject::Unbind()
{
}
void Object::Draw()
void AbstractObject::Draw()
{
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (GLvoid*)0);
}
void Object::BindAndDraw()
void AbstractObject::BindAndDraw()
{
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, (GLvoid*)0);
glBindVertexArray(0);
}
void Object::RegisterVertexAttribPointer(GLuint index, const VertexAttribute& topology)
void AbstractObject::RegisterVertexAttribPointer(GLuint index, const VertexAttribute& topology)
{
glVertexAttribPointer(topology.index, topology.size, topology.type, topology.normalized, topology.stride, topology.pointer);
glEnableVertexAttribArray(index);

View file

@ -15,10 +15,10 @@ namespace oglu
}
std::shared_ptr<AbstractShader> MakeShader(const char* vertexShaderFile, const char* fragmentShaderFile)
Shader MakeShader(const char* vertexShaderFile, const char* fragmentShaderFile)
{
AbstractShader* tmp = new AbstractShader(vertexShaderFile, fragmentShaderFile);
return std::shared_ptr<AbstractShader>(tmp);
return Shader(tmp);
}
AbstractShader::AbstractShader(const char* vertexShaderFile, const char* fragmentShaderFile) :