PDA

View Full Version : How does this line stipple stuff work?!!!



Y-T
07-17-2000, 02:54 PM
this code is from the OpenGL SuperBible and i was wondering what the hell this line stipple code like "0x00" means and how it works... thanx,
Y-T

// PStipple.c
// OpenGL SuperBible, Chapter 4
// Demonstrates OpenGL Polygon Stippling
// Program by Richard S. Wright Jr.

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <math.h>


// Define a constant for the value of PI
#define GL_PI 3.1415f

// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;

// Bitmap of camp fire
GLubyte fire[] = { 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xc0,
0x00, 0x00, 0x01, 0xf0,
0x00, 0x00, 0x07, 0xf0,
0x0f, 0x00, 0x1f, 0xe0,
0x1f, 0x80, 0x1f, 0xc0,
0x0f, 0xc0, 0x3f, 0x80,
0x07, 0xe0, 0x7e, 0x00,
0x03, 0xf0, 0xff, 0x80,
0x03, 0xf5, 0xff, 0xe0,
0x07, 0xfd, 0xff, 0xf8,
0x1f, 0xfc, 0xff, 0xe8,
0xff, 0xe3, 0xbf, 0x70,
0xde, 0x80, 0xb7, 0x00,
0x71, 0x10, 0x4a, 0x80,
0x03, 0x10, 0x4e, 0x40,
0x02, 0x88, 0x8c, 0x20,
0x05, 0x05, 0x04, 0x40,
0x02, 0x82, 0x14, 0x40,
0x02, 0x40, 0x10, 0x80,
0x02, 0x64, 0x1a, 0x80,
0x00, 0x92, 0x29, 0x00,
0x00, 0xb0, 0x48, 0x00,
0x00, 0xc8, 0x90, 0x00,
0x00, 0x85, 0x10, 0x00,
0x00, 0x03, 0x00, 0x00,
0x00, 0x00, 0x10, 0x00 };


// Called to draw scene
void RenderScene(void)
{
// Clear the window
glClear(GL_COLOR_BUFFER_BIT);

// Save matrix state and do the rotation
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);

// Begin the stop sign shape,
// use a standard polygon for simplicity
glBegin(GL_POLYGON);
glVertex2f(-20.0f, 50.0f);
glVertex2f(20.0f, 50.0f);
glVertex2f(50.0f, 20.0f);
glVertex2f(50.0f, -20.0f);
glVertex2f(20.0f, -50.0f);
glVertex2f(-20.0f, -50.0f);
glVertex2f(-50.0f, -20.0f);
glVertex2f(-50.0f, 20.0f);
glEnd();

// Restore transformations
glPopMatrix();

// Flush drawing commands
glutSwapBuffers();
}


// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

// Set drawing color to red
glColor3f(1.0f, 0.0f, 0.0f);

// Enable polygon stippling
glEnable(GL_POLYGON_STIPPLE);

// Specify a specific stipple pattern
glPolygonStipple(fire);
}

void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;

if(key == GLUT_KEY_DOWN)
xRot += 5.0f;

if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;

if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;

if(key > 356.0f)
xRot = 0.0f;

if(key < -1.0f)
xRot = 355.0f;

if(key > 356.0f)
yRot = 0.0f;

if(key < -1.0f)
yRot = 355.0f;

// Refresh the Window
glutPostRedisplay();
}


void ChangeSize(int w, int h)
{
GLfloat nRange = 100.0f;

// Prevent a divide by zero
if(h == 0)
h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);

// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Polygon Stippling");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();

return 0;
}

Relic
07-18-2000, 09:40 AM
Actually there is no line stipple in the program. It's polygon stipple.
Fairly easy: OpenGL supports a 32x32 bit stipple pattern which is window relative and can be used to cull fragments. If polygon stipple is enabled, pixels are only set if the bit in the pattern is 1. This can be used to generate simple (ugly) transparency effects without the need for depth sorting the transparent polygons.
Or it can be used to fade in or out drawings by changing the stipple pattern in some orderer dither technique.

Line stipple is also available with a 16 bit 1D pattern.

Try the program you provided and you'll see the polygon stipple drawing some pattern with a single primitive.

ngill
07-18-2000, 12:39 PM
Originally posted by Y-T:
this code is from the OpenGL SuperBible and i was wondering what the hell this line stipple code like "0x00" means and how it works... thanx,
Y-T

I think this is what it is... 0x?? represents a number in hex... a 16 bit number... (1 byte = 8 bits)

you need to convert each of the ?? into binary. So for example if you have 0x54 it would be this in binary:

5=101
4=100

//add the zeroes to turn it into a 16 bit

5=0000000000000101
4=0000000000000100

so your pattern comes out to be:

0x54 =

00000000000001010000000000000100

so by having a lot of these you can make a pattern...

by the way the letters are
a=10
b=11
c=12
d=13
e=14
f=15
(no g because it's base 16, thus next would come 0, then 1...)

Hope this helps

Marc
07-19-2000, 06:44 AM
Errr..., ngill, 0x?? is 8bit !!!!! the first ? is the first 4 bits !!! and the second ? is the last 4 bits. 16 bits would be 0x????.

ngill
07-19-2000, 07:20 AM
heh heh, should've noticed that...

yeah the highest ? = f(15) thus:

1111... 4 bit gives (2^4)16 possibilites, not 16 1s and 0s, whoopsa.

I guess I wasn't thinking...