Browse Source

working passing geometry shader

Kajetan Johannes Hammerle 3 years ago
parent
commit
89f0f7883f
6 changed files with 51 additions and 22 deletions
  1. 18 11
      Game.cpp
  2. 2 1
      Main.cpp
  3. 1 1
      gaming-core
  4. 10 5
      resources/fragment.fs
  5. 14 0
      resources/geometry.gs
  6. 6 4
      resources/vertex.vs

+ 18 - 11
Game.cpp

@@ -7,8 +7,9 @@
 #include "gaming-core/utils/List.h"
 #include "gaming-core/utils/Random.h"
 #include "gaming-core/utils/Utils.h"
+#include "gaming-core/wrapper/GL.h"
 
-// static GLuint texture3d;
+static GLuint texture3d;
 static List<Vector3, 122880> list;
 static float tData[16][16][16];
 
@@ -41,7 +42,7 @@ Game::Game(Shader& shader, Buttons& buttons, const Size& size)
       front(buttons.add(GLFW_KEY_W, "front")),
       back(buttons.add(GLFW_KEY_S, "back")) {
     shader.use();
-    vertexBuffer.setAttributes(Attributes().addFloat(3));
+    // vertexBuffer.setAttributes(Attributes().addFloat(3));
 
     Random r(0);
     for(int x = 0; x < 16; x++) {
@@ -49,13 +50,13 @@ Game::Game(Shader& shader, Buttons& buttons, const Size& size)
             for(int z = 0; z < 16; z++) {
                 if(x == 0 || x == 15 || y == 0 || y == 15 || z == 0 ||
                    z == 15) {
-                    tData[x][y][z] = 0.0f;
+                    tData[z][x][y] = 0.1f;
                 } else {
                     // tData[x][y][z] = 1.0f;
                     float sinX = sinf(M_PI * x / 8.0f);
                     float cosY = cosf(M_PI * y / 8.0f);
                     float cosZ = cosf(M_PI * z / 8.0f);
-                    tData[x][y][z] = (sinX * sinX + cosY * cosY + cosZ * cosZ) *
+                    tData[z][x][y] = (sinX * sinX + cosY * cosY + cosZ * cosZ) *
                                      (1.0f / 3.0f);
                 }
             }
@@ -72,17 +73,17 @@ Game::Game(Shader& shader, Buttons& buttons, const Size& size)
     vertexBuffer.setStaticData(sizeof(Vector3) * list.getLength(),
                                list.begin());
 
-    /*glGenTextures(1, &texture3d);
+    glGenTextures(1, &texture3d);
     glBindTexture(GL_TEXTURE_3D, texture3d);
     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_REPEAT);
-    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
-    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
+    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
+    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
 
-    glTexImage3D(GL_TEXTURE_3D, 0, GL_R32F, 16, 16, 1, 0, GL_RED, GL_FLOAT,
+    glTexImage3D(GL_TEXTURE_3D, 0, GL_R32F, 16, 16, 16, 0, GL_RED, GL_FLOAT,
                  tData);
-    glActiveTexture(GL_TEXTURE0);*/
+    glActiveTexture(GL_TEXTURE0);
 }
 
 void Game::render(float lag) {
@@ -93,9 +94,15 @@ void Game::render(float lag) {
     m.translate(interPos);
     m.translateZ(-30.0f);
     m.translateX(-8.0f);
+
     shader.setMatrix("view", m.getValues());
 
-    vertexBuffer.draw(list.getLength());
+    glPointSize(10.0f);
+    // vertexBuffer.draw(list.getLength());
+    GL::checkAndPrintError("test3");
+    vertexBuffer.drawPoints(0);
+    GL::checkAndPrintError("HERE");
+    glDrawArrays(GL_POINTS, 0, 16 * 16 * 16);
 }
 
 void Game::tick() {

+ 2 - 1
Main.cpp

@@ -28,7 +28,8 @@ int main() {
         return 0;
     }
 
-    Shader shader("resources/vertex.vs", "resources/fragment.fs");
+    Shader shader("resources/vertex.vs", "resources/fragment.fs",
+                  "resources/geometry.gs");
     if(shader.hasError()) {
         return 0;
     }

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit ec1c9f1198e7e0b81f3134c2068dd6d90cd7f24a
+Subproject commit 740eae7b0f10e4f0947dc65a9dac55bf2e0c570f

+ 10 - 5
resources/fragment.fs

@@ -2,15 +2,20 @@
 
 layout (binding = 0) uniform sampler3D samp;
 
-in vec3 varTexture;
+in vec3 varTextureG;
 
 out vec4 color;
 
 void main(void) {
     //float f = texture(samp, varTexture).r;
-    float x = varTexture.x - int(varTexture.x);
-    float y = varTexture.y - int(varTexture.y);
-    float z = varTexture.z - int(varTexture.z);
-    color = vec4(x, y, z, 1.0);
+    //float x = varTexture.x - int(varTexture.x);
+    //float y = varTexture.y - int(varTexture.y);
+    //float z = varTexture.z - int(varTexture.z);
+    //color = vec4(x, y, z, 1.0);
+
+    float f = texture(samp, varTextureG).r;
+    color = vec4(f, f, f, 1.0);
+
+    //color = vec4(1.0, 1.0, 0.0, 1.0);
     //color = vec4(varTexture, 0.0, 1.0);
 }

+ 14 - 0
resources/geometry.gs

@@ -0,0 +1,14 @@
+#version 430
+
+layout (points) in;
+layout (points, max_vertices = 1) out;
+
+in vec3 varTexture[];
+out vec3 varTextureG;
+
+void main (void) {	
+    gl_Position = gl_in[0].gl_Position;
+    varTextureG = varTexture[0];
+    EmitVertex();
+	EndPrimitive();
+}

+ 6 - 4
resources/vertex.vs

@@ -1,13 +1,15 @@
 #version 430
 
-layout (location = 0) in vec3 position;
-
 uniform mat4 proj;
 uniform mat4 view;
 
 out vec3 varTexture;
 
 void main(void) { 
-    gl_Position = proj * view * vec4(position, 1.0);
-    varTexture = position;
+    int id = gl_VertexID;
+    int x = id & 0xF;
+    int y = (id >> 4) & 0xF;
+    int z = (id >> 8) & 0xF;
+    gl_Position =  proj * view * vec4(x, y, z, 1.0);
+    varTexture = vec3(x, y, z) * (1.0 / 15.0);
 }