...
 
Commits (2)
......@@ -15,7 +15,7 @@ void ShaderSkyBox::update() {
CVK::CubeMapTexture* cubeMap =
CVK::State::getInstance()->getCubeMapTexture();
if (cubeMap != 0) {
if (cubeMap != nullptr) {
cubeMap->bind();
}
}
......@@ -73,14 +73,13 @@ CVK::Material *mat_blue, *mat_red;
CVK::Light* plight;
void initScene() {
// Textures
std::vector<std::string> textureNames = {
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/posx.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/negx.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/posy.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/negy.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/posz.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/negz.jpg"};
cubeMap = new CVK::CubeMapTexture(textureNames);
cubeMap = new CVK::CubeMapTexture(
{CVK::State::getInstance()->getResourcesPath() + "/cubeMap/posx.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/negx.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/posy.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/negy.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/posz.jpg",
CVK::State::getInstance()->getResourcesPath() + "/cubeMap/negz.jpg"});
// Geometries
teapot = new CVK::Teapot;
......
......@@ -23,10 +23,10 @@ CubeMapTexture::~CubeMapTexture() {
void CubeMapTexture::createCubeMapTexture() {
glGenTextures(1, &m_textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureID);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
......
#include <CVK_2/CVK_Geometry.h>
#include <algorithm>
namespace CVK {
Geometry::Geometry() = default;
......@@ -112,6 +114,7 @@ void Geometry::render() {
void Geometry::computeTangents() {
m_tangents.resize(m_vertices.size());
std::fill(m_tangents.begin(), m_tangents.end(), glm::vec3(0.0f));
for (int i = 0; i < m_indices; i += 3) {
// Edges of the triangle : postion delta
......@@ -137,10 +140,16 @@ void Geometry::computeTangents() {
glm::vec4 tmp = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y) * r;
glm::vec3 tangent = glm::vec3(tmp.x, tmp.y, tmp.z);
// Set the same tangent for all three vertices of the triangle
m_tangents[index_v0] = tangent;
m_tangents[index_v1] = tangent;
m_tangents[index_v2] = tangent;
// Adding, as multiple triangles share indices
// This will smooth out sudden changes
m_tangents[index_v0] += tangent;
m_tangents[index_v1] += tangent;
m_tangents[index_v2] += tangent;
}
// normalize at the end
for (auto& t : m_tangents) {
t = glm::normalize(t);
}
}
......
......@@ -5,7 +5,6 @@ in vec4 passPosition;
in vec3 passNormal;
in vec2 passUVCoord;
in vec3 passReflectionNormal;
uniform struct
{
......@@ -40,7 +39,7 @@ void main(){
///////////////// Cube Map Reflection /////////////////
///////////////////////////////////////////////////////
vec3 pos = normalize(vec3(passPosition));
vec3 normal = normalize(passReflectionNormal);
vec3 normal = normalize(passNormal);
vec3 reflected = reflect(pos, normal);
reflected = vec3(inverse(viewMatrix) * vec4(reflected, 0.0));
......
......@@ -12,15 +12,11 @@ out vec4 passPosition;
out vec3 passNormal;
out vec2 passUVCoord;
out vec3 passReflectionNormal;
void main(){
passPosition = viewMatrix * modelMatrix * position;
passNormal = vec3(transpose(inverse(viewMatrix * modelMatrix)) * vec4(normal, 0));
passUVCoord = uv;
passReflectionNormal = vec3(viewMatrix * modelMatrix * vec4(normal, 0));
gl_Position = projectionMatrix * viewMatrix * modelMatrix * position;
}
......@@ -26,7 +26,8 @@ void main(){
/*********** Calculate matrix that goes from camera space to tangent space **********/
mat4 normalMatrix = transpose(inverse(viewMatrix * modelMatrix));
vec3 normal_cs = normalize((normalMatrix * vec4(normal, 0)).xyz);
vec3 tangent_cs = normalize((normalMatrix * vec4(tangent, 0)).xyz);
// tangent transforms just like points
vec3 tangent_cs = normalize((viewMatrix * modelMatrix * vec4(tangent, 0)).xyz);
vec3 bitangent_cs = cross(tangent_cs, normal_cs);
mat3 tbn = transpose(mat3(
......@@ -40,7 +41,7 @@ void main(){
/*********** Calculate light vector (tangent space) *********************************/
// light position in world coordinates
vec3 lightPosition_cs = (light.pos).xyz;
vec3 lightPosition_cs = (viewMatrix *light.pos).xyz;
// calcluate vector that goes from the vertex to the light, in tangent space
passLightVector = tbn * normalize(lightPosition_cs - position_cs);
......