From 2d8b61bf9fc36e933a0091e1098e62faf143412c Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 27 Dec 2017 21:52:21 +0100 Subject: Add triangle type --- src/RenderBackend/OpenGL/DeviceMemory.cpp | 12 ++++++++++-- src/main.cpp | 21 +++++++++++---------- 2 files changed, 21 insertions(+), 12 deletions(-) (limited to 'src') 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 &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 &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 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 triangles(&cpuTriangle, 1); + gpuTriangle->copy(triangles, DeviceMemory::StorageType::STATIC); - CompiledVertexShader *vertexShader = createVertexShader(*triangle); + CompiledVertexShader *vertexShader = createVertexShader(*gpuTriangle); CompiledPixelShader *pixelShader = createPixelShader(); ShaderProgram shaderProgram; -- cgit v1.2.3