completed lab 6
This commit is contained in:
parent
e2e44be86a
commit
e959f7c792
@ -72,5 +72,8 @@ void main()
|
||||
float intensity = clamp((angleCos - cos(reflectorOutAngle)) / epsilon, 0.0, 1.0);
|
||||
outColor += calcSpotLight(textureColor, reflectorPos, reflectorColor, reflectorLightDir, reflectorLightExp * intensity);
|
||||
}
|
||||
|
||||
//Debug
|
||||
//outColor = vec4(textureColor, 1);
|
||||
}
|
||||
|
||||
|
@ -71,4 +71,4 @@ void main()
|
||||
outColor += calcSpotLight(textureColor, reflectorPos, reflectorColor, reflectorDir,
|
||||
reflectorAngle, reflectorAngle + radians(10), reflectorLightExp);
|
||||
|
||||
}
|
||||
}
|
@ -18,5 +18,5 @@ void main()
|
||||
vertexNormalOut = worldNormal.xyz;
|
||||
vertexPosOut = (modelMat * vec4(vertexPosition, 1.0)).xyz;
|
||||
vertexTexCoordOut = vertexTexCoord;
|
||||
}
|
||||
|
||||
vertexTexCoordOut.y = 1 - vertexTexCoord.y;// corrects inversion (bottom at top) of the earth
|
||||
}
|
86
cw 6/shaders/shader_proc_tex.frag
Normal file
86
cw 6/shaders/shader_proc_tex.frag
Normal file
@ -0,0 +1,86 @@
|
||||
#version 430 core
|
||||
|
||||
uniform sampler2D colorTexture;
|
||||
uniform sampler2D rust;
|
||||
uniform sampler2D scratches;
|
||||
|
||||
uniform vec3 sunPos;
|
||||
uniform vec3 sunColor;
|
||||
uniform float sunLightExp;
|
||||
|
||||
uniform vec3 cameraPos;
|
||||
uniform float time;
|
||||
|
||||
uniform vec3 reflectorPos;
|
||||
uniform vec3 reflectorDir;
|
||||
uniform vec3 reflectorColor;
|
||||
uniform float reflectorAngle;
|
||||
uniform float reflectorLightExp;
|
||||
|
||||
vec3 normalizedVertexNormal;
|
||||
|
||||
in vec3 vertexNormalOut;
|
||||
in vec3 vertexPosOut;
|
||||
in vec2 vertexTexCoordOut;
|
||||
in vec3 vertexLocPos;
|
||||
|
||||
out vec4 outColor;
|
||||
|
||||
vec4 calcPointLight(vec3 fragColor, vec3 lightPos, vec3 lightColor, float lightExp) {
|
||||
vec3 lightDir = normalize(vertexPosOut - lightPos);
|
||||
float lightDistance = length(vertexPosOut - lightPos);
|
||||
vec3 newLightColor = lightColor / pow(lightDistance, 2);
|
||||
|
||||
float intensity = dot(normalizedVertexNormal, -lightDir);
|
||||
intensity = max(intensity, 0.0);
|
||||
|
||||
vec3 viewDir = normalize(cameraPos - vertexPosOut);
|
||||
vec3 reflectDir = reflect(lightDir, normalizedVertexNormal);
|
||||
|
||||
float glossPow = 8;
|
||||
float specular = pow(max(dot(viewDir, reflectDir), 0.0), glossPow);
|
||||
|
||||
float diffuse = intensity;
|
||||
vec3 resultColor = newLightColor * (fragColor * diffuse + specular );
|
||||
return vec4(1 - exp(-resultColor * lightExp), 1.0);
|
||||
}
|
||||
|
||||
vec4 calcSpotLight(vec3 fragColor, vec3 lightPos, vec3 lightColor, vec3 lightDir, float lightExp) {
|
||||
vec3 reflectorLightDir = normalize(vertexPosOut - reflectorPos);
|
||||
float angleCos = dot(reflectorLightDir, reflectorDir);
|
||||
float reflectorOutAngle = reflectorAngle + radians(10);
|
||||
float epsilon = cos(reflectorAngle) - cos(reflectorOutAngle);
|
||||
vec4 res = vec4(0, 0, 0, 1);
|
||||
if (angleCos > cos(reflectorOutAngle)) {
|
||||
float intensity = clamp((angleCos - cos(reflectorOutAngle)) / epsilon, 0.0, 1.0);
|
||||
res = calcPointLight(fragColor, reflectorPos, reflectorColor, reflectorLightExp * intensity);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 shipColor = texture2D(colorTexture, vertexTexCoordOut).rgb;
|
||||
vec3 rustColor = texture2D(rust, vertexTexCoordOut).rgb;
|
||||
vec3 scratchesColor = texture2D(scratches, vertexTexCoordOut).rgb;
|
||||
vec3 textureColor = mix(rustColor, shipColor, scratchesColor .r);
|
||||
|
||||
if (sin(vertexLocPos.y * vertexLocPos.x * vertexLocPos.z) > 0) {
|
||||
textureColor = vec3(1, 0, 0);
|
||||
}
|
||||
|
||||
normalizedVertexNormal = normalize(vertexNormalOut);
|
||||
|
||||
outColor = calcPointLight(textureColor, sunPos, sunColor, sunLightExp);
|
||||
|
||||
vec3 reflectorLightDir = normalize(vertexPosOut - reflectorPos);
|
||||
float angleCos = dot(reflectorLightDir, reflectorDir);
|
||||
float reflectorOutAngle = reflectorAngle + radians(10);
|
||||
float epsilon = cos(reflectorAngle) - cos(reflectorOutAngle);
|
||||
if (angleCos > cos(reflectorOutAngle)) {
|
||||
float intensity = clamp((angleCos - cos(reflectorOutAngle)) / epsilon, 0.0, 1.0);
|
||||
outColor += calcSpotLight(textureColor, reflectorPos, reflectorColor, reflectorLightDir, reflectorLightExp * intensity);
|
||||
}
|
||||
}
|
||||
|
24
cw 6/shaders/shader_proc_tex.vert
Normal file
24
cw 6/shaders/shader_proc_tex.vert
Normal file
@ -0,0 +1,24 @@
|
||||
#version 430 core
|
||||
|
||||
layout(location = 0) in vec3 vertexPosition;
|
||||
layout(location = 1) in vec3 vertexNormal;
|
||||
layout(location = 2) in vec2 vertexTexCoord;
|
||||
|
||||
uniform mat4 transformation;
|
||||
uniform mat4 modelMat;
|
||||
|
||||
out vec3 vertexNormalOut;
|
||||
out vec3 vertexPosOut;
|
||||
out vec2 vertexTexCoordOut;
|
||||
out vec3 vertexLocPos;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = transformation * vec4(vertexPosition, 1.0);
|
||||
vec4 worldNormal = modelMat * vec4(vertexNormal, 0.0);
|
||||
vertexNormalOut = worldNormal.xyz;
|
||||
vertexPosOut = (modelMat * vec4(vertexPosition, 1.0)).xyz;
|
||||
vertexTexCoordOut = vertexTexCoord;
|
||||
vertexLocPos = vertexPosition;
|
||||
}
|
||||
|
@ -4,23 +4,22 @@
|
||||
#include "ext.hpp"
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
#include "Shader_Loader.h"
|
||||
#include "Render_Utils.h"
|
||||
#include "Texture.h"
|
||||
|
||||
#include "Box.cpp"
|
||||
#include <assimp/Importer.hpp>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace texture {
|
||||
GLuint earth;
|
||||
GLuint clouds;
|
||||
GLuint moon;
|
||||
GLuint ship;
|
||||
GLuint scratches;
|
||||
GLuint rust;
|
||||
|
||||
GLuint grid;
|
||||
|
||||
@ -34,6 +33,7 @@ GLuint program;
|
||||
GLuint programSun;
|
||||
GLuint programTex;
|
||||
GLuint programEarth;
|
||||
GLuint programProcTex;
|
||||
Core::Shader_Loader shaderLoader;
|
||||
|
||||
Core::RenderContext shipContext;
|
||||
@ -137,6 +137,37 @@ void drawObjectColor(GLuint program, Core::RenderContext& context, glm::mat4 mod
|
||||
|
||||
}
|
||||
|
||||
void drawObjectProc(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color) {
|
||||
program = programProcTex;
|
||||
|
||||
glUseProgram(program);
|
||||
Core::SetActiveTexture(texture::ship, "colorTexture", program, 0);
|
||||
Core::SetActiveTexture(texture::rust, "rust", program, 1);
|
||||
Core::SetActiveTexture(texture::scratches, "scratches", program, 2);
|
||||
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
|
||||
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
|
||||
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
|
||||
glUniformMatrix4fv(glGetUniformLocation(program, "modelMat"), 1, GL_FALSE, (float*)&modelMatrix);
|
||||
//sun
|
||||
glUniform3f(glGetUniformLocation(program, "sunPos"), 0.f, 0.f, 0.f);
|
||||
glUniform3f(glGetUniformLocation(program, "sunColor"), sunLightColor.x, sunLightColor.y, sunLightColor.z);
|
||||
glUniform1f(glGetUniformLocation(program, "sunLightExp"), sunLightExp);
|
||||
//spaceship reflector
|
||||
glm::vec3 reflectorPos = spaceshipPos + 0.037f * spaceshipDir;
|
||||
glUniform3f(glGetUniformLocation(program, "reflectorPos"), reflectorPos.x, reflectorPos.y, reflectorPos.z);
|
||||
glUniform3f(glGetUniformLocation(program, "reflectorDir"), spaceshipDir.x, spaceshipDir.y, spaceshipDir.z);
|
||||
glUniform1f(glGetUniformLocation(program, "reflectorAngle"), reflectorAngle);
|
||||
glUniform3f(glGetUniformLocation(program, "reflectorColor"),
|
||||
reflectorColor.x, reflectorColor.y, reflectorColor.z);
|
||||
glUniform1f(glGetUniformLocation(program, "reflectorLightExp"), reflectorLightExp);
|
||||
|
||||
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
|
||||
|
||||
Core::DrawContext(context);
|
||||
glUseProgram(0);
|
||||
|
||||
}
|
||||
|
||||
void drawObjectTexture(GLuint program, Core::RenderContext& context, glm::mat4 modelMatrix, GLuint textureId) {
|
||||
glUseProgram(program);
|
||||
Core::SetActiveTexture(textureId, "colorTexture", program, 0);
|
||||
@ -158,9 +189,7 @@ void drawObjectTexture(GLuint program, Core::RenderContext& context, glm::mat4 m
|
||||
glUniform1f(glGetUniformLocation(program, "reflectorLightExp"), reflectorLightExp);
|
||||
|
||||
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
|
||||
//TEST
|
||||
glUniform1f(glGetUniformLocation(program, "time"), lastTime);
|
||||
|
||||
|
||||
Core::DrawContext(context);
|
||||
glUseProgram(0);
|
||||
|
||||
@ -189,9 +218,7 @@ void drawEarth(Core::RenderContext& context, glm::mat4 modelMatrix) {
|
||||
glUniform1f(glGetUniformLocation(program, "reflectorLightExp"), reflectorLightExp);
|
||||
|
||||
glUniform3f(glGetUniformLocation(program, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
|
||||
//TEST
|
||||
glUniform1f(glGetUniformLocation(program, "time"), lastTime);
|
||||
|
||||
|
||||
Core::DrawContext(context);
|
||||
glUseProgram(0);
|
||||
|
||||
@ -205,19 +232,17 @@ void renderScene(GLFWwindow* window)
|
||||
float time = glfwGetTime();
|
||||
updateDeltaTime(time);
|
||||
|
||||
time = 0;
|
||||
time = 2;
|
||||
|
||||
//sun
|
||||
drawObjectColor(programSun, sphereContext, glm::mat4(), glm::vec3(1.0, 1.0, 0.3));
|
||||
|
||||
drawEarth(sphereContext,
|
||||
glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::scale(glm::vec3(0.3f)));
|
||||
/*drawObjectTexture(programEarth, sphereContext,
|
||||
glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::scale(glm::vec3(0.3f)),
|
||||
texture::grid);*/
|
||||
|
||||
drawObjectColor(program, sphereContext,
|
||||
glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::eulerAngleY(time) * glm::scale(glm::vec3(0.3f)));
|
||||
|
||||
drawObjectTexture(programTex, sphereContext,
|
||||
glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::eulerAngleY(time) * glm::translate(glm::vec3(1.f, 0, 0)) * glm::scale(glm::vec3(0.1f)),
|
||||
glm::vec3(0.5, 0.5, 0.5));
|
||||
texture::grid);
|
||||
|
||||
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f)));
|
||||
glm::vec3 spaceshipUp = glm::normalize(glm::cross(spaceshipSide, spaceshipDir));
|
||||
@ -229,14 +254,14 @@ void renderScene(GLFWwindow* window)
|
||||
});
|
||||
|
||||
|
||||
//drawObjectColor(shipContext,
|
||||
// glm::translate(cameraPos + 1.5 * cameraDir + cameraUp * -0.5f) * inveseCameraRotrationMatrix * glm::eulerAngleY(glm::pi<float>()),
|
||||
// glm::vec3(0.3, 0.3, 0.5)
|
||||
// );
|
||||
drawObjectColor(program, shipContext,
|
||||
drawObjectProc(shipContext,
|
||||
glm::translate(spaceshipPos) * spaceshipCameraRotrationMatrix * glm::eulerAngleY(glm::pi<float>()) * glm::scale(glm::vec3(0.2f)),
|
||||
glm::vec3(0.3, 0.3, 0.5)
|
||||
glm::vec3(0., 0., 1.)
|
||||
);
|
||||
//drawObjectTexture(programTex, shipContext,
|
||||
// glm::translate(spaceshipPos) * spaceshipCameraRotrationMatrix * glm::eulerAngleY(glm::pi<float>()) * glm::scale(glm::vec3(0.2f)),
|
||||
// texture::ship
|
||||
//);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
@ -250,7 +275,7 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||
void loadModelToContext(std::string path, Core::RenderContext& context)
|
||||
{
|
||||
Assimp::Importer import;
|
||||
const aiScene* scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_CalcTangentSpace);
|
||||
const aiScene * scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_CalcTangentSpace);
|
||||
|
||||
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
|
||||
{
|
||||
@ -269,14 +294,18 @@ void init(GLFWwindow* window)
|
||||
programSun = shaderLoader.CreateProgram("shaders/shader_5_sun.vert", "shaders/shader_5_sun.frag");
|
||||
programTex = shaderLoader.CreateProgram("shaders/shader_5_1_tex.vert", "shaders/shader_5_1_tex.frag");
|
||||
programEarth = shaderLoader.CreateProgram("shaders/shader_earth.vert", "shaders/shader_earth.frag");
|
||||
programProcTex = shaderLoader.CreateProgram("shaders/shader_proc_tex.vert", "shaders/shader_proc_tex.frag");
|
||||
|
||||
loadModelToContext("./models/sphere.obj", sphereContext);
|
||||
loadModelToContext("./models/spaceship.obj", shipContext);
|
||||
|
||||
texture::earth = Core::LoadTexture("./textures/earth.png");
|
||||
texture::clouds = Core::LoadTexture("./textures/clouds.jpg");
|
||||
//moon.png doesn't load correctly
|
||||
texture::moon = Core::LoadTexture("./textures/moon.png");
|
||||
texture::ship = Core::LoadTexture("./textures/spaceship.jpg");
|
||||
texture::scratches = Core::LoadTexture("./textures/scratches.jpg");
|
||||
texture::rust = Core::LoadTexture("./textures/rust.jpg");
|
||||
|
||||
texture::grid = Core::LoadTexture("./textures/grid_color.png");
|
||||
|
||||
@ -325,7 +354,6 @@ void processInput(GLFWwindow* window)
|
||||
cameraDir = spaceshipDir;
|
||||
|
||||
//cameraDir = glm::normalize(-cameraPos);
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)
|
||||
sunLightExp = glm::max(0.f, sunLightExp - 0.1f);
|
||||
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)
|
||||
|
Loading…
Reference in New Issue
Block a user