diff --git a/mybox.cpp b/mybox.cpp index 4ab5644..357c7e4 100644 --- a/mybox.cpp +++ b/mybox.cpp @@ -24,6 +24,8 @@ int main(void) { // 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(); @@ -40,12 +42,15 @@ int main(void) { return -1; } + /* ====================================================================== */ + /* ====================================================================== */ + /* ====================================================================== */ i32 success; char infoLog[512]; // Create the vertex shader GLchar *shdr_src_vertex; - readshader("vertex.glsl", &shdr_src_vertex); + readshader("vertex.vert", &shdr_src_vertex); u32 vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &shdr_src_vertex, NULL); glCompileShader(vertexShader); @@ -61,7 +66,7 @@ int main(void) { // Create the fragment shader GLchar *shdr_src_fragment; - readshader("fragment.glsl", &shdr_src_fragment); + readshader("fragment.frag", &shdr_src_fragment); u32 fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &shdr_src_fragment, NULL); glCompileShader(fragmentShader); @@ -92,6 +97,9 @@ int main(void) { glDeleteShader(vertexShader); glDeleteShader(fragmentShader); + /* ====================================================================== */ + /* ====================================================================== */ + // Define the vertex shader dat. f32 vertices[] = { 0.5f, 0.5f, 0.0f, // tr @@ -101,32 +109,47 @@ int main(void) { }; u32 indicies[] { 0, 1, 3, // t1 - 1, 2, 3 // t2 + 2, 1, 3 // t2 }; - - u32 EBO; - u32 VBO; - u32 VAO; - glGenBuffers(1, &EBO); - glGenBuffers(1, &VBO); - glGenVertexArrays(1, &VAO); + 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 - glBindVertexArray(VAO); - // copy in data to the VBO + // 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); - // Setup the indexing into the VBO data - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies), indicies, 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); - // STart at Zero + // For the VAO array (theoretically, but we have an array of 1), start + // at the 0th element of the VAO array. glEnableVertexAttribArray(0); - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + // 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); @@ -136,7 +159,6 @@ int main(void) { glUseProgram(shaderProgram); glBindVertexArray(VAO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // Finish up @@ -144,6 +166,16 @@ int main(void) { 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; } \ No newline at end of file diff --git a/res/shaders/fragment.glsl b/res/shaders/fragment.frag similarity index 100% rename from res/shaders/fragment.glsl rename to res/shaders/fragment.frag diff --git a/res/shaders/vertex.glsl b/res/shaders/vertex.glsl deleted file mode 100644 index 667fc4c..0000000 --- a/res/shaders/vertex.glsl +++ /dev/null @@ -1,6 +0,0 @@ -#version 420 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/res/shaders/vertex.vert b/res/shaders/vertex.vert new file mode 100644 index 0000000..a2a38a0 --- /dev/null +++ b/res/shaders/vertex.vert @@ -0,0 +1,10 @@ +#version 420 core +layout (location = 0) in vec3 aPos; +//layout (location = 0) in vec2 aPos; +//layout (location = 0) in float aPosx; +//layout (location = 1) in float aPosy; +//layout (location = 2) in float aPosz; + +void main() { + gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); +} \ No newline at end of file diff --git a/src/lgl_callbacks.cpp b/src/lgl_callbacks.cpp index d0639b8..7db7e6c 100644 --- a/src/lgl_callbacks.cpp +++ b/src/lgl_callbacks.cpp @@ -3,7 +3,7 @@ #include "lgl_callbacks.h" void framebuffer_size_callback( - GLFWwindow* window, GLint width, GLint height) { + GLFWwindow* window, GLsizei width, GLsizei height) { /* GLint nwidth = width - 60; diff --git a/src/lgl_shader_tools.cpp b/src/lgl_shader_tools.cpp index 4396081..631adc5 100644 --- a/src/lgl_shader_tools.cpp +++ b/src/lgl_shader_tools.cpp @@ -9,6 +9,9 @@ #define CONST_READSHADER_MAX_FN_LEN 256 #define CONST_READSHADER_MAX_FN_SIZE 32*1024 + +//#define LPR__VERBOSE_SHADER + int readshader(const char *fn, GLchar** pshdr_source) { char shader_fn[CONST_READSHADER_MAX_FN_LEN]; assert(strlen(fn) + 9 < CONST_READSHADER_MAX_FN_LEN); @@ -18,12 +21,15 @@ int readshader(const char *fn, GLchar** pshdr_source) { snprintf(shader_fn, CONST_READSHADER_MAX_FN_LEN, "res/shaders/%s", fn); - printf("%s\n", shader_fn); + #ifdef LPR__VERBOSE_SHADER + printf("Load: '%s'\n", shader_fn); + #endif - if (stat(shader_fn, &status_buffer) == -1) { + if (stat(shader_fn, &status_buffer) != 0) { printf("Failed to stat file '%s'\n", shader_fn); - return 0; + return -1; } + assert(status_buffer.st_size < CONST_READSHADER_MAX_FN_SIZE-1); FILE *fh = fopen(shader_fn, "r"); @@ -37,12 +43,13 @@ int readshader(const char *fn, GLchar** pshdr_source) { assert(read_bytes < CONST_READSHADER_MAX_FN_SIZE-1); shdr_source[read_bytes] = '\0'; + #ifdef LPR__VERBOSE_SHADER puts(shdr_source); + #endif fclose(fh); assert(shdr_source != NULL); - - + *pshdr_source = shdr_source; return 0;