Warning: Use of undefined constant image_setup - assumed 'image_setup' (this will throw an Error in a future version of PHP) in /home/westleyw/public_html/wp-content/themes/autofocus/functions.php on line 576

Warning: Use of undefined constant image_setup - assumed 'image_setup' (this will throw an Error in a future version of PHP) in /home/westleyw/public_html/wp-content/themes/autofocus/functions.php on line 577

Warning: Declaration of Jetpack_IXR_Client::query() should be compatible with IXR_Client::query(...$args) in /home/westleyw/public_html/wp-content/plugins/jetpack/class.jetpack-ixr-client.php on line 91

Warning: Cannot modify header information - headers already sent by (output started at /home/westleyw/public_html/wp-content/themes/autofocus/functions.php:576) in /home/westleyw/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1794

Warning: Cannot modify header information - headers already sent by (output started at /home/westleyw/public_html/wp-content/themes/autofocus/functions.php:576) in /home/westleyw/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1794

Warning: Cannot modify header information - headers already sent by (output started at /home/westleyw/public_html/wp-content/themes/autofocus/functions.php:576) in /home/westleyw/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1794

Warning: Cannot modify header information - headers already sent by (output started at /home/westleyw/public_html/wp-content/themes/autofocus/functions.php:576) in /home/westleyw/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1794

Warning: Cannot modify header information - headers already sent by (output started at /home/westleyw/public_html/wp-content/themes/autofocus/functions.php:576) in /home/westleyw/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1794

Warning: Cannot modify header information - headers already sent by (output started at /home/westleyw/public_html/wp-content/themes/autofocus/functions.php:576) in /home/westleyw/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1794

Warning: Cannot modify header information - headers already sent by (output started at /home/westleyw/public_html/wp-content/themes/autofocus/functions.php:576) in /home/westleyw/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1794

Warning: Cannot modify header information - headers already sent by (output started at /home/westleyw/public_html/wp-content/themes/autofocus/functions.php:576) in /home/westleyw/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1794
{"id":579,"date":"2013-07-03T20:23:18","date_gmt":"2013-07-04T02:23:18","guid":{"rendered":"http:\/\/www.westleywood.com\/?p=579"},"modified":"2013-07-05T15:37:38","modified_gmt":"2013-07-05T21:37:38","slug":"terrain-editor-texture-painting-2","status":"publish","type":"post","link":"http:\/\/www.westleywood.com\/2013\/07\/terrain-editor-texture-painting-2\/","title":{"rendered":"Terrain Editor (Texture Painting)"},"content":{"rendered":"

Terrain texture painting is when your painting a terrain with a specified texture. \u00a0The way this works is that there \u00a0is one blend texture and then three terrain textures. \u00a0The blend texture is sampled by the shader in order to determine the correct texture to apply at this location.<\/p>\n

To get started with the basics of muti-texturing I recommend reading chapter 11.8 in Frank D. Luna’s\u00a0Introduction To 3D Game Programming With DirectX 9.0c A Shader Approach<\/em>. \u00a0This book is definitely one of my favorite Graphics Programming books.<\/p>\n

\u00a0Creating The Blend Texture<\/h3>\n

The first thing you have to do is create the custom blending texture. \u00a0For my Terrain Editor I allowed the user to specify the detail that they want to use for the texture. \u00a0I then determined the width and height of the texture by multiplying the width and length of the terrain by the user specified scale. \u00a0The default scale of 1 would produce 1 blend texture pixel to define 1 unit in the 3D space.<\/p>\n

Like the terrain mesh, since we are editing the texture we can not use a managed memory pool. \u00a0This means we will want to store a copy of the texture locally. \u00a0The way I did this is to just create a list of\u00a0D3DXCOLOR<\/em>\u00a0values of the same amount of pixels as in the texture.<\/p>\n

Editing The Blend Texture<\/h3>\n

One of the first questions I was asking when starting this project was\u00a0“How do I edit the texture?”<\/em>\u00a0\u00a0After some research I learned that you can edit a DirectX texture by calling the\u00a0LockRect()<\/em>\u00a0function before the edit and\u00a0UnlockRect()<\/em>\u00a0after the edit.<\/p>\n

The next question I was asking was\u00a0“What is the data type stored in this rectangle buffer?”<\/em>\u00a0\u00a0Again some research into some forums proved to be useful as I learned that it was an array of bytes and that you would need to index these bytes in order to edit the correct pixel that you wish to edit. \u00a0The equation for getting the index of a pixel is:<\/p>\n

Index = (Y Position) * (LockedRect.Pitch \/ 4) + X Position<\/em><\/p>\n

To start off I iterated through the entire texture setting each index to\u00a0D3DXCOLOR(1.0, 0.0, 0.0, 1.0)<\/em>. \u00a0This set all the colors to a solid red, which represented the first texture of my three textures. \u00a0Green was reference to my second texture and blue was a reference to my third texture. \u00a0Using the RGB of my blend texture I can determine the texture to use at the specified position where that blend texture pixel is located.<\/p>\n

\"websiteExample\"<\/a><\/p>\n

\u00a0Converting World Coordinates to Texture Coordinates<\/h3>\n

One of the larger hurdles to get over was converting a 3D World Position into a 2D Texture Position. \u00a0To start since the texture coordinates are in relation to the terrain I have to move the world coordinate over by the same amount that I initially move the starting terrain vertex coordinate over by.<\/p>\n

World Coordinate X = World Coordinate X + (Terrain Width) * 0.5<\/em><\/p>\n

World Coordinate Z = -(World Coordinate Z – (Terrain Length) * 0.5)<\/em><\/p>\n

Next, scale the terrain coordinate by the user defined texture detail scale value. \u00a0Finally, for DirectX9 the texture will need to be offset by half a pixel down each negative axis.<\/p>\n

World Coordinate X = World Coordinate X – (Blend Texture Scale) * 0.5<\/em><\/p>\n

World Coordinate Z = World Coordinate Z – (Blend Texture Scale) * 0.5<\/em><\/p>\n

The resulting X and Z 3D Coordinate is your X and Y 2D Texture position. \u00a0I implemented and recommend implementing a rounding function to round the 3D decimal coordinate to its proper 2D texture position.<\/p>\n","protected":false},"excerpt":{"rendered":"Terrain texture painting is when your painting a terrain with a specified texture. \u00a0The way this works is that there \u00a0is one blend texture and …","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/posts\/579"}],"collection":[{"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/comments?post=579"}],"version-history":[{"count":8,"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/posts\/579\/revisions"}],"predecessor-version":[{"id":590,"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/posts\/579\/revisions\/590"}],"wp:attachment":[{"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/media?parent=579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/categories?post=579"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.westleywood.com\/wp-json\/wp\/v2\/tags?post=579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}