PDA

View Full Version : Problems with texture



Le Bayonnais
03-15-2000, 04:07 AM
I want to put a texture in a polygon. But i don't know which before running my application. So I create a dynamic matrix, BUT (threre is a BUT) something is wrong, but I don't what!! The matrix is well initialise.
Please help me...

void __fastcall TForm1::OpenGLPanel1Init(TObject *Sender)
{
glViewport(0,0,(GLsizei)OpenGLPanel1->Width,(GLsizei)OpenGLPanel1->Height);
glMatrixMode(GL_PROJECTION);

glOrtho(-100.0,100.0,-100.0,100.0,-2000.0,2000.0);

glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glClearColor(0.0,0.0,0.0,0.0);

OpenGLPanel1TextureInit("earth.bmp");
}
//-------------------------------------------void __fastcall TForm1::OpenGLPanel1TextureInit(char *Name)
{
Graphics::TBitmap *bitmap;

bitmap = new Graphics::TBitmap;
bitmap->LoadFromFile(Name);

static GLubyte ***bits;

bits = (GLubyte***)malloc(bitmap->Height*sizeof(GLubyte**));
for(int i=0;i<bitmap->Height;i++)
{
bits[i] = (GLubyte**)malloc(bitmap->Width*sizeof(GLubyte*));
for (int j=0;j<bitmap->Width;j++)
bits[i][j] = (GLubyte*)malloc(4*sizeof(GLubyte));
}


for (int i=0; i<bitmap->Height; i++)
for (int j=0; j<bitmap->Width; j++)
{
bits[i][j][0] = (GLubyte) GetRValue(bitmap->Canvas->Pixels[i][j]);
bits[i][j][1] = (GLubyte) GetGValue(bitmap->Canvas->Pixels[i][j]);
bits[i][j][2] = (GLubyte) GetBValue(bitmap->Canvas->Pixels[i][j]);
bits[i][j][3] = (GLubyte) 255;
}

glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glGenTextures(1,&texName);
glBindTexture(GL_TEXTURE_2D,texName);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,
bitmap->Width,bitmap->Height,0,
GL_RGBA,GL_UNSIGNED_BYTE,bits);


delete bitmap;
}
//-------------------------------------------void __fastcall TForm1::OpenGLPanel1Texture()
{
// Draw the Background Image

glEnable(GL_TEXTURE_2D);
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0,0.0,-500);

glBindTexture(GL_TEXTURE_2D,texName);

glBegin(GL_POLYGON);
glTexCoord2f(0.0, 1.0); glVertex3f(-100.0, 100.0,-500.0);
glTexCoord2f(1.0, 1.0); glVertex3f( 100.0, 100.0,-500.0);
glTexCoord2f(1.0, 0.0); glVertex3f( 100.0,-100.0,-500.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-100.0,-100.0,-500.0);
glEnd();

glPopMatrix();
glFlush();

glDisable(GL_TEXTURE_2D);


}

fshana
03-20-2000, 10:14 AM
I don't see you initializing texName anywhere. Is this a global ? If I were you I'd throw in some error handling for both the c and the openGL. Check glGetError and gluGetErrorDesc ( I think ). These function names are from memory but should give you something to look up. Hope this helps,
fs

Sjonny
03-20-2000, 12:55 PM
Originally posted by Le Bayonnais:

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,
bitmap->Width,bitmap->Height,0,
GL_RGBA,GL_UNSIGNED_BYTE,bits);


shouldn't the first GL_RGBA just be the number 4, telling OpenGL the about of elements in the picture?
GL_RGBA is defined in my gl.h as 0x1908. now 6408 elements per pixel seem as a lot to me http://www.opengl.org/discussion_boards/ubb/wink.gif

Oh, and the call to gluBuild2DMipmaps(...) is missing. I'm not sure if this is really required, but most people I've seen code from do this after the other call I just mentioned.

John

[This message has been edited by Sjonny (edited 03-20-2000).]