144 lines
3.8 KiB
C++
144 lines
3.8 KiB
C++
#pragma once
|
|
#ifndef _NB_DRAW
|
|
#define _NB_DRAW
|
|
|
|
#include <GLLoad.h>
|
|
|
|
#include <exception>
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "Buffers.h"
|
|
#include "Shader.h"
|
|
|
|
#define THROW_DRAW_ERROR(msg) throw DrawError(msg, __FILE__, __LINE__);
|
|
|
|
namespace NB{
|
|
|
|
class DrawError : public std::runtime_error {
|
|
public:
|
|
DrawError(const std::string&, const std::string& file="", int line=-1);
|
|
};
|
|
|
|
uint8_t VerticesFromPrimitives(GLenum, unsigned int num_prims=1);
|
|
|
|
uint8_t PrimitivesFromVertices(GLenum, unsigned int);
|
|
|
|
void copyVertexAttribute(const VertexAttribute&, VertexAttribute&);
|
|
|
|
class DrawBuffer {
|
|
public:
|
|
DrawBuffer(const Shader&, VertexAttributeList, GLenum prim_type=GL_TRIANGLES);
|
|
//DrawBuffer(std::initializer_list<VertexAttribute>, std::initializer_list<VertexAttributePointer>);
|
|
|
|
unsigned int vertSize() const;
|
|
const Shader shader() const;
|
|
bool keepLocalElement() const;
|
|
bool keepLocalVertex() const;
|
|
unsigned int elementBufferID() const;
|
|
unsigned int vertexBufferID() const;
|
|
virtual void draw() const;
|
|
|
|
VertexAttributeList changeVertLayout(unsigned int, VertexAttributePointer);
|
|
bool keepLocalElement(bool);
|
|
bool keepLocalVertex(bool);
|
|
const Shader shader(const Shader&);
|
|
virtual void generateEBO(const std::vector<unsigned int>&);
|
|
virtual void generateEBO(unsigned int);
|
|
virtual void loadEBO(const std::vector<unsigned int>&, unsigned int offset=0);
|
|
virtual void generateVBO(const RawVec&);
|
|
virtual void generateVBO(unsigned int);
|
|
virtual void loadVBO(const RawVec&, unsigned int offset=0);
|
|
|
|
protected:
|
|
VAOManager<unsigned int> _VAO;
|
|
const GLenum _gl_primitive_type=0;
|
|
virtual VertexAttributeList defaultVAO(VertexAttributeList);
|
|
|
|
BufferManager<GL_ARRAY_BUFFER, unsigned char> _vert_data;
|
|
BufferManager<GL_ELEMENT_ARRAY_BUFFER, unsigned int> _elmt_data;
|
|
Shader _shader;
|
|
unsigned int _num_verts, _num_prims;
|
|
};
|
|
|
|
class DrawInstanceBuffer : public DrawBuffer {
|
|
public:
|
|
DrawInstanceBuffer(const Shader&, VertexAttributeList, VertexAttributeList, GLenum primt_type=GL_TRIANGLES);
|
|
|
|
unsigned int instSize() const;
|
|
virtual void draw() const;
|
|
|
|
VertexAttributeList changeInstanceLayout(unsigned int, VertexAttributePointer);
|
|
virtual void generateIBO(const RawVec&);
|
|
virtual void generateIBO(unsigned int);
|
|
virtual void loadIBO(const RawVec&, unsigned int offset=0);
|
|
|
|
private:
|
|
virtual VertexAttributeList defaultVAO(VertexAttributeList);
|
|
BufferManager<GL_ARRAY_BUFFER, unsigned char> _inst_data;
|
|
unsigned int _num_inst;
|
|
|
|
};
|
|
|
|
class Camera {
|
|
public:
|
|
typedef glm::vec3 Vec3;
|
|
typedef glm::mat4 Mat4;
|
|
|
|
enum CameraType : unsigned char {
|
|
None,
|
|
Orthographic,
|
|
Perspective
|
|
};
|
|
|
|
static Vec3 normCross(const Vec3&, const Vec3&);
|
|
|
|
Camera(
|
|
const Vec3& pos=Vec3(1.0f, 0.0f, 0.0f),
|
|
const Vec3& tar=Vec3(0.0f, 0.0f, 0.0f),
|
|
const Vec3& up =Vec3(0.0f, 1.0f, 0.0f)
|
|
);
|
|
|
|
Vec3 getPos() const;
|
|
Vec3 getTarget() const;
|
|
Vec3 getDirection() const;
|
|
Vec3 getWorldUp() const;
|
|
Vec3 getUp() const;
|
|
Vec3 getRight() const;
|
|
Mat4 getLookMatrix() const;
|
|
Mat4 getProjectionMatrix() const;
|
|
Mat4 getFlattenedMatrices() const;
|
|
|
|
void setProjection(const Mat4&);
|
|
void setPos(const Vec3&);
|
|
void setTarget(const Vec3&);
|
|
void setDirection(const Vec3&);
|
|
void setWorldUp(const Vec3&);
|
|
void addPos(const Vec3&);
|
|
void panPos(const Vec3&);
|
|
void addTarget(const Vec3&);
|
|
void roll(float);
|
|
void pitch(float);
|
|
void yaw(float);
|
|
|
|
protected:
|
|
Vec3 _pos;
|
|
Vec3 _tar;
|
|
Vec3 _dir;
|
|
Vec3 _world_up;
|
|
Vec3 _up;
|
|
Vec3 _right;
|
|
Mat4 _look;
|
|
Mat4 _proj;
|
|
const CameraType _camera_type;
|
|
|
|
void _calcLook();
|
|
|
|
};
|
|
|
|
}
|
|
#endif |