2D in OpenGL

BlitzMax Forums/OpenGL Module/2D in OpenGL

tin(Posted 2008) [#1]
hi all,

i have a question.
now BlitzMax can do 2D games, with simple command sets.
is it possible to do 2D Games in BlitzMax OpenGL ?
what's the different.?

Thanks


tonyg(Posted 2008) [#2]
Do you mean using Setgraphicsdriver glmax2ddriver()? This will allow all the normal max2d commands but in a GL context.


ImaginaryHuman(Posted 2008) [#3]
I think what you're asking is whether you can directly code 2D graphics using OpenGL calls rather than using Max2D?

And of course yes you can. Your 2D graphics are really 3-dimensional pairs of triangles (quads) which just happen to be directly facing the camera and usually also has no perspective applied. Some people who think in terms of 3D being `normal` like to call it billboarding since if you are really using 3D objects it's an `extra process` to rotate an object to always face the camera. Makes you wonder if the moon is a billboard since it always shows the same face to the earth ;-D

So anyway, you can start off by using SetGraphicsDriver GLMax2dDriver(), which will let you still use Max2D commands at the same time as your own OpenGL calls. The only catch is that Max2D is basically a layer `on top of` OpenGL and it has its own assumptions about the current `state` of the OpenGL layer - ie which options are switched on/off and set to what. So if you do OpenGL calls alongside Max2D calls you may well confuse it and cause it to break. An alternative then is that before you start to do your own OpenGL calls, you save all likely-to-be-influencing-Max2D state onto some kind of stack or storage area, then do your GL calls, then restore the state back to how it was before you continue with Max2D calls. That will work.

A perhaps more efficient way is to throw Max2D out the window by using GLGraphics() whereby you then can no longer use any Max2D calls at all. You are then required to write all of your own OpenGL code including loading textures, uploading them to OpenGL, defining traingles/quads, specifying texture coordinates and colors and all that jazz. You certainly can do that to create all of the functionality of Max2D, because that's exactly what Max2D does for you. Or you can even do it in some different way, whatever you like. With OpenGL you get full freedom to decide how your graphics engine will work.


tin(Posted 2008) [#4]
so. basically Max2D is built on-top of OpenGL to make game programmer easy access to graphic library? If it is i should not dig into OpenGL for 2D games, wright?


ImaginaryHuman(Posted 2008) [#5]
Yes Max2D is on top of OpenGL to make it easier to do simple things like draw an image. DrawImage is a lot easier than setting up all the OpenGL commands.

It's up to you whether you write your own OpenGL code or not, but basically if you do you will be replacing much of what Max2D does.


tonyg(Posted 2008) [#6]
Max2D is supplied with drivers for DX7 (default) and OGL. The drivers are set using the setgraphicsdriver command. Once set all the Bmax native commands can be used but within either a DX or GL context.
Check d3d7max2d.mod and glmax2d.mod for the command implementation for both.


DavidSimon(Posted 2008) [#7]
local drawX:float,drawY:float
local drawWidth:float,drawHeight:float


glBegin(GL_QUADS)

glTexCoord2f 0,0
glVertex2f drawX,drawY

glTexCoord2f 1,0
glVertex2f drawX+drawWidth,drawY


glTexCoord2f 1,1
glVertex2f drawX+DrawWidth,drawy+drawheight


glTexCoord2f 0,1
glVertex2f drawx,drawy+drawheight


glEnd()

to help you visualize it. of course that does not cover the gl code to load and bind a texture, that would simply render a solid rect.(Whose color could be changed by calling glColor4f)

In short, always roll your own renderer. Even if you don't need to, do it. You'll learn faster, end up with more flexible engines(max2d is a newbie engine, whichever way you look at it. Not one "Wow" feature, at least in my eyes)

If you need more help, speak to my mate dave. he's a wizard at this coding lark. me, I just make him tea.