Fairly basic question, but I'm having trouble on finding information on this sort of thing for beginners. I'm just trying to make cubes with different textures on the sides as a starting point. I have downloaded the source code for an 'infinite' cube engine that is similar to minecraft (this particular one is called TechCraft).
I am simply having trouble finding where the textures are actually being applied and how they are being grabbed and when and blah blah. Here is the code I am currently using:
public void Draw(GameTime gameTime, bool underWater)
{
BoundingFrustum viewFrustrum = new BoundingFrustum(Game.Camera.View * Game.Camera.Projection);
_game.GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
_game.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
_game.GraphicsDevice.BlendState = BlendState.Opaque;
_solidBlockEffect.Parameters["World"].SetValue(Matrix.Identity);
_solidBlockEffect.Parameters["View"].SetValue(Game.Camera.View);
_solidBlockEffect.Parameters["Projection"].SetValue(Game.Camera.Projection);
_solidBlockEffect.Parameters["CameraPosition"].SetValue(Game.Camera.Position);
_solidBlockEffect.Parameters["AmbientColor"].SetValue(Color.White.ToVector4());
_solidBlockEffect.Parameters["AmbientIntensity"].SetValue(0.8f);
_solidBlockEffect.Parameters["FogColor"].SetValue(Color.SkyBlue.ToVector4());
_solidBlockEffect.Parameters["FogNear"].SetValue(WorldSettings.FOGNEAR);
_solidBlockEffect.Parameters["FogFar"].SetValue(WorldSettings.FOGFAR);
_solidBlockEffect.Parameters["BlockTexture"].SetValue(_textureAtlas);
foreach (EffectPass pass in _solidBlockEffect.CurrentTechnique.Passes)
{
pass.Apply();
for (int x=0;x<REGIONRATIOWIDTH;x++)
{
for (int y=0;y<REGIONRATIOHEIGHT;y++)
{
for (int z=0;z<REGIONRATIOLENGTH;z++)
{
Region region = _regions[x,y,z];
if (region.BoundingBox.Intersects(viewFrustrum))
{
if (region.Dirty) region.Build();
if (region.SolidVertexBuffer != null)
{
_game.GraphicsDevice.SetVertexBuffer(region.SolidVertexBuffer);
_game.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, region.SolidVertexBuffer.VertexCount / 3);
}
}
}
}
}
}
And here is the SolidBlockEffect class:
float4x4 World;
float4x4 View;
float4x4 Projection;
float4 AmbientColor;
float AmbientIntensity;
float3 CameraPosition;
float FogNear = 250;
float FogFar = 300;
float4 FogColor = {0.5,0.5,0.5,1.0};
Texture BlockTexture;
sampler BlockTextureSampler = sampler_state
{
texture = <BlockTexture>;
magfilter = POINT;
minfilter = POINT;
mipfilter = POINT;
AddressU = WRAP;
AddressV = WRAP;
};
struct VertexShaderInput
{
float4 Position : POSITION0;
float2 TexCoords : TEXCOORD0;
float Shade : TEXCOORD1;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 TexCoords : TEXCOORD0;
float3 CameraView : TEXCOORD1;
float Shade : TEXCOORD2;
float Distance : TEXCOORD3;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
//output.Position.x += RippleAmount * sin(RippleTime + (output.Position.y));
output.Shade = input.Shade;
output.CameraView = normalize(CameraPosition - worldPosition);
output.Distance = length(CameraPosition - worldPosition);
output.TexCoords = input.TexCoords;
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float4 texColor = tex2D(BlockTextureSampler, input.TexCoords);
float4 ambient = AmbientIntensity * AmbientColor;
float fog = saturate((input.Distance - FogNear) / (FogNear-FogFar));
float4 color = texColor * input.Shade * ambient;
return lerp(FogColor, color ,fog);
}
technique BlockTechnique
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
So, I don't know if the texture is being picked from the texture atlas during the effect pass or what. If someone could please shed some light on how this code might be working.
I guess if it helps, my goal is to be able to draw my own texture atlas and tell the code what texture I want a particular cube face to use. And if this isn't enough code please tell me what I need to add!
The source code: https://techcraft.codeplex.com/