From 1ddfb9865a985ebc140fb92467bf67cceca7683e Mon Sep 17 00:00:00 2001 From: realtradam Date: Sun, 20 Nov 2022 01:03:31 -0500 Subject: drawing rectangles --- src/main.cpp | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index bdd92d5..82e7fa0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,16 +24,16 @@ void main() } )"; -//float vertices[] = { -// 0.5f, 0.5f, 0.0f, // top right -// 0.5f, -0.5f, 0.0f, // bottom right -// -0.5f, -0.5f, 0.0f, // bottom left -// -0.5f, 0.5f, 0.0f // top left -//}; -//unsigned int indices[] = { // note that we start from 0! -// 0, 1, 3, // first triangle -// 1, 2, 3 // second triangle -//}; +float vertices[] = { + 0.5f, 0.5f, 0.0f, // top right + 0.5f, -0.5f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, // bottom left + -0.5f, 0.5f, 0.0f // top left +}; +unsigned int indices[] = { // note that we start from 0! + 0, 1, 3, // first triangle + 1, 2, 3 // second triangle +}; void framebuffer_size_callback(GLFWwindow* window, int width, int height) { @@ -65,14 +65,6 @@ int main() { glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); - // Vertex Input - float vertices[] = { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.0f, 0.5f, 0.0f - }; - - unsigned int vertexShader; vertexShader = glCreateShader(GL_VERTEX_SHADER); // create a blank shader glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); @@ -122,16 +114,23 @@ int main() { glDeleteShader(vertexShader); // we dont need these anymore once they are linked in the shader program glDeleteShader(fragmentShader); - unsigned int VBO, VAO; + unsigned int VBO, VAO, EBO; glGenBuffers(1, &VBO); // generate the buffer + glGenBuffers(1, &EBO); + glGenVertexArrays(1, &VAO); // generate array glBindVertexArray(VAO); // bind array to apply "settings" to it + glBindBuffer(GL_ARRAY_BUFFER, VBO); // bind(activate) the buffer to the ARRAY_BUFFER glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // upload the data + // + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); // configure how the vertex array looks glEnableVertexAttribArray(0); + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // game loop while(!glfwWindowShouldClose(window)) @@ -144,7 +143,8 @@ int main() { glUseProgram(shaderProgram); // this will activate the shader and use them for all subsequent shader and render calls glBindVertexArray(VAO); // activate the preconfigured settings - glDrawArrays(GL_TRIANGLES, 0, 3); // :) + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // :) + glBindVertexArray(0); glfwSwapBuffers(window); glfwPollEvents(); -- cgit v1.2.3