From ca92d8c90f7103db6d7cae4cef49b278d804b474 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 20 Dec 2017 20:55:05 +0100 Subject: Create shader using c++ code --- src/main.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/main.cpp (limited to 'src/main.cpp') 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 +#include +#include +#include +#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 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; +} -- cgit v1.2.3