* Shader (Sahde + r)
- 음영을 만드는 주체
Graphic Pipeline
1. Vertex Shader
- The Vertex Shader takes the positions of all the vertices and transforms them- 점들을 찍는다* 기존의 정점을 지우거나 새로운 정점을 추가하는 등의 작업은 할 수 없다
2. Shape Assembler
- The Shape Assembler takes all the positions, and connects them according to a primitive
- primitive: shape such as a triangle, or maybe a point, or a line
- 주어진 primitive에 따라 점들을 잇는다
3. Geometry Shader
- The Geometry Shader can add vertices and then create new primitives out of already existing primitives
- 점이나 primitives를 추가 혹은 덧씌운다
4. Rasterization
- all the perfect mathematical shapes get transformed into actual pixels
- a perfect mathematical triangle, now becomes just a bunch of pixels
- 수학적으로 계산된 그래픽들을 기반으로 픽셀을 할당
5. Fragment Shader
- THe Fragment Shader adds colors to the pixels
- These depend on many things such as the lighting, or the textures, or shadows
- At this point you might have multiple colors for just one pixel because of multiple objects overlapping
- 픽셀들에 색 정보를 입력 ( 중첩 가능 )
6. Tests And Blending
- Blending of transparent objects into the final color
- 중첩된 색 정보들을 규칙에 따라 연산한 후, 최종 색 값을 픽셀에 입력
OpenGL
- OpenGL에서는 Vertex와 Fragment 셰이더를 기본 제공해주지 않는다
- OpenGL 자료형은 c++ 자료형과 약간 차이가 있기에 OpenGL에서 제공하는 자료형을 사용
ex) GLfloat
- OpenGL에서 객체들은 레퍼런스로만 접근 가능하다 (reference integer)
ex) GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
- Vertex Array Object: stores pointers to one or more VBOs and tells OpendGL how to interpret them
ㄴ exits in order to quickly be able to switch between different VBOs
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
3: how many values we have per vertex
Q. 점의 개수 or 좌표 개수(x,y,z)?
#include<iostream>
#include<glad/glad.h>
#include<GLFW/glfw3.h>
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(0.8f, 0.3f, 0.02f, 1.0f);\n"
"}\0";
int main()
{
glfwInit();
// GLFW 버전 명시
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// OPENGL 프로필 설정 (Core 프로필)
// Core 프로필: 최신 함수들만 사용
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLfloat vertices[] =
{
-0.5f, -0.5f * float(sqrt(3)) / 3, 0.0f,
0.5f, -0.5f * float(sqrt(3)) / 3, 0.0f,
0.0, 0.5f * float(sqrt(3)) * 2 / 3, 0.0f
};
// 창 이름이 "OpenGL" 인 800 x 800 GLFWwindow 객체 생성
GLFWwindow* window = glfwCreateWindow(800, 800, "OpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Introduce the window into the current context
glfwMakeContextCurrent(window);
// Load GLAD so it configures OpenGL
gladLoadGL();
// Specify the viewport of OpenGL in the window
// In this case the viewport goes from x = 0, y = 0, to x = 800, y = 800
glViewport(0, 0, 800, 800);
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
GLuint VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
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);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Specify the color of the background
glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
// Clean the back buffer and assign the new color to it
glClear(GL_COLOR_BUFFER_BIT);
// Swap the back buffer with the front buffer
glfwSwapBuffers(window);
// 윈도우 창 x 버튼 누를 경우에만 프로그램이 꺼지도록 설정
// Main while loop
while (!glfwWindowShouldClose(window))
{
glClearColor(0.07f, 0.13f, 0.17f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
// Take care of all GLFW events
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
// Delete window before ending the program
glfwDestroyWindow(window);
// Terminate GLFW before ending the program
glfwTerminate();
return 0;
}
'OpenGL' 카테고리의 다른 글
[OpenGL] Types of Light (0) | 2022.11.04 |
---|---|
[OpenGL] VBO / VAO (0) | 2022.10.14 |
[OpenGL] 1. 윈도우 창 생성 및 배경색 조절 (0) | 2022.10.07 |
[OpenGL] 설치 (0) | 2022.10.07 |