Long time no see, created drawing lib
This commit is contained in:
parent
c7dd8e6b49
commit
c3b76c4d57
37
.vscode/settings.json
vendored
37
.vscode/settings.json
vendored
@ -47,6 +47,41 @@
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"typeinfo": "cpp"
|
||||
"typeinfo": "cpp",
|
||||
"charconv": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"format": "cpp",
|
||||
"ios": "cpp",
|
||||
"locale": "cpp",
|
||||
"mutex": "cpp",
|
||||
"queue": "cpp",
|
||||
"ranges": "cpp",
|
||||
"ratio": "cpp",
|
||||
"span": "cpp",
|
||||
"stop_token": "cpp",
|
||||
"thread": "cpp",
|
||||
"xfacet": "cpp",
|
||||
"xiosbase": "cpp",
|
||||
"xlocale": "cpp",
|
||||
"xlocbuf": "cpp",
|
||||
"xlocinfo": "cpp",
|
||||
"xlocmes": "cpp",
|
||||
"xlocmon": "cpp",
|
||||
"xlocnum": "cpp",
|
||||
"xloctime": "cpp",
|
||||
"xmemory": "cpp",
|
||||
"xstring": "cpp",
|
||||
"xtr1common": "cpp",
|
||||
"xutility": "cpp",
|
||||
"chrono": "cpp",
|
||||
"condition_variable": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"semaphore": "cpp",
|
||||
"variant": "cpp",
|
||||
"forward_list": "cpp",
|
||||
"map": "cpp",
|
||||
"xstddef": "cpp",
|
||||
"xtree": "cpp"
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,10 @@
|
||||
cmake_minimum_required(VERSION 3.5.0)
|
||||
project(GraphicsTest VERSION 0.1.0 LANGUAGES C CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
include(CTest)
|
||||
enable_testing()
|
||||
|
||||
@ -11,11 +15,10 @@ include(CPack)
|
||||
include_directories(./dep ./include)
|
||||
link_directories(../libs/glfw-3.3.9/build/src ../libs/NBEngine/lib)
|
||||
|
||||
add_library(NBDraw Draw.cpp)
|
||||
add_library(NBDrawing Draw.cpp Shader.cpp)
|
||||
|
||||
|
||||
add_executable(GraphicsTest main.cpp funcs.cpp shader.cpp ../libs/glad/src/glad.c)
|
||||
target_link_libraries(GraphicsTest glfw3 NBWindows NBEvents)
|
||||
add_executable(GraphicsTest main.cpp funcs.cpp Shader.cpp Draw.cpp ../libs/glad/src/glad.c)
|
||||
target_link_libraries(GraphicsTest glfw3 NBWindows NBDrawing)
|
||||
|
||||
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
|
||||
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
|
||||
|
||||
206
Draw.cpp
206
Draw.cpp
@ -2,4 +2,210 @@
|
||||
|
||||
namespace NB{
|
||||
|
||||
DrawWarning::DrawWarning(const std::string& msg) : std::runtime_error(msg) {}
|
||||
|
||||
uint8_t GLSLTypeSize(GLenum type) {
|
||||
switch(type) {
|
||||
case GL_SHORT:
|
||||
case GL_UNSIGNED_SHORT:
|
||||
case GL_HALF_FLOAT:
|
||||
return 2;
|
||||
break;
|
||||
case GL_INT:
|
||||
case GL_UNSIGNED_INT:
|
||||
case GL_FLOAT:
|
||||
return 4;
|
||||
break;
|
||||
case GL_DOUBLE:
|
||||
return 8;
|
||||
break;
|
||||
default:
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t GLSLPrimitiveVertexSize(GLenum type, unsigned int num_prims) {
|
||||
switch(type) {
|
||||
case GL_POINTS:
|
||||
return num_prims;
|
||||
case GL_LINES:
|
||||
return num_prims*2;
|
||||
break;
|
||||
case GL_LINE_STRIP:
|
||||
return num_prims+1;
|
||||
break;
|
||||
case GL_LINE_LOOP:
|
||||
return num_prims;
|
||||
break;
|
||||
case GL_TRIANGLES:
|
||||
return num_prims*3;
|
||||
break;
|
||||
case GL_TRIANGLE_STRIP:
|
||||
case GL_TRIANGLE_FAN:
|
||||
return num_prims+2;
|
||||
break;
|
||||
case GL_QUADS:
|
||||
return 4*num_prims;
|
||||
break;
|
||||
case GL_QUAD_STRIP:
|
||||
return 2*num_prims+2;
|
||||
break;
|
||||
default:
|
||||
return num_prims;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
DrawBuffer::DrawBuffer(
|
||||
std::vector<VertexAttribute> vas,
|
||||
const Shader& shader
|
||||
) : _vertex_attributes(vas), _shader(shader),
|
||||
_vert_data(GL_ARRAY_BUFFER), _elmt_data(GL_ELEMENT_ARRAY_BUFFER) {}
|
||||
|
||||
unsigned int DrawBuffer::vertSize() const {
|
||||
size_t num_attrs = _vertex_attributes.size();
|
||||
size_t vert_size = 0;
|
||||
for (int i = 0; i < num_attrs; ++i) {
|
||||
vert_size += _vertex_attributes[i].GLSLSize*GLSLTypeSize(_vertex_attributes[i].GLSLType);
|
||||
}
|
||||
return vert_size;
|
||||
}
|
||||
|
||||
std::vector<VertexAttributePointer> DrawBuffer::getLayout() const {
|
||||
unsigned int num_attrs = _vertex_attributes.size();
|
||||
std::vector<VertexAttributePointer> ret(num_attrs);
|
||||
for (int i = 0; i < num_attrs; ++i) {
|
||||
ret[i] = _vertex_attributes[i].ptr;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void DrawBuffer::draw() const {
|
||||
_elmt_data.bind();
|
||||
glDrawElements(_gl_primitive_type, _elmt_data.size(), GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
|
||||
const Shader DrawBuffer::shader() const {
|
||||
return _shader;
|
||||
}
|
||||
|
||||
bool DrawBuffer::keepLocalElement() const { return _elmt_data.keepLocal(); }
|
||||
|
||||
bool DrawBuffer::keepLocalVertex() const { return _vert_data.keepLocal(); }
|
||||
|
||||
unsigned int DrawBuffer::elementBufferID() const { return _elmt_data.id(); }
|
||||
|
||||
unsigned int DrawBuffer::vertexBufferID() const { return _vert_data.id(); }
|
||||
|
||||
bool DrawBuffer::keepLocalElement(bool keep) { return _elmt_data.keepLocal(keep); }
|
||||
|
||||
bool DrawBuffer::keepLocalVertex(bool keep) { return _vert_data.keepLocal(keep); }
|
||||
|
||||
const Shader DrawBuffer::shader(const Shader& new_shader) {
|
||||
_shader = new_shader;
|
||||
return _shader;
|
||||
}
|
||||
|
||||
std::vector<VertexAttributePointer> DrawBuffer::setLayout() {
|
||||
int vert_size = vertSize();
|
||||
unsigned int num_attrs = _vertex_attributes.size();
|
||||
std::vector<VertexAttributePointer> ret(num_attrs);
|
||||
unsigned int curr_byte = 0;
|
||||
for (int i = 0; i < num_attrs; ++i) {
|
||||
_vertex_attributes[i].ptr.stride = vert_size;
|
||||
_vertex_attributes[i].ptr.offset = curr_byte;
|
||||
curr_byte += _vertex_attributes[i].GLSLSize * GLSLTypeSize(_vertex_attributes[i].GLSLType);
|
||||
ret[i] = _vertex_attributes[i].ptr;
|
||||
}
|
||||
|
||||
return getLayout();
|
||||
}
|
||||
|
||||
std::vector<VertexAttributePointer> DrawBuffer::setLayout(std::vector<VertexAttributePointer> vap) {
|
||||
unsigned int num_attrs = _vertex_attributes.size();
|
||||
if (vap.size() != num_attrs) {
|
||||
throw DrawWarning("DrawBuffer Vertex Attributes and passed Vertex Attributes do not match.");
|
||||
}
|
||||
for (int i = 0; i < num_attrs; ++i) {
|
||||
if (vap[i].stride == -1) {
|
||||
throw DrawWarning("Unset Vertex Attribute Pointer in index " + std::to_string(i) + ".");
|
||||
}
|
||||
_vertex_attributes[i].ptr = vap[i];
|
||||
}
|
||||
|
||||
return getLayout();
|
||||
}
|
||||
|
||||
void DrawBuffer::generateElementBuffer(unsigned int num_prims, GLenum gl_primitive_type) {
|
||||
generateElementBuffer(std::vector<unsigned int>(GLSLPrimitiveVertexSize(gl_primitive_type, num_prims)));
|
||||
}
|
||||
|
||||
void DrawBuffer::generateElementBuffer(std::vector<unsigned int> elements, GLenum gl_primitive_type) {
|
||||
_num_prims = elements.size();
|
||||
_gl_primitive_type = gl_primitive_type;
|
||||
_elmt_data.setData(elements);
|
||||
}
|
||||
|
||||
void DrawBuffer::generateVertexBuffer(unsigned int num_verts) {
|
||||
generateVertexBuffer(std::vector<unsigned char>(num_verts * vertSize()));
|
||||
}
|
||||
|
||||
void DrawBuffer::generateVertexBuffer(std::vector<unsigned char> vert_data) {
|
||||
int vert_size = vertSize();
|
||||
if (vert_data.size() % vert_size != 0) {
|
||||
throw DrawWarning("Starting data does not match vertex size of " + std::to_string(vert_size) + ".");
|
||||
}
|
||||
_num_verts = vert_data.size() / vert_size;
|
||||
unsigned int num_attrs = _vertex_attributes.size();
|
||||
for (int i = 0; i < num_attrs; ++i) {
|
||||
if (_vertex_attributes[i].ptr.stride == -1) {
|
||||
setLayout();
|
||||
i = num_attrs+1;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Make it not static draw all the time
|
||||
_vert_data.setData(vert_data);
|
||||
|
||||
glGenVertexArrays(1, &_VAO);
|
||||
glBindVertexArray(_VAO);
|
||||
_vert_data.bind();
|
||||
for (GLuint i = 0; i < num_attrs; ++i) {
|
||||
glVertexAttribPointer(
|
||||
i,
|
||||
_vertex_attributes[i].GLSLSize,
|
||||
_vertex_attributes[i].GLSLType,
|
||||
_vertex_attributes[i].GLSLNormalization,
|
||||
_vertex_attributes[i].ptr.stride,
|
||||
(void*)(_vertex_attributes[i].ptr.offset)
|
||||
);
|
||||
glEnableVertexAttribArray(i);
|
||||
}
|
||||
_elmt_data.bind();
|
||||
}
|
||||
|
||||
void DrawBuffer::loadElementBuffer(const std::vector<unsigned int>& elements, unsigned int offset, GLenum gl_primitive_type) {
|
||||
if (!_elmt_data.isInitialized()) {
|
||||
std::vector<unsigned int> load_data(offset);
|
||||
load_data.insert(load_data.end(), elements.begin(), elements.end());
|
||||
generateElementBuffer(load_data);
|
||||
} else {
|
||||
if (gl_primitive_type != _gl_primitive_type) {
|
||||
throw DrawWarning("Attempting to overload GL primitive type.");
|
||||
}
|
||||
_elmt_data.loadData(elements, offset);
|
||||
}
|
||||
}
|
||||
|
||||
void DrawBuffer::loadVertexBuffer(const std::vector<unsigned char>& data, unsigned int offset) {
|
||||
if (!_vert_data.isInitialized()) {
|
||||
std::vector<unsigned char> load_data(offset);
|
||||
load_data.insert(load_data.end(), data.begin(), data.end());
|
||||
generateVertexBuffer(load_data);
|
||||
} else {
|
||||
_vert_data.loadData(data, offset);
|
||||
}}
|
||||
|
||||
}
|
||||
59
funcs.cpp
59
funcs.cpp
@ -1,70 +1,11 @@
|
||||
#include "funcs.h"
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||
int windowX, windowY;
|
||||
glViewport(0, 0, width, height);
|
||||
glfwGetFramebufferSize(window, &windowX, &windowY);
|
||||
float ratioX = (float)sizeX / (float)windowX;
|
||||
float ratioY = (float)sizeY / (float)windowY;
|
||||
float* screenCoords = scaleGridPoints(gridX, gridY, ratioX, ratioY, tileCoords);
|
||||
glBufferData(GL_ARRAY_BUFFER, 5*gridX*gridY*sizeof(float), screenCoords, GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
void processInput(GLFWwindow* window) {
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
}
|
||||
|
||||
float* makeGridPoints(unsigned int X, unsigned int Y) {
|
||||
int numPoints = 5*X*Y;
|
||||
float xStep = 1/ ((float)(X-1));
|
||||
float yStep = 1/ ((float)(Y-1));
|
||||
float* ret = new float[numPoints];
|
||||
for (int i = 0; i < Y; i++) {
|
||||
for (int j = 0; j < X; j++) {
|
||||
ret[5*(i*X+j)] = 2*xStep*j-1.0f;
|
||||
ret[5*(i*X+j)+1] = 2*yStep*i-1.0f;
|
||||
ret[5*(i*X+j)+2] = 0.0;
|
||||
ret[5*(i*X+j)+3] = yStep*i;
|
||||
ret[5*(i*X+j)+4] = xStep*j;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
float* scaleGridPoints(unsigned int X, unsigned int Y, float ratioX, float ratioY, float* points) {
|
||||
ratioX /= 2;
|
||||
ratioY /= 2;
|
||||
float* ret = new float[5*X*Y];
|
||||
for (int i = 0; i < X*Y; i++) {
|
||||
ret[5*i] = ratioX*points[5*i];
|
||||
ret[5*i+1] = ratioY*points[5*i+1];
|
||||
ret[5*i+2] = 0.0;
|
||||
ret[5*i+3] = points[5*i+3];
|
||||
ret[5*i+4] = points[5*i+4];
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
unsigned int* makeGridIndex(unsigned int X, unsigned int Y) {
|
||||
unsigned int tiles = (X-1)*(Y-1);
|
||||
unsigned int* ret = new unsigned int[tiles*6];
|
||||
unsigned int tileInd;
|
||||
for (int i = 0; i < Y-1; i++) {
|
||||
for (int j = 0; j < X-1; j++) {
|
||||
tileInd = i*(X-1)+j;
|
||||
ret[6*tileInd] = i*X+j;
|
||||
ret[6*tileInd+1] = (i+1)*X+j;
|
||||
ret[6*tileInd+2] = (i+1)*X+(j+1);
|
||||
|
||||
ret[6*tileInd+3] = i*X+j;
|
||||
ret[6*tileInd+4] = i*X+j+1;
|
||||
ret[6*tileInd+5] = (i+1)*X+(j+1);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
162
include/Buffers.h
Normal file
162
include/Buffers.h
Normal file
@ -0,0 +1,162 @@
|
||||
#pragma once
|
||||
#ifndef _NB_BUFFER
|
||||
#define _NB_BUFFER
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "Shader.h"
|
||||
|
||||
namespace NB{
|
||||
|
||||
class BufferWarning : public std::runtime_error {
|
||||
public:
|
||||
BufferWarning(const std::string& msg) : std::runtime_error(msg) {}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class BufferManager {
|
||||
public:
|
||||
BufferManager(
|
||||
GLenum buffer_type,
|
||||
bool keep_local=true,
|
||||
GLenum usage=GL_STATIC_DRAW
|
||||
) : _buffer_type(buffer_type), _initialized(false), _keep_local(keep_local), _usage(usage),
|
||||
_size(0), _max_size(0) {}
|
||||
BufferManager(
|
||||
GLenum buffer_type,
|
||||
const std::vector<T>& data,
|
||||
bool keep_local=true,
|
||||
GLenum usage=GL_STATIC_DRAW
|
||||
) : _buffer_type(buffer_type), _keep_local(keep_local), _usage(usage) {
|
||||
setData(data);
|
||||
}
|
||||
BufferManager(
|
||||
GLenum buffer_type,
|
||||
unsigned int size,
|
||||
bool keep_local=true,
|
||||
GLenum usage=GL_STATIC_DRAW
|
||||
) : BufferManager<T>(buffer_type, std::vector<T>(size), keep_local, usage) {}
|
||||
|
||||
GLenum bufferType() const { return _buffer_type; }
|
||||
GLenum usageType() const { return _usage; }
|
||||
unsigned int id() const { return _id; }
|
||||
unsigned int size() const { return _size; }
|
||||
unsigned int capacity() const { return _max_size; }
|
||||
bool isInitialized() const { return _initialized; }
|
||||
bool keepLocal() const { return _keep_local; }
|
||||
std::vector<T> getDataDirect() const {
|
||||
std::vector<T> ret(_size);
|
||||
glBindBuffer(_buffer_type, _id);
|
||||
glGetBufferSubData(_buffer_type, 0, sizeof(T)*_size, ret.data());
|
||||
glBindBuffer(_buffer_type, 0);
|
||||
|
||||
return ret;
|
||||
}
|
||||
std::vector<T> getData() const {
|
||||
if(_keep_local) {
|
||||
return _data;
|
||||
} else {
|
||||
return getDataDirect();
|
||||
}
|
||||
}
|
||||
unsigned int bind() const { glBindBuffer(_buffer_type, _id); return _id; }
|
||||
bool isBound() const { return (getBound(_buffer_type) == _id); }
|
||||
|
||||
GLenum usageType(GLenum usage) { _usage=usage; return usageType(); }
|
||||
bool keepLocal(bool keep_local) {
|
||||
if(keep_local && !_keep_local) {
|
||||
_data = getDataDirect();
|
||||
}
|
||||
_keep_local = keep_local;
|
||||
return _keep_local;
|
||||
}
|
||||
bool initialize(unsigned int n=1) {
|
||||
if (!_initialized) {
|
||||
setData(std::vector<T>(n));
|
||||
}
|
||||
return _initialized;
|
||||
}
|
||||
void loadData(const std::vector<T>& newData, unsigned int offset=0) {
|
||||
if ( newData.size()+offset > _max_size ) {
|
||||
throw BufferWarning("Attempting to overflow buffer of capacity " + std::to_string(_max_size) + ".");
|
||||
}
|
||||
glBindBuffer(_buffer_type, _id);
|
||||
glBufferSubData(_buffer_type, offset*sizeof(T), sizeof(T)*newData.size(), newData.data());
|
||||
if(_keep_local) {
|
||||
memcpy(_data.data()+offset*sizeof(T), newData.data(), sizeof(T)*newData.size());
|
||||
}
|
||||
glBindBuffer(_buffer_type, 0);
|
||||
}
|
||||
void setData(const std::vector<T>& set_data) {
|
||||
if (_keep_local) {_data = set_data;}
|
||||
if (!_initialized) {
|
||||
glGenBuffers(1, &_id);
|
||||
_initialized = true;
|
||||
}
|
||||
glBindBuffer(_buffer_type, _id);
|
||||
if (set_data.size() <= _max_size) {
|
||||
glBufferSubData(_buffer_type, 0, sizeof(T)*_size, _data.data());
|
||||
_size = _data.size();
|
||||
} else {
|
||||
_size = _data.size();
|
||||
_max_size = _size;
|
||||
glBufferData(_buffer_type, sizeof(T)*_max_size, _data.data(), _usage);
|
||||
}
|
||||
glBindBuffer(_buffer_type, 0);
|
||||
}
|
||||
unsigned int resize(unsigned int n) {
|
||||
if (_keep_local) { _data.resize(n); }
|
||||
if (n > _max_size) {
|
||||
_max_size = n;
|
||||
if (_initialized) {
|
||||
glBindBuffer(_buffer_type, _id);
|
||||
glBufferData(_buffer_type, sizeof(T)*_max_size, _data.data(), _usage);
|
||||
glBindBuffer(_buffer_type, 0);
|
||||
}
|
||||
}
|
||||
_size = n;
|
||||
return _size;
|
||||
}
|
||||
unsigned int shrink_to_fit() {
|
||||
if (_size < _max_size) {
|
||||
if (_keep_local) {
|
||||
_data.shrink_to_fit();
|
||||
_size = _data.size();
|
||||
glBindBuffer(_buffer_type, _id);
|
||||
glBufferData(_buffer_type, _size*sizeof(T), _data.data(),_usage);
|
||||
glBindBuffer(_buffer_type, 0);
|
||||
_max_size = _size;
|
||||
} else {
|
||||
_data = getDataDirect();
|
||||
_size = _data.data();
|
||||
_max_size = _size;
|
||||
}
|
||||
}
|
||||
return _size;
|
||||
}
|
||||
|
||||
static unsigned int getBound(GLenum buffer) {
|
||||
unsigned int ret;
|
||||
glGetIntegerv(buffer, &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private:
|
||||
const GLenum _buffer_type;
|
||||
|
||||
GLenum _usage;
|
||||
std::vector<T> _data;
|
||||
unsigned int _id, _size, _max_size;
|
||||
bool _initialized;
|
||||
bool _keep_local;
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -2,11 +2,82 @@
|
||||
#ifndef _NB_DRAW
|
||||
#define _NB_DRAW
|
||||
|
||||
#include <exception>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "Buffers.h"
|
||||
#include "Shader.h"
|
||||
|
||||
namespace NB{
|
||||
class NBDrawObject {
|
||||
|
||||
|
||||
class DrawWarning : public std::runtime_error {
|
||||
public:
|
||||
DrawWarning(const std::string&);
|
||||
};
|
||||
|
||||
uint8_t GLSLTypeSize(GLenum);
|
||||
|
||||
uint8_t GLSLPrimitiveVertexSize(GLenum, unsigned int num_prims=1);
|
||||
|
||||
template<typename T>
|
||||
std::vector<unsigned char> rawDataVector(const std::vector<T>& vec) {
|
||||
unsigned int num_bytes = vec.size() * sizeof(T);
|
||||
std::vector<unsigned char> ret(num_bytes);
|
||||
memcpy(ret.data(), vec.data(), num_bytes);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
struct VertexAttributePointer {
|
||||
int32_t offset;
|
||||
GLsizei stride = -1;
|
||||
};
|
||||
|
||||
struct VertexAttribute {
|
||||
const GLint GLSLSize;
|
||||
const GLenum GLSLType;
|
||||
const GLboolean GLSLNormalization;
|
||||
VertexAttributePointer ptr;
|
||||
};
|
||||
|
||||
class DrawBuffer {
|
||||
public:
|
||||
DrawBuffer(std::vector<VertexAttribute>, const Shader&);
|
||||
//DrawBuffer(std::initializer_list<VertexAttribute>, std::initializer_list<VertexAttributePointer>);
|
||||
|
||||
unsigned int vertSize() const;
|
||||
std::vector<VertexAttributePointer> getLayout() const;
|
||||
const Shader shader() const;
|
||||
bool keepLocalElement() const;
|
||||
bool keepLocalVertex() const;
|
||||
unsigned int elementBufferID() const;
|
||||
unsigned int vertexBufferID() const;
|
||||
virtual void draw() const;
|
||||
|
||||
bool keepLocalElement(bool);
|
||||
bool keepLocalVertex(bool);
|
||||
const Shader shader(const Shader&);
|
||||
virtual std::vector<VertexAttributePointer> setLayout();
|
||||
virtual std::vector<VertexAttributePointer> setLayout(std::vector<VertexAttributePointer>);
|
||||
virtual void generateElementBuffer(std::vector<unsigned int>, GLenum gl_primitive_type=GL_TRIANGLES);
|
||||
virtual void generateElementBuffer(unsigned int, GLenum gl_primitive_type=GL_TRIANGLES);
|
||||
virtual void generateVertexBuffer(std::vector<unsigned char>);
|
||||
virtual void generateVertexBuffer(unsigned int);
|
||||
virtual void loadElementBuffer(const std::vector<unsigned int>&, unsigned int offset=0, GLenum gl_primitive_type=GL_TRIANGLES);
|
||||
virtual void loadVertexBuffer(const std::vector<unsigned char>&, unsigned int offset=0);
|
||||
|
||||
protected:
|
||||
std::vector<VertexAttribute> _vertex_attributes;
|
||||
BufferManager<unsigned char> _vert_data;
|
||||
BufferManager<unsigned int> _elmt_data;
|
||||
Shader _shader;
|
||||
unsigned int _num_verts, _num_prims, _VAO;
|
||||
GLenum _gl_primitive_type;
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
@ -3,26 +3,14 @@
|
||||
#define _FUNCS_HEADER_
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
extern unsigned int gridX, gridY, sizeX, sizeY;
|
||||
extern float* tileCoords;
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||
|
||||
void processInput(GLFWwindow* window);
|
||||
|
||||
float* makeGridPoints(unsigned int X=2, unsigned int Y=2);
|
||||
|
||||
float* scaleGridPoints(unsigned int X, unsigned int Y, float ratioX, float ratioY, float* points);
|
||||
|
||||
unsigned int* makeGridIndex(unsigned int X=2, unsigned int Y=2);
|
||||
|
||||
#endif
|
||||
@ -1,4 +1,7 @@
|
||||
#pragma once
|
||||
#ifndef _NB_SHADER
|
||||
#define _NB_SHADER
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
@ -8,13 +11,24 @@
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace NB {
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
unsigned int id;
|
||||
Shader(const char* vertexPath, const char* fragmentPath);
|
||||
Shader();
|
||||
Shader(const Shader&);
|
||||
Shader& operator=(const Shader&);
|
||||
|
||||
void use() const;
|
||||
void setBool(const std::string& name, bool value) const;
|
||||
void setInt(const std::string& name, int value) const;
|
||||
void setFloat(const std::string& name, float value) const;
|
||||
void setMat4(const std::string& name, glm::mat4& value) const;
|
||||
};
|
||||
unsigned int id;
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
64
main.cpp
64
main.cpp
@ -6,12 +6,7 @@
|
||||
#include <funcs.h>
|
||||
#include <shader.h>
|
||||
#include <NBEngine/Window.h>
|
||||
#include "data.cpp"
|
||||
|
||||
unsigned int gridX = 8, gridY = 8;
|
||||
unsigned int sizeX=500, sizeY=500;
|
||||
float* tileCoords = makeGridPoints(gridX, gridY);
|
||||
unsigned int* tileIndices = makeGridIndex(gridX, gridY);
|
||||
#include "Draw.h"
|
||||
|
||||
int main() {
|
||||
NB::NBWindow myWindow(800, 600, "Howdy Naif!");
|
||||
@ -20,47 +15,38 @@ int main() {
|
||||
myWindow.init();
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
|
||||
|
||||
// Create Data
|
||||
float* screenCoords = scaleGridPoints(gridX, gridY, (float)sizeX/(float)800, (float)sizeY/(float)600, tileCoords);
|
||||
|
||||
// Use and set shader
|
||||
Shader myShader("../shaders/vert.vs", "../shaders/frag.fs");
|
||||
NB::Shader myShader("../shaders/vert.vs", "../shaders/frag.fs");
|
||||
myShader.use();
|
||||
|
||||
// Create Vertex Buffer and bind it to array buffer, then specifiy how to use that data
|
||||
unsigned int VAO, VBO;
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, 5*gridX*gridY*sizeof(float), screenCoords, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)(3*sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
delete[] screenCoords;
|
||||
|
||||
// Element Buffers
|
||||
unsigned int EBO;
|
||||
glGenBuffers(1, &EBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*(gridX-1)*(gridY-1)*sizeof(unsigned int), tileIndices, GL_STATIC_DRAW);
|
||||
std::vector<NB::VertexAttribute> attrs = {
|
||||
{2, GL_FLOAT, false},
|
||||
{3, GL_FLOAT, false}
|
||||
};
|
||||
NB::DrawBuffer myBuffer(attrs, myShader);
|
||||
myBuffer.generateElementBuffer({0, 1, 2}, GL_TRIANGLES);
|
||||
std::vector<float> data_float = {
|
||||
-0.5, -0.5, 1.0, 0.0, 0.0,
|
||||
0.5, -0.5, 0.0, 1.0, 0.0,
|
||||
0.0, 0.5, 0.0, 0.0, 1.0
|
||||
};
|
||||
myBuffer.generateVertexBuffer(NB::rawDataVector(data_float));
|
||||
|
||||
myShader.use();
|
||||
glBindVertexArray(VAO);
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
//glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);
|
||||
// Window loop
|
||||
GLenum err;
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
while(err=glGetError()) {
|
||||
std::cout << "[GL ERROR] " << err << "\n";
|
||||
}
|
||||
// Input checking
|
||||
processInput(window);
|
||||
|
||||
// Rendering Loop
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glDrawElements(GL_TRIANGLES, (gridX-1)*(gridY-1)*6, GL_UNSIGNED_INT, 0);
|
||||
myShader.use();
|
||||
glClearColor(0.2f, 0.4f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
|
||||
myBuffer.draw();
|
||||
|
||||
// Events and buffer swap
|
||||
glfwPollEvents();
|
||||
@ -69,7 +55,5 @@ int main() {
|
||||
|
||||
glfwTerminate();
|
||||
|
||||
delete[] tileCoords;
|
||||
//delete[] tileIndices;
|
||||
return 0;
|
||||
}
|
||||
12
shader.cpp
12
shader.cpp
@ -1,4 +1,6 @@
|
||||
#include "shader.h"
|
||||
#include "Shader.h"
|
||||
|
||||
namespace NB{
|
||||
|
||||
Shader::Shader(const char* vertexPath, const char* fragmentPath) {
|
||||
std::string vertexCode, fragmentCode;
|
||||
@ -60,6 +62,12 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath) {
|
||||
glDeleteShader(fragment);
|
||||
}
|
||||
|
||||
Shader::Shader() { id = 0x0; }
|
||||
|
||||
Shader::Shader(const Shader& cpy_shader) { id = cpy_shader.id; }
|
||||
|
||||
Shader& Shader::operator=(const Shader& cpy_shader) { id = cpy_shader.id; }
|
||||
|
||||
void Shader::use() const{
|
||||
glUseProgram(id);
|
||||
}
|
||||
@ -78,4 +86,6 @@ void Shader::setFloat(const std::string& name, float value) const {
|
||||
|
||||
void Shader::setMat4(const std::string& name, glm::mat4& value) const {
|
||||
glUniformMatrix4fv(glGetUniformLocation(id, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,8 +1,7 @@
|
||||
#version 330 core
|
||||
flat in vec3 ourColor;
|
||||
in vec3 vertColor;
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
void main() {
|
||||
FragColor = vec4(ourColor, 1.0);
|
||||
FragColor = vec4(vertColor, 0.0);
|
||||
}
|
||||
@ -1,11 +1,9 @@
|
||||
#version 330 core
|
||||
layout (location=0) in vec2 aPos;
|
||||
layout (location=1) in vec3 aColor;
|
||||
|
||||
layout(location=0) in vec3 aPos;
|
||||
layout(location=1) in vec2 val;
|
||||
|
||||
flat out vec3 ourColor;
|
||||
|
||||
out vec3 vertColor;
|
||||
void main() {
|
||||
gl_Position = vec4(aPos, 1.0);
|
||||
ourColor = vec3(val.x, 0.0, val.y);
|
||||
vertColor = aColor;
|
||||
gl_Position = vec4(aPos, 0.0, 1.0);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user