From 70b68ad9473a9e9bc2da5e9b2eeb5786c468e037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anselme=20FRAN=C3=87OIS?= Date: Wed, 18 May 2016 00:12:10 +0200 Subject: [PATCH] fixed inputs, added phong lighting --- shaders/world.frag.glsl | 9 ++++++--- shaders/world.vert.glsl | 2 +- src/pixelpipeline.cpp | 2 +- src/pixelpipeline.h | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/shaders/world.frag.glsl b/shaders/world.frag.glsl index 01c873f..25d7c07 100644 --- a/shaders/world.frag.glsl +++ b/shaders/world.frag.glsl @@ -11,6 +11,8 @@ in vec3 normal; out vec4 outColor; +const vec3 lightDir = normalize(vec3(0.5, -1, -1)); + vec3 phongLighting(in vec3 kd, in vec3 ks, in float ns, in vec3 color, in vec3 normal, in vec3 lightDir, in vec3 halfVec){ float diffuseComponent = max(dot(normal, lightDir), 0); float specularComponent = max(dot(halfVec, normal), 0); @@ -19,7 +21,7 @@ vec3 phongLighting(in vec3 kd, in vec3 ks, in float ns, in vec3 color, in vec3 n void main() { - vec2 worldCoord = texCoord*worldSize + camera.xy; + vec2 worldCoord = texCoord*worldSize + camera.xy*worldSize.y/screenSize; ivec2 nbRevolutions = ivec2(floor(worldCoord / worldSize)); if(abs(mod(nbRevolutions.y, 2)) > 0.5) { @@ -28,6 +30,7 @@ void main() } worldCoord = worldCoord - nbRevolutions*worldSize; vec3 texColor = texelFetch(colorMap, ivec2(worldCoord)).xyz; - float pseudoLighting = 0.4+dot(normal, normalize(vec3(-0.5))); // TODO : use phong lighting - outColor = vec4(texColor*pseudoLighting, 1.0); + + vec3 lighting = phongLighting(texColor, vec3(0.5), 10, vec3(1), normal, lightDir, normalize(lightDir+vec3(0, 0, -1))); + outColor = vec4(texColor*0.2 + 0.8*lighting, 1.0); } diff --git a/shaders/world.vert.glsl b/shaders/world.vert.glsl index 0a71e1b..771c0c4 100644 --- a/shaders/world.vert.glsl +++ b/shaders/world.vert.glsl @@ -12,7 +12,7 @@ out vec3 normal; void main(void) { - texCoord = inTexCoord.xy; + texCoord = vec2(inTexCoord.x/2, inTexCoord.y); normal = inNormal.xyz; gl_Position = mvp * vec4(inPosition*camera.z, 1.0); } diff --git a/src/pixelpipeline.cpp b/src/pixelpipeline.cpp index 1008cb2..ce1578f 100644 --- a/src/pixelpipeline.cpp +++ b/src/pixelpipeline.cpp @@ -53,7 +53,7 @@ public: float clockAngle = atan2(relUV.y, relUV.x); float depthAngle = glm::length(relUV)*3.1416f*MAGIC_RATIO; float r = sin(depthAngle); - return glm::vec3(r*cos(clockAngle), r*sin(clockAngle), cos(depthAngle)); + return glm::vec3(r*cos(clockAngle), r*sin(clockAngle), cos(depthAngle)-1); } }; diff --git a/src/pixelpipeline.h b/src/pixelpipeline.h index 9279c0f..92043be 100644 --- a/src/pixelpipeline.h +++ b/src/pixelpipeline.h @@ -41,7 +41,7 @@ public: virtual void renderGL(Scene *scene); virtual void resizeGL(int w, int h); - void cameraMove(int x, int y) { m_camera.x -= x; m_camera.y += y; } + void cameraMove(int x, int y) { m_camera.x -= x/m_camera.z; m_camera.y += y/m_camera.z; } void cameraZoom(int nbScrolls); };