I'm building my own little "cube voxel" toy, in an attempt to figure it all out. When I went researching how I should go about texturing everything once it all worked I came here and found these threads.
I had to fix a few things in the way I generated my cubes(from a heightmap image). Their size was one big problem, I was using an offset of 8 from the center point for each vertex, but that ended up creating a bunch of fragments -so each time the shader ran on each fragment it would tile the image all over the place. Changing the offset to .5 seems to have worked, and made all the cubes a lot smaller of course.
I had bleeding issues with a .png atlas, so I went and got nvidias legacy atlas creator tool, which runs just fine via wine. It generated a nice .dds that was 4096x256 from the textures I resized.
so the code to generate the color value only needs to offset the r, instead of both the r and g.
However, though it looks much better now, the 6 texture according to the NvidiaComparisonViewer should be an ice texture, and grass is the texture 3 (assuming the first texture is 0). oh hand of course, if I change the matid it falls apart.
Code:
int matid = 6;// pick the 6th texture
float texture_count = 16;
colours = new Ogre::RGBA[nVertices];
{
Ogre::RGBA *pColour = colours;
for(size_t l = 0; l<nVertices;l++) {
rs->convertColourValue(Ogre::ColourValue(matid%texture_count*(1.0/texture_count), 0,0.0,1.0f), pColour++);
}
}
Then I used the last shader jmgr posted with only a few small changes:
Code:
#define ATLAS_WIDTH 4096.0
#define TEXTURE_WIDTH 256.0
#define RATIO TEXTURE_WIDTH / ATLAS_WIDTH
void voxeltexture_vp(
float4 position : POSITION,
float4 tile : COLOR,
out float4 clipPosition : POSITION,
out float4 worldPosition : TEXCOORD0,
out float4 textureAtlasOffset : TEXCOORD1,
uniform float4x4 world,
uniform float4x4 viewProj)
{
worldPosition = mul(world, position);
clipPosition = mul(viewProj, worldPosition);
float tileX = ((tile.r != 0.0) ? (1.0 / tile.r) : 0.0);
textureAtlasOffset = float4(tileX + 0.5f, 0, 0, 0) * RATIO;
}
void voxeltexture_fp(
float4 position : POSITION,
float4 worldPosition : TEXCOORD0,
float4 textureAtlasOffset : TEXCOORD1,
uniform sampler2D texture : TEXUNIT0,
out float4 result : COLOR)
{
float3 worldNormal = cross(ddy(worldPosition.xyz),ddx(worldPosition.xyz));
worldNormal = normalize(worldNormal);
worldPosition -= 0.5;
float2 uv = float2(1.0, 1.0);
if(worldNormal.x > 0.5)
uv = frac(float2(-worldPosition.z, -worldPosition.y));
if(worldNormal.x < -0.5)
uv = frac(float2(-worldPosition.z, worldPosition.y));
if(worldNormal.y > 0.5)
uv = frac(worldPosition.xz);
if(worldNormal.y < -0.5)
uv = frac(float2(-worldPosition.x, worldPosition.z));
if(worldNormal.z > 0.5)
uv = frac(float2(worldPosition.x, -worldPosition.y));
if(worldNormal.z < -0.5)
uv = frac(float2(-worldPosition.x,-worldPosition.y));
textureAtlasOffset += float4(uv * 0.5, 0.0, 0.0) * RATIO;
result = tex2D(texture, textureAtlasOffset.xy);
}
and my material file
Code:
vertex_program VoxelTexture_VP cg
{
source VoxelTexture.cg
entry_point voxeltexture_vp
profiles vs_3_0 vs_2_x vs_2_0 vs_1_1 vp40 vp30 vp20 arbvp1 glsl
default_params
{
param_named_auto world world_matrix
param_named_auto viewProj viewproj_matrix
}
}
fragment_program VoxelTexture_FP cg
{
source VoxelTexture.cg
entry_point voxeltexture_fp
profiles ps_3_x ps_3_0 ps_2_x ps_2_0 ps_1_4 ps_1_3 ps_1_2 ps_1_1 fp40 fp30 fp20 arbfp1 glsl
}
material VoxelTexture
{
receive_shadows on
technique
{
pass
{
vertex_program_ref VoxelTexture_VP
{
}
fragment_program_ref VoxelTexture_FP
{
}
texture_unit
{
texture TextureAtlas.dds 2d
filtering point point point
tex_address_mode wrap
}
}
}
}




Uploaded with
ImageShack.usthis is my first time playing with Cg, so I'm learning a lot from this whole experience, thank heavens for The Cg Tutorial or I'd be completely lost.. now I'm lost with a giant complex map
