Using the Projection Matrix to zoom in on a screen

BlitzMax Forums/BlitzMax Programming/Using the Projection Matrix to zoom in on a screen

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Hmmm...A quick hack (didn't bother sorting out the origin) and it appears to be working alright here...
Strict

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 D3D7Driver:TD3D7Max2DDriver = TD3D7Max2DDriver(_max2dDriver)
			
		If D3D7Driver
			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#]
			
			D3D7Driver.device.SetTransform(D3DTS_PROJECTION, matrix)
		Else
	? 
		glMatrixMode(GL_PROJECTION)
		glLoadIdentity()
		glortho(0, width / scale#, height / scale#, 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

'----------------------------------------------- TEST -----------------------------------------------

'SetGraphicsDriver GLMax2DDriver()
SeedRnd MilliSecs()

Local index = Rand(0, CountGraphicsModes() - 1)
Local screenW, screenH, screenD, screenR

GetGraphicsMode(index, screenW, screenH, screenD, screenR) 

Graphics screenW, screenH, screenD
TVirtualGraphics.Set(1024, 768, 0.5)

HideMouse
AutoMidHandle True

Local image:TImage = LoadImage(getenv_("BMXPATH") + "/docs/src/bmax120.png")
Assert image Else "Oi...What's happend to yer docs?"

Local x = Rand(0, TVirtualGraphics.virtualWidth - 1)
Local y = Rand(0, TVirtualGraphics.virtualheight - 1) 
Local xInc = Rand(1, 4)
Local yInc = Rand(1, 4)
Local scale#

SetClsColor 0, 0, 100

Repeat
	x :+ xInc
	If x >= TVirtualGraphics.virtualWidth
		x = TVirtualGraphics.virtualWidth - 1
		xInc = -xInc 
	ElseIf x < 0
		x = 0
		xInc = -xInc
	EndIf
	
	y :+ yInc
	If y >= TVirtualGraphics.virtualHeight
		y = TVirtualGraphics.virtualHeight - 1
		yInc = -yInc 
	ElseIf y < 0
		y = 0
		yInc = -yInc
	EndIf
	
	scale# = 1 + (Cos(MilliSecs() / 10.0) * 0.5)
	
	Cls
	SetTransform(Sin(MilliSecs() / 40.0) * 180.0, scale#, scale#)
	DrawImage image, x, y
	
	SetTransform(0, TVirtualGraphics.xRatio!, TVirtualGraphics.xRatio!) 
	DrawText screenW + " x " + screenH + " x " + screenD, 12, 12
	
	Plot TVirtualGraphics.MouseX(), TVirtualGraphics.MouseY()

	Flip
Until KeyHit(KEY_ESCAPE)

End


...as expected, it seems to work fine with the extra LoadIdentity() too?

??

[edit]
Aha...The GL section of code I've previously posted differs from the code I have here (0 rather than -1 for nearVal), not sure how that happened but fixed wherever I found it. :o/

I'll stop talking to myself now. ;o)
[edit]