]> git.localhorst.tv Git - blank.git/blobdiff - src/model/model.cpp
sky box model & shader
[blank.git] / src / model / model.cpp
index 423e00d842150202a6948cc6ec3ad4be879a12b9..7aaf8f613b931a595e16a89800197260dd6fd044 100644 (file)
 #include "BlockModel.hpp"
-#include "Model.hpp"
+#include "EntityModel.hpp"
 #include "OutlineModel.hpp"
+#include "SkyBoxModel.hpp"
+#include "SpriteModel.hpp"
 
+#include "shapes.hpp"
+
+#include <algorithm>
 #include <iostream>
 
 
 namespace blank {
 
-Model::Model() noexcept
-: va(0)
-, handle{}
-, count(0) {
-       glGenVertexArrays(1, &va);
-       glGenBuffers(ATTRIB_COUNT, handle);
-}
-
-Model::~Model() noexcept {
-       glDeleteBuffers(ATTRIB_COUNT, handle);
-       glDeleteVertexArrays(1, &va);
-}
-
-Model::Model(Model &&other) noexcept
-: va(other.va)
-, count(other.count) {
-       other.va = 0;
-       for (int i = 0; i < ATTRIB_COUNT; ++i) {
-               handle[i] = other.handle[i];
-               other.handle[i] = 0;
-       }
-}
-
-Model &Model::operator =(Model &&other) noexcept {
-       std::swap(va, other.va);
-       for (int i = 0; i < ATTRIB_COUNT; ++i) {
-               std::swap(handle[i], other.handle[i]);
-       }
-       count = other.count;
-       return *this;
-}
-
-void Model::Update(const Buffer &buf) noexcept {
-       glBindVertexArray(va);
-       glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
-       glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW);
-       glEnableVertexAttribArray(ATTRIB_VERTEX);
-       glVertexAttribPointer(
-               ATTRIB_VERTEX, // location (for shader)
-               3,             // size
-               GL_FLOAT,      // type
-               GL_FALSE,      // normalized
-               0,             // stride
-               nullptr        // offset
-       );
-
+void EntityModel::Update(const Buffer &buf) noexcept {
 #ifndef NDEBUG
+       if (buf.tex_coords.size() < buf.vertices.size()) {
+               std::cerr << "EntityModel: not enough tex coords!" << std::endl;
+       }
        if (buf.colors.size() < buf.vertices.size()) {
-               std::cerr << "Model: not enough colors!" << std::endl;
+               std::cerr << "EntityModel: not enough colors!" << std::endl;
        }
-#endif
-       glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
-       glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW);
-       glEnableVertexAttribArray(ATTRIB_COLOR);
-       glVertexAttribPointer(
-               ATTRIB_COLOR,  // location (for shader)
-               3,             // size
-               GL_FLOAT,      // type
-               GL_FALSE,      // normalized
-               0,             // stride
-               nullptr        // offset
-       );
-
-#ifndef NDEBUG
        if (buf.normals.size() < buf.vertices.size()) {
-               std::cerr << "Model: not enough normals!" << std::endl;
+               std::cerr << "EntityModel: not enough normals!" << std::endl;
        }
 #endif
-       glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]);
-       glBufferData(GL_ARRAY_BUFFER, buf.normals.size() * sizeof(glm::vec3), buf.normals.data(), GL_STATIC_DRAW);
-       glEnableVertexAttribArray(ATTRIB_NORMAL);
-       glVertexAttribPointer(
-               ATTRIB_NORMAL, // location (for shader)
-               3,             // size
-               GL_FLOAT,      // type
-               GL_FALSE,      // normalized
-               0,             // stride
-               nullptr        // offset
-       );
-
-       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
-       glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW);
-       count = buf.indices.size();
-}
 
-
-void Model::Draw() const noexcept {
-       glBindVertexArray(va);
-       glDrawElements(
-               GL_TRIANGLES,    // how
-               count,           // count
-               GL_UNSIGNED_INT, // type
-               nullptr          // offset
-       );
+       vao.Bind();
+       vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
+       vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords);
+       vao.PushAttribute(ATTRIB_COLOR, buf.colors);
+       vao.PushAttribute(ATTRIB_NORMAL, buf.normals);
+       vao.PushIndices(ATTRIB_INDEX, buf.indices);
 }
 
 
-BlockModel::BlockModel() noexcept
-: va(0)
-, handle{}
-, count(0) {
-       glGenVertexArrays(1, &va);
-       glGenBuffers(ATTRIB_COUNT, handle);
-}
-
-BlockModel::~BlockModel() noexcept {
-       glDeleteBuffers(ATTRIB_COUNT, handle);
-       glDeleteVertexArrays(1, &va);
-}
-
-BlockModel::BlockModel(BlockModel &&other) noexcept
-: va(other.va)
-, count(other.count) {
-       other.va = 0;
-       for (int i = 0; i < ATTRIB_COUNT; ++i) {
-               handle[i] = other.handle[i];
-               other.handle[i] = 0;
-       }
+void EntityModel::Draw() const noexcept {
+       vao.DrawTriangleElements();
 }
 
-BlockModel &BlockModel::operator =(BlockModel &&other) noexcept {
-       std::swap(va, other.va);
-       for (int i = 0; i < ATTRIB_COUNT; ++i) {
-               std::swap(handle[i], other.handle[i]);
-       }
-       count = other.count;
-       return *this;
-}
 
 void BlockModel::Update(const Buffer &buf) noexcept {
-       glBindVertexArray(va);
-       glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
-       glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW);
-       glEnableVertexAttribArray(ATTRIB_VERTEX);
-       glVertexAttribPointer(
-               ATTRIB_VERTEX, // location (for shader)
-               3,             // size
-               GL_FLOAT,      // type
-               GL_FALSE,      // normalized
-               0,             // stride
-               nullptr        // offset
-       );
-
 #ifndef NDEBUG
+       if (buf.tex_coords.size() < buf.vertices.size()) {
+               std::cerr << "BlockModel: not enough tex coords!" << std::endl;
+       }
        if (buf.colors.size() < buf.vertices.size()) {
                std::cerr << "BlockModel: not enough colors!" << std::endl;
        }
-#endif
-       glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
-       glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW);
-       glEnableVertexAttribArray(ATTRIB_COLOR);
-       glVertexAttribPointer(
-               ATTRIB_COLOR,  // location (for shader)
-               3,             // size
-               GL_FLOAT,      // type
-               GL_FALSE,      // normalized
-               0,             // stride
-               nullptr        // offset
-       );
-
-#ifndef NDEBUG
        if (buf.lights.size() < buf.vertices.size()) {
                std::cerr << "BlockModel: not enough lights!" << std::endl;
        }
 #endif
-       glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_LIGHT]);
-       glBufferData(GL_ARRAY_BUFFER, buf.lights.size() * sizeof(float), buf.lights.data(), GL_STATIC_DRAW);
-       glEnableVertexAttribArray(ATTRIB_LIGHT);
-       glVertexAttribPointer(
-               ATTRIB_LIGHT, // location (for shader)
-               1,            // size
-               GL_FLOAT,     // type
-               GL_FALSE,     // normalized
-               0,            // stride
-               nullptr       // offset
-       );
-
-       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
-       glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW);
-       count = buf.indices.size();
+
+       vao.Bind();
+       vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
+       vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords);
+       vao.PushAttribute(ATTRIB_COLOR, buf.colors);
+       vao.PushAttribute(ATTRIB_LIGHT, buf.lights);
+       vao.PushIndices(ATTRIB_INDEX, buf.indices);
 }
 
 
 void BlockModel::Draw() const noexcept {
-       glBindVertexArray(va);
-       glDrawElements(
-               GL_TRIANGLES,    // how
-               count,           // count
-               GL_UNSIGNED_INT, // type
-               nullptr          // offset
-       );
+       vao.DrawTriangleElements();
 }
 
-OutlineModel::OutlineModel() noexcept
-: vertices()
-, colors()
-, indices()
-, va(0)
-, handle{}
-, dirty(false) {
-       glGenVertexArrays(1, &va);
-       glGenBuffers(ATTRIB_COUNT, handle);
+
+void OutlineModel::Update(const Buffer &buf) noexcept {
+#ifndef NDEBUG
+       if (buf.colors.size() < buf.vertices.size()) {
+               std::cerr << "OutlineModel: not enough colors!" << std::endl;
+       }
+#endif
+
+       vao.Bind();
+       vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
+       vao.PushAttribute(ATTRIB_COLOR, buf.colors);
+       vao.PushIndices(ATTRIB_INDEX, buf.indices);
 }
 
-OutlineModel::~OutlineModel() noexcept {
-       glDeleteBuffers(ATTRIB_COUNT, handle);
-       glDeleteVertexArrays(1, &va);
+
+void OutlineModel::Draw() noexcept {
+       glEnable(GL_LINE_SMOOTH);
+       glLineWidth(2.0f);
+       vao.DrawLineElements();
 }
 
 
-void OutlineModel::Clear() noexcept {
-       vertices.clear();
-       colors.clear();
-       indices.clear();
-       Invalidate();
+void SkyBoxModel::LoadUnitBox() {
+       Buffer buffer;
+       CuboidShape shape({{ -1, -1, -1 }, { 1, 1, 1 }});
+       shape.Vertices(buffer);
+       Update(buffer);
 }
 
-void OutlineModel::Reserve(int v, int i) {
-       vertices.reserve(v);
-       colors.reserve(v);
-       indices.reserve(i);
+void SkyBoxModel::Update(const Buffer &buf) noexcept {
+       vao.Bind();
+       vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
+       vao.PushIndices(ATTRIB_INDEX, buf.indices);
 }
 
+void SkyBoxModel::Draw() const noexcept {
+       vao.DrawTriangleElements();
+}
 
-void OutlineModel::Update() noexcept {
-       glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
-       glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
-       glEnableVertexAttribArray(ATTRIB_VERTEX);
-       glVertexAttribPointer(
-               ATTRIB_VERTEX, // location (for shader)
-               3,             // size
-               GL_FLOAT,      // type
-               GL_FALSE,      // normalized
-               0,             // stride
-               nullptr        // offset
-       );
 
-#ifndef NDEBUG
-       if (colors.size() < vertices.size()) {
-               std::cerr << "OutlineModel: not enough colors!" << std::endl;
-               colors.resize(vertices.size(), { 1, 0, 1 });
-       }
-#endif
-       glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
-       glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
-       glEnableVertexAttribArray(ATTRIB_COLOR);
-       glVertexAttribPointer(
-               ATTRIB_COLOR,  // location (for shader)
-               3,             // size
-               GL_FLOAT,      // type
-               GL_FALSE,      // normalized
-               0,             // stride
-               nullptr        // offset
-       );
-
-       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
-       glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
-
-       dirty = false;
-}
+void SpriteModel::Buffer::LoadRect(
+       float w, float h,
+       const glm::vec2 &pivot,
+       const glm::vec2 &tex_begin,
+       const glm::vec2 &tex_end
+) {
+       Clear();
+       Reserve(4, 6);
 
+       vertices.emplace_back( -pivot.x,  -pivot.y, 0.0f);
+       vertices.emplace_back(w-pivot.x,  -pivot.y, 0.0f);
+       vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f);
+       vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f);
 
-void OutlineModel::Draw() noexcept {
-       glBindVertexArray(va);
+       coords.emplace_back(tex_begin.x, tex_begin.y);
+       coords.emplace_back(tex_end.x,   tex_begin.y);
+       coords.emplace_back(tex_begin.x, tex_end.y);
+       coords.emplace_back(tex_end.x,   tex_end.y);
+
+       indices.assign({ 0, 2, 1, 1, 2, 3 });
+}
 
-       if (dirty) {
-               Update();
+
+void SpriteModel::Update(const Buffer &buf) noexcept {
+#ifndef NDEBUG
+       if (buf.coords.size() < buf.vertices.size()) {
+               std::cerr << "SpriteModel: not enough coords!" << std::endl;
        }
+#endif
+
+       vao.Bind();
+       vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
+       vao.PushAttribute(ATTRIB_TEXCOORD, buf.coords);
+       vao.PushIndices(ATTRIB_INDEX, buf.indices);
+}
 
-       glEnable(GL_LINE_SMOOTH);
-       glLineWidth(2.0f);
 
-       glDrawElements(
-               GL_LINES,          // how
-               indices.size(),    // count
-               GL_UNSIGNED_SHORT, // type
-               nullptr            // offset
-       );
+void SpriteModel::Draw() noexcept {
+       vao.DrawTriangleElements();
 }
 
 }