My Thoughts on Dragon Age: Origins
I have been nervously awaiting Dragon Age: Origins, I’ve been following all related news and so on for months, so when the release date finally drew near I was super excited. The game launched on the 9th of November here, a monday, so I ended up taking 2 days leave just so me and the game could spend some quality time together. Now before I continue, I do need to state for the record I am a huge Baldur’s Gate / Original Fallout fan, those two series are my top two games of all times, basically that means that any future RPG will be compared to those games, and boy do they set the bar high!
We got the game a few days after the rest of the world so I read through several reviews, most of them giving the game great scores! (“Yay!” I thought, but then remembered that fallout 3 also got great scores and the moment died) Then I read the bit-tech.net review (I honestly value those guys opinions – they are definitely one of the better review sites on the net – well their hardware reviews need work) and in that review they said the game was good but nothing new or special, that it brought nothing to the table and yeh… I was honestly a little ticked off by that since in the last 5 years there have been a lot of games that tried doing the same old stuff and failed miserably, the witcher & mass effect are the only RPGs in the last 5 years that I felt did things right, I dont mind if its the same concepts and stuff, just do it right! I’m so tired of devs being forced to bring new elements to the table and in many cases have those exact elements sink the game.
So how did I find the game?! well I technically finished it twice in the space of 5 days. I really didn’t get much sleep, I think I sunk around 50+ hours into it, and how do i feel about it? Well, I love it!! This game is exactly what I expected and much much more! It is everything baldur’s gate 2 was and more. (Yes,unfortunately the world is smaller, but its understandable since created 3D environments is a lot harder than drawing a few sprites).
I’m not going to go into too much detail regarding the game since there are a million detailed reviews out there to read, simple this will be a brief overview of what I loved/disliked about the game.
What they did right
To start the new character system at first glance looks very simple and scaled down, with not a lot of customization options, so I thought this might be a bad thing then I remembered how much I disliked the D&D 3rd edition rules just for the opposite reason, where there was no real specialization, ie. i could get a lock picking, plate wearing, archer mage who would be terrible at all those thing but the combination was still possible. The new character system in DA is simple and specialized and I love that about it, from when you create the character you can already see the bigger picture, although on the first play through I did make a few mistakes in my character build that made me play through again.
The combat is tactical and involved, its not simple point at the enemy and chill, you need to be constantly on your toes watching what the enemy is doing and responding to it, the whole combat tactics section was awesome, in it you can set up condition that control your characters, for example if you’re magic resistant templar sees a mage on the battlefield that must be his priority target, or if your fighter encounters an enemy with high armor should use a armor piercing ability. There is also a nice feature that when a player attacks and enemy from behind he gets large bonuses to his hit chance and damage, this has been present in other RPGs but it was usually restricted to rogue/thieves and backstabbing, here its open to anyone so character positioning is very important. There is also a heavy reliance on traps and poisons, the poisons/traps available are quite varied and mixing them up in battle is pretty damn awesome, leading an enemy through a grease trap you laid then finishing him off with some crow poison… All characters should have at least one level of poison making so they can make use of the poisons.
The armor/damage system needs special mention, this is probably the first game I cnat think of where armor does not affect a character chance to be hit score, a character’s armor and defense are entirely seperate entities and honestly thats exactly how it needs to be, armor only reduces the amount of damage taken when hit, so it is there to help out low dex characters like fighters that do tend to be hit a lot, and it also allows a mage to wear light robes and and end up being more protected than my fighter wearing the heaviest armor possible. And to counter that each weapon has an armor penetration number, so certain weapon types are better against certain targets.
I could go on and on about how elegant and well implemented the combat system in the game is but i wont
There have been complaints regarding the difficulty but I personally loved it, I can understand that the average console player was probably tearing his hair out during some sections of the game, and that they’re probably not used to having to plan ahead and that the slow pace of combat would probably be frustrating, but that just made the game so much better for me. A lot of the battles that I kept losing over and over ended up being easily won once I changed tactics and I like the idea that the reason I was losing was because I wasn’t thinking.
The character design is excellent, not really much to say in that regard, the character interaction is very mass effect’ish (most recent example) and that’s a good thing, bioware have improved the inter party dialogue with each game they make just making it better and better. The world/mythology they created is also excellent, and very detailed, its really nice to discover a new world. Half the enjoyment of the game for me was reading all the info about the world lying around in books and scrolls. The overall writing quality in the game is excellent and I cant wait to revisit the world they created
Movement is achieved by means of a world map (BG style), I personally prefer this as this allows for more detailed locations than in a free roam game, I’d rather have less unique locations then tons of the same dungeons/subways (cough fallout3/oblivion cough).
The size of the world is a little small, but it still provides you with plenty of characters to meet and areas to explore, even so with the small world it still took me around 35hrs to finish the game the second time round, and that’s with me rushing through through the first half. Granted around 5hours of that was watching loading screens :/
What they did wrong
While I was praising the earlier character and combat systems, they aren’t perfect. The main concern for me was the lack of detailed info on the effects of abilities and spells. Getting told that this ability boosts damage at the cost of defence doesn’t particularly help especially when you don’t know by how much, you end up having to write down the character stats then enable the ability and check the differences. Also there was absolutely no display available for a characters missile defense score or for his attack speed. The lack of any sort of attack speed indicator on weapons is honestly unacceptable, and the fact that certain abilities seem to affect this attack speed with absolutely no mention of it is also unnacceptable (ie. I swear powerful swings slows down the attack rate, but it might just be me imagining things). Also info that’s missing is the level of skill required to lock pick certain items / disarm traps / etc. and most importantly the amount of damage spells do, players are left totally in the dark regarding that – descriptions state: spell does moderate damage over a short period (WHAT THE HELL DOES THAT MEAN – what does moderate mean? moderate at level 5 is different to moderate at level 15).
What I would have liked to see when a player mouse overs a score like attack, is the base attack score, the additional attack score from attributes, then each item and buffs contributions to the score. Its frustrating when your character has 3 or more effects on him and you cant tell what the hell each one does, apart from that it affects a certain score. Also add the damn attack speed on weapon descriptions, I want to clearly see exactly how much faster a long bow is compared to a short bow, I also want to see exactly how much faster enabling rapid fire on an archer is compared to his standard attack. The game combat system is very intricate and detailed and then hiding all the elegant maths from the player is simply not on. I cant imagine that any player would be scared off by the numbers if anything it would probably improve the experience. Currently playing the game feels like you’re driving a car with gauges telling you your battery voltage, oil pressure, tire pressure, outside temperature and a million other non essential things in detail, and then having your speedometer have three sections marked: slow/fast/very fast, not really very useful…
Another problem for me was the loading times, my machine at home is by no means low end or even mid range, its well up in the high end bracket and even so the load times were a bit ridiculous, ranging from 10s to 2minutes for even the smallest areas, and it may be my imagination but they seems to deteriorate over time, starting out reasonably quick and gettign progressively worse over time. Another thing is the the mini transitions between main areas and sub areas in thsoe main areas also take long but it seems like that’s due to some sort of race condition / thread blocking since during each transition I can see my CPU load meters lock at 100% for both cores for a few seconds, once they unlock the loading starts and the transition occurs quickly. So if they can fix that problem the load times should decrease by a fair bit.
Conclusion
I know i barely went into any detail about the game and what its about and so on but like I said there are a ton of other places to check up on that, while like people have said the game isn’t anything revolutionary or new, it’s an old concept done right! Change isn’t always a good thing, take an old concept and improve on it, do it right, take the effort to make a good solid game that people enjoy playing rather than some half assed proof of concept BS or some watered down consoletard game (cough COD:MW2 I’m looking at you cough). It’s also really good to know that there are companies out there that care about PC gamers. Bioware and EA, thank you so much for Dragon Age, this game is by far the game of the year for me!!! You are welcome to my money any time in the future.
Basically this game is Baldur’s Gate 2, just not using the D&D rules and not set in the forgotten realms! If you liked BG then you’ll love this!! Honestly this is a game worth buying so go out and buy it if you haven’t already!!!
October Update! Exciting News!!!
I’ve had a rough few months, and have finally started to recover! In the middle of last year I ended up burning from stress and trying to juggle a million things at once, and I’m finally starting to feel like I’ve recovered. I’ve spent the better part of the day cleaning up the blog layout and the content of several of my popular blog posts just to make it easier to read.
I’ll be writing up a few new tutorials soon! As a sneak peak the upcoming tutorials cover: windows RAW input, how to write a 3D free look camera, an introduction to pathfinding and a tutorial on an efficient implementation of A*.
In other news I’ve had a bit of a career change, I’ll be lecturing the final year and honors level graphics courses, 3D graphics is one of my key interests second only to AI, and this gives me the opportunity to focus on it full time! This also mean that in setting up the course content for next year, a few more DX10 tutorials will pop up
And I’ll finally get the time to do and show some more advanced techniques like bump-mapping, anti-aliasing and maybe even occlusion culling. I’m pretty damn excited!!!
Also in the pipeline is a complete upgrade for my slightly less monstrous machine, once the upgrade is complete it will be back in the realm of monstrous
I cant wait!! I’ll post photos of the work-log and everything once I get all the parts in, I cant believe that I’m forced to import components due to lack of local suppliers or if there is a supplier with stock then I have to deal with absolutely insane prices.
I don’t know how much free time I’ll have in the next few days since I have one last deadline to complete at my current work, but once that is done I’ll write that camera tutorial!
In other news my master’s thesis is going well, I’ve completed my literature survey and have developed a very basic pathfinding framework to test various algorithms with, but the actual writing of the thesis is whats killing me. Writing has never been a strong point of mine, I know what I want to say but I struggle to put it into words and ironically I have a pretty large vocabulary so even that isn’t an issue. I’m never really happy with anything I write, but i guess that’s the nature of being a little bit of a perfectionist.
Downtime
I havent really updated this blog in forever, I’ve been swamped with work. Its not helping matters that my band is also completing our full-length album release so thats also taking up a fair chunk of my time.
The last few months have been crazy, I’ve been reading so much about pathfinding in games and I’ve learnt so much, I’ve started writing my masters thesis but I’m finding it extremely difficult and time consuming, writing has never been a strong point, never mind academic writing. I’ve spent around 2 weeks writing and re-writing and only have 3 completed pages of my introduction.
Its and interesting field and I have a few ideas on extending a few common algorithms to provide faster and better quality results, but as with all things research related there is always the chance that my “optimizations” might end up making things worse, as Adam Savage says “Failure is always an option”.
Looking at the directx 11 specs, tech demos, etc I cant help but feel excited, I kinda just wanna lock myself in a room and play with it
My directx 10 tutorials kinda stopped coming as my workload increased and I’m sorry about that, I’ll try my best to find the time to perhaps write a new tutorial or two.
Anyways I had a bit of time before a meeting at work and realized I havent touched this blog in a while so decided to show it some love
IE8 Brief Performance
so it got released yesterday and there’s a lot of speculation regarding performance, so I just briefly timed the load timed of a few of my favorite sites:
| ie | ff | |
|---|---|---|
| wordpress | 8 | 15 |
| 8.8 | 9.1 | |
| xs forums | 9.6 | 13 |
| cnn.com (uncached) | 18 | 28.2 |
| myspace | 5 | 8 |
| myspace login | 27 | 26.7 |
IE8 seems to do okay, my tests arent super scientific or even done properly, just a quick test between the latest stable FF3 and IE8 on my machine. The memory usage of IE8 seems crazy tho: 60mb for two tabs across 2 processes. Earlier it was sitting at around 210mb for 3 tabs open with 4 processes running.
I’m sure they’ll be some in depth performance reviews in the future, and I’ll be looking forward to it…
My attempt at a DX10 game engine… Name Ideas
So I’ve started with developing the AI test bed for my masters experiments, and since I kinda wanted something that looked nice, I basically started developing a game engine without knowing it
I’ve been working on it for around a week now, and have a very basic renderer and a basic camera system going… The next step will be developing the scene graph and spatial data structures needed for rendering. I’ve been doing so much reading on scene graphs and so one that it’s coing out my ears and yet I’m not any closer to having an idea on a good solution. I could probably do my entire masters on scene graphs and spatial sorting.
Anyways I’m going to discontinue my DX10 tutorials since all the future tutorials will anyways be based off of my engine, so I’m going to start a new series of tutorials on building a very basic dx0 game engine.
The amount of files in the projects are growing and I need to come up wiht a nice name so i can start encapsulating the classes in namespaces, and have a nice uniform naming across the components, since the engine is going to be super super simple i was thinking as using one of the following as the engine name:
- Cimplicity
- basikEngine
- CimplEngine
- SimplEngine
- engineBasix
Any other suggestions?
DirectX 10 Tutorial 5: Basic Meshes
Since my car has been broken for the last two days, I’ve taken off work and have been working on my Masters degree, since part of my Masters involves building a small “game engine” for AI testing, I’ve been doing some more DX10 work, so its convenient for me to quickly slap together a few more tutorials.
Mesh Basics
I covered the basics of indexed buffers and the depth testing in the last tutorial, in this short tut, I’m going to cover the basics of directX meshes. A mesh is a data structure that contains all the vertex and index buffers needed to draw an object. It’s a neater method of drawing objects as we’ll see.
There are four steps to using meshes:
- Create the mesh
- Fill the Mesh with the index and vertex data necessary
- Commit the mesh to the device
- Draw the mesh
So let’s create a new mesh, first things first, we’ll define an ID3DX10Mesh* pointer called pMesh.
//create mesh
if ( FAILED( D3DX10CreateMesh( pD3DDevice, vertexInputLayout, 2, "POSITION", 8, 12, D3DX10_MESH_32_BIT, &pMesh) ) ) return fatalError("Could not create mesh!");
//vertices for a cube
vertex v[8];
v[0] = vertex( D3DXVECTOR3(-1,1,-1), D3DXVECTOR4(1,0,0,1) ); //front top left
v[1] = vertex( D3DXVECTOR3(1,1,-1), D3DXVECTOR4(0,1,0,1) ); //front top right
v[2] = vertex( D3DXVECTOR3(-1,-1,-1), D3DXVECTOR4(0,0,1,1) ); //front bottom left
v[3] = vertex( D3DXVECTOR3(1,-1,-1), D3DXVECTOR4(1,1,0,1) ); //front bottom right
v[4] = vertex( D3DXVECTOR3(-1,1,1), D3DXVECTOR4(1,0,0,1) ); //back top left
v[5] = vertex( D3DXVECTOR3(1,1,1), D3DXVECTOR4(0,1,0,1) ); //back top right
v[6] = vertex( D3DXVECTOR3(-1,-1,1), D3DXVECTOR4(0,0,1,1) ); //back bottom left
v[7] = vertex( D3DXVECTOR3(1,-1,1), D3DXVECTOR4(1,1,0,1) ); //back bottom right
//create indexes for a cube
unsigned int i[36] = { 2,0,3,3,1,0,
3,1,7,7,5,1,
6,4,2,2,0,4,
7,5,6,6,4,5,
0,4,1,1,5,4,
6,2,7,7,3,2 };
//insert data into mesh and commit changes
pMesh->SetVertexData(0, v);
pMesh->SetIndexData(i, 36);
pMesh->CommitToDevice();
Now we use D3DX10CreateMesh to create the mesh, the parameters are the d3d device, the vertex input layout, number of elements in the vertex layout, the name of the element that stores the vertex position, number of vertices, number of faces, mesh flag and finally the output mesh pointer.
There is a little trick here, remember how before we had triangle strips? Here we don’t, we have to specify every single triangle (referred to as a face in the mesh) by hand. So our index list looks a bit different from before.
To add the vertex data and index data, we simply use the mesh SET methods, a mesh can have multiple vertex buffers and so when you set a vertex buffer, you need to specify in which slot you wish to store it. The index data SET method simply takes the index array and the number of indexes in it.
The final step is to commit the mesh, every time you make a change to a mesh you need to commit it to the device before the changes will be taken into effect.
Drawing the Mesh
So how do we draw a mesh?
for( UINT p = 0; p < techDesc.Passes; p++ )
{
//apply technique
pBasicTechnique->GetPassByIndex( p )->Apply( 0 );
pMesh->DrawSubset(0);
}
How simple is that? We use the draw subset method of the mesh interface to draw the mesh, the value given to the method specifies with attribute group to draw, attribute groups for meshes will be covered in a later tutorial, for now just specify 0. And that’s how to create and draw a basic mesh.
I’ve updated the draw code to render a bunch of spinning cubes just for fun:

Source Code
Source code: tutorial5.zip
It’s been a while
It feels like its been forever since I posted on my blog. I’ve been very busy rolling our the new CMS for the department of computer science at the University of Pretoria. The system was a complete rewrite of the legacy system we had in place. That old system was falling apart and was tearing at the seams for the last year. The rewrite took me and the two part time guys at work around 2 and a half months to complete. It’s around 90% complete and sitting at over 17000 lines of code. I’m now taking the next few days off to recoup since I’d been working 65+ hour weeks for the last month.
What are my plans for this year? Well I’m starting my masters, I’m lucky that the masters here is a research masters and involves no coursework. I’ve selected a topic with some help from some developers in the game industry (huge thanks to Chris Jurney), I’ll be doing my masters on pathfinding in destructable environments. I’m also going to continue working on the DPT, and trying to apply it to real world problems (I’m currently busy using it to do automatic alaysis of aerial photos).
I’m going to carry on with my DX10 tutorials, and try and get a good solid background in graphics as well as AI. I’m planning on trying to make a small complete game this year just to have something I can show case. There is also one last pet project that has been on the back burner for over a year now, I’m finally gonna sit down and complete the design document for my idea of a post-apocalyptic RPG.
I’ll hopefully be posting a new dx10 tutorial or my work on aerial photographic analysis within the next week or so!
Bobby – over and out!
Bad start to the new year!
So i just returned from my trip to the USA. I presented a paper at the ICPR (international conference on pattern recognition) conference. The trip was amazing, it’s been my first vacation in close to 8 years. I really managed to unwind. While i was there i saw a gtx260 core 216 video card for $300 which is around $150 cheaper than here and i couldnt resist. Buying the card meant that i could move my 8800gtx to my work machine and have two high end workstations instead of one, so i did.
The card worked beautifully until yesterday when i rebooted my machine and the card was dead! FUCK! So now i have to go through the process of an international RMA with eVGA (thankfully they have that option). Sigh… I cant seem to catch a break
DirectX 10 Tutorial 3 – Textures
So it’s been sometime since the last tutorial and I apologize for that, I’ve been busy wrapping up my exams for my second degree and finishing off a mini thesis for one of my subjects. So now that it’s all over with I‘ve sat down and done a small tutorial on dx10 texturing.
A lot of other tutorials leave texturing for later on in the tutorial but I’m going to do it now because it’s so simple and further illustrates the point of shader programs and what role they play.
Texturing?
So what is texturing? You can think of it like applying wallpaper to a blank wall, you take an image and attach it to an object in your scene. Well that’s not entirely true, what texturing actually does is use an image as a reference for what color an object is. Where before we set the color for each vertex and then let the API automatically interpolate the colors between vertices now we’re giving it an image to sample for those colors.
I’m not going to go into great detail regarding mipmaps and so on since it isn’t really necessary at this point, hopefully I’ll have the time to come back to it at a later stage and even if I don’t google is your friend
Texturing occurs in the pixel shader stage of the pipeline, just before fragments are rasterized and so the bulk of the texturing code (all 1 line of it) needs to be done in HLSL.
There is one last thing I need to cover before I can get into the code, as I said earlier we give a object an image to use as a reference for the colors on the surface defined in the object but how does the API know which pixel in the image to use for a set of world co-ordinates on the surface? Well this is actually quite simple.

If we look at the above picture, for a 128×128 pixel texture which we want to use for the quad on the right. The first we need to do is set up some sort of coordinate system for the texture. We use the letters u and v for the two axes, please notice that v increases in a downwards direction, u and v are known as texture coordinates. This is because in an image, the first pixel is the top left image and the last pixel is the bottom right. Another restriction on this coordinate system is that u and v are real numbers ranging from 0 to 1 where 1 is 100% of the texture dimension. So the texture coordinates u=0.5, v=0.5 will return the center pixel in a texture.
Okay to attaching this texture to an object defined by a set of vertices in world space, we need to add a piece of information to each vertex: the texture coordinate of that vertex in respect to the texture. So for any two vertices, the API will sample the color values of the texture between the texture coordinates specified for each vertex. At any point in the object it will have a texture coordinate, to calculate what pixel to use in the image as the color reference is simple. The pixel coordinates x,y are simply calculated as such: x= round(u*(textureWidth-1)), y= round(v*(textureHeight-1)). Simple huh?
In the above image we want to use the entire texture for the quad and so specify the u,v coordinates accordingly. We also show the corresponding pixel coordinates for a point on the object with u,v coordinates: (0.2,0.3). Okay so that an idiots guide to texturing theory. Let’s dig into the code.
The first thing we need to do is take tutorial 2 and modify it for texturing. We remove the rotation for the triangle and add an extra vertex to generate a quad as in the below image:

The code for the quad is:
v[0] = vertex( D3DXVECTOR3(-1,-1,0), D3DXVECTOR4(1,0,0,1)); v[1] = vertex( D3DXVECTOR3(-1,1,0), D3DXVECTOR4(0,1,0,1)); v[2] = vertex( D3DXVECTOR3(1,-1,0), D3DXVECTOR4(0,0,1,1)); v[3] = vertex( D3DXVECTOR3(1,1,0), D3DXVECTOR4(1,1,0,1));
Loading the Textures
So let’s load our textures, our first step is setting up storage for our textures and a way to pass it through to the shader program:
std::vector<ID3D10ShaderResourceView*> textureSRV; ID3D10EffectShaderResourceVariable* pTextureSR;
Textures are loaded as ID3D10 resources, to which you need to create a view; a view tells DX how to access a particular resource. Since a texture is a shader resource that’s the type of view we need. The second variable is how we pass the texture id to the HLSL shaders.
So how do we load a texture from an image file? Well if you thought it would be a complicated and involved process you’re wrong, it’s a single function call (D3DX10CreateShaderResourceViewFromFile), and the code to load multiple textures into our texture storage is below.
vector<string> filenames;
filenames.push_back("textures/t1.bmp");
filenames.push_back("textures/t2.bmp");
filenames.push_back("textures/t3.bmp");
//load textures
for ( int i=0; i < (int) filenames.size(); i++ )
{
textureSRV.push_back(NULL);
if ( FAILED( D3DX10CreateShaderResourceViewFromFile( pD3DDevice, filenames[i].c_str(), NULL, NULL, &textureSRV[i], NULL ) ) )
{
char err[255];
sprintf_s(err, "Could not load texture: %s!", filenames[i].c_str());
return fatalError( err );
}
}
That function merges two steps, the creation of the texture and the resulting view to it, in one. Okay so now we’ve loaded the texture now what? Well we need to modify our vertex struct to support texture coordinates, texture coordinates have two dimensions, u and v, horizontal and vertical respectively. To attach a texture to a polygon you need to specify what part of the texture you want to put over the polygon.
struct vertex
{
D3DXVECTOR3 pos;
D3DXVECTOR4 color;
D3DXVECTOR2 texCoord;
}
We use a 2d vector to specify the u,v coordinates for each vertex. So this also means the code for the qaud has changed to:
v[0] = vertex( D3DXVECTOR3(-1,-1,0),D3DXVECTOR4(1,0,0,1),D3DXVECTOR2(0.0f, 1.0f) ); v[1] = vertex( D3DXVECTOR3(-1,1,0),D3DXVECTOR4(0,1,0,1),D3DXVECTOR2(0.0f, 0.0f) ); v[2] = vertex( D3DXVECTOR3(1,-1,0),D3DXVECTOR4(0,0,1,1),D3DXVECTOR2(1.0f, 1.0f) ); v[3] = vertex( D3DXVECTOR3(1,1,0),D3DXVECTOR4(1,1,0,1),D3DXVECTOR2(1.0f, 0.0f) );
Since we changed the vertex struct we also need to modify the inputlayout accordingly, remember that the input layout tells DX what each vertex looks like. All we need to change is the input_element_desc for the input layout:
D3D10_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D10_INPUT_PER_VERTEX_DATA, 0 }
};
Last step is to tell the API which texture to which just before we draw an object. Since in most basic cases we will only be using a single texture (multi-texturing comes later) we only need a single texture shader resource variable which we just update to the texture we want.
//set texture pTextureSR->SetResource( textureSRV[textureIndex] );
Okay so now if we run the program we get… The exact same thing as before. Why?! Well this is obvious since we havent touched the pixel shader program at all.
Texturing using the Pixel Shader
So lets just tweak the shader program (basicEffect.fx), first thing we need to do is specify a texture variable for the shader as such:
Texture2D tex2D;
Then we create a samplerState, remember when I said earlier that we sample the texture to get a color at a specific set of u,v coordinates, a texture resource resource in HLSL has a sample method that samples that texture and returns the value at a specific point, now the way in which it sample that texture is done via the sampleState object. This object sets all the parameters for the default sampler. A very basic samplerState is show below:
SamplerState linearSampler
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
It has three elements: a filter which specifies how the sampling is to be done over the texture, in our case we’re just doing a basic linear sampling (all the sampling states and their descriptions are available in the SDK docs, the addressU and addressV specify how to handle u and v values that lie outside the 0 to 1 range.
Remember how I said that u and v are in the range 0 to 1, well that’s not exactly true, sometimes to want to texture an object using 4 smaller versions of a texture rather than stretching the texture to fit the the object. The below image shows what happens for a few common addressU , addressV values:

There are other state variables for the samplerState object, once again all the info necessary is in the sdk docs.
Now we need to modify the vs_input and ps_input structs to handle the extra 2d texcoord variable:
struct VS_INPUT
{
float4 Pos : POSITION;
float4 Color : COLOR;
float2 Tex : TEXCOORD;
};
struct PS_INPUT
{
float4 Pos : SV_POSITION;
float4 Color : COLOR;
float2 Tex : TEXCOORD;
};
And the vector and pixel shaders accordingly. The only difference in the pixel shader is that for texturing we need to return the sampled color and not the vertex color. We use the sample method on the texture object , the sampler state we defined earlier and the texture coordinates we specified earlier.
PS_INPUT VS( VS_INPUT input )
{
PS_INPUT output;
output.Pos = mul( input.Pos, World );
output.Pos = mul( output.Pos, View );
output.Pos = mul( output.Pos, Projection );
output.Color = input.Color;
output.Tex = input.Tex;
return output;
}
float4 textured( PS_INPUT input ) : SV_Target
{
return tex2D.Sample( linearSampler, input.Tex );
}
float4 noTexture( PS_INPUT input ) : SV_Target
{
return input.Color;
}
We also create two new techniques “full” and “texturing disabled”:
technique10 full
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, textured() ) );
}
}
technique10 texturingDisabled
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, noTexture() ) );
}
}
Then we load these two techniques exactly as we did in the previous tutorial. If we compile and run the program we’re presented with a now textured quad.

If we change the texture co-ordinates to :
v[0] = vertex( D3DXVECTOR3(-1,-1,0), D3DXVECTOR4(1,0,0,1), D3DXVECTOR2(0.0f, 2.0f) ); v[1] = vertex( D3DXVECTOR3(-1,1,0), D3DXVECTOR4(0,1,0,1), D3DXVECTOR2(0.0f, 0.0f) ); v[2] = vertex( D3DXVECTOR3(1,-1,0), D3DXVECTOR4(0,0,1,1), D3DXVECTOR2(2.0f, 2.0f) ); v[3] = vertex( D3DXVECTOR3(1,1,0), D3DXVECTOR4(1,1,0,1), D3DXVECTOR2(2.0f, 0.0f) );
The result is this:

You can download the source code for this tutorial below. In the code you’ll notice that I add a method to swap the texture on the quad by modifying the textureIndex when I set the texture and to disable texturing by using the “texturingDisabled” technique instead of “Full”. Take a look at the wndProc method to see the controls and how they work.
I hope you’ve enjoyed this tutorial, I’ve already started working on the 4th one which will be about meshes and index buffers. I’m going to slowly write the turorials and build upon each one towards the final goal of rendering a field of thousands of blades of waving grass. I feel its pretty pointless to show you all the section seperately without explaining how they all link up together.
Source Code
Download the visual studio 2008 project: dxTutorial3.zip