07.04.2024, 07:11
|
Новичок
|
|
Регистрация: 17.08.2010
Сообщения: 74
Репутация: 518
|
|
Заменить загрузку BMP на картинку из Image
Доброго времени суток.
Нашёл библиотеку (UrickGL, U3Dpolys, GL, GLU, Glut ) в которой просто можно работать OpenGL и решил поизучать её и вставить в проект. Столкнулся с "неудобностью" библиотеки - текстуры надо грузить из файла во время работы программы. Можно ли их хранить в ресурсах или допустим загрузить в TImage и оттуда уже грузить картинку в текстуру?При этом я могу в неё сразу рисовать и печатать текст модифицируя её!! Я же могу и имею доступ к битовому пространству картинки в Image.. Но вот не соображу как это совместить передачей в текстуру. Привожу оригинальный код создания текстуры с загрузкой по имени файла xxxx.bmp.
Код:
Function TTexture.LoadTexture(st:string):shortint; // сейчас так
//Function TTexture.LoadTextureIMG(IMG:TImage):shortint; // хотелось бы так
Const
MB=19778;
type
ptybuff=^tybuff;
ptybuffa=^tybuffa;
tybuff=array[1..128000] of record
r:byte;
g:byte;
b:byte;
end;
tybuffa=array[1..128000] of record
r:byte;
g:byte;
b:byte;
a:byte;
end;
var
f:file;
Buffer2b:ptybuff;
Buffer2:pointer;
Buffer3b:ptybuffa;
Buffer3:pointer;
i:longint;
{$A-}
header:record
FileType:Word; {always MB}
size:longint;
Reserved1,
Reserved2:word; {reserved for future purposes}
offset:longint; {offset to image in bytes}
end; {header}
BMPInfo:record
size:longint; {size of BMPinfo in bytes}
width:longint; {width of the image in pixels}
height:longint; {height of the image in pixels}
planes:word; {number of planes (always 1)}
Colorbits:word; {number of bits used to describe color in each pixel}
compression:longint; {compression used}
ImageSize:longint; {image size in bytes}
XpixPerMeter:longint; {pixels per meter in X}
YpixPerMeter:longint; {pixels per meter in Y}
ColorUsed:longint; {number of the color used їїї???}
Important:longint; {number of "important" colors}
end; {info}
{$A+}
begin
if Not FileExists(st) then
begin
result:=-1; {file not found}
exit;
end;
assignfile(f,st);
reset(f,1);
blockread(f,header,sizeof(header));
blockread(f,BMPinfo,sizeof(BMPinfo));
if header.FileType <> MB then
begin
result:=-2; {file type is not BMP}
exit;
end;
header.size:=header.size-sizeof(header)-sizeof(BMPinfo);
getmem(buffer2,header.size);
getmem(buffer3,header.size*4 div 3);
buffer2b:=ptybuff(buffer2);
buffer3b:=ptybuffA(buffer3);
Blockread(f,buffer2^,header.size);
for i:=1 to header.size div 3 do
begin
buffer3b^[i].r:=buffer2b^[i].b;
buffer3b^[i].g:=buffer2b^[i].g;
buffer3b^[i].b:=buffer2b^[i].r;
buffer3b^[i].a:=envblendcolor[3]; {obtain blend alpha from envblendcolor.alpha}
end;
closefile(f);
GlNewList(MyList,gl_compile);
glpixelstorei(gl_unpack_alignment,4); {OpenGL 1.0 ignores this one}
glpixelstorei(gl_unpack_row_length,0);
glpixelstorei(gl_unpack_skip_rows,0);
glpixelstorei(gl_unpack_skip_pixels,0);
{for GLteximage2D the parameters are:
gl_Texture_2d,
level of detail (0 unless using mipmapped textures)
components: 3 for RGB, 4 for RGBA 1 for indexed 256 color
width, height
border: width of the border, between 0 and 2.
Format: gl_color_index, GL_RGB, GL_rgbA, GL_luminance are the most used
type of the data for each pixel
pointer to image data}
glenable(GL_BLEND);
glblendfunc(gl_src_alpha, gl_one_minus_src_alpha);
gltexImage2d(gl_texture_2d,0,4,BMPinfo.width,BMPinfo.height,0,gl_rgba,gl_unsigned_byte,buffer3);
glendlist;
result:=0; {no error}
end;
имеем вызов:
Код:
Cube1.texture.LoadTexture('texture3.bmp');
хотелось бы:
Код:
Cube1.texture.LoadTextureIMG(image1);
Понятно что уже нет необходимости проверять битность, есть файл или нет, проверять заголовок, надо просто как-то сразу обратиться в область данных и передать этот массив.. туплю - не соображу.
|