Skip to content

Commit

Permalink
First implementation of batches
Browse files Browse the repository at this point in the history
  • Loading branch information
olympus112 committed Aug 15, 2019
1 parent 9d62849 commit 8986e73
Show file tree
Hide file tree
Showing 38 changed files with 318 additions and 181 deletions.
Binary file modified application/application.rc
Binary file not shown.
2 changes: 1 addition & 1 deletion application/resourceManager.cpp
Expand Up @@ -19,7 +19,7 @@ ResourceStruct resources[] {
{ IDR_SHADER8, "SHADER" },
{ IDR_SHADER9, "SHADER" },
{ IDR_SHADER10, "SHADER" },
{ IDR_SHADER11, "SHADER" },
{ IDR_SHADER11, "SHADER" },
{ IDR_SHADER12, "SHADER" },
{ IDR_SHADER13, "SHADER" },
{ IDR_SHADER14, "SHADER" },
Expand Down
2 changes: 1 addition & 1 deletion application/resourceManager.h
Expand Up @@ -15,7 +15,7 @@
#define TEST_SHADER 10
#define BLUR_SHADER 11
#define COLORWHEEL_SHADER 12
#define TEST1_SHADER 13
#define GUI_SHADER 13
#define EDGE_SHADER 14
#define SPHERE_MODEL 15
#define STALL_MODEL 16
Expand Down
114 changes: 114 additions & 0 deletions application/view/batch.h
@@ -0,0 +1,114 @@
#pragma once

#include "batchConfig.h"

#include "GL\glew.h"
#include "GLFW\glfw3.h"

#include <vector>

#include "../engine/math/vec.h"

#include "buffers\vertexArray.h"
#include "buffers\vertexBuffer.h"
#include "buffers\indexBuffer.h"

template<typename Vertex>
class Batch {
private:
VertexArray* vao = nullptr;
VertexBuffer* vbo = nullptr;
IndexBuffer* ibo = nullptr;

BatchConfig config;

std::vector<Vertex> vertexBuffer;
std::vector<unsigned int> indexBuffer;

int indexCounter;
public:
Vertex* vertexPointer;
unsigned int* indexPointer;
int currentIndex;

Batch(BatchConfig config) : config(config) {
vao = new VertexArray();
vbo = new VertexBuffer(nullptr, 0);
ibo = new IndexBuffer(nullptr, 0);

vao->addBuffer(*vbo, config.bufferLayout);

vertexPointer = vertexBuffer.data();
indexPointer = indexBuffer.data();

currentIndex = 0;
};

inline void pushVertex(Vertex vertex) {
vertexPointer++[0] = vertex;
indexCounter++;
}

inline void pushIndex(unsigned int index) {
indexPointer++[0] = currentIndex + index;
}

template<class... Vertex>
inline void pushVertices(Vertex... vertices) {
for (Vertex vertex : vertices) {
pushVertex(vertex);
}
}

template<class... Index>
inline void pushIndices(Index... indices) {
for (unsigned int index : indices) {
pushIndex(index);
}
}

void reserve(int vertexCount, int indexCount) {
int oldVertexBufferSize = vertexBuffer.size();
vertexBuffer.resize(oldVertexBufferSize + vertexCount);
vertexPointer = vertexBuffer.data() + oldVertexBufferSize;

int oldIndexBufferSize = indexBuffer.size();
indexBuffer.resize(oldIndexBufferSize + indexCount);
indexPointer = indexBuffer.data() + oldIndexBufferSize;
}

void submit() {
vao->bind();

vbo->bind();
glBufferData(GL_ARRAY_BUFFER, vertexBuffer.size() * sizeof(Vertex), (const void*) vertexBuffer.data(), GL_STREAM_DRAW);

ibo->bind();
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.size() * sizeof(unsigned int), (const void *) indexBuffer.data(), GL_STREAM_DRAW);

glDrawElements(GL_TRIANGLES, indexBuffer.size(), GL_UNSIGNED_INT, nullptr);

vbo->unbind();
ibo->unbind();

vao->unbind();

clear();
}

void endIndex() {
currentIndex += indexCounter;
indexCounter = 0;
}

void clear() {
vertexBuffer.clear();
indexBuffer.clear();

vertexPointer = vertexBuffer.data();
indexPointer = indexBuffer.data();

currentIndex = 0;
indexCounter = 0;
}
};
9 changes: 9 additions & 0 deletions application/view/batchConfig.h
@@ -0,0 +1,9 @@
#pragma once

#include "buffers\bufferLayout.h"

struct BatchConfig {
BufferLayout bufferLayout;

BatchConfig(BufferLayout bufferLayout) : bufferLayout(bufferLayout) {};
};
1 change: 1 addition & 0 deletions application/view/batchManager.h
@@ -0,0 +1 @@
#pragma once
32 changes: 19 additions & 13 deletions application/view/buffers/bufferLayout.cpp
Expand Up @@ -4,24 +4,30 @@
#include "../util/log.h"

namespace BufferDataType {
const BufferDataTypeInfo NONE = { "none" , 0, 0, 0 };
const BufferDataTypeInfo BOOL = { "bool" , GL_BYTE , 1, 1 };
const BufferDataTypeInfo INT = { "int" , GL_INT , 1, 4 };
const BufferDataTypeInfo INT2 = { "vec2i", GL_INT , 2, 8 };
const BufferDataTypeInfo INT3 = { "vec3i", GL_INT , 3, 12 };
const BufferDataTypeInfo INT4 = { "vec4i", GL_INT , 4, 16 };
const BufferDataTypeInfo FLOAT = { "float", GL_FLOAT, 1, 4 };
const BufferDataTypeInfo FLOAT2 = { "vec2" , GL_FLOAT, 2, 8 };
const BufferDataTypeInfo NONE = { "none" , 0, 0, 0 };
const BufferDataTypeInfo BOOL = { "bool" , GL_BYTE , 1, 1 };
const BufferDataTypeInfo INT = { "int" , GL_INT , 1, 4 };
const BufferDataTypeInfo INT2 = { "vec2i", GL_INT , 2, 8 };
const BufferDataTypeInfo INT3 = { "vec3i", GL_INT , 3, 12 };
const BufferDataTypeInfo INT4 = { "vec4i", GL_INT , 4, 16 };
const BufferDataTypeInfo FLOAT = { "float", GL_FLOAT, 1, 4 };
const BufferDataTypeInfo FLOAT2 = { "vec2" , GL_FLOAT, 2, 8 };
const BufferDataTypeInfo FLOAT3 = { "vec3" , GL_FLOAT, 3, 12 };
const BufferDataTypeInfo FLOAT4 = { "vec4" , GL_FLOAT, 4, 16 };
const BufferDataTypeInfo MAT2 = { "mat2" , GL_FLOAT, 2, 8 }; // per row
const BufferDataTypeInfo MAT3 = { "mat3" , GL_FLOAT, 3, 12 }; // per row
const BufferDataTypeInfo MAT4 = { "mat4" , GL_FLOAT, 4, 16 }; // per row
const BufferDataTypeInfo MAT2 = { "mat2" , GL_FLOAT, 2, 8 }; // per row
const BufferDataTypeInfo MAT3 = { "mat3" , GL_FLOAT, 3, 12 }; // per row
const BufferDataTypeInfo MAT4 = { "mat4" , GL_FLOAT, 4, 16 }; // per row
}

BufferLayout::BufferLayout() : stride(0) {};
BufferElement::BufferElement(std::string name, BufferDataType::BufferDataTypeInfo info, bool normalized) : name(name), info(info), normalized(normalized) {

BufferLayout::BufferLayout(std::vector<BufferElement> elements) : elements(elements), stride(0) {
}

BufferLayout::BufferLayout() : stride(0) {

};

BufferLayout::BufferLayout(std::vector<BufferElement> elements) : elements(elements) {
stride = 0;

for (size_t i = 0; i < elements.size(); i++) {
Expand Down
6 changes: 5 additions & 1 deletion application/view/buffers/bufferLayout.h
Expand Up @@ -18,6 +18,8 @@ namespace BufferDataType {
// size of type in bytes
int size;

BufferDataTypeInfo(std::string name, int type, int count, int size) : name(name), type(type), count(count), size(size) {};

bool operator==(const BufferDataTypeInfo& other) const {
return other.name == name;
}
Expand All @@ -41,7 +43,9 @@ namespace BufferDataType {
struct BufferElement {
std::string name;
BufferDataType::BufferDataTypeInfo info;
bool normalized = false;
bool normalized;

BufferElement(std::string name, BufferDataType::BufferDataTypeInfo info, bool normalized = false);
};

struct BufferLayout {
Expand Down
3 changes: 2 additions & 1 deletion application/view/buffers/indexBuffer.cpp
Expand Up @@ -13,7 +13,8 @@ IndexBuffer::IndexBuffer() {
IndexBuffer::IndexBuffer(const unsigned int* data, unsigned int size, unsigned int mode) {
glGenBuffers(1, &id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size * sizeof(unsigned int), data, mode);
if (size != 0)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size * sizeof(unsigned int), data, mode);
}

IndexBuffer::IndexBuffer(const unsigned int* data, unsigned int size) : IndexBuffer(data, size, GL_STATIC_DRAW) {}
Expand Down
2 changes: 2 additions & 0 deletions application/view/buffers/vertexArray.cpp
Expand Up @@ -25,6 +25,8 @@ void VertexArray::bind() {
}

void VertexArray::unbind() {
for (unsigned int i = 0; i < attributeArrayOffset; i++)
glDisableVertexAttribArray(i);
glBindVertexArray(0);
}

Expand Down
3 changes: 2 additions & 1 deletion application/view/buffers/vertexBuffer.cpp
Expand Up @@ -8,7 +8,8 @@ VertexBuffer::VertexBuffer() {
VertexBuffer::VertexBuffer(const float* data, size_t size, unsigned int mode) : Bindable() {
glGenBuffers(1, &id);
glBindBuffer(GL_ARRAY_BUFFER, id);
glBufferData(GL_ARRAY_BUFFER, size * sizeof(float), data, mode);
if (size != 0)
glBufferData(GL_ARRAY_BUFFER, size * sizeof(float), data, mode);
}

VertexBuffer::VertexBuffer(const float* data, size_t size) : VertexBuffer(data, size, GL_STATIC_DRAW) {}
Expand Down
48 changes: 48 additions & 0 deletions application/view/gui/gui.cpp
Expand Up @@ -13,6 +13,7 @@
#include "../texture.h"
#include "../screen.h"
#include "../shaderProgram.h"
#include "../renderUtils.h"

#include "../buffers/frameBuffer.h"
#include "../mesh/indexedMesh.h"
Expand Down Expand Up @@ -240,7 +241,26 @@ namespace GUI {
Vec4 fontColor = COLOR::SILVER;
double fontSize = 0.0009;

// Batch
BufferLayout bufferLayout({
{ "pos", BufferDataType::FLOAT2 },
{ "uv", BufferDataType::FLOAT2 },
{ "col", BufferDataType::FLOAT4 }
});

struct GUIVertex {
Vec2f pos;
Vec2f uv;
Vec4f col;
};

BatchConfig batchConfig(bufferLayout);
Batch<GUIVertex>* batch;

void init(Screen* screen, Font* font) {

batch = new Batch<GUIVertex>(batchConfig);

GUI::screen = screen;
GUI::font = font;
GUI::guiFrameBuffer = new FrameBuffer(screen->dimension.x, screen->dimension.y);
Expand Down Expand Up @@ -401,5 +421,33 @@ namespace GUI {
for (auto iterator = components.rbegin(); iterator != components.rend(); ++iterator) {
(*iterator)->render();
}

line(Vec2f(-0.5, -0.5), Vec2f(-0.5, 0.5));
line(Vec2f(0.5, 0.3), Vec2f(-0.5, 0.5));
line(Vec2f(0.5, 0.3), Vec2f(0.1, -0.3));
line(Vec2f(-0.5, -0.5), Vec2f(0.1, -0.3));

Renderer::disableCulling();
Shaders::guiShader.updateProjection(screen->camera.orthoMatrix);
batch->submit();
}

void line(Vec2f a, Vec2f b) {
Vec2f dxy = normalize(Vec2f(b.y - a.y, a.x - b.x)) / screen->dimension.y * 2;

batch->reserve(4, 6);

batch->pushVertex({ Vec2f(a + dxy), Vec2f(1), COLOR::R });
batch->pushVertex({ Vec2f(a - dxy), Vec2f(1), COLOR::R });
batch->pushVertex({ Vec2f(b + dxy), Vec2f(1), COLOR::B });
batch->pushVertex({ Vec2f(b - dxy), Vec2f(1), COLOR::B });

batch->pushIndex(0);
batch->pushIndex(1);
batch->pushIndex(2);
batch->pushIndex(2);
batch->pushIndex(1);
batch->pushIndex(3);
batch->endIndex();
}
}
12 changes: 11 additions & 1 deletion application/view/gui/gui.h
Expand Up @@ -3,7 +3,11 @@
#include "../engine/math/vec.h"
#include "../engine/math/mat4.h"

#include "../batch.h"
#include "../batchConfig.h"

#include <string>
#include <vector>

class Component;
class ColorPicker;
Expand Down Expand Up @@ -141,25 +145,31 @@ namespace GUI {
extern Vec4 fontColor;
extern double fontSize;

// Container functions
void add(Component* component);
void remove(Component* component);
void select(Component* component);

Component* superParent(Component* child);

// Map functions
double clamp(double value, double min, double max);
double map(double x, double minIn, double maxIn, double minOut, double maxOut);
Vec2 map(Vec2 point);
Vec2 unmap(Vec2 point);
Vec2 mapDimension(Vec2 dimension);
Vec2 unmapDimension(Vec2 dimension);

// Event functions
void intersect(Vec2 mouse);
bool intersectsSquare(Vec2 point, Vec2 topleft, Vec2 dimension);

// State function
void init(Screen* screen, Font* font);
void update(Mat4f orthoMatrix);
void render(Mat4f orthoMatrix);

// Draw functions
void line(Vec2f a, Vec2f b);
};

#include "component.h"
2 changes: 1 addition & 1 deletion application/view/layer/debugLayer.cpp
Expand Up @@ -99,7 +99,7 @@ DebugLayer::DebugLayer() {

}

DebugLayer::DebugLayer(Screen* screen) : Layer("Debug layer", screen, noUpdate | noEvents) {
DebugLayer::DebugLayer(Screen* screen, char flags) : Layer("Debug layer", screen, flags) {

}

Expand Down
2 changes: 1 addition & 1 deletion application/view/layer/debugLayer.h
Expand Up @@ -14,7 +14,7 @@ class DebugLayer : public Layer {

public:
DebugLayer();
DebugLayer(Screen* screen);
DebugLayer(Screen* screen, char flags = noUpdate | noEvents);

void init() override;
void update() override;
Expand Down
8 changes: 7 additions & 1 deletion application/view/layer/debugOverlay.cpp
@@ -1,6 +1,8 @@
#include "debugOverlay.h"

#include "../screen.h"
#include "../renderUtils.h"
#include "../shaderProgram.h"

#include "../debug/profilerUI.h"
#include "../debug/visualDebug.h"
Expand All @@ -20,7 +22,7 @@ DebugOverlay::DebugOverlay() {

}

DebugOverlay::DebugOverlay(Screen* screen) : Layer("Debug overlay", screen, noEvents) {
DebugOverlay::DebugOverlay(Screen* screen, char flags) : Layer("Debug overlay", screen, flags) {

}

Expand All @@ -33,6 +35,10 @@ void DebugOverlay::update() {
}

void DebugOverlay::render() {

Renderer::disableDepthTest();
Shaders::fontShader.updateProjection(screen->camera.orthoMatrix);

graphicsMeasure.mark(GraphicsProcess::PROFILER);

size_t objCount = screen->world->getPartCount();
Expand Down

0 comments on commit 8986e73

Please sign in to comment.