DirectX9 3D Quick Pick 3
By SSSA2000
4/15/2005
We temporarily skip, boring index buffering and depth buffering content, first look at how to achieve a thing in 3D space, give yourself a sense of accomplishment.
Just the SDK's wizard is also arranged, huh, let's continue from the wizard, take Tutorial 3 as an example.
This example mainly explains the use of transform matrices to achieve object transformation, learn graphics or linear algebra, it is easy to understand, did not learn, find some information in this area, you can see it.
Let's take a look at this example, compile operation, oh, compared with the front two examples, a refreshing feeling, a triangle rotates around the Y-axis.
Compared with Tutorial 2, there are more than 2 functions: OnResetDevice and Setupmatrices. Among them, Chinese is translated by me, E text is too bad, translation is not good, don't want B4, I am.
Public void OnResetDevice (Object Sender, Eventargs E)
{
Device dev = (device) sender;
// Turn Off Culling, So We See The Front and Back of The Triangle
// Turn off, so we can see the triangles in front and back.
Dev.renderstate.cullmode = Cull.none;
// Turn Off D3D Lighting, Since We are providing Our OWN VERTEX Colors
// Turn off the light because we have provided your own color
Dev.renderstate.lighting = false;
}
Private void setupmatrices ()
{
// for Our World Matrix, We Well Just Rotate The Object About The Y-AXIS.
// set up the rotation matrix to generate 1 Full Rotation (2 * pi radians)
// every 1000 ms. To avoid the loss of precision inherent in Very Highh
// Floating Point Numbers, The System Time Is Moducted by The Rotation
// Period Before Conversion to a Radian Angle.
// In the world coordinates, we want to rotate around Y, establish a rotating matrix to generate a rotation of 360 degrees per second. In order to avoid the loss of the number of bits in the floating point number, we have changed to an arc, forced adjustment system time
Int itime = environment.tickcount% 1000;
FLOAT FANGLE = Itime * (
2.0F * (float) Math.pi) /
1000.0F;
Device.Transform.World = Matrix.RotationY (Fangle);
// SET UP OUR View Matrix. A View Matrix Can Be Defined Given An Eye Point,
// a point to lookat, and a direction for Which Way is up. here, We set the
// Eye Five Units Back Along The Z-Axis and Up Three Units, Look At The
// ORIGIN, AND Define "UP" to be in the y-direction. // Establish a viewing matrix, which can be defined as a viewpoint, just like a camera. Here we are located in the Z-axis-5, Y-axis 3
Device.Transform.View = Matrix.lookatlh (New Vector3
0.0F,
3.0F,
-5.0F), New Vector3 (
0.0F,
0.0F,
0.0F), New Vector3 (
0.0F,
1.0f,
0.0F));
// for the Projection Matrix, We set up a persftive transform (Which
// Transforms Geometry from 3D View Space To 2D ViewPort Space, with
// a atpective divide making Objects smaller in the distance). To build
// a Perpsective Transform, We need the Field of View (1/4 pi is common),
// The Aspect Ratio, And The Near And Far Clipping Planes (Which Define At
// What Distances Geometry Should Be No longer be rendered.
// Among the shooting matrix, we create a perspective transformation (perspective transformation transform 3D space to 2D viewport, resulting in perspective), in order to establish a perspective transformation, we need visual space, (usually 1/4 pi) aspect ratio and far Distance elimination (not rendered in the distance)
Device.Transform.Projection = Matrix.PerspectiveFovlh (FLOAT) Math.pi / 4,
1.0f,
1.0f,
100.0f);
}
All new things are here, less than 10 statements, simple? There are a few points to explain it.
Dev.renderstate.cullmode = Cull.none;
This sentence, maybe many people will be confused, first understand the back of the DX, like reality, we can only see one side of an object at the same time, can't see its back, this is the back. Cull is an enumeration, a total of 3 values:
CounterClockWise3cull Back Faces with counterclockwise Vertices.clockwise2cull Back Faces with clockwise vertices.none1do Not Cull Back Faces.
If we specify the way back, then we can't see the back of the object, so when we turn over, we can't see the turns, so set to none, if not, you can modify it in the program. This sentence can be understood very intuitively.
This example is so much content, of course, you can also change Rotatey to Rotatex, Rotatez, etc., which can be better understood.
Let's take a look at Mesh.
For our beginners, Mesh should be an exciting thing, let's take a look at the introduction: Mesh can be used to store any type of graphic data, but mainly used to encapsulate complex models. The Mesh class also has some methods used to improve rendering object performance. With Mesh, you can read 3D models from external files such as .3DS files, so we can do a model in 3D Max, then read the program, think about it, is a game's prototype is already in your mind? ? The Mesh object also contains a lot of geometric models. Let's take a look at how to use it, because it is more convenient than using the vertices, you will deeply understand.
First we have to build a Mesh object private mesh mesh = NULL;
Then Mesh = Mesh.box (Device,
2.0F,
2.0F,
2.0f); so that we have established a cube with a long-range height of 2, is it simple? If you use the vertex, the fastest way, that is, the index buffer, the depth buffer should also write 8 top points, but also setstreamsource, etc., all this is done in Mesh. . Of course, there are many geometries built into mesh, such as cylindrical, teapot, etc., you can try one by one.
Let's take a look at the core DRAWBOX function:
this is equivalent
Private void Drawbox (Float Yaw, Float Pitch, Float Roll, Float X, Float Y, Float Z)
{
Angle =
0.01F;
Device.Transform.World = Matrix.RotationYawpitchroll (YAW, Pitch, Roll) * Matrix.Translation (X, Y, Z);
Material boxmaterial = new material ();
Boxmaterial.ambient = color.white; // Environmental color
Boxmare.diffuse = color.white; // diffuse reflection
Device.material = boxmaterial;
Mesh.drawsubset (0);
}
First introduce Matrix.RotationYawpitchRoll method, see his original shape:
Public Static Matrix RotationYawpitchroll (float yaw, // offset, the rotation angle float pitch, // slope, the angle float roll // rolling around the X-axis, is an angle of Z,
Materials describes such an attribute. You can specify how objects reflect ambient light and scattering (SPECUSE) light (Specular Highlights) (will discuss it), as well as the object is completely reflected (EMIT) light. Here, a new material is created, its environment color (Note: the color of the ambient color and the ambient light is different, and the scatter color value is set to white. Use white to refer to it reflects all light. Next, we give the material to the Device's Material property, so Direct3D knows that material data is used when rendering.
Here is a little knowledge about light and color: Ambient Color, when it is black, indicates that (ambient light) does not affect the color of the material, when the environment is shallow, it will illuminate the material, and The two colors are mixed, which affects the color of the material. How to have ambient light in the scene, then the color and brightness of these light will control the effect of the environment for the final material color). Change the material with no red ingredients (such as green) will make the object again become black (Note: because the object does not reflect red, the red light is absorbed by the object, and the color containing some red ingredients (Such as gray gray) will make the object deep gray. Mesh will be divided into a series of subsets (allocated in accordance with the size of the property buffer), and use a method called "DrawSubset" to render. Normal elements created using the Mesh class always only one subset of 0-based subsets. So use mesh.drawsubset (0)
Below with all of this example:
Using system;
Using system.drawing;
Using system.collections;
Using system.componentmodel;
Using system.windows.forms;
Using system.data;
Using Microsoft.directX;
Using Microsoft.directx.direct3d;
Namespace Chapter5code
{
///
/// summary description for Form1.
/// summary>
Public Class Form1: System.Windows.Forms.form
{
PRIVATE DEVICE DEVICE = NULL;
PRIVATE MESH MESH = NULL;
///
/// Required Designer Variable.
/// summary>
Private system.componentmodel.Container Components = NULL;
Private float angle =
0.0F;
Public Form1 ()
{
//
// Required for Windows Form Designer Support
//
InitializationComponent ();
This.SetStyle (ControlStyles.allpaintingInwmpaint | Controlstyles.opaque, true);
}
///
/// We Will Initialize Our Graphics Device Here
/// summary>
Public void initializegraphics ()
{
// setur presentation parameters
PresentParameters Presentparams = New Presentparameters ();
Presentparams.windowed = true;
Presentparams.swaPeffect = swapeffect.discard;
Presentparams.autodepthstencilFormat = depthformat.d16;
Presentparams.enableautodepthstencil = true; // Create Our Device
Device = New Device (0, DeviceType.hardware, this, createflags.softwarevertexprocessing, presentparams);
Mesh = Mesh.box (Device,
2.0F,
2.0F,
2.0F);
}
Private void setupcamera ()
{
Device.Transform.Projection = Matrix.Perspectivefovlh (FLOAT) Math.pi / 4, this.width / this.height,
1.0f,
100.0F);
Device.Transform.View = Matrix.lookatlh (New Vector3 (0, 0,
18.0F), New Vector3 (), New Vector3 (0, 1, 0));
Device.renderstate.Ambient = color.darkBlue;
Device.lights [0] .type = lightttype.directional;
Device.lights [0] .diffuse = color.darkBlue;
Device.lights [0] .direction = new vector3 (0, -1, -1);
Device.lights [0] .update ();
Device.lights [0] .enabled = true;
}
Protected Override Void Onpaint (System.Windows.Forms.Painteventargs E)
{
Device.clear (Clearflags.target | Clearflags.zbuffer, Color.CornflowerBlue,
1.0F, 0);
Setupcamera ();
DEVICE.BEGINSCENE ();
// Draw Our Boxes
DrawBox (Angle / (Float) Math.pi, Angle / (Float) Math.pi *
2.0F, Angle / (FLOAT) Math.pi /
4.0F,
0.0F,
0.0F,
0.0F);
// DrawBox (Angle / (Float) Math.pi, Angle / (Float) Math.pi /
2.0F, Angle / (FLOAT) Math.pi *
4.0F,
5.0f,
0.0F,
0.0F);
// DrawBox (Angle / (FLOAT) Math.pi, Angle / (Float) Math.pi *
4.0F, Angle / (FLOAT) Math.pi /
2.0F,
-5.0F,
0.0F,
0.0F);
// DrawBox (Angle / (FLOAT) Math.pi, Angle / (Float) Math.pi *
2.0F, Angle / (FLOAT) Math.pi /
4.0F,
0.0F,
-5.0F,
0.0F);
// DrawBox (Angle / (Float) Math.pi, Angle / (Float) Math.pi /
2.0F, Angle / (FLOAT) Math.pi *
4.0F,
5.0f,
-5.0F,
0.0f);
// DrawBox (Angle / (FLOAT) Math.pi, Angle / (Float) Math.pi *
4.0F, Angle / (FLOAT) Math.pi /
2.0F,
-5.0F,
-5.0F,
0.0f);
// DrawBox (Angle / (Float) Math.pi, Angle / (Float) Math.pi * 2.0F, Angle / (FLOAT) Math.pi /
4.0F,
0.0F,
5.0f,
0.0f);
DrawBox (Angle / (Float) Math.pi, Angle / (Float) Math.pi /
2.0F, Angle / (FLOAT) Math.pi *
4.0F,
5.0f,
5.0f,
0.0f);
DrawBox (Angle / (Float) Math.pi, Angle / (Float) Math.pi *
4.0F, Angle / (FLOAT) Math.pi /
2.0F,
-5.0F,
5.0f,
0.0f);
Device.endscene ();
Device.Present ();
THIS.INVALIDATE ();
}
Private void Drawbox (Float Yaw, Float Pitch, Float Roll, Float X, Float Y, Float Z)
{
Angle =
0.01F;
Device.Transform.World = Matrix.RotationYawpitchroll (YAW, Pitch, Roll) * Matrix.Translation (X, Y, Z);
Material boxmaterial = new material ();
BoxMaterial.Ambient = color.white;
Boxmare.diffuse = color.white;
Device.material = boxmaterial;
Mesh.drawsubset (0);
}
///
/// Clean Up Any Resources Being Used.
/// summary>
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code
///
/// Required Method for Designer Support - Do Not Modify
/// The contents of this method with the code editor.
/// summary>
Private vidinitiRizeComponent ()
{
This.Components = new system.componentmodel.container ();
This.size = new size (800, 600);
THIS.TEXT = "Form1";
}
#ndregion
///
/// The main entry point for the application.
/// summary>
Static void
Main ()
{
Using (Form1 FRM = New Form1 ())
{
// Show Our Form and Initialize Our Graphics Engine
Frm.show ();
frm.initializegraphics ();
Application.run (frm);
}
}
}
}
I don't know if you pay attention to it, I commented on a lot of DrawBox functions, you can also change it, run, you will find a question, just if you draw 9 boxes and you draw a box, rotate The speed is not the same because there is no forced adjustment like the last example. In this example, there are some about lighting. We will talk soon, in fact, from the literal, you can see what it means. Next, we have to read our own model, excited. 2005-4-15