
Divide Et Impera
 
Профиль
Группа: Участник
Сообщений: 983
Регистрация: 7.4.2009
Где: Что, Когда?
Репутация: нет Всего: 8
|
Здравствуйте, Пишу небольшую простую игру на опен гл. Проблема возникла на уровне смены текстуры: Для загрузки текстуры использую набор функций Textures.pas: Код |
unit Textures; {$mode DELPHI} interface
uses Classes,SysUtils,Graphics,IntfGraphics,LCLtype, GL,GLu;
function LoadTexture(Filename: String; var Texture: GLuint; LoadFromRes : Boolean): Boolean; function LoadBMPTexture(Filename: String; var Texture : GLuint; LoadFromResource : Boolean) : Boolean; implementation
{------------------------------------------------------------------} { Create the Texture } {------------------------------------------------------------------} function CreateTexture(Width, Height, Format : Word; pData : Pointer) : Integer; var Texture : GLuint; begin glGenTextures(1, @Texture); glBindTexture(GL_TEXTURE_2D, Texture); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); {Texture blends with object background}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); { only first two can be used } glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); { all of the above can be used }
if Format = GL_RGBA then gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, pData) else gluBuild2DMipmaps(GL_TEXTURE_2D, 3, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, pData);
result :=Texture; end;
{------------------------------------------------------------------} { Load BMP textures } {------------------------------------------------------------------} function LoadBMPTexture(Filename: String; var Texture : GLuint; LoadFromResource : Boolean) : Boolean; var FileHeader: BITMAPFILEHEADER; InfoHeader: BITMAPINFOHEADER; Palette: array of RGBQUAD; BitmapFile: TFileStream; BitmapLength: LongWord; PaletteLength: LongWord; ReadBytes: LongWord; Front: ^Byte; Back: ^Byte; Temp: Byte; I : Integer; Width, Height : Integer; pData : Pointer;
// used for loading from resource ResHandle : THandle; MemHandle : THandle; MemStream : TMemoryStream; ResPtr : PByte; ResSize : Longint; begin
if LoadFromResource then // Load from resource begin try ResHandle := FindResource(hInstance, PChar(copy(Filename, 1, Pos('.', Filename)-1)), 'BMP'); if ResHandle = 0 then begin
//MessageBox(0, PChar('File not found in resource - ' + Filename), PChar('BMP Texture'), MB_OK); result :=FALSE; Exit; end; MemHandle := LoadResource(hInstance, ResHandle); ResPtr := LockResource(MemHandle); MemStream := TMemoryStream.Create; ResSize := SizeOfResource(hInstance, ResHandle); MemStream.SetSize(ResSize); MemStream.Write(ResPtr^, ResSize); FreeResource(MemHandle); MemStream.Seek(0, 0);
MemStream.ReadBuffer(FileHeader, SizeOf(FileHeader)); // FileHeader MemStream.ReadBuffer(InfoHeader, SizeOf(InfoHeader)); // InfoHeader PaletteLength := InfoHeader.biClrUsed; SetLength(Palette, PaletteLength); MemStream.ReadBuffer(Palette, PaletteLength); // Palette
Width := InfoHeader.biWidth; Height := InfoHeader.biHeight;
BitmapLength := InfoHeader.biSizeImage; if BitmapLength = 0 then BitmapLength := Width * Height * InfoHeader.biBitCount Div 8;
GetMem(pData, BitmapLength); MemStream.ReadBuffer(pData^, BitmapLength); // Bitmap Data MemStream.Free; except //MessageBox(0, PChar('Error reading file from resource - ' + Filename), PChar('BMP Unit'), MB_OK); result :=FALSE; Exit; end; end else begin // Load image from file
BitmapFile:= TFileStream.Create(Filename,fmOpenRead); ReadBytes:=BitmapFile.Read(FileHeader, SizeOf(FileHeader)); ReadBytes:=BitmapFile.Read(InfoHeader, SizeOf(InfoHeader));
// Get palette PaletteLength := InfoHeader.biClrUsed; SetLength(Palette, PaletteLength);
ReadBytes:=BitmapFile.Read(Palette, PaletteLength); if (ReadBytes <> PaletteLength) then begin // MessageBox(0, PChar('Error reading palette'), PChar('BMP Unit'), MB_OK); result :=FALSE; Exit; end;
Width := InfoHeader.biWidth; Height := InfoHeader.biHeight; BitmapLength := InfoHeader.biSizeImage;
if BitmapLength = 0 then BitmapLength := Width * Height * InfoHeader.biBitCount Div 8;
// Get the actual pixel data GetMem(pData, BitmapLength); ReadBytes:=BitmapFile.Read( pData^, BitmapLength); if (ReadBytes <> BitmapLength) then begin // MessageBox(0, PChar('Error reading bitmap data'), PChar('BMP Unit'), MB_OK); result :=FALSE; Exit; end;
BitmapFile.Free; end;
//=================================================================== // Bitmaps are stored BGR and not RGB, so swap the R and B bytes. for I :=0 to Width * Height - 1 do begin Front := Pointer(Integer(pData) + I*3); Back := Pointer(Integer(pData) + I*3 + 2); Temp := Front^; Front^ := Back^; Back^ := Temp; end;
Texture :=CreateTexture(Width, Height, GL_RGB, pData); FreeMem(pData); result :=TRUE; end;
{------------------------------------------------------------------} { Load JPEG textures } {------------------------------------------------------------------} function LoadJPGTexture(Filename: String; var Texture: GLuint; LoadFromResource : Boolean): Boolean; var Data : Array of LongWord; W, Width : Integer; H, Height : Integer; BMP : TBitmap; iBMP:TLazIntfImage; JPG : TJPEGImage; C : LongWord; Line : ^LongWord;
// used for loading from resource ResHandle : THandle; MemHandle : THandle; MemStream : TMemoryStream; ResPtr : PByte; ResSize : Longint; begin JPG:=TJPEGImage.Create;
if LoadFromResource then // Load from resource begin try ResHandle := FindResource(hInstance, PChar(copy(Filename, 1, Pos('.', Filename)-1)), 'JPEG'); MemHandle := LoadResource(hInstance, ResHandle); ResPtr := LockResource(MemHandle); MemStream := TMemoryStream.Create; ResSize := SizeOfResource(hInstance, ResHandle); MemStream.SetSize(ResSize); MemStream.Write(ResPtr^, ResSize); FreeResource(MemHandle); MemStream.Seek(0, 0); JPG.LoadFromStream(MemStream); MemStream.Free; except //MessageBox(0, PChar('Couldn''t load JPG Resource - "'+ Filename +'"'), PChar('BMP Unit'), MB_OK); result :=FALSE; Exit; end; end else begin try JPG.LoadFromFile(Filename); except // MessageBox(0, PChar('Couldn''t load JPG - "'+ Filename +'"'), PChar('BMP Unit'), MB_OK); result :=FALSE; Exit; end; end;
// Create Bitmap BMP:=TBitmap.Create; BMP.pixelformat:=pf32bit; BMP.width:=JPG.width; BMP.height:=JPG.height; BMP.canvas.draw(0,0,JPG); // Copy the JPEG onto the Bitmap
Width :=BMP.Width; Height :=BMP.Height; SetLength(Data, Width*Height);
//======================================== iBMP:=bmp.CreateIntfImage;
For H:=0 to Height-1 do Begin Line :=iBMP.GetDataLineStart(Height-H-1); // flip JPEG For W:=0 to Width-1 do Begin c:=Line^ and $FFFFFF; // Need to do a color swap Data[W+(H*Width)] :=(((c and $FF) shl 16)+(c shr 16)+(c and $FF00)) or $FF000000; // 4 channel. inc(Line); End; End; FreeAndNil(iBMP); //======================================
BMP.free; JPG.free;
Texture :=CreateTexture(Width, Height, GL_RGBA, addr(Data[0])); result :=TRUE; end;
{------------------------------------------------------------------} { Loads 24 and 32bpp (alpha channel) TGA textures } {------------------------------------------------------------------} function LoadTGATexture(Filename: String; var Texture: GLuint; LoadFromResource : Boolean): Boolean; var TGAHeader : packed record // Header type for TGA images FileType : Byte; ColorMapType : Byte; ImageType : Byte; ColorMapSpec : Array[0..4] of Byte; OrigX : Array [0..1] of Byte; OrigY : Array [0..1] of Byte; Width : Array [0..1] of Byte; Height : Array [0..1] of Byte; BPP : Byte; ImageInfo : Byte; end; TGAFile : File; bytesRead : Integer; image : Pointer; {or PRGBTRIPLE} Width, Height : Integer; ColorDepth : Integer; ImageSize : Integer; I : Integer; Front: ^Byte; Back: ^Byte; Temp: Byte;
// used for loading from resource ResHandle : THandle; MemHandle : THandle; MemStream : TMemoryStream; ResPtr : PByte; ResSize : Longint; begin GetMem(Image, 0); if LoadFromResource then // Load from resource begin try ResHandle := FindResource(hInstance, PChar(copy(Filename, 1, Pos('.', Filename)-1)), 'TGA'); if ResHandle = 0 then begin //MessageBox(0, PChar('File not found in resource - ' + Filename), PChar('BMP Texture'), MB_OK); result :=FALSE; Exit; end; MemHandle := LoadResource(hInstance, ResHandle); ResPtr := LockResource(MemHandle); MemStream := TMemoryStream.Create; ResSize := SizeOfResource(hInstance, ResHandle); MemStream.SetSize(ResSize); MemStream.Write(ResPtr^, ResSize); FreeResource(MemHandle); MemStream.Seek(0, 0);
MemStream.ReadBuffer(TGAHeader, SizeOf(TGAHeader)); // FileHeader // Only support uncompressed images if (TGAHeader.ImageType <> 2) then { TGA_RGB } begin Result := False; // MessageBox(0, PChar('Couldn''t load "'+ Filename +'". Compressed TGA files not supported.'), PChar('TGA Resource Error'), MB_OK); Exit; end;
// Don't support colormapped files if TGAHeader.ColorMapType <> 0 then begin Result := False; //MessageBox(0, PChar('Couldn''t load "'+ Filename +'". Colormapped TGA files not supported.'), PChar('TGA Resource Error'), MB_OK); Exit; end;
// Get the width, height, and color depth Width := TGAHeader.Width[0] + TGAHeader.Width[1] * 256; Height := TGAHeader.Height[0] + TGAHeader.Height[1] * 256; ColorDepth := TGAHeader.BPP; ImageSize := Width*Height*(ColorDepth div 8);
if ColorDepth <> 24 then begin Result := False; CloseFile(TGAFile); //MessageBox(0, PChar('Couldn''t load "'+ Filename +'". Only 24 bit TGA files supported.'), PChar('TGA Resource Error'), MB_OK); Exit; end;
GetMem(Image, ImageSize);
MemStream.ReadBuffer(Image^, ImageSize); MemStream.Free; except // MessageBox(0, PChar('Error reading file from resource - ' + Filename), PChar('TGA Resource'), MB_OK); result :=FALSE; Exit; end; end else begin if FileExists(Filename) then begin AssignFile(TGAFile, Filename); Reset(TGAFile, 1);
// Read in the bitmap file header BlockRead(TGAFile, TGAHeader, SizeOf(TGAHeader));
// Only support uncompressed images if (TGAHeader.ImageType <> 2) then { TGA_RGB } begin Result := False; CloseFile(tgaFile); //MessageBox(0, PChar('Couldn''t load "'+ Filename +'". Compressed TGA files not supported.'), PChar('TGA File Error'), MB_OK); Exit; end;
// Don't support colormapped files if TGAHeader.ColorMapType <> 0 then begin Result := False; CloseFile(TGAFile); // MessageBox(0, PChar('Couldn''t load "'+ Filename +'". Colormapped TGA files not supported.'), PChar('TGA File Error'), MB_OK); Exit; end;
// Get the width, height, and color depth Width := TGAHeader.Width[0] + TGAHeader.Width[1] * 256; Height := TGAHeader.Height[0] + TGAHeader.Height[1] * 256; ColorDepth := TGAHeader.BPP; ImageSize := Width*Height*(ColorDepth div 8);
if ColorDepth <> 24 then begin Result := False; CloseFile(TGAFile); // MessageBox(0, PChar('Couldn''t load "'+ Filename +'". Only 24 bit TGA files supported.'), PChar('TGA File Error'), MB_OK); Exit; end;
GetMem(Image, ImageSize);
// Read in the image BlockRead(TGAFile, image^, ImageSize, bytesRead); if bytesRead <> ImageSize then begin Result := False; CloseFile(TGAFile); // MessageBox(0, PChar('Couldn''t read file "'+ Filename +'".'), PChar('TGA File Error'), MB_OK); Exit; end; end; end;
// TGAs are stored BGR and not RGB, so swap the R and B bytes. for I :=0 to Width * Height - 1 do begin Front := Pointer(Integer(Image) + I*3); Back := Pointer(Integer(Image) + I*3 + 2); Temp := Front^; Front^ := Back^; Back^ := Temp; end;
Texture :=CreateTexture(Width, Height, GL_RGB, Image); Result :=TRUE; FreeMem(Image); end;
{------------------------------------------------------------------} { Determines file type and sends to correct function } {------------------------------------------------------------------} function LoadTexture(Filename: String; var Texture : GLuint; LoadFromRes : Boolean) : Boolean; begin if copy(filename, length(filename)-3, 4) = '.bmp' then LoadBMPTexture(Filename, Texture, LoadFromRes); if copy(filename, length(filename)-3, 4) = '.jpg' then LoadJPGTexture(Filename, Texture, LoadFromRes); if copy(filename, length(filename)-3, 4) = '.tga' then LoadTGATexture(Filename, Texture, LoadFromRes); end;
end.
|
Разрешаю наложение текстуры: glEnable(GL_TEXTURE_2D); Загруэаю: LoadBMPTexture('fon.bmp',tex1,false); После этого, когда надо накладываю текстуру так: glBindTexture(GL_TEXTURE_2D, tex1); И ничего не происходит, объект как был белый, так и остался белый. сам объект: Код | glBegin(GL_POLYGON); glColor3f(1.0, 1.0, 1.0); glTexCoord2f(-0.5, -0.5); glVertex2f(-0.5,-0.5); glTexCoord2f(-0.5, 0.5); glVertex2f(-0.5,0.5); glTexCoord2f(0.5, 0.5); glVertex2f(0.5,0.5); glTexCoord2f(0.5, -0.5); glVertex2f(0.5,-0.5); glEnd();
|
Ничего понять не могу, раньше использовал функцию glBindTexture на си, и проблем небыло. Если загружать картинку перед каждым рендером объекта, то текстура хорошо наклдываеться, но грузить ткстуру постоянно, это явно не айс. Текстура имеет размер 512*512, но пробовал и текстуры размером 32*32
|