My previous post was too long so I decided to double post to make it clearer.
I've made a lot of progress and is only missing one final part.
Here is my new .cg that has worked so far:
Code:
void voxeltexture_vp(
float4 inPosition : POSITION,
float4 inNormal : NORMAL,
out float4 outPosition : POSITION,
out float4 outWorldPosition : TEXCOORD0,
out float4 outWorldNormal : TEXCOORD1,
uniform float4x4 world,
uniform float4x4 viewProj)
{
outWorldPosition = mul(world, inPosition);
outPosition = mul(viewProj, outWorldPosition);
outWorldNormal = inNormal;
}
void voxeltexture_fp(
float4 inWorldPosition : TEXCOORD0,
float4 inWorldNormal : TEXCOORD1,
out float4 color : COLOR,
uniform sampler2D texture)
{
inWorldNormal = normalize(inWorldNormal);
color = tex2D(texture, inWorldPosition.yz);
}
There are several things I want to note:
1. In David Williams' original .cg, it was uniform sampler2D [name] : TEXUNIT0
2. In David Williams' original .cg, under "void voxeltexture_fp" there was another parameter: float4 inPosition : POSITION. This is never referenced inside the FP itself so I didn't keep it.
3. In David Williams' original .cg, he created a float3 named col and used it to store the result of tex2D, and then returned a float4 with (col, 1.0).
4. I am aware that this displays the faces wrong. I'll explain why below.
Here is the intended code. However, Ogre crashes as soon as I add these conditions using inNormal. Ogre log part of this will be pasted after the code.
Code:
void voxeltexture_vp(
float4 inPosition : POSITION,
float4 inNormal : NORMAL,
out float4 outPosition : POSITION,
out float4 outWorldPosition : TEXCOORD0,
out float4 outWorldNormal : TEXCOORD1,
uniform float4x4 world,
uniform float4x4 viewProj)
{
outWorldPosition = mul(world, inPosition);
outPosition = mul(viewProj, outWorldPosition);
outWorldNormal = inNormal;
}
void voxeltexture_fp(
float4 inWorldPosition : TEXCOORD0,
float4 inWorldNormal : TEXCOORD1,
out float4 color : COLOR,
uniform sampler2D texture)
{
inWorldNormal = normalize(inWorldNormal);
if(inWorldNormal.x > 0.5)
{
color = tex2D(texture, inWorldPosition.yz);
}
if(inWorldNormal.x < -0.5)
{
color = tex2D(texture, inWorldPosition.yz);
}
if(inWorldNormal.y > 0.5)
{
color = tex2D(texture, inWorldPosition.xz);
}
if(inWorldNormal.y < -0.5)
{
color = tex2D(texture, inWorldPosition.xz);
}
if(inWorldNormal.z > 0.5)
{
color = tex2D(texture, inWorldPosition.xy);
}
if(inWorldNormal.z < -0.5)
{
color = tex2D(texture, inWorldPosition.xy);
}
}
Here is the Ogre log part:
Code:
02:49:19: OGRE EXCEPTION(3:RenderingAPIException): Cannot assemble D3D9 shader VoxelTexture_FP Errors:
C:\Users\Aura\Documents\Personal Projects\TechDemo\MageGameTechDemo\Debug\memory(29,1): error X5204: Read of uninitialized components(*) in r2: *r/x/0 *g/y/1 *b/z/2 *a/w/3
in D3D9GpuProgram::loadFromSource at ..\..\..\..\..\RenderSystems\Direct3D9\src\OgreD3D9GpuProgram.cpp (line 202)
Does anyone know why this is happening?
I am going to post this on the Ogre forum as well since this isn't PolyVox's doing. I'll update when I get a solution (or not if someone finds out here). This should be helpful for anyone who is looking to do something similar.
Edit: The thread on the Ogre forum:
http://www.ogre3d.org/forums/viewtopic.php?f=2&t=65310Edit 2: Turns out that it is crashing because CubicSurfaceExtractorWithNormals doesn't provide surface normals, only vertex normals. It is better to go with CubicSurfaceExtractor and calculate the surface normals in the shader myself.
What I found is this on PolyVox's wiki:
http://thermite3d.org/dokuwiki/computing_normals_in_a_pixel_shaderSpecifically:
Code:
float3 worldNormal = cross(ddy(inWorldPosition.xyz), ddx(inWorldPosition.xyz));
worldNormal = normalize(worldNormal);
I slapped that in and changed all the checks to check worldNormal instead of inWorldNormal and it is still crashing because worldNormal is blank after that.