Problem Loading Textures
I found this over at Tao's site. Any idea why it doesn't work?
// ------------------------------
// Mr. Texture @ work,....
// but not doin' it's job :o(
// ------------------------------
Gl.glEnable(Gl.GL_TEXTURE_2D);
string file = @"terrain.jpg";
Bitmap image = null;
image = new Bitmap(file);
//image.RotateFlip(RotateFlipType.RotateNoneFlipY);
System.Drawing.Imaging.BitmapData bitmapdata;
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
bitmapdata = image.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Gl.glGenTextures(1, texture);
// Create MipMapped Texture
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[0]);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR_MIPMAP_NEAREST);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_NEAREST);
Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, (int)Gl.GL_RGB, image.Width, image.Height, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
image.UnlockBits(bitmapdata);
image.Dispose();
*News tagenigma.com is my new domain.
It seems easy to find non-working examples:
http://66.102.7.104/search?q=cache:ItnO2kuUyhgJ:www.randyridge.com/Tao/Discussions/Help/1437.aspx+system+drawing+image+gluBuild2DMipmaps&hl=en
http://www.experts-exchange.com/Programming/Game_Development/3D_Programming/Q_20983278.html
This one relies on CsGL:
http://www.unc.edu/courses/2003spring/comp/236/001/ImageViewer.cs
http://66.102.7.104/search?q=cache:ItnO2kuUyhgJ:www.randyridge.com/Tao/Discussions/Help/1437.aspx+system+drawing+image+gluBuild2DMipmaps&hl=en
http://www.experts-exchange.com/Programming/Game_Development/3D_Programming/Q_20983278.html
This one relies on CsGL:
http://www.unc.edu/courses/2003spring/comp/236/001/ImageViewer.cs
*News tagenigma.com is my new domain.
Thx got it working. You have to lock the image, force it to be in BMP mode, and then pass a byte[] to mipmaps. I'll post the solution later. It's working!!
*News tagenigma.com is my new domain.
Here is the working solution.
public void CreateTexture()
{
DeleteTexture();
if(GetMatFile() == "")
return;
// 1st enable texturing
GL.glEnable(GL.GL_TEXTURE_2D);
// Load Bitmap as textures
using(Bitmap image = new Bitmap(GetMatFile()))
{
image.RotateFlip(RotateFlipType.RotateNoneFlipY);
Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height);
BitmapData bitmapData = image.LockBits(
rectangle,
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
MemoryStream streamedData = new MemoryStream();
image.Save(streamedData, ImageFormat.Jpeg);
m_Material.XPos = 0;
m_Material.YPos = 0;
m_Material.Width = (Int16)image.PhysicalDimension.Width;
m_Material.Height = (Int16)image.PhysicalDimension.Height;
m_Material.MaterialType = MaterialTypeEnums.Diffuse;
m_Material.ImageData = streamedData.ToArray();
MemoryStream streamedDataBMP = new MemoryStream();
image.Save(streamedDataBMP, ImageFormat.Bmp);
byte[] imageBMP = streamedDataBMP.ToArray();
// Generate a texture with the associative texture ID stored in the array
m_GlTextureIdList = new uint[32];
try
{
GL.glGenTextures(1, out m_GlTextureIdList[0]);
}
catch
{
Console.Error.WriteLine("glGenTextures [ FAILED ]");
}
// This sets the alignment requirements for the start of each pixel row in memory.
//GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
// Bind the texture to the texture arrays index and init the texture
GL.glColor3f(1, 1, 1);
BindTexture();
// Build Mipmaps (builds different versions of the picture for distances
// - looks better)
GLU.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, (int)GL.GL_RGB8,
m_Material.Width, m_Material.Height,
GL.GL_BGR_EXT, GL.GL_UNSIGNED_BYTE,
imageBMP);
// Lastly, we need to tell OpenGL the quality of our texture map. GL_LINEAR is the smoothest.
// GL_NEAREST is faster than GL_LINEAR, but looks blochy and pixelated. Good for slower computers though.
// Read more about the MIN and MAG filters at the bottom of main.cpp
GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
//GL.glTexGeni(GL.GL_S, GL.GL_TEXTURE_GEN_MODE, (int)GL.GL_OBJECT_LINEAR);
//GL.glTexGeni(GL.GL_T, GL.GL_TEXTURE_GEN_MODE, (int)GL.GL_OBJECT_LINEAR);
//GL.glEnable(GL.GL_TEXTURE_GEN_S);
//GL.glEnable(GL.GL_TEXTURE_GEN_T);
// Flush GL to catch up
//GL.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_DECAL);
GL.glFlush();
// Store image in JPG format instead of BMP to save space
streamedData.Close();
image.UnlockBits(bitmapData);
image.Dispose();
}
}
public void CreateTexture()
{
DeleteTexture();
if(GetMatFile() == "")
return;
// 1st enable texturing
GL.glEnable(GL.GL_TEXTURE_2D);
// Load Bitmap as textures
using(Bitmap image = new Bitmap(GetMatFile()))
{
image.RotateFlip(RotateFlipType.RotateNoneFlipY);
Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height);
BitmapData bitmapData = image.LockBits(
rectangle,
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
MemoryStream streamedData = new MemoryStream();
image.Save(streamedData, ImageFormat.Jpeg);
m_Material.XPos = 0;
m_Material.YPos = 0;
m_Material.Width = (Int16)image.PhysicalDimension.Width;
m_Material.Height = (Int16)image.PhysicalDimension.Height;
m_Material.MaterialType = MaterialTypeEnums.Diffuse;
m_Material.ImageData = streamedData.ToArray();
MemoryStream streamedDataBMP = new MemoryStream();
image.Save(streamedDataBMP, ImageFormat.Bmp);
byte[] imageBMP = streamedDataBMP.ToArray();
// Generate a texture with the associative texture ID stored in the array
m_GlTextureIdList = new uint[32];
try
{
GL.glGenTextures(1, out m_GlTextureIdList[0]);
}
catch
{
Console.Error.WriteLine("glGenTextures [ FAILED ]");
}
// This sets the alignment requirements for the start of each pixel row in memory.
//GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
// Bind the texture to the texture arrays index and init the texture
GL.glColor3f(1, 1, 1);
BindTexture();
// Build Mipmaps (builds different versions of the picture for distances
// - looks better)
GLU.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, (int)GL.GL_RGB8,
m_Material.Width, m_Material.Height,
GL.GL_BGR_EXT, GL.GL_UNSIGNED_BYTE,
imageBMP);
// Lastly, we need to tell OpenGL the quality of our texture map. GL_LINEAR is the smoothest.
// GL_NEAREST is faster than GL_LINEAR, but looks blochy and pixelated. Good for slower computers though.
// Read more about the MIN and MAG filters at the bottom of main.cpp
GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
//GL.glTexGeni(GL.GL_S, GL.GL_TEXTURE_GEN_MODE, (int)GL.GL_OBJECT_LINEAR);
//GL.glTexGeni(GL.GL_T, GL.GL_TEXTURE_GEN_MODE, (int)GL.GL_OBJECT_LINEAR);
//GL.glEnable(GL.GL_TEXTURE_GEN_S);
//GL.glEnable(GL.GL_TEXTURE_GEN_T);
// Flush GL to catch up
//GL.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_DECAL);
GL.glFlush();
// Store image in JPG format instead of BMP to save space
streamedData.Close();
image.UnlockBits(bitmapData);
image.Dispose();
}
}
*News tagenigma.com is my new domain.
The previous example had some offset problems with the BMP texture when I used the byte[], because it is in reverse order. When reversed it changes the colors to RGB and sometimes BGR or GRB.
So using Scan0 instead fixes the problem. Here's the definitive solution:
public void CreateTexture()
{
DeleteTexture();
if(GetMatFile() == "")
return;
// 1st enable texturing
GL.glEnable(GL.GL_TEXTURE_2D);
// Load Bitmap as textures
using(Bitmap image = new Bitmap(GetMatFile()))
{
// Store in memory as JPG to take up less space
MemoryStream streamedData = new MemoryStream();
image.Save(streamedData, ImageFormat.Jpeg);
m_Material.ImageData = streamedData.ToArray();
if(m_Material.Width > (Int16)image.PhysicalDimension.Width)
m_Material.Width = (Int16)image.PhysicalDimension.Width;
if(m_Material.Height > (Int16)image.PhysicalDimension.Height)
m_Material.Height = (Int16)image.PhysicalDimension.Height;
streamedData.Close();
Rectangle rectangle = new Rectangle(
m_Material.XPos,
m_Material.YPos,
m_Material.Width,
m_Material.Height);
BitmapData bitmapData = image.LockBits(
rectangle,
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
// Generate a texture with the associative texture ID stored in the array
m_GlTextureIdList = new uint[1];
try
{
GL.glGenTextures(1, out m_GlTextureIdList[0]);
}
catch
{
Console.Error.WriteLine("glGenTextures [ FAILED ]");
}
// Bind the texture to the texture arrays index and init the texture
GL.glColor3f(1, 1, 1);
BindTexture();
// Build Mipmaps (builds different versions of the picture for distances
// - looks better)
GLU.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, (int)GL.GL_RGB,
m_Material.Width, m_Material.Height,
GL.GL_BGR_EXT, GL.GL_UNSIGNED_BYTE,
bitmapData.Scan0);
// Lastly, we need to tell OpenGL the quality of our texture map. GL_LINEAR is the smoothest.
// GL_NEAREST is faster than GL_LINEAR, but looks blochy and pixelated. Good for slower computers though.
// Read more about the MIN and MAG filters at the bottom of main.cpp
GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
// Store image in JPG format instead of BMP to save space
streamedData.Close();
image.UnlockBits(bitmapData);
image.Dispose();
}
Good luck
So using Scan0 instead fixes the problem. Here's the definitive solution:
public void CreateTexture()
{
DeleteTexture();
if(GetMatFile() == "")
return;
// 1st enable texturing
GL.glEnable(GL.GL_TEXTURE_2D);
// Load Bitmap as textures
using(Bitmap image = new Bitmap(GetMatFile()))
{
// Store in memory as JPG to take up less space
MemoryStream streamedData = new MemoryStream();
image.Save(streamedData, ImageFormat.Jpeg);
m_Material.ImageData = streamedData.ToArray();
if(m_Material.Width > (Int16)image.PhysicalDimension.Width)
m_Material.Width = (Int16)image.PhysicalDimension.Width;
if(m_Material.Height > (Int16)image.PhysicalDimension.Height)
m_Material.Height = (Int16)image.PhysicalDimension.Height;
streamedData.Close();
Rectangle rectangle = new Rectangle(
m_Material.XPos,
m_Material.YPos,
m_Material.Width,
m_Material.Height);
BitmapData bitmapData = image.LockBits(
rectangle,
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
// Generate a texture with the associative texture ID stored in the array
m_GlTextureIdList = new uint[1];
try
{
GL.glGenTextures(1, out m_GlTextureIdList[0]);
}
catch
{
Console.Error.WriteLine("glGenTextures [ FAILED ]");
}
// Bind the texture to the texture arrays index and init the texture
GL.glColor3f(1, 1, 1);
BindTexture();
// Build Mipmaps (builds different versions of the picture for distances
// - looks better)
GLU.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, (int)GL.GL_RGB,
m_Material.Width, m_Material.Height,
GL.GL_BGR_EXT, GL.GL_UNSIGNED_BYTE,
bitmapData.Scan0);
// Lastly, we need to tell OpenGL the quality of our texture map. GL_LINEAR is the smoothest.
// GL_NEAREST is faster than GL_LINEAR, but looks blochy and pixelated. Good for slower computers though.
// Read more about the MIN and MAG filters at the bottom of main.cpp
GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
// Store image in JPG format instead of BMP to save space
streamedData.Close();
image.UnlockBits(bitmapData);
image.Dispose();
}
Good luck
*News tagenigma.com is my new domain.
Keywords:
image blit bitmap blit crop image crop bitmap c# csharp
I just got blitting bitmaps into textures working in C#. Here is the latest code.
public void CreateTexture()
{
DeleteTexture();
if(GetMatFile() == "")
return;
// 1st enable texturing
GL.glEnable(GL.GL_TEXTURE_2D);
// Load the main image before cropping
Bitmap image = new Bitmap(GetMatFile());
// Get cropped size
if(m_Material.XPos > (Int16)image.PhysicalDimension.Width)
m_Material.XPos = 0;
if(m_Material.YPos > (Int16)image.PhysicalDimension.Height)
m_Material.YPos = 0;
if((m_Material.Width+m_Material.XPos) >
(Int16)image.PhysicalDimension.Width)
m_Material.Width =
(Int16)(image.PhysicalDimension.Width - m_Material.XPos);
if((m_Material.Height+m_Material.YPos) >
(Int16)image.PhysicalDimension.Height)
m_Material.Height =
(Int16)(image.PhysicalDimension.Height - m_Material.YPos);
// Load Bitmap as textures
using(Bitmap cropped = new Bitmap(m_Material.Width, m_Material.Height))
{
Graphics g = Graphics.FromImage(cropped);
g.DrawImage(image,
new Rectangle(0,0,cropped.Width,cropped.Height), m_Material.XPos,
m_Material.YPos,cropped.Width,cropped.Height,GraphicsUnit.Pixel);
g.Dispose();
// Store in memory as JPG to take up less space
MemoryStream streamedData = new MemoryStream();
cropped.Save(streamedData, ImageFormat.Jpeg);
m_Material.ImageData = streamedData.ToArray();
streamedData.Close();
Rectangle rectangle = new Rectangle(
0,
0,
cropped.Width,
cropped.Height);
BitmapData bitmapData = cropped.LockBits(
rectangle,
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
// Generate a texture with the associative texture ID stored in the array
m_GlTextureIdList = new uint[1];
try
{
GL.glGenTextures(1, out m_GlTextureIdList[0]);
}
catch
{
Console.Error.WriteLine("glGenTextures [ FAILED ]");
}
// Bind the texture to the texture arrays index and init the texture
GL.glColor3f(1, 1, 1);
BindTexture();
// Build Mipmaps (builds different versions of the picture for distances
// - looks better)
GLU.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, (int)GL.GL_RGB,
m_Material.Width, m_Material.Height,
GL.GL_BGR_EXT, GL.GL_UNSIGNED_BYTE,
bitmapData.Scan0);
// Lastly, we need to tell OpenGL the quality of our texture map. GL_LINEAR is the smoothest.
// GL_NEAREST is faster than GL_LINEAR, but looks blochy and pixelated. Good for slower computers though.
// Read more about the MIN and MAG filters at the bottom of main.cpp
GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
// Store image in JPG format instead of BMP to save space
streamedData.Close();
image.UnlockBits(bitmapData);
image.Dispose();
}
}
image blit bitmap blit crop image crop bitmap c# csharp
I just got blitting bitmaps into textures working in C#. Here is the latest code.
public void CreateTexture()
{
DeleteTexture();
if(GetMatFile() == "")
return;
// 1st enable texturing
GL.glEnable(GL.GL_TEXTURE_2D);
// Load the main image before cropping
Bitmap image = new Bitmap(GetMatFile());
// Get cropped size
if(m_Material.XPos > (Int16)image.PhysicalDimension.Width)
m_Material.XPos = 0;
if(m_Material.YPos > (Int16)image.PhysicalDimension.Height)
m_Material.YPos = 0;
if((m_Material.Width+m_Material.XPos) >
(Int16)image.PhysicalDimension.Width)
m_Material.Width =
(Int16)(image.PhysicalDimension.Width - m_Material.XPos);
if((m_Material.Height+m_Material.YPos) >
(Int16)image.PhysicalDimension.Height)
m_Material.Height =
(Int16)(image.PhysicalDimension.Height - m_Material.YPos);
// Load Bitmap as textures
using(Bitmap cropped = new Bitmap(m_Material.Width, m_Material.Height))
{
Graphics g = Graphics.FromImage(cropped);
g.DrawImage(image,
new Rectangle(0,0,cropped.Width,cropped.Height), m_Material.XPos,
m_Material.YPos,cropped.Width,cropped.Height,GraphicsUnit.Pixel);
g.Dispose();
// Store in memory as JPG to take up less space
MemoryStream streamedData = new MemoryStream();
cropped.Save(streamedData, ImageFormat.Jpeg);
m_Material.ImageData = streamedData.ToArray();
streamedData.Close();
Rectangle rectangle = new Rectangle(
0,
0,
cropped.Width,
cropped.Height);
BitmapData bitmapData = cropped.LockBits(
rectangle,
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
// Generate a texture with the associative texture ID stored in the array
m_GlTextureIdList = new uint[1];
try
{
GL.glGenTextures(1, out m_GlTextureIdList[0]);
}
catch
{
Console.Error.WriteLine("glGenTextures [ FAILED ]");
}
// Bind the texture to the texture arrays index and init the texture
GL.glColor3f(1, 1, 1);
BindTexture();
// Build Mipmaps (builds different versions of the picture for distances
// - looks better)
GLU.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, (int)GL.GL_RGB,
m_Material.Width, m_Material.Height,
GL.GL_BGR_EXT, GL.GL_UNSIGNED_BYTE,
bitmapData.Scan0);
// Lastly, we need to tell OpenGL the quality of our texture map. GL_LINEAR is the smoothest.
// GL_NEAREST is faster than GL_LINEAR, but looks blochy and pixelated. Good for slower computers though.
// Read more about the MIN and MAG filters at the bottom of main.cpp
GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR);
// Store image in JPG format instead of BMP to save space
streamedData.Close();
image.UnlockBits(bitmapData);
image.Dispose();
}
}
*News tagenigma.com is my new domain.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement