Browse Source

texturing

Kajetan Johannes Hammerle 3 years ago
parent
commit
59453f59bf
7 changed files with 116 additions and 8 deletions
  1. 54 3
      Game.cpp
  2. 2 0
      Game.h
  3. 26 0
      Texture3D.cpp
  4. 21 0
      Texture3D.h
  5. 2 0
      meson.build
  6. 6 3
      resources/fragment.fs
  7. 5 2
      resources/geometry.gs

+ 54 - 3
Game.cpp

@@ -5,6 +5,7 @@
 #include "MarchingCubes.h"
 #include "gaming-core/utils/Array.h"
 #include "gaming-core/utils/List.h"
+#include "gaming-core/utils/Random.h"
 #include "gaming-core/utils/Utils.h"
 #include "gaming-core/wrapper/GL.h"
 
@@ -25,10 +26,58 @@ Game::Game(Shader& shader, Shader& noiceShader, LayeredFramebuffer& buffer,
     buttons.add(toggle);
 
     rectangleBuffer.setAttributes(Attributes().addFloat(2));
-    float data[6][2] = {{-1.0f, -1.0f}, {-1.0, 1.0}, {1.0, -1.0},
-                        {1.0f, 1.0f},   {-1.0, 1.0}, {1.0, -1.0}};
-    rectangleBuffer.setStaticData(sizeof(data), data);
+    float recData[6][2] = {{-1.0f, -1.0f}, {-1.0, 1.0}, {1.0, -1.0},
+                           {1.0f, 1.0f},   {-1.0, 1.0}, {1.0, -1.0}};
+    rectangleBuffer.setStaticData(sizeof(recData), recData);
     noiceBuffer.bindTextureTo(0);
+
+    Random r;
+    const int textureSize = 64;
+    float tData[textureSize][textureSize][textureSize][3];
+    for(int x = 0; x < textureSize; x++) {
+        for(int y = 0; y < textureSize; y++) {
+            for(int z = 0; z < textureSize; z++) {
+                tData[x][y][z][0] = r.nextFloat();
+            }
+        }
+    }
+    for(int k = 1; k < 3; k++) {
+        int factor = (k * 2 + 1) * (k * 2 + 1) * (k * 2 + 1);
+        for(int x = 0; x < textureSize; x++) {
+            for(int y = 0; y < textureSize; y++) {
+                for(int z = 0; z < textureSize; z++) {
+                    float sum = 0.0f;
+                    for(int mx = -k; mx <= k; mx++) {
+                        for(int my = -k; my <= k; my++) {
+                            for(int mz = -k; mz <= k; mz++) {
+                                int rx = std::abs(x + mx);
+                                int ry = std::abs(y + my);
+                                int rz = std::abs(z + mz);
+                                rx = rx >= 64 ? 127 - rx : rx;
+                                ry = ry >= 64 ? 127 - ry : ry;
+                                rz = rz >= 64 ? 127 - rz : rz;
+                                sum += tData[rx][ry][rz][0];
+                            }
+                        }
+                    }
+                    tData[x][y][z][0] = sum / factor;
+                }
+            }
+        }
+    }
+    for(int x = 0; x < textureSize; x++) {
+        for(int y = 0; y < textureSize; y++) {
+            for(int z = 0; z < textureSize; z++) {
+                float v = tData[x][y][z][0];
+                float vvv = v * v * v;
+                v = 6.0f * vvv * v * v - 15.0f * vvv * v + 10.0f * vvv;
+                tData[x][y][z][0] = v;
+                tData[x][y][z][1] = v;
+                tData[x][y][z][2] = v;
+            }
+        }
+    }
+    texture.setData(textureSize, textureSize, textureSize, tData);
 }
 
 void Game::render(float lag) {
@@ -55,11 +104,13 @@ void Game::render(float lag) {
     m.translateY(-32.0f + (oldHeight - height) * lag);
     m.translateX(-32.0f);
     shader.setMatrix("view", m.getValues());
+    shader.setFloat("height", oldHeight * step * 0.5f);
 
     if(toggle.isDown()) {
         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
     }
     noiceBuffer.bindTextureTo(0);
+    texture.bindTo(1);
     emptyBuffer.drawPoints(64 * 64 * 64);
     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 }

+ 2 - 0
Game.h

@@ -2,6 +2,7 @@
 #define GAME_H
 
 #include "LayeredFramebuffer.h"
+#include "Texture3D.h"
 #include "gaming-core/input/Buttons.h"
 #include "gaming-core/math/Frustum.h"
 #include "gaming-core/rendering/Shader.h"
@@ -12,6 +13,7 @@ class Game final {
     Shader& shader;
     Shader& noiceShader;
     LayeredFramebuffer& noiceBuffer;
+    Texture3D texture;
     Buttons& buttons;
     const Size& size;
     VertexBuffer rectangleBuffer;

+ 26 - 0
Texture3D.cpp

@@ -0,0 +1,26 @@
+#include "Texture3D.h"
+
+Texture3D::Texture3D() : texture(0) {
+    glGenTextures(1, &texture);
+    glBindTexture(GL_TEXTURE_3D, texture);
+    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
+    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT);
+    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
+}
+
+Texture3D::~Texture3D() {
+    glDeleteTextures(1, &texture);
+}
+
+void Texture3D::setData(int width, int height, int depth, void* data) {
+    glBindTexture(GL_TEXTURE_3D, texture);
+    glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, width, height, depth, 0, GL_RGB,
+                 GL_FLOAT, data);
+}
+
+void Texture3D::bindTo(int unit) const {
+    glActiveTexture(GL_TEXTURE0 + unit);
+    glBindTexture(GL_TEXTURE_3D, texture);
+}

+ 21 - 0
Texture3D.h

@@ -0,0 +1,21 @@
+#ifndef TEXTURE_3D_H
+#define TEXTURE_3D_H
+
+#include <GL/glew.h>
+
+class Texture3D final {
+    GLuint texture;
+
+public:
+    Texture3D();
+    ~Texture3D();
+    Texture3D(const Texture3D&) = delete;
+    Texture3D(Texture3D&&) = delete;
+    Texture3D& operator=(const Texture3D&) = delete;
+    Texture3D& operator=(Texture3D&&) = delete;
+
+    void setData(int width, int height, int depth, void* data);
+    void bindTo(int unit) const;
+};
+
+#endif

+ 2 - 0
meson.build

@@ -4,8 +4,10 @@ sources = ['Main.cpp',
     'Game.cpp',
     'MarchingCubes.cpp',
     'LayeredFramebuffer.cpp',
+    'Texture3D.cpp',
     'gaming-core/utils/Size.cpp',
     'gaming-core/utils/Clock.cpp',
+    'gaming-core/utils/Random.cpp',
     'gaming-core/utils/Error.cpp',
     'gaming-core/wrapper/GL.cpp',
     'gaming-core/rendering/Window.cpp',

+ 6 - 3
resources/fragment.fs

@@ -1,12 +1,15 @@
 #version 430
 
-layout (binding = 0) uniform sampler3D samp;
+layout (binding = 0) uniform sampler3D noiseSamp;
+layout (binding = 1) uniform sampler3D textureSamp;
+
+uniform float height;
 
 in vec3 varTextureG;
 
 out vec4 color;
 
 void main(void) {
-    float f = texture(samp, varTextureG).r;
-    color = vec4(f, f, f, 1.0);
+    vec3 f = texture(textureSamp, (varTextureG + vec3(0.0, height, 0.0)) * 8.0).xyz;
+    color = vec4(f, 1.0);
 }

+ 5 - 2
resources/geometry.gs

@@ -182,13 +182,16 @@ int table[3072] = {
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
 };
 
+const float step = 1.0 / 63.0;
+
 void main(void) {	
     vec4 base = gl_in[0].gl_Position;
     int index = varIndex[0] * 12;
     for(int i = 0; i < 12; i += 3) {
         for(int k = 0; k < 3; k++) {
-            gl_Position = proj * view * (base + vec4(vectors[table[index + i + k]], 0.0));
-            varTextureG = varTexture[0];
+            vec3 offset = vectors[table[index + i + k]];
+            gl_Position = proj * view * (base + vec4(offset, 0.0));
+            varTextureG = varTexture[0] + offset * step;
             EmitVertex();
         }
         EndPrimitive();