DefiniteIntegral wrote:
Hello. In the images where you are using the number "10" with different colors on different faces, would you mind please posting your source image for the material? I am just interested to see how you did it.
Sure, no problem.
VoxelTexture.material <-- Name doesn't matter for the material. You can set the name of the material in the material [name] loop. That's what you call in your ManualObject or Entity or whatever you are using.
Code:
vertex_program VoxelTexture_VP cg
{
source VoxelTexture.cg
entry_point voxeltexture_vp
profiles vs_1_1
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_2_x ps_2_0
}
material VoxelTexture
{
receive_shadows on
technique
{
pass
{
vertex_program_ref VoxelTexture_VP
{
}
fragment_program_ref VoxelTexture_FP
{
}
texture_unit
{
texture Dirt.png 2d
filtering anisotropic
max_anisotropy 16
}
}
}
}
For the image, I just grabbed the image in the materials/textures folder in the OGRESDK. It's called 10points.png and I just changed the name. It's a nice texture to check out how each face is being drawn.
VoxelTexture.cg <--This name does matter.
Code:
#define VOXELCENTEROFFSET 0.5
void voxeltexture_vp(
float4 inPosition : POSITION,
out float4 outPosition : POSITION,
out float4 outWorldPosition : TEXCOORD0,
uniform float4x4 world,
uniform float4x4 viewProj)
{
outWorldPosition = mul(world, inPosition);
outPosition = mul(viewProj, outWorldPosition);
}
void voxeltexture_fp(
float4 inWorldPosition : TEXCOORD0,
out float4 color : COLOR,
uniform sampler2D texture)
{
float3 worldNormal = cross(ddy(inWorldPosition.xyz), ddx(inWorldPosition.xyz));
worldNormal = normalize(worldNormal);
//Right
if(worldNormal.x > 0.5)
{
color = tex2D(texture, float2((1 - inWorldPosition.z) + VOXELCENTEROFFSET, (1 - inWorldPosition.y) + VOXELCENTEROFFSET));
}
//Left
if(worldNormal.x < -0.5)
{
color = tex2D(texture, float2(inWorldPosition.z + VOXELCENTEROFFSET, (1 - inWorldPosition.y) + VOXELCENTEROFFSET));
}
//Top
if(worldNormal.y > 0.5)
{
color = tex2D(texture, inWorldPosition.xz + VOXELCENTEROFFSET);
}
//Bottom
if(worldNormal.y < -0.5)
{
color = tex2D(texture, float2(inWorldPosition.x + VOXELCENTEROFFSET, (1 - inWorldPosition.z) + VOXELCENTEROFFSET));
}
//Front
if(worldNormal.z > 0.5)
{
color = tex2D(texture, float2(inWorldPosition.x + VOXELCENTEROFFSET, (1 - inWorldPosition.y) + VOXELCENTEROFFSET));
}
//Back
if(worldNormal.z < -0.5)
{
color = tex2D(texture, float2((1 - inWorldPosition.x) + VOXELCENTEROFFSET, (1 - inWorldPosition.y) + VOXELCENTEROFFSET));
}
}
This does not color code the different faces however. I'll talk about that below.
A word of caution here. You can use:
Code:
if(worldNormal.x > 0.0)
if(worldNormal.x < 0.0)
as conditions for the x-axis (and same for y and z). This worked for me. I was running this in DX9 with an ATI card. OpenGL crashes Ogre for me (and not just in this project) so I have no way of knowing.
My partner has an nVidia card, and using 0.0 would cause the textures on the left/right and top/bottom images to look like static. Changing it to 0.5/-0.5 fixed it for him. He was also running in DX as OpenGL is causing problems. For his scenario with the OpenGL trouble it may be some other problem and not related to Ogre at all. But because of that I have no idea if this works for OpenGL. This is to let you and anyone who happens to find this know.
As for colors, inside one of the conditionals that test the normal, you can do something like this:
This is the original:
Code:
color = tex2D(texture, float2((1 - inWorldPosition.x) + VOXELCENTEROFFSET, (1 - inWorldPosition.y) + VOXELCENTEROFFSET));
Change it to:
Code:
float4 col; //More useful to define this outside of the conditional
col = tex2D(texture, float2((1 - inWorldPosition.x) + VOXELCENTEROFFSET, (1 - inWorldPosition.y) + VOXELCENTEROFFSET));
color = col + float4(0.0, 0.0, 0.0, 1.0);
Just replace the first 3 0.0's with your RGB color (last one is A). float4(1.0, 0.0, 0.0, 1.0) would give you red, float4(1.0, 1.0, 0.0, 1.0) would give you yellow and so on. I color coded the different faces when I was testing if I got their orientation right.
Lastly, you can also search through
http://www.thermite3d.org/phpBB3/viewtopic.php?t=77 to find some additional things (like scaling, texture atlas). I based mine off of that with help. I'll end up doing things like those as well but right now I am messing with Libnoise to generate terrain so it will be a while.