aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp116
1 files changed, 42 insertions, 74 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 24ef4f8..f6f827f 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,7 +1,6 @@
#include <cstdio>
#include "../include/RenderBackend/OpenGL/opengl.hpp"
-#include "../include/RenderBackend/OpenGL/VertexShader.hpp"
-#include "../include/RenderBackend/OpenGL/PixelShader.hpp"
+#include "../include/RenderBackend/OpenGL/Shader.hpp"
#include "../include/RenderBackend/OpenGL/ShaderProgram.hpp"
#include "../include/RenderBackend/OpenGL/DeviceMemory.hpp"
#include "../include/RenderBackend/OpenGL/DeviceFrame.hpp"
@@ -16,13 +15,11 @@
using namespace amalgine;
using namespace std;
-void glfwErrorHandler(int errorCode, const char *errorDescription);
-void initGlfw();
-void initGlew();
-GLFWwindow *createWindow();
-
-CompiledVertexShader* createVertexShader(const DeviceMemory &inputData);
-CompiledPixelShader* createPixelShader();
+static void glfwErrorHandler(int errorCode, const char *errorDescription);
+static void initGlfw();
+static void initGlew();
+static GLFWwindow *createWindow();
+static std::unique_ptr<Shader> load_shader_from_file(const char *filepath, Shader::Type shader_type);
int main()
{
@@ -40,25 +37,19 @@ int main()
DataView<Triangle2D> cpuTriangles(&cpuTriangle, 1);
DeviceMemory *gpuTriangle = frame.alloc();
gpuTriangle->copy(cpuTriangles, DeviceMemory::StorageType::STATIC);
+
+ std::unique_ptr<Shader> vertex_shader = load_shader_from_file("shaders/vertex.vert", Shader::Type::VERTEX);
+ std::unique_ptr<Shader> pixel_shader = load_shader_from_file("shaders/fragment.frag", Shader::Type::PIXEL);
- CompiledVertexShader *vertexShader = createVertexShader(*gpuTriangle);
- CompiledPixelShader *pixelShader = createPixelShader();
-
- ShaderProgram shaderProgram;
- shaderProgram.setVertexShader(vertexShader);
- shaderProgram.setPixelShader(pixelShader);
- // Make ShaderProgram.build return an object which we can do things on? since some operations
- // require the shader to be built to be used
- Result<bool> shaderBuildResult = shaderProgram.build();
-
- ShaderProgramGlobalVec3 triangleColor = shaderProgram.getGlobalVec3("triangleColor");
- triangleColor.set(1.0f, 0.0f, 0.0f);
-
- if(!shaderBuildResult)
- {
- fprintf(stderr, "Failed to build shader program: %s\n", shaderBuildResult.getErrorMsg().c_str());
- exit(20);
+ Result<std::unique_ptr<ShaderProgram>> shader_program_result = ShaderProgram::build({ vertex_shader.get(), pixel_shader.get() });
+ if(!shader_program_result) {
+ fprintf(stderr, "Failed to link shaders, error: %s\n", shader_program_result.getErrorMsg().c_str());
+ exit(13);
}
+ std::unique_ptr<ShaderProgram> shader_program = std::move(shader_program_result.unwrap());
+
+ shader_program->set_vertex_input("position", *gpuTriangle);
+ shader_program->set_uniform("triangle_color", vec3f{ 1.0f, 0.0f, 0.0f });
while(!glfwWindowShouldClose(window))
{
@@ -70,8 +61,7 @@ int main()
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Do the actual screen clearing, using the color set using glClearColor
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- shaderProgram.use();
- frame.draw();
+ frame.draw(shader_program.get());
glfwSwapBuffers(window);
}
@@ -128,53 +118,31 @@ GLFWwindow* createWindow()
return window;
}
-CompiledVertexShader* createVertexShader(const DeviceMemory &inputData)
-{
- VertexShader vertexShader;
- ShaderInputVec2 inputPosition = vertexShader.defineInputVec2("position");
- inputPosition.setData(inputData);
- #if 0
- ShaderGlobalMat4x4 model = vertexShader.defineGlobalMat4x4("model");
- ShaderGlobalMat4x4 view = vertexShader.defineGlobalMat4x4("view");
- ShaderGlobalMat4x4 proj = vertexShader.defineGlobalMat4x4("proj");
-
- vertexShader.defineMain([&inputPosition, &model, &view, &proj]()
- {
- return proj * view * model * ShaderVec4(inputPosition, 0.0f, 1.0f);
- });
- #endif
- vertexShader.defineMain([&inputPosition]()
- {
- return ShaderVec4(inputPosition, 0.0f, 1.0f);
- });
-
- Result<CompiledVertexShader*> compiledVertexShader = vertexShader.compile();
- if(!compiledVertexShader)
- {
- fprintf(stderr, "Failed to compile vertex shader:\n%s", compiledVertexShader.getErrorMsg().c_str());
- exit(2);
+static int file_read_all(const char *filepath, std::string &result) {
+ FILE *file = fopen(filepath, "rb");
+ if(!file) {
+ perror("file_read_all");
+ return errno;
}
-
- return compiledVertexShader.unwrap();
+
+ fseek(file, 0, SEEK_END);
+ size_t file_size = ftell(file);
+ fseek(file, 0, SEEK_SET);
+ result.resize(file_size);
+ fread(&result[0], 1, result.size(), file);
+ fclose(file);
+ return 0;
}
-CompiledPixelShader* createPixelShader()
-{
- PixelShader pixelShader;
- ShaderGlobalVec3 triangleColor = pixelShader.defineGlobalVec3("triangleColor");
- ShaderOutputVec4 outColor = pixelShader.defineOutputVec4("outColor");
-
- pixelShader.defineMain([&triangleColor, &outColor]()
- {
- outColor = ShaderVec4(triangleColor, 1.0f);
- });
-
- Result<CompiledPixelShader*> compiledPixelShader = pixelShader.compile();
- if(!compiledPixelShader)
- {
- fprintf(stderr, "Failed to compile pixel shader:\n%s", compiledPixelShader.getErrorMsg().c_str());
- exit(2);
+std::unique_ptr<Shader> load_shader_from_file(const char *filepath, Shader::Type shader_type) {
+ std::string file_content;
+ if(file_read_all(filepath, file_content) != 0)
+ exit(13);
+
+ Result<std::unique_ptr<Shader>> vertex_shader_result = Shader::compile(shader_type, file_content.data(), file_content.size());
+ if(!vertex_shader_result) {
+ fprintf(stderr, "Failed to compile shader, error: %s\n", vertex_shader_result.getErrorMsg().c_str());
+ exit(12);
}
-
- return compiledPixelShader.unwrap();
-}
+ return std::move(vertex_shader_result.unwrap());
+} \ No newline at end of file