PDA

View Full Version : Multiple texture problem with PFD_DRAW_TO_BITMAP?



HanWu
07-07-2009, 07:37 PM
I implemented the off-screen rendering with the PIXELFORMATDESCRIPTOR set to PFD_DRAW_TO_BITMAP instead of PFD_DRAW_TO_WINDOW. I try to render two different textures and it works fine within the PFD_DRAW_TO_WINDOW (render to screen) mode but when I try to output it to a bitmap with off-screen rendering with PFD_DRAW_TO_BITMAP mode, both of the polygon were render with the same texture (only the last initialize texture is used, the previous initialize textures are always ignore by the system). Every time when I start off-screen rendering, I will re-create all the texture list by calling Gl.glGenTexture(1000,texture); The snippet code for rendering the polygon is as follow.

internal virtual void DrawTextureEx(int texID,VertexC vertex,PointDList cor,float []color){
Gl.glEnable(Gl.GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texID);
Gl.glBegin(Gl.GL_POLYGON);
for(int i=0;i<vertex.Count;i++)
{
Gl.glTexCoord2d(cor[i].X,cor[i].Y);
Gl.glVertex3d(vertex[i].X,vertex[i].Y,vertex[i].Z);
}
Gl.glEnd();
}
SetOpenGLState(){
SetIniTexture();
string fileName="";
bool temStatus = false;

fileName = Application.StartupPath + @"\Data\brick.bmp";
TextureBrickWallID = LoadTextures(fileName, ref temStatus);

fileName = Application.StartupPath + @"\Data\roof covering20.BMP";
TextureRoof = LoadTextures(fileName, ref temStatus);
}

internal virtual void SetIniTexture()
{
Gl.glDeleteTextures(1000,texture);
textureCounter=0;
Gl.glGenTextures(1000,texture); // Create The Texture }


public virtual int LoadTextures(string str,ref bool success)
{
int tmp = texture[textureCounter];
success=LoadTexture(str,textureCounter,0);
if(success==false)
return -1;
TextureTable[textureCounter++]=str;

return tmp;
}

public virtual bool LoadTexture(string fileName,int texCount,int type)
{
bool status = false; // Status Indicator
if(type==0)
{ // bitmap
Bitmap[] textureImage = new Bitmap[1]; // Create Storage Space For The Texture
textureImage[0] = LoadBMP(fileName); // Load The Bitmap
// Check For Errors, If Bitmap's Not Found, Quit

if(textureImage[0] != null)
{
status = true; // Set The Status To True
textureImage[0].RotateFlip(RotateFlipType.RotateNoneFlipY); // Flip The Bitmap Along The Y-Axis
// Rectangle For Locking The Bitmap In Memory
Rectangle rectangle = new Rectangle(0, 0, textureImage[0].Width, textureImage[0].Height);
// Get The Bitmap's Pixel Data From The Locked Bitmap
BitmapData bitmapData = textureImage[0].LockBits(rectangle, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

// Typical Texture Generation Using Data From The Bitmap
Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[texCount]);
Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, Gl.GL_RGB8, textureImage[0].Width, textureImage[0].Height, Gl.GL_BGR, Gl.GL_UNSIGNED_BYTE, bitmapData.Scan0);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
// Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_NEAREST);
// Gl.glTexGeni(Gl.GL_S, Gl.GL_TEXTURE_GEN_MODE, Gl.GL_OBJECT_LINEAR);
// Gl.glTexGeni(Gl.GL_T, Gl.GL_TEXTURE_GEN_MODE, Gl.GL_OBJECT_LINEAR);

Gl.glTexEnvf(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, Gl.GL_DECAL);

// Gl.glEnable(Gl.GL_TEXTURE_GEN_S);
// Gl.glEnable(Gl.GL_TEXTURE_GEN_T);

if(textureImage[0] != null)
{ // If Texture Exists
textureImage[0].UnlockBits(bitmapData); // Unlock The Pixel Data From Memory
textureImage[0].Dispose(); // Dispose The Bitmap
}
Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0);
Gl.glDisable(Gl.GL_TEXTURE_2D);
}
}
return status; // Return The Status
}
Any special setting to render more than one texture under the PFD_DRAW_TO_BITMAP mode?

ZbuffeR
07-08-2009, 05:23 AM
PFD_DRAW_TO_BITMAP : did you check your GL version on this mode ? It should give you software renering GL 1.1 only.

You should better use FBO (framebuffer_object) for real offscreen rendering. Searching these forums will return you tutorials and docs.

HanWu
07-13-2009, 08:12 PM
Hi, I try to use FBO for offset rendering and retrieve the texure with glGetTexImage and it is working fine for the image size less than 2048, but if try with larger image size like 4096, the glGetTexImage return empty image, the message return by glGetError() says it is an unknown error. Is it any image size limitation on FBO or glGetTexIamge? Thanks in advance!

_NK47
07-14-2009, 01:46 AM
probably yes. check the renderer capabilities before doing anything. see all glGetIntegerv (http://www.opengl.org/sdk/docs/man/xhtml/glGet.xml) values in the help as well as the additional tokens on FBO (http://www.opengl.org/registry/specs/EXT/framebuffer_object.txt).