diff options
-rw-r--r-- | include/RenderBackend/OpenGL/DeviceMemory.hpp | 4 | ||||
-rw-r--r-- | include/Triangle2D.hpp | 16 | ||||
-rw-r--r-- | include/Vertex2D.hpp | 18 | ||||
-rw-r--r-- | src/RenderBackend/OpenGL/DeviceMemory.cpp | 12 | ||||
-rw-r--r-- | src/main.cpp | 21 |
5 files changed, 58 insertions, 13 deletions
diff --git a/include/RenderBackend/OpenGL/DeviceMemory.hpp b/include/RenderBackend/OpenGL/DeviceMemory.hpp index 10ed040..c37acfb 100644 --- a/include/RenderBackend/OpenGL/DeviceMemory.hpp +++ b/include/RenderBackend/OpenGL/DeviceMemory.hpp @@ -2,6 +2,7 @@ #include "../../DataView.hpp" #include "../../utils.hpp" +#include "../../Triangle2D.hpp" #include <stdexcept> namespace amalgine @@ -36,7 +37,8 @@ namespace amalgine }; ~DeviceMemory(); - void copy(const DataView<f32> &data, StorageType storageType, PrimitiveType primitiveType = PrimitiveType::TRIANGLE); + //void copy(const DataView<f32> &data, StorageType storageType, PrimitiveType primitiveType = PrimitiveType::TRIANGLE); + void copy(const DataView<Triangle2D> &triangles, StorageType storageType); void draw(); private: DeviceMemory(); diff --git a/include/Triangle2D.hpp b/include/Triangle2D.hpp new file mode 100644 index 0000000..d5d76bd --- /dev/null +++ b/include/Triangle2D.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "Vertex2D.hpp" + +namespace amalgine +{ + class Triangle2D + { + public: + Triangle2D(const Vertex2D &_p1, const Vertex2D &_p2, const Vertex2D &_p3) : p1(_p1), p2(_p2), p3(_p3) {} + + Vertex2D p1; + Vertex2D p2; + Vertex2D p3; + }; +} diff --git a/include/Vertex2D.hpp b/include/Vertex2D.hpp new file mode 100644 index 0000000..de22cd9 --- /dev/null +++ b/include/Vertex2D.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "types.hpp" + +namespace amalgine +{ + class Vertex2D + { + public: + Vertex2D(f32 _x = 0.0f, f32 _y = 0.0f) : x(_x), y(_y) + { + + } + + f32 x; + f32 y; + }; +} diff --git a/src/RenderBackend/OpenGL/DeviceMemory.cpp b/src/RenderBackend/OpenGL/DeviceMemory.cpp index faf2943..fb9dec5 100644 --- a/src/RenderBackend/OpenGL/DeviceMemory.cpp +++ b/src/RenderBackend/OpenGL/DeviceMemory.cpp @@ -49,7 +49,7 @@ namespace amalgine // TODO: Bind vao here? glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId); } - + /* void DeviceMemory::copy(const DataView<f32> &data, StorageType storageType, PrimitiveType primitiveType) { glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId); @@ -57,11 +57,19 @@ namespace amalgine this->primitiveType = getOpenglPrimitiveType(primitiveType); numVertices = getPrimitiveTypePointsPerVertices(primitiveType); } + */ + void DeviceMemory::copy(const DataView<Triangle2D> &triangles, StorageType storageType) + { + glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId); + glBufferData(GL_ARRAY_BUFFER, triangles.getByteSize(), triangles.data, getOpenglStorageType(storageType)); + primitiveType = GL_TRIANGLES; + numVertices = triangles.size * 3; + } void DeviceMemory::draw() { if(primitiveType == 0) throw DeviceMemoryEmpty("Unable to draw buffer as no data has been copied to device memory"); - glDrawArrays(primitiveType, 0, 3); + glDrawArrays(primitiveType, 0, numVertices); } } diff --git a/src/main.cpp b/src/main.cpp index 0e72df0..a04fb99 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include "../include/RenderBackend/OpenGL/ShaderProgram.hpp" #include "../include/RenderBackend/OpenGL/DeviceMemory.hpp" #include "../include/RenderBackend/OpenGL/DeviceFrame.hpp" +#include "../include/Triangle2D.hpp" // TODO: Disallow shader variables that begin with "gl_" as reserved variables // start with that. What about hlsl? @@ -29,17 +30,17 @@ int main() DeviceFrame frame; - f32 verticesRaw[] = - { - 0.0f, 0.5f, - 0.5f, -0.5f, - -0.5f, -0.5f - }; - DeviceMemory *triangle = frame.alloc(); - DataView<f32> vertices(verticesRaw, 6); - triangle->copy(vertices, DeviceMemory::StorageType::STATIC); + Triangle2D cpuTriangle( + Vertex2D(0.0f, 0.5f), + Vertex2D(0.5f, -0.5f), + Vertex2D(-0.5f, -0.5f) + ); + + DeviceMemory *gpuTriangle = frame.alloc(); + DataView<Triangle2D> triangles(&cpuTriangle, 1); + gpuTriangle->copy(triangles, DeviceMemory::StorageType::STATIC); - CompiledVertexShader *vertexShader = createVertexShader(*triangle); + CompiledVertexShader *vertexShader = createVertexShader(*gpuTriangle); CompiledPixelShader *pixelShader = createPixelShader(); ShaderProgram shaderProgram; |