VBGamer |
|
Re: Saving Rendered Scene as Bitmap Sylvain Dupont (0 replies, 0 views) (2001-Mar-24) This is a code to "take a screenshot" of the current Backbuffer scene in DirectX8 : I know it's quite hard but it's working.
'//////////////////////////////////////////////////////
' SCREEN SHOT CODE FROM TRUEVISION8 ENGINE
'//////////////////////////////////////////////////////
Public Type BITMAPINFOHEADER '40 bytes
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Public Type BITMAPHEADER
magic As Integer
Size As Long
Reserved As Integer
Reserved2 As Integer
offset As Long
End Type
Public Type BITMAPHEADER2
magic As Integer
Size1 As Integer
Size2 As Integer
Reserved As Integer
Reserved2 As Integer
offset As Integer
offset2 As Integer
End Type
Sub ScreenShot(file As String)
'Take a screen shot of the current scene
Dim Surface As Direct3DSurface8, Surf2 As Direct3DSurface8
Dim Data() As Byte
Set Surface = Device.GetBackbuffer(0, D3DBACKBUFFER_TYPE_MONO)
Dim w As Long, h As Long
Dim Desc As D3DSURFACE_DESC
Surface.GetDesc Desc
Set Surf2 = Device.CreateImageSurface(Desc.width, Desc.height, Desc.Format)
Device.CopyRects Surface, ByVal 0, 0, Surf2, ByVal 0
Surf2.GetDesc Desc
ReDim Preserve Data(Desc.Size) As Byte
Dim RECT As D3DLOCKED_RECT
Surf2.LockRect RECT, ByVal 0, 0
DXCopyMemory Data(0), ByVal RECT.pBits, Desc.Size
Surf2.UnlockRect
Dim MemoryCount As Long, y As Long, temp As Byte
Dim data2() As Byte
ReDim data2(Desc.Size) As Byte
For y = 0 To Desc.height - 1
DXCopyMemory data2(y * RECT.Pitch), Data((Desc.height - y - 1) * RECT.Pitch), RECT.Pitch
Next y
Dim offset As Long, offset2 As Long
offset = 0
offset2 = 0
If Desc.Format = D3DFMT_R5G6B5 Then
'16bits modes
ReDim Data3(Desc.Size * 3 / 2) As Byte
For i = 0 To Desc.Size / 2 - 1
Data3(offset + 2) = data2(offset2 + 1) And &HF8
Data3(offset + 1) = (data2(offset2 + 1) And &H7) * 32 + (data2(offset2) And &HE0) \ 8
Data3(offset + 0) = (data2(offset2) And &H1F) * 8
offset = offset + 3
offset2 = offset2 + 2
Next i
Else
'32bits modes, alpha is unsignifiant
ReDim Data3(Desc.Size * 3 / 4) As Byte
For i = 0 To Desc.Size / 4 - 1
Data3(offset + 2) = data2(offset2 + 1)
Data3(offset + 1) = data2(offset2 + 2)
Data3(offset + 0) = data2(offset2 + 3)
offset = offset + 3
offset2 = offset2 + 4
Next i
End If
MemoryCount = UBound(Data3) + 54
Dim bm As BITMAPHEADER
Dim bm2 As BITMAPINFOHEADER
bm.magic = 19778
bm.Size = MemoryCount
bm.offset = 54
bm2.biBitCount = 24
bm2.biWidth = Desc.width
bm2.biHeight = Desc.height
bm2.biSize = 40
bm2.biPlanes = 1
bm2.biSizeImage = MemoryCount - 54
bm2.biCompression = 0
bm2.biXPelsPerMeter = 50
bm2.biYPelsPerMeter = 50
Close 1
Open file For Binary As 1
Put #1, , bm
Put #1, , bm2
Put #1, , Data3()
Close
'End If
Erase Data()
Set Surface = Nothing
End Sub
Hope this helps
Regards
Sylvain
TrueVision8 programmer. |