mirror of
https://github.com/guezoloic/LearnOpenGL.git
synced 2026-01-25 03:34:15 +00:00
feat: rework ebo, vao and vbo
This commit is contained in:
@@ -5,16 +5,17 @@
|
|||||||
|
|
||||||
#include "glad/glad.h"
|
#include "glad/glad.h"
|
||||||
|
|
||||||
|
|
||||||
class EBO
|
class EBO
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
GLuint id;
|
GLuint id;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EBO(unsigned int* indices, size_t size);
|
EBO();
|
||||||
~EBO();
|
~EBO();
|
||||||
|
|
||||||
|
void setData(unsigned int* indices, size_t size);
|
||||||
|
|
||||||
void bind();
|
void bind();
|
||||||
void unbind();
|
void unbind();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -11,9 +11,11 @@ class VBO
|
|||||||
unsigned int id;
|
unsigned int id;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
VBO(GLfloat* vertices, size_t size);
|
VBO();
|
||||||
~VBO();
|
~VBO();
|
||||||
|
|
||||||
|
void setData(GLfloat* vertices, size_t size);
|
||||||
|
|
||||||
void bind();
|
void bind();
|
||||||
void unbind();
|
void unbind();
|
||||||
};
|
};
|
||||||
|
|||||||
12
src/ebo.cpp
12
src/ebo.cpp
@@ -1,14 +1,16 @@
|
|||||||
#include "ebo.hpp"
|
#include "ebo.hpp"
|
||||||
|
|
||||||
EBO::EBO(unsigned int* indices, size_t size)
|
EBO::EBO() : id(0) {}
|
||||||
|
|
||||||
|
void EBO::setData(unsigned int* indices, size_t size)
|
||||||
{
|
{
|
||||||
glGenBuffers(1, &id);
|
if (this->id == 0) glGenBuffers(1, &this->id);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW);
|
||||||
}
|
}
|
||||||
|
|
||||||
EBO::~EBO() { glDeleteBuffers(1, &id); }
|
EBO::~EBO() { glDeleteBuffers(1, &this->id); }
|
||||||
|
|
||||||
void EBO::bind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); }
|
void EBO::bind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->id); }
|
||||||
|
|
||||||
void EBO::unbind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); }
|
void EBO::unbind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); }
|
||||||
@@ -11,7 +11,6 @@ void VAO::unbind() { glBindVertexArray(0); }
|
|||||||
void VAO::setAttributePointer(GLuint index, GLuint size, GLenum type,
|
void VAO::setAttributePointer(GLuint index, GLuint size, GLenum type,
|
||||||
GLsizei stride, const void* offset)
|
GLsizei stride, const void* offset)
|
||||||
{
|
{
|
||||||
bind();
|
|
||||||
glEnableVertexAttribArray(index);
|
glEnableVertexAttribArray(index);
|
||||||
glVertexAttribPointer(index, size, type, GL_FALSE, stride, offset);
|
glVertexAttribPointer(index, size, type, GL_FALSE, stride, offset);
|
||||||
}
|
}
|
||||||
@@ -19,12 +18,10 @@ void VAO::setAttributePointer(GLuint index, GLuint size, GLenum type,
|
|||||||
void VAO::drawElement(GLenum mode, GLsizei count, GLenum type,
|
void VAO::drawElement(GLenum mode, GLsizei count, GLenum type,
|
||||||
const void* indices)
|
const void* indices)
|
||||||
{
|
{
|
||||||
bind();
|
|
||||||
glDrawElements(mode, count, type, indices);
|
glDrawElements(mode, count, type, indices);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VAO::drawArray(GLenum mode, GLint first, GLsizei count)
|
void VAO::drawArray(GLenum mode, GLint first, GLsizei count)
|
||||||
{
|
{
|
||||||
bind();
|
|
||||||
glDrawArrays(mode, first, count);
|
glDrawArrays(mode, first, count);
|
||||||
}
|
}
|
||||||
12
src/vbo.cpp
12
src/vbo.cpp
@@ -1,14 +1,16 @@
|
|||||||
#include "vbo.hpp"
|
#include "vbo.hpp"
|
||||||
|
|
||||||
VBO::VBO(GLfloat* vertices, size_t size)
|
VBO::VBO() : id(0) {}
|
||||||
|
|
||||||
|
void VBO::setData(GLfloat* vertices, size_t size)
|
||||||
{
|
{
|
||||||
glGenBuffers(1, &id);
|
if (this->id == 0) glGenBuffers(1, &this->id);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, id);
|
glBindBuffer(GL_ARRAY_BUFFER, this->id);
|
||||||
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
|
||||||
}
|
}
|
||||||
|
|
||||||
VBO::~VBO() { glDeleteBuffers(1, &id); }
|
VBO::~VBO() { glDeleteBuffers(1, &this->id); }
|
||||||
|
|
||||||
void VBO::bind() { glBindBuffer(GL_ARRAY_BUFFER, id); }
|
void VBO::bind() { glBindBuffer(GL_ARRAY_BUFFER, this->id); }
|
||||||
|
|
||||||
void VBO::unbind() { glBindBuffer(GL_ARRAY_BUFFER, 0); }
|
void VBO::unbind() { glBindBuffer(GL_ARRAY_BUFFER, 0); }
|
||||||
Reference in New Issue
Block a user