I'm trying to create a texture from a TBitmap and not directly from a *.bmp file, because my bitmaps are loaded from a resource file(but it is a type of resource file created by me - and it is working fine).
This is my class TNewBitmap descendant of TBitmap, and it has a FBitmapIntF(TLazIntFImage) and FTexture(gluint) variables - an how I am trying to load the texture:
Code:
procedure TNewBitmap.UpdateTexture;
var
BData: Array of Byte;
BIndexX, BIndexY : Integer;
begin
Self.FBitmapIntF := TLazIntFImage.Create(Self.Width, Self.Height);
Self.FBitmapIntF.LoadFromBitmap(Self.Handle, Self.MaskHandle);
SetLength(BData, Self.Width * Self.Height * 3);
for BIndexY := 0 to Self.Height - 1 do
begin
for BIndexX := 0 to Self.Width - 1 do
begin
BData[(BIndexY * BIndexX) + BIndexX + 0] := GetBValue(Self.FBitmapIntF.Pixels[BIndexX, BIndexY]);
BData[(BIndexY * BIndexX) + BIndexX + 1] := GetGValue(Self.FBitmapIntF.Pixels[BIndexX, BIndexY]);
BData[(BIndexY * BIndexX) + BIndexX + 2] := GetRValue(Self.FBitmapIntF.Pixels[BIndexX, BIndexY]);
end;
end;
Self.FTexture := CreateTexture(Width, Height, @BData);
Self.LoadFromIntFImage(Self.FBitmapIntF);
Self.FBitmapIntF.Free;
end;
function CreateTexture(Width, Height: Integer; pData : Pointer) : GLUInt;
var
Texture : GLuint;
begin
glEnable(GL_TEXTURE_2D);
glGenTextures(1, Texture);
glBindTexture(GL_TEXTURE_2D, Texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, 3, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, pData);
Result := Texture;
end;
And this is how I'm try to draw the texture in the screen - this is in the render procedure:
Code:
glViewport(0, 0, FMainForm.Width, FMainForm.Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, FMainForm.Width, 0, FMainForm.Height, -100, 100);
glMatrixMode(GL_MODELVIEW);
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
// loop for each object in game:
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, BSprite.FTexture); // Bind the Texture of the the object
glTranslatef(BObjectPosX + BObjectCenterX, BObjectPosY + BObjectCenterY, 0.0);
glRotatef(BSprite.FAngle, 0.0, 0.0, 1.0);
glNormal3f( 0.0, 0.0, 1.0);
glBegin(GL_QUADS);
//glColor3f(0, 0.35, 0.5);
glTexCoord2f(0.0, 0.0); glVertex3f(-32.0, -32.0, 1.0);
glTexCoord2f(1.0, 0.0); glVertex3f( 32.0, -32.0, 1.0);
glTexCoord2f(1.0, 1.0); glVertex3f( 32.0, 32.0, 1.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-32.0, 32.0, 1.0);
glEnd();
// end of the loop for;
glPopMatrix;
SwapBuffers(GetDC(FMainForm.Handle));
glFlush;
Bookmarks