aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: ada75e2e547d526f23aad4d60e23233e2a837393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <cstdio>
#include "../include/RenderBackend/OpenGL/opengl.hpp"
#include "../include/RenderBackend/OpenGL/VertexShader.hpp"
#include "../include/RenderBackend/OpenGL/PixelShader.hpp"
#include "../include/RenderBackend/OpenGL/ShaderProgram.hpp"
#include "../include/RenderBackend/OpenGL/DeviceMemory.hpp"
#include "../include/RenderBackend/OpenGL/DeviceFrame.hpp"
#include "../include/Triangle2D.hpp"

// TODO: Disallow shader variables that begin with "gl_" as reserved variables
// start with that. What about hlsl?

// TODO: Creating buffers etc should be somehow created/drawn using the window object, since they are associated with the window
// opengl context

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();

int main()
{
    initGlfw();
    glfwSetErrorCallback(glfwErrorHandler);
    GLFWwindow *window = createWindow();
    
    DeviceFrame frame;
    
    Triangle2D cpuTriangle(
        Vertex2D(0.0f, 0.5f),
        Vertex2D(0.5f, -0.5f),
        Vertex2D(-0.5f, -0.5f)
    );
    DataView<Triangle2D> cpuTriangles(&cpuTriangle, 1);
    DeviceMemory *gpuTriangle = frame.alloc();
    gpuTriangle->copy(cpuTriangles, DeviceMemory::StorageType::STATIC);
    
    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);
    }
    
    while(!glfwWindowShouldClose(window))
    {
        glfwPollEvents();
        if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, GL_TRUE);

        // Set color for clearing
        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();
        glfwSwapBuffers(window);
    }

    glfwTerminate();
    return 0;
}

void glfwErrorHandler(int errorCode, const char *errorDescription)
{
    printf("GLFW error code: %d, description: %s\n", errorCode, errorDescription);
}

void initGlfw()
{
    if(!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW\n");
        exit(-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);
}

void initGlew()
{
    glewExperimental = true;
    if(glewInit() != GLEW_OK)
    {
        fprintf(stderr, "Failed to initialize GLEW\n");
        glfwTerminate();
        exit(-1);
    }
}

GLFWwindow* createWindow()
{
    GLFWwindow *window = glfwCreateWindow(1280, 720, "Amalgine", nullptr, nullptr);
    if(!window)
    {
        fprintf(stderr, "Failed to open GLFW window\n");
        glfwTerminate();
        exit(10);
    }

    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(0);
    initGlew();
    return window;
}

CompiledVertexShader* createVertexShader(const DeviceMemory &inputData)
{
    VertexShader vertexShader;
    ShaderInputVec2 inputPosition = vertexShader.defineInputVec2("position");
    inputPosition.setData(inputData);
    
    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);
    }
    
    return compiledVertexShader.unwrap();
}

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);
    }
    
    return compiledPixelShader.unwrap();
}