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_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,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);
}