本文整理汇总了C#中OpenGL.TexImage2D方法的典型用法代码示例。如果您正苦于以下问题:C# OpenGL.TexImage2D方法的具体用法?C# OpenGL.TexImage2D怎么用?C# OpenGL.TexImage2D使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OpenGL
的用法示例。
在下文中一共展示了OpenGL.TexImage2D方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
/// <summary>
/// This function creates the texture from an image file.
/// </summary>
/// <param name="gl">The OpenGL object.</param>
/// <param name="path">The path to the image file.</param>
/// <returns>True if the texture was successfully loaded.</returns>
public virtual bool Create(OpenGL gl, string path)
{
// Create the underlying OpenGL object.
Create(gl);
// Try and load the bitmap. Return false on failure.
Bitmap image = new Bitmap(path);
if (image == null)
return false;
// Get the maximum texture size supported by OpenGL.
int[] textureMaxSize = { 0 };
gl.GetInteger(OpenGL.MAX_TEXTURE_SIZE, textureMaxSize);
// Find the target width and height sizes, which is just the highest
// posible power of two that'll fit into the image.
int targetWidth = textureMaxSize[0];
int targetHeight = textureMaxSize[0];
for (int size = 1; size <= textureMaxSize[0]; size *= 2)
{
if (image.Width < size)
{
targetWidth = size / 2;
break;
}
if (image.Width == size)
targetWidth = size;
}
for (int size = 1; size <= textureMaxSize[0]; size *= 2)
{
if (image.Height < size)
{
targetHeight = size / 2;
break;
}
if (image.Height == size)
targetHeight = size;
}
// If need to scale, do so now.
if(image.Width != targetWidth || image.Height != targetHeight)
{
// Resize the image.
Image newImage = image.GetThumbnailImage(targetWidth, targetHeight, null, IntPtr.Zero);
// Destory the old image, and reset.
image.Dispose();
image = (Bitmap)newImage;
}
// Create an array of pixels the correct size.
pixelData = new byte[image.Width * image.Height * 4];
int index = 0;
// TODO: The loop below is staggeringly slow and needs speeding up.
// Go through the pixels backwards, this seems to be how OpenGL wants the data.
for (int y = image.Height - 1; y >= 0; y--)
{
for (int x = 0; x < image.Width; x++)
{
Color pixel = image.GetPixel(x, y);
pixelData[index++] = pixel.R;
pixelData[index++] = pixel.G;
pixelData[index++] = pixel.B;
pixelData[index++] = pixel.A;
}
}
// Set the width and height.
width = image.Width;
height = image.Height;
// Dispose of the image file.
image.Dispose();
// Bind our texture object (make it the current texture).
gl.BindTexture(OpenGL.TEXTURE_2D, TextureName);
// Set the image data.
gl.TexImage2D(OpenGL.TEXTURE_2D, 0, (int)OpenGL.RGBA,
width, height, 0, OpenGL.RGBA, OpenGL.UNSIGNED_BYTE,
pixelData);
// Set linear filtering mode.
gl.TexParameter(OpenGL.TEXTURE_2D, OpenGL.TEXTURE_MIN_FILTER, OpenGL.LINEAR);
gl.TexParameter(OpenGL.TEXTURE_2D, OpenGL.TEXTURE_MAG_FILTER, OpenGL.LINEAR);
// We're done!
return true;
}
示例2: Create
/// <summary>
/// This function creates the texture from an image.
/// </summary>
/// <param name="gl">The OpenGL object.</param>
/// <param name="image">The image.</param>
/// <returns>True if the texture was successfully loaded.</returns>
public virtual bool Create(OpenGL gl, Bitmap image)
{
// Create the underlying OpenGL object.
Create(gl);
// Get the maximum texture size supported by OpenGL.
int[] textureMaxSize = { 0 };
gl.GetInteger(OpenGL.GL_MAX_TEXTURE_SIZE, textureMaxSize);
// Find the target width and height sizes, which is just the highest
// posible power of two that'll fit into the image.
int targetWidth = textureMaxSize[0];
int targetHeight = textureMaxSize[0];
for (int size = 1; size <= textureMaxSize[0]; size *= 2)
{
if (image.Width < size)
{
targetWidth = size / 2;
break;
}
if (image.Width == size)
targetWidth = size;
}
for (int size = 1; size <= textureMaxSize[0]; size *= 2)
{
if (image.Height < size)
{
targetHeight = size / 2;
break;
}
if (image.Height == size)
targetHeight = size;
}
// If need to scale, do so now.
if (image.Width != targetWidth || image.Height != targetHeight)
{
// Resize the image.
Image newImage = image.GetThumbnailImage(targetWidth, targetHeight, null, IntPtr.Zero);
// Destory the old image, and reset.
image.Dispose();
image = (Bitmap)newImage;
}
// Lock the image bits (so that we can pass them to OGL).
BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
// Set the width and height.
width = image.Width;
height = image.Height;
// Bind our texture object (make it the current texture).
gl.BindTexture(OpenGL.GL_TEXTURE_2D, TextureName);
// Set the image data.
gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA,
width, height, 0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE,
bitmapData.Scan0);
// Unlock the image.
image.UnlockBits(bitmapData);
// Dispose of the image file.
image.Dispose();
// Set linear filtering mode.
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
// We're done!
return true;
}
示例3: SetImage
/// <summary>
/// This function creates the texture from an image.
/// </summary>
/// <param name="gl">The OpenGL object.</param>
/// <param name="image">The image.</param>
/// <returns>True if the texture was successfully loaded.</returns>
public void SetImage(OpenGL gl, Bitmap image)
{
// Get the maximum texture size supported by OpenGL.
int[] textureMaxSize = { 0 };
gl.GetInteger(OpenGL.GL_MAX_TEXTURE_SIZE, textureMaxSize);
// Find the target width and height sizes, which is just the highest
// posible power of two that'll fit into the image.
int targetWidth = textureMaxSize[0];
int targetHeight = textureMaxSize[0];
for (int size = 1; size <= textureMaxSize[0]; size *= 2)
{
if (image.Width < size)
{
targetWidth = size / 2;
break;
}
if (image.Width == size)
targetWidth = size;
}
for (int size = 1; size <= textureMaxSize[0]; size *= 2)
{
if (image.Height < size)
{
targetHeight = size / 2;
break;
}
if (image.Height == size)
targetHeight = size;
}
// If need to scale, do so now.
bool destroyImage = false;
if (image.Width != targetWidth || image.Height != targetHeight)
{
// Resize the image.
Image newImage = image.GetThumbnailImage(targetWidth, targetHeight, null, IntPtr.Zero);
image = (Bitmap)newImage;
destroyImage = true;
}
// Lock the image bits (so that we can pass them to OGL).
BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
// Set the width and height.
Width = (uint)image.Width;
Height = (uint)image.Height;
// Bind our texture object (make it the current texture).
gl.BindTexture(OpenGL.GL_TEXTURE_2D, textureObject);
// Set the image data.
gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA,
(int)Width, (int)Height, 0, OpenGL.GL_BGRA, OpenGL.GL_UNSIGNED_BYTE,
bitmapData.Scan0);
// Unlock the image.
image.UnlockBits(bitmapData);
// Dispose of the image file if it's an intermediate we created.
if(destroyImage)
image.Dispose();
}