aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 0e72df08111be58d019bf2593301baedb2f5139c (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
#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"

// 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 initGlfw();
void initGlew();
GLFWwindow *createWindow();

CompiledVertexShader* createVertexShader(const DeviceMemory &inputData);
CompiledPixelShader* createPixelShader();

int main()
{
    initGlfw();
    GLFWwindow *window = createWindow();
    
    DeviceFrame frame;
    
    f32 verticesRaw[] = 
    {
        0.0f, 0.5f,
        0.5f, -0.5f,
        -0.5f, -0.5f
    };
    DeviceMemory *triangle = frame.alloc();
    DataView<f32> vertices(verticesRaw, 6);
    triangle->copy(vertices, DeviceMemory::StorageType::STATIC);
    
    CompiledVertexShader *vertexShader = createVertexShader(*triangle);
    CompiledPixelShader *pixelShader = createPixelShader();
    
    ShaderProgram shaderProgram;
    shaderProgram.setVertexShader(vertexShader);
    shaderProgram.setPixelShader(pixelShader);
    Result<bool> shaderBuildResult = shaderProgram.build();
    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);

        shaderProgram.use();
        frame.draw();
        glfwSwapBuffers(window);
    }

    glfwTerminate();
    return 0;
}

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);
    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;
    ShaderOutputVec4 outColor = pixelShader.defineOutputVec4("outColor");
    
    pixelShader.defineMain([&outColor]()
    {
        outColor = ShaderVec4(1.0f, 1.0f, 1.0f, 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();
}