[bmx] Projection Matrix by _Skully [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

Title : Projection Matrix
Author : _Skully
Posted : 1+ years ago

Description : Projection Matrix - Scaling your game on the fly
"Yan to GfK to me to here :)


Code :
Code (blitzmax) Select
[code]
Type TVirtualGraphics
Global virtualWidth, virtualHeight
Global xRatio!, yRatio!

Function Set(width=640, height=480, scale#=1)
TVirtualGraphics.virtualWidth = width
TVirtualGraphics.virtualHeight = height
TVirtualGraphics.xRatio! = width / Double(GraphicsWidth())
TVirtualGraphics.yRatio! = height / Double(GraphicsHeight())

?Win32
Local dxVer:Byte
Local D3D7Driver:TD3D7Max2DDriver = TD3D7Max2DDriver(_max2dDriver)
Local D3D9Driver:TD3D9Max2DDriver = TD3D9Max2DDriver(_max2dDriver)

If TD3D7Max2DDriver(_max2dDriver) <> Null
dxVer = 7
EndIf
  If TD3D9Max2DDriver(_max2dDriver) <> Null
dxVer = 9
EndIf

If dxVer <> 0 'dx driver was set, otherwise its GL
Local matrix#[] = [2.0 / (width / scale#), 0.0, 0.0, 0.0,..
0.0, -2.0 / (height / scale#), 0.0, 0.0,..
0.0, 0.0, 1.0, 0.0,..
-1 - (1.0 / width), 1 + (1.0 / height), 1.0, 1.0] ',scale#]

Select dxVer
Case 7
D3D7Driver.device.SetTransform(D3DTS_PROJECTION, matrix)
Case 9
D3D9Driver._D3DDevice9.SetTransform(D3DTS_PROJECTION, matrix)
End Select
Else
?
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, width / scale:Float, height / scale:Float, 0, - 1, 1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
?Win32
EndIf
?
End Function

Function MouseX()
Return (BRL.PolledInput.MouseX() * TVirtualGraphics.xRatio!)
End Function

Function MouseY()
Return (BRL.PolledInput.MouseY() * TVirtualGraphics.yRatio!)
End Function
End Type
[/code]

Comments :


QuickSilva(Posted 1+ years ago)

 Any chance of a usage example?Does this method cause the same sort of performance hit that simply scaling the display up using the SetScale command does? For example, if I set my game to 2 times the scale my framerate drops by half using SetScale. Would this method do the same? [Edit - OK it does.]Also, I`m getting a driver not found for the DX9 one. I assume that you have to find this yourself? Maybe this should be mentioned for new users?Other than that what are the main reasons for using this method over a simple SetScale on all game objects?Jason.


GW(Posted 1+ years ago)

 <a href="http://code.google.com/p/max2ddx9/" target="_blank">http://code.google.com/p/max2ddx9/</a>


_Skully(Posted 1+ years ago)

 Usage is simple enough
T:TVirtualGraphics=new TVirtualGraphics
T.Set(800,600)
done...


Nigel Brown(Posted 1+ years ago)

 Compiler Error Identifier 'TD3DpMax2DDriver' not foundJust downloaded and installed the above max2ddx9 link, yet get this error?


_Skully(Posted 1+ years ago)

 Did you compile it?You could always comment out the DX9 stuff to test it if thats what your looking to do.


GfK(Posted 1+ years ago)

 <div class="quote"> Usage is simple enoughT:TVirtualGraphics=new TVirtualGraphicsT.Set(800,600) </div>Its not even THAT complex - you don't need to create a tVirtualGraphics object as it uses Global/Function rather than Field/Method:Graphics 1024,768 'or any user-defined res
tVirtualGraphics.Set(800,600)



therevills(Posted 1+ years ago)

 Heres a quick example (without DX9):Graphics 1024,768,0 'or any user-defined res
tVirtualGraphics.Set(800,600)
AutoMidHandle 1
Global BMX01IMG:TImage = LoadImage("C:BlitzMaxsamplesirdiemiscfilmclipmediaB-Max.png",FILTEREDIMAGE|DYNAMICIMAGE)

While Not AppTerminate()
Cls
DrawLine 0,0,TVirtualGraphics.virtualWidth,TVirtualGraphics.virtualHeight
DrawImage BMX01IMG,TVirtualGraphics.virtualWidth/2,TVirtualGraphics.virtualHeight/2
DrawText "Virtual Width: "+TVirtualGraphics.virtualWidth,10,10
DrawText "Virtual Width: "+TVirtualGraphics.virtualHeight ,10,20
controls()
Flip
Wend

Function controls()
If KeyHit(KEY_1)
tVirtualGraphics.Set(640,480)
EndIf
If KeyHit(KEY_2)
tVirtualGraphics.Set(800,600)
EndIf
If KeyHit(KEY_3)
tVirtualGraphics.Set(1024,768)
EndIf
End Function


Type TVirtualGraphics
Global virtualWidth, virtualHeight
Global xRatio!, yRatio!

Function Set(width=640, height=480, scale#=1)
TVirtualGraphics.virtualWidth = width
TVirtualGraphics.virtualHeight = height
TVirtualGraphics.xRatio! = width / Double(GraphicsWidth())
TVirtualGraphics.yRatio! = height / Double(GraphicsHeight())

?Win32
Local dxVer:Byte
Local D3D7Driver:TD3D7Max2DDriver = TD3D7Max2DDriver(_max2dDriver)

If TD3D7Max2DDriver(_max2dDriver) <> Null
dxVer = 7
EndIf


If dxVer <> 0 'dx driver was set, otherwise its GL
Local matrix#[] = [2.0 / (width / scale#), 0.0, 0.0, 0.0,..
0.0, -2.0 / (height / scale#), 0.0, 0.0,..
0.0, 0.0, 1.0, 0.0,..
-1 - (1.0 / width), 1 + (1.0 / height), 1.0, 1.0] ',scale#]

Select dxVer
Case 7
D3D7Driver.device.SetTransform(D3DTS_PROJECTION, matrix)
End Select
Else
?
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, width / scale:Float, height / scale:Float, 0, - 1, 1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
?Win32
EndIf
?
End Function

Function MouseX()
Return (BRL.PolledInput.MouseX() * TVirtualGraphics.xRatio!)
End Function

Function MouseY()
Return (BRL.PolledInput.MouseY() * TVirtualGraphics.yRatio!)
End Function
End Type
Press 1 for (640,480)Press 2 for (800,600)Press 3 for (1024,768)


Chroma(Posted 1+ years ago)

 No one has been able to get this to work in DX9 yet.


_Skully(Posted 1+ years ago)

 Didn't Mark build this into BMax?


xlsior(Posted 1+ years ago)

 Yup, 1.37's 'virtual resolution' commands are using the projection matrix. [/i]