Dark Bit Factory & Gravity

PROGRAMMING => Other languages => Blitz => Topic started by: Pixel_Outlaw on March 27, 2009

Title: [BMAX + OpenGL] Stipple polygons.
Post by: Pixel_Outlaw on March 27, 2009
I've needed a way to draw stippled polygons. I'm not quite sure how to translate the example in my book into the Bmax equivalent.

Now in the example they specify a stipple pattern like this: (strait out of the Red Book p.58 in mine)

Code: [Select]
GLubyte halftone[] = {
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};

How does this translate to Bmax? Do I need a 2 dimensional array or a single dimensional array? The book mentions that they can be 32x32 byte arrays. The syntax for the array is what throws me, no size value is given nor is there any dimensional value given.
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: hellfire on March 27, 2009
The stipple-pattern is a 32x32 *bit*mask (there's no option about the size), so glPolygonStipple (http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/polygonstipple.html) expects a pointer to 1024 bits (or 128 bytes or 32 dwords).
2d-arrays are laid out linearly in memory (it's a 1d-array and the compiler secretly computes the correct address for you), so it doesn't really matter how you declare your data - OpenGL just requires the address of the first value.
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: TinDragon on March 27, 2009
You would use a 1 dimension byte array,so you could go something like

Local halftone:Byte[]=[Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85),Byte(170), Byte(170), Byte(170), Byte(170), Byte(85), Byte(85), Byte(85), Byte(85)]


I think that would do it, then you can pass the array to the required function, I have put what I think the values are for the hex OxAA and Ox55. I will be honest I havent tried the polygon stipple feature, so it might need some tweaking :)

For anyone who's wondering what were talking about you can check out the online Version 1 "red book" (http://www.glprogramming.com/red/chapter02.html) this is from chapter 2 about half way down the webpage :)

[Edit] Added a complete array for the halftone that works but I think there's likely an easier way  :D
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: zawran on March 27, 2009
You could also do something like this to create the byte array:

Code: [Select]
Local halftone:Byte[128]

For Local i:Int = 0 To 127
ReadData halftone[i]
Next

DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: TinDragon on March 27, 2009
Ah there we go, new there would be an easier way :D and why the hell did i change the hex to decimal  ???
Probably shouldn't post when I just got up :D
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: Pixel_Outlaw on March 27, 2009
Thanks for cleaning that up gents. ;)
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: Pixel_Outlaw on March 27, 2009
I just thought I;d try it with one of the basic Nehe tutorials.

I must be doing something wrong here...

Code: [Select]
GLGraphics 640,480

glEnable(GL_POLYGON_STIPPLE)
glEnable GL_DEPTH_TEST 'Enables Depth Testing

glMatrixMode GL_PROJECTION 'Select The Projection Matrix
glLoadIdentity 'Reset The Projection Matrix

glFrustum -0.1, 0.1,-0.1, 0.1, 0.1, 100.0 'Setup The Projection Matrix Frustum

glMatrixMode GL_MODELVIEW 'Select The ModelView Matrix
glLoadIdentity 'Reset The ModelView Matrix

Local rtri:Float 'Angle For The Triangle ( New )
Local rquad:Float 'Angle For The Quad     ( New )


'stipple patern
Local HALFTONE:Byte[128]

For Local i:Int = 0 To 127
ReadData halftone[i]
Next

DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55
DefData $AA, $AA, $AA, $AA, $55, $55, $55, $55

glPolygonStipple(HALFTONE)

While Not KeyHit(KEY_ESCAPE)

glClear GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT 'Clear The Screen And The Depth Buffer

glLoadIdentity 'Reset The ModelView Matrix
glTranslatef -1.5,0.0,-6.0 'Move Left 1.5 Units And Into The Screen 6.0

glRotatef rtri,0.0,1.0,0.0 'Rotate The Triangle On The Y axis ( New )

glBegin GL_TRIANGLES 'Drawing Using Triangles
glColor3f 1.0, 0.0, 0.0 'Set The Color To Red
glVertex3f 0.0, 1.0, 0.0 'Top
glColor3f 0.0,1.0,0.0 'Set The Color To Green
glVertex3f -1.0,-1.0, 0.0 'Bottom Left
glColor3f 0.0,0.0,1.0 'Set The Color To Blue
glVertex3f 1.0,-1.0, 0.0 'Bottom Right

glEnd 'Finished Drawing The Triangle

glLoadIdentity() 'Reset The Current Modelview Matrix
glTranslatef 1.5,0.0,-6.0 'Move Right 1.5 Units And Into The Screen 6.0
glRotatef rquad,1.0,0.0,0.0 'Rotate The Quad On The X axis ( New )

glColor3f 0.5,0.5,1.0 'Set The Color To Blue One Time Only
glBegin GL_QUADS 'Draw A Quad

glVertex3f -1.0, 1.0, 0.0 'Top Left
glVertex3f  1.0, 1.0, 0.0 'Top Right
glVertex3f  1.0,-1.0, 0.0 'Bottom Right
glVertex3f-1.0,-1.0,0.0 'Bottom Left

glEnd 'Finished Drawing The Quad

rtri:+0.7 'Increase The Rotation Variable For The Triangle ( New )
rquad:-0.55 'Decrease The Rotation Variable For The Quad     ( New )

Flip

Wend
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: zawran on March 27, 2009
This is how it looks on my computer:

(http://zac-interactive.dk/temp/trianglesquare.png)

What do you expect it to look like? It looks like it halftones the objects, was that not the expected result?
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: Pixel_Outlaw on March 27, 2009
This is really odd! My computer shows no effect.

card is Ati radeon Xpress 1100
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: TinDragon on March 27, 2009
works fine here as well, this is ment to be a standard opengl feature so it might be driver related, I think both me and zawran have nvidia cards but it should work on an ati :S 
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: Pixel_Outlaw on March 28, 2009
This really sucks, this was going to be a key component of my project.  :boxer:
In a perfect world everyone would have the same computer.
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: TinDragon on March 28, 2009
I dont understand why it isnt working, polygon stipple isnt anything super modern, I was under the impression that all basic opengl functions up to 1.1 were in even the software drivers that MS had on windows, really strange its not showing on an modern chipset, even if it is a laptop.  ???

Will give the code a spin on my other pc, that has ati card x1650, and post back what I get.

[EDIT] Well it runs fine on my xp machine with the radeon card so thats good new in one sense but bad news in another as it looks like its something at your end but what i have no idea what, maybe some weird driver error or something.

Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: Clyde on March 29, 2009
What exactly are Stipple Polygons?
Those screenshots look like to me as having Gourad Shading
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: zawran on March 29, 2009
Quote
What exactly are Stipple Polygons?

Follow the link that Jon have in his post and you will get a good explaination. Its about halfway down the page.
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: Jim on March 30, 2009
You could maybe try
glEnable(GL_POLYGON_STIPPLE)
after glPolygonStipple()
though it shouldn't make any difference.

Jim
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: Pixel_Outlaw on March 31, 2009
Ok Jim tried that. No effect.Looks like ATI really dropped the ball on my end.
Title: Re: [BMAX + OpenGL] Stipple polygons.
Post by: hellfire on March 31, 2009
You could use a texture which holds the stipple-pattern in the alpha-channel,
apply texture-coordinates in screenspace (eg. using a projection matrix or glTexGen)
and use alpha-test to reject the "unstippled" fragments.