init
This commit is contained in:
parent
ef8a42de8f
commit
00d6f03442
31
Engine/Engine.sln
Normal file
31
Engine/Engine.sln
Normal file
|
@ -0,0 +1,31 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.5.33516.290
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Engine", "Engine\Engine.vcxproj", "{3BEE640E-71B2-4D18-9D8C-8B6B85CAE21A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{3BEE640E-71B2-4D18-9D8C-8B6B85CAE21A}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{3BEE640E-71B2-4D18-9D8C-8B6B85CAE21A}.Debug|x64.Build.0 = Debug|x64
|
||||
{3BEE640E-71B2-4D18-9D8C-8B6B85CAE21A}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{3BEE640E-71B2-4D18-9D8C-8B6B85CAE21A}.Debug|x86.Build.0 = Debug|Win32
|
||||
{3BEE640E-71B2-4D18-9D8C-8B6B85CAE21A}.Release|x64.ActiveCfg = Release|x64
|
||||
{3BEE640E-71B2-4D18-9D8C-8B6B85CAE21A}.Release|x64.Build.0 = Release|x64
|
||||
{3BEE640E-71B2-4D18-9D8C-8B6B85CAE21A}.Release|x86.ActiveCfg = Release|Win32
|
||||
{3BEE640E-71B2-4D18-9D8C-8B6B85CAE21A}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {25B3C080-617C-4BEA-9F36-818FB47E657F}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
158
Engine/Engine/Engine.vcxproj
Normal file
158
Engine/Engine/Engine.vcxproj
Normal file
|
@ -0,0 +1,158 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{3bee640e-71b2-4d18-9d8c-8b6b85cae21a}</ProjectGuid>
|
||||
<RootNamespace>Engine</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<IncludePath>T:\Privat\Daniel\Selfmade\libraries\glad\include;T:\Privat\Daniel\Selfmade\libraries\glfw\include;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>T:\Privat\Daniel\Selfmade\libraries\glfw\lib;$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<IncludePath>C:\Users\Daniel\Documents\SelfmadeProgramme\libraries\glad\include;C:\Users\Daniel\Documents\SelfmadeProgramme\libraries\glfw\include;$(IncludePath)</IncludePath>
|
||||
<LibraryPath>C:\Users\Daniel\Documents\SelfmadeProgramme\libraries\glfw\lib;$(LibraryPath)</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>glfw3.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>glfw3.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\..\libraries\glad\src\glad.c" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="Shaders\Shader.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Shaders\default\default.frag" />
|
||||
<None Include="Shaders\default\default.vert" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Exceptions\IOException.h" />
|
||||
<ClInclude Include="Exceptions\Shaders\CreateProgramException.h" />
|
||||
<ClInclude Include="Exceptions\Shaders\ShaderCompileException.h" />
|
||||
<ClInclude Include="Shaders\Enums.h" />
|
||||
<ClInclude Include="Shaders\Shader.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
51
Engine/Engine/Engine.vcxproj.filters
Normal file
51
Engine/Engine/Engine.vcxproj.filters
Normal file
|
@ -0,0 +1,51 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\..\libraries\glad\src\glad.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Shaders\Shader.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Shaders\Shader.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Exceptions\IOException.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Exceptions\Shaders\ShaderCompileException.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Shaders\Enums.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Exceptions\Shaders\CreateProgramException.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Shaders\default\default.vert">
|
||||
<Filter>Resource Files</Filter>
|
||||
</None>
|
||||
<None Include="Shaders\default\default.frag" />
|
||||
</ItemGroup>
|
||||
</Project>
|
22
Engine/Engine/exceptions/IOException.h
Normal file
22
Engine/Engine/exceptions/IOException.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Nebulix
|
||||
{
|
||||
/// <summary>
|
||||
/// This exception will be thrown if there is an error to do with its IO. For example opening the file
|
||||
/// </summary>
|
||||
class IOException : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
using runtime_error::runtime_error;
|
||||
|
||||
IOException() : runtime_error{ "ERROR::FILE::IO" } {}
|
||||
/// <summary>
|
||||
/// Overrides the default error message
|
||||
/// </summary>
|
||||
IOException(std::string message) : runtime_error{ message } {}
|
||||
|
||||
private:
|
||||
};
|
||||
}
|
15
Engine/Engine/exceptions/shaders/CreateProgramException.h
Normal file
15
Engine/Engine/exceptions/shaders/CreateProgramException.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#pragma once
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Nebulix
|
||||
{
|
||||
class CreateProgramException : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
using runtime_error::runtime_error;
|
||||
|
||||
CreateProgramException(std::string &message) : runtime_error{ "ERROR::SHADER::CREATE_PROGRAM\n" + message } {}
|
||||
|
||||
private:
|
||||
};
|
||||
}
|
39
Engine/Engine/exceptions/shaders/ShaderCompileException.h
Normal file
39
Engine/Engine/exceptions/shaders/ShaderCompileException.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
#pragma once
|
||||
#include <stdexcept>
|
||||
#include "../../shaders/Enums.h"
|
||||
|
||||
namespace Nebulix
|
||||
{
|
||||
class ShaderCompileException: public std::exception
|
||||
{
|
||||
public:
|
||||
ShaderCompileException() = delete;
|
||||
|
||||
ShaderCompileException(Nebulix::ShaderType shaderType, std::string compileMessage)
|
||||
{
|
||||
std::string shader;
|
||||
|
||||
switch (shaderType)
|
||||
{
|
||||
case Nebulix::ShaderType::Vertex:
|
||||
shader = std::string{"VERTEX"};
|
||||
break;
|
||||
case Nebulix::ShaderType::Fragment:
|
||||
shader = std::string{ "FRAGMENT" };
|
||||
break;
|
||||
default:
|
||||
shader = std::string{ "UNKNOWN" };
|
||||
break;
|
||||
}
|
||||
_message = "ERROR::SHADER::" + shader + "::COMPILATION_FAILED\n Error message -> " + compileMessage;
|
||||
}
|
||||
|
||||
virtual const char* what() const override
|
||||
{
|
||||
return _message.c_str();
|
||||
}
|
||||
|
||||
private:
|
||||
std::string _message;
|
||||
};
|
||||
}
|
131
Engine/Engine/main.cpp
Normal file
131
Engine/Engine/main.cpp
Normal file
|
@ -0,0 +1,131 @@
|
|||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
#include "shaders/Shader.h"
|
||||
|
||||
// Continue: https://learnopengl.com/Getting-started/Shaders
|
||||
// Chapter: Not yet started
|
||||
//
|
||||
|
||||
|
||||
//
|
||||
// HOW TO redirect error output to file
|
||||
// Maybe it is possible to redirect to another stream object which adds infos like thread id/time etc.?
|
||||
//
|
||||
// std::ofstream errorFile("error.log");
|
||||
// std::streambuf* originalCerrBuffer = std::cerr.rdbuf(); // Save original buffer
|
||||
// Redirect cerr to the error file
|
||||
// std::cerr.rdbuf(errorFile.rdbuf());
|
||||
|
||||
#define WINDOW_WIDTH 800
|
||||
#define WINDOW_HEIGHT 600
|
||||
|
||||
|
||||
void OnWindowResize(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void ProcessInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Henlo worl", nullptr, nullptr);
|
||||
|
||||
if (window == nullptr)
|
||||
{
|
||||
std::cerr << "Failed to create GLFW window";
|
||||
glfwTerminate();
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
{
|
||||
std::cerr << "Failed to initialise GLAD";
|
||||
return -1;
|
||||
}
|
||||
|
||||
OnWindowResize(window, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
glfwSetFramebufferSizeCallback(window, OnWindowResize);
|
||||
|
||||
GLfloat vertices[] = {
|
||||
0.5f, 0.5f, 0.0f, // top right
|
||||
0.5f, -0.5f, 0.0f, // bottom right
|
||||
-0.5f, -0.5f, 0.0f, // bottom left
|
||||
-0.5f, 0.5f, 0.0f // top left
|
||||
};
|
||||
GLuint indices[]{
|
||||
0, 1, 3,
|
||||
1, 2, 3
|
||||
};
|
||||
|
||||
GLuint vertexBufferObject, elementBufferObject, vertexArrayObject;
|
||||
glGenVertexArrays(1, &vertexArrayObject);
|
||||
glGenBuffers(1, &vertexBufferObject);
|
||||
glGenBuffers(1, &elementBufferObject);
|
||||
|
||||
glBindVertexArray(vertexArrayObject);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
// I could unbind the VAO, but this is usually not needed because when creating a new VAO we need to bind that to change it anyway, so there shouldn't be a problem
|
||||
|
||||
std::string vertexPath = "shaders/default/default.vert";
|
||||
std::string fragmentPath = "shaders/default/default.frag";
|
||||
|
||||
std::unique_ptr<Nebulix::Shader> shader;
|
||||
try
|
||||
{
|
||||
shader = std::make_unique<Nebulix::Shader>(vertexPath, fragmentPath);
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
std::cerr << e.what();
|
||||
return -1;
|
||||
}
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
|
||||
// main loop
|
||||
while(!glfwWindowShouldClose(window))
|
||||
{
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
ProcessInput(window);
|
||||
|
||||
shader->Use();
|
||||
glBindVertexArray(vertexArrayObject);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
// optional, as we are at the end of the program.
|
||||
glDeleteVertexArrays(1, &vertexArrayObject);
|
||||
glDeleteBuffers(1, &elementBufferObject);
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
12
Engine/Engine/shaders/Enums.h
Normal file
12
Engine/Engine/shaders/Enums.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
namespace Nebulix
|
||||
{
|
||||
enum class ShaderType
|
||||
{
|
||||
Vertex,
|
||||
Fragment,
|
||||
Unknown
|
||||
// TODO: Add compute etc in future
|
||||
};
|
||||
}
|
98
Engine/Engine/shaders/Shader.cpp
Normal file
98
Engine/Engine/shaders/Shader.cpp
Normal file
|
@ -0,0 +1,98 @@
|
|||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include "Shader.h"
|
||||
|
||||
#include "../Exceptions/IOException.h"
|
||||
#include "../Exceptions/Shaders/ShaderCompileException.h"
|
||||
#include "../exceptions/shaders/CreateProgramException.h"
|
||||
|
||||
namespace Nebulix
|
||||
{
|
||||
|
||||
const char* vertexShaderSourceCode = "#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";
|
||||
const char* fragmentShaderSourceCode = "#version 330 core\n"
|
||||
"out vec4 FragColor;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
|
||||
"}\n\0";
|
||||
|
||||
Shader::Shader(std::string& vertexShaderfile, std::string& fragmentShaderfile): _vertexFile{vertexShaderfile}, _fragmentFile{fragmentShaderfile}
|
||||
{
|
||||
std::ifstream vertexShaderSource(vertexShaderfile), fragmentShaderSource(fragmentShaderfile);
|
||||
GLuint vertexShader = 0, fragmentShader = 0;
|
||||
|
||||
CompileShadercode(vertexShaderSource, vertexShader, ShaderType::Vertex);
|
||||
CompileShadercode(fragmentShaderSource, fragmentShader, ShaderType::Fragment);
|
||||
|
||||
CreateProgram(vertexShader, fragmentShader);
|
||||
}
|
||||
|
||||
void Shader::CompileShadercode(std::ifstream& shaderSource, GLuint& shaderObject, ShaderType type)
|
||||
{
|
||||
if (!shaderSource.is_open())
|
||||
{
|
||||
throw IOException("ERROR::SHADER::CANNOT_OPEN_FILE");
|
||||
}
|
||||
|
||||
const char* shader;
|
||||
|
||||
shaderSource.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
try
|
||||
{
|
||||
std::stringstream sourceCode;
|
||||
sourceCode << shaderSource.rdbuf();
|
||||
shaderSource.close();
|
||||
std::string code = sourceCode.str();
|
||||
shader = code.c_str();
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
throw Nebulix::IOException("ERROR::SHADER::READ_FILE");
|
||||
}
|
||||
|
||||
|
||||
if (type == ShaderType::Vertex)
|
||||
shaderObject = glCreateShader(GL_VERTEX_SHADER);
|
||||
else if (type == ShaderType::Fragment)
|
||||
shaderObject = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
else
|
||||
throw std::runtime_error("ERROR::SHADER::TYPE \nCannot create shader program with the given shader type '" + std::to_string((int)type) + "'");
|
||||
|
||||
glShaderSource(shaderObject, 1, &shader, NULL);
|
||||
glCompileShader(shaderObject);
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
glGetShaderiv(shaderObject, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(shaderObject, 512, NULL, infoLog);
|
||||
throw Nebulix::ShaderCompileException(type, infoLog);
|
||||
}
|
||||
}
|
||||
|
||||
void Shader::CreateProgram(GLuint& vertexShader, GLuint& fragmentShader)
|
||||
{
|
||||
shaderId = glCreateProgram();
|
||||
glAttachShader(shaderId, vertexShader);
|
||||
glAttachShader(shaderId, fragmentShader);
|
||||
glLinkProgram(shaderId);
|
||||
|
||||
int success;
|
||||
char infoLog[512];
|
||||
glGetProgramiv(shaderId, GL_LINK_STATUS, &success);
|
||||
if (!success) {
|
||||
glGetProgramInfoLog(shaderId, 512, NULL, infoLog);
|
||||
throw Nebulix::CreateProgramException(infoLog);
|
||||
}
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
}
|
||||
}
|
46
Engine/Engine/shaders/Shader.h
Normal file
46
Engine/Engine/shaders/Shader.h
Normal file
|
@ -0,0 +1,46 @@
|
|||
#pragma once
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <glad/glad.h>
|
||||
#include "Enums.h"
|
||||
|
||||
|
||||
namespace Nebulix
|
||||
{
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
Shader() = delete;
|
||||
Shader(std::string& vertexShaderfile, std::string& fragmentShaderfile);
|
||||
~Shader()
|
||||
{
|
||||
glDeleteProgram(shaderId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// To actually use and activate the shader, call this.
|
||||
/// </summary>
|
||||
void Use() { glUseProgram(shaderId); }
|
||||
|
||||
void setBool(const std::string& name, bool value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(shaderId, name.c_str()), (int)value);
|
||||
}
|
||||
void setInt(const std::string& name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(shaderId, name.c_str()), value);
|
||||
}
|
||||
void setFloat(const std::string& name, float value) const
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(shaderId, name.c_str()), value);
|
||||
}
|
||||
|
||||
private:
|
||||
std::string &_vertexFile, &_fragmentFile;
|
||||
GLuint shaderId;
|
||||
|
||||
void CompileShadercode(std::ifstream& shaderSource, GLuint& shaderId, ShaderType type);
|
||||
void CreateProgram(GLuint& vertexShader, GLuint& fragmentShader);
|
||||
};
|
||||
}
|
7
Engine/Engine/shaders/default/default.frag
Normal file
7
Engine/Engine/shaders/default/default.frag
Normal file
|
@ -0,0 +1,7 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
||||
}
|
7
Engine/Engine/shaders/default/default.vert
Normal file
7
Engine/Engine/shaders/default/default.vert
Normal file
|
@ -0,0 +1,7 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user