From 4829622988036fa5120a67d38ea07a5cec174c2a Mon Sep 17 00:00:00 2001 From: Ed94 Date: Wed, 12 Feb 2020 03:29:35 -0500 Subject: [PATCH] First push. --- Cpp_Alias.hpp | 81 ++++++++++++ DGL.hpp | 273 +++++++++++++++++++++++++++++++++++++++ DGL_Buffers.hpp | 81 ++++++++++++ DGL_Enum.hpp | 86 ++++++++++++ DGL_FundamentalTypes.hpp | 17 +++ DGL_MiscTypes.hpp | 29 +++++ DGL_Shader.hpp | 126 ++++++++++++++++++ 7 files changed, 693 insertions(+) create mode 100644 Cpp_Alias.hpp create mode 100644 DGL.hpp create mode 100644 DGL_Buffers.hpp create mode 100644 DGL_Enum.hpp create mode 100644 DGL_FundamentalTypes.hpp create mode 100644 DGL_MiscTypes.hpp create mode 100644 DGL_Shader.hpp diff --git a/Cpp_Alias.hpp b/Cpp_Alias.hpp new file mode 100644 index 0000000..03a357a --- /dev/null +++ b/Cpp_Alias.hpp @@ -0,0 +1,81 @@ +/* +Title: C++ Aliasing Library +Author: Edward R. Gonzalez +Date: + +Description: +This is a segment of the C++ Assist Library I use for other code. + +This merely removes the need to use operators I don't like and wraps them in easier to manage typedefs, functions or macros +*/ + + +#pragma once + +// Macros + +#define sfn \ +auto + +#define deduce \ +auto + +#define Ref(_type) \ +_type& + +#define rRef(_type) \ +_type&& + + +// Aliases + +// Fundamental + +using uInt64 = unsigned long long int; + +// Pointers + +template +using ptr = Type*; + +template +using FnPtr = ReturnType(*)(ParamTypes...); + +// Strings + +template +using RawString = ptr; + + +// Enum + +enum class ExitCode +{ + Success = EXIT_SUCCESS, + Failed = EXIT_FAILURE +}; + + +// Functions + +// Ptr + +template +sfn Address(Ref(Type) _instance) -> ptr +{ + return &_instance; +} + +template +sfn Dref(ptr _type) -> Ref(Type) +{ + return *_type; +} + + +// Exit + +sfn Exit(ExitCode _code) +{ + exit(int(_code)); +} diff --git a/DGL.hpp b/DGL.hpp new file mode 100644 index 0000000..81d0ea3 --- /dev/null +++ b/DGL.hpp @@ -0,0 +1,273 @@ +#pragma once + +// C++ STL +#include +#include +#include + +// GL +#include +#include + +#include "DGL_FundamentalTypes.hpp" +#include "DGL_MiscTypes.hpp" +#include "DGL_Enum.hpp" +#include "DGL_Shader.hpp" +#include "DGL_Buffers.hpp" + +// Non-Standard C++ +#include "Cpp_Alias.hpp" + + + +namespace GL +{ + // Aliases + + // C++ STL + + using std::cout; + using std::endl; + + + // GLFW + + using Monitor = GLFWmonitor ; + using TimeValInt = uint64_t ; + using TimeValDec = double ; + using Window = GLFWwindow ; + using WindowRefList = std::vector< ptr >; + + + + // Object Instances + + WindowRefList Windows; + + + + // Error Stuff + + sfn ErrorRuntime(Ref(std::runtime_error) _error) + { + cout << "Runtime error occurred: " << _error.what() << endl; + + return; + } + + + + // Constants + + sfn constexpr NotShared () -> ptr { return NULL; } + sfn constexpr WindowedMode() -> ptr { return NULL; } + + + + // Functionality + + sfn SwapBuffers(const ptr _window) -> void + { + glfwSwapBuffers(_window); + + return; + } + + sfn CanClose(const ptr _theWindow) + { + return glfwWindowShouldClose(_theWindow); + } + + sfn CreateWindow + ( + int _width , + int _height , + RawString _title , + ptr _monitorToFullscreen , + ptr _windowToShareResourcesWith + ) + -> ptr + { + Windows.push_back(glfwCreateWindow(_width, _height, _title, _monitorToFullscreen, _windowToShareResourcesWith)); + + try + { + if (Windows.back() == NULL) + { + throw std::runtime_error("Failed to create a window"); + } + } + catch (std::runtime_error _error) + { + ErrorRuntime(_error); + + Exit(ExitCode::Failed); + } + + return Windows.back(); + } + + sfn DestoryWindow(const ptr _window) + { + using ElementType = decltype(Windows.begin()); + + for (ElementType element = Windows.begin(); element != Windows.end(); element++) + { + if (*element == _window) + { + glfwDestroyWindow(_window); + + Windows.erase(element); + } + } + + return; + } + + sfn GetTime() -> TimeValDec + { + return glfwGetTime(); + } + + sfn GetRawTime() -> TimeValInt + { + return glfwGetTimerValue(); + } + + sfn InitalizeGLFW() + { + try + { + std::cout << "Initializing GLFW Version: " << glfwGetVersionString() << std::endl; + + /* Initialize the library */ + if (!glfwInit()) + { + throw std::runtime_error("Failed to initialize GLFW"); + } + } + catch (std::runtime_error _error) + { + ErrorRuntime(_error); + + Exit(ExitCode::Failed); + } + + return; + } + + sfn InitalizeGLEW() + { + try + { + // If using GLEW version 1.13 or earlier + //glewExperimental = true; + + std::cout << "Initializing Glew Version: " << glewGetString(GLEW_VERSION) << std::endl; + + GLenum err = glewInit(); + + if (err != GLEW_OK) + { + // Problem: glewInit failed, something is seriously wrong. + std::cout << "glewInit failed: " << glewGetErrorString(err) << std::endl; + + throw std::runtime_error("Failed to initialize GLFW"); + } + + cout << "OpenGL Version: " << glGetString(GL_VERSION) << endl; + } + catch (std::runtime_error _error) + { + ErrorRuntime(_error); + + Exit(ExitCode::Failed); + } + } + + sfn PollEvents() + { + glfwPollEvents(); + + return; + } + + sfn RunBasicWindowLoop(const ptr _window) + { + /* Loop until the user closes the window */ + while (not CanClose(_window)) + { + ClearBuffer(); + + SwapBuffers(_window); + + PollEvents(); + } + + return; + } + + sfn RunBasicWindowLoop_Timed(const ptr _window, TimeValDec _interval, FnPtr _renderProcedure) + { + TimeValDec start, end, deltaSinceClear = 0.0; + + while (not CanClose(_window)) + { + start = GetTime(); + + if (deltaSinceClear > _interval) + { + ClearBuffer(); + + _renderProcedure(); + + SwapBuffers(_window); + + PollEvents(); + } + + end = GetTime(); + + deltaSinceClear = deltaSinceClear + end - start; + } + + return; + } + + sfn RunTimingLoop() + { + return; + } + + sfn SetCurrentContext(const ptr _window) + { + try + { + glfwMakeContextCurrent(_window); + + ptr< RawString > ErrorMsg = NULL; + + int code = glfwGetError(ErrorMsg); + + if (code == GLFW_NO_WINDOW_CONTEXT) + { + throw std::runtime_error( Dref(ErrorMsg) ); + } + } + catch (std::runtime_error _error) + { + ErrorRuntime(_error); + + Exit(ExitCode::Failed); + } + + return; + } + + sfn TerminateGLFW() + { + glfwTerminate(); + + return; + } +} diff --git a/DGL_Buffers.hpp b/DGL_Buffers.hpp new file mode 100644 index 0000000..d588d4e --- /dev/null +++ b/DGL_Buffers.hpp @@ -0,0 +1,81 @@ +#pragma once + +// GLEW +#include + +// DGL +#include "DGL_FundamentalTypes.hpp" +#include "DGL_MiscTypes.hpp" +#include "DGL_Enum.hpp" + +// Non-Standard C++ +#include "Cpp_Alias.hpp" + + + +namespace GL +{ + sfn ClearBuffer() + { + glClear(GL_COLOR_BUFFER_BIT); + + return; + } + + sfn BindBuffer(EBufferTarget _targetType, ID _buffer) + { + glBindBuffer(GLenum(_targetType), _buffer); + + return; + } + + sfn BindVertexArray(ptr _referenceToTrackArray) + { + glBindVertexArray(Dref(_referenceToTrackArray)); + + return; + } + + template + sfn BufferData(ptr _data, EBufferTarget _targetType, EBufferUsage _usageType) + { + glBufferData(GLenum(_targetType), sizeof(TypeOfData), _data, GLenum(_usageType)); + + return; + } + + template + sfn FormatVertexAttributes + ( + gUInt _attributeIndex , + EDataType _vertexComponenetType , + ptr _firstVertexComponentLocation, + gInt _numberOfVertexComponents , + EBool _shouldNormalize + ) + { + glVertexAttribPointer + ( + _attributeIndex , + _numberOfVertexComponents , + GLenum(_vertexComponenetType), + GLenum(_shouldNormalize ), + sizeof(VertType ), + _firstVertexComponentLocation + ); + } + + sfn GenerateBuffers(ptr _bufferReferencer, uInt64 _numberOfBuffersToReserve) + { + glGenBuffers(_numberOfBuffersToReserve, _bufferReferencer); + + return; + } + + sfn GenerateVertexBuffers(ptr __referenceRetainerToBuffer, uInt64 _numOfObjectsToReserveFor) + { + glGenVertexArrays(_numOfObjectsToReserveFor, __referenceRetainerToBuffer); + + return; + } +} diff --git a/DGL_Enum.hpp b/DGL_Enum.hpp new file mode 100644 index 0000000..d443db7 --- /dev/null +++ b/DGL_Enum.hpp @@ -0,0 +1,86 @@ +#pragma once + +// GLEW +#include + + + +namespace GL +{ + enum class EBool + { + True = GL_TRUE , + False = GL_FALSE + }; + + enum class EBufferTarget + { + VertexAttributes = GL_ARRAY_BUFFER , + AtomicCounter = GL_ATOMIC_COUNTER_BUFFER , + CopySource = GL_COPY_READ_BUFFER , + CopyDestination = GL_COPY_WRITE_BUFFER , + IndirectComputeDispatchCommands = GL_DISPATCH_INDIRECT_BUFFER , + IndirectCommandArguments = GL_DRAW_INDIRECT_BUFFER , + VertexIndices = GL_ELEMENT_ARRAY_BUFFER , + PixelReadTarget = GL_PIXEL_PACK_BUFFER , + TextureDataSource = GL_PIXEL_UNPACK_BUFFER , + QueryResult = GL_QUERY_BUFFER , + ReadWriteShaderStorage = GL_SHADER_STORAGE_BUFFER , + TextureData = GL_TEXTURE_BUFFER , + TransformFeedback = GL_TRANSFORM_FEEDBACK_BUFFER, + UniformBlockStorage = GL_UNIFORM_BUFFER + }; + + enum class EBufferUsage + { + DynamicCopy = GL_DYNAMIC_COPY, + DynamicDraw = GL_DYNAMIC_DRAW, + DynamicRead = GL_DYNAMIC_READ, + + StreamCopy = GL_STREAM_COPY, + StreamDraw = GL_STREAM_DRAW, + StreamRead = GL_STREAM_READ, + + StaticCopy = GL_STATIC_COPY, + StaticDraw = GL_STATIC_DRAW, + StaticRead = GL_STATIC_READ + }; + + enum class EDataType + { + Byte = GL_BYTE , + UnsignedByte = GL_UNSIGNED_BYTE , + Short = GL_SHORT , + UnsignedShort = GL_UNSIGNED_SHORT, + Int = GL_INT , + UnsignedInt = GL_UNSIGNED_INT , + Fixed = GL_FIXED , + Half = GL_HALF_FLOAT , + Float = GL_FLOAT , + Double = GL_DOUBLE + }; + + enum class EShaderType + { + Vertex = GL_VERTEX_SHADER , + Fragment = GL_FRAGMENT_SHADER + }; + + enum class EPrimitives + { + Points = GL_POINTS, + + Lines = GL_LINES , + LineStrip = GL_LINE_STRIP, + LineLoop = GL_LINE_LOOP , + + Triangles = GL_TRIANGLES , + TriangleStrip = GL_TRIANGLE_STRIP, + TriangleFan = GL_TRIANGLE_FAN , + + Quads = GL_QUADS , + QuadsStrip = GL_QUAD_STRIP, + + Patches = GL_PATCHES + }; +} diff --git a/DGL_FundamentalTypes.hpp b/DGL_FundamentalTypes.hpp new file mode 100644 index 0000000..f11cbad --- /dev/null +++ b/DGL_FundamentalTypes.hpp @@ -0,0 +1,17 @@ +#pragma once + +// GLEW +#include + + + +namespace GL +{ + // Fundamental Types + + using gChar = GLchar ; + using gFloat = GLfloat; + using gInt = GLint ; + using gUInt = GLuint ; + using gSize = GLsizei; +} diff --git a/DGL_MiscTypes.hpp b/DGL_MiscTypes.hpp new file mode 100644 index 0000000..4d4c05b --- /dev/null +++ b/DGL_MiscTypes.hpp @@ -0,0 +1,29 @@ +#pragma once + +// OpenGL +#include + +// Duck Tape +#include "DGL_FundamentalTypes.hpp" + +// Non-Standard C++ +#include "Cpp_Alias.hpp" + + + +namespace GL +{ + // Made up types. + + template + using ID = gUInt; + + // ID Reference Types + + class Buffer ; + class Shader ; + class ShaderProgram; + + using DataPtr = ptr; + using VertexBuffer = gUInt ; +} diff --git a/DGL_Shader.hpp b/DGL_Shader.hpp new file mode 100644 index 0000000..3610d81 --- /dev/null +++ b/DGL_Shader.hpp @@ -0,0 +1,126 @@ +#pragma once + +// C++ +#include + +//DGL +#include "DGL_FundamentalTypes.hpp" +#include "DGL_MiscTypes.hpp" +#include "DGL_Enum.hpp" + + + +namespace GL +{ + // Some Raw Source Defaults: + + RawString RawVertextShaderSource = + "#version 330 core\n" + + "layout (location = 0) in vec3 aPos;\n" + + "void main()\n" + "{\n" + " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" + "}\0"; + + RawString RawFragmentShaderSource = + "#version 400\n" + + "out vec4 frag_colour;" + + "void main() {" + + " frag_colour = vec4(0.5, 0.0, 0.5, 1.0);" + + "}"; + + + // Functions + + sfn AttachShader(ID _shaderProgram, ID _shaderToAttach) + { + glAttachShader(_shaderProgram, _shaderToAttach); + + return; + } + + sfn BindShaderSource(ID _shader, uInt64 _numberOfStringElements, ptr< RawString> _sourceCode, ptr _lengthsOfStrings) + { + glShaderSource(_shader, _numberOfStringElements, _sourceCode, _lengthsOfStrings); + + return; + } + + sfn CompileShader(ID _shaderToCompile) + { + glCompileShader(_shaderToCompile); + + return; + } + + sfn CreateShader(EShaderType _typeOfShader) -> ID + { + return glCreateShader(GLenum(_typeOfShader)); + } + + sfn CreateShaderProgram() -> ID + { + return glCreateProgram(); + } + + sfn DeleteShader(ID _shaderToDelete) + { + glDeleteShader(_shaderToDelete); + + return; + } + + sfn LinkProgramShader(ID _shaderProgramToLink) + { + glLinkProgram(_shaderProgramToLink); + + return; + } + + sfn UseProgramShader(ID _shaderProgramToUse) + { + glUseProgram(_shaderProgramToUse); + + return; + } + + + // Raw Tape + + sfn MakeShader + ( + Ref(ID) _shaderIDHolder , + EShaderType _typeOfShader , + uInt64 _numberOfStringElements, + ptr> _sourceCode , + ptr< const gInt > _lengthsOfStrings + ) + { + _shaderIDHolder = CreateShader(_typeOfShader); + + BindShaderSource(_shaderIDHolder, _numberOfStringElements, _sourceCode, _lengthsOfStrings); + + CompileShader(_shaderIDHolder); + } + + sfn MakeShaderProgram(Ref(ID) _shaderProgramIDHolder, ID _shadersToAttach...) + { + _shaderProgramIDHolder = CreateShaderProgram(); + + va_list args; + + va_start(args, _shadersToAttach); + + AttachShader(_shaderProgramIDHolder, va_arg(args, ID)); + + va_end(args); + + LinkProgramShader(_shaderProgramIDHolder); + } +}