1. u_offset 유니폼을 vec2 타입으로 바꾸고, 동일한 결과가 나타나도록 코드를 수정해 보세요.
...
uniform vec2 u_offset;
void main()
{
gl_Position = position + vec4(u_offset, 0.0f, 0.0f);
}
...
...
int location = glGetUniformLocation(shader, "u_offset");
ASSERT(location != -1);
glUniform2f(location, 0.5f, 0.0f);
...
- vec4 와 vec2의 연산이 불가능하기에 u_offset을 vec4로 바꾼 후 연산을 진행한다.
2. 사각형이 좌우로 반복 운동하도록 코드를 수정해 보세요.
...
int frame_count = 0;
float offset = 0.0f;
float increment = 0.001f;
int direction = 1;
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
frame_count++;
offset += (increment * direction);
if (direction == 1 && offset >= 1.0f) direction = -1;
if (direction == -1 && offset <= -2.0f) direction = 1;
glUniform4f(location, offset, 0.0f, 0.0f, 0.0f);
...
- direction 변수를 통해 좌우 반복 운동이 가능하도록 수정함.
3. 사각형이 빨간색으로 그려지도록 프래그먼트 셰이더에 유니폼을 추가하고, 값을 전달해 보세요.
...
#shader fragment
#version 330 core
layout(location = 0) out vec4 color;
uniform vec4 u_Color;
void main()
{
color = u_Color;
}
...
int location = glGetUniformLocation(shader, "u_Color");
ASSERT(location != -1);
glUniform4f(location, 5.0f, 0.0f, 0.0f, 0.0f);
...