[PA 1] 삼각형 Rotation + Color Change

Update:     Updated:

Category:

Tags:



Task Lists

Practice how to use OpenGL basic gl* functions

  1. Rotate your triangle with respect to time [4 Points]
  2. Change your triangle color with respect to time [4 Points]

그래픽스 첫번째 PA는 빙글빙글 돌아가는 삼각형 color change + rotation 구현하기 !



Result Image

image



Code Implementation

GitHub Link : programming-assignment-1-binnie723

main.cpp 파일

#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <cmath>  // sin, cos, abs 연산을 위해 추가

// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);

// Window dimensions
const GLuint WIDTH = 1280, HEIGHT = 720;

// The MAIN function, from here we start the application and run the game loop
int main()
{
    std::cout << "Starting GLFW context, OpenGL 3.1" << std::endl;
    // Init GLFW
    glfwInit();
    // Set all the required options for GLFW
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    // Create a GLFWwindow object that we can use for GLFW's functions
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "glskeleton", NULL, NULL);
    glfwMakeContextCurrent(window);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

    // Set the required callback functions
    glfwSetKeyCallback(window, key_callback);

    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
    {
        std::cout << "Failed to initialize OpenGL context" << std::endl;
        return -1;
    }

    // Define the viewport dimensions
    glViewport(0, 0, WIDTH, HEIGHT);

    // Render loop
    while (!glfwWindowShouldClose(window))
    {
        // Check if any events have been activated (key pressed, mouse moved etc.) and call corresponding response functions
        glfwPollEvents();
        double now = glfwGetTime();  // 실시간 변환을 위해서 현재 타임 모듈 불러오기 

        // Render
        // Clear the colorbuffer
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_TRIANGLES);
				// 삼각형 색깔 변화시키는 코드 (픽셀 값은 -가 될 수 없기 때문에 절댓값으로 처리)
        glColor3f(1.8f*abs(cos(now))+0.5, 0.1f*abs(cos(now))+0.5, 0.3f*abs(cos(now))+0.5);
				// 삼각형 회전시키는 코드 (세타를 불러온 현재 시간으로 설정 -> 계속 회전하도록 함)
        glVertex3f(-0.5f*cos(now)-(-0.5f)*sin(now), sin(now)*(-0.5f)+cos(now)*(-0.5f), 1);
        glVertex3f(0.5f*cos(now)-(-0.5f)*sin(now), sin(now)*(0.5f)+cos(now)*(-0.5f), 1);
        glVertex3f(0*cos(now)-(0.5f)*sin(now), sin(now)*(0)+cos(now)*(0.5f), 1);
        glEnd();

        // Swap the screen buffers
        glfwSwapBuffers(window);
    }

    // Terminates GLFW, clearing any resources allocated by GLFW.
    glfwTerminate();
    return 0;
}

// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
  if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
    glfwSetWindowShouldClose(window, GL_TRUE);
}






맨 위로 이동하기