How do you do 2D drawing in BMAX and OGL?

BlitzMax Forums/OpenGL Module/How do you do 2D drawing in BMAX and OGL?

Clyde(Posted 2005) [#1]
I have no idea where to start when it comes to drawing and plotting in 2D with OpenGL. I can open and setup a graphics window, as per the numerous excellent tutorials and examples on OpenGL with 3D stuff.

I'd like to know how to being and do this, as I have many things I'd like to try and do (mainly special effects) and see if they are any faster then using WritePixel and Pixel Maps. As I was under the impression that with BMAX everything was going to be much much quicker then previous flavours.

If you could help get me started with this, then that would be magic. One of the pitfulls I am gonig to get really perplexed over is converting Alpha. Red, Green and Blue colours into converting with open GL ones 0.0 - 1.0.

I take it I'd need to have an array of somekind to store image infromation, etc.

This is how Im currently doing things with WPF. And would like very much to do with OpenGL, to see if it is quicker. It's the main reason I bought BMAX. As im finding MAX2D very slow at doing effects.




Hugest of thanks,
THUMBZ


N(Posted 2005) [#2]
After reading your post twice, I have determined that I am unable to figure out what it is you're asking.


Clyde(Posted 2005) [#3]
How to Do drawing using OpenGL, in 2D rather than 3D.
And also manipulate colours for effects etc.

Must appologize, as im very tired when I wrote it.


N(Posted 2005) [#4]
Instead of going into an elaborate explanation stating that it's doable but not fast or a good idea, I'll just tell you that you can't do it.

Really, though, it's not a good idea to be trying to do 'true' 2D stuff with OpenGL last I checked.


eizdealer(Posted 2005) [#5]
Well actually it's easy. You'd just have to setup the Projection matrix with an orthographical view. To do this, use:
glMatrixMode GL_PROJECTION
glLoadIdentity
gluOrtho2D(0, GRAPHICSWIDTH, GRAPHICSHEIGHT, 0)
glMatrixMode GL_MODELVIEW
glLoadIdentity
glDisable GL_DEPTH_TEST 'you may want to disable depth testing so you can define the render order exactly

After having set the matrices you can draw any primitve directly to the screen coordinates using glBegin, glEnd and glVertex2f with screen coordinates. A little example:
glBegin GL_LINES
glVertex2f X1#, Y1# 'draws a line from P1 to P2
glVertex2f X2#, Y2# 'note that these coords are screen coords
glEnd

You can use many primitives with glBegin, just search for pictures with "gl primitives" in Google and it will show you some. And have a look into the Redbook for explanations of how to use them.

And by the way, if you have any more OpenGL regarding questions, could you post them all in one of your threads?


Clyde(Posted 2005) [#6]
@NC:
Instead of going into an elaborate explanation stating that it's doable but not fast or a good idea, I'll just tell you that you can't do it.

Really, though, it's not a good idea to be trying to do 'true' 2D stuff with OpenGL last I checked.

Whys that, seeing as MAX2D uses OpenGL to draw with?

---------------------------------------------------------

I knew this would stumble me, any ideas on how to convert RGB values into readable OpenGL ones?

Normal RGB 255 colour ranges, and HEX $000000 - $ FFFFFF. Would be terrific.


Thanks for the help,
THUMBZ


col(Posted 2005) [#7]
can you not do this:

OpenglRed:float = 1.0 / (255.0 * red)
OpenglGreen:float = 1.0 / (255.0 *green)
OpenglBlue:float = 1.0 / (255.0 * blue)


This would do things in a 'component' wise order but it should work?

[EDITED] You should use RndFloat() for random numbers between 0.0 and 1.0. You should also change the SeedRnd value everytime your code is run or the random number generator commands will generate the same value. ie at the top of your code use

SeedRnd Millisecs() 'Generate a random seed based on Millisecs (which is always changing)



Clyde(Posted 2005) [#8]
I'll give it a bash. Thanks Colonel.


Clyde(Posted 2005) [#9]
Thanks Col!

Here's my progress so far:


And as you'll see with using your own images with the example, that the colours are still a tad wrong.

Big Thanks,
THUMBZ


col(Posted 2005) [#10]
I couldn't get this code to work!!

gluOrtho2D(left,right,bottom,top) -<you set it up as left,right,top,bottom !!
should be gluOrtho(0,xres,0,yres)

then I get a blank screen. Doesn't seem to be reading the image I have here.[EDIT] image I used was too big.


col(Posted 2005) [#11]
Ok I got it working. I assume you have it working but this colors are wrong, so you need to do this:

Local Red2#= 1.0 - 1.0 / (255.0 * Red)
Local Grn2#= 1.0 - 1.0 / (255.0 * Grn)
Local Blu2#= 1.0 - 1.0 / (255.0 * Blu)


Clyde(Posted 2005) [#12]
Thanks Col.

I've Just tried it out and the colours are still off.
I wonder if it's do to with the Red, Grn and Blu before converting to OpenGL Colours?


tonyg(Posted 2005) [#13]
Local Red2#= (1.0 / 255.0) * Red
Local Grn2#= (1.0 / 255.0) * Grn
Local Blu2#= (1.0 / 255.0) * Blu
e.g....
Local Red2#= (1.0 / 255.0) * (Red/2)
Local Grn2#= (1.0 / 255.0) * (Grn/2)
Local Blu2#= (1.0 / 255.0) * (Blu/2)
is half colour
P.S the way to think about it is...
255 = 1.0
1 = 1.0/255
RED = (1.0/255)*RED


col(Posted 2005) [#14]
doh. Of course.

That was what I meant ;) ...........hhmmmm


Clyde(Posted 2005) [#15]
Greatest!

Thanks for your help.
And I'll post back in a little while, if I cant get the next step in Blurring the image working.


Clyde(Posted 2005) [#16]
Here's an attempted fader, as you'll witness the colours dont really fade very well.

And I've not had much luck with doing a blurring effect based on this approach. Any ideas?


Humble Thankyous,
THUMBZ


xlsior(Posted 2005) [#17]
Here's an attempted fader, as you'll witness the colours dont really fade very well.


I put some fader routines in the Code Archives the other day.


Clyde(Posted 2005) [#18]
Are they OpenGL ones?
Will take another look.

I've done a normal MAX2D starfield, and an OpenGL one. And I'm highly surprised by the difference in Frame Rate.

OPEN GL METHOD: 482 FPS


BMAX 2D METHOD: 60 FPS


I would really love some help in making the OpenGL method make the starfield blur. Many thanks in that department.

Edit: I think I may need to alter the way it stores the x and y positions and colours. Let me know, it a blur is doable, or how'd you'd go about it. Im having great fun with GL.

Thanks,
THUMBZ


Extron(Posted 2005) [#19]
I'm not surprised, Flip command use a delay. ;)


Clyde(Posted 2005) [#20]
Im running into allsorts of problems, most especially understanding OpenGL and BMAX. So with one last and final attempt, I came up with this which is pathetic really. I need lots of help, as BMAX is alot lot different then previous versions. Documention and stuff is pretty lapse at the momemt.

If any of these trials and tribulations of mine are of any use to somebody, then the experimenting was worth it.



Just like to say a big thankyou to everyone who've helped me with my dabbling into BMAX and OpenGL.

Thanks Heaps,
THUMBZ


Takuan(Posted 2005) [#21]
What should that code do?
It crashes on my old FX5200
BMAX 2D METHOD: 60 FPS works at 61 FPS;-)


Stoop Solo(Posted 2005) [#22]
Cool, some useful info to be had there. So, thanks.

Oh so very new to the BlitzMax scene, and though there is much to like, I confess I miss the lockedbuffer-writepixelfast performance of BlitzPlus, which although not uber-fast itself, was plenty fast enough to do lots of really cool particle stuff, whilst retaining respectable system requirements. Doing tests using pixmaps, which seem to be the fastest way of producing similar functionality yielded results that were generally quite a bit quicker than both (B+) Plot and Writepixel, but still (understandably) blown away by the lockedbuffer performance.

Now, after some perusal of this thread (and the contents of the bmax modules), I have a couple of functions for performing pixel writing operations that seem on the whole to run only 5-10% slower than a locked writepixelfast, with the added bonus of having alpha. Particle extravagansa, here I come!

Of course, this is only OpenGL. DirectX seems to be a lot more convoluted, and I haven't been able to create equivalent functionality in the DX environment yet.


LarsG(Posted 2005) [#23]
wow.. this thread was over 7 months old.. :p


Stoop Solo(Posted 2005) [#24]
Useful threads don't go off with age. :P

I have been in possession of BMax for roughly 72 hours. I have much ground to cover. Admittedly, I was hoping a search would return more recent threads. Surely I can't be the only person hoping to do lots of 2D pixel particle effects?


ImaginaryHuman(Posted 2005) [#25]
Plotting your own particles (vertexes) with glVertex is going to be a lot faster than using Plot because you remove the overhead.

With regards to the grayscale thing, a simple way to do it is to convert the image to a one-channel texture, like Red only, where Red=Red+Green+Blue /3

Upload the one-channel texture to the gfx card as a GL_ALPHA texture. Then draw it, using a blend mode, which takes the alpha channel of the source data and expands it into each color component - ie if alpha is 200, then RG and B all become 200. That turns it into a grayscale representation. Probably faster overall than doing the whole conversion in main memory.


Stoop Solo(Posted 2005) [#26]
Yes indeed, thus far, glvertex is doing a splendid job, and in general, it seems to be running as quickly as writepixelfast did, even with a color and alpha function. Very useful. Can't be bothered trying to get d3d to do the same anymore, ridiculous cruddy API. I've decided to stick with OpenGL, it's proven far more cooperative, plus that makes everything ready to cross the great platform divide.