Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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; 


        foreach (EffectPass pass in _solidBlockEffect.CurrentTechnique.Passes)
            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.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:

share|improve this question
up vote 1 down vote accepted

Before `myVertexBuffer.SetData(vertices);' is called you need to set the 'TextureCoordinate' for each vertex. You will want to make 9 squares between 0-1 where 0,0 is the TL of you texture and 1,1 is the BR.

Example textcoords for the topleft most square. This would grab the top left 1/9 of a texture. ((0,0),(0,1/3),(1/3,0)),(((0,1/3),(1/3,1/3,(1/3,0)))

You are probably going to have to alter the library you are using.

Edit: You way of aproaching this is a little backwards of reality. You don't fetch textures from an atlas and apply them to a 3d model. The 3d model's verts contain the uv data and pull it from the texture.

share|improve this answer
Hmm... Well, the current code I am showing already works just fine, I'm just trying to understand it. It is currently already using a texture atlas, and the cubes are being rendered properly. I don't see any statements in the code that are performing the operations you have stated... If I can find the code that does something like that, I'll mark this as the answer. Also, I will add the location of the source code to my question. – Colton Jun 18 '13 at 21:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.