diff options
author | crupest <crupest@outlook.com> | 2021-12-04 20:57:26 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-12-04 20:57:26 +0800 |
commit | 039b80b24ce71c1998b15f7cc12f7da3a9c3edd2 (patch) | |
tree | a10f6a001a80de9fdf21ab93ec7e37967a6d05b9 /works/life | |
parent | 44c58b206b910b15819a738c4a9995f8f840c039 (diff) | |
download | crupest-039b80b24ce71c1998b15f7cc12f7da3a9c3edd2.tar.gz crupest-039b80b24ce71c1998b15f7cc12f7da3a9c3edd2.tar.bz2 crupest-039b80b24ce71c1998b15f7cc12f7da3a9c3edd2.zip |
import(life): ...
Diffstat (limited to 'works/life')
-rw-r--r-- | works/life/computer-graphics-experiment/.gitignore | 2 | ||||
-rw-r--r-- | works/life/computer-graphics-experiment/3.cpp | 144 | ||||
-rw-r--r-- | works/life/computer-graphics-experiment/4.cpp | 192 | ||||
-rw-r--r-- | works/life/computer-graphics-experiment/CMakeLists.txt | 14 |
4 files changed, 352 insertions, 0 deletions
diff --git a/works/life/computer-graphics-experiment/.gitignore b/works/life/computer-graphics-experiment/.gitignore new file mode 100644 index 0000000..7194ea7 --- /dev/null +++ b/works/life/computer-graphics-experiment/.gitignore @@ -0,0 +1,2 @@ +.cache +build diff --git a/works/life/computer-graphics-experiment/3.cpp b/works/life/computer-graphics-experiment/3.cpp new file mode 100644 index 0000000..2526d5d --- /dev/null +++ b/works/life/computer-graphics-experiment/3.cpp @@ -0,0 +1,144 @@ +// Code referred from +// https://www.geeksforgeeks.org/line-clipping-set-1-cohen-sutherland-algorithm/ + +// C++ program to implement Cohen Sutherland algorithm +// for line clipping. +#include <iostream> +using namespace std; + +// Defining region codes +const int INSIDE = 0; // 0000 +const int LEFT = 1; // 0001 +const int RIGHT = 2; // 0010 +const int BOTTOM = 4; // 0100 +const int TOP = 8; // 1000 + +// Defining x_max, y_max and x_min, y_min for +// clipping rectangle. Since diagonal points are +// enough to define a rectangle +const int x_max = 10; +const int y_max = 8; +const int x_min = 4; +const int y_min = 4; + +// Function to compute region code for a point(x, y) +int computeCode(double x, double y) +{ + // initialized as being inside + int code = INSIDE; + + if (x < x_min) // to the left of rectangle + code |= LEFT; + else if (x > x_max) // to the right of rectangle + code |= RIGHT; + if (y < y_min) // below the rectangle + code |= BOTTOM; + else if (y > y_max) // above the rectangle + code |= TOP; + + return code; +} + +// Implementing Cohen-Sutherland algorithm +// Clipping a line from P1 = (x2, y2) to P2 = (x2, y2) +void cohenSutherlandClip(double x1, double y1, + double x2, double y2) +{ + // Compute region codes for P1, P2 + int code1 = computeCode(x1, y1); + int code2 = computeCode(x2, y2); + + // Initialize line as outside the rectangular window + bool accept = false; + + while (true) { + if ((code1 == 0) && (code2 == 0)) { + // If both endpoints lie within rectangle + accept = true; + break; + } + else if (code1 & code2) { + // If both endpoints are outside rectangle, + // in same region + break; + } + else { + // Some segment of line lies within the + // rectangle + int code_out; + double x, y; + + // At least one endpoint is outside the + // rectangle, pick it. + if (code1 != 0) + code_out = code1; + else + code_out = code2; + + // Find intersection point; + // using formulas y = y1 + slope * (x - x1), + // x = x1 + (1 / slope) * (y - y1) + if (code_out & TOP) { + // point is above the clip rectangle + x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1); + y = y_max; + } + else if (code_out & BOTTOM) { + // point is below the rectangle + x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1); + y = y_min; + } + else if (code_out & RIGHT) { + // point is to the right of rectangle + y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1); + x = x_max; + } + else if (code_out & LEFT) { + // point is to the left of rectangle + y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1); + x = x_min; + } + + // Now intersection point x, y is found + // We replace point outside rectangle + // by intersection point + if (code_out == code1) { + x1 = x; + y1 = y; + code1 = computeCode(x1, y1); + } + else { + x2 = x; + y2 = y; + code2 = computeCode(x2, y2); + } + } + } + if (accept) { + cout << "Line accepted from " << x1 << ", " + << y1 << " to " << x2 << ", " << y2 << endl; + // Here the user can add code to display the rectangle + // along with the accepted (portion of) lines + } + else + cout << "Line rejected" << endl; +} + +// Driver code +int main() +{ + // First Line segment + // P11 = (5, 5), P12 = (7, 7) + cohenSutherlandClip(5, 5, 7, 7); + + // Second Line segment + // P21 = (7, 9), P22 = (11, 4) + cohenSutherlandClip(7, 9, 11, 4); + + // Third Line segment + // P31 = (1, 5), P32 = (4, 1) + cohenSutherlandClip(1, 5, 4, 1); + + return 0; +} + diff --git a/works/life/computer-graphics-experiment/4.cpp b/works/life/computer-graphics-experiment/4.cpp new file mode 100644 index 0000000..9852844 --- /dev/null +++ b/works/life/computer-graphics-experiment/4.cpp @@ -0,0 +1,192 @@ +// Code from https://learnopengl.com/Getting-started/Hello-Triangle + +#include <glad/glad.h> + +#include <GLFW/glfw3.h> + +#include <iostream> + +void framebuffer_size_callback(GLFWwindow *window, int width, int height); +void processInput(GLFWwindow *window); + +// settings +const unsigned int SCR_WIDTH = 800; +const unsigned int SCR_HEIGHT = 600; + +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 *fragmentShaderSource = + "#version 330 core\n" + "out vec4 FragColor;\n" + "void main()\n" + "{\n" + " FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" + "}\n\0"; + +int main() { + // glfw: initialize and configure + // ------------------------------ + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + +#ifdef __APPLE__ + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#endif + + // glfw window creation + // -------------------- + GLFWwindow *window = + glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); + if (window == NULL) { + std::cout << "Failed to create GLFW window" << std::endl; + glfwTerminate(); + return -1; + } + glfwMakeContextCurrent(window); + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + + // glad: load all OpenGL function pointers + // --------------------------------------- + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { + std::cout << "Failed to initialize GLAD" << std::endl; + return -1; + } + + // build and compile our shader program + // ------------------------------------ + // vertex shader + unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); + glCompileShader(vertexShader); + // check for shader compile errors + int success; + char infoLog[512]; + glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); + std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" + << infoLog << std::endl; + } + // fragment shader + unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); + glCompileShader(fragmentShader); + // check for shader compile errors + glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); + std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" + << infoLog << std::endl; + } + // link shaders + unsigned int shaderProgram = glCreateProgram(); + glAttachShader(shaderProgram, vertexShader); + glAttachShader(shaderProgram, fragmentShader); + glLinkProgram(shaderProgram); + // check for linking errors + glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); + if (!success) { + glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); + std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" + << infoLog << std::endl; + } + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); + + // set up vertex data (and buffer(s)) and configure vertex attributes + // ------------------------------------------------------------------ + float vertices[] = { + -0.5f, -0.5f, 0.0f, // left + 0.5f, -0.5f, 0.0f, // right + 0.0f, 0.5f, 0.0f // top + }; + + unsigned int VBO, VAO; + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + // bind the Vertex Array Object first, then bind and set vertex buffer(s), and + // then configure vertex attributes(s). + 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); + + // note that this is allowed, the call to glVertexAttribPointer registered VBO + // as the vertex attribute's bound vertex buffer object so afterwards we can + // safely unbind + glBindBuffer(GL_ARRAY_BUFFER, 0); + + // You can unbind the VAO afterwards so other VAO calls won't accidentally + // modify this VAO, but this rarely happens. Modifying other VAOs requires a + // call to glBindVertexArray anyways so we generally don't unbind VAOs (nor + // VBOs) when it's not directly necessary. + glBindVertexArray(0); + + // uncomment this call to draw in wireframe polygons. + // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + + // render loop + // ----------- + while (!glfwWindowShouldClose(window)) { + // input + // ----- + processInput(window); + + // render + // ------ + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + // draw our first triangle + glUseProgram(shaderProgram); + glBindVertexArray( + VAO); // seeing as we only have a single VAO there's no need to bind it + // every time, but we'll do so to keep things a bit more organized + glDrawArrays(GL_TRIANGLES, 0, 3); + // glBindVertexArray(0); // no need to unbind it every time + + // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved + // etc.) + // ------------------------------------------------------------------------------- + glfwSwapBuffers(window); + glfwPollEvents(); + } + + // optional: de-allocate all resources once they've outlived their purpose: + // ------------------------------------------------------------------------ + glDeleteVertexArrays(1, &VAO); + glDeleteBuffers(1, &VBO); + glDeleteProgram(shaderProgram); + + // glfw: terminate, clearing all previously allocated GLFW resources. + // ------------------------------------------------------------------ + glfwTerminate(); + return 0; +} + +// process all input: query GLFW whether relevant keys are pressed/released this +// frame and react accordingly +// --------------------------------------------------------------------------------------------------------- +void processInput(GLFWwindow *window) { + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) + glfwSetWindowShouldClose(window, true); +} + +// glfw: whenever the window size changed (by OS or user resize) this callback +// function executes +// --------------------------------------------------------------------------------------------- +void framebuffer_size_callback(GLFWwindow *window, int width, int height) { + // make sure the viewport matches the new window dimensions; note that width + // and height will be significantly larger than specified on retina displays. + glViewport(0, 0, width, height); +}
\ No newline at end of file diff --git a/works/life/computer-graphics-experiment/CMakeLists.txt b/works/life/computer-graphics-experiment/CMakeLists.txt new file mode 100644 index 0000000..b9557d4 --- /dev/null +++ b/works/life/computer-graphics-experiment/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.14) + +set(CMAKE_TOOLCHAIN_FILE $ENV{VCPKG_INSTALLATION_ROOT}/scripts/buildsystems/vcpkg.cmake + CACHE STRING "Vcpkg toolchain file") + +set(CMAKE_CXX_STANDARD 20) + +project(computer-graphics-experiment) + +find_package(glad CONFIG REQUIRED) +find_package(glfw3 CONFIG REQUIRED) + +add_executable(4 4.cpp) +target_link_libraries(4 PRIVATE glad::glad glfw) |