Subscribe

RSS Feed (xml)

Powered By

Powered by Blogger

Google
 
xnahelp.blogspot.com

Jumat, 04 April 2008

Texturing 3D Models

Now, let’s replace that texture with one we will load on the fly. We could just modify our
texture resource if we wanted to, but let’s assume that we want to keep that intact and as
we load different asteroids we want some to use that brownish texture and some to use
our newly created texture, on which we will simply remove the color. So let’s fire up our
favorite paint program and open up the .tga file and turn it into a grayscale image. If
needed, the image asteroid1-grey.tga can be taken from the CD in this chapter’s source
code. Let’s add our newly created image into our Solution Explorer (or we can just include
it into our project if we saved the new version in our /Textures/ subfolder from our paint
program).
Add the following code in our LoadGraphicsContent method:
originalAsteroid = content.Load(@”Content\Textures\asteroid1“);
greyAsteroid = content.Load(@”Content\Textures\asteroid1-grey”);
Just like adding our model, we can add our texture asset very easily. Of course, now we
have to actually declare our private member field:
private Texture2D originalAsteroid;
private Texture2D greyAsteroid;
Now we can make a couple of changes to our DrawModel method. We need to add in
another parameter of type Texture2D and call it texture. We also need to actually set our
effect to use that texture if it was passed in, which can be seen in the following code:
if (texture != null)
be.Texture = texture;
This code statement is inside of the inner foreach loop with the rest of the code that sets
the properties of our basic effect that is being used by our model. We are simply checking
to see if null is passed in; if not, we are setting the texture of the effect. As we saw earlier,
the effect is getting applied to the mesh of our model, so we only need to modify our call
to our DrawModel to pass in the new texture. We will go ahead and just create another
asteroid on our screen by replacing our current drawing code with the following:
world = Matrix.CreateTranslation(new Vector3(0, 0, -4000));
DrawModel(ref model, ref world, greyAsteroid);
world = Matrix.CreateTranslation(new Vector3(0, 0, 4000));
DrawModel(ref model, ref world, originalAsteroid);
The first line did not change, but it is added here for readability purposes. The second
statement we are actually passing in is our new texture. The last two statements are
placing another asteroid behind us and resetting the texture to the original one. We can
run this program and spin our camera to see both asteroids being drawn.
120 CHAPTER 6 Loading and Texturing 3D Objects
Now, let’s apply some transformations to our asteroids. We can add some rotation to get
them to do a little more than they are right now. To do this, we just need to replace our
Draw code with the following:
world = Matrix.CreateRotationY(MathHelper.ToRadians(
270.0f * (float)gameTime.TotalGameTime.TotalSeconds)) *
Matrix.CreateTranslation(new Vector3(0, 0, -4000));
DrawModel(ref model, ref world, greyAsteroid);
world = Matrix.CreateRotationY(MathHelper.ToRadians(
45.0f * (float)gameTime.TotalGameTime.TotalSeconds)) *
Matrix.CreateRotationZ(MathHelper.ToRadians(
45.0f * (float)gameTime.TotalGameTime.TotalSeconds)) *
Matrix.CreateTranslation(new Vector3(0, 0, 4000));
DrawModel(ref model, ref world, originalAsteroid);
Even with the code broken to fit on the page, we can still see that we only have four
statements, just like before. The only two that changed are our code that modified the
world matrix. This makes sense because we want to rotate our asteroids. We start by
looking at our first world transformation and can see that we are simply rotating it
around the y axis by 270 degrees * the number of seconds our game has been running.
This effectively makes it continuously render every frame. After rotating we still translate
it like we did before, moving it 4,000 units into our world. The second world transformation
is similar except we are only rotating by 45 degrees instead of 270 degrees. We are
also rotating around the x axis by the same amount. This should give us a decent wobble
effect. Finally we are translating 4,000 units behind us just like before. We can compile
and run our program and see our asteroids are moving (well, rotating in place).

0 komentar: