mirror of
https://github.com/Linloir/SceneEditor.git
synced 2025-12-17 07:28:12 +08:00
Implement VBO EBO VAO
This commit is contained in:
parent
5bc0cb6c6f
commit
64b4116979
@ -1 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include "ebo.h"
|
||||
#include "utils.h"
|
||||
|
||||
ElementBufferObject::ElementBufferObject() {
|
||||
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id);
|
||||
}
|
||||
|
||||
ElementBufferObject::ElementBufferObject(const std::vector<unsigned int>& indices) :
|
||||
_indices(indices) {
|
||||
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBufferData(
|
||||
GL_ELEMENT_ARRAY_BUFFER,
|
||||
_indices.size() * sizeof(unsigned int),
|
||||
_indices.data(),
|
||||
GL_STATIC_DRAW
|
||||
);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
ElementBufferObject::ElementBufferObject(std::vector<unsigned int>&& indices) :
|
||||
_indices(std::move(indices)) {
|
||||
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBufferData(
|
||||
GL_ELEMENT_ARRAY_BUFFER,
|
||||
_indices.size() * sizeof(unsigned int),
|
||||
_indices.data(),
|
||||
GL_STATIC_DRAW
|
||||
);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
inline void ElementBufferObject::dispose() const {
|
||||
OPENGL_EXTRA_FUNCTIONS->glDeleteBuffers(1, &_id);
|
||||
}
|
||||
|
||||
@ -5,13 +5,16 @@
|
||||
class ElementBufferObject
|
||||
{
|
||||
private:
|
||||
unsigned int _id;
|
||||
unsigned int _id = 0;
|
||||
std::vector<unsigned int> _indices;
|
||||
|
||||
public:
|
||||
ElementBufferObject(std::vector<unsigned int> indices);
|
||||
ElementBufferObject();
|
||||
ElementBufferObject(const std::vector<unsigned int>& indices);
|
||||
ElementBufferObject(std::vector<unsigned int>&& indices);
|
||||
|
||||
inline unsigned int id() const { return _id; }
|
||||
|
||||
inline void bind() const;
|
||||
inline void unbind() const;
|
||||
inline std::vector<unsigned int> indices() const { return _indices; }
|
||||
|
||||
inline void dispose() const;
|
||||
};
|
||||
@ -20,7 +20,7 @@ void Model::loadModel(std::string path) {
|
||||
|
||||
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
|
||||
Logger::error("Failed to load model: " + std::string(importer.GetErrorString()));
|
||||
_status = ERROR;
|
||||
_status = ERR;
|
||||
return;
|
||||
}
|
||||
_directory = path.substr(0, path.find_last_of('/'));
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
|
||||
class Model {
|
||||
public:
|
||||
enum MODELSTATUS { LOADING, LOADED, ERROR};
|
||||
enum MODELSTATUS { LOADING, LOADED, ERR };
|
||||
|
||||
private:
|
||||
std::vector<Mesh> _meshes;
|
||||
@ -24,7 +24,7 @@ public:
|
||||
~Model();
|
||||
|
||||
public:
|
||||
inline MODELSTATUS isReady() const { return _status; }
|
||||
inline MODELSTATUS status() const { return _status; }
|
||||
|
||||
private:
|
||||
void loadModel(std::string path);
|
||||
|
||||
@ -1,6 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <STBImage/stb_image.h>
|
||||
#include <qopengl.h>
|
||||
#include <qopenglcontext.h>
|
||||
|
||||
#include "texture.h"
|
||||
#include "logger.h"
|
||||
#include "utils.h"
|
||||
|
||||
QOpenGLContext* sharedContext;
|
||||
|
||||
// Must set context before calling this function
|
||||
Texture::Texture(TextureType type, std::string path) {
|
||||
_type = type;
|
||||
_path = path;
|
||||
|
||||
OPENGL_FUNCTIONS->glGenTextures(1, &_id);
|
||||
OPENGL_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, _id);
|
||||
|
||||
// Set the texture wrapping parameters
|
||||
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture wrapping to GL_REPEAT (usually basic wrapping method)
|
||||
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
// Set texture filtering parameters
|
||||
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
// Load image, create texture and generate mipmaps
|
||||
int width, height, nrChannels;
|
||||
unsigned char* data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0);
|
||||
if (data) {
|
||||
GLenum format;
|
||||
if (nrChannels == 1) {
|
||||
format = GL_RED;
|
||||
}
|
||||
else if (nrChannels == 3) {
|
||||
format = GL_RGB;
|
||||
}
|
||||
else if (nrChannels == 4) {
|
||||
format = GL_RGBA;
|
||||
}
|
||||
|
||||
OPENGL_FUNCTIONS->glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
|
||||
OPENGL_FUNCTIONS->glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else {
|
||||
Logger::error("Failed to load texture: " + path);
|
||||
}
|
||||
stbi_image_free(data);
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <qopenglcontext.h>
|
||||
|
||||
enum TextureType { DIFFUSE, SPECULAR };
|
||||
|
||||
|
||||
@ -1,3 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <qopenglcontext.h>
|
||||
#include <qopenglfunctions.h>
|
||||
#include <qopenglextrafunctions.h>
|
||||
|
||||
#define offsetof(s,m) (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m))
|
||||
|
||||
#define OPENGL_FUNCTIONS QOpenGLContext::currentContext()->functions()
|
||||
#define OPENGL_EXTRA_FUNCTIONS QOpenGLContext::currentContext()->extraFunctions()
|
||||
|
||||
@ -1 +1,107 @@
|
||||
#pragma once
|
||||
|
||||
#include <qopengl.h>
|
||||
|
||||
#include "vao.h"
|
||||
#include "utils.h"
|
||||
#include "logger.h"
|
||||
|
||||
VertexArrayObject::VertexArrayObject() {
|
||||
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id);
|
||||
}
|
||||
|
||||
VertexArrayObject::VertexArrayObject(const VertexBufferObject& vbo) :
|
||||
_vbo(vbo) {
|
||||
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
|
||||
}
|
||||
|
||||
VertexArrayObject::VertexArrayObject(VertexBufferObject&& vbo) :
|
||||
_vbo(std::move(vbo)) {
|
||||
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
|
||||
}
|
||||
|
||||
VertexArrayObject::VertexArrayObject(const VertexBufferObject& vbo, const ElementBufferObject& ebo) :
|
||||
_vbo(vbo), _ebo(ebo) {
|
||||
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id());
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
|
||||
}
|
||||
|
||||
VertexArrayObject::VertexArrayObject(VertexBufferObject&& vbo, ElementBufferObject&& ebo) :
|
||||
_vbo(std::move(vbo)), _ebo(std::move(ebo)) {
|
||||
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id());
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
|
||||
}
|
||||
|
||||
inline void VertexArrayObject::setActive() const {
|
||||
// Check is id is valid
|
||||
if (_id == -1) {
|
||||
Logger::error("Binding an invalid VertexArrayObject");
|
||||
return;
|
||||
}
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
|
||||
}
|
||||
|
||||
inline void VertexArrayObject::setInactive() const {
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
|
||||
}
|
||||
|
||||
inline void VertexArrayObject::bindVertexBufferObject(const VertexBufferObject& vbo) {
|
||||
_vbo = vbo;
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
|
||||
}
|
||||
|
||||
inline void VertexArrayObject::bindVertexBufferObject(VertexBufferObject&& vbo) {
|
||||
_vbo = std::move(vbo);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
|
||||
}
|
||||
|
||||
inline void VertexArrayObject::bindElementBufferObject(const ElementBufferObject& ebo) {
|
||||
_ebo = ebo;
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id());
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
|
||||
}
|
||||
|
||||
inline void VertexArrayObject::bindElementBufferObject(ElementBufferObject&& ebo) {
|
||||
_ebo = std::move(ebo);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id());
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
|
||||
}
|
||||
|
||||
inline void VertexArrayObject::setVertexAttributePointer(
|
||||
unsigned int index,
|
||||
int size,
|
||||
unsigned int type,
|
||||
bool normalized,
|
||||
unsigned int stride,
|
||||
const void* pointer
|
||||
) const {
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glVertexAttribPointer(
|
||||
index,
|
||||
size,
|
||||
type,
|
||||
normalized,
|
||||
stride,
|
||||
pointer
|
||||
);
|
||||
OPENGL_EXTRA_FUNCTIONS->glEnableVertexAttribArray(index);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
|
||||
}
|
||||
@ -7,20 +7,28 @@
|
||||
|
||||
class VertexArrayObject {
|
||||
private:
|
||||
unsigned int _id;
|
||||
unsigned int _id = 0;
|
||||
VertexBufferObject _vbo;
|
||||
ElementBufferObject _ebo;
|
||||
|
||||
public:
|
||||
VertexArrayObject(VertexBufferObject vbo);
|
||||
VertexArrayObject(std::vector<Vertex> vertices);
|
||||
VertexArrayObject(VertexBufferObject vbo, ElementBufferObject ebo);
|
||||
VertexArrayObject(std::vector<Vertex> vertices, std::vector<unsigned int> indices);
|
||||
VertexArrayObject();
|
||||
VertexArrayObject(const VertexBufferObject& vbo);
|
||||
VertexArrayObject(VertexBufferObject&& vbo);
|
||||
VertexArrayObject(const VertexBufferObject& vbo, const ElementBufferObject& ebo);
|
||||
VertexArrayObject(VertexBufferObject&& vbo, ElementBufferObject&& ebo);
|
||||
|
||||
inline unsigned int id() const { return _id; }
|
||||
|
||||
inline void setActive() const;
|
||||
inline void setInactive() const;
|
||||
|
||||
void setVertexAttributePointer(unsigned int index, int size, unsigned int type, bool normalized, unsigned int stride, const void* pointer) const;
|
||||
inline void bindVertexBufferObject(const VertexBufferObject& vbo);
|
||||
inline void bindVertexBufferObject(VertexBufferObject&& vbo);
|
||||
inline void bindElementBufferObject(const ElementBufferObject& ebo);
|
||||
inline void bindElementBufferObject(ElementBufferObject&& ebo);
|
||||
|
||||
inline void setVertexAttributePointer(unsigned int index, int size, unsigned int type, bool normalized, unsigned int stride, const void* pointer) const;
|
||||
inline void enableVertexAttribute(unsigned int index) const;
|
||||
inline void disableVertexAttribute(unsigned int index) const;
|
||||
};
|
||||
@ -1 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include "vbo.h"
|
||||
#include "utils.h"
|
||||
|
||||
VertexBufferObject::VertexBufferObject() {
|
||||
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id);
|
||||
}
|
||||
|
||||
VertexBufferObject::VertexBufferObject(const std::vector<Vertex>& vertices) :
|
||||
_vertices(vertices) {
|
||||
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBufferData(
|
||||
GL_ARRAY_BUFFER,
|
||||
_vertices.size() * sizeof(Vertex),
|
||||
_vertices.data(),
|
||||
GL_STATIC_DRAW
|
||||
);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
VertexBufferObject::VertexBufferObject(std::vector<Vertex>&& vertices) :
|
||||
_vertices(std::move(vertices)) {
|
||||
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _id);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBufferData(
|
||||
GL_ARRAY_BUFFER,
|
||||
_vertices.size() * sizeof(Vertex),
|
||||
_vertices.data(),
|
||||
GL_STATIC_DRAW
|
||||
);
|
||||
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
inline void VertexBufferObject::dispose() const {
|
||||
OPENGL_EXTRA_FUNCTIONS->glDeleteBuffers(1, &_id);
|
||||
}
|
||||
|
||||
@ -6,14 +6,16 @@
|
||||
|
||||
class VertexBufferObject {
|
||||
private:
|
||||
unsigned int _id;
|
||||
unsigned int _id = 0;
|
||||
std::vector<Vertex> _vertices;
|
||||
|
||||
public:
|
||||
VertexBufferObject(std::vector<Vertex> vertices);
|
||||
~VertexBufferObject();
|
||||
VertexBufferObject();
|
||||
VertexBufferObject(const std::vector<Vertex>& vertices);
|
||||
VertexBufferObject(std::vector<Vertex>&& vertices);
|
||||
|
||||
inline unsigned int id() const { return _id; }
|
||||
|
||||
inline void bind() const;
|
||||
inline void unbind() const;
|
||||
inline std::vector<Vertex> vertices() const { return _vertices; }
|
||||
|
||||
inline void dispose() const;
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user