diff --git a/build.sh b/build.sh index 84686de..ab45978 100755 --- a/build.sh +++ b/build.sh @@ -1,2 +1,2 @@ #!/usr/bin/env bash -cd build && cmake .. && make && ./TimeCoil +cd build && cmake -DCMAKE_C_FLAGS="-g" .. && make && ./TimeCoil diff --git a/shaders/shader.frag b/shaders/shader.frag new file mode 100644 index 0000000..1ce7c9f --- /dev/null +++ b/shaders/shader.frag @@ -0,0 +1,6 @@ +#version 330 core +out vec4 FragColor; +void main() +{ + FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); +} \ No newline at end of file diff --git a/shaders/shader.vert b/shaders/shader.vert new file mode 100644 index 0000000..5d16282 --- /dev/null +++ b/shaders/shader.vert @@ -0,0 +1,6 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +void main() +{ + gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index bb70083..02fbf2f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include void framebufferSizeCallback(GLFWwindow *window, int width, int height) { glViewport(10, 10, width-10, height-10); @@ -16,20 +18,91 @@ void renderGraphics(GLFWwindow *window) { } +void gracefulExit(int code) { + glfwTerminate(); + exit(code); +} + +std::string readFile(std::string path) { + std::ifstream file(path); + if (!file.is_open()) + std::cerr << "File " << path << " was not found." << std::endl; + + std::string content = ""; + std::string buffer = ""; + + while(getline(file, buffer)) { + content += buffer + "\n"; + } + + content += '\0'; + std::cout << content << std::endl; + file.close(); + return content; +} + +unsigned int prepareShaders() { + unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); + unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + + std::string vertexShaderSourceString = readFile("../shaders/shader.vert"); + std::string fragmentShaderSourceString = readFile("../shaders/shader.frag"); + + const char *vertexShaderSource = vertexShaderSourceString.c_str(); + const char *fragmentShaderSource = fragmentShaderSourceString.c_str(); + + glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); + glCompileShader(vertexShader); + + int success; + char log[512]; + glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(vertexShader, 512, NULL, log); + std::cerr << "Could not compile vertex shader: " << log << std::endl; + gracefulExit(-1); + } + glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); + glCompileShader(fragmentShader); + + glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(fragmentShader, 512, NULL, log); + std::cerr << "Could not compile fragment shader: " << log << std::endl; + gracefulExit(-1); + } + + unsigned int shaderProgram; + shaderProgram = glCreateProgram(); + glAttachShader(shaderProgram, vertexShader); + glAttachShader(shaderProgram, fragmentShader); + glLinkProgram(shaderProgram); + + glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); + if(!success) { + glGetProgramInfoLog(shaderProgram, 512, NULL, log); + std::cerr << "Could not link program: " << log << std::endl; + gracefulExit(-1); + } + + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); + return shaderProgram; +} + int main () { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - #ifdef __APPLE__ +#ifdef __APPLE__ glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); - #endif +#endif GLFWwindow *window = glfwCreateWindow(800, 600, "TimeCoil", NULL, NULL); if (window == NULL) { std::cerr << "Failed to create GLFW window." << std::endl; - glfwTerminate(); - return -1; + gracefulExit(-1); } glfwMakeContextCurrent(window); @@ -40,16 +113,43 @@ int main () { return -1; } + unsigned int shaderProgram = prepareShaders(); + + float vertices[] = { + -0.5, 0.0, 0.0, + 0.5, 0.5, 0.0, + 0.5, -0.5, 0.0 + }; + + unsigned int VBO, VAO; + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + glBindVertexArray(VAO); + + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + while(!glfwWindowShouldClose(window)) { processInput(window); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); + glUseProgram(shaderProgram); + glBindVertexArray(VAO); + glDrawArrays(GL_TRIANGLES, 0, 3); + glfwSwapBuffers(window); - glfwPollEvents(); + glfwPollEvents(); } - glfwTerminate(); - return 0; + glDeleteVertexArrays(1, &VAO); + glDeleteBuffers(1, &VBO); + glDeleteProgram(shaderProgram); + gracefulExit(0); }