Browse Source

finished parallax mapping

Kajetan Johannes Hammerle 3 years ago
parent
commit
874f0fc6e8
6 changed files with 102 additions and 54 deletions
  1. 41 2
      Game.cpp
  2. 9 0
      Game.h
  3. 1 1
      gaming-core
  4. 46 21
      resources/fragment.fs
  5. 0 26
      resources/geometry.gs
  6. 5 4
      resources/noiceFragment.fs

+ 41 - 2
Game.cpp

@@ -19,7 +19,11 @@ Game::Game(Shader& shader, Shader& noiceShader, LayeredFramebuffer& buffer,
       right(GLFW_KEY_D, "right"), front(GLFW_KEY_W, "front"),
       back(GLFW_KEY_S, "back"), toggle(GLFW_KEY_T, "toggle"),
       scaleUp(GLFW_KEY_G, "scale up"), scaleDown(GLFW_KEY_H, "scale down"),
-      oldHeight(0.0f), height(0.0f), heightScale(0.0f) {
+      stepsUp(GLFW_KEY_Y, "steps up"), stepsDown(GLFW_KEY_U, "steps down"),
+      fineStepsUp(GLFW_KEY_I, "fine steps up"),
+      fineStepsDown(GLFW_KEY_O, "fine steps down"),
+      modeToggle(GLFW_KEY_C, "mode toggle"), oldHeight(0.0f), height(0.0f),
+      heightScale(0.01f), steps(1), fineSteps(1), mode(false) {
     buttons.add(up);
     buttons.add(down);
     buttons.add(left);
@@ -29,8 +33,17 @@ Game::Game(Shader& shader, Shader& noiceShader, LayeredFramebuffer& buffer,
     buttons.add(toggle);
     buttons.add(scaleUp);
     buttons.add(scaleDown);
+    buttons.add(stepsUp);
+    buttons.add(stepsDown);
+    buttons.add(fineStepsUp);
+    buttons.add(fineStepsDown);
+    buttons.add(modeToggle);
 
-    position = Vector3(0.0f, 0.0f, -40.0f);
+    bricks.setLinearFilter();
+    bricksBump.setLinearFilter();
+    bricksNormal.setLinearFilter();
+
+    position = Vector3(-32.0f, 0.0f, -120.0f);
 
     rectangleBuffer.setAttributes(Attributes().addFloat(2));
     float recData[6][2] = {{-1.0f, -1.0f}, {-1.0, 1.0}, {1.0, -1.0},
@@ -66,6 +79,9 @@ void Game::render(float lag) {
     shader.setVector("viewPos", -position + Vector3(0.0f, 32.0f, 0.0f));
     shader.setVector("lightPos", Vector3());
     shader.setFloat("heightScale", heightScale);
+    shader.setInt("steps", steps);
+    shader.setInt("fineSteps", fineSteps);
+    shader.setInt("kajetan", mode);
 
     if(toggle.isDown()) {
         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
@@ -79,6 +95,13 @@ void Game::render(float lag) {
 }
 
 void Game::tick() {
+    StringBuffer<50>()
+        .append(steps)
+        .append(" ")
+        .append(fineSteps)
+        .append(" ")
+        .append(heightScale)
+        .printLine();
     oldHeight = height;
     oldPosition = position;
     if(up.isDown()) {
@@ -110,6 +133,22 @@ void Game::tick() {
             heightScale = 0.0f;
         }
     }
+
+    if(stepsUp.wasReleased()) {
+        steps++;
+    }
+    if(stepsDown.wasReleased() && steps > 1) {
+        steps--;
+    }
+    if(fineStepsUp.wasReleased()) {
+        fineSteps++;
+    }
+    if(fineStepsDown.wasReleased() && fineSteps > 1) {
+        fineSteps--;
+    }
+    if(modeToggle.wasReleased()) {
+        mode = !mode;
+    }
 }
 
 bool Game::isRunning() const {

+ 9 - 0
Game.h

@@ -32,12 +32,21 @@ class Game final {
     Button toggle;
     Button scaleUp;
     Button scaleDown;
+    Button stepsUp;
+    Button stepsDown;
+    Button fineStepsUp;
+    Button fineStepsDown;
+    Button modeToggle;
+
     Vector3 oldPosition;
     Vector3 position;
 
     float oldHeight;
     float height;
     float heightScale;
+    int steps;
+    int fineSteps;
+    bool mode;
 
 public:
     Game(Shader& shader, Shader& noiceShader, LayeredFramebuffer& buffer,

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit 35425beaa48eab218b807971ff8d8294eb3ca6f3
+Subproject commit fe4502a3d5a8cdb35829e979c77cd34231082598

+ 46 - 21
resources/fragment.fs

@@ -6,21 +6,19 @@ layout (binding = 2) uniform sampler2D bumpSamp;
 layout (binding = 3) uniform sampler2D normalSamp;
 
 uniform float height;
+uniform vec3 viewPos;
+uniform int steps;
+uniform int fineSteps;
+uniform float heightScale;
+uniform bool kajetan;
 
 in vec3 varPositionG;
 in vec2 varTextureG;
-in vec3 tangentLightPosG;
-in vec3 tangentViewPosG;
-in vec3 tangentFragPosG;
-
-uniform vec3 viewPos;
 
 out vec4 color;
 
 const vec3 light = vec3(-0.746, -0.373, -0.224);
 
-uniform float heightScale;
-
 vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir) { 
     float height = texture(bumpSamp, texCoords, 0.0).r;    
     vec2 p = viewDir.xy / viewDir.z * (height * heightScale);
@@ -29,23 +27,50 @@ vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir) {
 
 void main(void) {
     vec2 tex = varTextureG + vec2(0.0, height);
-    //float depth = texture(textureSamp, tex).x;
-    //vec3 dir = (camPosition - varPositionG) * depth * 0.05;
+    vec2 eye = normalize(viewPos - varPositionG).xy * heightScale;
 
-    //vec3 f = texture(textureSamp, tex).xyz;
-    //float l = max(dot(light, varNormalG), 0.0) * 0.7 + 0.3;
-    //color = vec4(f, 1.0);
+    // kajetans variant
+    if(kajetan) {
+        eye *= texture(bumpSamp, tex).x;
+        tex += -eye;
+    } else {
+        // lecture
+        float h = 1.0;
+        float prev_hits = 0;
+        float hit_h = 0;
+        vec2 mTex = tex;
+        float step = 1.0 / steps;
+        vec2 stepEye = eye / steps;
+        for (int it = 0; it < steps; it++) {
+            h -= step;
+            mTex += stepEye;
+            float h_tex = texture(bumpSamp, mTex).x;
+            float is_first_hit = float((h_tex - h - prev_hits) > 0.0);
+            hit_h += is_first_hit * h;
+            prev_hits += is_first_hit;
+        }
+        hit_h = max(0.0, hit_h);
 
-    // offset texture coordinates with Parallax Mapping
-    vec3 viewDir = normalize(tangentViewPosG - tangentFragPosG);
-    vec2 texCoords = ParallaxMapping(tex.xy, viewDir);
+        h = hit_h;
+        prev_hits = 0;
+        mTex = tex + stepEye * hit_h;
+        hit_h = 0;
+        step /= fineSteps;
+        stepEye = eye / fineSteps;
+        for (int it = 0; it < fineSteps; it++) {
+            h -= step;
+            mTex += stepEye;
+            float h_tex = texture(bumpSamp, mTex).x;
+            float is_first_hit = float((h_tex - h - prev_hits) > 0.0);
+            hit_h += is_first_hit * h;
+            prev_hits += is_first_hit;
+        }
 
-    // then sample textures with new texture coords
-    //vec3 diffuse = texture(diffuseMap, texCoords);
-    vec3 normal  = texture(normalSamp, texCoords).xyz;
-    normal = normalize(normal * 2.0 - 1.0);
+        tex += eye * hit_h;
+    }
 
+    vec3 normal  = texture(normalSamp, tex).xyz;
+    normal = normalize(normal * 2.0 - 1.0);
     float l = max(dot(-light, normal), 0.0) * 0.7 + 0.3;
-
-    color = vec4(texture(textureSamp, texCoords).xyz * l, 1.0);
+    color = vec4(texture(textureSamp, tex).xyz * l, 1.0);
 }

+ 0 - 26
resources/geometry.gs

@@ -6,17 +6,11 @@ layout (triangle_strip, max_vertices = 12) out;
 uniform mat4 proj;
 uniform mat4 view;
 
-uniform vec3 lightPos;
-uniform vec3 viewPos;
-
 in vec3 varTexture[];
 in int varIndex[];
 
 out vec3 varPositionG;
 out vec2 varTextureG;
-out vec3 tangentLightPosG;
-out vec3 tangentViewPosG;
-out vec3 tangentFragPosG;
 
 vec3 vectors[13] = {
     vec3(0.0, 0.0, 0.0), vec3(0.5, 1.0, 1.0), vec3(0.0, 0.5, 1.0), 
@@ -178,40 +172,20 @@ void main(void) {
         vec3 ab = b - a;
         vec3 ac = c - a;
         vec3 normal = normalize(cross(ab, ac));
-        // tangent, same for all vertices
-        vec2 abt = tb - ta;
-        vec2 act = tc - ta;
-        float f = 1.0f / (abt.x * act.y - act.x * abt.y);
-        vec3 tangent = normalize(f * (act.y * ab - abt.y * ac));
-
-        vec3 biTangent = cross(normal, tangent);
-
-        mat3 TBN = transpose(mat3(tangent, biTangent, normal));
-        vec3 tangentLightPos = TBN * lightPos;
-        vec3 tangentViewPos  = TBN * viewPos;
 
         gl_Position = proj * view * vec4(a, 1.0);
         varPositionG = a;
         varTextureG = ta;
-        tangentLightPosG = tangentLightPos;
-        tangentViewPosG = tangentViewPos;
-        tangentFragPosG = TBN * a;
         EmitVertex();
 
         gl_Position = proj * view * vec4(b, 1.0);
         varPositionG = b;
         varTextureG = tb;
-        tangentLightPosG = tangentLightPos;
-        tangentViewPosG = tangentViewPos;
-        tangentFragPosG = TBN * b;
         EmitVertex();
 
         gl_Position = proj * view * vec4(c, 1.0);
         varPositionG = c;
         varTextureG = tc;
-        tangentLightPosG = tangentLightPos;
-        tangentViewPosG = tangentViewPos;
-        tangentFragPosG = TBN * c;
         EmitVertex();
 
         EndPrimitive();

+ 5 - 4
resources/noiceFragment.fs

@@ -5,8 +5,9 @@ in vec3 varPosition;
 out float noice;
 
 void main(void) {
-    float sinX = sin(varPosition.x * 5.8905);
-    float cosY = cos(varPosition.y * 5.8905);
-    float cosZ = cos(varPosition.z * 5.8905);
-    noice = (sinX * sinX + cosY * cosY + cosZ * cosZ) * (1.0f / 3.0f);
+    //float sinX = sin(varPosition.x * 5.8905);
+    //float cosY = cos(varPosition.y * 5.8905);
+    //float cosZ = cos(varPosition.z * 5.8905);
+    //noice = (sinX * sinX + cosY * cosY + cosZ * cosZ) * (1.0f / 3.0f);
+    noice = 1.0;
 }