Skip to content

Commit cc0c15e

Browse files
committed
Worked on voxelization geometry shader
1 parent 78304b1 commit cc0c15e

File tree

2 files changed

+98
-23
lines changed

2 files changed

+98
-23
lines changed

assets/shader/VoxelConeTracing/voxelizeGeom.shader

+95-21
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,110 @@ out VertexData {
4040
vec2 uv;
4141
} Out;
4242

43-
// Constants for the projection planes to key into the worldAxes array
44-
const uint YZ = 0;
45-
const uint XZ = 1;
46-
const uint XY = 2;
47-
4843
// (TODO) replace with an uniform
4944
// This is the lower left corner of the voxel grid
5045
const vec3 voxelGridOrigin = vec3(0.0, 0.0, 0.0);
5146

5247
// (TODO) replace with an uniform
48+
// This is the length of the voxel grid, but not neccessarily the number of
49+
// voxels (only if they have a side-lenght of 1)
5350
const float voxelGridLength = 12;
5451

52+
// Constants for the projection planes to key into the worldAxes array
53+
const uint YZ = 0;
54+
const uint XZ = 1;
55+
const uint XY = 2;
56+
57+
// Constants to key into worldaxes
58+
const uint X = 0;
59+
const uint Y = 1;
60+
const uint Z = 2;
61+
5562
const vec3 worldAxes[3] = vec3[3]( vec3(1.0, 0.0, 0.0),
5663
vec3(0.0, 1.0, 0.0),
5764
vec3(0.0, 0.0, 1.0) );
5865

66+
mat4 viewProjs[3];
67+
68+
void init() {
69+
mat4 camProjMatrix = mat4(2.0 / voxelGridLength, 0, 0, 0,
70+
0, 2.0 / voxelGridLength, 0, 0,
71+
0, 0, - 2.0 / voxelGridLength, 0,
72+
0, 0, 0, 1);
73+
74+
vec3 camPositions[3] =
75+
vec3[3] ( voxelGridOrigin
76+
+ vec3(voxelGridLength, // Right
77+
voxelGridLength / 2.0, voxelGridLength / 2.0),
78+
79+
voxelGridOrigin
80+
+ vec3(voxelGridLength / 2.0, voxelGridLength, // TOP
81+
voxelGridLength / 2.0),
82+
83+
voxelGridOrigin
84+
+ vec3(voxelGridLength / 2.0, voxelGridLength / 2.0, // Far
85+
voxelGridLength) );
86+
87+
mat4 viewMats[3] = mat4[3]( mat4(1.0), // Right
88+
mat4(1.0), // Top
89+
mat4(1.0) ); // Far
90+
91+
// View Matrix for right camera
92+
viewMats[0][0] = vec4(0.0, 0.0, 1.0, 0.0);
93+
viewMats[0][1] = vec4(0.0, 1.0, 0.0, 0.0);
94+
viewMats[0][2] = vec4(1.0, 0.0, 0.0, 0.0);
95+
viewMats[0][3] = vec4(dot(viewMats[0][0].xyz, -camPositions[0]),
96+
dot(viewMats[0][1].xyz, -camPositions[0]),
97+
dot(viewMats[0][2].xyz, -camPositions[0]), 1.0);
98+
99+
// View Matrix for top camera
100+
viewMats[1][0] = vec4(1.0, 0.0, 0.0, 0.0);
101+
viewMats[1][1] = vec4(0.0, 0.0, 1.0, 0.0);
102+
viewMats[1][2] = vec4(0.0, 1.0, 0.0, 0.0);
103+
viewMats[1][3] = vec4(dot(viewMats[1][0].xyz, -camPositions[1]),
104+
dot(viewMats[1][1].xyz, -camPositions[1]),
105+
dot(viewMats[1][2].xyz, -camPositions[1]), 1.0);
106+
107+
108+
// View Matrix for far camera
109+
viewMats[2][0] = vec4(-1.0, 0.0, 0.0, 0.0);
110+
viewMats[2][1] = vec4(0.0, 1.0, 0.0, 0.0);
111+
viewMats[2][2] = vec4(0.0, 0.0, 1.0, 0.0);
112+
viewMats[2][3] = vec4(dot(viewMats[2][0].xyz, -camPositions[2]),
113+
dot(viewMats[2][1].xyz, -camPositions[2]),
114+
dot(viewMats[2][2].xyz, -camPositions[2]), 1.0);
115+
116+
viewProjs = mat4[3]( mat4(1.0), mat4(1.0), mat4(1.0) );
117+
viewProjs[0] = camProjMatrix * viewMats[0];
118+
viewProjs[1] = camProjMatrix * viewMats[1];
119+
viewProjs[2] = camProjMatrix * viewMats[2];
120+
}
59121

60-
61-
const mat4 camProjMatrix = mat4(2.0 / voxelGridLength, 0, 0, 0,
62-
0, 2.0 / voxelGridLength, 0, 0,
63-
0, 0, - 2.0 / voxelGridLength, 0,
64-
0, 0, 0, 1);
65-
66-
const vec3 camPositions[3] = vec3[3] ( voxelGridOrigin + vec3(voxelGridLength, voxelGridLength / 2.0, voxelGridLength / 2.0),
67-
voxelGridOrigin + vec3(voxelGridLength / 2.0, voxelGridLength, voxelGridLength / 2.0),
68-
voxelGridOrigin + vec3(voxelGridLength / 2.0, voxelGridLength / 2.0, voxelGridLength) );
69-
70-
71-
const mat4 camViewMatrices[3] = mat4[3](
72-
mat4(
122+
mat4 lookAt(const in vec3 eye, const in vec3 at, const in vec3 up) {
123+
/*
124+
detail::tvec3<T> f = normalize(center - eye);
125+
detail::tvec3<T> u = normalize(up);
126+
detail::tvec3<T> s = normalize(cross(f, u));
127+
u = cross(s, f);
128+
129+
detail::tmat4x4<T> Result(1);
130+
Result[0][0] = s.x;
131+
Result[1][0] = s.y;
132+
Result[2][0] = s.z;
133+
Result[0][1] = u.x;
134+
Result[1][1] = u.y;
135+
Result[2][1] = u.z;
136+
Result[0][2] =-f.x;
137+
Result[1][2] =-f.y;
138+
Result[2][2] =-f.z;
139+
Result[3][0] =-dot(s, eye);
140+
Result[3][1] =-dot(u, eye);
141+
Result[3][2] = dot(f, eye);
142+
return Result;
143+
*/
144+
145+
return mat4(1.0);
146+
}
73147

74148

75149
uint calcProjAxis() {
@@ -91,7 +165,7 @@ uint calcProjAxis() {
91165

92166
void main()
93167
{
94-
168+
init();
95169

96170
uint projAxisIdx = calcProjAxis();
97171

@@ -102,10 +176,10 @@ void main()
102176
vec3(In[i].pos.xy, 0.0) ); // XY-plane
103177

104178
// (TODO) +Z or -Z?
105-
vec3 clipSpacePos = ((In[i].pos - voxelGridOrigin) / maxVoxelGridSize) * 2.0 - 1.0;
179+
vec3 clipSpacePos = ((In[i].pos - voxelGridOrigin) / voxelGridLength) * 2.0 - 1.0;
106180
gl_Position = vec4(clipSpacePos, 1.0);
107181

108-
Out.posVoxelGrid = (In[i].pos - voxelGridOrigin) + (maxVoxelGridSize/2.0); // 0..maxVoxelGridSize
182+
Out.posVoxelGrid = (In[i].pos - voxelGridOrigin) + (voxelGridLength/2.0); // 0..voxelGridLength
109183
Out.normal = In[i].normal;
110184
Out.uv = In[i].uv;
111185

demos/VoxelConeTracing/src/VoxelConeTracing/main.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ void initTex3D(kore::Texture* tex, const ETex3DContent texContent) {
9696
texProps.pixelType = GL_UNSIGNED_BYTE;
9797

9898

99+
99100
// Create data
100101
glm::detail::tvec4<unsigned char> colorValues[VOXEL_GRID_RESOLUTION_X]
101102
[VOXEL_GRID_RESOLUTION_Y]
@@ -183,7 +184,7 @@ void setupVoxelizeTest() {
183184
sceneMgr->getSceneNodesByComponent(COMPONENT_MESH, meshNodes);
184185

185186

186-
/*
187+
///*
187188
ShaderProgramPass* voxelizePass = new ShaderProgramPass;
188189
voxelizePass->setShaderProgram(voxelizeShader);
189190

@@ -245,7 +246,7 @@ void setupVoxelizeTest() {
245246
}
246247

247248
backBufferStage->addProgramPass(voxelizePass);
248-
*/
249+
//*/
249250

250251

251252
// Init 3D texture sampling procedure

0 commit comments

Comments
 (0)