How can I use Indiepath's ProjMatrix mod w/1.24?

BlitzMax Forums/BlitzMax Programming/How can I use Indiepath's ProjMatrix mod w/1.24?

SofaKng(Posted 2007) [#1]
I'm trying to have resolution independent graphics (so the user can choose any resolution they want and it will still look correct so I was told to use Indiepath's "ProjMatrix" module.

Well, I've found a copy of it for free (modules.indiepath.com), but it doesn't seem to work for v1.24 of BlitzMax. I think the problem is that "PrimaryDevice" is no longer valid which is what the module relied upon.

Any help is appreciated...


popcade(Posted 2007) [#2]
The new dxgraphics module will cause problems with any apps
that depended on low level details of the old one.

These will need to be rewritten, using the following:

D3D7GraphicsDriver().DirectDraw7()
D3D7GraphicsDriver().Direct3D7()
D3D7GraphicsDriver().Direct3DDevice7()


Replace "PrimaryDevice" to "D3D7GraphicsDriver().Direct3DDevice7()", try again...


sswift(Posted 2007) [#3]
Unfortunately, it looks like that is the module I was thinking of, and it says it does not support rotation or scaling, which is a huge limitation.


Grey Alien(Posted 2007) [#4]
bummer. Oh well I'm stickin got one res for my games anyway.


Yan(Posted 2007) [#5]
This should still work with 1.24...

http://www.blitzbasic.com/Community/posts.php?topic=61822#690808


sswift(Posted 2007) [#6]
Does that version work with scaled and rotated images? (Including scaled on only one axis?)


TartanTangerine (was Indiepath)(Posted 2007) [#7]
Unfortunately, it looks like that is the module I was thinking of, and it says it does not support rotation or scaling, which is a huge limitation.

You read it wrong, it does not support viewport rotation. Image scaling and rotation is fine.

Think of this as your camera (ala Blitz3D) on an orthographic world.


Grey Alien(Posted 2007) [#8]
Image scaling and rotation is fine.
YAY


ImaginaryHuman(Posted 2007) [#9]
There's nothing stopping you from changing the projection matrix to position the camera anywhere in 3D space with any orientation, including scaling, rotation, translation, etc I have it working in my engine. The OpenGL code is simple, something like:

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0,ScrWidth*XScale,ScrHeight*YScale,0,-1,1)
glViewport(0,0,ScrWidth,ScrHeight)
glTranslatef(XOffset,YOffset,0)
glRotatef(Angle,0,0,GL_TRUE)
glMatrixMode(GL_MODELVIEW)

This would set the camera to look at the game world at any scale and at any angle and at an horizontal or vertical offset. Then you would just draw your game objects at a given size and position and just move the camera around to look at different parts. You'd then only need to rotate the individual objects if they need to rotate to different angles individually.

I'm not sure if all of Max2D would work fine with a rotated/scaled/panned camera, maybe collision detection might be off? Who knows, but it's not a difficult thing.


TartanTangerine (was Indiepath)(Posted 2007) [#10]
Max2D would be fine since it's making it's calculations independent of the projection matrix. Remember the matrix is simply your view on the world - period.

Btw, projection matrix rotation in directX is a little harder than OGL.


SofaKng(Posted 2007) [#11]
So to summarize...

Indiepath's code doesn't work in 1.24.

I should use Yan's code which will accomplish the same thing.

Is this right?

Does Yan's code support both OpenGL and DirectX? (it seems like it does)

Thanks!!


TartanTangerine (was Indiepath)(Posted 2007) [#12]
DOn't forget that if you use Yans code you'll need to transform the mouse co-ordinates.


SofaKng(Posted 2007) [#13]
Does your code automatically do that?

If so, can you fix it to make it work with BlitzMax v1.24? =)


SofaKng(Posted 2007) [#14]
Btw, IndiePath, do you still sell your RenderToTexture module?

If so, can you read my topic here: http://www.blitzbasic.com/Community/posts.php?topic=66756

I think your module does what I need, but I'm not sure...

Thanks


popcade(Posted 2007) [#15]
I want to buy Lights2D module BTW...


TartanTangerine (was Indiepath)(Posted 2007) [#16]
Updated but not tested : http://modules.indiepath.com/forum/viewtopic.php?p=55#55

Use the translate mouse Method to do the stuff.

With regard to your post I'd recommend the Textured Polygon module and not the Render2TExture.


SofaKng(Posted 2007) [#17]
Thanks for your reply and translation!

However, I'm trying to compile your translated ProjMatrix module and this line causes an error:

      If D3D7GraphicsDriver().Direct3DDevice7().device = Null Then 

The error is:

Identifier "device" not found.


Also... your Textured Polygon module suffers from the same "PrimaryDevice" problem...

Any chance you could fix that one too? :)


TartanTangerine (was Indiepath)(Posted 2007) [#18]
kill the "device" part and see if that works.

TExtured Poly will have the same problem but you probably know how to fix that already :P


SofaKng(Posted 2007) [#19]
Ok, I've removed all of the .device parts and I made it down to this line:

         Local res=D3D7GraphicsDriver().Direct3DDevice7().backbuffer.Lock(Null,sdesc,DDLOCK_WAIT|DDLOCK_READONLY,Null) 


Identifier 'backbuffer' not found


=(


SofaKng(Posted 2007) [#20]
EDIT: Ignore this post. I've gotten TexturedPoly to work!

However I still can't get ProjMatrix to work...

(and I'm going to try to get RenderToTexture to work too)


Yan(Posted 2007) [#21]
I'm not sure if you got ProjMatrix.mod to work, so here's the full Virtual Graphics Type...

Here's a textured poly function, if you need it (I think there are a couple of these in the code archives too)...


SofaKng(Posted 2007) [#22]
Nice, thanks!!


ImaginaryHuman(Posted 2007) [#23]
Oh yea, I forgot the textured polygon obvious solution. You could draw your own polygon with texture coordinates pretty easily.


SofaKng(Posted 2007) [#24]
Am I correct in thinking that I will need one polygon for every pixel in the width of my terrain?

Each polygon would then be 1 pixel wide, by whatever the terrain height is at that point. Then, I would texture that polygon using UV coordinates according to where I'm at in the landscape?

At first I thought I would just have one single polygon and the texture would "sit on top of it", but either I'm doing something wrong or this simply isn't the case.

Also, I think that maybe IndiePath's texture polygon code is broken or I don't know how to use it. However, Yan's code seems to work.

(Hopefully I can get IndiePath's Render-To-Texture code to work because then I could simply render my constructed landscape to an image and save it for future processing...)


tonyg(Posted 2007) [#25]
For Indie's ProjectionMatrix 'bacbkbuffer' issue can't you just remove the 'flip' method?


TartanTangerine (was Indiepath)(Posted 2007) [#26]
For Indie's ProjectionMatrix 'bacbkbuffer' issue can't you just remove the 'flip' method?

That's old code, remove it.


MGE(Posted 2007) [#27]
AngelDaniel - "There's nothing stopping you from changing the projection matrix to position the camera anywhere in 3D space with any orientation, including scaling, rotation, translation, etc I have it working in my engine."

ooohh..that would be fun! Do you have any working code (hopefully compatible with both DX/GL) Or can Yan's code do this? If so, it's not jumping out at me. Thanks!


Muttley(Posted 2007) [#28]
I've come across a problem with using a Projection Matrix with DirectX.

If the screen/window is minimised and the maximised again the Projection Matrix seems to be lost, in that when my PM is set to 1024x768 and my display is 1280x1024, when the screen is brought back all the graphics are drawn in a 1024x768 box in the top left corner. Everything works fine using the OpenGL driver however.

I've tried capturing the EVENT_APPRESUME event in a hook and re-creating the PM, but that has no effect. From some DirectX articles I've been reading while searching for a solution, I'm guessing (and it is a wild stab in the dark that may be totally wrong) I may need to call D3DXMatrixIdentity, but I have no idea how to do this from Max.

Anybody got any ideas?

Cheers

Muttley


ImaginaryHuman(Posted 2007) [#29]
Muttley, possibly your problem could be that you need to redefine the ViewPort, not the projection matrix, after you've changed resolution. If it is still using the old viewport specification then it's basically going to resize your graphics output to fit into an area the size of the previous resolution. A simple call to SetViewport with the width and height of the new resolution, after you've switched to it, should fix that - if that's the problem. And if that is the problem, it means that your projection matrix IS kept but because your viewport is also kept it's interfering with the output. Changing the projection matrix to scale your graphics according to the aspect ratio needs to also be accompanied by a redefinition of the viewport when the size/resolution changes, otherwise it doesn't know how many pixels the output should render to.

MGE Developer, sorry I missed your question way back over a month ago. It is possible to apply operations like scale, rotate, translate to the projection matrix which effectively does move the camera to whatever size and position you like, but there are some websites which warn against using the projection matrix for camera positioning. In OpenGL if you change the projection matrix to position the camera somewhere other than looking down the z axis at 0,0 (at the center of the screen) then later on when you want to do things like lighting and other calculations the camera's position will not be taken into account because its position is stored in the projection matrix which is separate from where the pipeline looks for its information to do lighting etc. You are supposed to calculate the inverse of the scale/translate/rotate that you want to do to the camera, and instead move the *model* itself (the modelview matrix) to position it in front of a stationary camera. ie move the world in front of the camera, don't move the camera in front of the world, and make sure you do it to the model matrix.

The function of the projection matrix is supposed to be for defining the way in which each pixel in the viewport is projected in the z direction, to create either an orthographic projection or a perspective projection, and to define a camera lens with a given field of view. But you are not supposed to really use it for anything else - move/rotate/scale your model to make other changes to what is being viewed.


Muttley(Posted 2007) [#30]
Muttley, possibly your problem could be that you need to redefine the ViewPort, not the projection matrix, after you've changed resolution. If it is still using the old viewport specification then it's basically going to resize your graphics output to fit into an area the size of the previous resolution. A simple call to SetViewport with the width and height of the new resolution, after you've switched to it, should fix that - if that's the problem. And if that is the problem, it means that your projection matrix IS kept but because your viewport is also kept it's interfering with the output. Changing the projection matrix to scale your graphics according to the aspect ratio needs to also be accompanied by a redefinition of the viewport when the size/resolution changes, otherwise it doesn't know how many pixels the output should render to.


Hi IH, thanks for the suggestions.

I've now tried to change the viewport when the app is resumed, but it makes no difference. I'm not sure if I made it clear in my post, but I'm not changing the resolution at all. This is purely down to someone Alt-Tabbing away from the game in full-screen mode and then bring the app back to to foreground again.

Using the OpenGL driver it works perfectly with no modifications at all.

Cheers

Muttley


MGE(Posted 2007) [#31]
Hi Muttley, I'm sorry I didn't see this earlier, I had the same problem but found a simple solution which works for me on my test machines. I make a call to Graphics() to re-init the screen and then I re-init the proj matrix as usual right afterwards.

So after AppSuspended() returns TRUE I try to grab the screen and go into a loop....

Before I go into a While AppSuspened() Loop, I first try to grab the screen into a pixmap, if this returns NULL then I know the device is gone and AFTER the AppSuspended() returns FALSE I now know to call the Graphics() again followed by the ProjMatrix() reset.

I hope this helps. ;)


Muttley(Posted 2007) [#32]
Hi, thanks for that.

Unfortunately I've tried that (or similar, I'm not doing the grabpixmap thing). The re-creation of the Graphics when resuming from a suspended works OK in windowed mode, but not fullscreen where I just get a crash when the screen is being recreated, along with the following error message.

"DXERROR err=UNKNOWN:-2005532101 571"

This is very odd.


MGE(Posted 2007) [#33]
Muttley - download my latest demo, run it in DirectX mode full screen in any of the available resolutions, hit alt-tab and see if it comes back ok. Weird how OGL seems to be failing this on my computer but I think I know why, in any event, see if the DirectX restore is working for you.
http://jgoware.com/mge/downloads/asteroidblast.zip


Muttley(Posted 2007) [#34]
OK, well you're code works a treat, so it's obviously me doing something stupid. ;)

I wonder if it's because I'm using an Event Hook? You couldn't possible post a snippit of your code could you? It would be most appreciated as this is really beginning to annoy me. :D


Muttley(Posted 2007) [#35]
Sorted.

It turned out it was due to me trying to reset everything in my event hook code. It was obviously getting multiple resume events and getting its knickers in a twist.

I moved the code to my main loop and everything works nicely now. :D


MGE(Posted 2007) [#36]
Did you ever get my email?


hub(Posted 2007) [#37]
Hi !
it's a newbie questions but :
is it easy to add the indiepath projection matrix module into an existant project to have resolution independent graphics into your game ? is it just a line of code to set the screen size or you must modified all your project (movement, rotation, ... you know as delta time ) ?
Thanks !


Big&(Posted 2007) [#38]
@hub:
You just add it to your project, make an initialization function call and that's it.


hub(Posted 2007) [#39]
Cool ! Thanks.

Have you the plain bmx file. Could you diffuse your source with it ? (as my game is freeware, my source will be open to public).

and where to find the latest version ?

thx.


Muttley(Posted 2007) [#40]
@MGE - Nope, I didn't. I've just noticed that my account had one of my old addresses though and that one gets chock full of spam (50-60 a day), so maybe I just missed it. Never mind, all fixed now.

@hub - I rolled my own projection matrix module, but the indiepath one is similar and you just have to make one function call to set it up after you create your graphics device. Then you can basically forget about it and just use you projection matrix resolution for all your draw routines. The one thing to be aware of is if you need to read the mouse position you need to scale the result to your projection matrix resolution. I just added a ProjectMouse function to my module to handle this for me.

If you want a copy of mine, let me know.

Cheers

Muttley


hub(Posted 2007) [#41]
If you want a copy of mine, let me know.


Yes. Muttley you can send me a mail to hubert.bayre at wanadoo.fr (please put 'zigwigwis' inside your message object to evit my spam box !).
Many thanks for this !


MGE(Posted 2007) [#42]
Or better yet, just add it to this thread! ;)


hub(Posted 2007) [#43]
i've tested the yan code and it works very well here.


MGE(Posted 2007) [#44]
What's "Yan Code" ? My code failed to restore on an older card so I'm looking for alternatives.


Grey Alien(Posted 2008) [#45]
If the screen/window is minimised and the maximised again the Projection Matrix seems to be lost
Well spotted, good job I read this.

@MGE:
I make a call to Graphics() to re-init the screen and then I re-init the proj matrix as usual right afterwards.
Cool, trying now. But what about this isse you had where it failed to restore on an older card? Ever resolve that?