carl granberg

Member
  • Content count

    48
  • Joined

  • Last visited

Community Reputation

56 Neutral

About carl granberg

  • Rank
    Member
  1. Make cube

    Hi everybody,I had a problem want to someguys show help,you know I working with XNA primitive,I want to build a cube with six side,I already done it,but it can not rotate along with axes(X,y,z),I don't know is I should use orbit Rotate or just rotate?show me some code please!as soon as answer me!
  2. Making an airplane

    Hi body,here I'm deal a difficult problem with my XNA programm,I'm going to let a ariplane that is form by XNA primitive to following along the Y and Z axis,but it can not change to the propely direction,and even the propeller is always far from it,does someone knowing the slove way? using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; using CameraViewer; namespace MGHGame { /// <summary> /// This is the driving class for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { //------------------------------------------------------------ // C L A S S L E V E L D E C L A R A T I O N S //------------------------------------------------------------ // constant definitions private const float BOUNDARY = 16.0f; // accesses drawing methods and properties GraphicsDeviceManager graphics; // handle mouse on the PC #if !XBOX MouseState mouse; #endif // for loading and drawing 2D images on the game window SpriteBatch spriteBatch; // load and access PositionColor.fx shader private Effect positionColorEffect; // shader object private EffectParameter positionColorEffectWVP; // to set display matrix for window // load and access Texture.fx shader private Effect textureEffect; // shader object private EffectParameter textureEffectWVP; // cumulative matrix w*v*p private EffectParameter textureEffectImage; // texture parameter // camera private Camera cam = new Camera(); // vertex types and buffers private VertexDeclaration positionColor; private VertexDeclaration positionColorTexture; // ground vertices and texture VertexPositionColorTexture[] groundVertices = new VertexPositionColorTexture[4]; private Texture2D grassTexture; VertexPositionColor[] airplaneVertices = new VertexPositionColor[3]; VertexPositionColor[] propellerVertices = new VertexPositionColor[4]; float propellerSpin; Vector3 speed; Vector3 airplanePosition = new Vector3(0.0f, 0.75f, -8.0f); /// <summary> /// Initializes: -GraphicsDeviceManager object for drawing /// -ContentManager object for loading media /// </summary> public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } /// <summary> /// This method is called when the program begins to set game application /// properties such as status bar title and draw mode. It initializes the /// camera viewer projection, vertex types, and shaders. /// </summary> private void InitializeBaseCode() { // set status bar in PC Window (there is none for the Xbox 360) Window.Title = "Microsoft?XNA Game Studio Creator's Guide, Second Edition"; // see both sides of objects drawn graphics.GraphicsDevice.RenderState.CullMode = CullMode.None; // set camera matrix cam.SetProjection(Window.ClientBounds.Width, Window.ClientBounds.Height); // initialize vertex types positionColor = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColor.VertexElements); positionColorTexture = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColorTexture.VertexElements); // load PositionColor.fx and set global params positionColorEffect = Content.Load<Effect>("Shaders\\PositionColor"); positionColorEffectWVP = positionColorEffect.Parameters["wvpMatrix"]; // load Texture.fx and set global params textureEffect = Content.Load<Effect>("Shaders\\Texture"); textureEffectWVP = textureEffect.Parameters["wvpMatrix"]; textureEffectImage = textureEffect.Parameters["textureImage"]; } private void InitializeAirplaneBody() { Vector3 position; Color color = Color.Orange; position = new Vector3(0.0f, 0.0f, 0.0f); // lower front airplaneVertices[0] = new VertexPositionColor(position, color); position = new Vector3(0.5f, 0.5f, 0.0f); // top back airplaneVertices[1] = new VertexPositionColor(position, color); position = new Vector3(0.5f, -0.0f, 0.0f); // lower back airplaneVertices[2] = new VertexPositionColor(position, color); } private void InitializePropeller() { Vector3 position; Color color = Color.LightBlue; position = new Vector3(-0.5f, 0.05f, 0.0f); // top left propellerVertices[0] = new VertexPositionColor(position, color); position = new Vector3(-0.5f, -0.05f, 0.0f);// lower left propellerVertices[1] = new VertexPositionColor(position, color); position = new Vector3(0.5f, 0.05f, 0.0f); // top right propellerVertices[2] = new VertexPositionColor(position, color); position = new Vector3(0.5f, -0.05f, 0.0f); // lower right propellerVertices[3] = new VertexPositionColor(position, color); } void InitializeSpeed() { Random randomNumber = new Random(); speed.Y = -1.0f - randomNumber.Next(3); speed.Z = -1.0f - randomNumber.Next(3); } /// <summary> /// Set vertices for rectangular surface that is drawn using a triangle strip. /// </summary> private void InitializeGround(){ const float BORDER = BOUNDARY; Vector2 uv = new Vector2(0.0f, 0.0f); Vector3 pos = new Vector3(0.0f, 0.0f, 0.0f); Color color = Color.White; // top left uv.X= 0.0f; uv.Y= 0.0f; pos.X=-BORDER; pos.Y=0.0f; pos.Z=-BORDER; groundVertices[0] = new VertexPositionColorTexture(pos, color, uv); // bottom left uv.X= 0.0f; uv.Y=10.0f; pos.X=-BORDER; pos.Y=0.0f; pos.Z= BORDER; groundVertices[1] = new VertexPositionColorTexture(pos, color, uv); // top right uv.X=10.0f; uv.Y= 0.0f; pos.X= BORDER; pos.Y=0.0f; pos.Z=-BORDER; groundVertices[2] = new VertexPositionColorTexture(pos, color, uv); // bottom right uv.X=10.0f; uv.Y=10.0f; pos.X= BORDER; pos.Y=0.0f; pos.Z= BORDER; groundVertices[3] = new VertexPositionColorTexture(pos, color, uv); } /// <summary> /// Executes set-up routines when program begins. /// </summary> protected override void Initialize() { InitializeBaseCode(); InitializeGround(); InitializeAirplaneBody(); InitializePropeller(); InitializeSpeed(); base.Initialize(); } /// <summary> /// Draws colored surfaces with PositionColor.fx shader. /// </summary> /// <param name="primitiveType">Object type drawn with vertex data.</param> /// <param name="vertexData">Array of vertices.</param> /// <param name="numPrimitives">Total primitives drawn.</param> private void PositionColorShader(PrimitiveType primitiveType, VertexPositionColor[] vertexData, int numPrimitives){ positionColorEffect.Begin(); // begin using PositionColor.fx positionColorEffect.Techniques[0].Passes[0].Begin(); // set drawing format and vertex data then draw primitive surface graphics.GraphicsDevice.VertexDeclaration = positionColor; graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>( primitiveType, vertexData, 0, numPrimitives); positionColorEffect.Techniques[0].Passes[0].End(); positionColorEffect.End(); // stop using PositionColor.fx } /// <summary> /// Draws textured primitive objects using Texture.fx shader. /// </summary> /// <param name="primitiveType">Object type drawn with vertex data.</param> /// <param name="vertexData">Array of vertices.</param> /// <param name="numPrimitives">Total primitives drawn.</param> private void TextureShader(PrimitiveType primitiveType, VertexPositionColorTexture[] vertexData, int numPrimitives){ textureEffect.Begin(); // begin using Texture.fx textureEffect.Techniques[0].Passes[0].Begin(); // set drawing format and vertex data then draw surface graphics.GraphicsDevice.VertexDeclaration = positionColorTexture; graphics.GraphicsDevice.DrawUserPrimitives <VertexPositionColorTexture>( primitiveType, vertexData, 0, numPrimitives); textureEffect.Techniques[0].Passes[0].End(); textureEffect.End(); // stop using Textured.fx } private void DrawAirplaneBody() { // 1: declare matrices Matrix world, translation, rotationY; // 2: initialize matrices translation = Matrix.CreateTranslation(airplanePosition); rotationY = Matrix.CreateRotationX(RotationAngle()); // 3: build cumulative world matrix using I.S.R.O.T. sequence // identity, scale, rotate, orbit(translate & rotate), translate world = translation; // 4: set shader parameters positionColorEffectWVP.SetValue(world * cam.viewMatrix * cam.projectionMatrix); // 5: draw object - primitive type, vertices, total primitives PositionColorShader(PrimitiveType.TriangleStrip, airplaneVertices, 1); } /// <summary> /// Triggers drawing of ground with texture shader. /// </summary> private void DrawGround() { // 1: declare matrices Matrix world, translation; // 2: initialize matrices translation = Matrix.CreateTranslation(0.0f, 0.0f, 0.0f); // 3: build cumulative world matrix using I.S.R.O.T. sequence // identity, scale, rotate, orbit(translate & rotate), translate world = translation; // 4: set shader parameters textureEffectWVP.SetValue(world * cam.viewMatrix * cam.projectionMatrix); textureEffectImage.SetValue(grassTexture); // 5: draw object - primitive type, vertex data, # primitives TextureShader(PrimitiveType.TriangleStrip, groundVertices, 2); } /// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // create SpriteBatch object for drawing animated 2D images spriteBatch = new SpriteBatch(GraphicsDevice); // load texture grassTexture = Content.Load<Texture2D>("Images\\grass"); } /// <summary> /// UnloadContent will be called once per game and is the place to unload /// all content. /// </summary> protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <summary> /// Updates camera viewer in forwards and backwards direction. /// </summary> float Move() { KeyboardState kb = Keyboard.GetState(); GamePadState gp = GamePad.GetState(PlayerIndex.One); float move = 0.0f; const float SCALE = 1.50f; // gamepad in use if (gp.IsConnected) { // left stick shifted left/right if (gp.ThumbSticks.Left.Y != 0.0f) move = (SCALE * gp.ThumbSticks.Left.Y); } // no gamepad - use UP&DOWN or W&S else { #if !XBOX if (kb.IsKeyDown(Keys.Up) || kb.IsKeyDown(Keys.W)) move = 1.0f; // Up or W - move ahead else if (kb.IsKeyDown(Keys.Down) || kb.IsKeyDown(Keys.S)) move = -1.0f; // Down or S - move back #endif } return move; } /// <summary> /// Updates camera viewer in sideways direction. /// </summary> float Strafe() { KeyboardState kb = Keyboard.GetState(); GamePadState gp = GamePad.GetState(PlayerIndex.One); // using gamepad leftStick shifted left / right for strafe if (gp.IsConnected) { if (gp.ThumbSticks.Left.X != 0.0f) return gp.ThumbSticks.Left.X; } // using keyboard - strafe with Left&Right or A&D else if (kb.IsKeyDown(Keys.Left) || kb.IsKeyDown(Keys.A)) return -1.0f; // strafe left else if (kb.IsKeyDown(Keys.Right) || kb.IsKeyDown(Keys.D)) return 1.0f; // strafe right return 0.0f; } /// <summary> /// Changes camera viewing angle. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> Vector2 ChangeView(GameTime gameTime) { const float SENSITIVITY = 250.0f; const float VERTICAL_INVERSION = -1.0f; // vertical view control // negate to reverse // handle change in view using right and left keys KeyboardState kbState = Keyboard.GetState(); int widthMiddle = Window.ClientBounds.Width/2; int heightMiddle = Window.ClientBounds.Height/2; Vector2 change = Vector2.Zero; GamePadState gp = GamePad.GetState(PlayerIndex.One); if (gp.IsConnected == true) // gamepad on PC / Xbox { float scaleY = VERTICAL_INVERSION*(float) gameTime.ElapsedGameTime.Milliseconds/50.0f; change.Y = scaleY * gp.ThumbSticks.Right.Y * SENSITIVITY; change.X = gp.ThumbSticks.Right.X * SENSITIVITY; } else { // use mouse only (on PC) #if !XBOX float scaleY = VERTICAL_INVERSION*(float) gameTime.ElapsedGameTime.Milliseconds/100.0f; float scaleX = (float)gameTime.ElapsedGameTime.Milliseconds/400.0f; // get cursor position mouse = Mouse.GetState(); // cursor not at center on X if (mouse.X != widthMiddle) { change.X = mouse.X - widthMiddle; change.X /= scaleX; } // cursor not at center on Y if (mouse.Y != heightMiddle) { change.Y = mouse.Y - heightMiddle; change.Y /= scaleY; } // reset cursor back to center Mouse.SetPosition(widthMiddle, heightMiddle); #endif } return change; } void UpdateAirplanePosition(GameTime gameTime) { // change corresponding speed if beyond world? X and Z boundaries if (airplanePosition.Y > BOUNDARY || airplanePosition.Y < -BOUNDARY) speed.X *= -1.0f; if (airplanePosition.Z > BOUNDARY || airplanePosition.Z < -BOUNDARY) speed.Z *= -1.0f; // increment position by speed * time scale between frames float timeScale = gameTime.ElapsedGameTime.Milliseconds/1000.0f; airplanePosition.Y -= speed.Y * timeScale; airplanePosition.Z += speed.Z * timeScale; } float RotationAngle() { return (float)Math.Atan2((double)speed.Y, (double)speed.Z); } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit KeyboardState kbState = Keyboard.GetState(); if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || kbState.IsKeyDown(Keys.Escape)) { this.Exit(); } // update camera cam.SetFrameInterval(gameTime); cam.Move(Move()); cam.Strafe(Strafe()); cam.SetView(ChangeView(gameTime)); UpdateAirplanePosition(gameTime); base.Update(gameTime); } private void DrawPropeller(GameTime gameTime) { // 1: declare matrices Matrix world, translation, orbitTranslate, orbitRotateY, rotationZ; // 2: initialize matrices // continous rotation - restrict it between 0 and 2pi propellerSpin += gameTime.ElapsedGameTime.Milliseconds/50.0f; propellerSpin = propellerSpin % (MathHelper.Pi * 2.0f); rotationZ = Matrix.CreateRotationZ(propellerSpin); orbitTranslate = Matrix.CreateTranslation(0.0f, -0.35f, 0.5f); orbitRotateY = Matrix.CreateRotationX(RotationAngle()); translation = Matrix.CreateTranslation(airplanePosition.X + 0.4f,airplanePosition.Y + 0.1f, airplanePosition.Z); // 3: build cumulative world matrix using I.S.R.O.T. sequence // identity, scale, rotate, orbit(translate & rotate), translate world = rotationZ * orbitTranslate * orbitRotateY * translation; // 4: set shader parameters positionColorEffectWVP.SetValue(world * cam.viewMatrix * cam.projectionMatrix); // 5: draw object - primitive type, vertices, # of primitives PositionColorShader(PrimitiveType.TriangleStrip, propellerVertices, 2); } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); DrawGround(); DrawAirplaneBody(); DrawPropeller(gameTime); base.Draw(gameTime); } } }
  3. click mesh code

    A member of Game.dev net share me some code show below: [b][color="#808080"]bool[/color][/b] CPolygon::Pic( [b][color="#808080"]HWND[/color][/b] m_hWnd, [b][color="#808080"]int[/color][/b] Width, [b][color="#808080"]int[/color][/b] Height, [b][color="#808080"]float[/color][/b] &fRetDist ) { D3DXVECTOR3 vPickRayDir; D3DXVECTOR3 vDir; D3DXVECTOR3 vPickRayOrig; D3DXMATRIXA16 matProj; pd3d_Device->GetTransform( D3DTS_PROJECTION, &matProj ); POINT ptCursor; GetCursorPos( &ptCursor ); ScreenToClient( m_hWnd, &ptCursor ); [color="#008200"]// Compute the vector of the pick ray in screen space [/color] D3DXVECTOR3 v; v.x = ( ( ( 2.0f * ptCursor.x ) / Width ) - 1 ) / matProj._11; v.y = -( ( ( 2.0f * ptCursor.y ) / Height ) - 1 ) / matProj._22; v.z = 1.0f; [color="#008200"]// Get the inverse of the composite view and world matrix [/color] D3DXMATRIXA16 matView, matWorld, m; pd3d_Device->GetTransform( D3DTS_VIEW, &matView ); pd3d_Device->GetTransform( D3DTS_WORLD, &matWorld ); m = matWorld * matView; D3DXMatrixInverse( &m, NULL, &m ); [color="#008200"]// Transform the screen space pick ray into 3D space [/color] vPickRayDir.x = v.x*m._11 + v.y*m._21 + v.z*m._31; vPickRayDir.y = v.x*m._12 + v.y*m._22 + v.z*m._32; vPickRayDir.z = v.x*m._13 + v.y*m._23 + v.z*m._33; D3DXVec3Normalize(&vDir,&vPickRayDir); vPickRayOrig.x = m._41; vPickRayOrig.y = m._42; vPickRayOrig.z = m._43; [b][color="#808080"]BOOL[/color][/b] bHitItersect; [b][color="#808080"]DWORD[/color][/b] dwFace; [b][color="#808080"]FLOAT[/color][/b] fBary1, fBary2, fDist; D3DXIntersect(m_pMeshPolygon, &vPickRayOrig, &vDir, &bHitItersect, &dwFace, &fBary1, &fBary2, &fDist, NULL, NULL); [b][color="#006699"]if[/color][/b]( bHitItersect ) { fRetDist = fDist; [b][color="#006699"]return[/color][/b] [b][color="#006699"]true[/color][/b]; } [b][color="#006699"]return[/color][/b] [b][color="#006699"]false[/color][/b]; } I have a similiar function like this and I use it in my projects,but it in my projects,this function work incorrect,actually I only click one mesh,but it reply me I click all the mesh In the programm,how do you think this problem?
  4. when mouse click the mesh

    did someone really don't know why,I have no any way to fix the problem,unless someone share me example and comment very detail.
  5. when mouse click the mesh

    did someone can show me a full example ,all the mesh in the example must created from a same file.the problem show above can not occur in my projects
  6. Hey guys,I reading the book"Programming Role playing games with Directx",I found a few lines of code that show on below BOOL cApp::MouseClickMesh(cMesh *Mesh) { D3DXVECTOR3 vecRay, vecDir; // Ray’s position and direction D3DXVECTOR3 v; // Temporary vector D3DXMATRIX matProj, matView; // Projection and view matrices D3DXMATRIX m; // Temporary matrix // Get the current projection and view transformations m_Graphics.GetDeviceCOM()->GetTransform(D3DTS_PROJECTION, \ &matProj); m_Graphics.GetDeviceCOM()->GetTransform(D3DTS_VIEW, \ &matView); // Inverse the view matrix D3DXMatrixInverse(&m, NULL, &matView); // Read mouse coordinates (to prepare for reading) //m_Mouse.Read(); // Compute the vector of the pick ray in screen space v.x = (((2.0f * m_Mouse.GetXPos()) / m_Graphics.GetWidth()) - \ 1) / matProj._11; v.y = -(((2.0f * m_Mouse.GetYPos()) / m_Graphics.GetHeight()) - \ 1) / matProj._22; v.z = 1.0f; // Transform the screen space ray vecRay.x = m._41; vecRay.y = m._42; vecRay.z = m._43; vecDir.x = v.x*m._11 + v.y*m._21 + v.z*m._31; vecDir.y = v.x*m._12 + v.y*m._22 + v.z*m._32; vecDir.z = v.x*m._13 + v.y*m._23 + v.z*m._33; // Mesh = pre-loaded cMesh object ID3DXMesh *pMesh; BOOL Hit; DWORD Face; float u, V, Dist; // Get pointer to cMesh ID3DXMesh object pMesh = Mesh->GetParentMesh()->m_Mesh; // call above code to get ray vectors and call intersect D3DXIntersect(pMesh, &vecRay, &vecDir, &Hit, &Face, \ &u, &V, &Dist, NULL, NULL); // If Hit is TRUE, then user clicked on mesh if(Hit == true && m_Mouse.GetButtonState(MOUSE_LBUTTON ) == true) return true; return false; } The book's author says thease code will use to do a collision test check did mouse click the dest mesh.I use it to my projects.I build a room use a x file,and I rollout many mesh for it,but when the programm has running,this function reply me a information says I click all mesh comprise the room.but I just click one mesh,why?I put a break point to that statement if(Hit == true && m_Mouse.GetButtonState(MOUSE_LBUTTON ) == true),then debug again,the hit is always true as long as I click the left button on mouse and I run the function ,even I just click one mesh,have you encounter this scenario,if so please help me find why cause the result.
  7. Role playing code

    There is some code experts from jim adams's book Programming Role playing games if(Size > m_MaxSize && Num > m_MaxPolygons) { for(i=0;i<(unsigned long)((m_TreeType==QUADTREE)?4:8);i++) { XOff = (((i % 2) < 1) ? -1.0f : 1.0f) * (Size / 4.0f); ZOff = (((i % 4) < 2) ? -1.0f : 1.0f) * (Size / 4.0f); YOff = (((i % 8) < 4) ? -1.0f : 1.0f) * (Size / 4.0f); // See if any polygons in new node bounding box if(CountPolygons(XPos+XOff,YPos+YOff,ZPos+ZOff,Size/2.0f)) { Node->Nodes[i] = new sNode(); // Create new child node // Sort the polygons with the new child node SortNode(Node->Nodes[i],XPos+XOff,YPos+YOff,ZPos+ZOff,Size/2.0f); } } does someone undertstand why we caculate the Xoff and Yoff and ZOff as this way,try to explain that code very detail don't use the depth math than I can undertstand !
  8. Role games code

    There is some code experts from jim adams's book Programming Role playing games if(Size > m_MaxSize && Num > m_MaxPolygons) { for(i=0;i<(unsigned long)((m_TreeType==QUADTREE)?4:8);i++) { XOff = (((i % 2) < 1) ? -1.0f : 1.0f) * (Size / 4.0f); ZOff = (((i % 4) < 2) ? -1.0f : 1.0f) * (Size / 4.0f); YOff = (((i % 8) < 4) ? -1.0f : 1.0f) * (Size / 4.0f); // See if any polygons in new node bounding box if(CountPolygons(XPos+XOff,YPos+YOff,ZPos+ZOff,Size/2.0f)) { Node->Nodes[i] = new sNode(); // Create new child node // Sort the polygons with the new child node SortNode(Node->Nodes[i],XPos+XOff,YPos+YOff,ZPos+ZOff,Size/2.0f); } } does someone undertstand why we caculate the Xoff and Yoff and ZOff as this way,try to explain that code very detail don't use the depth math than I can undertstand !
  9. Developing a advanced 3D game engine

    Your guys don't know the expalin of thease code is so rude for me,I read that section again again,but no words can give me a way to slove it understand it!so don,t delay my time,just to explain it that is all!
  10. if you read the book programming role playing games,you must remember the function as below: if(Size > m_MaxSize && Num > m_MaxPolygons) { for(i=0;i<(unsigned long)((m_TreeType==QUADTREE)?4:8);i++) { if(Size > m_MaxSize && Num > m_MaxPolygons) { for(i=0;i<(unsigned long)((m_TreeType==QUADTREE)?4:8);i++) { XOff = (((i % 2) < 1) ? -1.0f : 1.0f) * (Size / 4.0f); ZOff = (((i % 4) < 2) ? -1.0f : 1.0f) * (Size / 4.0f); YOff = (((i % 8) < 4) ? -1.0f : 1.0f) * (Size / 4.0f); // See if any polygons in new node bounding box if(CountPolygons(XPos+XOff,YPos+YOff,ZPos+ZOff,Size/2.0f)) { Node->Nodes[i] = new sNode(); // Create new child node // Sort the polygons with the new child node SortNode(Node->Nodes[i],XPos+XOff,YPos+YOff,ZPos+ZOff,Size/2.0f); } } // See if any polygons in new node bounding box if(CountPolygons(XPos+XOff,YPos+YOff,ZPos+ZOff,Size/2.0f)) { Node->Nodes[i] = new sNode(); // Create new child node // Sort the polygons with the new child node SortNode(Node->Nodes[i],XPos+XOff,YPos+YOff,ZPos+ZOff,Size/2.0f); } } Ok so jog your mind what 's mean with thease code?: if(Size > m_MaxSize && Num > m_MaxPolygons) { for(i=0;i<(unsigned long)((m_TreeType==QUADTREE)?4:8);i++) { XOff = (((i % 2) < 1) ? -1.0f : 1.0f) * (Size / 4.0f); ZOff = (((i % 4) < 2) ? -1.0f : 1.0f) * (Size / 4.0f); YOff = (((i % 8) < 4) ? -1.0f : 1.0f) * (Size / 4.0f); please explain thease code to me special on how to caculate the xOff and ZOff and YOff value,tell me where they come?
  11. Noise function

    why does no man give me an correct answer ,I need to buid na desert terrain /jungle terrain,so it is random height or use some special picture,so please first give me some noise function use in Rts 4 game produce differnt terrain!please email me at [email="frankluna11@yahoo.com"]frankluna11@yahoo.com[/email]
  12. Noise function

    I read the book<Programming Rts game programming with directx>I find a function Name is Noise,they are shown as below the are use to produce random height use for Terrain!if someone undertstand the code please explain to me! float Noise(int x) { x = (x<<13) ^ x; return (1.0 - ((x * (x*x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0); } float CosInterpolate(float v1, float v2, float a) { float angle = a * D3DX_PI; float prc = (1.0f - cos(angle)) * 0.5f; return v1*(1.0f - prc) + v2*prc; }
  13. Does someone undersatnd the code

    Don't understand why the ind[index++] = z0 * (width + 1) + x0; and for you reply why you says gird[2][6] = gird1D(6*8+2) ok the code from here I don't understand : for( z=source.top, z0 = 0;z<source.bottom;z++, z0++) for(int x=source.left, x0 = 0;x<source.right;x++, x0++) { //Triangle 1 ind[index++] = z0 * (width + 1) + x0; ind[index++] = z0 * (width + 1) + x0 + 1; ind[index++] = (z0+1) * (width + 1) + x0; //Triangle 2 ind[index++] = (z0+1) * (width + 1) + x0; ind[index++] = z0 * (width + 1) + x0 + 1; ind[index++] = (z0+1) * (width + 1) + x0 + 1; } m_pMesh->UnlockIndexBuffer(); //Set Attributes DWORD *att = 0, a = 0; m_pMesh->LockAttributeBuffer(0,&att); memset(att, 0, sizeof(DWORD)*nrTri); m_pMesh->UnlockAttributeBuffer(); it's so hard to understand the concept of save the vertices into traingles,and another things please explain D3DXCreateMeshFVF function!and what that mean attributes?
  14. Did someone can share his learn methoods

    Hi actually is not just on thread ,you will look the title is really diffrent,one request is want to get a learn plan,second is try to get a explain on that code
  15. Does someone undersatnd the code

    the code has a FVF define as:const DWORD TERRAINVertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;the book says have two UV coordinate first alpha second color,but I don't really understand the all code as here! const DWORD TERRAINVertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1; ////////////////////////////////////////////////////////////////////////////////////////// // PATCH // ////////////////////////////////////////////////////////////////////////////////////////// PATCH::PATCH() { m_pDevice = NULL; m_pMesh = NULL; } PATCH::~PATCH() { Release(); } void PATCH::Release() { if(m_pMesh != NULL) m_pMesh->Release(); m_pMesh = NULL; } HRESULT PATCH::CreateMesh(HEIGHTMAP &hm, RECT source, IDirect3DDevice9* Dev) { if(m_pMesh != NULL) { m_pMesh->Release(); m_pMesh = NULL; } try { m_pDevice = Dev; int width = source.right - source.left; int height = source.bottom - source.top; int nrVert = (width + 1) * (height + 1); int nrTri = width * height * 2; if(FAILED(D3DXCreateMeshFVF(nrTri, nrVert, D3DXMESH_MANAGED, TERRAINVertex::FVF, m_pDevice, &m_pMesh))) { debug.Print("Couldn't create mesh for PATCH"); return E_FAIL; } //Create vertices TERRAINVertex* ver = 0; m_pMesh->LockVertexBuffer(0,(void**)&ver); for(int z=source.top, z0 = 0;z<=source.bottom;z++, z0++) for(int x=source.left, x0 = 0;x<=source.right;x++, x0++) { D3DXVECTOR3 pos = D3DXVECTOR3(x, hm.m_pHeightMap[x + z * hm.m_size.x], -z); D3DXVECTOR2 uv = D3DXVECTOR2(x * 0.2f, z * 0.2f); ver[z0 * (width + 1) + x0] = TERRAINVertex(pos, uv); } m_pMesh->UnlockVertexBuffer(); //Calculate Indices WORD* ind = 0; m_pMesh->LockIndexBuffer(0,(void**)&ind); int index = 0; for( z=source.top, z0 = 0;z<source.bottom;z++, z0++) for(int x=source.left, x0 = 0;x<source.right;x++, x0++) { //Triangle 1 ind[index++] = z0 * (width + 1) + x0; ind[index++] = z0 * (width + 1) + x0 + 1; ind[index++] = (z0+1) * (width + 1) + x0; //Triangle 2 ind[index++] = (z0+1) * (width + 1) + x0; ind[index++] = z0 * (width + 1) + x0 + 1; ind[index++] = (z0+1) * (width + 1) + x0 + 1; } m_pMesh->UnlockIndexBuffer(); //Set Attributes DWORD *att = 0, a = 0; m_pMesh->LockAttributeBuffer(0,&att); for( z=source.top;z<source.bottom;z++) for(int x=source.left;x<source.right;x++) { //Calculate quad subsets depending on height int subset; if(hm.m_pHeightMap[x + z * hm.m_size.x] == 0.0f) subset = 0; else if(hm.m_pHeightMap[x + z * hm.m_size.x] <= hm.m_maxHeight * 0.6f) subset = 1; else subset = 2; att[a++] = subset; att[a++] = subset; } m_pMesh->UnlockAttributeBuffer(); //Compute normals D3DXComputeNormals(m_pMesh, NULL); } catch(...) { debug.Print("Error in PATCH::CreateMesh()"); return E_FAIL; } return S_OK; } void PATCH::Render(int texture) { //Draw mesh if(m_pMesh != NULL) m_pMesh->DrawSubset(texture); } Did you can tell me how to understand D3DCreateMeshFVF?