fix light

This commit is contained in:
barmal4 2022-01-07 21:46:21 +01:00
parent 2c9efa0ed8
commit 3f9711c675
13 changed files with 38 additions and 14 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,13 +1,20 @@
#version 410 core
uniform vec3 objectColor;
uniform vec3 lightDir;
uniform vec3 lightPos;
uniform vec3 cameraPos;
in vec3 interpNormal;
in vec3 fragPos;
void main()
{
vec3 lightDir = normalize(lightPos-fragPos);
vec3 V = normalize(cameraPos-fragPos);
vec3 normal = normalize(interpNormal);
float diffuse = max(dot(normal, -lightDir), 0.0);
gl_FragColor = vec4(objectColor * diffuse, 1.0);
vec3 R = reflect(-normalize(lightDir),normal);
float specular = pow(max(0,dot(R,V)),10);
float diffuse = max(0,dot(normal,normalize(lightDir)));
gl_FragColor = vec4(mix(objectColor,objectColor*diffuse+vec3(1)*specular,0.9), 1.0);
}

View File

@ -8,9 +8,11 @@ uniform mat4 modelViewProjectionMatrix;
uniform mat4 modelMatrix;
out vec3 interpNormal;
out vec3 fragPos;
void main()
{
gl_Position = modelViewProjectionMatrix * vec4(vertexPosition, 1.0);
interpNormal = (modelMatrix * vec4(vertexNormal, 0.0)).xyz;
fragPos = (modelMatrix*vec4(vertexPosition,1)).xyz;
}

View File

@ -1,16 +1,23 @@
#version 410 core
uniform sampler2D textureSampler;
uniform vec3 lightDir;
uniform vec3 lightPos;
uniform vec3 cameraPos;
in vec3 interpNormal;
in vec2 interpTexCoord;
in vec3 fragPos;
void main()
{
vec2 modifiedTexCoord = vec2(interpTexCoord.x, 1.0 - interpTexCoord.y); // Poprawka dla tekstur Ziemi, ktore bez tego wyswietlaja sie 'do gory nogami'
vec3 color = texture2D(textureSampler, modifiedTexCoord).rgb;
vec3 lightDir = normalize(lightPos-fragPos);
vec3 V = normalize(cameraPos-fragPos);
vec3 normal = normalize(interpNormal);
float diffuse = max(dot(normal, -lightDir), 0.0);
gl_FragColor = vec4(color * diffuse, 1.0);
vec3 R = reflect(-normalize(lightDir),normal);
float specular = pow(max(0,dot(R,V)),10);
float diffuse = max(0,dot(normal,normalize(lightDir)));
gl_FragColor = vec4(mix(color,color*diffuse+vec3(1)*specular,0.9), 1.0);
}

View File

@ -9,10 +9,12 @@ uniform mat4 modelMatrix;
out vec3 interpNormal;
out vec2 interpTexCoord;
out vec3 fragPos;
void main()
{
gl_Position = modelViewProjectionMatrix * vec4(vertexPosition, 1.0);
interpNormal = (modelMatrix * vec4(vertexNormal, 0.0)).xyz;
interpTexCoord = vertexTexCoord;
fragPos = (modelMatrix*vec4(vertexPosition,1)).xyz;
}

View File

@ -19,6 +19,7 @@ GLuint programTexture;
Core::Shader_Loader shaderLoader;
Core::RenderContext sharkModel;
Core::RenderContext sphereContext;
glm::vec3 cameraPos = glm::vec3(0, 0, 5);
glm::vec3 cameraDir; // Wektor "do przodu" kamery
@ -27,7 +28,7 @@ float cameraAngle = 0;
glm::mat4 cameraMatrix, perspectiveMatrix;
glm::vec3 lightDir = glm::normalize(glm::vec3(0.0f, -10.0f, 0.0f));
glm::vec3 lightDir = glm::vec3(0.0f, 100.0f, 0.0f);
glm::quat rotation = glm::quat(1, 0, 0, 0);
std::vector<glm::vec3> planetsCoords;
@ -82,7 +83,8 @@ void drawObjectColor(Core::RenderContext context, glm::mat4 modelMatrix, glm::ve
glUseProgram(program);
glUniform3f(glGetUniformLocation(program, "objectColor"), color.x, color.y, color.z);
glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z);
glUniform3f(glGetUniformLocation(program, "lightPos"), lightDir.x, lightDir.y, lightDir.z);
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "modelViewProjectionMatrix"), 1, GL_FALSE, (float*)&transformation);
@ -99,7 +101,8 @@ void drawObjectTexture(Core::RenderContext context, glm::mat4 modelMatrix, GLuin
glUseProgram(program);
glUniform3f(glGetUniformLocation(program, "lightDir"), lightDir.x, lightDir.y, lightDir.z);
glUniform3f(glGetUniformLocation(program, "lightPos"), lightDir.x, lightDir.y, lightDir.z);
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
Core::SetActiveTexture(textureId, "textureSampler", program, 0);
glm::mat4 transformation = perspectiveMatrix * cameraMatrix * modelMatrix;
@ -123,9 +126,11 @@ void renderScene()
glm::mat4 shipModelMatrix = glm::translate(cameraPos + cameraDir * 0.5f) * glm::mat4_cast(glm::inverse(rotation)) * shipInitialTransformation;
drawObjectTexture(sharkModel, shipModelMatrix, sharkTexture);
/*for (auto& coords : planetsCoords) {
for (auto& coords : planetsCoords) {
drawObjectColor(sphereContext, glm::translate(coords), coords);
}*/
}
drawObjectTexture(sharkModel, glm::mat4() * glm::scale(glm::vec3(0.25f)), sharkTexture);
glutSwapBuffers();
}
@ -150,11 +155,12 @@ void init()
programColor = shaderLoader.CreateProgram("shaders/shader_color.vert", "shaders/shader_color.frag");
programTexture = shaderLoader.CreateProgram("shaders/shader_tex.vert", "shaders/shader_tex.frag");
loadModelToContext("models/orca.obj", sharkModel);
loadModelToContext("models/sphere.obj", sphereContext);
sharkTexture = Core::LoadTexture("textures/Orca_Diffuse.jpg");
/*for (int i = 0; i < 10; i++) {
for (int i = 0; i < 10; i++) {
float r = (float)(rand()) / (float)(RAND_MAX/20.0);
planetsCoords.push_back(glm::ballRand(r));
}*/
}
}
void shutdown()