Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d62849
commit 8986e73
Showing
38 changed files
with
318 additions
and
181 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
#include "buffers\bufferLayout.h" | ||
|
||
struct BatchConfig { | ||
BufferLayout bufferLayout; | ||
|
||
BatchConfig(BufferLayout bufferLayout) : bufferLayout(bufferLayout) {}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#pragma once |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.