지금 2가지 문제가 있는데...
첫번째로는 태양에서 빛이 안나요... 위에서 바라봤을 때에는 태양쪽에서 빛이 나고 있는거 처럼 그림자가 잡히기는 하는데
실제로 태양은 어둡네요ㅠㅠ
// init light properties
light.position = vec4(0.0f, 0.0f, 0.0f, 1.0f);
light.ambient = vec4(0.2f, 0.2f, 0.2f, 1.0f);
light.diffuse = vec4(1.8f, 0.8f, 0.8f, 1.0f);
light.specular = vec4(1.0f, 1.0f, 1.0f, 1.0f);
// init material properties
material.ambient = vec4(0.2f, 0.2f, 0.2f, 1.0f);
material.diffuse = vec4(0.8f, 0.8f, 0.8f, 1.0f);
material.specular = vec4(1.0f, 1.0f, 1.0f, 1.0f);
material.shininess = 10.0f;
태양의 좌표가 0, 0, 0이라 light 위치를 위에처럼 잡았는데...
두번째는 위에서 볼때는 태양 반대쪽에 그림자가 제대로 생기는데, 옆에서 보면 마치 광원이 제 쪽에 있는거처럼 그림자가 생기네요... normal 벡터를 잘못잡은건지...
Vertex Shader 는 이거구요.
#version 120
attribute vec3 position; // vertex input attribute
attribute vec3 normal;
attribute vec4 color; // vertex input attribute
varying vec4 vertexColor; // second output of vertex shader = input to fragment shader
varying vec3 norm; // per-vertex normal before interpolation
varying vec4 ecPos; // eye-coordinate position
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
// light properties
uniform vec4 lightPosition, Ia, Id, Is;
// material properties
uniform vec4 Ka, Kd, Ks;
uniform float shininess;
void main()
{
mat4 modelViewMatrix = viewMatrix*modelMatrix;
ecPos = modelViewMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * ecPos;
norm = normalize(mat3(modelViewMatrix)*normal); // per-vertex normal passed to the rasterizer
vertexColor = color;
}
fragment shader는 이거구요.
// input from the rasterizer
varying vec4 ecPos;
varying vec3 norm;
// light properties
uniform vec4 lightPosition, Ia, Id, Is;
// material properties
uniform vec4 Ka, Kd, Ks;
uniform float shininess;
uniform mat4 viewMatrix;
// the second input from vertex shader
varying vec4 vertexColor;
// shader's global variables, called the uniform variables
uniform bool bUseAnotherColor;
uniform vec4 anotherColor;
void main()
{
vec4 lPos = viewMatrix * lightPosition;
vec3 n = normalize(norm);
vec3 p = ecPos.xyz;
vec3 l = normalize(lPos.xyz-(lPos.a==0.0?vec3(0):p));
vec3 v = normalize(-p);
vec3 h = normalize(l+v);
vec4 Ira = Ka*Ia; // ambient reflection
vec4 Ird = max(Kd*dot(l,n)*Id, 0.0); // diffuse reflection
vec4 Irs = max(Ks*pow(dot(h,n),shininess)*Is, 0.0); // specular reflection
gl_FragColor = (Ira + Ird + Irs)*vertexColor;
}
구만들고 normal 벡터 잡는건 이렇게 생겼습니다.
void makeSphere(){
float radius = 1.0f;
vertex radiusVertices[triCount];
float rTheta = (180 / (float)triCount) * PI / 180;
for (int i = 0; i < triCount; i++){
float px = (cosf(rTheta * i) * 0) - (sinf(rTheta * i) * radius);
float py = (sinf(rTheta * i) * 0) + (cosf(rTheta * i) * radius);
radiusVertices[i].pos = vec3(-px, py, 0);
radiusVertices[i].color = vec4(1, 1, 0, 1);
}
vertex vertices[triCount][triCount];
GLuint Indices[((triCount - 1) * 2)*triCount];
float theta = (360 / (float)triCount) * PI / 180;
float x = radiusVertices[0].pos[0];
float y = radiusVertices[0].pos[1];
float z = 0;
int index_counter = 0;
for (int i = 0; i < triCount; i++){
x = radiusVertices[i].pos[0];
y = radiusVertices[i].pos[1];
for (int j = 0; j < triCount; j++){
float x2 = (cosf(theta * j) * x) - (sinf(theta * j) * 0);
float z2 = (sinf(theta * j) * x) + (cosf(theta * j) * 0);
vertices[i][j].pos = vec3(x2, y, z2);
vertices[i][j].color = vec4(1, 1, 0, 1);
vertices[i][j].norm = vec3(x2, y, z2).normalize();
vertices[i][j].index = index_counter++;
}
}
int k = 0;
for (int i = 0; i < triCount - 1; i++){
for (int j = 0; j < triCount; j++){
Indices[k++] = vertices[i][j].index;
Indices[k++] = vertices[i + 1][j].index;
}
}
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// geneation of index buffer
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
}