#include #include #include #include #include "src/standard_ints.h" #include "src/lgl_callbacks.h" #include "src/lgl_shader_tools.h" int main(void) { std::cout << "Hello World." << std::endl; // Setup GLFW context glfwInit(); // Establish 4.2 GL requirement glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Build the winodw GLFWwindow *window = glfwCreateWindow(960, 400, "Hello MyBox", NULL, NULL); glfwSetWindowPos(window, 140, 1000); if (window == NULL) { std::cout << "Failed to create GLFW window." << std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); // Set the resize callback; glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); // Setup the glad loader if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initalize GLAD." << std::endl; return -1; } /* ====================================================================== */ /* ====================================================================== */ /* ====================================================================== */ i32 success; char infoLog[512]; // Create the vertex shader GLchar *shdr_src_vertex; readshader("vertex.vert", &shdr_src_vertex); u32 vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &shdr_src_vertex, NULL); glCompileShader(vertexShader); glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); freeshader(shdr_src_vertex); if (!success) { glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); std::cout << "ERROR SHADER::VERTEX::Compilation Failed\n" << infoLog << std::endl; return -1; } // Create the fragment shader GLchar *shdr_src_fragment; readshader("fragment.frag", &shdr_src_fragment); u32 fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &shdr_src_fragment, NULL); glCompileShader(fragmentShader); glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); freeshader(shdr_src_fragment); if (!success) { glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); std::cout << "ERROR SHADER::FRAGMENT::Compilation Failed\n" << infoLog << std::endl; return -1; } // Create the shader program u32 shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); if (!success) { glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); std::cout << "ERROR SHADER::PROGRAM::Linking Failed\n" << infoLog << std::endl; return -1; } // Free the old shader bits glDeleteShader(vertexShader); glDeleteShader(fragmentShader); /* ====================================================================== */ /* ====================================================================== */ // Define the vertex shader dat. f32 vertices[] = { 0.5f, 0.5f, 0.0f, // tr 0.5f, -0.5f, 0.0f, // br -0.5f, -0.5f, 0.0f, // bl -0.5f, 0.5f, 0.0f // tl }; u32 indicies[] { 0, 1, 3, // t1 2, 1, 3 // t2 }; u32 VBO; // Vertex Buffer Object refernce number glGenBuffers(1, &VBO); // ask the GPU for a reference for one buffer u32 VAO; // Vertex Array Object refernce number glGenVertexArrays(1, &VAO); // ask the GPU for a reference for one buffer u32 EBO; // glGenBuffers(1, &EBO); // ask the gpu for a reference for one buffer // Bind VAO, meaning we glBindVertexArray(VAO); // Tell the GPU subsequent calls to modify buffers apply to this buffer, // namely, the VBO buffer. glBindBuffer(GL_ARRAY_BUFFER, VBO); // copy in data to the VBO. I.e. give it a pointer to some data, tell it // how the entire buffer will be. sizeof(*[]) evaluates the full allocated // size of the data object. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // Next, tell the GPU how to use the GL_ELEMENT_ARRAY_BUfFER. // In this case, read from the 0th index, 3 elements at a time as // off of the indirected GL_ARRAY_BUFFER as floating point numbers. Do not // re-normalize the values (i.e., they're already normalized), and use a // stride of 3xf32 as we walk through the data in the original GL_ARRAY_BUFFER // Target the Atrib pointer in the VAO to the VBO inside it. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(f32), (void *)0); // For the VAO array (theoretically, but we have an array of 1), start // at the 0th element of the VAO array. glEnableVertexAttribArray(0); // Setup the indexing into the VBO data. The EBO is the indirection buffer // that tells the GPU where to look into it's known GL_ARRAY_BUFFER object // for data. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies), indicies, GL_STATIC_DRAW); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // ?? glBindVertexArray(0); // i.e., UNBIND the vertex array. while(!glfwWindowShouldClose(window)) { processInput(window); // Do some rendering glClearColor(0.0f, 0.1f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glUseProgram(shaderProgram); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // Finish up glfwSwapBuffers(window); glfwPollEvents(); } glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); glDeleteBuffers(1, &EBO); glDeleteProgram(shaderProgram); i32 xpos, ypos; glfwGetWindowPos(window, &xpos, &ypos); std::cout << "Closed window at position\n (X,Y) = " << xpos << ", " << ypos << "\n" << std::endl; glfwTerminate(); return 0; }