// Here is the cel shade texture we will apply to the object
unsigned char diffuseRange[16] = {127, 127, 127, 160, 160, 160, 160, 160, 255, 255, 255, 255, 255, 255, 255, 255};
// Texture
LPDIRECT3DTEXTURE9 diffuseTex = NULL;
// Surface rectangle
D3DLOCKED_RECT dataRect;
// Create the ramp textures.
if(D3D_Device->CreateTexture(16, 1, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,
&diffuseTex, 0) != D3D_OK) return false;
memset(&dataRect, 0, sizeof(D3DLOCKED_RECT));
diffuseTex->LockRect(0, &dataRect, NULL, 0);
unsigned char *dataPtr = (unsigned char*)dataRect.pBits;
for(int i = 0; i < 16; i++)
{
dataPtr = diffuseRange<span style="font-weight:bold;">;
dataPtr = diffuseRange<span style="font-weight:bold;">;
dataPtr = diffuseRange<span style="font-weight:bold;">;
dataPtr = diffuseRange<span style="font-weight:bold;">;
}
diffuseTex->UnlockRect(<span class="cpp-number">0</span>);
</pre></div><!–ENDSCRIPT–>
I've converted the code to c# in the following way. It compiles, but it's not doing the same thing as the code above. Looks like I need to use texture.LockRectangle in c#, but I'm not exactly sure how to manipulate the GraphicsStream like they're doing iwth the dataPtr. Any help would be appreciated.
<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>
<span class="cpp-keyword">int</span>[] d = <span class="cpp-keyword">new</span> <span class="cpp-keyword">int</span>[<span class="cpp-number">16</span>] { <span class="cpp-number">127</span>, <span class="cpp-number">127</span>, <span class="cpp-number">127</span>, <span class="cpp-number">160</span>, <span class="cpp-number">160</span>, <span class="cpp-number">160</span>, <span class="cpp-number">160</span>, <span class="cpp-number">160</span>, <span class="cpp-number">255</span>, <span class="cpp-number">255</span>, <span class="cpp-number">255</span>, <span class="cpp-number">255</span>, <span class="cpp-number">255</span>, <span class="cpp-number">255</span>, <span class="cpp-number">255</span>, <span class="cpp-number">255</span> };
Bitmap b = <span class="cpp-keyword">new</span> Bitmap(<span class="cpp-number">16</span>, <span class="cpp-number">1</span>); <span class="cpp-comment">// height, width</span>
<span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> i=<span class="cpp-number">0</span>; i<<span class="cpp-number">16</span>; i++) b.SetPixel(i, <span class="cpp-number">0</span>, Color.FromArgb(d<span style="font-weight:bold;">, d<span style="font-weight:bold;">, d<span style="font-weight:bold;">, d<span style="font-weight:bold;">);
Texture diffuseTex = <span class="cpp-keyword">new</span> Texture(device, <span class="cpp-number">16</span>, <span class="cpp-number">1</span>, <span class="cpp-number">1</span>, Usage.None, Format.A8R8G8B8, Pool.Managed);
</pre></div><!–ENDSCRIPT–>
[MDX] Creating a ramp texture
Hi,
I'm trying to convert the following code from C++ to C#. The code generates a ramp texture for use in cel shading from an array of char values.
The easiest way is probably to use the Texture constructor that takes a Bitmap as a parameter, like this:
Another option would be to just create the ramp texture in an image editor and load it as a texture from file.
int[] colors = new int[] { 127, 127, 127, 160, 160, 160, 160, 160, 255, 255, 255, 255, 255, 255, 255, 255 };Bitmap bitmap = new Bitmap(colors.Length, 1);for (int i = 0; i < colors.Length; i++) bitmap.SetPixel(i, 0, Color.FromArgb(255, colors, colors, colors));Texture ramp = new Texture(e.Device, bitmap, 0, Pool.Managed);
Another option would be to just create the ramp texture in an image editor and load it as a texture from file.
Creating a texture from a bitmap using the in built function can be slow, as is SetPixel.
Give this a try:
Give this a try:
byte[] diffuseRange = new byte[] {127, 127, 127, 160, 160, 160, 160, 160, 255, 255, 255, 255, 255, 255, 255, 255}; Texture diffuseTex = new Texture(device, diffuseRange.Length, 1, 1, Usage.None, Format.A8R8G8B8, Pool.Managed); GraphicsStream stream = texture.LockRectangle(0, LockFlags.Discard); for (int i = 0; i < diffuseRange.Length; i++){ int j = 4; while (j-- > 0) stream.Write(diffuseRange); } texture.UnlockRectangle(0);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement