diff options
author | dec05eba <dec05eba@protonmail.com> | 2017-12-20 20:55:05 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-11-18 15:21:46 +0100 |
commit | ca92d8c90f7103db6d7cae4cef49b278d804b474 (patch) | |
tree | baf44e42fcc1cf7e871a7b2f1d3d92f680f3a845 /src/main.cpp | |
parent | 3d7f123c0fc0af4b8df3ad791b264d22268bd423 (diff) |
Create shader using c++ code
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..fe48ddf --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,88 @@ +#include <cstdio> +#include <GL/glew.h> +#include <GLFW/glfw3.h> +#include <glm/glm.hpp> +#include "../include/RenderBackend/OpenGL/VertexShader.hpp" +#include "../include/RenderBackend/OpenGL/PixelShader.hpp" +#include "../include/RenderBackend/OpenGL/DeviceMemory.hpp" + +using namespace amalgine; +using namespace std; + +int main() +{ + if(!glfwInit()) + { + fprintf(stderr, "Failed to initialize GLFW\n"); + return -1; + } + + glfwWindowHint(GLFW_SAMPLES, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); + + GLFWwindow *window = glfwCreateWindow(1920, 1080, "Amalgine", nullptr, nullptr); + if(!window) + { + fprintf(stderr, "Failed to open GLFW window\n"); + glfwTerminate(); + return -1; + } + + glfwMakeContextCurrent(window); + glewExperimental = true; + if(glewInit() != GLEW_OK) + { + fprintf(stderr, "Failed to initialize GLEW\n"); + glfwTerminate(); + return -1; + } + + glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); + + + f32 verticesRaw[] = + { + 0.0f, 0.5f, + 0.5f, -0.5f, + -0.5f, -0.5f + }; + DataView<f32> vertices(verticesRaw, 6); + DeviceMemory triangle; + triangle.copyStatic(vertices); + + VertexShader vertexShader; + ShaderInputVec2 inputPosition = vertexShader.defineInputVec2("position"); + + vertexShader.defineMain([&inputPosition]() + { + return ShaderVec4(inputPosition, 0.0f, 1.0f); + }); + + PixelShader pixelShader; + ShaderOutputVec4 outColor = pixelShader.defineOutputVec4("outColor"); + + pixelShader.defineMain([&outColor]() + { + outColor = ShaderVec4(1.0f, 1.0f, 1.0f, 1.0f); + }); + + string vertexShaderSource = vertexShader.build(); + printf("Vertex shader source:\n%s", vertexShaderSource.c_str()); + string pixelShaderSource = pixelShader.build(); + printf("Pixel shader source:\n%s", pixelShaderSource.c_str()); + + while(!glfwWindowShouldClose(window)) + { + if(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS) + glfwSetWindowShouldClose(window, GL_TRUE); + + glfwSwapBuffers(window); + glfwPollEvents(); + } + + return 0; +} |