From 224c27de5092735a930515939cb9eec6b27b9a06 Mon Sep 17 00:00:00 2001 From: David Williams Date: Thu, 29 May 2014 23:31:36 +0200 Subject: [PATCH] Got normal decoding working in shader. --- examples/DecodeOnGPU/decode.vert | 10 ++++++++-- examples/DecodeOnGPU/main.cpp | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/examples/DecodeOnGPU/decode.vert b/examples/DecodeOnGPU/decode.vert index 00675617..5164468a 100644 --- a/examples/DecodeOnGPU/decode.vert +++ b/examples/DecodeOnGPU/decode.vert @@ -1,7 +1,7 @@ #version 140 in uvec4 position; // This will be the position of the vertex in model-space -in vec4 normal; +in uint normal; // The usual matrices are provided uniform mat4 cameraToClipMatrix; @@ -18,7 +18,13 @@ void main() vec4 decodedPosition = position; decodedPosition.xyz = decodedPosition.xyz * (1.0 / 256.0); - worldNormal = normal; + uint encodedX = (normal >> 10u) & 0x1Fu; + uint encodedY = (normal >> 5u) & 0x1Fu; + uint encodedZ = (normal) & 0x1Fu; + worldNormal.xyz = vec3(encodedX, encodedY, encodedZ); + worldNormal.xyz = worldNormal.xyz / 15.5; + worldNormal.xyz = worldNormal.xyz - vec3(1.0, 1.0, 1.0); + worldNormal.w = 1.0; // Standard sequence of OpenGL transformations. worldPosition = modelToWorldMatrix * decodedPosition; diff --git a/examples/DecodeOnGPU/main.cpp b/examples/DecodeOnGPU/main.cpp index fd1e0b16..f286efc2 100644 --- a/examples/DecodeOnGPU/main.cpp +++ b/examples/DecodeOnGPU/main.cpp @@ -99,7 +99,7 @@ OpenGLMeshData buildOpenGLMeshData(const PolyVox::Mesh< PolyVox::MarchingCubesVe // shader. This is mostly just to simplify this example code - in a real application you will know whether your // chosen surface extractor generates normals and can skip uploading them if not. glEnableVertexAttribArray(1); // Attrib '1' is the vertex normals. - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(MarchingCubesVertex< uint8_t >), (GLvoid*)(offsetof(MarchingCubesVertex< uint8_t >, normal))); + glVertexAttribIPointer(1, 1, GL_UNSIGNED_SHORT, sizeof(MarchingCubesVertex< uint8_t >), (GLvoid*)(offsetof(MarchingCubesVertex< uint8_t >, normal))); // Finally a surface extractor will probably output additional data. This is highly application dependant. For this example code // we're just uploading it as a set of bytes which we can read individually, but real code will want to do something specialised here.