Results 1 to 10 of 13

Thread: Create OpenGL Texture from TBitmap

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Best i can come up with is to pitch tBitmap in the trash, it does NOT provide the information you need to do this in a reasonable manner.

    Code:
    function createGlTextureFromResource(h:hInstance; resId:integer):glInt;
    var
    	textureStream:tResourceStream;
    	fileHeader:BITMAPFILEHEADER;
    	infoHeader:BITMAPINFOHEADER;
    	data:pointer;
    	dataSize:longint;
    begin
    	ShowMessage('Opening Bitmap File');
    	textureStream:=tResourceStream.createFromId(h,resID,RT_RCDATA);
    	
    	showMessage('Loading Headers');
    	// Get header information
    	textureStream.read(fileHeader,sizeOf(fileHeader));
    	textureStream.read(infoHeader,sizeOf(infoHeader));
    	
    	if not(infoHeader.biBitcount=24) then begin
    		MessageBox(
    			0,
    			PChar('Error - Attempt to load non 24 bit texture'),
    			PChar('BMP Unit'),
    			MB_OK
    		);
    		exit;
    	end;
    	
    	// 24 bit means we don't need the palette, so let's skip it!
    	
    	textureStream.seek(fileHeader.bfOffBits);
    	
    	dataSize:=(
    		infoHeader.width *
    		infoHeader.height * 
    		infoHeader.biBitCount div 8
    	);
    	getMem(data,dataSize);
    
    	showMessage('Loading bitmap data');
    	// Get the actual pixel data
    	textureStream.read(data^,dataSize);
    	
    	showMessage('Assigning bitmap to OpenGL texture');
    	
    	createGlTextureFromResource:=createTexture(
    		infoHeader.biWidth,
    		infoHeader.biHeight,
    		GL_RGB,
    		data
    	);
    	
    	showMessage('Unallocating bitmap memory');
    	
    	freeMem(dData,dataSize);
    	
    	showMessage('Bitmap resource to OpenGL texture complete');
    end;
    Is roughly how I'd go about it. Note you have to pass the application hInstance to it... untested, so I'm not sure if you should be forcing it to be rcdata, or rcBitmap -- I suspect the latter may have 'issues'... also not sure if you need to rgbswap or not. (sdl_image's load_img seems to handle that for me)... if you do need the swap, rather than wasting time flipping it manually, why not send gl_BGR to the texture loader?
    The accessibility of a website from time to time must be refreshed with the blood of designers and owners. It is its natural manure

  2. #2

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •