Skip to content

Commit

Permalink
feat: added lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
mohlek committed Sep 15, 2021
1 parent e9279e2 commit f2ed644
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
2 changes: 1 addition & 1 deletion engine
Submodule engine updated 1 files
+3 −0 src/Window.cpp
7 changes: 3 additions & 4 deletions include/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,15 @@ namespace Shader {
vec3 color = texture(diffuseMap, fs_in.uv).rgb;
vec3 ambient = 0.1 * color;
vec3 lightDir = fs_in.tangentLightDir;
float diff = dot(lightDir, normal);
diff = sign(diff) * diff;
float diff = max(dot(lightDir, normal), 0.0);
vec3 diffuse = diff * color;
vec3 viewDir = normalize(fs_in.tangentViewPos - fs_in.tangentFragPos);
vec3 reflectDir = reflect(-lightDir, normal);
vec3 halfwayDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0);
vec3 specular = vec3(0.2) * spec;
fragColor = vec4(isnan(lightDir.x), isnan(lightDir.y), isnan(lightDir.z), 1.0); // + specular, 1.0);
fragColor = vec4(ambient + diffuse + specular, 1.0); // + specular, 1.0);
}
)";

Expand Down Expand Up @@ -186,7 +185,7 @@ namespace Shader {
out vec4 fragColor;
void main() {
fragColor = texture(groundTexture, texCoord*10);
fragColor = texture(groundTexture, texCoord*500);
}
)";
}
Expand Down
51 changes: 32 additions & 19 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct Clock
};

int main(int argc, char** argv) {
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
// feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
Engine::Window* win = new Engine::Window("Medieval something", 800, 600);

if (!win->create()) {
Expand Down Expand Up @@ -143,7 +143,7 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
i + 0 - (i % 3), // i1
i + 1 - (i % 3), // i2
i + 2 - (i % 3) // i3
}
};

/*
example:
Expand Down Expand Up @@ -174,9 +174,9 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
glm::vec2 deltaUV1 = uv2 - uv1;
glm::vec2 deltaUV2 = uv3 - uv1;

constexpr auto epsilon = std::numeric_limits<T>::epsilon();
float fd = (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y)
float f = 1.0f / (fd + std::copysign(epsilon, fd); // add epsilon .. fixed division by zero problem..
constexpr auto epsilon = std::numeric_limits<float>::epsilon();
float fd = (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);
float f = 1.0f / (fd + std::copysign(epsilon, fd)); // add epsilon .. fixed division by zero problem..

glm::vec3 t;
t.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x);
Expand Down Expand Up @@ -242,8 +242,8 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);

glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)win->getWidth() / (float)win->getHeight(), 0.1f, 10000.0f);
glm::mat4 modelHouse = glm::mat4(1.0f);
modelHouse = glm::scale(modelHouse, glm::vec3(0.5));
modelHouse = glm::translate(modelHouse, glm::vec3(5000, 800, 8065));
modelHouse = glm::scale(modelHouse, glm::vec3(0.01));
modelHouse = glm::translate(modelHouse, glm::vec3(50000, 23450, 80065));

GLint uniformModelHouse = program.getUniformLocation("model");
GLint uniformViewHouse = program.getUniformLocation("view");
Expand Down Expand Up @@ -303,6 +303,11 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);

// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

//SDL_ShowCursor(false);
SDL_SetRelativeMouseMode(SDL_TRUE);
float yaw, pitch;
float lastX = 400, lastY = 300;

glClearColor(1.0, 1.0, 1.0, 1.0);
do {
SDL_Event event;
Expand All @@ -323,11 +328,25 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
}

int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
SDL_GetRelativeMouseState(&mouseX, &mouseY);

const float sensitivity = 0.05f;
mouseX *= sensitivity;
mouseY *= sensitivity;

yaw += mouseX;
pitch += -mouseY;

// fprintf(stdout, "%d %d\n", mouseX, mouseY);
if(pitch > 89.0f)
pitch = 89.0f;
if(pitch < -89.0f)
pitch = -89.0f;

float cameraSpeed = (float)(clock.delta / 1.0f);
camera.direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
camera.direction.y = sin(glm::radians(pitch));
camera.direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));

float cameraSpeed = (float)(clock.delta / 10.0f);
if (KEYS[SDLK_w]) {
camera.position += camera.direction * cameraSpeed;
}
Expand Down Expand Up @@ -356,6 +375,9 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
terrain.setFactor(factor);
fprintf(stdout, "factor %f\n", factor);
}
if (KEYS[SDLK_q]) {
win->close();
}

clock.tick();

Expand All @@ -367,22 +389,13 @@ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
glUniformMatrix4fv(uniformViewTerrain, 1, GL_FALSE, glm::value_ptr(viewTerrain));
terrain.render();


program.use();
vao.bind();
textureDiffHouse.bind();
textureNormalHouse.bind();
glUniform1ui(uniformTime, SDL_GetTicks());
glUniformMatrix4fv(uniformViewHouse, 1, GL_FALSE, glm::value_ptr(viewTerrain));
glDrawElements(GL_TRIANGLES, faceIndex.size(), GL_UNSIGNED_INT, 0);

displayNormalShader.use();
vao.bind();
textureDiffHouse.bind();
textureNormalHouse.bind();
glUniformMatrix4fv(uniformViewDisplayNormal, 1, GL_FALSE, glm::value_ptr(viewTerrain));
glDrawElements(GL_TRIANGLES, faceIndex.size(), GL_UNSIGNED_INT, 0);

} while (win->loop());

return 0;
Expand Down

0 comments on commit f2ed644

Please sign in to comment.