diff --git a/Hohmann b/Hohmann index 8515bfd..bcbd99f 100755 Binary files a/Hohmann and b/Hohmann differ diff --git a/src/main.cpp b/src/main.cpp index 921985c..90e3f0e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,27 @@ #include #include #include +#include + +#include "shaders.cpp" #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 +const char *vertexShaderSource = "#version 330 core\n" + "layout (location = 0) in vec3 aPos;\n" + "void main()\n" + "{\n" + " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" + "}\0"; + +const char *fragShaderSource = "#version 330 core\n" +"out vec4 FragColor;\n" +"void main()\n" +"{\n" +" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" +"}\0"; + static float verts[] = { -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, @@ -22,16 +39,41 @@ void processInput(GLFWwindow *win) { } } -void renderCallback(GLFWwindow *win) { - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - +void preRenderCallback(GLFWwindow *win) { unsigned int VBO; glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_DYNAMIC_DRAW); // for moving stuff //glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); + + // Compile a bunch of shaders etc + unsigned int vertShader = Shader::compile(GL_VERTEX_SHADER, vertexShaderSource); + unsigned int fragShader = Shader::compile(GL_FRAGMENT_SHADER, fragShaderSource); + unsigned int shaders[2] = {vertShader, fragShader}; + + // Create a shader program & link the shaders etc + unsigned int shaderProg; + shaderProg = Shader::createProgram( std::vector (std::begin(shaders), std::end(shaders)) ); + + // Copy verts into buffer + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); + + // Set attrib pointers + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + + // Use the shader program whenever we want to render + glUseProgram(shaderProg); + glDeleteShader(vertShader); + glDeleteShader(vertShader); + +} + +void renderCallback(GLFWwindow *win) { + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); } int main() { @@ -57,6 +99,8 @@ int main() { glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); glfwSetFramebufferSizeCallback(win, framebuffer_size_callback); + preRenderCallback(win); + while (!glfwWindowShouldClose(win)) { // Handle input processInput(win); diff --git a/src/shader_loader.cpp b/src/shader_loader.cpp deleted file mode 100644 index 51da84c..0000000 --- a/src/shader_loader.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include - diff --git a/src/shaders.cpp b/src/shaders.cpp new file mode 100644 index 0000000..11f6d80 --- /dev/null +++ b/src/shaders.cpp @@ -0,0 +1,48 @@ +#include +#include +#include + +namespace Shader { + unsigned int compile(GLenum shadertype, const char* shaderSource) { + unsigned int shader; + shader = glCreateShader(shadertype); + glShaderSource(shader, 1, &shaderSource, NULL); + glCompileShader(shader); + + int success; + char infolog[512]; + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(shader, 512, NULL, infolog); + printf("Shader Compilation Error:\n%s\n", infolog); + } else { + printf("Compiled shader %u!\n", shadertype); + } + + return shader; + } + + unsigned int createProgram(std::vector shaders) { + unsigned int shaderProg; + shaderProg = glCreateProgram(); + + // Link the shaders + for(unsigned int s : shaders) + glAttachShader(shaderProg, s); + + glLinkProgram(shaderProg); + + // Check for errors + int success; + char infolog[512]; + glGetProgramiv(shaderProg, GL_LINK_STATUS, &success); + if (!success) { + glGetProgramInfoLog(shaderProg, 512, NULL, infolog); + printf("Shader Program Linking Error:\n%s\n", infolog); + } else { + printf("Linked shaders & created program: %u\n", shaderProg); + } + + return shaderProg; + } +}